Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid loading Stripe script if Stripe gateway is not selected for a payment action. #2081

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 86 additions & 16 deletions stripe/controllers/FrmStrpLiteActionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ 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;

/**
* Override the credit card field HTML if there is a Stripe action.
*
Expand Down Expand Up @@ -437,6 +444,85 @@ 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 );
}

/**
* 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;
}
global $frm_vars;
$form_ids = $frm_vars['forms_loaded'];
AbdiTolesa marked this conversation as resolved.
Show resolved Hide resolved
if ( empty( $form_ids ) || ! 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;
}
Comment on lines +473 to +491
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add early return and empty check for forms_loaded.

The function could be simplified with early returns, and the empty check for $frm_vars['forms_loaded'] should be moved before accessing it.

 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 ( empty( $form_ids ) || ! is_array( $form_ids ) ) {
+  if ( empty( $frm_vars['forms_loaded'] ) || ! is_array( $frm_vars['forms_loaded'] ) ) {
     return false;
   }
+  $form_ids = $frm_vars['forms_loaded'];
   $form_ids = array_unique( wp_list_pluck( $form_ids, 'id' ) );
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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 ( empty( $form_ids ) || ! 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() {
if ( self::$has_loaded_stripe_script ) {
return false;
}
global $frm_vars;
if ( empty( $frm_vars['forms_loaded'] ) || ! is_array( $frm_vars['forms_loaded'] ) ) {
return false;
}
$form_ids = $frm_vars['forms_loaded'];
$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;
}


/**
* 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 ) {
$payment_actions = FrmTransLiteActionsController::get_actions_for_form( $form_id );
if ( empty( $payment_actions ) ) {
return false;
}

foreach ( $payment_actions as $action ) {
if ( in_array( 'stripe', $action->post_content['gateway'], true ) ) {
return true;
}
}
AbdiTolesa marked this conversation as resolved.
Show resolved Hide resolved
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;
}

$suffix = FrmAppHelper::js_suffix();
$dependencies = array( 'stripe', 'formidable' );

Expand All @@ -462,22 +548,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 );
}

/**
Expand Down
Loading