禁用woocommerce api身份验证?

use*_*364 3 php wordpress woocommerce

woocommerce api 为 HTTP 请求采用 oauth1.0,为 HTTPS 请求采用基本的 HTTP 身份验证。我的查询很简单。如何简单地删除此身份验证?我做了一些挖掘,发现 woocommerce 插件中有一个类,其构造函数为

public function __construct() {

        // To disable authentication, hook into this filter at a later priority and return a valid WP_User
        add_filter( 'woocommerce_api_check_authentication', array( $this, 'authenticate' ), 0 );
    }
Run Code Online (Sandbox Code Playgroud)

我的工作是简单地删除身份验证部分。这里是说在以后的优先级上钩住这个过滤器。如何这样做以及如何返回有效的 WP_User?

Kar*_*hik 5

创建您自己的插件并放置以下代码:

function wc_authenticate_alter(){
    //return wp_get_current_user();
    if( 'GET' ==  WC()->api->server->method ){
        return new WP_User( 1 );
    } else {
        throw new Exception( __( 'You dont have permission', 'woocommerce' ), 401 );
    }
}

add_filter( 'woocommerce_api_check_authentication', 'wc_authenticate_alter', 1 );
Run Code Online (Sandbox Code Playgroud)

这将绕过 woocommerce api 身份验证。使用它需要您自担风险。

(您可以将其添加到主题functions.php而不是自己的插件中。但未经过测试。)