Skip to content

Commit b7c7a24

Browse files
Replace usage of deprecated wc_enqueue_js() (#800)
* Replace usage of deprecated wc_enqueue_js * Separate out registration * Remove dependencies & add doing it wrong check * Update doing it wrong wording Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add jQuery helper --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 420fb8b commit b7c7a24

11 files changed

Lines changed: 132 additions & 18 deletions

woocommerce/Handlers/Script_Handler.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
namespace SkyVerge\WooCommerce\PluginFramework\v6_0_1\Handlers;
2626

27+
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Helpers\ScriptHelper;
2728
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\SV_WC_Helper;
2829
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\SV_WC_Plugin_Exception;
2930

@@ -287,6 +288,20 @@ public function ajax_log_event() {
287288
}
288289

289290

291+
/**
292+
* Adds inline JavaScript to the page.
293+
*
294+
* @since 6.0.1
295+
*
296+
* @param string $data The JavaScript code to add inline
297+
* @return bool True if successfully added
298+
*/
299+
protected function addInlineScript(string $data): bool
300+
{
301+
return ScriptHelper::addInlineScript($this->get_id().'-inline-scripts', $data);
302+
}
303+
304+
290305
/**
291306
* Adds a log entry.
292307
*
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* WooCommerce Plugin Framework
4+
*
5+
* This source file is subject to the GNU General Public License v3.0
6+
* that is bundled with this package in the file license.txt.
7+
* It is also available through the world-wide-web at this URL:
8+
* http://www.gnu.org/licenses/gpl-3.0.html
9+
* If you did not receive a copy of the license and are unable to
10+
* obtain it through the world-wide-web, please send an email
11+
* to license@skyverge.com so we can send you a copy immediately.
12+
*
13+
* DISCLAIMER
14+
*
15+
* Do not edit or add to this file if you wish to upgrade the plugin to newer
16+
* versions in the future. If you wish to customize the plugin for your
17+
* needs please refer to http://www.skyverge.com
18+
*
19+
* @package SkyVerge/WooCommerce/Helpers
20+
* @author SkyVerge
21+
* @copyright Copyright (c) 2013-2026, SkyVerge, Inc.
22+
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
23+
*/
24+
25+
namespace SkyVerge\WooCommerce\PluginFramework\v6_0_1\Helpers;
26+
27+
class ScriptHelper
28+
{
29+
/**
30+
* Adds inline JavaScript.
31+
*
32+
* @since 6.0.1
33+
*
34+
* @param string $handle Handle name
35+
* @param string $javaScriptString The JavaScript code to add inline
36+
* @return bool True if successfully added
37+
*/
38+
public static function addInlineScript(string $handle, string $javaScriptString) : bool
39+
{
40+
if (did_action('wp_print_footer_scripts')) {
41+
_doing_it_wrong(__METHOD__, 'Inline scripts should be added before the wp_print_footer_scripts action.', '6.0.1');
42+
}
43+
44+
if (! wp_script_is($handle, 'registered')) {
45+
wp_register_script($handle, false, [], false, true);
46+
}
47+
48+
if (! wp_script_is($handle, 'enqueued')) {
49+
wp_enqueue_script($handle);
50+
}
51+
52+
return wp_add_inline_script($handle, $javaScriptString);
53+
}
54+
55+
/**
56+
* Adds inline jQuery.
57+
* This calls {@see static::addInlineScript()} but with automatic jQuery wrapping.
58+
*
59+
* @since 6.0.1
60+
*
61+
* @param string $handle Handle name
62+
* @param string $javaScriptString The JavaScript code to add inline
63+
* @return bool True if successfully added
64+
*/
65+
public static function addInlinejQuery(string $handle, string $javaScriptString) : bool
66+
{
67+
return static::addInlineScript(
68+
$handle,
69+
'jQuery(function($) { '.$javaScriptString.' });'
70+
);
71+
}
72+
}

woocommerce/changelog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
*** SkyVerge WooCommerce Plugin Framework Changelog ***
22

33
2025.nn.nn - version 6.0.1
4+
* New: Introduced a new ScriptHelper class with addInlineScript() method
5+
* Tweak: Update usage of deprecated wc_enqueue_js() in favour of wp_add_inline_script()
46
* Dev - Set PHP 8.1 defaults on `html_entity_decode()` usage (ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401)
57

68
2025.nn.nn - version 6.0.0

woocommerce/class-sv-wc-admin-notice-handler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
namespace SkyVerge\WooCommerce\PluginFramework\v6_0_1;
2626

27+
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Helpers\ScriptHelper;
28+
2729
defined( 'ABSPATH' ) or exit;
2830

2931
if ( ! class_exists( '\\SkyVerge\\WooCommerce\\PluginFramework\\v6_0_1\\SV_WC_Admin_Notice_Handler' ) ) :
@@ -304,7 +306,7 @@ function log_dismissed_notice( pluginID, messageID ) {
304306
} ) ( jQuery );
305307
<?php
306308

307-
wc_enqueue_js( ob_get_clean() );
309+
ScriptHelper::addInlineScript($plugin_slug.'-admin-notices', ob_get_clean());
308310
}
309311

310312

woocommerce/class-sv-wc-helper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace SkyVerge\WooCommerce\PluginFramework\v6_0_1;
2626

2727
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Helpers\NumberHelper;
28+
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Helpers\ScriptHelper;
2829
use WC_Data;
2930
use WP_Post;
3031

@@ -915,7 +916,7 @@ function getEnhancedSelectFormatString() {
915916

916917
$javascript .= '} )();';
917918

918-
wc_enqueue_js( $javascript );
919+
ScriptHelper::addInlineScript('sv-wc-select2', $javascript);
919920

920921
/**
921922
* WC Select2 Ajax Rendered Action.

woocommerce/payment-gateway/External_Checkout/Frontend.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Blocks\Blocks_Handler;
2929
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Handlers\Script_Handler;
30+
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Helpers\ScriptHelper;
3031
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\SV_WC_Helper;
3132
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\SV_WC_Payment_Gateway;
3233
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\SV_WC_Payment_Gateway_Plugin;
@@ -424,9 +425,9 @@ protected function should_enqueue_scripts() : bool {
424425
* @param string $object_name JS object name
425426
* @param string $handler_name handler class name
426427
*/
427-
protected function enqueue_js_handler( array $args, $object_name = '', $handler_name = '' ) {
428-
429-
wc_enqueue_js( $this->get_safe_handler_js( $args, $handler_name, $object_name ) );
428+
protected function enqueue_js_handler(array $args, $object_name = '', $handler_name = '')
429+
{
430+
$this->addInlineScript($this->get_safe_handler_js($args, $handler_name, $object_name));
430431
}
431432

432433

woocommerce/payment-gateway/class-sv-wc-payment-gateway-hosted.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace SkyVerge\WooCommerce\PluginFramework\v6_0_1;
2626

2727
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Helpers\OrderHelper;
28+
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Helpers\ScriptHelper;
2829

2930

3031
defined( 'ABSPATH' ) or exit;
@@ -250,7 +251,7 @@ public function render_auto_post_form( \WC_Order $order, $request_params ) {
250251
$args = $this->get_auto_post_form_args( $order );
251252

252253
// attempt to automatically submit the form and redirect
253-
wc_enqueue_js('
254+
$script = '
254255
( function( $ ) {
255256
256257
$( "body" ).block( {
@@ -273,7 +274,9 @@ public function render_auto_post_form( \WC_Order $order, $request_params ) {
273274
$( "#submit_' . $this->get_id() . '_payment_form" ).click();
274275
275276
} ) ( jQuery );
276-
');
277+
';
278+
279+
ScriptHelper::addInlineScript($this->get_gateway_js_handle().'-inline', $script);
277280

278281
echo '<p>' . esc_html( $args['message'] ) . '</p>';
279282
echo '<form action="' . esc_url( $args['submit_url'] ) . '" method="post">';

woocommerce/payment-gateway/class-sv-wc-payment-gateway-my-payment-methods.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,9 +1156,9 @@ public function handle_payment_method_actions() {
11561156
*
11571157
* @since 5.1.0
11581158
*/
1159-
public function render_js() {
1160-
1161-
wc_enqueue_js( $this->get_safe_handler_js() );
1159+
public function render_js()
1160+
{
1161+
$this->addInlineScript($this->get_safe_handler_js());
11621162
}
11631163

11641164

woocommerce/payment-gateway/class-sv-wc-payment-gateway-payment-form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ public function render_js() {
10981098
break;
10991099
case "wc_{$gateway_id}_payment_form_end" :
11001100
$this->payment_form_js_rendered[] = $gateway_id;
1101-
wc_enqueue_js( $this->get_safe_handler_js() );
1101+
$this->addInlineScript($this->get_safe_handler_js());
11021102
break;
11031103
endswitch;
11041104
}

woocommerce/payment-gateway/class-sv-wc-payment-gateway.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;
2828
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Blocks\Blocks_Handler;
2929
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Helpers\OrderHelper;
30+
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Helpers\ScriptHelper;
3031
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Payment_Gateway\Blocks\Gateway_Checkout_Block_Integration;
3132
use SkyVerge\WooCommerce\PluginFramework\v6_0_1\Payment_Gateway\Dynamic_Props;
3233
use stdClass;
@@ -1646,6 +1647,7 @@ public function admin_options() {
16461647

16471648
parent::admin_options();
16481649

1650+
$scriptHandler = $this->get_gateway_js_handle().'-admin-inline';
16491651
?>
16501652
<style type="text/css">.nowrap { white-space: nowrap; }</style>
16511653
<?php
@@ -1668,7 +1670,7 @@ public function admin_options() {
16681670
} ).change();
16691671
<?php
16701672

1671-
wc_enqueue_js( ob_get_clean() );
1673+
ScriptHelper::addInlinejQuery($scriptHandler, ob_get_clean());
16721674

16731675
}
16741676

@@ -1692,7 +1694,7 @@ public function admin_options() {
16921694
} ).change();
16931695
<?php
16941696

1695-
wc_enqueue_js( ob_get_clean() );
1697+
ScriptHelper::addInlinejQuery($scriptHandler, ob_get_clean());
16961698
}
16971699

16981700
// if there's more than one environment include the environment settings switcher code
@@ -1722,7 +1724,7 @@ public function admin_options() {
17221724
} ).change();
17231725
<?php
17241726

1725-
wc_enqueue_js( ob_get_clean() );
1727+
ScriptHelper::addInlinejQuery($scriptHandler, ob_get_clean());
17261728

17271729
}
17281730

@@ -1748,7 +1750,7 @@ public function admin_options() {
17481750
} ).change();
17491751
<?php
17501752

1751-
wc_enqueue_js( ob_get_clean() );
1753+
ScriptHelper::addInlinejQuery($scriptHandler, ob_get_clean());
17521754

17531755
}
17541756

0 commit comments

Comments
 (0)