blob: 651ff259fe9d97dcd82e8d2e0a382abbdaf0d180 [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2
3if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5}
6
7/**
8 * Enqueues some JS to ensure that some needed UI elements for the old settings are available.
9 *
10 * @since 5.5.0
11 */
12class WC_Stripe_Old_Settings_UPE_Toggle_Controller {
13 protected $was_upe_checkout_enabled = null;
14
15 public function __construct() {
16 add_filter( 'pre_update_option_woocommerce_stripe_settings', [ $this, 'pre_options_save' ] );
17 add_action( 'update_option_woocommerce_stripe_settings', [ $this, 'maybe_enqueue_script' ] );
18 }
19
20 /**
21 * Stores whether UPE was enabled before saving the options.
22 *
23 * @param mixed $value
24 *
25 * @return mixed
26 */
27 public function pre_options_save( $value ) {
28 $this->was_upe_checkout_enabled = WC_Stripe_Feature_Flags::is_upe_checkout_enabled();
29
30 return $value;
31 }
32
33 /**
34 * Determines what to do after the options have been saved.
35 */
36 public function maybe_enqueue_script() {
37 $is_upe_checkout_enabled = WC_Stripe_Feature_Flags::is_upe_checkout_enabled();
38
39 if ( $this->was_upe_checkout_enabled !== $is_upe_checkout_enabled ) {
40 add_action( 'admin_enqueue_scripts', [ $this, 'upe_toggle_script' ] );
41 }
42 }
43
44 /**
45 * Enqueues the script to determine what to do once UPE has been toggled.
46 */
47 public function upe_toggle_script() {
48 // Webpack generates an assets file containing a dependencies array for our built JS file.
49 $script_asset_path = WC_STRIPE_PLUGIN_PATH . '/build/old_settings_upe_toggle.asset.php';
50 $script_asset = file_exists( $script_asset_path )
51 ? require $script_asset_path
52 : [
53 'dependencies' => [],
54 'version' => WC_STRIPE_VERSION,
55 ];
56
57 wp_register_script(
58 'woocommerce_stripe_old_settings_upe_toggle',
59 plugins_url( 'build/old_settings_upe_toggle.js', WC_STRIPE_MAIN_FILE ),
60 $script_asset['dependencies'],
61 $script_asset['version'],
62 true
63 );
64 wp_localize_script(
65 'woocommerce_stripe_old_settings_upe_toggle',
66 'wc_stripe_old_settings_param',
67 [
68 'was_upe_enabled' => $this->was_upe_checkout_enabled,
69 'is_upe_enabled' => WC_Stripe_Feature_Flags::is_upe_checkout_enabled(),
70 ]
71 );
72 wp_set_script_translations(
73 'woocommerce_stripe_old_settings_upe_toggle',
74 'woocommerce-gateway-stripe'
75 );
76 wp_enqueue_script( 'woocommerce_stripe_old_settings_upe_toggle' );
77 }
78}