diff --git a/includes/class-pinterest-for-woocommerce-ads-supported-countries.php b/includes/class-pinterest-for-woocommerce-ads-supported-countries.php index 5f19bfea8..1c87440ff 100644 --- a/includes/class-pinterest-for-woocommerce-ads-supported-countries.php +++ b/includes/class-pinterest-for-woocommerce-ads-supported-countries.php @@ -6,7 +6,7 @@ * @since 1.0.5 */ -use Automattic\WooCommerce\Pinterest\API\Base; +use Automattic\WooCommerce\Pinterest\API\Base; if ( ! defined( 'ABSPATH' ) ) { exit; @@ -24,10 +24,21 @@ class Pinterest_For_Woocommerce_Ads_Supported_Countries { * * @see https://help.pinterest.com/en/business/availability/ads-availability * + * @since x.x.x Don't fetch the list of supported countries if the user is not connected. Use a fallback instead. + * + * @throws Exception If the user is not connected and the list of supported countries can't be fetched. + * * @return string[] */ public static function get_countries() { try { + /* + * If the user is not connected, we can't get the list of supported countries. + * We throw an exception and use a fallback. + */ + if ( ! Pinterest_For_Woocommerce()::is_connected() ) { + throw new Exception( 'Pinterest user is not connected, using fallback list of supported countries.' ); + } $allowed_countries = Base::get_list_of_ads_supported_countries(); $get_country_code = function( $country_object ) { @@ -41,7 +52,7 @@ public static function get_countries() { ); return $allowed_countries_codes; - } catch ( \Exception $th ) { + } catch ( Exception $th ) { // A fallback in case of error. return array( 'AR', // Argentina. diff --git a/src/API/Base.php b/src/API/Base.php index 3d39a6ec3..8ca1d3ab9 100644 --- a/src/API/Base.php +++ b/src/API/Base.php @@ -803,6 +803,6 @@ public static function get_available_discounts( $advertiser_id ) { */ public static function get_list_of_ads_supported_countries() { $request_url = 'advertisers/countries'; - return self::make_request( $request_url, 'GET', array(), 'ads', DAY_IN_SECONDS ); + return self::make_request( $request_url, 'GET', array(), 'ads', 2 * DAY_IN_SECONDS ); } }