Check for instance of WooCommerce and WP_Error before initializing session and cart in `rest_authentication_errors` callback. (https://github.com/woocommerce/woocommerce-blocks/pull/1698)

* check for instance of WooCommerce before initializing session and cart

* fix invalid filter usage and include WP_Error check

* check for existence of instance method rather than WC instance itself
This commit is contained in:
Darren Ethier 2020-02-05 13:45:09 -08:00 committed by GitHub
parent ad08e0e10b
commit 58867bc328
1 changed files with 11 additions and 11 deletions

View File

@ -19,7 +19,7 @@ class RestApi {
*/
public static function init() {
add_action( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ), 10 );
add_filter( 'rest_authentication_errors', array( __CLASS__, 'maybe_init_cart_session' ), 1, 2 );
add_filter( 'rest_authentication_errors', array( __CLASS__, 'maybe_init_cart_session' ), 1 );
}
/**
@ -61,19 +61,19 @@ class RestApi {
*
* @todo check compat < WC 3.6. Make specific to cart endpoint.
* @param mixed $return Value being filtered.
* @param array $request Request data.
* @return mixed
*/
public static function maybe_init_cart_session( $return, $request = false ) {
// Pass through other errors.
if ( ! empty( $error ) ) {
return $error;
public static function maybe_init_cart_session( $return ) {
$wc_instance = wc();
// if WooCommerce instance isn't available or already have an
// authentication error, just return.
if ( ! method_exists( $wc_instance, 'initialize_session' ) || \is_wp_error( $return ) ) {
return $return;
}
wc()->frontend_includes();
wc()->initialize_session();
wc()->initialize_cart();
wc()->cart->get_cart();
$wc_instance->frontend_includes();
$wc_instance->initialize_session();
$wc_instance->initialize_cart();
$wc_instance->cart->get_cart();
return $return;
}