( $tables ) <= 1 ) { $w3tc_group = 'singletables'; // Request with single table affected. } else { $w3tc_group = 'remaining'; } if ( $this->use_filters && function_exists( 'apply_filters' ) ) { $w3tc_group = apply_filters( 'w3tc_dbcache_get_sql_group', $w3tc_group, $sql, $tables ); } return $w3tc_group; } /** * Determines if the query contains only allowed tables. * * @param array $tables List of tables used in the query. * @param array $allowed Allowed tables. * * @return bool True if only allowed tables are used, false otherwise. */ private function contains_only_tables( $tables, $allowed ) { if ( empty( $tables ) ) { return false; } foreach ( $tables as $t ) { if ( ! isset( $allowed[ $t ] ) ) { return false; } } return true; } /** * Retrieves the groups to flush based on the cache group. * * @param string $w3tc_group The cache group to flush. * @param array $extras Optional extra parameters for flushing. * * @return array The groups to flush. */ private function _get_flush_groups( $w3tc_group, $extras = array() ) { $groups_to_flush = array(); switch ( $w3tc_group ) { case 'remaining': case 'singletables': $groups_to_flush = array( 'remaining' => '*', 'options' => '*', 'comments' => '*', 'singletables' => '*', ); break; /** * Options are updated on each second request, * ignore by default probability that SELECTs with joins with options are critical and don't flush "remaining". * That can be changed by w3tc_dbcache_get_flush_groups filter. */ case 'options': $groups_to_flush = array( $w3tc_group => '*' ); break; default: $groups_to_flush = array( $w3tc_group => '*', 'remaining' => '*', ); } if ( $this->use_filters && function_exists( 'apply_filters' ) ) { $groups_to_flush = apply_filters( 'w3tc_dbcache_get_flush_groups', $groups_to_flush, $w3tc_group, $extras ); } return $groups_to_flush; } /** * Clears request-wide dbcache reject state so subsequent queries can be reconsidered. * * @since 2.9.0 * * @return void */ public function reset_reject_state() { $this->cache_reject_reason = null; $this->cache_reject_request_wide = false; $this->can_cache_once_per_request_result = null; } /** * Retrieves the reject reason message for the database cache. * * @return string The reject reason message, or an empty string if no reason is set. */ public function get_reject_reason() { if ( is_null( $this->cache_reject_reason ) ) { return ''; } $request_wide_string = $this->cache_reject_request_wide ? ( function_exists( '__' ) ? __( 'Request-wide ', 'w3-total-cache' ) : 'Request ' ) : ''; return $request_wide_string . $this->_get_reject_reason_message( $this->cache_reject_reason ); } /** * Retrieves a specific reject reason message based on the given key. * * @param string $w3tc_key The key to identify the reject reason. * * @return string The reject reason message. */ private function _get_reject_reason_message( $w3tc_key ) { if ( ! function_exists( '__' ) ) { return $w3tc_key; } switch ( $w3tc_key ) { case 'dbcache.disabled': return __( 'Database caching is disabled', 'w3-total-cache' ); case 'DONOTCACHEDB': return __( 'DONOTCACHEDB constant is defined', 'w3-total-cache' ); case 'DOING_AJAX': return __( 'Doing AJAX', 'w3-total-cache' ); case 'request': return __( 'Request URI is rejected', 'w3-total-cache' ); case 'cookie': return __( 'Cookie is rejected', 'w3-total-cache' ); case 'DOING_CRONG': return __( 'Doing cron', 'w3-total-cache' ); case 'APP_REQUEST': return __( 'Application request', 'w3-total-cache' ); case 'XMLRPC_REQUEST': return __( 'XMLRPC request', 'w3-total-cache' ); case 'WP_ADMIN': return __( 'wp-admin', 'w3-total-cache' ); case 'SHORTINIT': return __( 'Short init', 'w3-total-cache' ); case 'query': return __( 'Query is rejected', 'w3-total-cache' ); case 'user.logged_in': return __( 'User is logged in', 'w3-total-cache' ); default: return $w3tc_key; } } /** * Appends the database cache status information to the footer comment. * * @param array $strings An array of strings to append additional information to. * * @return array The updated array of strings with appended database cache status. */ public function w3tc_footer_comment( $strings ) { $reject_reason = $this->get_reject_reason(); $append = empty( $reject_reason ) ? '' : sprintf( ' (%1$s)', $reject_reason ); if ( $this->query_hits ) { $strings[] = sprintf( // translators: 1: Query hits, 2: Total queries, 3: Total time, 4: Engine name, 5: Reject reason. __( 'Database Caching %1$d/%2$d queries in %3$.3f seconds using %4$s%5$s', 'w3-total-cache' ), $this->query_hits, $this->query_total, $this->time_total, Cache::engine_name( $this->_config->get_string( 'dbcache.engine' ) ), $append ); } else { $strings[] = sprintf( // translators: 1: Engine name, 2: Reject reason. __( 'Database Caching using %1$s%2$s', 'w3-total-cache' ), Cache::engine_name( $this->_config->get_string( 'dbcache.engine' ) ), $append ); } if ( $this->debug ) { $strings[] = ''; $strings[] = __( 'Db cache debug info:', 'w3-total-cache' ); $strings[] = sprintf( '%1$s%2$d', str_pad( __( 'Total queries: ', 'w3-total-cache' ), 20 ), $this->query_total ); $strings[] = sprintf( '%1$s%2$d', str_pad( __( 'Cached queries: ', 'w3-total-cache' ), 20 ), $this->query_hits ); $strings[] = sprintf( '%1$s%2$.4f', str_pad( __( 'Total query time: ', 'w3-total-cache' ), 20 ), $this->time_total ); } if ( $this->log_filehandle ) { fclose( $this->log_filehandle ); $this->log_filehandle = false; } return $strings; } /** * Records the usage statistics related to the database cache. * * @param object $storage The storage object where the statistics are saved. * * @return void */ public function w3tc_usage_statistics_of_request( $storage ) { $storage->counter_add( 'dbcache_calls_total', $this->query_total ); $storage->counter_add( 'dbcache_calls_hits', $this->query_hits ); $storage->counter_add( 'dbcache_flushes', $this->cache_flushes ); $time_ms = (int) ( $this->time_total * 1000 ); $storage->counter_add( 'dbcache_time_ms', $time_ms ); } /** * Logs a query line to the log file. * * @param array $fields The query info to log. * * @return void */ private function log_query( $fields ) { foreach ( $fields as $i => $field ) { if ( \is_string( $field ) ) { if ( 2 === $i ) { $field = Util_Debug::scrub_sql_literals( $field ); } $fields[ $i ] = Util_Debug::redact( $field ); } } if ( ! $this->log_filehandle ) { $filename = Util_Debug::log_filename( 'dbcache-queries' ); $this->log_filehandle = fopen( $filename, 'a' ); } fputcsv( $this->log_filehandle, $fields, "\t" ); } /** * Check if this is a WP-CLI call and objectcache.engine is using Disk. * * @since 2.8.2 * @access private * * @return bool */ private function is_wpcli_disk(): bool { $is_engine_disk = 'file' === $this->_config->get_string( 'dbcache.engine' ); $is_wpcli_disk = $this->_config->get_boolean( 'dbcache.wpcli_disk' ); return defined( 'WP_CLI' ) && \WP_CLI && $is_engine_disk && ! $is_wpcli_disk; } }