wp-xu/xu

此软件包已被弃用且不再维护。作者建议使用frozzare/wp-xu软件包。

WordPress 和 PHP 函数和类的集合

安装量: 3,462

依赖关系: 0

建议者: 0

安全: 0

星标: 6

关注者: 2

分支: 0

开放问题: 0

类型:wordpress-muplugin

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

This package is auto-updated.

Last update: 2020-03-17 14:05:42 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