_integer( 'objectcache.redis.retry_interval' ), 'read_timeout' => $this->_config->get_integer( 'objectcache.redis.read_timeout' ), 'dbid' => $this->_config->get_integer( 'objectcache.redis.dbid' ), 'password' => $this->_config->get_string( 'objectcache.redis.password' ), ); break; case 'file': $engine_config = array( 'section' => 'object', 'locking' => $this->_config->get_boolean( 'objectcache.file.locking' ), 'flush_timelimit' => $this->_config->get_integer( 'timelimit.cache_flush' ), ); break; default: $engine_config = array(); } $engine_config['blog_id'] = $blog_id; $engine_config['module'] = 'object'; $engine_config['host'] = Util_Environment::host(); $engine_config['instance_id'] = Util_Environment::instance_id(); $cache[ $blog_id ] = Cache::instance( $engine, $engine_config ); } return $cache[ $blog_id ]; } /** * Determines whether caching is enabled based on configuration. * * @return bool Returns true if caching is enabled, false otherwise. */ private function _can_cache() { // Skip if disabled. if ( ! $this->_config->getf_boolean( 'objectcache.enabled' ) ) { $this->cache_reject_reason = 'objectcache.disabled'; return false; } // Check for DONOTCACHEOBJECT constant. if ( defined( 'DONOTCACHEOBJECT' ) && DONOTCACHEOBJECT ) { $this->cache_reject_reason = 'DONOTCACHEOBJECT'; return false; } return true; } /** * Checks if caching is allowed for runtime based on the group. * * @param string $group The cache group to check. * * @return bool Returns true if caching is allowed for the group. */ private function _check_can_cache_runtime( $group ) { // Need to be handled in wp admin as well as frontend. if ( $this->_is_transient_group( $group ) ) { return true; } if ( null !== $this->_can_cache_dynamic ) { return $this->_can_cache_dynamic; } if ( $this->_config->get_boolean( 'objectcache.enabled_for_wp_admin' ) ) { $this->_can_cache_dynamic = true; } elseif ( $this->_caching && defined( 'WP_ADMIN' ) && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) { $this->_can_cache_dynamic = false; $this->cache_reject_reason = 'WP_ADMIN defined'; return $this->_can_cache_dynamic; } return $this->_caching; } /** * Determines whether the specified group is a transient group. * * @param string $group The cache group to check. * * @return bool Returns true if the group is transient. */ private function _is_transient_group( $group ) { return in_array( $group, array( 'transient', 'site-transient' ), true ); } /** * Appends information about object cache usage to the footer comment. * * @param array $strings The array of strings to append the data to. * * @return array The modified array of strings. */ public function w3tc_footer_comment( $strings ) { $reason = $this->get_reject_reason(); $append = empty( $reason ) ? '' : sprintf( ' (%1$s)', $reason ); $strings[] = sprintf( // translators: 1: Cache hits, 2: Cache total cache objects, 3: Engine anme, 4: Reason. __( 'Object Caching %1$d/%2$d objects using %3$s%4$s', 'w3-total-cache' ), $this->cache_hits, $this->cache_total, Cache::engine_name( $this->_config->get_string( 'objectcache.engine' ) ), $append ); if ( $this->_config->get_boolean( 'objectcache.debug' ) ) { $strings[] = ''; $strings[] = __( 'Object Cache debug info:', 'w3-total-cache' ); $strings[] = sprintf( '%s%s', str_pad( 'Caching: ', 20 ), ( $this->_caching ? 'enabled' : 'disabled' ) ); $strings[] = sprintf( '%s%d', str_pad( 'Total calls: ', 20 ), $this->cache_total ); $strings[] = sprintf( '%s%d', str_pad( 'Cache hits: ', 20 ), $this->cache_hits ); $strings[] = sprintf( '%s%.4f', str_pad( 'Total time: ', 20 ), $this->time_total ); } return $strings; } /** * Tracks object cache usage statistics. * * @param Storage $storage The storage instance to track statistics in. * * @return void */ public function w3tc_usage_statistics_of_request( $storage ) { $storage->counter_add( 'objectcache_get_total', $this->cache_total ); $storage->counter_add( 'objectcache_get_hits', $this->cache_hits ); $storage->counter_add( 'objectcache_sets', $this->cache_sets ); $storage->counter_add( 'objectcache_flushes', $this->cache_flushes ); $storage->counter_add( 'objectcache_time_ms', (int) ( $this->time_total * 1000 ) ); } /** * Retrieves the reason why the cache is being rejected. * * @return string The rejection reason. */ public function get_reject_reason() { if ( is_null( $this->cache_reject_reason ) ) { return ''; } return $this->_get_reject_reason_message( $this->cache_reject_reason ); } /** * Retrieves a rejection message based on a given key. * * @param string $key The rejection key. * * @return string The rejection message. */ private function _get_reject_reason_message( $key ) { if ( ! function_exists( '__' ) ) { return $key; } switch ( $key ) { case 'objectcache.disabled': return __( 'Object caching is disabled', 'w3-total-cache' ); case 'DONOTCACHEOBJECT': return __( 'DONOTCACHEOBJECT constant is defined', 'w3-total-cache' ); default: return ''; } } /** * Logs cache-related calls for debugging purposes. * * @param array $data The data to log. * * @return void */ private function log_call( array $data ): void { $filepath = Util_Debug::log_filename( 'objectcache-calls' ); $content = implode( "\t", $data ) . PHP_EOL; file_put_contents( $filepath, $content, FILE_APPEND ); } /** * Check if this is a WP-CLI call and objectcache.engine is using Disk and disabled for WP-CLI. * * @since 2.8.1 * * @return bool True if running WP-CLI with a file-based object cache, false otherwise. */ private function is_wpcli_disk(): bool { $is_engine_disk = 'file' === $this->_config->get_string( 'objectcache.engine' ); $is_wpcli_disk = $this->_config->get_boolean( 'objectcache.wpcli_disk' ); return defined( 'WP_CLI' ) && \WP_CLI && $is_engine_disk && ! $is_wpcli_disk; } }