/** * ob_callback() * * @param string $buffer * @return string $buffer **/ static function ob_callback($buffer) { if (!class_exists('sem_cache')) { return $buffer; } if (DONOTCACHEPAGE) { return $buffer; } # some things just shouldn't be cached if (self::$nocache && !in_array(self::$status_code, array(301, 302, 404)) || !in_array(self::$status_code, array(200, 301, 302, 404))) { return $buffer; } # sanity check on cookies global $sem_cache_cookies; if (!$sem_cache_cookies || $sem_cache_cookies != sem_cache::get_cookies()) { return $buffer; } # only cache visitor requests if (array_intersect(array_keys($_COOKIE), $sem_cache_cookies)) { return $buffer; } # bail on valid but rarely served pages if (self::$status_code == 200 && !(!is_feed() && is_singular() || !is_feed() && (is_home() || is_category()) && !is_paged() || is_feed() && (!is_archive() && !is_singular() || is_category()))) { return $buffer; } #don't cache a contact form page due to spam prevention techniques // if ( preg_match("/(\bsem_contact_form\b)/i",$buffer) ) // return $buffer; #don't cache a page due if a sem_no_cache flag is found. Provide manual way to exclude a page if (preg_match("/(\\bsem_do_not_cache\\b)/i", $buffer)) { return $buffer; } $permalink_structure = get_option('permalink_structure'); # statically cache only when relevant self::$static &= strpos($_SERVER['REQUEST_URI'], './') === false && self::$status_code == 200 && !is_feed() && empty($_GET) && $permalink_structure && strpos($permalink_structure, "|/index\\.php/|") === false; # sanity check on the base folder $host = get_option('home'); if (preg_match("|^([^/]+://[^/]+)/|", $host, $_host)) { $host = end($_host); } if ($host != self::$host) { return $buffer; } # sanity check on incomplete files if (!in_array(self::$status_code, array(301, 302)) && !preg_match("/(?:<\\/html>|<\\/rss>|<\\/feed>)/i", $buffer)) { return $buffer; } # sanity check on mobile users global $sem_mobile_agents; if ($sem_mobile_agents != sem_cache::get_mobile_agents()) { return $buffer; } $mobile_agents = $sem_mobile_agents; // $mobile_agents = array_map('preg_quote', (array) $mobile_agents); $mobile_agents = implode("|", $mobile_agents); if (preg_match("{({$mobile_agents})}i", $_SERVER['HTTP_USER_AGENT'])) { return $buffer; } if (preg_match("/(?=.*?\\bandroid\\b)(?=.*?\\bmobile\\b).*\$/i", $_SERVER['HTTP_USER_AGENT'])) { return $buffer; } // made it through all the checks. Let's minify the html now // $buffer = self::minify_html( $buffer ); if (self::$static) { $file = preg_replace("/#.*/", '', $_SERVER['REQUEST_URI']); $file = '/static/' . trim($file, '/'); if (!preg_match("/\\.html\$/", $file)) { global $wp_rewrite; if ($wp_rewrite->use_trailing_slashes) { $file = $file . '/index.html'; } else { $file = $file . '.html'; } } cache_fs::put_contents($file, $buffer); } elseif (self::$memory) { $cache_id = $host . preg_replace("/#.*/", '', $_SERVER['REQUEST_URI']); $cache_id = md5($cache_id); $headers = headers_list(); if (self::$status_header) { array_unshift($headers, self::$status_header); } wp_cache_add($cache_id, $headers, 'cached_headers', cache_timeout); if ($buffer && !in_array(self::$status_code, array(301, 302))) { wp_cache_add($cache_id, $buffer, 'cached_buffers', cache_timeout); } } elseif (!(function_exists('is_multisite') && is_multisite())) { # poor man's memcached $cache_id = $host . preg_replace("/#.*/", '', $_SERVER['REQUEST_URI']); $cache_id = md5($cache_id); $file = '/semi-static/' . $cache_id; $headers = headers_list(); if (self::$status_header) { array_unshift($headers, self::$status_header); } cache_fs::put_contents($file . '.meta', serialize($headers)); if ($buffer && !in_array(self::$status_code, array(301, 302))) { cache_fs::put_contents($file . '.html', $buffer); } } return $buffer; }
/** * enable_static() * * @return bool $success **/ static function enable_static() { if (!self::can_static() && !self::can_memory()) { return false; } $static_cache = (bool) get_option('static_cache'); $memory_cache = (bool) get_option('memory_cache'); wp_clear_scheduled_hook('cache_timeout'); wp_clear_scheduled_hook('static_cache_timeout'); # sanity check if (!$static_cache && !$memory_cache) { return self::disable_static(); } if (!wp_mkdir_p(WP_CONTENT_DIR . '/cache')) { echo '<div class="error">' . '<p>' . sprintf(__('Error: Failed to create %s.', 'sem-cache'), 'wp-content/cache') . '</p>' . '</div>' . "\n"; return false; } $use_static_cache = $static_cache ? 'true' : 'false'; $use_memory_cache = $memory_cache ? 'true' : 'false'; $sem_cache_cookies = var_export(sem_cache::get_cookies(), true); $sem_mobile_agents = var_export(sem_cache::get_mobile_agents(), true); $sem_cache_file = dirname(__FILE__) . '/static-cache.php'; $contents = <<<EOS <?php define('static_cache', {$use_static_cache}); define('memory_cache', {$use_memory_cache}); \$sem_cache_cookies = {$sem_cache_cookies}; \$sem_mobile_agents = {$sem_mobile_agents}; include '{$sem_cache_file}'; ?> EOS; $file = WP_CONTENT_DIR . '/advanced-cache.php'; $perms = stat(WP_CONTENT_DIR); $perms = $perms['mode'] & 0666; if (!file_put_contents($file, $contents) || !chmod($file, $perms)) { echo '<div class="error">' . '<p>' . sprintf(__('Error: Failed to write %s.', 'sem-cache'), 'wp-content/advanced-cache.php') . '</p>' . '</div>' . "\n"; return false; } # Enable the static cache if (!function_exists('save_mod_rewrite_rules') || !function_exists('get_home_path')) { include_once ABSPATH . 'wp-admin/includes/admin.php'; } if (!isset($GLOBALS['wp_rewrite'])) { $GLOBALS['wp_rewrite'] = new WP_Rewrite(); } # prevent mass-flushing when the permalink structure hasn't changed remove_action('generate_rewrite_rules', array('sem_cache', 'flush_cache')); global $wp_rewrite; $wp_rewrite->flush_rules(); # Enable the memory cache if ($memory_cache && !self::enable_memcached()) { return false; } # Enable the cache $file = ABSPATH . 'wp-config.php'; if (!defined('WP_CACHE') || !WP_CACHE) { $contents = file_get_contents($file); $line = "define('WP_CACHE', true);"; if (stristr($contents, "'WP_CACHE'")) { $contents = preg_replace("/\n\t\t\t\t\t(?!(?:\\/\\/|\\#)\\s*)\n\t\t\t\t\tdefine\\s*\\(\\s*\n\t\t\t\t\t\t(['\"])WP_CACHE\\1\n\t\t\t\t\t\t.*?;\n\t\t\t\t\t/x", $line, $contents); } else { $contents = preg_replace("/^<\\?php\\s*/", "<?php" . PHP_EOL . PHP_EOL . $line . PHP_EOL . PHP_EOL, $contents); } if (!$contents || !file_put_contents($file, $contents)) { echo '<div class="error">' . '<p>' . __('Error: Failed to override the WP_CACHE define in wp-config.php.', 'sem-cache') . '</p>' . '</div>' . "\n"; return false; } } return true; }