Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#16 Flush the object cache when switching databases. #17

Merged
merged 2 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions activate.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ function sqlite_activation() {
}
add_action( 'admin_init', 'sqlite_activation' );

// Flush the cache at the last moment before the redirect.
add_filter(
'x_redirect_by',
function ( $result ) {
wp_cache_flush();
return $result;
},
PHP_INT_MAX,
1
);

/**
* Add the db.php file in wp-content.
*
Expand Down
14 changes: 12 additions & 2 deletions deactivate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ function sqlite_plugin_remove_db_file() {

// Init the filesystem if needed, then delete custom drop-in.
if ( $wp_filesystem || WP_Filesystem() ) {
// Flush any persistent cache.
wp_cache_flush();
// Delete the drop-in.
$wp_filesystem->delete( WP_CONTENT_DIR . '/db.php' );
// Flush the cache again to mitigate a possible race condition.
wp_cache_flush();
}

// Run an action on `shutdown`, to deactivate the option in the MySQL database.
Expand All @@ -47,12 +52,17 @@ function() {
$value = maybe_unserialize( $row->option_value );
if ( is_array( $value ) ) {
$value_flipped = array_flip( $value );
unset( $value_flipped[ str_replace( WP_PLUGIN_DIR . '/', '', SQLITE_MAIN_FILE ) ] );
$items = array_reverse( explode( DIRECTORY_SEPARATOR, SQLITE_MAIN_FILE ) );
$item = $items[1] . DIRECTORY_SEPARATOR . $items[0];
unset( $value_flipped[ $item ] );
aristath marked this conversation as resolved.
Show resolved Hide resolved
$value = array_flip( $value_flipped );
$wpdb_mysql->update( $wpdb_mysql->options, array( 'option_value' => maybe_serialize( $value ) ), array( 'option_name' => 'active_plugins' ) );
}
}
}
},
PHP_INT_MAX
);
// Flush any persistent cache.
wp_cache_flush();
}
register_deactivation_hook( SQLITE_MAIN_FILE, 'sqlite_plugin_remove_db_file' ); // Remove db.php file on plugin deactivation.