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 5 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
105 changes: 89 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,88 @@ 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 ( ! 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;
}

/**
* 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(
AbdiTolesa marked this conversation as resolved.
Show resolved Hide resolved
'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 ( 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 +551,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