Php 扩展WC\u Stripe\u Webhook\u处理程序时出现问题

Php 扩展WC\u Stripe\u Webhook\u处理程序时出现问题,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我试图扩展WooCommerce Stripe Gateway插件的WC\u Stripe\u Webhook\u处理程序类,以便检查Webhook请求主体 虽然我正在扩展类并更改函数check\u for\u webhooks,但仍在调用父函数。我尝试了几种替代方法,例如删除父操作和添加我的操作。我更愿意简单地扩展课程,但是,经过一天的谷歌搜索和调整,我遇到了麻烦 我希望能得到一些指导 父类文件:Class wc stripe webhook handler.php class WC_Stri

我试图扩展WooCommerce Stripe Gateway插件的
WC\u Stripe\u Webhook\u处理程序
类,以便检查Webhook请求主体

虽然我正在扩展类并更改函数
check\u for\u webhooks
,但仍在调用父函数。我尝试了几种替代方法,例如删除父操作和添加我的操作。我更愿意简单地扩展课程,但是,经过一天的谷歌搜索和调整,我遇到了麻烦

我希望能得到一些指导

父类文件:Class wc stripe webhook handler.php

class WC_Stripe_Webhook_Handler extends WC_Stripe_Payment_Gateway {
    /**
     * Delay of retries.
     *
     * @var int
     */
    public $retry_interval;

    /**
     * Is test mode active?
     *
     * @var bool
     */
    public $testmode;

    /**
     * The secret to use when verifying webhooks.
     *
     * @var string
     */
    protected $secret;

    /**
     * Constructor.
     *
     * @since 4.0.0
     * @version 4.0.0
     */
    public function __construct() {
        $this->retry_interval = 2;
        $stripe_settings      = get_option( 'woocommerce_stripe_settings', array() );
        $this->testmode       = ( ! empty( $stripe_settings['testmode'] ) && 'yes' === $stripe_settings['testmode'] ) ? true : false;
        $secret_key           = ( $this->testmode ? 'test_' : '' ) . 'webhook_secret';
        $this->secret         = ! empty( $stripe_settings[ $secret_key ] ) ? $stripe_settings[ $secret_key ] : false;

        add_action( 'woocommerce_api_wc_stripe', array( $this, 'check_for_webhook' ) );
    }

    /**
     * Check incoming requests for Stripe Webhook data and process them.
     *
     * @since 4.0.0
     * @version 4.0.0
     */
    public function check_for_webhook() {
        error_log('DEBUG BOO!!!! Entered parent function');
        if ( ( 'POST' !== $_SERVER['REQUEST_METHOD'] )
            || ! isset( $_GET['wc-api'] )
            || ( 'wc_stripe' !== $_GET['wc-api'] )
        ) {
            return;
        }

        $request_body    = file_get_contents( 'php://input' );
        $request_headers = array_change_key_case( $this->get_request_headers(), CASE_UPPER );

        // Validate it to make sure it is legit.
        if ( $this->is_valid_request( $request_headers, $request_body ) ) {
            $this->process_webhook( $request_body );
            status_header( 200 );
            exit;
        } else {
            WC_Stripe_Logger::log( 'Incoming webhook failed validation: ' . print_r( $request_body, true ) );
            status_header( 400 );
            exit;
        }
    }
/* other functions... */
}

new WC_Stripe_Webhook_Handler();
儿童班

require_once ABSPATH . 'wp-content/plugins/woocommerce-gateway-stripe/includes/class-wc-stripe-webhook-handler.php';

class BSD_WC_SCSP_Stripe_Webhook_Handler extends WC_Stripe_Webhook_Handler {

    public function __construct() {
        parent::__construct();
    }

    public function check_for_webhook() {
        error_log('DEBUG WOO-HOO!!!! Entered child version.');
        if ( ( 'POST' !== $_SERVER['REQUEST_METHOD'] )
            || ! isset( $_GET['wc-api'] )
            || ( 'wc_stripe' !== $_GET['wc-api'] )
        ) {
            return;
        }

        $request_body    = file_get_contents( 'php://input' );
        $request_headers = array_change_key_case( $this->get_request_headers(), CASE_UPPER );

        // Validate it to make sure it is legit.
        if ( $this->is_valid_request( $request_headers, $request_body ) ) {
            error_log('DEBUG: TO-DO Now examine the request_body');
            $this->process_webhook( $request_body );
            status_header( 200 );
            exit;
        } else {
            WC_Stripe_Logger::log( 'Incoming webhook failed validation: ' . print_r( $request_body, true ) );
            status_header( 400 );
            exit;
        }
    }

}
EDIT:根据Sebo的建议,我修改了我的构造函数以包含一个
add\u操作
调用,如下所示:

public function __construct() {
    parent::__construct();
    add_action( 'woocommerce_api_wc_stripe', array( $this, 'check_for_webhook'), 99 );           
}
我确认我的挂钩正在添加以下内容:

$hook_name = 'woocommerce_api_wc_stripe';
global $wp_filter;
error_log( print_r($wp_filter[$hook_name], true) ); 
但是,仍然没有调用该函数。注意:我尝试了check_for_webhook的父函数名,然后重命名了我的函数名并将其作为操作添加。在这两种情况下,它都没有被调用。还尝试使用remove_操作移除父钩子;没有骰子

编辑#2:
WC_Stripe_Webhook_处理程序
在WooCommerce Stripe网关插件启动时实例化。该插件在加载所有其他插件后启动:
add_action('plugins_-loaded'、'woocommerce_-gateway_-stripe_-init')

编辑#3/最终工作班:多亏了Sebo的指导,以及一位名叫Dung Nguyen Tien的同事的帮助,这项工作现在已经开始。关键是添加优先级为9的check_for_webhook的子版本。下面是完整的课程:

if (!class_exists('BSD_WC_SCSP_Stripe_Webhook_Handler')) :

    class BSD_WC_SCSP_Stripe_Webhook_Handler extends WC_Stripe_Webhook_Handler {

        public function __construct() {
            parent::__construct();

            // child filter
            add_action('woocommerce_api_wc_stripe', array($this, 'check_for_webhook'), 9);
        }

        public function check_for_webhook() {
            error_log('DEBUG WOO-HOO!!!! Entered child version.');
            if (('POST' !== $_SERVER['REQUEST_METHOD'])
                || !isset($_GET['wc-api'])
                || ('wc_stripe' !== $_GET['wc-api'])
            ) {
                return;
            }

            $request_body = file_get_contents('php://input');
            $request_headers = array_change_key_case($this->get_request_headers(), CASE_UPPER);

            // Validate it to make sure it is legit.
            if ($this->is_valid_request($request_headers, $request_body)) {
                error_log('DEBUG: TO-DO Now examine the request_body');
                $this->process_webhook($request_body);
                status_header(200);
                exit;
            } else {
                WC_Stripe_Logger::log('Incoming webhook failed validation: ' . print_r($request_body, true));
                status_header(400);
                exit;
            }
        }

    }

endif; // class_exists check

new BSD_WC_SCSP_Stripe_Webhook_Handler();

我认为不管你做什么,父类都会被调用,因为WordPress和Woocommerce的大部分部分不仅使用OOP,而且使用WP操作和过滤器挂钩,彼此之间都有着深刻的联系

您可以使用WordPress添加动作功能()来实现您的目标

使用此函数,您可以钩住父类构造函数中的操作,如下所示(未测试):

其中$this是对持有“my_check_for_webhook”函数的类的引用,第三个参数99将决定函数何时运行,因为可能有多个函数绑定到同一个钩子

最好的,
Sebo

谢谢@Sebo。我试了一下,但运气不好。简而言之,我确认我的操作正在添加,但仍然没有被调用。我用细节更新了我的问题。嗯,你的类WC\u Stripe\u Webhook\u处理程序在哪里实例化?是不是可能在某个深夜?因为这会阻止你的回调触发,因为这个链已经运行了,但它仍然在$wp_过滤器全局中。它被实例化为主插件本身的init函数的一部分。该插件依次在加载所有插件后添加:
add_操作('plugins_-loaded'、'woocommerce_-gateway_-stripe_-init')为了好玩,我又尝试了一些相关的东西。用EDIT#2更新了帖子。只有在你运行了_bsd_wc_scsp_最后一个函数的情况下,你的插件才能工作吗?因为通常你应该让WordPress决定以何种顺序加载哪些插件,并且在插件中使用add_操作和add_filter调用中的第三个参数,该参数控制钩子被触发的顺序。因此,在您的示例中:
add_action('woocommerce_api_wc_stripe',array($this,'check_for_webhook'),99)任何在同一个钩子中添加的小于99的内容都将在函数之前运行。是的,我误解了你之前关于99优先级的陈述。我忘了99是最后一个!问题解决了。将从我的问题中删除无关信息,发布最终代码,并将您的回答标记为答案。非常感谢。
add_action( 'woocommerce_api_wc_stripe', array( $this, 'my_check_for_webhook' ), 99 );