冻硬/wp-xu

WordPress 和 PHP 函数和类别的集合

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

This package is auto-updated.

Last update: 2024-09-17 23:20:15 UTC


README

Build Status codecov.io License

需要 PHP 5.5.9 和 WordPress 4.4

WordPress 和 PHP 函数和类别的集合。

安装

$ composer require frozzare/wp-xu

文档

API 参考文档

一些缓存函数需要存在 wp_cache_delete_group 函数以删除缓存组。

模型

使用 xu,您可以创建模型,您可以从主题(使用 xu_get_model)或任何地方加载这些模型,只需使用类即可。

<?php

use Xu\Model\Model;

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

在您的模板文件中

$model = xu_get_model( 'post' );

echo $post->title;

模型类实现了很多方法,如 to_arrayto_json,用于将模型转换为数组或 JSON 字符串。

模型集合

集合是一个包含一个或多个模型的集合。通过阅读 API 参考文档,您可以找到集合有哪些方法,firstlastfiltermaprejectwhere 等是其中的一些方法。

集合可以通过不同的方式创建

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