Skip to content

Commit

Permalink
Use the enqueue_script utility for all frontend scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed Oct 19, 2023
1 parent 6f2726f commit fd1c4be
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 48 deletions.
27 changes: 16 additions & 11 deletions src/Common/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
9 changes: 1 addition & 8 deletions src/Integration/ContactForm7.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}
}
16 changes: 2 additions & 14 deletions src/Integration/EasyDigitalDownloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
}
}

Expand Down
18 changes: 3 additions & 15 deletions src/Integration/WooCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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' ] );
}
}
}
Expand Down

0 comments on commit fd1c4be

Please sign in to comment.