WooCommerce成员资格中来自PHP类的remove_action [英] remove_action From PHP Class in WooCommerce Memberships

查看:66
本文介绍了WooCommerce成员资格中来自PHP类的remove_action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前曾使用这里描述的解决方案:从PHP类中删除_动作用于删除WooCommerce成员资格插件中的操作.

I have previously used a solution described here: remove_action From PHP Class for removing an action in the WooCommerce membership plugin.

但是,该解决方案不再起作用,因为WooComemerce更改了成员资格插件背后的代码.

However, the solution no longer works, as WooComemerce have changed the code behind the membership plugin.

这是新代码.

主要woocommerce-memberships.php

public function includes() {

    // load post types
    require_once( $this->get_plugin_path() . '/includes/class-wc-memberships-post-types.php' );

    // load user messages helper
    require_once( $this->get_plugin_path() . '/includes/class-wc-memberships-user-messages.php' );

    // load helper functions
    require_once( $this->get_plugin_path() . '/includes/functions/wc-memberships-functions.php' );

    // init general classes
    $this->rules            = $this->load_class( '/includes/class-wc-memberships-rules.php',            'WC_Memberships_Rules' );
    $this->plans            = $this->load_class( '/includes/class-wc-memberships-membership-plans.php', 'WC_Memberships_Membership_Plans' );
    $this->emails           = $this->load_class( '/includes/class-wc-memberships-emails.php',           'WC_Memberships_Emails' );
    $this->user_memberships = $this->load_class( '/includes/class-wc-memberships-user-memberships.php', 'WC_Memberships_User_Memberships' );
    $this->capabilities     = $this->load_class( '/includes/class-wc-memberships-capabilities.php',     'WC_Memberships_Capabilities' );
    $this->member_discounts = $this->load_class( '/includes/class-wc-memberships-member-discounts.php', 'WC_Memberships_Member_Discounts' );
    $this->restrictions     = $this->load_class( '/includes/class-wc-memberships-restrictions.php',     'WC_Memberships_Restrictions' );

主实例

    function wc_memberships() {
    return WC_Memberships::instance();
}

来自随附的class-wc-memberships-restrictions.php文件

    /**
 * Returns the general content restrictions handler.
 *
 * @since 1.9.0
 *
 * @return null|\WC_Memberships_Posts_Restrictions
 */
public function get_posts_restrictions_instance() {

    if ( ! $this->posts_restrictions instanceof WC_Memberships_Posts_Restrictions ) {
        $this->posts_restrictions = wc_memberships()->load_class( '/includes/frontend/class-wc-memberships-posts-restrictions.php', 'WC_Memberships_Posts_Restrictions' );
    }

    return $this->posts_restrictions;
}

然后在class-wc-memberships-posts-restrictions.php中

    public function __construct() {

    // decide whether attempting to access restricted content has to be redirected
    add_action( 'wp', array( $this, 'handle_restriction_modes' ) );

    // restrict the post by filtering the post object and replacing the content with a message and maybe excerpt
    add_action( 'the_post', array( $this, 'restrict_post' ), 0 );

如何删除"the_post"操作?

How do i remove the 'the_post' action?

到目前为止,我在functions.php主题文件中具有以下内容:

So far i have the following in functions.php theme file:

  function weteach_remove_actions(){
      if(is_singular( 'post' )) {
         if( function_exists( 'wc_memberships' ) ){
             remove_action( 'the_post', array( wc_memberships()->restrictions, 'restrict_post' ));
         }
      }
      return;
  }
  add_action( 'the_post', 'weteach_remove_actions', 1 );

哪个给我一个空白"错误.

Which gives me a "blank-page"-error.

推荐答案

您能告诉我们错误消息是什么吗?我的猜测是restrictionspost_restrictions不是相同的属性,因此您没有在正确的类中找到restrict_post方法.

Could you tell us what the error message was? My guess is that restrictions and post_restrictions aren't the same property and so you aren't finding the restrict_post method in the right class.

已编辑,因为我已经查看了会员资格,所以这似乎对我有用:

Edited now that I have looked at Memberships, this seems to work for me:

function so_41431558_remove_membership_post_restrictions(){ 
    if( function_exists( 'wc_memberships' ) && version_compare( WC_Memberships::VERSION, '1.9.0', '>=' ) && is_singular( 'post' ) ){
        remove_action( 'the_post', array( wc_memberships()->get_restrictions_instance()->get_posts_restrictions_instance(), 'restrict_post' ), 0 );
    }
}
add_action( 'wp_head', 'so_41431558_remove_membership_post_restrictions', 1 );

您的add_action尝试发生在优先级1上,即在之后该函数已经在优先级0上运行Memberships方法,因此即使您的其余代码正确,也是如此晚了.

Your add_action attempt is happening on priority 1, which is after the function has already run the Memberships method on priority 0, so even if the rest of your code was correct it would be too late.

所以1.我认为我们需要更早一些.

So 1. I think we need to go to an earlier hook.

然后2.我认为我们需要使用新方法来访问职位限制类实例.

And 2. I think we need to use the new method for accessing the post restrictions class instance.

已修改为添加

和3.我已切换到直接版本比较条件

and 3. I've switched to a direct version compare condition

和4.我误读了get_posts_restrictions_instance()方法的位置...通过wc_memberships()->get_restrictions_instance()->get_posts_restrictions_instance()

and 4. I misread where the get_posts_restrictions_instance() method was... it is accessed via wc_memberships()->get_restrictions_instance()->get_posts_restrictions_instance()

这篇关于WooCommerce成员资格中来自PHP类的remove_action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆