/** * Generates the configuration file for the current domain based on the values of options * * @since 2.0 * * @return array Names of all config files & The content that will be printed */ function get_rocket_config_file() { $options = get_option(WP_ROCKET_SLUG); if (!$options) { return; } $buffer = '<?php' . "\n"; $buffer .= 'defined( \'ABSPATH\' ) or die( \'Cheatin\\\' uh?\' );' . "\n\n"; if (apply_filters('rocket_override_min_documentRoot', false)) { $buffer .= '$min_documentRoot = \'' . ABSPATH . '\';' . "\n"; } $buffer .= '$rocket_cookie_hash = \'' . COOKIEHASH . '\'' . ";\n"; foreach ($options as $option => $value) { if ($option == 'cache_ssl' || $option == 'cache_mobile' || $option == 'secret_cache_key') { $buffer .= '$rocket_' . $option . ' = \'' . $value . '\';' . "\n"; } if ($option == 'cache_reject_uri') { $buffer .= '$rocket_' . $option . ' = \'' . get_rocket_cache_reject_uri() . '\';' . "\n"; } if ($option == 'cache_query_strings') { $buffer .= '$rocket_' . $option . ' = ' . var_export(get_rocket_cache_query_string(), true) . ';' . "\n"; } if ($option == 'cache_reject_cookies') { $cookies = get_rocket_cache_reject_cookies(); if (get_rocket_option('cache_logged_user')) { $logged_in_cookie = str_replace(COOKIEHASH, '', LOGGED_IN_COOKIE); $cookies = str_replace($logged_in_cookie . '|', '', $cookies); $cookies = trim($cookies, '|'); } $buffer .= '$rocket_' . $option . ' = \'' . $cookies . '\';' . "\n"; } if ($option == 'cache_reject_ua') { $buffer .= '$rocket_' . $option . ' = \'' . get_rocket_cache_reject_ua() . '\';' . "\n"; } } /** This filter is documented in inc/front/htaccess.php */ if (apply_filters('rocket_url_no_dots', false)) { $buffer .= '$rocket_url_no_dots = \'1\';'; } $config_files_path = array(); $urls = array(home_url()); // Check if a translation plugin is activated and this configuration is in subdomain if ($subdomains = get_rocket_i18n_subdomains()) { $urls = $subdomains; } foreach ($urls as $url) { list($host, $path) = get_rocket_parse_url(rtrim($url, '/')); if (!isset($path)) { $config_files_path[] = WP_ROCKET_CONFIG_PATH . strtolower($host) . '.php'; } else { $config_files_path[] = WP_ROCKET_CONFIG_PATH . strtolower($host) . str_replace('/', '.', rtrim($path, '/')) . '.php'; } } /** * Filter the content of all config files * * @since 2.1 * * @param string $buffer The content that will be printed * @param array $config_files_path Names of all config files */ $buffer = apply_filters('rocket_config_file', $buffer, $config_files_path); return array($config_files_path, $buffer); }
/** * Get all pages we don't cache (string) * * @since 1.0 * @deprecated 2.0 * @deprecated Use get_rocket_cache_reject_uri() * */ function get_rocket_pages_not_cached() { _deprecated_function(__FUNCTION__, '2.0', "get_rocket_cache_reject_uri()"); return get_rocket_cache_reject_uri(); }
/** * Rewrite rules to serve the cache file * * @since 1.0 * * @return string $rules Rules that will be printed */ function get_rocket_htaccess_mod_rewrite() { // No rewrite rules for multisite if (is_multisite()) { return; } // No rewrite rules for Korean if (defined('WPLANG') && 'ko_KR' == WPLANG || 'ko_KR' == get_locale()) { return; } // Get root base $home_root = parse_url(home_url()); $home_root = isset($home_root['path']) ? trailingslashit($home_root['path']) : '/'; $site_root = parse_url(site_url()); $site_root = isset($site_root['path']) ? trailingslashit($site_root['path']) : ''; // Get cache root if (strpos(ABSPATH, WP_ROCKET_CACHE_PATH) === false) { $cache_root = str_replace($_SERVER['DOCUMENT_ROOT'], '', WP_ROCKET_CACHE_PATH); } else { $cache_root = $site_root . str_replace(ABSPATH, '', WP_ROCKET_CACHE_PATH); } /** * Replace the dots by underscores to avoid some bugs on some shared hosting services on filenames (not multisite compatible!) * * @since 1.3.0 * * @param bool true will replace the . by _ */ $HTTP_HOST = apply_filters('rocket_url_no_dots', false) ? rocket_remove_url_protocol(home_url()) : '%{HTTP_HOST}'; /** * Allow the path to be fully printed or dependant od %DOCUMENT_ROOT (forced for 1&1 by default) * * @since 1.3.0 * * @param bool true will force the path to be full */ $is_1and1_or_force = apply_filters('rocket_force_full_path', strpos($_SERVER['DOCUMENT_ROOT'], '/kunden/') === 0); $rules = ''; $gzip_rules = ''; $enc = ''; /** * Allow to serve gzip cache file * * @since 2.4 * * @param bool true will force to serve gzip cache file */ if (function_exists('gzencode') && apply_filters('rocket_force_gzip_htaccess_rules', true)) { $rules = '<IfModule mod_mime.c>' . PHP_EOL; $rules .= 'AddType text/html .html_gzip' . PHP_EOL; $rules .= 'AddEncoding gzip .html_gzip' . PHP_EOL; $rules .= '</IfModule>' . PHP_EOL; $rules .= '<IfModule mod_setenvif.c>' . PHP_EOL; $rules .= 'SetEnvIfNoCase Request_URI \\.html_gzip$ no-gzip' . PHP_EOL; $rules .= '</IfModule>' . PHP_EOL . PHP_EOL; $gzip_rules .= 'RewriteCond %{HTTP:Accept-Encoding} gzip' . PHP_EOL; $gzip_rules .= 'RewriteRule .* - [E=WPR_ENC:_gzip]' . PHP_EOL; $enc = '%{ENV:WPR_ENC}'; } $rules .= '<IfModule mod_rewrite.c>' . PHP_EOL; $rules .= 'RewriteEngine On' . PHP_EOL; $rules .= 'RewriteBase ' . $home_root . PHP_EOL; $rules .= get_rocket_htaccess_ssl_rewritecond(); $rules .= $gzip_rules; $rules .= 'RewriteCond %{REQUEST_METHOD} GET' . PHP_EOL; $rules .= 'RewriteCond %{QUERY_STRING} =""' . PHP_EOL; if ($cookies = get_rocket_cache_reject_cookies()) { $rules .= 'RewriteCond %{HTTP:Cookie} !(' . $cookies . ') [NC]' . PHP_EOL; } if ($uri = get_rocket_cache_reject_uri()) { $rules .= 'RewriteCond %{REQUEST_URI} !^(' . $uri . ')$ [NC]' . PHP_EOL; } $rules .= !is_rocket_cache_mobile() ? get_rocket_htaccess_mobile_rewritecond() : ''; if ($ua = get_rocket_cache_reject_ua()) { $rules .= 'RewriteCond %{HTTP_USER_AGENT} !^(' . $ua . ').* [NC]' . PHP_EOL; } if ($is_1and1_or_force) { $rules .= 'RewriteCond "' . str_replace('/kunden/', '/', WP_ROCKET_CACHE_PATH) . $HTTP_HOST . '%{REQUEST_URI}/index%{ENV:WPR_SSL}.html' . $enc . '" -f' . PHP_EOL; } else { $rules .= 'RewriteCond "%{DOCUMENT_ROOT}/' . ltrim($cache_root, '/') . $HTTP_HOST . '%{REQUEST_URI}/index%{ENV:WPR_SSL}.html' . $enc . '" -f' . PHP_EOL; } $rules .= 'RewriteRule .* "' . $cache_root . $HTTP_HOST . '%{REQUEST_URI}/index%{ENV:WPR_SSL}.html' . $enc . '" [L]' . PHP_EOL; $rules .= '</IfModule>' . PHP_EOL; /** * Filter rewrite rules to serve the cache file * * @since 1.0 * * @param string $rules Rules that will be printed */ $rules = apply_filters('rocket_htaccess_mod_rewrite', $rules); return $rules; }