blob: 93a136b62e7254deab381222c6195c4a0d06fb88 [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2
3if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5}
6
7/**
8 * Controls whether we're on the settings page and enqueues the JS code.
9 *
10 * @since 5.4.1
11 */
12class WC_Stripe_Settings_Controller {
13 /**
14 * The Stripe account instance.
15 *
16 * @var WC_Stripe_Account
17 */
18 private $account;
19
20 /**
21 * Constructor
22 *
23 * @param WC_Stripe_Account $account Stripe account
24 */
25 public function __construct( WC_Stripe_Account $account ) {
26 $this->account = $account;
27 add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] );
28 add_action( 'wc_stripe_gateway_admin_options_wrapper', [ $this, 'admin_options' ] );
29 }
30
31 /**
32 * Prints the admin options for the gateway.
33 * Remove this action once we're fully migrated to UPE and move the wrapper in the `admin_options` method of the UPE gateway.
34 *
35 * @param WC_Stripe_Payment_Gateway $gateway the Stripe gateway.
36 */
37 public function admin_options( WC_Stripe_Payment_Gateway $gateway ) {
38 global $hide_save_button;
39 $hide_save_button = true;
40
41 echo '<h2>' . esc_html( $gateway->get_method_title() );
42 wc_back_link( __( 'Return to payments', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout' ) );
43 echo '</h2>';
44
45 $settings = get_option( WC_Stripe_Connect::SETTINGS_OPTION, [] );
46
47 $account_data_exists = ( ! empty( $settings['publishable_key'] ) && ! empty( $settings['secret_key'] ) ) || ( ! empty( $settings['test_publishable_key'] ) && ! empty( $settings['test_secret_key'] ) );
48 echo $account_data_exists ? '<div id="wc-stripe-account-settings-container"></div>' : '<div id="wc-stripe-new-account-container"></div>';
49 }
50
51 /**
52 * Load admin scripts.
53 */
54 public function admin_scripts( $hook_suffix ) {
55 if ( 'woocommerce_page_wc-settings' !== $hook_suffix ) {
56 return;
57 }
58
59 // TODO: refactor this to a regex approach, we will need to touch `should_enqueue_in_current_tab_section` to support it
60 if ( ! ( WC_Stripe_Helper::should_enqueue_in_current_tab_section( 'checkout', 'stripe' )
61 || WC_Stripe_Helper::should_enqueue_in_current_tab_section( 'checkout', 'stripe_sepa' )
62 || WC_Stripe_Helper::should_enqueue_in_current_tab_section( 'checkout', 'stripe_giropay' )
63 || WC_Stripe_Helper::should_enqueue_in_current_tab_section( 'checkout', 'stripe_ideal' )
64 || WC_Stripe_Helper::should_enqueue_in_current_tab_section( 'checkout', 'stripe_bancontact' )
65 || WC_Stripe_Helper::should_enqueue_in_current_tab_section( 'checkout', 'stripe_eps' )
66 || WC_Stripe_Helper::should_enqueue_in_current_tab_section( 'checkout', 'stripe_sofort' )
67 || WC_Stripe_Helper::should_enqueue_in_current_tab_section( 'checkout', 'stripe_p24' )
68 || WC_Stripe_Helper::should_enqueue_in_current_tab_section( 'checkout', 'stripe_alipay' )
69 || WC_Stripe_Helper::should_enqueue_in_current_tab_section( 'checkout', 'stripe_multibanco' )
70 || WC_Stripe_Helper::should_enqueue_in_current_tab_section( 'checkout', 'stripe_oxxo' )
71 || WC_Stripe_Helper::should_enqueue_in_current_tab_section( 'checkout', 'stripe_boleto' ) ) ) {
72 return;
73 }
74
75 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
76
77 // Webpack generates an assets file containing a dependencies array for our built JS file.
78 $script_asset_path = WC_STRIPE_PLUGIN_PATH . '/build/upe_settings.asset.php';
79 $script_asset = file_exists( $script_asset_path )
80 ? require $script_asset_path
81 : [
82 'dependencies' => [],
83 'version' => WC_STRIPE_VERSION,
84 ];
85
86 wp_register_script(
87 'woocommerce_stripe_admin',
88 plugins_url( 'build/upe_settings.js', WC_STRIPE_MAIN_FILE ),
89 $script_asset['dependencies'],
90 $script_asset['version'],
91 true
92 );
93 wp_register_style(
94 'woocommerce_stripe_admin',
95 plugins_url( 'build/upe_settings.css', WC_STRIPE_MAIN_FILE ),
96 [ 'wc-components' ],
97 $script_asset['version']
98 );
99
100 $oauth_url = woocommerce_gateway_stripe()->connect->get_oauth_url();
101 if ( is_wp_error( $oauth_url ) ) {
102 $oauth_url = '';
103 }
104
105 $message = sprintf(
106 /* translators: 1) Html strong opening tag 2) Html strong closing tag */
107 esc_html__( '%1$sWarning:%2$s your site\'s time does not match the time on your browser and may be incorrect. Some payment methods depend on webhook verification and verifying webhooks with a signing secret depends on your site\'s time being correct, so please check your site\'s time before setting a webhook secret. You may need to contact your site\'s hosting provider to correct the site\'s time.', 'woocommerce-gateway-stripe' ),
108 '<strong>',
109 '</strong>'
110 );
111
112 $params = [
113 'time' => time(),
114 'i18n_out_of_sync' => $message,
115 'is_upe_checkout_enabled' => WC_Stripe_Feature_Flags::is_upe_checkout_enabled(),
116 'stripe_oauth_url' => $oauth_url,
117 ];
118 wp_localize_script(
119 'woocommerce_stripe_admin',
120 'wc_stripe_settings_params',
121 $params
122 );
123 wp_set_script_translations(
124 'woocommerce_stripe_admin',
125 'woocommerce-gateway-stripe'
126 );
127
128 wp_enqueue_script( 'woocommerce_stripe_admin' );
129 wp_enqueue_style( 'woocommerce_stripe_admin' );
130 }
131}