swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame^] | 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | |
| 7 | /** |
| 8 | * Class that handles various admin tasks. |
| 9 | * |
| 10 | * @since 5.6.0 |
| 11 | */ |
| 12 | class WC_Stripe_Payment_Gateways_Controller { |
| 13 | /** |
| 14 | * Constructor |
| 15 | * |
| 16 | * @since 5.6.0 |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | // If UPE is enabled and there are enabled payment methods, we need to load the disable Stripe confirmation modal. |
| 20 | $stripe_settings = get_option( 'woocommerce_stripe_settings', [] ); |
| 21 | $enabled_upe_payment_methods = isset( $stripe_settings['upe_checkout_experience_accepted_payments'] ) ? $stripe_settings['upe_checkout_experience_accepted_payments'] : []; |
| 22 | $upe_payment_requests_enabled = 'yes' === $stripe_settings['payment_request']; |
| 23 | |
| 24 | if ( count( $enabled_upe_payment_methods ) > 0 || $upe_payment_requests_enabled ) { |
| 25 | add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_payments_scripts' ] ); |
| 26 | add_action( 'woocommerce_admin_field_payment_gateways', [ $this, 'wc_stripe_gateway_container' ] ); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | public function register_payments_scripts() { |
| 31 | $payment_gateways_script_asset_path = WC_STRIPE_PLUGIN_PATH . '/build/payment_gateways.asset.php'; |
| 32 | $payment_gateways_script_asset = file_exists( $payment_gateways_script_asset_path ) |
| 33 | ? require_once $payment_gateways_script_asset_path |
| 34 | : [ |
| 35 | 'dependencies' => [], |
| 36 | 'version' => WC_STRIPE_VERSION, |
| 37 | ]; |
| 38 | |
| 39 | wp_register_script( |
| 40 | 'woocommerce_stripe_payment_gateways_page', |
| 41 | plugins_url( 'build/payment_gateways.js', WC_STRIPE_MAIN_FILE ), |
| 42 | $payment_gateways_script_asset['dependencies'], |
| 43 | $payment_gateways_script_asset['version'], |
| 44 | true |
| 45 | ); |
| 46 | wp_set_script_translations( |
| 47 | 'woocommerce_stripe_payment_gateways_page', |
| 48 | 'woocommerce-gateway-stripe' |
| 49 | ); |
| 50 | wp_register_style( |
| 51 | 'woocommerce_stripe_payment_gateways_page', |
| 52 | plugins_url( 'build/payment_gateways.css', WC_STRIPE_MAIN_FILE ), |
| 53 | [ 'wc-components' ], |
| 54 | $payment_gateways_script_asset['version'] |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | public function enqueue_payments_scripts() { |
| 59 | global $current_tab, $current_section; |
| 60 | |
| 61 | $this->register_payments_scripts(); |
| 62 | |
| 63 | $is_payment_methods_page = ( |
| 64 | is_admin() && |
| 65 | $current_tab && ! $current_section |
| 66 | && 'checkout' === $current_tab |
| 67 | ); |
| 68 | |
| 69 | if ( $is_payment_methods_page ) { |
| 70 | wp_enqueue_script( 'woocommerce_stripe_payment_gateways_page' ); |
| 71 | wp_enqueue_style( 'woocommerce_stripe_payment_gateways_page' ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Adds a container to the "payment gateways" page. |
| 77 | * This is where the "Are you sure you want to disable Stripe?" confirmation dialog is rendered. |
| 78 | */ |
| 79 | public function wc_stripe_gateway_container() { |
| 80 | ?><div id="wc-stripe-payment-gateways-container" /> |
| 81 | <?php |
| 82 | } |
| 83 | |
| 84 | } |