disable caching on wp admin
Diff
src/Providers/WordpressServiceProvider.php | 13 +++++++++++++
src/Proxy/WordpressDatabase.php | 21 +++++++++++++--------
2 files changed, 26 insertions(+), 8 deletions(-)
@@ -34,6 +34,11 @@ class WordpressServiceProvider extends ServiceProvider
$this->setConfig();
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// Wordpress requires $table_prefix rather than another constant.
@@ -45,6 +50,11 @@ class WordpressServiceProvider extends ServiceProvider
// Set up the WordPress query.
wp();
if (defined('WP_ADMIN') || str_contains($_SERVER['SCRIPT_NAME'], strrchr(wp_login_url(), '/'))) {
// disable query caching when in Wordpress admin
config(['wordpress.caching' => 0]);
}
}
/**
@@ -115,6 +125,9 @@ class WordpressServiceProvider extends ServiceProvider
$_SERVER['HTTP_HOST'] = parse_url(config('app.url'))['host'];
}
// we need to register this hook before wp-settings.php is included, which also means
// that add_filter hasn't been included yet so we have to add to the wp_filter global
// manually.
$GLOBALS['wp_filter']['after_setup_theme'][10][] = [
'function' => [$this, 'addThemeSupport'],
'accepted_args' => 0,
@@ -60,7 +60,9 @@ class WordpressDatabase extends wpdb
*/
public function db_version()
{
return DB::selectOne('SELECT version() as v')->v;
return Cache::remember('sql_version', config('wordpress.caching'), function () {
return DB::selectOne('SELECT version() as v')->v;
});
}
/**
@@ -215,18 +217,21 @@ class WordpressDatabase extends wpdb
$this->timer_start();
}
if (preg_match('/^\s*(insert|create|alter|truncate|drop)\s/i', $query)) {
if (preg_match('/^\s*(insert|create|alter|truncate|drop|set)\s/i', $query)) {
$this->last_result = $this->result = DB::statement($query);
} elseif (preg_match('/^\s*(delete|update|replace)\s/i', $query)) {
$this->last_result = $this->result = DB::affectingStatement($query);
} else {
if (config('wordpress.caching') && starts_with(strtolower($query), 'select')) {
$this->result = Cache::remember('q_' . $query, config('wordpress.caching'), function () use ($query) {
return DB::select($query);
});
$this->last_result = $this->result;
if (!config('wordpress.caching')) {
// remove cached query if caching has been disabled
Cache::forget('q_' . $query);
}
$this->result = Cache::remember('q_' . $query, config('wordpress.caching'), function () use ($query) {
return DB::select($query);
});
$this->last_result = $this->result;
}
$this->num_queries++;