使用外部类的Remove_action或Remove_Filter?

时间:2011-12-09 作者:Tom Auger

如果插件将其方法封装在一个类中,然后针对其中一个方法注册了筛选器或操作,那么如果您不再有权访问该类的实例,如何删除该操作或筛选器?

例如,假设您有一个插件可以做到这一点:

class MyClass {
    function __construct() {
       add_action( "plugins_loaded", array( $this, \'my_action\' ) );
    }

    function my_action() {
       // do stuff...
    }
}

new MyClass();
注意到我现在无法访问该实例,如何注销该类?这是:remove_action( "plugins_loaded", array( MyClass, \'my_action\' ) ); 这似乎不是正确的方法——至少在我的情况下似乎不起作用。

8 个回复
最合适的回答,由SO网友:mfields 整理而成

这里最好使用静态类。以下代码应具有指导意义:

class MyClass {
    function __construct() {
        add_action( \'wp_footer\', array( $this, \'my_action\' ) );
    }
    function my_action() {
        print \'<h1>\' . __class__ . \' - \' . __function__ . \'</h1>\';
    }
}
new MyClass();


class MyStaticClass {
    public static function init() {
        add_action( \'wp_footer\', array( __class__, \'my_action\' ) );
    }
    public static function my_action() {
        print \'<h1>\' . __class__ . \' - \' . __function__ . \'</h1>\';
    }
}
MyStaticClass::init();

function my_wp_footer() {
    print \'<h1>my_wp_footer()</h1>\';
}
add_action( \'wp_footer\', \'my_wp_footer\' );

function mfields_test_remove_actions() {
    remove_action( \'wp_footer\', \'my_wp_footer\' );
    remove_action( \'wp_footer\', array( \'MyClass\', \'my_action\' ), 10 );
    remove_action( \'wp_footer\', array( \'MyStaticClass\', \'my_action\' ), 10 );
}
add_action( \'wp_head\', \'mfields_test_remove_actions\' );
如果您从插件运行此代码,您应该注意到StaticClass的方法以及函数将从wp\\u footer中删除。

SO网友:Otto

每当插件创建new MyClass();, 它应该将其分配给唯一命名的变量。这样,就可以访问该类的实例。

所以如果他在做$myclass = new MyClass();, 然后你可以这样做:

global $myclass;
remove_action( \'wp_footer\', array( $myclass, \'my_action\' ) );
这是因为插件包含在全局名称空间中,所以插件主体中的隐式变量声明是全局变量。

如果插件没有将新类的标识符保存在某个地方,那么从技术上讲,这就是一个bug。面向对象编程的一般原则之一是,在某个地方没有被某个变量引用的对象将被清除或消除。

现在,PHP尤其不像Java那样做到这一点,因为PHP有点像是一个半死不活的OOP实现。实例变量只是字符串,其中包含唯一的对象名,诸如此类。它们之所以有效,是因为变量函数名交互与-> 操作人员所以就这么做new class() 确实可以完美地工作,只是很愚蠢。:)

所以,底线是,永远不要这样做new class();. 做$var = new class(); 并以某种方式使$var可供其他位引用。

编辑:年后

我看到很多插件都在做一件事,那就是使用类似于“单例”模式的东西。它们创建一个getInstance()方法来获取类的单个实例。这可能是我见过的最好的解决方案。插件示例:

class ExamplePlugin
{
    protected static $instance = NULL;

    public static function getInstance() {
        NULL === self::$instance and self::$instance = new self;
        return self::$instance;
    }
}
第一次调用getInstance()时,它实例化类并保存其指针。你可以用它来勾引行动。

这样做的一个问题是,如果使用这样的东西,就不能在构造函数中使用getInstance()。这是因为new在设置$instance之前调用构造函数,因此从构造函数调用getInstance()会导致无限循环并中断一切。

一种解决方法是不使用构造函数(或者至少不在其中使用getInstance()),而是在类中显式使用“init”函数来设置操作等。像这样:

public static function init() {
    add_action( \'wp_footer\', array( ExamplePlugin::getInstance(), \'my_action\' ) );
}
这样,在文件末尾,在定义完类之后,实例化插件就变得很简单了:

ExamplePlugin::init();
Init开始添加您的操作,并在这样做时调用getInstance(),它实例化该类并确保其中只存在一个。如果没有init函数,则可以这样做来初始化该类:

ExamplePlugin::getInstance();
为了解决最初的问题,可以这样从外部删除该操作挂钩(在另一个插件中也可以这样做):

remove_action( \'wp_footer\', array( ExamplePlugin::getInstance(), \'my_action\' ) );
把它放在钩住的东西里plugins_loaded 操作挂钩,它将撤消由原始插件挂钩的操作。

SO网友:herewithme

2个小PHP函数,用于使用“匿名”类删除过滤器/操作:https://github.com/herewithme/wp-filters-extras/

SO网友:sMyles

下面是我创建的一个有大量文档记录的函数,用于在您无权访问类对象时删除过滤器(适用于WordPress 1.2+,包括4.7+):

https://gist.github.com/tripflex/c6518efc1753cf2392559866b4bd1a53

/**
 * Remove Class Filter Without Access to Class Object
 *
 * In order to use the core WordPress remove_filter() on a filter added with the callback
 * to a class, you either have to have access to that class object, or it has to be a call
 * to a static method.  This method allows you to remove filters with a callback to a class
 * you don\'t have access to.
 *
 * Works with WordPress 1.2+ (4.7+ support added 9-19-2016)
 * Updated 2-27-2017 to use internal WordPress removal for 4.7+ (to prevent PHP warnings output)
 *
 * @param string $tag         Filter to remove
 * @param string $class_name  Class name for the filter\'s callback
 * @param string $method_name Method name for the filter\'s callback
 * @param int    $priority    Priority of the filter (default 10)
 *
 * @return bool Whether the function is removed.
 */
function remove_class_filter( $tag, $class_name = \'\', $method_name = \'\', $priority = 10 ) {
    global $wp_filter;

    // Check that filter actually exists first
    if ( ! isset( $wp_filter[ $tag ] ) ) return FALSE;

    /**
     * If filter config is an object, means we\'re using WordPress 4.7+ and the config is no longer
     * a simple array, rather it is an object that implements the ArrayAccess interface.
     *
     * To be backwards compatible, we set $callbacks equal to the correct array as a reference (so $wp_filter is updated)
     *
     * @see https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/
     */
    if ( is_object( $wp_filter[ $tag ] ) && isset( $wp_filter[ $tag ]->callbacks ) ) {
        // Create $fob object from filter tag, to use below
        $fob = $wp_filter[ $tag ];
        $callbacks = &$wp_filter[ $tag ]->callbacks;
    } else {
        $callbacks = &$wp_filter[ $tag ];
    }

    // Exit if there aren\'t any callbacks for specified priority
    if ( ! isset( $callbacks[ $priority ] ) || empty( $callbacks[ $priority ] ) ) return FALSE;

    // Loop through each filter for the specified priority, looking for our class & method
    foreach( (array) $callbacks[ $priority ] as $filter_id => $filter ) {

        // Filter should always be an array - array( $this, \'method\' ), if not goto next
        if ( ! isset( $filter[ \'function\' ] ) || ! is_array( $filter[ \'function\' ] ) ) continue;

        // If first value in array is not an object, it can\'t be a class
        if ( ! is_object( $filter[ \'function\' ][ 0 ] ) ) continue;

        // Method doesn\'t match the one we\'re looking for, goto next
        if ( $filter[ \'function\' ][ 1 ] !== $method_name ) continue;

        // Method matched, now let\'s check the Class
        if ( get_class( $filter[ \'function\' ][ 0 ] ) === $class_name ) {

            // WordPress 4.7+ use core remove_filter() since we found the class object
            if( isset( $fob ) ){
                // Handles removing filter, reseting callback priority keys mid-iteration, etc.
                $fob->remove_filter( $tag, $filter[\'function\'], $priority );

            } else {
                // Use legacy removal process (pre 4.7)
                unset( $callbacks[ $priority ][ $filter_id ] );
                // and if it was the only filter in that priority, unset that priority
                if ( empty( $callbacks[ $priority ] ) ) {
                    unset( $callbacks[ $priority ] );
                }
                // and if the only filter for that tag, set the tag to an empty array
                if ( empty( $callbacks ) ) {
                    $callbacks = array();
                }
                // Remove this filter from merged_filters, which specifies if filters have been sorted
                unset( $GLOBALS[\'merged_filters\'][ $tag ] );
            }

            return TRUE;
        }
    }

    return FALSE;
}

/**
 * Remove Class Action Without Access to Class Object
 *
 * In order to use the core WordPress remove_action() on an action added with the callback
 * to a class, you either have to have access to that class object, or it has to be a call
 * to a static method.  This method allows you to remove actions with a callback to a class
 * you don\'t have access to.
 *
 * Works with WordPress 1.2+ (4.7+ support added 9-19-2016)
 *
 * @param string $tag         Action to remove
 * @param string $class_name  Class name for the action\'s callback
 * @param string $method_name Method name for the action\'s callback
 * @param int    $priority    Priority of the action (default 10)
 *
 * @return bool               Whether the function is removed.
 */
function remove_class_action( $tag, $class_name = \'\', $method_name = \'\', $priority = 10 ) {
    return remove_class_filter( $tag, $class_name, $method_name, $priority );
}

SO网友:Digerkam

上面的解决方案看起来过时了,不得不自己写。。。

function remove_class_action ($action,$class,$method) {
    global $wp_filter ;
    if (isset($wp_filter[$action])) {
        $len = strlen($method) ;
        foreach ($wp_filter[$action] as $pri => $actions) {
            foreach ($actions as $name => $def) {
                if (substr($name,-$len) == $method) {
                    if (is_array($def[\'function\'])) {
                        if (get_class($def[\'function\'][0]) == $class) {
                            if (is_object($wp_filter[$action]) && isset($wp_filter[$action]->callbacks)) {
                                unset($wp_filter[$action]->callbacks[$pri][$name]) ;
                            } else {
                                unset($wp_filter[$action][$pri][$name]) ;
                            }
                        }
                    }
                }
            }
        }
    }
}

SO网友:Alexander Ivashchenko

在这种情况下,Wordpress会向函数名添加一个哈希(唯一id),并将其存储在全局$wp_filter 变量所以如果你使用remove_filter 功能什么都不会发生。即使将类名添加到函数名中,如remove_filter(\'plugins_loaded\', [\'MyClass\', \'my_action\']).您所能做的就是删除所有my_action 来自全球的挂钩$wp_filter 手动变量。

以下是执行此操作的函数:

function my_remove_filter($tag, $function_name, $priority = 10){

    global $wp_filter;

    if( isset($wp_filter[$tag]->callbacks[$priority]) and !empty($wp_filter[$tag]->callbacks[$priority]) ){

        $wp_filter[$tag]->callbacks[$priority] = array_filter($wp_filter[$tag]->callbacks[$priority], function($v, $k) use ($function_name){

            return ( stripos($k, $function_name) === false );

        }, ARRAY_FILTER_USE_BOTH );
    }
}
使用方法如下:

my_remove_filter(\'plugins_loaded\', \'my_action\');

SO网友:Jonny

此函数基于@Digerkam answer。添加了比较if$def[\'function\'][0] 是字符串,它终于对我起作用了。

还使用$wp_filter[$tag]->remove_filter() 应该使它更稳定。

function remove_class_action($tag, $class = \'\', $method, $priority = null) : bool {
    global $wp_filter;
    if (isset($wp_filter[$tag])) {
        $len = strlen($method);

        foreach($wp_filter[$tag] as $_priority => $actions) {

            if ($actions) {
                foreach($actions as $function_key => $data) {

                    if ($data) {
                        if (substr($function_key, -$len) == $method) {

                            if ($class !== \'\') {
                                $_class = \'\';
                                if (is_string($data[\'function\'][0])) {
                                    $_class = $data[\'function\'][0];
                                }
                                elseif (is_object($data[\'function\'][0])) {
                                    $_class = get_class($data[\'function\'][0]);
                                }
                                else {
                                    return false;
                                }

                                if ($_class !== \'\' && $_class == $class) {
                                    if (is_numeric($priority)) {
                                        if ($_priority == $priority) {
                                            //if (isset( $wp_filter->callbacks[$_priority][$function_key])) {}
                                            return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
                                        }
                                    }
                                    else {
                                        return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
                                    }
                                }
                            }
                            else {
                                if (is_numeric($priority)) {
                                    if ($_priority == $priority) {
                                        return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
                                    }
                                }
                                else {
                                    return $wp_filter[$tag]->remove_filter($tag, $function_key, $_priority);
                                }
                            }

                        }
                    }
                }
            }
        }

    }

    return false;
}
示例用法:

精确匹配

add_action(\'plugins_loaded\', function() {
    remove_class_action(\'plugins_loaded\', \'MyClass\', \'my_action\', 0);
});
任何优先级

add_action(\'plugins_loaded\', function() {
    remove_class_action(\'plugins_loaded\', \'MyClass\', \'my_action\');
});
任何类别和任何优先级

add_action(\'plugins_loaded\', function() {
    remove_class_action(\'plugins_loaded\', \'\', \'my_action\');
});

SO网友:nabrown

这不是一般的答案,而是针对Avada theme and WooCommerce, 我认为其他人可能会觉得这很有帮助:

function remove_woo_commerce_hooks() {
    global $avada_woocommerce;
    remove_action( \'woocommerce_single_product_summary\', array( $avada_woocommerce, \'add_product_border\' ), 19 );
}
add_action( \'after_setup_theme\', \'remove_woo_commerce_hooks\' );

结束

相关推荐

hooks & filters and variables

我是updating the codex page example for action hooks, 在游戏中完成一些可重用的功能(最初是针对这里的一些Q@WA)。但后来我遇到了一个以前没有意识到的问题:在挂接到一个函数以修改变量的输出后,我再也无法决定是要回显输出还是只返回它。The Problem: 我可以修改传递给do_action 用回调函数钩住。使用变量修改/添加的所有内容仅在回调函数中可用,但在do_action 在原始函数内部调用。很高兴:我将其修改为一个工作示例,因此您可以将其复制/粘贴