From c628c15ac6230c0db17a9f266489711d1490ff31 Mon Sep 17 00:00:00 2001 From: raicem Date: Tue, 5 Jan 2021 12:43:17 +0300 Subject: [PATCH 1/5] Update featured products endpoint WooCommerce.com will provide this list of products. --- includes/admin/class-wc-admin-addons.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/admin/class-wc-admin-addons.php b/includes/admin/class-wc-admin-addons.php index ac072dc0aa5..458c53f3de1 100644 --- a/includes/admin/class-wc-admin-addons.php +++ b/includes/admin/class-wc-admin-addons.php @@ -25,7 +25,7 @@ class WC_Admin_Addons { public static function get_featured() { $featured = get_transient( 'wc_addons_featured' ); if ( false === $featured ) { - $raw_featured = wp_safe_remote_get( 'https://d3t0oesq8995hv.cloudfront.net/add-ons/featured-v2.json', array( 'user-agent' => 'WooCommerce Addons Page' ) ); + $raw_featured = wp_safe_remote_get( 'https://woocommerce.com/wp-json/wccom-extensions/1.0/featured', array( 'user-agent' => 'WooCommerce Addons Page' ) ); if ( ! is_wp_error( $raw_featured ) ) { $featured = json_decode( wp_remote_retrieve_body( $raw_featured ) ); if ( $featured ) { From c70eee2a0a970ced26640dfaf01df2fea703aac2 Mon Sep 17 00:00:00 2001 From: raicem Date: Tue, 5 Jan 2021 17:40:58 +0300 Subject: [PATCH 2/5] Send auth token when sending search and featured extension requests --- includes/admin/class-wc-admin-addons.php | 28 ++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/includes/admin/class-wc-admin-addons.php b/includes/admin/class-wc-admin-addons.php index 458c53f3de1..c618bcce508 100644 --- a/includes/admin/class-wc-admin-addons.php +++ b/includes/admin/class-wc-admin-addons.php @@ -25,7 +25,21 @@ class WC_Admin_Addons { public static function get_featured() { $featured = get_transient( 'wc_addons_featured' ); if ( false === $featured ) { - $raw_featured = wp_safe_remote_get( 'https://woocommerce.com/wp-json/wccom-extensions/1.0/featured', array( 'user-agent' => 'WooCommerce Addons Page' ) ); + $headers = array(); + $auth = WC_Helper_Options::get( 'auth' ); + + if ( ! empty( $auth['access_token'] ) ) { + $headers['Authorization'] = 'Bearer ' . $auth['access_token']; + } + + $raw_featured = wp_safe_remote_get( + 'https://woocommerce.com/wp-json/wccom-extensions/1.0/featured', + array( + 'headers' => $headers, + 'user-agent' => 'WooCommerce Addons Page', + ) + ); + if ( ! is_wp_error( $raw_featured ) ) { $featured = json_decode( wp_remote_retrieve_body( $raw_featured ) ); if ( $featured ) { @@ -71,9 +85,19 @@ class WC_Admin_Addons { */ public static function get_extension_data( $category, $term, $country ) { $parameters = self::build_parameter_string( $category, $term, $country ); + + $headers = array(); + $auth = WC_Helper_Options::get( 'auth' ); + + if ( ! empty( $auth['access_token'] ) ) { + $headers['Authorization'] = 'Bearer ' . $auth['access_token']; + } + $raw_extensions = wp_remote_get( - 'https://woocommerce.com/wp-json/wccom-extensions/1.0/search' . $parameters + 'https://woocommerce.com/wp-json/wccom-extensions/1.0/search' . $parameters, + array( 'headers' => $headers ) ); + if ( ! is_wp_error( $raw_extensions ) ) { $addons = json_decode( wp_remote_retrieve_body( $raw_extensions ) )->products; } From 7b67977a31d6de6529e1fe5e9526a5afc0d45235 Mon Sep 17 00:00:00 2001 From: raicem Date: Tue, 5 Jan 2021 17:45:34 +0300 Subject: [PATCH 3/5] Reduce "wc_addons_featured" transient cache duration The featured extension list was static but now it's fetched from WooCommerce.com and may change more frequently. Reducing the cache duration to allow users to see changes much more quickly. --- includes/admin/class-wc-admin-addons.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/admin/class-wc-admin-addons.php b/includes/admin/class-wc-admin-addons.php index c618bcce508..74fe1774fd6 100644 --- a/includes/admin/class-wc-admin-addons.php +++ b/includes/admin/class-wc-admin-addons.php @@ -43,7 +43,7 @@ class WC_Admin_Addons { if ( ! is_wp_error( $raw_featured ) ) { $featured = json_decode( wp_remote_retrieve_body( $raw_featured ) ); if ( $featured ) { - set_transient( 'wc_addons_featured', $featured, WEEK_IN_SECONDS ); + set_transient( 'wc_addons_featured', $featured, DAY_IN_SECONDS ); } } } From f0166fcc568b087dd74d8ba400d3a5413c275b7f Mon Sep 17 00:00:00 2001 From: raicem Date: Thu, 7 Jan 2021 15:14:38 +0300 Subject: [PATCH 4/5] Fix indentation errors as per WordPress code style guides --- includes/admin/class-wc-admin-addons.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/admin/class-wc-admin-addons.php b/includes/admin/class-wc-admin-addons.php index 74fe1774fd6..68cc14ef174 100644 --- a/includes/admin/class-wc-admin-addons.php +++ b/includes/admin/class-wc-admin-addons.php @@ -26,7 +26,7 @@ class WC_Admin_Addons { $featured = get_transient( 'wc_addons_featured' ); if ( false === $featured ) { $headers = array(); - $auth = WC_Helper_Options::get( 'auth' ); + $auth = WC_Helper_Options::get( 'auth' ); if ( ! empty( $auth['access_token'] ) ) { $headers['Authorization'] = 'Bearer ' . $auth['access_token']; @@ -35,7 +35,7 @@ class WC_Admin_Addons { $raw_featured = wp_safe_remote_get( 'https://woocommerce.com/wp-json/wccom-extensions/1.0/featured', array( - 'headers' => $headers, + 'headers' => $headers, 'user-agent' => 'WooCommerce Addons Page', ) ); @@ -87,7 +87,7 @@ class WC_Admin_Addons { $parameters = self::build_parameter_string( $category, $term, $country ); $headers = array(); - $auth = WC_Helper_Options::get( 'auth' ); + $auth = WC_Helper_Options::get( 'auth' ); if ( ! empty( $auth['access_token'] ) ) { $headers['Authorization'] = 'Bearer ' . $auth['access_token']; From eb3ba820dab8e3c663e26d30e11d2e27714ad6fb Mon Sep 17 00:00:00 2001 From: raicem Date: Wed, 13 Jan 2021 23:53:53 +0300 Subject: [PATCH 5/5] Use "wp_safe_remote_get" for sending search requests --- includes/admin/class-wc-admin-addons.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/admin/class-wc-admin-addons.php b/includes/admin/class-wc-admin-addons.php index 68cc14ef174..a01784abc1b 100644 --- a/includes/admin/class-wc-admin-addons.php +++ b/includes/admin/class-wc-admin-addons.php @@ -93,7 +93,7 @@ class WC_Admin_Addons { $headers['Authorization'] = 'Bearer ' . $auth['access_token']; } - $raw_extensions = wp_remote_get( + $raw_extensions = wp_safe_remote_get( 'https://woocommerce.com/wp-json/wccom-extensions/1.0/search' . $parameters, array( 'headers' => $headers ) );