remove_action Из класса PHP в членствах WooCommerce

Ранее я использовал описанное здесь решение: remove_action Из класса PHP для удаления действия в плагине членства WooCommerce.

Однако решение больше не работает, так как WooComemerce изменили код, лежащий в основе плагина членства.

Итак, это новый код.

Главная 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-limits.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; } 

Затем в классе-wc-membershiphips-posts-limits.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»?

Пока у меня есть файл theme.php:

  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 ); 

Который дает мне «пустую страницу» – ужас.

Solutions Collecting From Web of "remove_action Из класса PHP в членствах WooCommerce"

Не могли бы вы рассказать нам, что такое сообщение об ошибке? Я предполагаю, что restrictions и post_restrictions не являются одним и тем же свойством, и поэтому вы не найдете метод restrict_post в правильном классе.

Отредактированный теперь, когда я посмотрел членство, это, похоже, работает для меня:

 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, который после того, как функция уже выполнила метод Memberships при приоритете 0, поэтому, даже если остальная часть вашего кода была правильной, было бы слишком поздно.

Итак, я думаю, нам нужно пойти на более ранний крючок.

И 2. Думаю, нам нужно использовать новый метод для доступа к экземпляру класса post post.

отредактирован для добавления

и 3. Я переключился на условие сравнения с прямой версией

и 4. Я неправильно понял, где метод get_posts_restrictions_instance() был … он доступен через wc_memberships()->get_restrictions_instance()->get_posts_restrictions_instance()