SILENT KILLERPanel

Current Path: > home > codekrsu > > ameliagraphics.com > wp-content > plugins > pinterest-for-woocommerce > src >


Operation   : Linux premium131.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64
Software     : Apache
Server IP    : 162.0.232.56 | Your IP: 216.73.216.111
Domains      : 1034 Domain(s)
Permission   : [ 0755 ]

Files and Folders in: /home/codekrsu//ameliagraphics.com/wp-content/plugins/pinterest-for-woocommerce/src/

NameTypeSizeLast ModifiedActions
API Directory - -
Admin Directory - -
Exception Directory - -
MultichannelMarketing Directory - -
Notes Directory - -
Product Directory - -
Tracking Directory - -
Utilities Directory - -
View Directory - -
AdCredits.php File 8406 bytes August 26 2024 16:35:44.
AdCreditsCoupons.php File 1461 bytes August 26 2024 16:35:44.
AdsCreditCurrency.php File 1946 bytes May 20 2025 15:25:46.
Billing.php File 4901 bytes August 26 2024 16:35:44.
CommerceIntegration.php File 7185 bytes December 04 2024 18:47:06.
Compat.php File 952 bytes November 16 2021 18:14:38.
Crypto.php File 3718 bytes June 23 2025 21:16:30.
FeedFileOperations.php File 4680 bytes August 26 2024 16:35:44.
FeedGenerator.php File 22733 bytes August 26 2024 16:35:44.
FeedRegistration.php File 7230 bytes December 04 2024 18:47:06.
FeedStatusService.php File 20815 bytes March 18 2025 17:31:36.
Feeds.php File 13838 bytes December 04 2024 18:47:06.
Heartbeat.php File 1882 bytes October 23 2024 17:23:10.
LocalFeedConfigs.php File 3327 bytes September 24 2024 11:32:30.
LocaleMapper.php File 3167 bytes March 18 2025 17:31:36.
Logger.php File 3132 bytes August 26 2024 16:35:44.
Merchants.php File 6575 bytes September 24 2024 11:32:30.
PinterestApiException.php File 2767 bytes December 04 2024 18:47:06.
PinterestShippingZone.php File 7161 bytes February 16 2022 03:57:48.
PinterestSyncSettings.php File 3016 bytes August 26 2024 16:35:44.
PluginActivate.php File 785 bytes June 18 2022 00:23:22.
PluginHelper.php File 2537 bytes October 11 2022 17:51:18.
PluginUpdate.php File 11166 bytes May 29 2025 17:27:42.
ProductFeedStatus.php File 4453 bytes August 26 2024 16:35:44.
ProductSync.php File 6351 bytes September 24 2024 11:32:30.
ProductsXmlFeed.php File 17527 bytes February 11 2025 16:39:50.
RefreshToken.php File 3769 bytes August 26 2024 16:35:44.
RichPins.php File 9177 bytes August 26 2024 16:35:44.
SaveToPinterest.php File 3525 bytes April 26 2023 13:47:12.
Shipping.php File 8372 bytes February 16 2022 03:57:48.
TrackerSnapshot.php File 2574 bytes August 26 2024 16:35:44.
Tracking.php File 7488 bytes August 29 2024 22:54:16.
WPConsentAPI.php File 1178 bytes June 16 2025 21:44:24.
error_log File 260 bytes June 23 2025 21:16:32.

Reading File: /home/codekrsu//ameliagraphics.com/wp-content/plugins/pinterest-for-woocommerce/src//AdCredits.php

<?php
/**
 * Pinterest for WooCommerce Ads Credits.
 *
 * @package     Pinterest_For_WooCommerce/Classes/
 * @version     1.0.10
 */

namespace Automattic\WooCommerce\Pinterest;

use Automattic\WooCommerce\Pinterest\API\APIV5;
use Automattic\WooCommerce\Pinterest\PinterestApiException;
use Exception;
use Pinterest_For_Woocommerce;
use Pinterest_For_Woocommerce_Ads_Supported_Countries;
use Throwable;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Handling ad credits.
 */
class AdCredits {

	const ADS_CREDIT_CAMPAIGN_TRANSIENT = PINTEREST_FOR_WOOCOMMERCE_PREFIX . '-ads-credit-campaign-transient';
	const ADS_CREDIT_CAMPAIGN_OPTION    = 'ads_campaign_is_active';

	private const FUTURE_CREDIT = 'FUTURE_CREDIT';

	private const MARKETING_OFFER_CREDIT = 'MARKETING_OFFER_CREDIT';

	/**
	 * Initialize Ad Credits actions and Action Scheduler hooks.
	 *
	 * @since 1.2.5
	 */
	public static function schedule_event() {
		if ( ! Pinterest_For_Woocommerce::is_connected() ) {
			return;
		}

		add_action( Heartbeat::HOURLY, array( static::class, 'handle_redeem_credit' ), 20 );
	}

	/**
	 * Check if the advertiser has set the billing data.
	 *
	 * @since 1.2.5
	 *
	 * @return mixed
	 */
	public static function handle_redeem_credit() {
		if ( ! Pinterest_For_Woocommerce::is_connected() ) {
			// Advertiser not connected redeem operation makes no sense.
			return true;
		}

		Pinterest_For_Woocommerce()::add_available_credits_info_to_account_data();

		if ( ! Pinterest_For_Woocommerce()::get_billing_setup_info_from_account_data() ) {
			// Do not redeem credits if the billing is not setup.
			return true;
		}

		if ( self::check_if_coupon_was_redeemed() ) {
			// Redeem credits only once.
			return true;
		}

		if ( ! self::check_if_ads_campaign_is_active() ) {
			return true;
		}

		Pinterest_For_Woocommerce()::add_redeem_credits_info_to_account_data();

		return true;
	}

		/**
		 * Check if coupon was redeemed. We can redeem only once.
		 *
		 * @since 1.2.5
		 * @since 1.4.1 Update for API v5. and moved to AdCredits class.
		 *
		 * @return bool
		 */
	public static function check_if_coupon_was_redeemed() {
		$account_data  = Pinterest_For_Woocommerce()::get_setting( 'account_data' );
		$redeem_status = $account_data['coupon_redeem_info']['redeem_status'] ?? false;
		$error_id      = $account_data['coupon_redeem_info']['error_id'] ?? false;

		if ( PinterestApiException::OFFER_ALREADY_REDEEMED === $error_id ) {
			// Advertiser has already redeemed the coupon.
			return true;
		}

		if ( PinterestApiException::OFFER_ALREADY_REDEEMED_BY_ANOTHER_ADVERTISER === $error_id ) {
			// Different advertiser id has already redeemed the coupon.
			return true;
		}

		if ( false === $redeem_status ) {
			return false;
		}

		return true;
	}


	/**
	 * Redeem Ad Credits.
	 *
	 * @since 1.2.5
	 *
	 * @param string  $offer_code Coupon string.
	 * @param integer $error_code Reference parameter for error number.
	 * @param string  $error_message Reference parameter for error message.
	 *
	 * @return bool Weather the coupon was successfully redeemed or not.
	 */
	public static function redeem_credits( $offer_code, &$error_code = null, &$error_message = null ) {
		if ( ! Pinterest_For_Woocommerce::is_connected() ) {
			// Advertiser not connected, we can't check if credits were redeemed.
			return false;
		}

		$ad_account_id = Pinterest_For_WooCommerce()::get_setting( 'tracking_advertiser' );
		try {
			$offer_code_credits_data = APIV5::redeem_ads_offer_code( $ad_account_id, $offer_code );
			if ( ! $offer_code_credits_data['success'] ) {
				$error_code    = $offer_code_credits_data['errorCode'];
				$error_message = $offer_code_credits_data['errorMessage'];
				Logger::log( "{$error_code}: {$error_message}", 'error' );
				return false;
			}
			return true;
		} catch ( PinterestApiException $e ) {
			$error_code    = $e->get_pinterest_code();
			$error_message = $e->getMessage();

			if ( PinterestApiException::NO_VALID_BILLING_SETUP === $error_code ) {
				// Invalidate billing setup.
				Billing::add_billing_setup_status_to_account_data( false );
			}
		} catch ( Throwable $th ) {
			Logger::log( $th->getMessage(), 'error' );
		}

		return false;
	}

	/**
	 * Check if the ads campaign is active. In order for that to happen the
	 * following conditions need to be met:
	 * 1. Merchant needs to be in ads supported country.
	 * 2. Merchant needs to have coupon available for his chosen currency.
	 * 3. Ads Campaign needs to be globally active.
	 *
	 * @since 1.2.5
	 *
	 * @return bool Wether campaign is active or not.
	 */
	public static function check_if_ads_campaign_is_active() {

		$is_campaign_active = get_transient( self::ADS_CREDIT_CAMPAIGN_TRANSIENT );

		// 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 WooCommerce.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://woocommerce.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
	 * @since 1.4.1
	 *
	 * @throws PinterestApiException Originating from get_coupon_for_merchant.
	 * @return mixed False when no info is available, discounts object when discounts are available.
	 */
	public static function process_available_discounts() {
		if ( ! Pinterest_For_Woocommerce::is_connected() ) {
			// Advertiser not connected, we can't check if credits were redeemed.
			return false;
		}

		$advertiser_id      = Pinterest_For_Woocommerce()::get_setting( 'tracking_advertiser' );
		$discounts          = APIV5::get_ads_credit_discounts( $advertiser_id )['items'] ?? array();
		$coupon             = AdCreditsCoupons::get_coupon_for_merchant();
		$remaining_discount = 0;
		$found_discounts    = array();

		foreach ( $discounts as $discount ) {
			if ( ! $discount['active'] ) {
				continue;
			}
			if ( static::FUTURE_CREDIT === $discount['discount_type'] ) {
				$offer_code = $discount['discount_restrictions']['marketing_offer_code_hash'] ?? '';
				if ( $offer_code === $coupon ) {
					$found_discounts['future_discount'] = true;
				}
			} elseif ( static::MARKETING_OFFER_CREDIT === $discount['discount_type'] ) {
				$remaining_discount += (float) $discount['remaining_discount_in_micro_currency'] / 1000000;
			}
		}

		$found_discounts['marketing_offer'] = array(
			'remaining_discount' => wc_price( $remaining_discount ),
		);

		return $found_discounts;
	}

}

SILENT KILLER Tool