From e63778895836536ad08647067e423a50418b294f Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Thu, 27 Oct 2016 00:17:10 +0100 Subject: [PATCH] disable caching on wp admin --- src/Providers/WordpressServiceProvider.php | 13 +++++++++++++ src/Proxy/WordpressDatabase.php | 21 +++++++++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Providers/WordpressServiceProvider.php b/src/Providers/WordpressServiceProvider.php index 93ec772..ee7ec61 100644 --- a/src/Providers/WordpressServiceProvider.php +++ b/src/Providers/WordpressServiceProvider.php @@ -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, diff --git a/src/Proxy/WordpressDatabase.php b/src/Proxy/WordpressDatabase.php index f19348f..af5e14d 100644 --- a/src/Proxy/WordpressDatabase.php +++ b/src/Proxy/WordpressDatabase.php @@ -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++; -- libgit2 1.7.2