age', 'xml_sitemap', // SEOPress sitemap 'seopress_sitemap', 'seopress_news', 'seopress_video', 'seopress_cpt', 'seopress_paged', 'sitemap', // YOAST SEO sitemap 'sitemap_n', ); // Analytics extension - only works in premium version if (file_exists(WPO_CACHE_EXT_DIR . '/analytics.php')) { $analytics_variables = include(WPO_CACHE_EXT_DIR . '/analytics.php'); $user_defined_variables = wpo_cache_config_get('cache_ignore_query_variables'); $user_defined_variables = is_array($user_defined_variables) ? $user_defined_variables : array(); $exclude_variables = array_merge($exclude_variables, $analytics_variables, $user_defined_variables); } if (empty($exclude_variables)) return $variables; foreach ($exclude_variables as $variable) { $exclude = array_search($variable, $variables); if (false !== $exclude) { array_splice($variables, $exclude, 1); } } return $variables; } endif; /** * Get cache config * * @param string $key - The config item * @param mixed $default - The default value * * @return mixed */ if (!function_exists('wpo_cache_config_get')) : function wpo_cache_config_get($key, $default = false) { $config = $GLOBALS['wpo_cache_config']; if (!$config) return false; if (isset($config[$key])) { return $config[$key]; } else { return $default; } } endif; /** * Checks if cache only specific urls option enabled * * @return boolean */ if (!function_exists('wpo_cache_specific_urls_only')) : function wpo_cache_specific_urls_only(): bool { return wpo_cache_config_get('cache_specific_urls_only', false); } endif; if (!function_exists('wpo_read_cache_directory_htaccess')) : /** * Read .htaccess file for the cache directory. * * @return string */ function wpo_read_cache_directory_htaccess() { $htaccess_filename = WPO_CACHE_FILES_DIR . '/.htaccess'; return is_file($htaccess_filename) ? file_get_contents($htaccess_filename) : ''; // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- WP_Filesystem not available this early } endif; if (!function_exists('wpo_write_cache_directory_htaccess')) : /** * Write .htaccess file for the cache directory. * * @param string $htaccess_content * * @return void */ function wpo_write_cache_directory_htaccess($htaccess_content) { $htaccess_filename = WPO_CACHE_FILES_DIR . '/.htaccess'; // phpcs:disable // WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- WP_Filesystem not available this early // Generic.PHP.NoSilencedErrors.Discouraged -- suppress errors from displaying @file_put_contents($htaccess_filename, $htaccess_content); // phpcs:enable } endif; if (!function_exists('wpo_allow_access_to_index_cache_files')) : /** * Update the .htaccess file to allow access to index.html files in the cache directory. * * @return void */ function wpo_allow_access_to_index_cache_files() { $htaccess_content = wpo_read_cache_directory_htaccess(); if (false === strpos($htaccess_content, 'Allow access to index.html files')) { $allow_access_to_index_html = "\n\n# Allow access to index.html files\n\n\tOrder allow,deny\n\tAllow from all\n"; $htaccess_content .= $allow_access_to_index_html; wpo_write_cache_directory_htaccess($htaccess_content); } } endif; if (!function_exists('wpo_disable_cache_directories_viewing')) : /** * Create config files to disable cache directory viewing * * @return void */ function wpo_disable_cache_directories_viewing() { global $is_apache, $is_IIS, $is_iis7; if (!is_dir(WPO_CACHE_FILES_DIR)) return; // Create a .htaccess file for apache server. if ($is_apache) { $htaccess_filename = WPO_CACHE_FILES_DIR . '/.htaccess'; // CS does not like heredoc // phpcs:disable $htaccess_content = << Order allow,deny Deny from all EOF; // phpcs:enable if (!is_file($htaccess_filename)) wpo_write_cache_directory_htaccess($htaccess_content); } // Create web.config file for IIS servers. if ($is_IIS || $is_iis7) { $webconfig_filename = WPO_CACHE_FILES_DIR . '/web.config'; $webconfig_content = "\n\n\n\n\n\n\n"; // phpcs:disable // Generic.PHP.NoSilencedErrors.Discouraged -- suppress the error when there is file permission issues // WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- WP_Filesystem not available this early if (!is_file($webconfig_filename)) @file_put_contents($webconfig_filename, $webconfig_content); // phpcs:enable } // Create empty index.php file for all servers. // phpcs:disable // Generic.PHP.NoSilencedErrors.Discouraged -- suppress the error when there is file permission issues // WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- WP_Filesystem not available this early if (!is_file(WPO_CACHE_FILES_DIR . '/index.php')) @file_put_contents(WPO_CACHE_FILES_DIR . '/index.php', ''); // phpcs:enable } endif; /** * Add the headers indicating why the page is not cached or served from cache * * @param string $message - The headers * * @return void */ if (!function_exists('wpo_cache_add_nocache_http_header')) : function wpo_cache_add_nocache_http_header($message = '') { if (!headers_sent()) { header('WPO-Cache-Status: not cached'); header('WPO-Cache-Message: '. trim(str_replace(array("\r", "\n", ':'), ' ', strip_tags($message)))); // phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags -- wp_strip_all_tags not available this early } } endif; /** * Add the headers indicating why the page is not cached or served from cache by integrating with the send_headers filter * * @param string $message - The headers * * @return void */ if (!function_exists('wpo_cache_add_nocache_http_header_with_send_headers_action')) : function wpo_cache_add_nocache_http_header_with_send_headers_action($message) { if ('' !== $message && !headers_sent()) { wpo_cache_add_nocache_http_header($message); } } endif; /** * Check if feeds caching enabled * * @return bool */ if (!function_exists('wpo_feeds_caching_enabled')) : function wpo_feeds_caching_enabled() { return apply_filters('wpo_feeds_caching_enabled', true); } endif; /** * Check if REST caching enabled * * @return bool */ if (!function_exists('wpo_rest_caching_enabled')) : function wpo_rest_caching_enabled() { return wpo_cache_config_get('enable_rest_caching', false); } endif; if (!function_exists('wpo_debug_backtrace_summary')) { function wpo_debug_backtrace_summary($ignore_class = null, $skip_frames = 0, $pretty = true) { static $truncate_paths; $trace = debug_backtrace(false); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace -- Edge case, using for debugging purpose $caller = array(); $check_class = !is_null($ignore_class); $skip_frames++; // Skip this function. if (!isset($truncate_paths)) { $truncate_paths = array( wpo_normalize_path(WP_CONTENT_DIR), wpo_normalize_path(ABSPATH), ); } foreach ($trace as $call) { if ($skip_frames > 0) { $skip_frames--; } elseif (isset($call['class'])) { if ($check_class && $ignore_class === $call['class']) { continue; // Filter out calls. } $caller[] = "{$call['class']}{$call['type']}{$call['function']}"; } else { if (in_array($call['function'], array('do_action', 'apply_filters', 'do_action_ref_array', 'apply_filters_ref_array'), true)) { $caller[] = "{$call['function']}('{$call['args'][0]}')"; } elseif (in_array($call['function'], array('include', 'include_once', 'require', 'require_once'), true)) { $filename = isset($call['args'][0]) ? $call['args'][0] : ''; $caller[] = $call['function'] . "('" . str_replace($truncate_paths, '', wpo_normalize_path($filename)) . "')"; } else { $caller[] = $call['function']; } } } if ($pretty) { return implode(', ', array_reverse($caller)); } else { return $caller; } } } if (!function_exists('wpo_normalize_path')) { function wpo_normalize_path($path) { // Standardise all paths to use '/'. $path = str_replace('\\', '/', $path); // Replace multiple slashes down to a singular, allowing for network shares having two slashes. $path = preg_replace('|(?<=.)/+|', '/', $path); // Windows paths should uppercase the drive letter. if (':' === substr($path, 1, 1)) { $path = ucfirst($path); } return $path; } } /** * Get path to wp-config.php when called from WP-CLI. * * @return string */ if (!function_exists('wpo_wp_cli_locate_wp_config')) : function wpo_wp_cli_locate_wp_config() { $config_path = ''; if (is_callable('\WP_CLI\Utils\locate_wp_config')) { $config_path = \WP_CLI\Utils\locate_wp_config(); } return $config_path; } endif; /** * Retrieves and sanitizes a value from a superglobal array in a way similar to WordPress's sanitize_text_field(), * for use before WordPress is fully loaded * * @param string $key * @param string $global_type * @return string sanitized string. */ if (!function_exists('wpo_early_sanitize_superglobal_text')) : function wpo_early_sanitize_superglobal_text($key, $global_type = 'server') { $str = ''; // phpcs:disable // Sanitized later in the code, without using WordPress functions as they are not available at this stage if ('server' === $global_type) { $str = $_SERVER[$key] ?? ''; } // phpcs:enable if ('' === $str || !is_scalar($str)) { return ''; } // Remove backslashes (simulate wp_unslash()). $str = stripslashes((string) $str); // Remove ASCII control characters (0x00–0x1F and 0x7F). $str = preg_replace('/[\x00-\x1F\x7F]/u', '', $str); // Trim whitespace and encode special HTML characters. return htmlspecialchars(trim($str), ENT_QUOTES, 'UTF-8'); } endif;