From e96cd522e4b7bd02b11acbf9e585c9dc50f922b8 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Fri, 1 Nov 2024 16:27:33 +0300 Subject: [PATCH 1/7] Load stripe script conditionally --- .../FrmStrpLiteActionsController.php | 85 +++++++++++++++---- 1 file changed, 69 insertions(+), 16 deletions(-) diff --git a/stripe/controllers/FrmStrpLiteActionsController.php b/stripe/controllers/FrmStrpLiteActionsController.php index 1cf2049e04..5192f86282 100644 --- a/stripe/controllers/FrmStrpLiteActionsController.php +++ b/stripe/controllers/FrmStrpLiteActionsController.php @@ -10,6 +10,8 @@ class FrmStrpLiteActionsController extends FrmTransLiteActionsController { */ private static $customer; + private static $has_loaded_stripe_script; + /** * Override the credit card field HTML if there is a Stripe action. * @@ -437,6 +439,73 @@ public static function load_scripts( $form_id ) { false ); + self::maybe_load_stripe_script(); + + $action_settings = self::prepare_settings_for_js( $form_id ); + $style_settings = self::get_style_settings_for_form( $form_id ); + $stripe_vars = array( + 'publishable_key' => $publishable, + 'form_id' => $form_id, + 'nonce' => wp_create_nonce( 'frm_strp_ajax' ), + 'ajax' => esc_url_raw( FrmAppHelper::get_ajax_url() ), + 'settings' => $action_settings, + 'locale' => self::get_locale(), + 'baseFontSize' => $style_settings['field_font_size'], + 'appearanceRules' => self::get_appearance_rules( $style_settings ), + 'account_id' => FrmStrpLiteConnectHelper::get_account_id(), + ); + + wp_localize_script( 'formidable-stripe', 'frm_stripe_vars', $stripe_vars ); + } + + private static function get_payment_actions( $form_id ) { + return array(); + $actions = self::get_actions_before_submit( FrmAppHelper::get_param( 'form_id' ) ); + return ! empty( $actions ); + } + + private static function should_load_stripe_script(){ + if ( self::$has_loaded_stripe_script ) { + return false; + } + global $frm_vars; + $form_ids = $frm_vars['forms_loaded']; + if ( ! is_array( $form_ids ) ) { + return false; + } + $form_ids = array_unique( wp_list_pluck( $form_ids, 'id' ) ); + foreach ( $form_ids as $form_id ) { + if ( self::should_load_stripe_script_for_form( $form_id ) ) { + self::$has_loaded_stripe_script = true; + return true; + } + } + + return false; + } + + private static function should_load_stripe_script_for_form( $form_id ) { + $action_status = array( + 'post_status' => 'publish', + ); + $payment_actions = FrmFormAction::get_action_for_form( $form_id, 'payment', $action_status ); + if ( empty( $payment_actions ) ) { + return false; + } + + foreach ( $payment_actions as $action ) { + if ( ! empty( $action->post_content['stripe_link'] ) ) { + return true; + } + } + return false; + } + + private static function maybe_load_stripe_script() { + if ( ! self::should_load_stripe_script() ) { + return; + } + $suffix = FrmAppHelper::js_suffix(); $dependencies = array( 'stripe', 'formidable' ); @@ -462,22 +531,6 @@ public static function load_scripts( $form_id ) { FrmAppHelper::plugin_version(), false ); - - $action_settings = self::prepare_settings_for_js( $form_id ); - $style_settings = self::get_style_settings_for_form( $form_id ); - $stripe_vars = array( - 'publishable_key' => $publishable, - 'form_id' => $form_id, - 'nonce' => wp_create_nonce( 'frm_strp_ajax' ), - 'ajax' => esc_url_raw( FrmAppHelper::get_ajax_url() ), - 'settings' => $action_settings, - 'locale' => self::get_locale(), - 'baseFontSize' => $style_settings['field_font_size'], - 'appearanceRules' => self::get_appearance_rules( $style_settings ), - 'account_id' => FrmStrpLiteConnectHelper::get_account_id(), - ); - - wp_localize_script( 'formidable-stripe', 'frm_stripe_vars', $stripe_vars ); } /** From ac485f76d9b913a5602db746cdb70fa904cc9b0f Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Fri, 1 Nov 2024 16:48:07 +0300 Subject: [PATCH 2/7] Make the conditions work --- stripe/controllers/FrmStrpLiteActionsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stripe/controllers/FrmStrpLiteActionsController.php b/stripe/controllers/FrmStrpLiteActionsController.php index 5192f86282..c68d7a1595 100644 --- a/stripe/controllers/FrmStrpLiteActionsController.php +++ b/stripe/controllers/FrmStrpLiteActionsController.php @@ -494,7 +494,7 @@ private static function should_load_stripe_script_for_form( $form_id ) { } foreach ( $payment_actions as $action ) { - if ( ! empty( $action->post_content['stripe_link'] ) ) { + if ( in_array( 'stripe', $action->post_content['gateway'], true ) ) { return true; } } From 95f494b7f7cae027690ddc0b6bafc96780cb00a5 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Fri, 1 Nov 2024 16:49:46 +0300 Subject: [PATCH 3/7] Comment functions --- .../FrmStrpLiteActionsController.php | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/stripe/controllers/FrmStrpLiteActionsController.php b/stripe/controllers/FrmStrpLiteActionsController.php index c68d7a1595..694e3c208c 100644 --- a/stripe/controllers/FrmStrpLiteActionsController.php +++ b/stripe/controllers/FrmStrpLiteActionsController.php @@ -458,12 +458,13 @@ public static function load_scripts( $form_id ) { wp_localize_script( 'formidable-stripe', 'frm_stripe_vars', $stripe_vars ); } - private static function get_payment_actions( $form_id ) { - return array(); - $actions = self::get_actions_before_submit( FrmAppHelper::get_param( 'form_id' ) ); - return ! empty( $actions ); - } - + /** + * Returns true if the Stripe script should be loaded. + * + * @since x.x + * + * @return bool + */ private static function should_load_stripe_script(){ if ( self::$has_loaded_stripe_script ) { return false; @@ -484,6 +485,14 @@ private static function should_load_stripe_script(){ return false; } + /** + * Returns true if the Stripe script should be loaded for a form. + * + * @since x.x + * @param int $form_id + * + * @return bool + */ private static function should_load_stripe_script_for_form( $form_id ) { $action_status = array( 'post_status' => 'publish', @@ -501,6 +510,12 @@ private static function should_load_stripe_script_for_form( $form_id ) { return false; } + /** + * Loads Stripe script if it hasn't been loaded yet and it should be loaded. + * + * @since x.x + * @return void + */ private static function maybe_load_stripe_script() { if ( ! self::should_load_stripe_script() ) { return; From 00f45ee9cb0684eb0dfe879eb25c4b79ea89ee14 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Mon, 4 Nov 2024 09:59:17 +0300 Subject: [PATCH 4/7] Add description to new class property --- stripe/controllers/FrmStrpLiteActionsController.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/stripe/controllers/FrmStrpLiteActionsController.php b/stripe/controllers/FrmStrpLiteActionsController.php index 694e3c208c..d71e412c96 100644 --- a/stripe/controllers/FrmStrpLiteActionsController.php +++ b/stripe/controllers/FrmStrpLiteActionsController.php @@ -10,6 +10,11 @@ class FrmStrpLiteActionsController extends FrmTransLiteActionsController { */ private static $customer; + /** + * @since x.x + * + * @var bool|null A memoized value for whether the Stripe script has been loaded. + */ private static $has_loaded_stripe_script; /** From beda56b56cb8da69a9a915490e79a767691aefff Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Mon, 4 Nov 2024 10:14:09 +0300 Subject: [PATCH 5/7] Add missing space --- stripe/controllers/FrmStrpLiteActionsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stripe/controllers/FrmStrpLiteActionsController.php b/stripe/controllers/FrmStrpLiteActionsController.php index d71e412c96..be3543c4fb 100644 --- a/stripe/controllers/FrmStrpLiteActionsController.php +++ b/stripe/controllers/FrmStrpLiteActionsController.php @@ -470,7 +470,7 @@ public static function load_scripts( $form_id ) { * * @return bool */ - private static function should_load_stripe_script(){ + private static function should_load_stripe_script() { if ( self::$has_loaded_stripe_script ) { return false; } From 983f49114fa34818fc37bb1ad2f47c96052762c6 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:50:56 +0300 Subject: [PATCH 6/7] Check emptiness of form_ids first --- stripe/controllers/FrmStrpLiteActionsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stripe/controllers/FrmStrpLiteActionsController.php b/stripe/controllers/FrmStrpLiteActionsController.php index be3543c4fb..1a5b37cdfb 100644 --- a/stripe/controllers/FrmStrpLiteActionsController.php +++ b/stripe/controllers/FrmStrpLiteActionsController.php @@ -476,7 +476,7 @@ private static function should_load_stripe_script() { } global $frm_vars; $form_ids = $frm_vars['forms_loaded']; - if ( ! is_array( $form_ids ) ) { + if ( empty( $form_ids ) || ! is_array( $form_ids ) ) { return false; } $form_ids = array_unique( wp_list_pluck( $form_ids, 'id' ) ); From 9d516d6578b03d8a21ccb6e65bd46a3c14170399 Mon Sep 17 00:00:00 2001 From: Abdi Tolessa <41271840+AbdiTolesa@users.noreply.github.com> Date: Mon, 11 Nov 2024 14:33:04 +0300 Subject: [PATCH 7/7] Reuse existing function to get payment actions --- stripe/controllers/FrmStrpLiteActionsController.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/stripe/controllers/FrmStrpLiteActionsController.php b/stripe/controllers/FrmStrpLiteActionsController.php index 1a5b37cdfb..3740ad1ca1 100644 --- a/stripe/controllers/FrmStrpLiteActionsController.php +++ b/stripe/controllers/FrmStrpLiteActionsController.php @@ -499,10 +499,7 @@ private static function should_load_stripe_script() { * @return bool */ private static function should_load_stripe_script_for_form( $form_id ) { - $action_status = array( - 'post_status' => 'publish', - ); - $payment_actions = FrmFormAction::get_action_for_form( $form_id, 'payment', $action_status ); + $payment_actions = FrmTransLiteActionsController::get_actions_for_form( $form_id ); if ( empty( $payment_actions ) ) { return false; }