如何覆盖插件脚本

时间:2016-10-09 作者:Magnus Pilegaard

我使用woocommerce简单拍卖插件,为了满足我的需要,我对插件中的一个文件进行了一些自定义。我需要通过自己的修改,以某种方式重写原始函数或其部分。我该怎么做?

原始函数如下所示:

            public function __construct(){
            $this->plugin_prefix    = \'wc_simple_auctions\';
            $this->plugin_basefile  = plugin_basename(__FILE__);
            $this->plugin_url       = plugin_dir_url($this->plugin_basefile);
            $this->plugin_path      = trailingslashit(dirname(__FILE__));

            $this->auction_types = array(\'normal\' => __(\'Normal\', \'wc_simple_auctions\'), \'reverse\' => __(\'Reverse\', \'wc_simple_auctions\'));
            $this->auction_item_condition = array(\'new\' => __(\'New\', \'wc_simple_auctions\'), \'used\' => __(\'Used\', \'wc_simple_auctions\'));

            add_action(\'woocommerce_init\', array(&$this, \'init\'));
            //require_once( ABSPATH .\'wp-includes/pluggable.php\');              
        }
我需要此更改覆盖它:

            public function __construct(){
            $this->plugin_prefix    = \'wc_simple_auctions\';
            $this->plugin_basefile  = plugin_basename(__FILE__);
            $this->plugin_url       = plugin_dir_url($this->plugin_basefile);
            $this->plugin_path      = trailingslashit(dirname(__FILE__));

            $this->auction_types = array(\'normal\' => __(\'Normal\', \'wc_simple_auctions\'), \'reverse\' => __(\'Reverse\', \'wc_simple_auctions\'));
            $this->auction_item_condition = array(\'new\' => __(\'Brand New\', \'wc_simple_auctions\'), \'used\' => __(\'1/10\', \'wc_simple_auctions\'),
            \'2/10\'=> __(\'2/10\', \'wc_simple_auctions\'), \'3/10\'=> __(\'3/10\', \'wc_simple_auctions\'), \'4/10\'=> __(\'4/10\', \'wc_simple_auctions\'), \'5/10\'=> __(\'2/10\', \'wc_simple_auctions\'), \'6/10\'=> __(\'6/10\', \'wc_simple_auctions\'), \'7/10\'=> __(\'7/10\', \'wc_simple_auctions\'), \'8/10\'=> __(\'8/10\', \'wc_simple_auctions\'), \'9/10\'=> __(\'9/10\', \'wc_simple_auctions\'), \'10/10\'=> __(\'10/10\', \'wc_simple_auctions\'));

            add_action(\'woocommerce_init\', array(&$this, \'init\'));
            //require_once( ABSPATH .\'wp-includes/pluggable.php\');              
        }
我该怎么做?

此处添加了调用上述内容的代码段:

            public function product_write_panel(){
            global $post;
            $product     = get_product( $post->ID );

            // Pull the video tab data out of the database
            $tab_data = maybe_unserialize(get_post_meta($post->ID, \'woo_auction_tab\', true));
            if(empty($tab_data)){
                $tab_data[] = array();
            }
            echo \'<div id="auction_tab" class="panel woocommerce_options_panel">\';

            woocommerce_wp_select( array( \'id\' => \'_auction_item_condition\', \'label\' => __( \'Item condition\', \'wc_simple_auctions\' ), \'options\' => apply_filters( \'simple_auction_item_condition\',$this->auction_item_condition) ) );
}

1 个回复
SO网友:Mark Kaplun

原则上,除非插件有文档化的API和/或过滤器,否则如果您希望将来能够升级它,就不应该再使用核心挂钩了。

如果没有合适的东西可用,你应该联系作者并提出要求,或者决定你要承担全部责任,然后直接交出来。

在您的特定情况下,您可能能够在对象初始化后更改该属性,但下一版本可能不再存在该属性。

Edit: 因为有一个过滤器,你只需要像这样使用它

function wpse_242093_auctions($auctions) {
  return array(\'new\' => __(\'Brand New\', \'wc_simple_auctions\'), \'used\' => __(\'1/10\', \'wc_simple_auctions\'),
            \'2/10\'=> __(\'2/10\', \'wc_simple_auctions\'), \'3/10\'=> __(\'3/10\', \'wc_simple_auctions\'), \'4/10\'=> __(\'4/10\', \'wc_simple_auctions\'), \'5/10\'=> __(\'2/10\', \'wc_simple_auctions\'), \'6/10\'=> __(\'6/10\', \'wc_simple_auctions\'), \'7/10\'=> __(\'7/10\', \'wc_simple_auctions\'), \'8/10\'=> __(\'8/10\', \'wc_simple_auctions\'), \'9/10\'=> __(\'9/10\', \'wc_simple_auctions\'), \'10/10\'=> __(\'10/10\', \'wc_simple_auctions\'));
}

add_filter(\'simple_auction_item_condition\',\'wpse_242093_auctions\');