ssnepenthe / wp-requirements
WordPress插件中声明依赖项的辅助工具。
0.1.0
2017-04-14 05:47 UTC
Requires
- php: >=5.3
This package is auto-updated.
Last update: 2024-09-09 13:49:58 UTC
README
WordPress插件中声明依赖项的辅助工具。
需求
WordPress, PHP 5.3或更高版本,以及Composer。
安装
使用Composer安装
$ composer require ssnepenthe/wp-requirements
使用方法
此包提供了一种简单的方法,以确保WordPress插件在PHP 5.3下失败时能够优雅地处理。
在您的主插件文件中创建一个检查器实例(例如 my-plugin/my-plugin.php
)
use WP_Requirements\Plugin_Checker; $checker = new Plugin_Checker( 'My Awesome Plugin', __FILE__ );
第一个参数是您插件的名字(用于不满足需求时的通知),第二个参数是您主插件文件的路径(用于禁用插件)。
然后添加任何数量的需求。以下都是有效的
// Verify that the Debug_Bar class exists - an indirect way of verifying that the Debug_Bar plugin is active. $checker->class_exists( 'Debug_Bar' ); // Verify that the DOM extension is loaded. $checker->extension_loaded( 'dom' ); // Verify that the cmb2_bootstrap() function exists - an indirect way of verifying that the CMB2 plugin is active. $checker->function_exists( 'cmb2_bootstrap' ); // Verify that the server has PHP 5.4 or greater. $checker->php_at_least( '5.4' ); // Verify that Hello Dolly is active. // First parameter is plugin path relative to the plugin directory. // Second parameter is plugin name used for label when requirement is not met. $checker->plugin_active( 'hello.php', 'Hello Dolly' ); // Verify that the server has WordPress 4.7 or greater. $checker->wp_at_least( '4.7' ); // Check any arbitrary condition. // First parameter is a closure that should return true when the requirement is met, false otherwise. // Second parameter is a message to display when the requirement is not met. Note that it will be prefixed with '{plugin name} deactivated: ' when it is displayed. $checker->add_check( function() { return defined( 'SOME_CONSTANT' ) && SOME_CONSTANT; }, 'SOME_CONSTANT must be defined and truthy' );
The Plugin_Checker类还提供了流畅的接口
use WP_Requirements\Plugin_Checker; $checker = Plugin_Checker::make( 'My Awesome Plugin', __FILE__ ) ->function_exists( 'cmb2_bootstrap' ) ->php_at_least( '5.6' ) ->wp_at_least( '4.7' );
最后,验证所有需求是否满足,并据此启动您的插件。
if ( $checker->requirements_met() ) { // Whatever logic is required to bootstrap your plugin. // This should mostly take place outside of this file to minimize risk of errors when requirements are not met. $plugin = new My_Awesome_Plugin_Bootstrap; $plugin->init(); } else { // This method hooks in to 'admin_notices' to inform the user which requirements weren't met and 'admin_init' to actually deactivate the plugin. $checker->deactivate_and_notify(); }