From a957ecf2e3c01e809b1c7eeee4eea2b4faacf6e4 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 | 4 ++++ readme.txt | 1 + src/Admin/SetupWizard.php | 2 +- src/Installation/Activation.php | 13 ++++++++++++- src/Installation/Upgrade.php | 16 +++++++++++++++- src/Options.php | 4 ++-- 6 files changed, 35 insertions(+), 5 deletions(-) diff --git a/inc/main.php b/inc/main.php index 13ef801..be81e50 100644 --- a/inc/main.php +++ b/inc/main.php @@ -48,6 +48,10 @@ function gtmkit_plugin_activation(): void { * Plugin activation hook. */ function gtmkit_plugin_deactivation(): void { + global $wpdb; + + $wpdb->query( "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 44b33ce..7e5e4fa 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 3ada571..d77184d 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 4cf7d12..5bb92af 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,13 @@ 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( "UPDATE $wpdb->options SET autoload = 'yes' WHERE option_name = 'gtmkit'" ); + } } diff --git a/src/Installation/Upgrade.php b/src/Installation/Upgrade.php index a3a787b..f181d20 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,17 @@ 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( "UPDATE $wpdb->options SET autoload = 'yes' WHERE option_name = 'gtmkit'" ); + + $wpdb->query( "UPDATE $wpdb->options SET autoload = 'no' WHERE option_name = 'gtmkit_version'" ); + + $wpdb->query( "UPDATE $wpdb->options SET autoload = 'no' WHERE option_name = 'gtmkit_activation_prevent_redirect'" ); + } } diff --git a/src/Options.php b/src/Options.php index 894470a..00b2541 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.