8webit / wp_stem

为WordPress主题/插件开发者提供的轻量级包

该包的官方仓库似乎已不存在,因此该包已被冻结。

v1.0.1 2018-07-19 20:32 UTC

This package is not auto-updated.

Last update: 2022-04-02 15:24:47 UTC


README

摘要

wp_stem 是为WordPress主题/插件开发者提供的轻量级包。使用此包,您可以

  • 排序列表脚本
  • 创建文章类型或向其添加字段
  • 修改文章类型元信息

安装

composer require 8webit/wp_stem

设置

functions.php 文件

use _8webit\wp_stem\Stem;

Stem::init();

PostType 类

PostType::create($post_type_slug, $options)

  • $post_type_slug (字符串) (必需)

  • $options(数组) (可选) - 与 register_post_type() $args 参数的相同值。

  • extend_defaults(布尔值) (可选)

创建文章类型。

创建文章类型的示例

PostType::create('your_post_type_slug')

PostType::meta_box($option)

  • $options(数组) - meta_box 与 add_meta_box() 具有相同的参数,但请注意,add_meta_box() 需要必需的 callback,但在 wp_stem 中则不是。

注意: 注意函数链的顺序。

创建元信息框的示例

PostType::create('your_post_type_slug)->meta_box(array(//minimal paramters
    'id'        => 'your_id',   //required
    'title'    => 'your_title' // required
));

PostType::field($field)

  • $field
    • id (整型) (可选) - 将自动生成的id设置为 field_name_id
    • name (字符串) (必需)
    • label (字符串) (必需)
    • value (字符串) (可选) - 字段的默认值
    • type (字符串) (必需) - HTML5输入类型,textarea,wp_mediacolor(spectrum color picker)font(google fonts picker)
    • cloneable (布尔值) (可选)
    • 任何HTML属性

将字段添加到文章类型。这里必须使用函数链。必须在 PostType::add() 或 PostType::create() 函数之后使用。请参阅以下示例。

注意: 要将字段添加到文章类型,您必须首先创建 元信息框。要创建元信息框,请使用 meta_box() 函数。

最后添加字段

PostType::create('your_post_type_slug')-> meta_box(array(
    'id'        => 'your_id',
    'title'    => 'your_title'
))->field(array(
    'id'        => 'your_field_id',
    'name'      => 'your_field_name',
    'label'     => 'your field label',
    'value'     => 'your_field_default_value',
    'type'      => 'your_field_type'
    'cloneable' => true
));

注意: 当 $field 'type' 参数等于 'color' 时,将使用 spectrum color picker(而不是HTML5颜色选择器)。

添加单个可克隆字段

要添加单个可克隆字段,请使用 PostType::field() 并将 'cloneable' => true 传递,如示例所示

PostType::cloneable_group($group)

  • $group (数组)
    • group_id (int)(必需) - 字段组的id。用于检索字段元值。
    • fields (数组)(必需) - 与 PostType::field() 相同的参数
    • options (数组)(可选)
      • title (字符串) - 可克隆元框的标题
      • add_button (字符串) - 添加(克隆)按钮标签
      • remove_button (字符串) - 移除(克隆)按钮标签

将可克隆字段组添加到元框

添加组可克隆字段的示例

PostType::create('your_post_type_slug')-> meta_box(array(
        'id'        => 'your_id',
        'title'    => 'your_title'
    ))->cloneable_group(array(
        'your_group_cloneable_id',
        'fields' => array($field1,$field2,$field3 ...),
        'options => array(
            'title' => 'title of meta box group',
            'add_button' => 'add button label',
            'remove_button' => 'remove button label'
        )

    ));

PostType:add($arg)

  • $arg (int | string) - 帖子类型id、帖子类型短名或模板名称('template-example.php')

通过(已创建的)帖子类型、帖子id或页面模板添加字段

例如,让我们向帖子添加字段

PostType::add('post')-> meta_box(array(
    'id'        => 'your_id',
    'title'    => 'your_title'
))->field(array(
    'id'        => 'your_field_id',            // required
    'name'      => 'your_field_name',          // required
    'label'     => 'your_field_label',         // required
    'value'     => 'your_field_default_value', // required
    'type'      => 'your_field_type'           // required
    'cloneable' => 'is_cloneable_field'        // optional
));

通过短名自动完成字段

自动完成动作使用 jQuery 自动完成。

要自动完成的项基于 HTML 类,因此如果您想由用户短名自动完成字段,请添加 'class'=> 'autocomplete-user',如示例所示。另一个类是 'autocomplete-page'。

用户自动完成的示例

 PostType->field(array(
        'name'  => 'author_search',
        'label' => 'Select Author',
        'type'  => 'text',
        'placeholder' => 'Search Author By Username...',
        'class' => 'autocomplete-user'
        ));

元类

Meta::sync($post_id, $meta_key, $new_value = "", $old_value = "")

  • $post_id (int) (必需)
  • $meta_key (string) (必需)
  • $new_value (string) (可选)
  • $old_value (string) (可选)

使用一个函数创建、删除或更新帖子类型

更新值的示例

$old_value = Meta::get('lorem_ipsum');
$new_value = $_POST['lorem_ipsum'];

Meta::sync(get_the_ID(), 'lorem_ipsum', $new_value, $old_value);

Meta::get($meta_key, $post_id = '', $single = true)

  • $meta_key (string) (必需)
  • $post_id (int) (可选)
  • $single (int) (可选)

从 1.0.1 版本开始,HTML 会自动转义。

获取帖子元值,无需传递帖子id。如果未传递 $post_id,则将使用 get_the_ID() 函数检索当前id

队列脚本类

Enqueue_Script 类负责在用户或管理页面中加载脚本和样式。

脚本应位于 $your_theme_dir/assets/js

样式应位于 $your_theme_dir/assets/css

Enqueue_Script::enqueue($filename, $src = '', $in_footer = false, $deps = array(), $ver = false)

  • $filename (string) (必需) - 包含扩展名的文件名。例如:myscript.js(不仅仅是 myscript
  • $src (string) (可选) - 相对于主题文件夹的路径。如果没有提供,将使用默认文件结构模式("assets/[file_type]/[file_name].[file_type]")。
  • $in_footer (布尔值) (可选)
  • $deps (数组) (可选) - 依赖项的文件名
  • $ver (数组) (可选)

用户 页面通过包含扩展名的文件名队列脚本或样式。如果没有提供 src,则将使用默认文件结构模式("assets/[file_type]/[file_name].[file_type]")。使用与 wp_enqueue_script()wp_enqueue_style() 相同的参数。

示例

文件结构

mytheme
|---enqueuescripts
|   |---js
|   |   |   myscript.js
|   |
|   |---css
|       |   mystyle.css
|
|---acme_vendor_script
|   |---lorem_ipsum
|   |   |   vendor.js

*  *  *  *

|--- other folder and files
    Enqueue_Script::enqueue('myscript.js');
    Enqueue_Script::enqueue('mystyle.css');

    Enqueue_Script::enqueue('vendor.js', 'acme_vendor_script/lorem_ispum');

Enqueue_Script::admin_enqueue($filename, $src = '', $in_footer = false, $deps = array(), $ver = false)

有关参数,请参阅Enqueue_Script::enqueue()

管理页面上加载脚本

Enqueue_Script::admin_specific($wp_screen_ids = array())

  • $wp_screen_ids (数组) (必需)

注意:仅当用于链式调用或挂钩到已队列的最后脚本时才有效

admin_specific仅加载指定视图中的管理器已队列脚本

示例

Admin::enqueue('sciprt.js')->admin_specific(array(
                                'first_admin_screen_id',
                                'second_admin_screen_id',
                            ));
// Or

 Admin::enqueue('sciprt.js');
 Enqueue_Script::admin_specific(array(
                        'first_admin_screen_id',
                        'second_admin_screen_id'
                        );

Enqueue_Script::uri($pattern)

  • $pattern (字符串) (必需) - 主题根目录相关的文件模式。参见glob()

根据给定的模式返回单个或多文件完整路径。在需要加载哈希脚本或样式时非常有用

示例

文件结构

mytheme
|---acme_vendor_script
|   |---lorem_ipsum
|   |   |   as8du9asud982381.bundle.js

*  *  *  *

|--- other folder and files
$uri = Enqueue_Script::uri('acme_vendor_script/lorem_ipsum/*.bundle.js');

echo '<script type="text/javascript" src="'. $uri .'"></script>';

版权所有 (C) 2018 8webit.com

本程序是自由软件:您可以按照自由软件基金会发布的GNU通用公共许可证的条款重新分发和/或修改它,许可证版本为3,或者(根据您的选择)任何更新的版本。

本程序以希望它会有所帮助的目的进行分发,但没有任何保证;甚至没有关于适销性或特定用途的隐含保证。有关详细信息,请参阅GNU通用公共许可证。

您应该已经收到一份GNU通用公共许可证副本。如果没有,请参阅许可证