Skip to content

Commit

Permalink
Optimize autoload of options for better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed Sep 30, 2023
1 parent b65c8f1 commit a957ecf
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
4 changes: 4 additions & 0 deletions inc/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}

Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion src/Admin/SetupWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 12 additions & 1 deletion src/Installation/Activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand All @@ -37,12 +39,21 @@ 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 );

// Add transient to trigger redirect to the Setup Wizard.
\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'" );
}
}
16 changes: 15 additions & 1 deletion src/Installation/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand All @@ -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' );
Expand Down Expand Up @@ -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'" );
}
}
4 changes: 2 additions & 2 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a957ecf

Please sign in to comment.