Example #1
0
 /**
  * start()
  *
  * @return void
  **/
 static function start()
 {
     if (strpos($_SERVER['HTTP_HOST'], '/') !== false) {
         trigger_error(sprintf('%s is not a valid hostname', $_SERVER['HTTP_HOST']), E_USER_ERROR);
     }
     self::$base_dir = defined('SUBDOMAIN_INSTALL') && SUBDOMAIN_INSTALL ? WP_CONTENT_DIR . '/cache/' . $_SERVER['HTTP_HOST'] : WP_CONTENT_DIR . '/cache';
 }
Example #2
0
 /**
  * concat_scripts()
  *
  * @param string $file
  * @param array $handles
  * @return void
  **/
 static function concat_scripts($file, $handles)
 {
     global $wp_scripts;
     $js = array();
     foreach ($handles as $handle) {
         $src = $wp_scripts->registered[$handle]->src;
         if (substr($src, 0, 1) == '/') {
             $src = ABSPATH . ltrim($src, '/');
         } else {
             $src = str_replace(array(plugins_url(), content_url()), array(WP_PLUGIN_DIR, WP_CONTENT_DIR), $src);
         }
         $script = self::strip_bom(file_get_contents($src));
         $js[] = self::compress_js($script);
     }
     cache_fs::put_contents($file, implode("\n\n", $js));
 }
Example #3
0
 /**
  * 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;
 }
Example #4
0
 /**
  * flush_post_url()
  *
  * @param $link
  * @internal param string $url
  * @internal param int $timeout
  * @return void
  */
 static function flush_post_url($link)
 {
     $cache_id = md5($link);
     wp_cache_delete($cache_id, 'url2post_id');
     if (static_cache) {
         static $permalink_structure;
         if (!isset($permalink_structure)) {
             $permalink_structure = get_option('permalink_structure');
         }
         # 5 min throttling in case the site is getting hammered by comments
         $timeout = !is_admin() && current_filter() == 'wp_update_comment_count' ? 300 : false;
         if ($permalink_structure) {
             $path = trim(preg_replace("|^[^/]+://[^/]+|", '', $link), '/');
             cache_fs::flush('/static/' . $path, $timeout, false);
         }
     }
     if (memory_cache) {
         wp_cache_delete($cache_id, 'cached_headers');
         wp_cache_delete($cache_id, 'cached_buffers');
     } elseif (static_cache) {
         cache_fs::flush('/semi-static/' . $cache_id . '.meta', $timeout, false);
         cache_fs::flush('/semi-static/' . $cache_id . '.html', $timeout, false);
     }
 }
Example #5
0
 /**
  * edit_options()
  *
  * @return void
  **/
 static function edit_options()
 {
     echo '<div class="wrap">' . "\n" . '<form method="post" action="">';
     wp_nonce_field('sem_cache');
     list($files, $expired) = cache_fs::stats('/', cache_timeout);
     $static_errors = array();
     $memory_errors = array();
     $query_errors = array();
     $object_errors = array();
     $assets_errors = array();
     $gzip_errors = array();
     $gzip_notice = array();
     if (!self::can_memcached()) {
         $error = sprintf(__('<a href="%1$s">Memcache</a> is not installed on your server, or the php extension is misconfigured, or the daemon is not running. Note that shared hosts never offer memcache; you need a dedicated server or a VPS to take advantage of it. Also note that there are two PHP extensions, and that only <a href="%1$s">this one</a> (Memcache not Memcached) is supported.', 'sem-cache'), 'http://www.php.net/manual/en/book.memcache.php');
         $memory_errors[] = $error;
         $query_errors[] = $error;
         $object_errors[] = $error;
     } elseif (!self::can_object()) {
         $error = __('WP cannot overwrite the object-cache.php file in your wp-content folder. The file needs to be writable by the server.', 'sem-cache');
         $memory_errors[] = $error;
         $query_errors[] = $error;
         $object_errors[] = $error;
     }
     if (!version_compare(phpversion(), '5.1', '>=')) {
         $error = sprintf(__('The Query Cache requires PHP 5.1 or more. Your server is currently running PHP %s. Please contact your host and have them upgrade PHP.', 'sem-cache'), phpversion());
         $query_errors[] = $error;
     }
     if ((@ini_get('safe_mode') || @ini_get('open_basedir')) && !wp_mkdir_p(WP_CONTENT_DIR . '/cache')) {
         $error = __('Safe mode or an open_basedir restriction is enabled on your server.', 'sem-cache');
         $static_errors[] = $error;
         $assets_errors[] = $error;
     }
     if (!(!get_option('permalink_structure') || is_writable(ABSPATH . '.htaccess')) && !(function_exists('is_multisite') && is_multisite())) {
         $error = __('WP cannot overwrite your site\'s .htaccess file to insert new rewrite rules. The file needs to be writable by your server.', 'sem-cache');
         $static_errors[] = $error;
     }
     if (!is_writable(ABSPATH . '.htaccess')) {
         $error = __('WP cannot overwrite your site\'s .htaccess file to insert extra instructions. The file needs to be writable by your server.', 'sem-cache');
         $gzip_errors[] = $error;
     }
     if (!(defined('WP_CACHE') && WP_CACHE || is_writable(ABSPATH . 'wp-config.php'))) {
         $error = __('WP cannot define a WP_CACHE constant in your site\'s wp-config.php file. It needs to be added manually, or the file needs to be writable by the server.', 'sem-cache');
         $static_errors[] = $error;
         $memory_errors[] = $error;
     }
     if (!(!file_exists(WP_CONTENT_DIR . '/advanced-cache.php') || is_writable(WP_CONTENT_DIR . '/advanced-cache.php'))) {
         $error = __('WP cannot overwrite the advanced-cache.php file in your wp-content folder. The file needs to be writable by the server.', 'sem-cache');
         $static_errors[] = $error;
         $memory_errors[] = $error;
     }
     if (!(!file_exists(WP_CONTENT_DIR . '/cache') && is_writable(WP_CONTENT_DIR) || is_dir(WP_CONTENT_DIR . '/cache') && is_writable(WP_CONTENT_DIR . '/cache'))) {
         $error = __('WP cannot create or write to the cache folder in your site\'s wp-content folder. It or the wp-content folder needs to be writable by the server.', 'sem-cache');
         $static_errors[] = $error;
         $assets_errors[] = $error;
     }
     if (function_exists('apache_get_modules')) {
         if (!apache_mod_loaded('mod_deflate')) {
             $error = __('mod_deflate is required in order to allow Apache to conditionally compress the files it sends. (mod_gzip is not supported because it is too resource hungry.)  Please contact your host so they configure Apache accordingly.', 'sem-cache');
             $gzip_errors[] = $error;
         }
         if (!apache_mod_loaded('mod_headers')) {
             $error = __('mod_headers is required in order to avoid that proxies serve gzipped items to user agents who cannot use them. Please contact your host so they configure Apache accordingly.', 'sem-cache');
             $gzip_errors[] = $error;
         }
     } else {
         # just assume it works
         $gzip_notice[] = __('gzip caching requires mod_deflate and mod_headers, but the Semiologic Cache plugin cannot determine whether they are installed on your server. Please check with your host.', 'sem-cache');
     }
     if (function_exists('is_multisite') && is_multisite()) {
         $static_errors = array(__('The filesystem-based cache cannot be enabled on multisite installations.', 'sem-cache'));
     }
     foreach (array('static_errors' => __('Filesystem-based static cache errors', 'sem-cache'), 'memory_errors' => __('Memcache-based static cache errors', 'sem-cache'), 'query_errors' => __('Query cache errors', 'sem-cache'), 'object_errors' => __('Object cache errors', 'sem-cache'), 'assets_errors' => __('Asset cache errors', 'sem-cache'), 'gzip_errors' => __('Gzip cache errors', 'sem-cache'), 'gzip_notice' => __('Gzip cache notice', 'sem-cache')) as $var => $title) {
         if (!${$var}) {
             ${$var} = false;
         } else {
             ${$var} = '<h3>' . $title . '</h3>' . "\n" . '<ul class="ul-square">' . "\n" . '<li>' . implode("</li>\n<li>", ${$var}) . '</li>' . "\n" . '</ul>' . "\n";
         }
     }
     echo '<h2>' . __('Cache Settings', 'sem-cache') . '</h2>' . "\n";
     echo '<table class="form-table">' . "\n";
     echo '<tr>' . "\n" . '<th scope="row">' . __('Quick and Easy', 'sem-cache') . '</th>' . "\n" . '<td>' . '<button type="submit" name="action" value="on" class="submit button">' . __('Turn the cache on', 'sem-cache') . '</button>' . ' ' . '<button type="submit" name="action" value="off" class="submit button">' . __('Turn the cache off', 'sem-cache') . '</button>' . ' ' . '<button type="submit" name="action" value="flush" class="submit button">' . sprintf(__('Flush %d cached files', 'sem-cache'), $files) . '</button>' . ' ' . '<button type="submit" name="action" value="clean" class="submit button">' . sprintf(__('Flush %d expired files', 'sem-cache'), $expired) . '</button>' . '<p>' . __('The first of the above four buttons will autodetect the best means to improve the performance of your site, and turn the cache on. The second one will turn the cache off. The last two will retain your settings, and stick to flushing the cache.', 'sem-cache') . '</p>' . "\n" . '</td>' . "\n" . '</tr>' . "\n";
     echo '<tr>' . "\n" . '<th scope="row">' . __('Static Cache', 'sem-cache') . '</th>' . "\n" . '<td>' . '<p>' . '<label>' . '<input type="checkbox"' . ' id="static_cache" name="static_cache"' . checked((bool) get_site_option('static_cache'), true, false) . ($static_errors ? ' disabled="disabled"' : '') . ' />' . '&nbsp;' . __('Serve filesystem-based, static versions of my site\'s web pages.', 'sem-cache') . '</label>' . '</p>' . "\n" . '<p>' . '<label>' . '<input type="checkbox"' . ' id="memory_cache" name="memory_cache"' . checked((bool) get_site_option('memory_cache'), true, false) . ($memory_errors ? ' disabled="disabled"' : '') . ' />' . '&nbsp;' . __('Serve memcache-based, static versions of my site\'s web pages.', 'sem-cache') . '</label>' . '</p>' . "\n" . '<p>' . __('The static cache will attempt to serve previously rendered version of the requested web pages to visitors who aren\'t logged in. The key drawback is that your visitors are not always viewing the latest version of your web pages. Lists of recent posts and recent comments, for instance, may take up to  hours to refresh across your site. In addition, it prevents any random elements that are introduced at the php level from working.', 'sem-cache') . '</p>' . "\n" . '<p>' . __('Key web pages on your site will get refreshed when you edit your posts and pages, so as to ensure they\'re reasonably fresh. Newly approved comments will trigger throttled refreshes of an even smaller subset of web pages. Statically cached web pages expire after 24 hours.', 'sem-cache') . '</p>' . "\n" . '<p>' . __('The benefit of the filesystem-based static cache is that your site\'s key web pages, such as the site\'s front page or individual posts, will be served without even loading PHP. This allows for maximum scalability if your site is getting hammered by excrutiating traffic.', 'sem-cache') . '</p>' . "\n" . '<p>' . __('The memcache-based static cache works in a similar manner, but stores cached pages in memcache rather than on the filesystem. PHP is always loaded, so it\'s a bit slower for key web pages; but it\'s much faster than using the filesystem for other web pages.', 'sem-cache') . '</p>' . "\n" . '<p>' . __('You\'ll usually want both turned on, in order to get the best of both worlds. The only exception is if your site is hosted on multiple servers: in this case, consider sticking to the memory-based static cache, because of the lag introduced by the filesystem\'s synchronisations from a server to the next.', 'sem-cache') . '</p>' . $static_errors . $memory_errors . '</td>' . "\n" . '</tr>' . "\n";
     echo '<tr>' . "\n" . '<th scope="row">' . __('Query Cache', 'sem-cache') . '</th>' . "\n" . '<td>' . '<p>' . '<label>' . '<input type="checkbox"' . ' id="query_cache" name="query_cache"' . checked((bool) get_site_option('query_cache'), true, false) . ($query_errors ? ' disabled="disabled"' : '') . ' />' . '&nbsp;' . __('Cache MySQL query results in memory.', 'sem-cache') . '</label>' . '</p>' . "\n" . '<p>' . __('The query cache lets WordPress work in a fully dynamic manner, while doing its best to avoid hits to the MySQL database.', 'sem-cache') . '</p>' . "\n" . '<p>' . __('The query cache primarily benefits commentors and users who are logged in; in particular yourself. These users cannot benefit from a static cache, because each of web page on your site potentially contains data that is specific to them; but they fully benefit from a query cache.', 'sem-cache') . '</p>' . "\n" . '<p>' . __('The query cache\'s refresh policy is similar to that of the memory-based static cache: key queries are flushed whenever you edit posts or pages, or approve new comments. All of the remaining queries expire after 24 hours.', 'sem-cache') . '</p>' . "\n" . $query_errors . '</td>' . "\n" . '</tr>' . "\n";
     echo '<tr>' . "\n" . '<th scope="row">' . __('Object Cache', 'sem-cache') . '</th>' . "\n" . '<td>' . '<p>' . '<label>' . '<input type="checkbox"' . ' id="object_cache" name="object_cache"' . checked((bool) get_site_option('object_cache'), true, false) . ($object_errors ? ' disabled="disabled"' : '') . ' />' . '&nbsp;' . __('Make WordPress objects persistent.', 'sem-cache') . '</label>' . '</p>' . "\n" . '<p>' . __('The object cache stores granular bits of information in memcache, and makes them available from a page to the next. This allows WordPress to load web pages without always needing to retrieve things such as options, users, or individual entries from the database.', 'sem-cache') . '</p>' . "\n" . '<p>' . __('The object cache\'s primary benefit is that it is always accurate: at no time will it ever serve data that is potentially outdated.', 'sem-cache') . '</p>' . "\n" . '<p>' . __('The object cache is automatically turned on, and cannot be disabled, if you use the memory-based static cache or the query cache.', 'sem-cache') . '</p>' . "\n" . $object_errors . '</td>' . "\n" . '</tr>' . "\n";
     echo '<tr>' . "\n" . '<th scope="row">' . __('Asset Cache', 'sem-cache') . '</th>' . "\n" . '<td>' . '<p>' . '<label>' . '<input type="checkbox"' . ' id="asset_cache" name="asset_cache"' . checked((bool) get_site_option('asset_cache'), true, false) . ($assets_errors ? ' disabled="disabled"' : '') . ' />' . '&nbsp;' . __('Enable the asset cache.', 'sem-cache') . '</label>' . '</p>' . "\n" . '<p>' . __('The asset cache speeds your site up by minimizing the number of server requests. It achieve this by concatenating your javascript and CSS files on the front end.', 'sem-cache') . '</p>' . "\n" . '<p>' . __('This setting should always be turned on, unless you\'re in the process of manually editing these assets.', 'sem-cache') . '</p>' . "\n" . $assets_errors . '</td>' . "\n" . '</tr>' . "\n";
     echo '<tr>' . "\n" . '<th scope="row">' . __('File Compression', 'sem-cache') . '</th>' . "\n" . '<td>' . '<p>' . '<label>' . '<input type="checkbox"' . ' id="gzip_cache" name="gzip_cache"' . checked((bool) get_site_option('gzip_cache'), true, false) . ($gzip_errors ? ' disabled="disabled"' : '') . ' />' . '&nbsp;' . __('Enable text file compression.', 'sem-cache') . '</label>' . '</p>' . "\n" . '<p>' . __('Compressing files that are sent by your site trims the load time by as much as 70%. The file compression itself is taken care of at the Apache level, by using mod_deflate.', 'sem-cache') . '</p>' . "\n" . '<p>' . __('This setting should always be turned on, unless you\'re in the process of manually editing files on your site.', 'sem-cache') . '</p>' . "\n" . $gzip_errors . $gzip_notice . '</td>' . "\n" . '</tr>' . "\n";
     echo '</table>' . "\n";
     echo '<p class="submit">' . '<button type="submit" name="action" value="save" class="submit button">' . __('Save Changes', 'sem-cache') . '</button>' . '</p>' . "\n";
     echo '</form>' . "\n" . '</div>' . "\n";
 }