Skip to content

Commit

Permalink
Cache the script settings
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed Jan 8, 2024
1 parent 9e6ba08 commit 631a4b0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Release date: 2024-MM-DD
Find out about what's new in our [our release post](https://gtmkit.com/gtm-kit-1-19/).

#### Enhancements:
* The script settings are the same on all pages and are now cached in the object cache for better performance.

#### Bugfixes:

Expand Down
13 changes: 13 additions & 0 deletions src/Admin/AbstractOptionsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public static function register(): void {
add_action( 'admin_enqueue_scripts', [ $page, 'enqueue_page_assets' ] );

add_filter( 'admin_body_class', [ $page, 'admin_body_class' ] );

add_filter( 'activated_plugin', [ $page, 'clear_script_settings_cache' ] );

Check failure on line 43 in src/Admin/AbstractOptionsPage.php

View workflow job for this annotation

GitHub Actions / PHPStan

Filter callback return statement is missing.
add_filter( 'deactivated_plugin', [ $page, 'clear_script_settings_cache' ] );

Check failure on line 44 in src/Admin/AbstractOptionsPage.php

View workflow job for this annotation

GitHub Actions / PHPStan

Filter callback return statement is missing.
add_filter( 'switch_theme', [ $page, 'clear_script_settings_cache' ] );

Check failure on line 45 in src/Admin/AbstractOptionsPage.php

View workflow job for this annotation

GitHub Actions / PHPStan

Filter callback return statement is missing.
}

/**
Expand Down Expand Up @@ -245,4 +249,13 @@ class="gtmkit-bg-color-primary gtmkit-text-white gtmkit-rounded-md gtmkit-py-4 g
</div>
<?php
}

/**
* Clear the script settings cache.
*
* @return void
*/
public function clear_script_settings_cache(): void {
wp_cache_delete( 'gtmkit_script_settings', 'gtmkit' );
}
}
19 changes: 14 additions & 5 deletions src/Frontend/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,23 @@ public static function register( Options $options ): void {
* The inline script for settings and data use by other GTM Kit scripts.
*/
public function enqueue_settings_and_data_script(): void {
$settings = [
'datalayer_name' => $this->datalayer_name,
'console_log' => $this->options->get( 'general', 'console_log' ),
];
$settings = wp_cache_get( 'gtmkit_script_settings', 'gtmkit' );
if ( ! $settings ) {

$settings = apply_filters(
'gtmkit_header_script_settings',
[
'datalayer_name' => $this->datalayer_name,
'console_log' => $this->options->get( 'general', 'console_log' ),
]
);

wp_cache_set( 'gtmkit_script_settings', $settings, 'gtmkit' );
}

ob_start();
?>
window.gtmkit_settings = <?php echo wp_json_encode( apply_filters( 'gtmkit_header_script_settings', $settings ), JSON_FORCE_OBJECT ); ?>;
window.gtmkit_settings = <?php echo wp_json_encode( $settings, JSON_FORCE_OBJECT ); ?>;
window.gtmkit_data = <?php echo wp_json_encode( apply_filters( 'gtmkit_header_script_data', [] ), JSON_FORCE_OBJECT ); ?>;
window.<?php echo esc_js( $this->datalayer_name ); ?> = window.<?php echo esc_js( $this->datalayer_name ); ?> || [];
<?php if ( $this->options->get( 'general', 'gcm_default_settings' ) ) : ?>
Expand Down
1 change: 1 addition & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ public function set( array $options, bool $first_install = false, bool $overwrit

// Now we need to re-cache values.
wp_cache_delete( self::OPTION_NAME, 'options' );
wp_cache_delete( 'gtmkit_script_settings', 'gtmkit' );
$this->options = get_option( self::OPTION_NAME, [] );
}

Expand Down

0 comments on commit 631a4b0

Please sign in to comment.