@@ -584,21 +584,57 @@ public static function should_enqueue_in_current_tab_section( $tab, $section ) {
584
584
return true ;
585
585
}
586
586
587
+ /**
588
+ * Returns true if the Stripe JS should be loaded on product pages.
589
+ *
590
+ * The critical part here is running the filter to allow merchants to disable Stripe's JS to
591
+ * improve their store's performance when PRBs are disabled.
592
+ *
593
+ * @since 5.8.0
594
+ * @return boolean True if Stripe's JS should be loaded, false otherwise.
595
+ */
587
596
public static function should_load_scripts_on_product_page () {
588
- $ prb_locations = self ::get_settings ( null , 'payment_request_button_locations ' ) ?? [ 'product ' , 'cart ' ];
589
- if ( ! in_array ( 'product ' , $ prb_locations , true ) ) {
590
- return apply_filters ( 'wc_stripe_load_scripts_on_product_page_when_prbs_disabled ' , true );
597
+ if ( self ::should_load_scripts_for_prb_location ( 'product ' ) ) {
598
+ return true ;
591
599
}
592
600
593
- return true ;
601
+ return apply_filters ( ' wc_stripe_load_scripts_on_product_page_when_prbs_disabled ' , true ) ;
594
602
}
595
603
604
+ /**
605
+ * Returns true if the Stripe JS should be loaded on the cart page.
606
+ *
607
+ * The critical part here is running the filter to allow merchants to disable Stripe's JS to
608
+ * improve their store's performance when PRBs are disabled.
609
+ *
610
+ * @since 5.8.0
611
+ * @return boolean True if Stripe's JS should be loaded, false otherwise.
612
+ */
596
613
public static function should_load_scripts_on_cart_page () {
597
- $ prb_locations = self ::get_settings ( null , 'payment_request_button_locations ' ) ?? [ 'product ' , 'cart ' ];
598
- if ( ! in_array ( 'cart ' , $ prb_locations , true ) ) {
599
- return apply_filters ( 'wc_stripe_load_scripts_on_cart_page_when_prbs_disabled ' , true );
614
+ if ( self ::should_load_scripts_for_prb_location ( 'cart ' ) ) {
615
+ return true ;
600
616
}
601
617
602
- return true ;
618
+ return apply_filters ( 'wc_stripe_load_scripts_on_cart_page_when_prbs_disabled ' , true );
619
+ }
620
+
621
+ /**
622
+ * Returns true if the Stripe JS should be loaded for the provided location.
623
+ *
624
+ * @since 5.8.1
625
+ * @param string $location Either 'product' or 'cart'. Used to specify which location to check.
626
+ * @return boolean True if Stripe's JS should be loaded for the provided location, false otherwise.
627
+ */
628
+ private static function should_load_scripts_for_prb_location ( $ location ) {
629
+ // Make sure location parameter is sanitized.
630
+ $ location = in_array ( $ location , [ 'product ' , 'cart ' ], true ) ? $ location : '' ;
631
+ $ are_prbs_enabled = self ::get_settings ( null , 'payment_request ' ) ?? 'yes ' ;
632
+ $ prb_locations = self ::get_settings ( null , 'payment_request_button_locations ' ) ?? [ 'product ' , 'cart ' ];
633
+
634
+ // The scripts should be loaded when all of the following are true:
635
+ // 1. The PRBs are enabled; and
636
+ // 2. The PRB location settings have an array value (saving an empty option in the GUI results in non-array value); and
637
+ // 3. The PRBs are enabled at $location.
638
+ return 'yes ' === $ are_prbs_enabled && is_array ( $ prb_locations ) && in_array ( $ location , $ prb_locations , true );
603
639
}
604
640
}
0 commit comments