diff --git a/woocommerce/Handlers/Script_Handler.php b/woocommerce/Handlers/Script_Handler.php index 1dc1862ba..70ee64ac0 100644 --- a/woocommerce/Handlers/Script_Handler.php +++ b/woocommerce/Handlers/Script_Handler.php @@ -24,6 +24,7 @@ namespace SkyVerge\WooCommerce\PluginFramework\v6_0_1\Handlers; +use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Helpers\ScriptHelper; use SkyVerge\WooCommerce\PluginFramework\v6_0_1\SV_WC_Helper; use SkyVerge\WooCommerce\PluginFramework\v6_0_1\SV_WC_Plugin_Exception; @@ -287,6 +288,20 @@ public function ajax_log_event() { } + /** + * Adds inline JavaScript to the page. + * + * @since 6.0.1 + * + * @param string $data The JavaScript code to add inline + * @return bool True if successfully added + */ + protected function addInlineScript(string $data): bool + { + return ScriptHelper::addInlineScript($this->get_id().'-inline-scripts', $data); + } + + /** * Adds a log entry. * diff --git a/woocommerce/Helpers/ScriptHelper.php b/woocommerce/Helpers/ScriptHelper.php new file mode 100644 index 000000000..a1ed95bd6 --- /dev/null +++ b/woocommerce/Helpers/ScriptHelper.php @@ -0,0 +1,72 @@ +get_safe_handler_js( $args, $handler_name, $object_name ) ); + protected function enqueue_js_handler(array $args, $object_name = '', $handler_name = '') + { + $this->addInlineScript($this->get_safe_handler_js($args, $handler_name, $object_name)); } diff --git a/woocommerce/payment-gateway/class-sv-wc-payment-gateway-hosted.php b/woocommerce/payment-gateway/class-sv-wc-payment-gateway-hosted.php index ea73bf5f2..c5b99eac2 100644 --- a/woocommerce/payment-gateway/class-sv-wc-payment-gateway-hosted.php +++ b/woocommerce/payment-gateway/class-sv-wc-payment-gateway-hosted.php @@ -25,6 +25,7 @@ namespace SkyVerge\WooCommerce\PluginFramework\v6_0_1; use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Helpers\OrderHelper; +use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Helpers\ScriptHelper; defined( 'ABSPATH' ) or exit; @@ -250,7 +251,7 @@ public function render_auto_post_form( \WC_Order $order, $request_params ) { $args = $this->get_auto_post_form_args( $order ); // attempt to automatically submit the form and redirect - wc_enqueue_js(' + $script = ' ( function( $ ) { $( "body" ).block( { @@ -273,7 +274,9 @@ public function render_auto_post_form( \WC_Order $order, $request_params ) { $( "#submit_' . $this->get_id() . '_payment_form" ).click(); } ) ( jQuery ); - '); + '; + + ScriptHelper::addInlineScript($this->get_gateway_js_handle().'-inline', $script); echo '

' . esc_html( $args['message'] ) . '

'; echo '
'; diff --git a/woocommerce/payment-gateway/class-sv-wc-payment-gateway-my-payment-methods.php b/woocommerce/payment-gateway/class-sv-wc-payment-gateway-my-payment-methods.php index 8f0cff402..8910193a6 100644 --- a/woocommerce/payment-gateway/class-sv-wc-payment-gateway-my-payment-methods.php +++ b/woocommerce/payment-gateway/class-sv-wc-payment-gateway-my-payment-methods.php @@ -1156,9 +1156,9 @@ public function handle_payment_method_actions() { * * @since 5.1.0 */ - public function render_js() { - - wc_enqueue_js( $this->get_safe_handler_js() ); + public function render_js() + { + $this->addInlineScript($this->get_safe_handler_js()); } diff --git a/woocommerce/payment-gateway/class-sv-wc-payment-gateway-payment-form.php b/woocommerce/payment-gateway/class-sv-wc-payment-gateway-payment-form.php index 44e85c287..a44cc39d0 100644 --- a/woocommerce/payment-gateway/class-sv-wc-payment-gateway-payment-form.php +++ b/woocommerce/payment-gateway/class-sv-wc-payment-gateway-payment-form.php @@ -1098,7 +1098,7 @@ public function render_js() { break; case "wc_{$gateway_id}_payment_form_end" : $this->payment_form_js_rendered[] = $gateway_id; - wc_enqueue_js( $this->get_safe_handler_js() ); + $this->addInlineScript($this->get_safe_handler_js()); break; endswitch; } diff --git a/woocommerce/payment-gateway/class-sv-wc-payment-gateway.php b/woocommerce/payment-gateway/class-sv-wc-payment-gateway.php index 1c3287571..5e5af2547 100755 --- a/woocommerce/payment-gateway/class-sv-wc-payment-gateway.php +++ b/woocommerce/payment-gateway/class-sv-wc-payment-gateway.php @@ -27,6 +27,7 @@ use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface; use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Blocks\Blocks_Handler; use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Helpers\OrderHelper; +use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Helpers\ScriptHelper; use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Payment_Gateway\Blocks\Gateway_Checkout_Block_Integration; use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Payment_Gateway\Dynamic_Props; use stdClass; @@ -1646,6 +1647,7 @@ public function admin_options() { parent::admin_options(); + $scriptHandler = $this->get_gateway_js_handle().'-admin-inline'; ?> get_job_handler()->get_identifier() . '_cancel_job', array( $this, 'ajax_cancel_job' ) ); } + /** + * Gets the name of the script handle. + * + * @since 6.0.1 + * + * @return string + */ + protected function getScriptHandle() : string + { + return $this->get_job_handler()->get_identifier() . '_batch_handler'; + } + /** * Enqueues the scripts. @@ -95,7 +109,7 @@ protected function add_hooks() { */ public function enqueue_scripts() { - wp_enqueue_script( $this->get_job_handler()->get_identifier() . '_batch_handler', $this->get_plugin()->get_framework_assets_url() . '/js/admin/sv-wp-admin-job-batch-handler.min.js', [ 'jquery' ], $this->get_plugin()->get_assets_version() ); + wp_enqueue_script( $this->getScriptHandle(), $this->get_plugin()->get_framework_assets_url() . '/js/admin/sv-wp-admin-job-batch-handler.min.js', [ 'jquery' ], $this->get_plugin()->get_assets_version() ); } @@ -116,11 +130,13 @@ protected function render_js() { */ $args = apply_filters( $this->get_job_handler()->get_identifier() . '_batch_handler_js_args', $this->get_js_args(), $this ); - wc_enqueue_js( sprintf( 'window.%1$s_batch_handler = new %2$s( %3$s );', + $script = sprintf( 'window.%1$s_batch_handler = new %2$s( %3$s );', esc_js( $this->get_job_handler()->get_identifier() ), esc_js( $this->get_js_class() ), json_encode( $args ) - ) ); + ); + + ScriptHelper::addInlineScript($this->getScriptHandle(), $script); }