From 616cfa10b39e719b012207eff9e45a4a889f8969 Mon Sep 17 00:00:00 2001 From: Torben Lundsgaard Date: Wed, 20 Sep 2023 16:02:12 +0200 Subject: [PATCH] Optimize autoload of options for better performance --- inc/main.php | 6 ++++++ readme.txt | 1 + src/Admin/SetupWizard.php | 2 +- src/Installation/Activation.php | 15 ++++++++++++++- src/Installation/Upgrade.php | 22 +++++++++++++++++++++- src/Options.php | 4 ++-- 6 files changed, 45 insertions(+), 5 deletions(-) diff --git a/inc/main.php b/inc/main.php index 13ef8015..9f027bbe 100644 --- a/inc/main.php +++ b/inc/main.php @@ -48,6 +48,12 @@ function gtmkit_plugin_activation(): void { * Plugin activation hook. */ function gtmkit_plugin_deactivation(): void { + global $wpdb; + + $wpdb->query( + $wpdb->prepare( "UPDATE $wpdb->options SET autoload = 'no' WHERE option_name = 'gtmkit'" ) + ); + wp_clear_scheduled_hook( 'gtmkit_send_anonymous_data' ); } diff --git a/readme.txt b/readme.txt index 44b33ce3..7e5e4fa0 100644 --- a/readme.txt +++ b/readme.txt @@ -91,6 +91,7 @@ Yes! Pagespeed is one of our main focus points, and we strive to make the plugin Enhancements: * Added a more robust method of adding data attributes to the HTML. +* Optimize autoload of options for better performance. Bugfixes: diff --git a/src/Admin/SetupWizard.php b/src/Admin/SetupWizard.php index 3ada5719..d77184d9 100644 --- a/src/Admin/SetupWizard.php +++ b/src/Admin/SetupWizard.php @@ -106,7 +106,7 @@ public function maybe_redirect_after_activation() { // Initial install. if ( get_option( 'gtmkit_initial_version' ) === GTMKIT_VERSION ) { - update_option( 'gtmkit_activation_prevent_redirect', true ); + update_option( 'gtmkit_activation_prevent_redirect', false ); wp_safe_redirect( self::get_site_url() ); exit; } diff --git a/src/Installation/Activation.php b/src/Installation/Activation.php index 4cf7d125..70cb8b21 100644 --- a/src/Installation/Activation.php +++ b/src/Installation/Activation.php @@ -20,6 +20,8 @@ final class Activation { public function __construct() { if ( $this->is_first_install() ) { \add_action( 'gtmkit_activate', [ $this, 'set_first_install_options' ] ); + } else { + $this->set_autoload_on_options(); } } @@ -37,7 +39,7 @@ private function is_first_install(): bool { */ public function set_first_install_options(): void { \add_option( 'gtmkit_initial_version', GTMKIT_VERSION, '', false ); - \update_option( 'gtmkit_version', GTMKIT_VERSION ); + \update_option( 'gtmkit_version', GTMKIT_VERSION, false ); Options::init()->set( Options::get_defaults(), true ); @@ -45,4 +47,15 @@ public function set_first_install_options(): void { \set_transient( 'gtmkit_activation_redirect', true, 30 ); \set_transient( 'gtmkit_first_install', true, 30 ); } + + /** + * Set autoload on options. + */ + public function set_autoload_on_options(): void { + global $wpdb; + + $wpdb->query( + $wpdb->prepare( "UPDATE $wpdb->options SET autoload = 'yes' WHERE option_name = 'gtmkit'" ) + ); + } } diff --git a/src/Installation/Upgrade.php b/src/Installation/Upgrade.php index a3a787b9..50c20d56 100644 --- a/src/Installation/Upgrade.php +++ b/src/Installation/Upgrade.php @@ -28,7 +28,7 @@ public function __construct() { \wp_cache_delete( 'gtmkit', 'options' ); - \update_option( 'gtmkit_version', GTMKIT_VERSION ); + \update_option( 'gtmkit_version', GTMKIT_VERSION, false ); } /** @@ -40,6 +40,7 @@ protected function get_upgrades(): array { $available_upgrades = [ '1.11' => 'v111_upgrade', + '1.14' => 'v114_upgrade', ]; $current_version = \get_option( 'gtmkit_version' ); @@ -71,4 +72,23 @@ protected function v111_upgrade(): void { Options::init()->set( $values, false, false ); } } + + /** + * Upgrade routine for v1.14 + */ + protected function v114_upgrade(): void { + global $wpdb; + + $wpdb->query( + $wpdb->prepare( "UPDATE $wpdb->options SET autoload = 'yes' WHERE option_name = 'gtmkit'" ) + ); + + $wpdb->query( + $wpdb->prepare( "UPDATE $wpdb->options SET autoload = 'no' WHERE option_name = 'gtmkit_version'" ) + ); + + $wpdb->query( + $wpdb->prepare( "UPDATE $wpdb->options SET autoload = 'no' WHERE option_name = 'gtmkit_activation_prevent_redirect'" ) + ); + } } diff --git a/src/Options.php b/src/Options.php index 894470a1..00b25414 100644 --- a/src/Options.php +++ b/src/Options.php @@ -288,11 +288,11 @@ public function set( array $options, bool $once = false, bool $overwrite_existin // Whether to update existing options or to add these options only once if they don't exist yet. if ( $once ) { - \add_option( self::OPTION_NAME, $options, '', 'no' ); // Do not autoload these options. + \add_option( self::OPTION_NAME, $options, '', true ); } elseif ( is_multisite() ) { \update_blog_option( get_main_site_id(), self::OPTION_NAME, $options ); } else { - \update_option( self::OPTION_NAME, $options, 'no' ); + \update_option( self::OPTION_NAME, $options, true ); } // Now we need to re-cache values.