From fd1c4be8a3d90b78fed9e373c4b765038132433a Mon Sep 17 00:00:00 2001 From: Torben Lundsgaard Date: Thu, 19 Oct 2023 12:10:47 +0200 Subject: [PATCH] Use the enqueue_script utility for all frontend scripts --- src/Common/Util.php | 27 ++++++++++++++---------- src/Integration/ContactForm7.php | 9 +------- src/Integration/EasyDigitalDownloads.php | 16 ++------------ src/Integration/WooCommerce.php | 18 +++------------- 4 files changed, 22 insertions(+), 48 deletions(-) diff --git a/src/Common/Util.php b/src/Common/Util.php index 1720684..14a195e 100644 --- a/src/Common/Util.php +++ b/src/Common/Util.php @@ -209,23 +209,28 @@ public function get_plugin_version(): string { * * @param string $handle The script hande. * @param string $script The script name. + * @param bool $has_asset_file If the script has an asset file or not. + * @param array $deps The script dependencies. + * @param array $args The loading strategy. * * @return void */ - public function enqueue_script( string $handle, string $script ): void { + public function enqueue_script( string $handle, string $script, bool $has_asset_file = false, array $deps = [], array $args = [ 'strategy' => 'defer' ] ): void { - $deps_file = GTMKIT_PATH . 'assets/' . $script . '.asset.php'; + $ver = $this->get_plugin_version(); - $dependency = []; - $version = false; - - if ( file_exists( $deps_file ) ) { - $deps_file = require $deps_file; - $dependency = $deps_file['dependencies']; - $version = $deps_file['version']; + if ( $has_asset_file ) { + $file = GTMKIT_PATH . 'assets/' . substr_replace( $script, '.asset.php', - strlen( '.js' ) ); + if ( file_exists( $file ) ) { + $deps_file = require $file; + $deps = $deps_file['dependencies']; + $ver = $deps_file['version']; + } } - $dependency[] = 'gtmkit'; - \wp_enqueue_script( $handle, GTMKIT_URL . 'assets/' . $script . '.js', $dependency, $version, [ 'strategy' => 'defer' ] ); + $deps[] = 'gtmkit'; + $deps[] = 'gtmkit-container'; + + \wp_enqueue_script( $handle, GTMKIT_URL . 'assets/' . $script, $deps, $ver, $args ); } } diff --git a/src/Integration/ContactForm7.php b/src/Integration/ContactForm7.php index e4194cb..e3eac38 100644 --- a/src/Integration/ContactForm7.php +++ b/src/Integration/ContactForm7.php @@ -58,13 +58,6 @@ public function enqueue_scripts(): void { if ( (int) $this->options->get( 'integrations', 'cf7_load_js' ) === 1 && ! wp_script_is( 'contact-form-7' ) ) { return; } - - wp_enqueue_script( - 'gtmkit-cf7', - GTMKIT_URL . 'assets/integration/contact-form-7.js', - [ 'gtmkit' ], - $this->util->get_plugin_version(), - true - ); + $this->util->enqueue_script( 'gtmkit-cf7', 'integration/contact-form-7.js' ); } } diff --git a/src/Integration/EasyDigitalDownloads.php b/src/Integration/EasyDigitalDownloads.php index 1221ca5..e7e9a39 100644 --- a/src/Integration/EasyDigitalDownloads.php +++ b/src/Integration/EasyDigitalDownloads.php @@ -81,23 +81,11 @@ public function enqueue_scripts(): void { } if ( ! edd_is_checkout() ) { - wp_enqueue_script( - 'gtmkit-edd', - GTMKIT_URL . 'assets/integration/edd.js', - [ 'gtmkit', 'jquery' ], - $this->util->get_plugin_version(), - true - ); + $this->util->enqueue_script( 'gtmkit-edd', 'integration/contact-edd.js', false, [ 'jquery' ] ); } if ( edd_is_checkout() ) { - wp_enqueue_script( - 'gtmkit-edd-checkout', - GTMKIT_URL . 'assets/integration/edd-checkout.js', - [ 'jquery' ], - $this->util->get_plugin_version(), - true - ); + $this->util->enqueue_script( 'gtmkit-edd-checkout', 'integration/contact-edd-checkout.js', false, [ 'jquery' ] ); } } diff --git a/src/Integration/WooCommerce.php b/src/Integration/WooCommerce.php index 169e946..acc0890 100644 --- a/src/Integration/WooCommerce.php +++ b/src/Integration/WooCommerce.php @@ -191,20 +191,14 @@ public function enqueue_scripts(): void { return; } - wp_enqueue_script( - 'gtmkit-woocommerce', - GTMKIT_URL . 'assets/integration/woocommerce.js', - [ 'gtmkit' ], - $this->util->get_plugin_version(), - true - ); + $this->util->enqueue_script( 'gtmkit-woocommerce', 'integration/woocommerce.js', false, [ 'jquery' ] ); if ( is_cart() || is_checkout() ) { if ( has_block( 'woocommerce/cart' ) || has_block( 'woocommerce/checkout' ) ) { wp_dequeue_script( 'gtmkit-woocommerce' ); - $this->util->enqueue_script( 'gtmkit-woocommerce-blocks', 'frontend/woocommerce-blocks' ); + $this->util->enqueue_script( 'gtmkit-woocommerce-blocks', 'frontend/woocommerce-blocks.js', true ); wp_localize_script( 'gtmkit-woocommerce-blocks', @@ -216,13 +210,7 @@ public function enqueue_scripts(): void { ); } else { - wp_enqueue_script( - 'gtmkit-woocommerce-checkout', - GTMKIT_URL . 'assets/integration/woocommerce-checkout.js', - [ 'gtmkit-woocommerce' ], - $this->util->get_plugin_version(), - true - ); + $this->util->enqueue_script( 'gtmkit-woocommerce-checkout', 'integration/woocommerce-checkout.js', false, [ 'gtmkit-woocommerce' ] ); } } }