admin管理员组文章数量:1317906
I'm developing a rather extensive plugin which has a lot of hooks that are prefixed with the plugin namespace. I have created a wrapper class like the one below but because this plugin is geared towards developers I want it to adhere to WordPress standards (coding and documentation) as best as possible.
Are the below examples recommended? If not, is there a more "WordPress" way of doing this or should I just stick to hardcoding the prefix in every hook?
Example of basic usage for reference:
add_filter( 'namespace/value', function( $value, $context ) { ... }, 10, 2 );
$value = apply_filters( 'namespace/value', $value, $context );
add_action( 'namespace/example', function( $context ) { ... } );
do_action( 'namespace/example', $context );
Example 1: Class:
<?php
namespace MyPlugin\Wrappers;
class Hooks {
private static function prefix( string $value ) {
return 'namespace/' . $value;
}
public static function add_filter( string $tag, $callback, int $priority = 10, int $accepted_args = 1 ) {
add_filter( self::prefix( $tag ), $callback, $priority, $accepted_args );
}
public static function apply_filters( string $tag, $value, ...$args ) {
return apply_filters( self::prefix( $tag ), $value, ...$args );
}
public static function add_action( string $tag, $callback, int $priority = 10, int $accepted_args = 1 ) {
add_action( self::prefix( $tag ), $callback, $priority, $accepted_args );
}
public static function do_action( string $tag, ...$args ) {
do_action( self::prefix( $tag ), ...$args );
}
}
Usage:
use MyPlugin\Wrappers\Hooks;
Hooks::add_filter( 'value', function( $value, $context ) { ... }, 10, 2 );
$value = Hooks::apply_filters( 'value', $value, $context );
Hooks::add_action( 'example', function( $context ) { ... } );
Hooks::do_action( 'example', $context );
Example 2: A more WordPress procedural approach...
function myplugin_prefix( $value ) {
return 'namespace/' . $value;
}
function myplugin_add_filter( string $tag, $callback, int $priority = 10, int $accepted_args = 1 ) {
add_filter( myplugin_prefix( $tag ), $callback, $priority, $accepted_args );
}
function myplugin_apply_filters( string $tag, $value, ...$args ) {
return apply_filters( myplugin_prefix( $tag ), $value, ...$args );
}
function myplugin_add_action( string $tag, $callback, int $priority = 10, int $accepted_args = 1 ) {
add_action( myplugin_prefix( $tag ), $callback, $priority, $accepted_args );
}
function myplugin_do_action( string $tag, ...$args ) {
do_action( myplugin_prefix( $tag ), ...$args );
}
Usage:
myplugin_add_filter( 'value', function( $value, $context ) { ... }, 10, 2 );
$value = myplugin_apply_filters( 'value', $value, $context );
myplugin_add_action( 'example', function( $context ) { ... } );
myplugin_do_action( 'example', $context );
本文标签: Prefixing plugin hooks (actionsfilters) with a wrapper class or functions
版权声明:本文标题:Prefixing plugin hooks (actionsfilters) with a wrapper class or functions 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742031639a2416550.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论