From b84f9d078df8a790a6ae70d6b43773940d965118 Mon Sep 17 00:00:00 2001 From: Torben Lundsgaard Date: Tue, 5 Mar 2024 08:15:37 +0100 Subject: [PATCH] Scripts were not enqueued if the container was inactive --- inc/main.php | 4 ++-- readme.txt | 1 + src/Common/Util.php | 19 +++++++++++++++++-- src/Integration/ContactForm7.php | 2 +- src/Integration/EasyDigitalDownloads.php | 2 +- src/Integration/WooCommerce.php | 2 +- 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/inc/main.php b/inc/main.php index bfa4967..bf27494 100644 --- a/inc/main.php +++ b/inc/main.php @@ -96,7 +96,7 @@ function gtmkit_load_text_domain(): void { function gtmkit_frontend_init(): void { $options = new Options(); $rest_api_server = new RestAPIServer(); - $util = new Util( $rest_api_server ); + $util = new Util( $options, $rest_api_server ); ( new AdminAPI( $options, $util ) )->rest_init(); @@ -140,7 +140,7 @@ function gtmkit_admin_init(): void { $options = new Options(); $rest_api_server = new RestAPIServer(); - $util = new Util( $rest_api_server ); + $util = new Util( $options, $rest_api_server ); Analytics::register( $options, $util ); MetaBox::register( $options ); diff --git a/readme.txt b/readme.txt index 4d5c746..40eb00b 100644 --- a/readme.txt +++ b/readme.txt @@ -97,6 +97,7 @@ Find out about what's new in our [our release post](https://gtmkit.com/gtm-kit-1 #### Enhancements: #### Bugfixes: +* Scripts were not enqueued if the container was inactive. #### Other: * Tested up to WooCommerce 8.7. diff --git a/src/Common/Util.php b/src/Common/Util.php index 65483ac..ee0db16 100644 --- a/src/Common/Util.php +++ b/src/Common/Util.php @@ -8,12 +8,20 @@ namespace TLA_Media\GTM_Kit\Common; use TLA_Media\GTM_Kit\Integration\WooCommerce; +use TLA_Media\GTM_Kit\Options; /** * Class for common utilities. */ final class Util { + /** + * Instance of Options + * + * @var Options + */ + public $options; + /** * Instance of RestAPIServer * @@ -38,11 +46,13 @@ final class Util { /** * Constructor. * + * @param Options $options Instance of Options. * @param RestAPIServer $rest_api_server Instance of RestAPIServer. * @param string $path The plugin path. * @param string $url The plugin URL. */ - public function __construct( RestAPIServer $rest_api_server, string $path = GTMKIT_PATH, string $url = GTMKIT_URL ) { + public function __construct( Options $options, RestAPIServer $rest_api_server, string $path = GTMKIT_PATH, string $url = GTMKIT_URL ) { + $this->options = $options; $this->rest_api_server = $rest_api_server; $this->asset_path = $path . 'assets/'; $this->asset_url = $url . 'assets/'; @@ -294,7 +304,12 @@ public function enqueue_script( string $handle, string $script, bool $has_asset_ } $deps[] = 'gtmkit'; - $deps[] = 'gtmkit-container'; + + $container_active = ( $this->options->get( 'general', 'container_active' ) && apply_filters( 'gtmkit_container_active', true ) ); + + if ( $container_active ) { + $deps[] = 'gtmkit-container'; + } \wp_enqueue_script( $handle, $this->asset_url . $script, $deps, $ver, $args ); } diff --git a/src/Integration/ContactForm7.php b/src/Integration/ContactForm7.php index e3eac38..a8a8b90 100644 --- a/src/Integration/ContactForm7.php +++ b/src/Integration/ContactForm7.php @@ -30,7 +30,7 @@ public static function instance(): ?ContactForm7 { if ( is_null( self::$instance ) ) { $options = new Options(); $rest_api_server = new RestAPIServer(); - $util = new Util( $rest_api_server ); + $util = new Util( $options, $rest_api_server ); self::$instance = new self( $options, $util ); } diff --git a/src/Integration/EasyDigitalDownloads.php b/src/Integration/EasyDigitalDownloads.php index deb736d..119a540 100644 --- a/src/Integration/EasyDigitalDownloads.php +++ b/src/Integration/EasyDigitalDownloads.php @@ -47,7 +47,7 @@ public static function instance(): ?EasyDigitalDownloads { if ( is_null( self::$instance ) ) { $options = new Options(); $rest_api_server = new RestAPIServer(); - $util = new Util( $rest_api_server ); + $util = new Util( $options, $rest_api_server ); self::$instance = new self( $options, $util ); } diff --git a/src/Integration/WooCommerce.php b/src/Integration/WooCommerce.php index 879c04e..487dfbb 100644 --- a/src/Integration/WooCommerce.php +++ b/src/Integration/WooCommerce.php @@ -62,7 +62,7 @@ public static function instance(): ?WooCommerce { if ( is_null( self::$instance ) ) { $options = new Options(); $rest_api_server = new RestAPIServer(); - $util = new Util( $rest_api_server ); + $util = new Util( $options, $rest_api_server ); self::$instance = new self( $options, $util ); }