Requires PHP 5.5.9 and WordPress 4.4
Collection of useful WordPress and PHP functions and classes.
$ composer require frozzare/wp-xu
Some cached functions requires that wp_cache_delete_group
function exists to delete cache group.
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.
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();
MIT © Fredrik Forsmo