{ return $classes; } if ( 'checkout' === $current_tab && ! str_contains( 'woocommerce-settings-payments-tab', $classes ) ) { $classes = "$classes woocommerce-settings-payments-tab"; } return $classes; } /** * Preload settings to make them available to the Payments settings page frontend logic. * * Added keys will be available in the window.wcSettings.admin object. * * @param array $settings The settings array. * * @return array Settings array with additional settings added. */ public function preload_settings( $settings = array() ) { // We only preload settings in the WP admin. if ( ! is_admin() ) { return $settings; } // Reset the received value if the type is invalid. if ( ! is_array( $settings ) ) { $settings = array(); } // Add the business location country to the settings. if ( ! isset( $settings[ Payments::PAYMENTS_NOX_PROFILE_KEY ] ) ) { $settings[ Payments::PAYMENTS_NOX_PROFILE_KEY ] = array(); } $settings[ Payments::PAYMENTS_NOX_PROFILE_KEY ]['business_country_code'] = $this->payments->get_country(); return $settings; } /** * Adds promo note IDs to the list of allowed ones. * * @param array $promo_notes Allowed promo note IDs. * * @return array The updated list of allowed promo note IDs. */ public function add_allowed_promo_notes( $promo_notes = array() ): array { // Reset the value if the type is invalid. if ( ! is_array( $promo_notes ) ) { $promo_notes = array(); } try { $providers = $this->payments->get_payment_providers( $this->payments->get_country(), false ); } 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', ) ); return $promo_notes; } // Add all incentive promo IDs to the allowed promo notes list. foreach ( $providers as $provider ) { if ( ! empty( $provider['_incentive']['promo_id'] ) ) { $promo_notes[] = $provider['_incentive']['promo_id']; } } return $promo_notes; } /** * Alter the Payments tab sections under certain conditions. * * @param array $sections The payments/checkout tab sections. * * @return array The filtered sections. */ public function handle_sections( $sections = array() ): array { global $current_section; // Reset the value if the type is invalid. if ( ! is_array( $sections ) ) { $sections = array(); } // Bail if the current section global is empty or of the wrong type. if ( empty( $current_section ) || ! is_string( $current_section ) ) { return $sections; } // For WooPayments and offline payment methods settings pages, we don't want any section navigation. if ( in_array( $current_section, array( WooPaymentsService::GATEWAY_ID, WC_Gateway_BACS::ID, WC_Gateway_Cheque::ID, WC_Gateway_COD::ID ), true ) ) { return array(); } return $sections; } /** * Handle the payments extension suggestion incentive dismissed event. * * @return void */ public function handle_incentive_dismissed(): void { // Clear the transient to force a new check for providers with an incentive. delete_transient( self::TRANSIENT_HAS_PROVIDERS_WITH_INCENTIVE_KEY ); } /** * Check if the store has any enabled gateways (including offline payment methods). * * @return bool True if the store has any enabled gateways, false otherwise. */ private function store_has_enabled_gateways(): bool { $gateways = WC()->payment_gateways->get_available_payment_gateways(); $enabled_gateways = array_filter( $gateways, function ( $gateway ) { return 'yes' === $gateway->enabled; } ); return ! empty( $enabled_gateways ); } /** * Check if the store has any payment providers that have an active incentive. * * @return bool True if the store has providers with an active incentive. */ 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(), false ); } 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', ) ); // In case of an error, default to false. // Set the transient to avoid repeated errors. 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; } /** * Check if the WooPayments account is onboarded. * * @return boolean */ private function is_woopayments_account_onboarded(): bool { // Sanity check: the WooPayments extension must be active. if ( ! class_exists( '\WC_Payments' ) ) { return false; } $account_data = get_option( 'wcpay_account_data', array() ); // The account ID must be present. if ( empty( $account_data['data']['account_id'] ) ) { return false; } // We consider the store to have an onboarded WooPayments account if account data in the WooPayments account cache // contains a details_submitted = true entry. This implies that WooPayments is also connected. if ( empty( $account_data['data']['details_submitted'] ) ) { return false; } return filter_var( $account_data['data']['details_submitted'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ) ?? false; } }