/** * Maybe output a notice to the user that action has taken place * * @since 2.0.0 */ public function notice() { // Default status & message $status = 'notice-warning'; $message = ''; // Bail if no notice if (isset($_GET['cache_cleared'])) { // Cleared $keys = isset($_GET['keys_cleared']) ? (int) $_GET['keys_cleared'] : 0; // Cache $cache = isset($_GET['cache_cleared']) ? $_GET['cache_cleared'] : 'none returned'; // Type $type = isset($_GET['type']) ? sanitize_key($_GET['type']) : 'none'; // Success $status = 'notice-success'; // Assemble the message if ('group' === $type) { $message = sprintf(_n('Cleared %s key from cache group: %s', 'Cleared %s keys from cache group: %s', $keys, 'wp-spider-cache'), '<strong>' . esc_html($keys) . '</strong>', '<strong>' . esc_html($cache) . '</strong>'); } elseif ('user' === $type) { $message = sprintf(_n('Cleared %s key for user ID: %s', 'Cleared %s keys for user ID: %s', $keys, 'wp-spider-cache'), '<strong>' . esc_html($keys) . '</strong>', '<strong>' . esc_html($cache) . '</strong>'); } } // No object cache if (!function_exists('wp_object_cache')) { $message .= sprintf(esc_html__('Hmm. It looks like you do not have a persistent object cache installed. Did you forget to copy the drop-in plugins to %s?', 'wp-spider-cache'), '<code>' . str_replace(ABSPATH, '', WP_CONTENT_DIR) . '</code>'); } else { // Get cache engine $cache_engine = wp_object_cache()->engine_class_name; // Missing cache engine extension if (empty($cache_engine)) { $message .= esc_html__('Cache engine name missing from Object Cache class.', 'wp-spider-cache'); } elseif (!class_exists($cache_engine)) { $message .= sprintf(esc_html__('Please install the %s extension.', 'wp-spider-cache'), $cache_engine); } } // Bail if no message if (empty($message)) { return; } ?> <div id="message" class="notice <?php echo esc_attr($status); ?> "> <p><?php echo $message; // May contain HTML ?> </p> </div> <?php }
/** * Get the map of cache groups $ keys * * The keymap is limited to global keys and keys to the current site. This * is because cache keys are built inside the WP_Object_Cache class, and * are occasionally prefixed with the current blog ID, meaning we cannot * reliably ask the cache server for data without a way to force the key. * * Maybe in a future version of WP_Object_Cache, a method to retrieve a raw * value based on a full cache key will exist. Until then, no bueno. * * @since 2.0.0 * * @param string $server * @return array */ private function get_keymaps($server = '') { // Use current blog ID to limit keymap scope $current_blog_id = get_current_blog_id(); // Set an empty keymap array $keymaps = array(); $offset = 0; // Offset by 1 if using cache-key salt if (wp_object_cache()->cache_key_salt) { $offset = 1; } // Get keys for this server and loop through them foreach ($this->retrieve_keys($server) as $item) { // Skip if CLIENT_ERROR or malforwed [sic] if (empty($item) || !strstr($item, ':')) { continue; } // Separate the item into parts $parts = explode(':', $item); // Remove key salts if ($offset > 0) { $parts = array_slice($parts, $offset); } // Multisite means first part is numeric if (is_numeric($parts[0])) { $blog_id = (int) $parts[0]; $group = $parts[1]; $global = false; // Single site or global cache group } else { $blog_id = 0; $group = $parts[0]; $global = true; } // Only show global keys and keys for this site if (!empty($blog_id) && $blog_id !== $current_blog_id) { continue; } // Build the cache key based on number of parts if (count($parts) === 1) { $key = $parts[0]; } else { if (true === $global) { $key = implode(':', array_slice($parts, 1)); } else { $key = implode(':', array_slice($parts, 2)); } } // Build group key by combining blog ID & group $group_key = $blog_id . $group; // Build the keymap if (isset($keymaps[$group_key])) { $keymaps[$group_key]['keys'][] = $key; } else { $keymaps[$group_key] = array('blog_id' => $blog_id, 'group' => $group, 'keys' => array($key), 'item' => $item); } } // Sort the keymaps by key ksort($keymaps); return $keymaps; }
/** * Adds a group or set of groups to the list of non-Memcached groups. * * @since 2.0.0 * * @param string|array $groups A group or an array of groups to add. * @return void */ function wp_cache_add_non_persistent_groups($groups) { wp_object_cache()->add_non_persistent_groups($groups); }