Add WooCommerce header to WooCommerce.com requests (#33927)

* Add WooCommerce header to WooCommerce.com requests

* Remove duplicate user-agent

* Add header to suggestions request

* Add missing comma
This commit is contained in:
berislav grgičak 2022-09-02 11:12:52 +02:00 committed by GitHub
parent 6da30852c4
commit ca91250b2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 69 additions and 14 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: tweak
Add WooCommerce user-agent to WooCommerce.com requests

View File

@ -40,6 +40,7 @@ class WC_Admin_Addons {
'https://woocommerce.com/wp-json/wccom-extensions/1.0/featured', 'https://woocommerce.com/wp-json/wccom-extensions/1.0/featured',
array( array(
'headers' => $headers, 'headers' => $headers,
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
) )
); );
@ -84,6 +85,7 @@ class WC_Admin_Addons {
'https://woocommerce.com/wp-json/wccom-extensions/2.0/featured' . $parameter_string, 'https://woocommerce.com/wp-json/wccom-extensions/2.0/featured' . $parameter_string,
array( array(
'headers' => $headers, 'headers' => $headers,
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
) )
); );
@ -190,7 +192,10 @@ class WC_Admin_Addons {
$raw_extensions = wp_safe_remote_get( $raw_extensions = wp_safe_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 ) array(
'headers' => $headers,
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
)
); );
if ( is_wp_error( $raw_extensions ) ) { if ( is_wp_error( $raw_extensions ) ) {
@ -233,7 +238,10 @@ class WC_Admin_Addons {
if ( false === ( $addon_sections ) ) { if ( false === ( $addon_sections ) ) {
$parameter_string = '?' . http_build_query( array( 'locale' => get_user_locale() ) ); $parameter_string = '?' . http_build_query( array( 'locale' => get_user_locale() ) );
$raw_sections = wp_safe_remote_get( $raw_sections = wp_safe_remote_get(
'https://woocommerce.com/wp-json/wccom-extensions/1.0/categories' . $parameter_string 'https://woocommerce.com/wp-json/wccom-extensions/1.0/categories' . $parameter_string,
array(
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
)
); );
if ( ! is_wp_error( $raw_sections ) ) { if ( ! is_wp_error( $raw_sections ) ) {
$addon_sections = json_decode( wp_remote_retrieve_body( $raw_sections ) ); $addon_sections = json_decode( wp_remote_retrieve_body( $raw_sections ) );
@ -277,7 +285,12 @@ class WC_Admin_Addons {
if ( ! empty( $section->endpoint ) ) { if ( ! empty( $section->endpoint ) ) {
$section_data = get_transient( 'wc_addons_section_' . $section_id ); $section_data = get_transient( 'wc_addons_section_' . $section_id );
if ( false === $section_data ) { if ( false === $section_data ) {
$raw_section = wp_safe_remote_get( esc_url_raw( $section->endpoint ) ); $raw_section = wp_safe_remote_get(
esc_url_raw( $section->endpoint ),
array(
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
)
);
if ( ! is_wp_error( $raw_section ) ) { if ( ! is_wp_error( $raw_section ) ) {
$section_data = json_decode( wp_remote_retrieve_body( $raw_section ) ); $section_data = json_decode( wp_remote_retrieve_body( $raw_section ) );

View File

@ -50,6 +50,10 @@ class WC_Helper_API {
} }
} }
if ( ! isset( $args['user-agent'] ) ) {
$args['user-agent'] = 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' );
}
/** /**
* Allow developers to filter the request args passed to wp_safe_remote_request(). * Allow developers to filter the request args passed to wp_safe_remote_request().
* Useful to remove sslverify when working on a local api dev environment. * Useful to remove sslverify when working on a local api dev environment.

View File

@ -44,7 +44,12 @@ class WC_Marketplace_Updater {
$data['updated'] = time(); $data['updated'] = time();
$url = 'https://woocommerce.com/wp-json/wccom/marketplace-suggestions/1.0/suggestions.json'; $url = 'https://woocommerce.com/wp-json/wccom/marketplace-suggestions/1.0/suggestions.json';
$request = wp_safe_remote_get( $url ); $request = wp_safe_remote_get(
$url,
array(
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
)
);
if ( is_wp_error( $request ) ) { if ( is_wp_error( $request ) ) {
self::retry(); self::retry();

View File

@ -742,7 +742,7 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
'https://www.paypal.com/cgi-bin/webscr', 'https://www.paypal.com/cgi-bin/webscr',
array( array(
'timeout' => 10, 'timeout' => 10,
'user-agent' => 'WooCommerce/' . WC()->version, 'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
'httpversion' => '1.1', 'httpversion' => '1.1',
'body' => array( 'body' => array(
'cmd' => '_notify-validate', 'cmd' => '_notify-validate',
@ -765,7 +765,12 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
$get_response_code = get_transient( 'woocommerce_test_remote_get' ); $get_response_code = get_transient( 'woocommerce_test_remote_get' );
if ( false === $get_response_code || is_wp_error( $get_response_code ) ) { if ( false === $get_response_code || is_wp_error( $get_response_code ) ) {
$response = wp_safe_remote_get( 'https://woocommerce.com/wc-api/product-key-api?request=ping&network=' . ( is_multisite() ? '1' : '0' ) ); $response = wp_safe_remote_get(
'https://woocommerce.com/wc-api/product-key-api?request=ping&network=' . ( is_multisite() ? '1' : '0' ),
array(
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
)
);
if ( ! is_wp_error( $response ) ) { if ( ! is_wp_error( $response ) ) {
$get_response_code = $response['response']['code']; $get_response_code = $response['response']['code'];
} }

View File

@ -169,6 +169,9 @@ abstract class DataSourcePoller {
'locale', 'locale',
get_user_locale(), get_user_locale(),
$url $url
),
array(
'user-agent' => 'WooCommerce/' . WC_VERSION . '; ' . home_url( '/' ),
) )
); );

View File

@ -194,7 +194,12 @@ class Marketing {
$plugins = get_transient( self::RECOMMENDED_PLUGINS_TRANSIENT ); $plugins = get_transient( self::RECOMMENDED_PLUGINS_TRANSIENT );
if ( false === $plugins ) { if ( false === $plugins ) {
$request = wp_remote_get( 'https://woocommerce.com/wp-json/wccom/marketing-tab/1.1/recommendations.json' ); $request = wp_remote_get(
'https://woocommerce.com/wp-json/wccom/marketing-tab/1.1/recommendations.json',
array(
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
)
);
$plugins = []; $plugins = [];
if ( ! is_wp_error( $request ) && 200 === $request['response']['code'] ) { if ( ! is_wp_error( $request ) && 200 === $request['response']['code'] ) {
@ -249,7 +254,12 @@ class Marketing {
'https://woocommerce.com/wp-json/wp/v2/posts?utm_medium=product' 'https://woocommerce.com/wp-json/wp/v2/posts?utm_medium=product'
); );
$request = wp_remote_get( $request_url ); $request = wp_remote_get(
$request_url,
array(
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
)
);
$posts = []; $posts = [];
if ( ! is_wp_error( $request ) && 200 === $request['response']['code'] ) { if ( ! is_wp_error( $request ) && 200 === $request['response']['code'] ) {

View File

@ -80,7 +80,12 @@ class OnboardingProducts {
public static function get_product_data( $product_types ) { public static function get_product_data( $product_types ) {
$woocommerce_products = get_transient( self::PRODUCT_DATA_TRANSIENT ); $woocommerce_products = get_transient( self::PRODUCT_DATA_TRANSIENT );
if ( false === $woocommerce_products ) { if ( false === $woocommerce_products ) {
$woocommerce_products = wp_remote_get( 'https://woocommerce.com/wp-json/wccom-extensions/1.0/search' ); $woocommerce_products = wp_remote_get(
'https://woocommerce.com/wp-json/wccom-extensions/1.0/search',
array(
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
)
);
if ( is_wp_error( $woocommerce_products ) ) { if ( is_wp_error( $woocommerce_products ) ) {
return $product_types; return $product_types;
} }

View File

@ -101,7 +101,12 @@ class OnboardingThemes {
public static function get_themes() { public static function get_themes() {
$themes = get_transient( self::THEMES_TRANSIENT ); $themes = get_transient( self::THEMES_TRANSIENT );
if ( false === $themes ) { if ( false === $themes ) {
$theme_data = wp_remote_get( 'https://woocommerce.com/wp-json/wccom-extensions/1.0/search?category=themes' ); $theme_data = wp_remote_get(
'https://woocommerce.com/wp-json/wccom-extensions/1.0/search?category=themes',
array(
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
)
);
$themes = array(); $themes = array();
if ( ! is_wp_error( $theme_data ) ) { if ( ! is_wp_error( $theme_data ) ) {

View File

@ -98,6 +98,7 @@ class MailchimpScheduler {
return wp_remote_post( return wp_remote_post(
$subscribe_endpoint, $subscribe_endpoint,
array( array(
'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ),
'method' => 'POST', 'method' => 'POST',
'body' => array( 'body' => array(
'email' => $store_email, 'email' => $store_email,