Skip to content

Commit

Permalink
Object Cache fixes (#1009)
Browse files Browse the repository at this point in the history
* Fix: Ensure WP Cron events get scheduled when using the Setup Guide wizard and on upgrade
* Fix: Undefined variable when the Object Cache purge debug log is enabled
* Fix: FAQ links and help tab content
* Update: Added warnings in the Setup Guide and the General Settings page when using Disk for Database and Object Caches
* Update: Skip Database and Object caches when using WP-CLI
* Updated Composer and NPM dependencies
  • Loading branch information
cssjoe authored Dec 10, 2024
1 parent e8d5ee4 commit 46a46cb
Show file tree
Hide file tree
Showing 20 changed files with 1,026 additions and 855 deletions.
2 changes: 1 addition & 1 deletion ConfigKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
),
'objectcache.file.gc' => array(
'type' => 'integer',
'default' => 3600
'default' => 600,
),
'objectcache.file.locking' => array(
'type' => 'boolean',
Expand Down
22 changes: 22 additions & 0 deletions DbCache_WpdbInjection_QueryCaching.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public function query( $query ) {
$this->query_total++;

$caching = $this->_can_cache( $query, $reject_reason );

if ( preg_match( '~^\s*start transaction\b~is', $query ) ) {
$this->cache_reject_reason = 'transaction';
$reject_reason = $this->cache_reject_reason;
Expand All @@ -166,6 +167,14 @@ public function query( $query ) {
$flush_after_query = true;
}

// Reject if this is a WP-CLI call and dbcache engine is set to Disk.
if ( $this->is_wpcli_disk() ) {
$this->cache_reject_reason = 'wp-cli and dbcache set to disk';
$reject_reason = $this->cache_reject_reason;
$caching = false;
}


if ( $this->use_filters && function_exists( 'apply_filters' ) ) {
$reject_reason = apply_filters(
'w3tc_dbcache_can_cache_sql',
Expand Down Expand Up @@ -943,4 +952,17 @@ private function log_query( $line ) {

fputcsv( $this->log_filehandle, $line, "\t" );
}

/**
* Check if this is a WP-CLI call and objectcache.engine is using Disk.
*
* @since 2.8.1
* @access private
*
* @return bool
*/
private function is_wpcli_disk(): bool {
$engine = $this->_config->get_string( 'dbcache.engine' );
return defined( 'WP_CLI' ) && WP_CLI && 'file' === $engine;
}
}
25 changes: 24 additions & 1 deletion Extension_FragmentCache_WpObjectCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ public function __construct() {
* @return mixed
*/
public function get( $id, $group = 'transient', $force = false, &$found = null ) {
// Abort if this is a WP-CLI call and fragmentcache engine is set to Disk.
if ( $this->is_wpcli_disk() ) {
return false;
}

if ( $this->_debug ) {
$time_start = Util_Debug::microtime();
}
Expand Down Expand Up @@ -228,9 +233,14 @@ public function get( $id, $group = 'transient', $force = false, &$found = null )
* @param string $group Group.
* @param integer $expire Expire.
*
* @return boolean
* @return bool
*/
public function set( $id, $data, $group = 'transient', $expire = 0 ) {
// Abort if this is a WP-CLI call and fragmentcache engine is set to Disk.
if ( $this->is_wpcli_disk() ) {
return false;
}

$key = $this->_get_cache_key( $id );

if ( is_object( $data ) ) {
Expand Down Expand Up @@ -687,4 +697,17 @@ public function w3tc_usage_statistics_of_request( $storage ) {
$storage->counter_add( 'fragmentcache_calls_total', $this->cache_total );
$storage->counter_add( 'fragmentcache_calls_hits', $this->cache_hits );
}

/**
* Check if this is a WP-CLI call and fragmentcache[engine] is using Disk.
*
* @since 2.8.1
* @access private
*
* @return bool
*/
private function is_wpcli_disk(): bool {
$engine = $this->_config->get_string( array( 'fragmentcache', 'engine' ) );
return defined( 'WP_CLI' ) && WP_CLI && 'file' === $engine;
}
}
74 changes: 34 additions & 40 deletions Generic_Faq.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* FIle: Generic_Faq.php
* File: Generic_Faq.php
*
* @package W3TC
*/
Expand All @@ -12,12 +12,11 @@
*/
class Generic_Faq {
/**
* Get sections
* Return FAQ section URLs.
*
* @return array
*/
public static function sections() {
// name => column where to show.
public static function sections(): array {
return array(
'General' => 'https://api.w3-edge.com/v1/faq/general',
'Usage' => 'https://api.w3-edge.com/v1/faq/usage',
Expand All @@ -34,61 +33,56 @@ public static function sections() {
}

/**
* Returns list of questions for section
* Returns list of questions for section.
*
* @param string $section Section.
* @static
*
* @see self::sections()
*
* @param string $section Section.
* @return array|null
*/
public static function parse( $section ) {
$faq = array();

public static function parse( string $section ): ?array {
$sections = self::sections();

if ( ! isset( $sections[ $section ] ) ) {
return null;
}

$url = $sections[ $section ];

$url = $sections[ $section ];
$response = wp_remote_get( $url );

if ( is_wp_error( $response ) ) {
return null;
}

$html = $response['body'];
$questions = array();

$m = array();
preg_match_all(
'~<h1>\s*<a[^>]+href="(#[^"]+)[^>]+>.*?</a>([^<]+)</h1>~mi',
$html,
$m
$regexes = array(
'~<h1[^>]*>(.*?)</h1>.*?<a[^>]*href="(#[^"]+)"~mi',
'~<li>.*?<a[^>]*href="/BoldGrid/w3-total-cache/wiki/FAQ([^"]+)"[^>]*>(.*?)</a>.*?</li>~mi',
);

if ( is_array( $m ) && count( $m ) > 1 ) {
$count = count( $m[1] );
for ( $n = 0; $n < $count; $n++ ) {
$questions[] = array(
'q' => $m[2][ $n ],
'a' => $url . $m[1][ $n ],
);
}
}
foreach ( $regexes as $i => $regex ) {
preg_match_all( $regex, $response['body'], $m );

$m = array();
preg_match_all(
'~<li>\s*<a[^>]+href="([^"]+)[^>]+>(.*?)</a>\s*[.]s*</li>~mi',
$html,
$m
);
if ( is_array( $m ) && count( $m ) > 1 ) {
$c = count( $m[1] );

if ( is_array( $m ) && count( $m ) > 1 ) {
$count = count( $m[1] );
for ( $n = 0; $n < $count; $n++ ) {
$questions[] = array(
'q' => $m[2][ $n ],
'a' => $m[1][ $n ],
);
for ( $n = 0; $n < $c; $n++ ) {
if ( 0 === $i ) {
// Index 0 has the question first then the URL fragment.
$questions[] = array(
'q' => $m[1][ $n ],
'a' => $url . $m[2][ $n ],
);
} else {
// Index 1 has the URL fragment first then the name. Just use the original URL.
$questions[] = array(
'q' => $m[2][ $n ],
'a' => $url,
);
}
}
}
}

Expand Down
42 changes: 39 additions & 3 deletions Generic_Plugin_Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ public function run() {
delete_option( 'w3tc_message' );
}
}

// Run post-update tasks.
$this->post_update_tasks();
}

/**
Expand Down Expand Up @@ -825,9 +828,7 @@ public function add_help_tabs() {
*/
public function w3tc_ajax_faq() {
$section = Util_Request::get_string( 'section' );

$entries = Generic_Faq::parse( $section );
$response = array();
$entries = Generic_Faq::parse( $section );

ob_start();
include W3TC_DIR . '/Generic_Plugin_Admin_View_Faq.php';
Expand Down Expand Up @@ -1225,4 +1226,39 @@ public function admin_notices() {
);
}
}

/**
* Run post-update tasks.
*
* @since 2.8.1
*
* @see Util_Admin::fix_on_event()
*
* @return void
*/
public function post_update_tasks(): void {
// Check if W3TC was updated.
$state = Dispatcher::config_state();
$last_run_version = $state->get_string( 'tasks.last_run_version' );

if ( empty( $last_run_version ) || version_compare( W3TC_VERSION, $last_run_version, '>' ) ) {
switch ( W3TC_VERSION ) {
case '2.8.1':
// Fix environment.
Util_Admin::fix_on_event( $this->_config, 'w3tc_plugin_updated' );

// Adjust "objectcache.file.gc".
if ( $this->_config->get_integer( 'objectcache.file.gc' ) === 3600 ) {
$this->_config->set( 'objectcache.file.gc', 600 );
$this->_config->save();
}
break;
default:
break;
}

$state->set( 'tasks.last_run_version', W3TC_VERSION );
$state->save();
}
}
}
25 changes: 24 additions & 1 deletion ObjectCache_WpObjectCache_Regular.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ public function __construct() {
* @return mixed
*/
public function get( $id, $group = 'default', $force = false, &$found = null ) {
// Abort if this is a WP-CLI call and objectcache engine is set to Disk.
if ( $this->is_wpcli_disk() ) {
return false;
}

if ( $this->_debug || $this->stats_enabled ) {
$time_start = Util_Debug::microtime();
}
Expand Down Expand Up @@ -342,6 +347,11 @@ public function get_multiple( $ids, $group = 'default', $force = false ) {
* @return boolean
*/
public function set( $id, $data, $group = 'default', $expire = 0 ) {
// Abort if this is a WP-CLI call and objectcache engine is set to Disk.
if ( $this->is_wpcli_disk() ) {
return false;
}

if ( $this->_debug || $this->stats_enabled ) {
$time_start = Util_Debug::microtime();
}
Expand Down Expand Up @@ -690,7 +700,7 @@ public function flush_group( $group ) {
}

if ( $this->_config->get_boolean( 'objectcache.debug_purge' ) ) {
Util_Debug::log_purge( 'objectcache', 'flush', $reason );
Util_Debug::log_purge( 'objectcache', 'flush' );
}

$this->cache = array();
Expand Down Expand Up @@ -1353,4 +1363,17 @@ private function log_call( $line ) {
$wp_filesystem->put_contents( $this->log_filehandle, $line_content, FS_CHMOD_FILE | FILE_APPEND );
}
}

/**
* Check if this is a WP-CLI call and objectcache.engine is using Disk.
*
* @since 2.8.1
* @access private
*
* @return bool
*/
private function is_wpcli_disk(): bool {
$engine = $this->_config->get_string( 'objectcache.engine' );
return defined( 'WP_CLI' ) && WP_CLI && 'file' === $engine;
}
}
12 changes: 12 additions & 0 deletions SetupGuide_Plugin_Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,12 @@ public function config_dbcache() {
if ( $is_updating ) {
$config->save();

// Flush Database Cache.
$f = Dispatcher::component( 'CacheFlush' );
$f->dbcache_flush();

// Fix environment on event.
Util_Admin::fix_on_event( $config, 'setupguide_dbcache' );
}

if ( $config->get_boolean( 'dbcache.enabled' ) === $enable &&
Expand Down Expand Up @@ -584,8 +588,12 @@ public function config_objcache() {
if ( $is_updating ) {
$config->save();

// Flush Object Cache.
$f = Dispatcher::component( 'CacheFlush' );
$f->objectcache_flush();

// Fix environment on event.
Util_Admin::fix_on_event( $config, 'setupguide_objectcache' );
}

if ( $config->getf_boolean( 'objectcache.enabled' ) === $enable &&
Expand Down Expand Up @@ -975,6 +983,10 @@ private function get_config() {
'notEnabled' => __( 'Not Enabled', 'w3-total-cache' ),
'dashboardUrl' => esc_url( Util_Ui::admin_url( 'admin.php?page=w3tc_dashboard' ) ),
'objcache_disabled' => ( ! $config->getf_boolean( 'objectcache.enabled' ) && has_filter( 'w3tc_config_item_objectcache.enabled' ) ),
'warning_disk' => __(
'Warning: Using disk storage for this setting can potentially create a large number of files. Please be aware of any inode or disk space limits you may have on your hosting account.',
'w3-total-cache'
),
),
),
),
Expand Down
15 changes: 15 additions & 0 deletions Util_Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -844,4 +844,19 @@ public static function is_w3tc_admin_page() {
public static function get_current_wp_page() {
return Util_Request::get_string( 'page' );
}

/**
* Fix environment once an event occurs.
*
* @since 2.8.1
* @static
*
* @param Config $config W3TC configuration object.
* @param string $event Event name (optional).
* @return void
*/
public static function fix_on_event( Config $config, ?string $event = '' ): void {
$environment = Dispatcher::component( 'Root_Environment' );
$environment->fix_on_event( $config, $event );
}
}
Loading

0 comments on commit 46a46cb

Please sign in to comment.