; // If transient is available then it means that we have already checked. if ( false !== $is_campaign_active ) { return wc_string_to_bool( $is_campaign_active ); } $request_error = false; try { // Check if all conditions are met. if ( Pinterest_For_Woocommerce_Ads_Supported_Countries::is_ads_supported_country() && AdCreditsCoupons::has_valid_coupon_for_merchant() && self::get_is_campaign_active_from_recommendations() ) { $is_campaign_active = true; } } catch ( Exception $ex ) { $request_error = true; } Pinterest_For_Woocommerce()->save_setting( self::ADS_CREDIT_CAMPAIGN_OPTION, $is_campaign_active ); /* * Try again in fifteen minutes in case we had problems fetching * the campaign status from the server, wait full day otherwise. */ set_transient( self::ADS_CREDIT_CAMPAIGN_TRANSIENT, wc_bool_to_string( $is_campaign_active ), $request_error ? 15 * MINUTE_IN_SECONDS : DAY_IN_SECONDS ); return $is_campaign_active; } /** * Check if campaign is enabled in the recommendations API from woo.com. * * @since 1.2.5 * * @throws Exception API fetch error. * * @return bool Wether the campaign is active or not. */ private static function get_is_campaign_active_from_recommendations() { $request = wp_remote_get( 'https://woo.com/wp-json/wccom/marketing-tab/1.2/recommendations.json' ); $recommendations = array(); if ( is_wp_error( $request ) ) { throw new Exception( sprintf( /* translators: API error message */ __( 'Could not fetch ads campaign status due to: %s', 'pinterest-for-woocommerce' ), $request->get_error_message() ) ); } if ( ! is_wp_error( $request ) && 200 === $request['response']['code'] ) { $recommendations = json_decode( $request['body'], true ); } // Find Pinterest plugin entry and check for promotions key. foreach ( $recommendations as $recommendation ) { if ( 'pinterest-for-woocommerce' === $recommendation['product'] ) { return array_key_exists( 'show_extension_promotions', $recommendation ) ? $recommendation['show_extension_promotions'] : false; } } return false; } /** * Fetch data from the discount endpoint and get the necessary fields. * * @since 1.2.5 * * @return mixed False when no info is available, discounts object when discounts are available. */ public static function process_available_discounts() { if ( ! Pinterest_For_Woocommerce()::get_data( 'is_advertiser_connected' ) ) { // Advertiser not connected, we can't check if credits were redeemed. return false; } $advertiser_id = Pinterest_For_Woocommerce()::get_setting( 'tracking_advertiser' ); if ( false === $advertiser_id ) { // No advertiser id stored. But we are connected. This is an abnormal state that should not happen. Logger::log( __( 'Advertiser connected but the connection id is missing.', 'pinterest-for-woocommerce' ) ); return false; } $result = Base::get_available_discounts( $advertiser_id ); if ( 'success' !== $result['status'] ) { return false; } $discounts = (array) $result['data']; $coupon = AdCreditsCoupons::get_coupon_for_merchant(); $found_discounts = array(); if ( array_key_exists( self::ADS_CREDIT_FUTURE_DISCOUNT, $discounts ) ) { foreach ( $discounts[ self::ADS_CREDIT_FUTURE_DISCOUNT ] as $future_discount ) { $discount_information = (array) $future_discount; if ( $discount_information['offer_code'] === $coupon ) { $found_discounts['future_discount'] = true; } } } $remaining_discount_value = 0; if ( array_key_exists( self::ADS_CREDIT_MARKETING_OFFER, $discounts ) ) { // Sum all of the available coupons values. foreach ( $discounts[ self::ADS_CREDIT_MARKETING_OFFER ] as $discount ) { $discount_information = (array) $discount; $remaining_discount_value += ( (float) $discount_information['remaining_discount_in_micro_currency'] ) / 1000000; } $found_discounts['marketing_offer'] = array( 'remaining_discount' => wc_price( $remaining_discount_value ), ); } return $found_discounts; } }