frozzare/wp-xu

Collection of useful WordPress and PHP functions and classes

v1.2.0 2016-01-18 17:40 UTC

This package is auto-updated.

Last update: 2024-04-17 22:30:54 UTC


README

Build Status codecov.io License

Requires PHP 5.5.9 and WordPress 4.4

Collection of useful WordPress and PHP functions and classes.

Install

$ composer require frozzare/wp-xu

Documentation

API reference docs

Some cached functions requires that wp_cache_delete_group function exists to delete cache group.

Models

With xu you can create models that you load from your theme (with xu_get_model) or any where, just use the class.

<?php

use Xu\Model\Model;

class Post extends Model {
  
  /**
   * Get model attributes.
   *
   * @return array
   */
  public function get_attributes() {
     return [
       'title' => $this->post->post_title
     ];
  }
}

In your template file:

$model = xu_get_model( 'post' );

echo $post->title;

The model class implements a lot of methods like to_array and to_json to convert the model to a array or JSON string.

Models Collection

A collection is a collection of one or more models. By reading the api reference docs you can find out which methods a collection has, first, last, filter, map, reject and where is some of the methods that exists.

A collection can be created in different ways:

use Xu\Model\Collection;

$collection = new Collection( [$model1, $model2] );
$collcetion = Post::collection( [$model1, $model2] );

class List extends Model {
  
  /**
   * Get all posts that exists on `post` post type.
   *
   * @return \Xu\Model\Collection
   */
  public function posts() {
    return static::collection( get_posts( 'post_type=post' ) );
  }
}

$collection = ( new List )->posts();

License

MIT © Fredrik Forsmo