|
| 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 | +} |
0 commit comments