cedaro/wp-plugin
Composer 安装命令:
composer require cedaro/wp-plugin
包简介
A base WordPress plugin library.
README 文档
README
Adds some structure to your WordPress plugins.
Requires PHP 8.0+.
Installation
To use this library in your project, add it to composer.json:
composer require cedaro/wp-plugin
Creating a Plugin
A plugin is a simple object created to help bootstrap functionality by allowing you to easily retrieve plugin information, reference internal files and URLs, and register hooks.
<?php /** * Plugin Name: Structure */ use Cedaro\WP\Plugin\PluginFactory; if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) { require( __DIR__ . '/vendor/autoload.php' ); } $structure = PluginFactory::create( 'structure' );
$stucture is an instance of Plugin and implements the PluginInterface, which provides a basic API to access information about the plugin.
Hook Providers
Related functionality can be encapsulated in a class called a "hook provider" that's registered when bootstrapping the plugin.
Hook providers allow you to encapsulate related functionality, maintain state without using globals, namespace methods without prefixing functions, limit access to internal methods, and make unit testing easier.
For an example, the Cedaro\WP\Plugin\Provider\I18n class is a default hook provider that automatically loads the text domain so the plugin can be translated.
The only requirement for a hook provider is that it should implement the HookProviderInterface by defining a method called register_hooks().
Hook providers are registered with the main plugin instance by calling Plugin::register_hooks() like this:
<?php $structure ->register_hooks( new \Cedaro\WP\Plugin\Provider\I18n() ) ->register_hooks( new \Structure\PostType\BookPostType() );
The BookPostType provider might look something like this:
<?php namespace Structure\PostType; use Cedaro\WP\Plugin\AbstractHookProvider; class BookPostType extends AbstractHookProvider { const POST_TYPE = 'book'; public function register_hooks() { $this->add_action( 'init', 'register_post_type' ); $this->add_action( 'init', 'register_meta' ); } protected function register_post_type() { register_post_type( static::POST_TYPE, $this->get_args() ); } protected function register_meta() { register_meta( 'post', 'isbn', array( 'type' => 'string', 'single' => true, 'sanitize_callback' => 'sanitize_text_field', 'show_in_rest' => true, ) ); } protected function get_args() { return array( 'hierarchical' => false, 'public' => true, 'rest_base' => 'books', 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => false, 'show_in_rest' => true, ); } }
Protected Hook Callbacks
In WordPress, it's only possible to use public methods of a class as hook callbacks, but in the BookPostType hook provider above, the callbacks are protected methods of the class.
Locking down the API like that is possible using the HooksTrait developed by John P. Bloch.
Plugin Awareness
A hook provider may implement the PluginAwareInterface to automatically receive a reference to the plugin when its hooks are registered.
For instance, in this class the enqueue_assets() method references the internal $plugin property to retrieve the URL to a JavaScript file in the plugin.
<?php namespace Structure\Provider; use Cedaro\WP\Plugin\AbstractHookProvider; class Assets extends AbstractHookProvider { public function register_hooks() { $this->add_action( 'wp_enqueue_scripts', 'enqueue_assets' ); } protected function enqueue_assets() { wp_enqueue_script( 'structure', $this->plugin->get_url( 'assets/js/structure.js' ) ); } }
Another example is the I18n provider mentioned earlier. It receives a reference to the plugin object so that it can use the plugin's base name and slug to load the text domain.
Classes that extend AbstractHookProvider are automatically "plugin aware."
License
Copyright (c) 2017 Cedaro, LLC
This library is licensed under MIT.
Attribution is appreciated, but not required.
cedaro/wp-plugin 适用场景与选型建议
cedaro/wp-plugin 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49.42k 次下载、GitHub Stars 达 41, 最近一次更新时间为 2015 年 11 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 cedaro/wp-plugin 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cedaro/wp-plugin 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 49.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 42
- 点击次数: 8
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-19