$headers, 'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ), ) ); if ( ! is_wp_error( $raw_featured ) ) { $featured = json_decode( wp_remote_retrieve_body( $raw_featured ) ); if ( $featured ) { self::set_locale_data_in_transient( 'wc_addons_featured_2', $featured, $locale, DAY_IN_SECONDS ); } } } if ( is_object( $featured ) ) { self::output_featured_sections( $featured->sections ); return $featured; } } /** * Render featured products and banners using WCCOM's the Featured 2.0 Endpoint * * @return void */ public static function render_featured() { $featured = self::fetch_featured(); if ( is_wp_error( $featured ) ) { self::output_empty( $featured->get_error_message() ); } self::output_featured( $featured ); } /** * Fetch featured products from WCCOM's the Featured 2.0 Endpoint and cache the data for a day. * * @return array|WP_Error */ public static function fetch_featured() { $locale = get_user_locale(); $featured = self::get_locale_data_from_transient( 'wc_addons_featured', $locale ); if ( false === $featured ) { $headers = array(); $auth = WC_Helper_Options::get( 'auth' ); if ( ! empty( $auth['access_token'] ) ) { $headers['Authorization'] = 'Bearer ' . $auth['access_token']; } $parameter_string = '?' . http_build_query( array( 'locale' => get_user_locale() ) ); $country = WC()->countries->get_base_country(); if ( ! empty( $country ) ) { $parameter_string = $parameter_string . '&' . http_build_query( array( 'country' => $country ) ); } // Important: WCCOM Extensions API v2.0 is used. $raw_featured = wp_safe_remote_get( 'https://woocommerce.com/wp-json/wccom-extensions/2.0/featured' . $parameter_string, array( 'headers' => $headers, 'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ), ) ); if ( is_wp_error( $raw_featured ) ) { do_action( 'woocommerce_page_wc-addons_connection_error', $raw_featured->get_error_message() ); $message = self::is_ssl_error( $raw_featured->get_error_message() ) ? __( 'We encountered an SSL error. Please ensure your site supports TLS version 1.2 or above.', 'woocommerce' ) : $raw_featured->get_error_message(); return new WP_Error( 'wc-addons-connection-error', $message ); } $response_code = (int) wp_remote_retrieve_response_code( $raw_featured ); if ( 200 !== $response_code ) { do_action( 'woocommerce_page_wc-addons_connection_error', $response_code ); /* translators: %d: HTTP error code. */ $message = sprintf( esc_html( /* translators: Error code */ __( 'Our request to the featured API got error code %d.', 'woocommerce' ) ), $response_code ); return new WP_Error( 'wc-addons-connection-error', $message ); } $featured = json_decode( wp_remote_retrieve_body( $raw_featured ) ); if ( empty( $featured ) || ! is_array( $featured ) ) { do_action( 'woocommerce_page_wc-addons_connection_error', 'Empty or malformed response' ); $message = __( 'Our request to the featured API got a malformed response.', 'woocommerce' ); return new WP_Error( 'wc-addons-connection-error', $message ); } if ( $featured ) { self::set_locale_data_in_transient( 'wc_addons_featured', $featured, $locale, DAY_IN_SECONDS ); } } return $featured; } /** * Check if the error is due to an SSL error * * @param string $error_message Error message. * * @return bool True if SSL error, false otherwise */ public static function is_ssl_error( $error_message ) { return false !== stripos( $error_message, 'cURL error 35' ); } /** * Build url parameter string * * @param string $category Addon (sub) category. * @param string $term Search terms. * @param string $country Store country. * * @return string url parameter string */ public static function build_parameter_string( $category, $term, $country ) { $parameters = array( 'category' => $category, 'term' => $term, 'country' => $country, 'locale' => get_user_locale(), ); return '?' . http_build_query( $parameters ); } /** * Call API to get extensions * * @param string $category Addon (sub) category. * @param string $term Search terms. * @param string $country Store country. * * @return object|WP_Error Object with products and promotions properties, or WP_Error */ 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_safe_remote_get( 'https://woocommerce.com/wp-json/wccom-extensions/1.0/search' . $parameters, array( 'headers' => $headers, 'user-agent' => 'WooCommerce/' . WC()->version . '; ' . get_bloginfo( 'url' ), ) ); if ( is_wp_error( $raw_extensions ) ) { do_action( 'woocommerce_page_wc-addons_connection_error', $raw_extensions->get_error_message() ); return $raw_extensions; } $response_code = (int) wp_remote_retrieve_response_code( $raw_extensions ); if ( 200 !== $response_code ) { do_action( 'woocommerce_page_wc-addons_connection_error', $response_code ); return new WP_Error( 'error', sprintf( esc_html( /* translators: Error code */ __( 'Our request to the search API got response code %s.', 'woocommerce' ) ), $response_code ) ); } $addons = json_decode( wp_remote_retrieve_body( $raw_extensions ) ); if ( ! is_object( $addons ) || ! isset( $addons->products ) ) { do_action( 'woocommerce_page_wc-addons_connection_error', 'Empty or malformed response' ); return new WP_Error( 'error', __( 'Our request to the search API got a malformed response.', 'woocommerce' ) ); } return $addons; } /** * Get sections for the addons screen * * @return array of objects */ public static function get_sections() { $locale = get_user_locale(); $addon_sections = self::get_locale_data_from_transient( 'wc_addons_sections', $locale ); if ( false === ( $addon_sections ) ) { $parameter_string = '?' . http_build_query( array( 'locale' => get_user_locale() ) ); $raw_sections = wp_safe_remote_get( '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 ) ) { $addon_sections = json_decode( wp_remote_retrieve_body( $raw_sections ) ); if ( $addon_sections ) { self::set_locale_data_in_transient( 'wc_addons_sections', $addon_sections, $locale, WEEK_IN_SECONDS ); } } } return apply_filters( 'woocommerce_addons_sections', $addon_sections ); } /** * Get section for the addons screen. * * @param string $section_id Required section ID. * * @return object|bool */ public static function get_section( $section_id ) { $sections = self::get_sections(); if ( isset( $sections[ $section_id ] ) ) { return $sections[ $section_id ]; } return false; } /** * Get section content for the addons screen. * * @deprecated 5.9.0 No longer used in In-App Marketplace * * @param string $section_id Required section ID. * * @return array */ public static function get_section_data( $section_id ) { $section = self::get_section( $section_id ); $section_data = ''; if ( ! empty( $section->endpoint ) ) { $section_data = get_transient( 'wc_addons_section_' . $section_id ); if ( false === $section_data ) { $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 ) ) { $section_data = json_decode( wp_remote_retrieve_body( $raw_section ) ); if ( ! empty( $section_data->products ) ) { set_transient( 'wc_addons_section_' . $section_id, $section_data, WEEK_IN_SECONDS ); } } } } return apply_filters( 'woocommerce_addons_section_data', $section_data->products, $section_id ); } /** * Handles the outputting of a contextually aware Storefront link (points to child themes if Storefront is already active). * * @deprecated 5.9.0 No longer used in In-App Marketplace */ public static function output_storefront_button() { $template = get_option( 'template' ); $stylesheet = get_option( 'stylesheet' ); if ( 'storefront' === $template ) { if ( 'storefront' === $stylesheet ) { $url = 'https://woo.com/product-category/themes/storefront-child-theme-themes/'; $text = __( 'Need a fresh look? Try Storefront child themes', 'woocommerce' ); $utm_content = 'nostorefrontchildtheme'; } else { $url = 'https://woo.com/product-category/themes/storefront-child-theme-themes/'; $text = __( 'View more Storefront child themes', 'woocommerce' ); $utm_content = 'hasstorefrontchildtheme'; } } else { $url = 'https://woo.com/storefront/'; $text = __( 'Need a theme? Try Storefront', 'woocommerce' ); $utm_content = 'nostorefront'; } $url = add_query_arg( array( 'utm_source' => 'addons', 'utm_medium' => 'product', 'utm_campaign' => 'woocommerceplugin', 'utm_content' => $utm_content, ), $url ); echo '' . esc_html( $text ) . '' . "\n"; } /** * Handles the outputting of a banner block. * * @deprecated 5.9.0 No longer used in In-App Marketplace * * @param object $block Banner data. */ public static function output_banner_block( $block ) { ?>
container ) && 'column_container_start' === $block->container ) { ?>description ); ?>
items as $item ) : ?>description ); ?>
description ); ?>
description ); ?>
Woo.com, where you\'ll find the most popular WooCommerce extensions.', 'woocommerce' ) ), 'https://woo.com/products/?utm_source=extensionsscreen&utm_medium=product&utm_campaign=connectionerror' ); ?>
description ); ?>
description ); ?>