donglin6109 2017-05-02 14:43
浏览 65
已采纳

如何使用WordPress插件上的构造函数覆盖类?

I'm trying to override a plugin class on a WordPress plugin. Here is the original plugin class :

class WCV_Vendor_Dashboard
{
       /**
        * __construct()
        */
        function __construct()
        {
            add_shortcode( 'wcv_shop_settings', array( $this, 'display_vendor_settings' ) );
            add_shortcode( 'wcv_vendor_dashboard', array( $this, 'display_vendor_products' ) );

            add_action( 'template_redirect', array( $this, 'check_access' ) );
            add_action( 'init', array( $this, 'save_vendor_settings' ) );
        }

        public function save_vendor_settings(){
               //some codes here
        }
 }

Here is what I'm trying (in functions.php), but it doesn't work :

$wcv_vendor_dashboard = new WCV_Vendor_Dashboard();
global $wcv_vendor_dashboard;
remove_action( 'init', array( $wcv_vendor_dashboard , 'save_vendor_settings' ) );

How to remove it correctly and how to create the replacement?

Additional info: I did similar thing on the WooCommerce core. When I want to override a class / function, I use this (for example):

remove_action( 'template_redirect', array( 'WC_Form_Handler', 'save_account_details' ) );
function new_save_account_details() {
  //custom code here
}
add_action( 'template_redirect', 'new_save_account_details' );

It's working properly on WooCommerce core. I tried something similar on WCV_Vendor_Dashboard but it doesn't work.

  • 写回答

2条回答 默认 最新

  • douyun8885 2017-05-02 17:46
    关注

    Why it was working with woocommerce, but it does not work in this case?

    When you attach a function on to a specific action, WoredPress creates unique id for that callback and stores that in global $wp_filter array ( in fact, it was an array before, but now it is an object ). For object method callbacks ( like array( $this, 'save_vendor_settings' ) ), the id is generated with spl_object_hash php function. For the above example,

    spl_object_hash( $this ) . 'save_vendor_settings'
    

    , and it looks like 000000001c0af63f000000006d7fe83asave_vendor_settings.

    To "legally" remove the object method with remove_action() you will need to have access to the original object that was used to attach the function in the first place. If the object exists in global namespace:

    global $wcv;
    remove_action( 'init', array( $wcv, 'save_vendor_settings' ) );
    

    Creating another class instance will not work because the generated id is unique for each object, even if they are instances of the same class.

    In the case of WooCommerce I guess it was about static class methods. Different logic is used to generate the ids for static class methods, functions and static method callbacks are just returned as strings. For your example it would be:

    'WC_Form_Handler' . '::' . 'save_account_details'
    

    You see why it works for one case, but not for the other.

    There is a hack to replace functions attached by replacing them directly in global $wp_filter object, but it's not 100% reliable. Since we do not have access to the original object, we can only filter $wp_filter by the name of the function, if there are identical names for the same action it will replace wrong handlers.

    global $wp_filter;
    foreach ( $wp_filter['init']->callbacks as $priority => &$callbacks ) {
    
        foreach ( $callbacks as $id => &$callback ) {
    
            if ( substr( $id, -strlen( 'save_vendor_settings' ) ) === 'save_vendor_settings' ) {
                // replace the callback with new function
                $callback['function'] = 'new_save_vendor_settings';
            }
        }
    }
    

    I hope it will work, greetings.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀