wpfulcrum/post-type

Fulcrum 文章类型模块 - 简化自定义文章类型的创建。

3.0.1 2017-12-12 03:30 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:37:11 UTC


README

Build Status Latest Stable Version License

Fulcrum 自定义文章类型模块可以帮助你更轻松地将自定义文章类型添加到项目中。传递一个配置,它将为你处理剩余的工作。

特性

  • 注册由它为你处理。
  • 标签生成 - 当你不需要国际化时很有用。
  • 支持的功能构建器 - 当你想要其他插件和/或主题添加的支持时很有用。
  • 存储在 Fulcrum 容器中 - 添加时,将自动存储在容器中以供全局使用。
  • 列筛选、排序和配置都由它为你处理。

安装

使用此组件的最佳方式是通过 Composer

composer require wpfulcrum/post-type

依赖关系

此模块需要

  • 至少 PHP 5.6
  • WordPress 4.8+

配置自定义文章类型

与所有 Fulcrum 模块一样,此模块由配置驱动,作为 ModularConfig 设计模式的一部分。在你的主题/插件配置文件夹中,你将想要创建一个配置文件。以下是该文件的基本结构

<?php

$config = [
    'autoload' => true,
    'postType' => 'book',
    'config'   => [
        'postTypeArgs'   => [],
        'labelsConfig'   => [],
        'supportsConfig' => [],
        'columnsConfig'  => [],
    ],
];

/**
 * Arguments Configuration Parameters
 *
 * @see https://codex.wordpress.org/Function_Reference/register_post_type#arguments for more details.
 *
 * Don't configure the label, labels, or supports here.  Those are handled separately below.
 */
$config['config']['postTypeArgs'] = [
    'description'  => 'Books - example custom post type',
    'public'       => true,
    'hierarchical' => false,
    'show_in_rest' => true,
    'has_archive'  => true,
    'menu_icon'    => 'dashicons-book', // @link https://developer.wordpress.org/resource/dashicons
];

/**
 * Labels Builder - Configuration Parameters
 */
$config['config']['labelsConfig'] = [

    /***************************************************************************************************
     * When Your Plugin Doesn't Need Internationalization:
     *
     * By default, the label builder automatically builds the labels for you using the plural and singular
     * names you configure below.
     **************************************************************************************************/
    'useBuilder'   => true, // set to false when you need internationalization.
    'pluralName'   => 'Books',
    'singularName' => 'Book',

    /***************************************************************************************************
     * Specify the labels you want here.
     *
     * When not using the automatic builder (i.e. when 'useBuilder' is set to `false`), then you specify
     * all the custom labels here.
     *
     * If you are using the builder, any labels you specify here will overwrite what the builder generates.
     *
     * @see https://codex.wordpress.org/Function_Reference/register_post_type#labels for more details.
     **************************************************************************************************/
    'labels'       => [
        'name'               => _x('Books', 'post type general name', 'your-plugin-textdomain'),
        'singular_name'      => _x('Book', 'post type singular name', 'your-plugin-textdomain'),
        'menu_name'          => _x('Books', 'admin menu', 'your-plugin-textdomain'),
        'name_admin_bar'     => _x('Book', 'add new on admin bar', 'your-plugin-textdomain'),
        'add_new'            => _x('Add New', 'book', 'your-plugin-textdomain'),
        'add_new_item'       => __('Add New Book', 'your-plugin-textdomain'),
        'new_item'           => __('New Book', 'your-plugin-textdomain'),
        'edit_item'          => __('Edit Book', 'your-plugin-textdomain'),
        'view_item'          => __('View Book', 'your-plugin-textdomain'),
        'all_items'          => __('All Books', 'your-plugin-textdomain'),
        'search_items'       => __('Search Books', 'your-plugin-textdomain'),
        'parent_item_colon'  => __('Parent Books:', 'your-plugin-textdomain'),
        'not_found'          => __('No books found.', 'your-plugin-textdomain'),
        'not_found_in_trash' => __('No books found in Trash.', 'your-plugin-textdomain'),
    ],
];

/**
 * Post Type's Supported Features Builder - Configuration Parameters
 */
$config['config']['supportsConfig'] = [

    /***************************************************************************************************
     * When you want only these specific supports, configure them here.
     *
     * @see https://codex.wordpress.org/Function_Reference/register_post_type#supports for more details.
     **************************************************************************************************/
    'supports'           => [
        'title',
        'editor',
        'author',
        'thumbnail',
        'excerpt',
        'comments',
    ],

    /***************************************************************************************************
     * Want all or some of the supports that other plugins/theme add? Use this option instead.
     *
     * For example, let's say you want your custom post type to use the features that Yoast SEO, Genesis,
     * or Beans add.  This option uses the "post" post type as its base to grab all of them. Cool, right?!
     *
     * Configure the post type support features you want to include (set to `true`) or exclude (set to `false`).
     * You can add new ones too.  Then the builder handles it for you.
     **************************************************************************************************/
    'additionalSupports' => [
        'title'           => true,
        'editor'          => true,
        'author'          => false,
        'thumbnail'       => true,
        'excerpt'         => true,
        'trackbacks'      => false,
        'custom-fields'   => false,
        'comments'        => false,
        'revisions'       => false,
        'page-attributes' => false,
        'post-formats'    => false,
        'foo'             => true,
        'bar'             => true,
    ],
];

/**
 * Columns Handler - Configuration Parameters
 */
$config['config']['columnsConfig'] = [
    'columnsFilter' => [],
    'columnsData'   => [],
];

return $config;

使其生效

使用此模块有两种方法

  1. 使用完整的Fulcrum 插件
  2. 或者在不使用 Fulcrum 的情况下。

使用 Fulcrum

在 Fulcrum 中,你的插件是一个附加组件。在你的插件配置文件中,你将有一个名为 serviceProviders 的参数,其中列出你想使用的每个服务提供商。在这种情况下,你将使用 provider.post_type

例如,使用我们上面的 Book 配置,这将是这样配置的

	'serviceProviders' => [

		/****************************
		 * Custom Post Types
		 ****************************/
		'book.post_type' => array(
			'provider' => 'provider.post_type', // this is the service provider to be used.
			'config'   => BOOK_PLUGIN_DIR . 'config/post-type/book.php', // path to the book post type's configuration file.
		),
	],

Fulcrum 的附加模块处理插件激活和停用时的重写刷新。这样可以节省你的时间。

不使用 Fulcrum

不使用 Fulcrum,你需要实例化每个依赖项和 PostType。例如,你会这样做

$config = require_once BOOK_PLUGIN_DIR . 'config/post-type/book.php';  // path to the book post type's configuration file.

$supportsConfig                 = $config['config']['supportsConfig'];
$supportsConfig['hierarchical'] = $config['config']['postTypeArgs']['hierarchical'];

$postType = new PostType(
    $config['postType'],
    ConfigFactory::create($config['config']['postTypeArgs']),
    new Columns($config['postType'], ConfigFactory::create($config['config']['columnsConfig'])),
    new SupportedFeatures(ConfigFactory::create($supportsConfig)),
    new LabelsBuilder(ConfigFactory::create($config['config']['labelsConfig']))
);
$postType->register();    

你需要处理你的插件的激活和停用时的重写刷新。

贡献

所有反馈、错误报告和拉取请求都受欢迎。