Automattic\WooCommerce\Internal\Admin\Settings

PaymentsController::store_has_providers_with_incentiveprivateWC 1.0

Check if the store has any payment providers that have an active incentive.

Method of the class: PaymentsController{}

No Hooks.

Returns

true|false. True if the store has providers with an active incentive.

Usage

// private - for code of main (parent) class only
$result = $this->store_has_providers_with_incentive(): bool;

PaymentsController::store_has_providers_with_incentive() code WC 9.9.4

private function store_has_providers_with_incentive(): bool {
	// First, try to use the transient value.
	$transient = get_transient( self::TRANSIENT_HAS_PROVIDERS_WITH_INCENTIVE_KEY );
	if ( false !== $transient ) {
		return filter_var( $transient, FILTER_VALIDATE_BOOLEAN );
	}

	try {
		$providers = $this->payments->get_payment_providers( $this->payments->get_country() );
	} catch ( Throwable $e ) {
		// Catch everything since we don't want to break all the WP admin pages.
		// Log so we can investigate.
		SafeGlobalFunctionProxy::wc_get_logger()->error(
			'Failed to get payment providers: ' . $e->getMessage(),
			array(
				'source' => 'settings-payments',
				'error'  => $e,
			)
		);

		// In case of an error, default to false.
		set_transient( self::TRANSIENT_HAS_PROVIDERS_WITH_INCENTIVE_KEY, 'no', HOUR_IN_SECONDS );
		return false;
	}

	$has_providers_with_incentive = false;
	// Go through the providers and check if any of them have a "prominently" visible incentive (i.e., modal or banner).
	foreach ( $providers as $provider ) {
		if ( empty( $provider['_incentive'] ) ) {
			continue;
		}

		$dismissals = $provider['_incentive']['_dismissals'] ?? array();

		// If there are no dismissals at all, the incentive is prominently visible.
		if ( empty( $dismissals ) ) {
			$has_providers_with_incentive = true;
			break;
		}

		// First, we check to see if the incentive was dismissed in the banner context.
		// The banner context has the lowest priority, so if it was dismissed, we don't need to check the modal context.
		// If the banner is dismissed, there is no prominent incentive.
		$is_dismissed_banner = ! empty(
			array_filter(
				$dismissals,
				function ( $dismissal ) {
					return isset( $dismissal['context'] ) && 'wc_settings_payments__banner' === $dismissal['context'];
				}
			)
		);
		if ( $is_dismissed_banner ) {
			continue;
		}

		// In case an incentive uses the modal surface also (like the WooPayments Switch incentive),
		// we rely on the fact that the modal falls back to the banner, once dismissed, after 30 days.
		// @see here's its frontend "brother" in client/admin/client/settings-payments/settings-payments-main.tsx.
		$is_dismissed_modal = ! empty(
			array_filter(
				$dismissals,
				function ( $dismissal ) {
					return isset( $dismissal['context'] ) && 'wc_settings_payments__modal' === $dismissal['context'];
				}
			)
		);
		// If there are no modal dismissals, the incentive is still visible.
		if ( ! $is_dismissed_modal ) {
			$has_providers_with_incentive = true;
			break;
		}

		$is_dismissed_modal_more_than_30_days_ago = ! empty(
			array_filter(
				$dismissals,
				function ( $dismissal ) {
					return isset( $dismissal['context'], $dismissal['timestamp'] ) &&
						'wc_settings_payments__modal' === $dismissal['context'] &&
						$dismissal['timestamp'] < strtotime( '-30 days' );
				}
			)
		);
		// If the modal was dismissed less than 30 days ago, there is no prominent incentive (aka the banner is not shown).
		if ( ! $is_dismissed_modal_more_than_30_days_ago ) {
			continue;
		}

		// The modal was dismissed more than 30 days ago, so the banner is visible.
		$has_providers_with_incentive = true;
		break;
	}

	// Save the value in a transient to avoid unnecessary processing throughout the WP admin.
	// Incentives don't change frequently, so it is safe to cache the value for 1 hour.
	set_transient( self::TRANSIENT_HAS_PROVIDERS_WITH_INCENTIVE_KEY, $has_providers_with_incentive ? 'yes' : 'no', HOUR_IN_SECONDS );

	return $has_providers_with_incentive;
}