function flush() { static $done = false; if ($done) { return false; } # can't flush if WP isn't loaded if (!function_exists('get_option')) { return false; } $done = true; global $wpdb; $this->flush = true; if (method_exists('static_cache', 'disable')) { static_cache::disable(); } # flush posts $posts = $wpdb->get_results("SELECT ID, post_title, post_name, post_date, post_type, post_status, post_author, post_parent FROM {$wpdb->posts} WHERE post_status IN ('publish', 'private') OR post_type = 'attachment'"); # force WP widgets to flush wp_cache_delete('widget_recent_posts', 'widget'); wp_cache_delete('recent_comments', 'widget'); wp_cache_delete('get_calendar', 'calendar'); wp_cache_delete('wp_get_archives', 'general'); wp_cache_delete('all_page_ids', 'posts'); wp_cache_delete('get_pages', 'posts'); $post_ids = array(); foreach ($posts as $post) { $post_ids[] = $post->ID; $this->delete($post->ID, 'posts'); $this->delete($post->ID, 'post_meta'); clean_object_term_cache($post->ID, 'post'); do_action('clean_post_cache', $post->ID); # fill a temporary bucket so as to handle permalinks $key = $this->key($post->ID, 'posts'); $this->cache[$key] = $post; if (class_exists('sem_cache') && $post->post_type == 'post') { sem_cache::do_flush_author($post->post_author); sem_cache::do_flush_date($post->post_date); } } unset($posts); if (class_exists('sem_cache')) { # flush get_permalink() intensive stuff before flushing terms foreach ($post_ids as $post_id) { sem_cache::do_flush_post($post_id); } # flush home sem_cache::do_flush_home(); } # flush terms $terms = $wpdb->get_results("SELECT term_id, taxonomy FROM {$wpdb->term_taxonomy} WHERE count > 0"); $taxonomies = array(); $term_ids = array(); if (class_exists('sem_cache')) { foreach ($terms as $term) { sem_cache::do_flush_term($term->term_id, $term->taxonomy); } } foreach ($terms as $term) { $taxonomies[] = $term->taxonomy; $term_ids[] = $term->term_id; $this->delete($term->term_id, $term->taxonomy); } $taxonomies = array_unique($taxonomies); foreach ($taxonomies as $taxonomy) { $this->delete('all_ids', $taxonomy); $this->delete('get', $taxonomy); delete_option("{$taxonomy}_children"); do_action('clean_term_cache', $term_ids, $taxonomy); } $this->delete('last_changed', 'terms'); unset($terms); # flush users $user_ids = $wpdb->get_col("SELECT ID FROM {$wpdb->users}"); foreach ($user_ids as $user_id) { $this->delete($user_id, 'users'); } unset($user_ids); # backup key transients $transients = array('update_core', 'update_plugins', 'update_themes', 'sem_memberships', 'sem_update_plugins', 'sem_update_themes'); $extra = array('feed_220431e2eb0959fa9c7fcb07c6e22632', 'feed_mod_220431e2eb0959fa9c7fcb07c6e22632'); foreach (array_merge($transients, $extra) as $var) { ${$var} = get_transient($var); } # flush options $options = $wpdb->get_col("SELECT option_name FROM {$wpdb->options}"); foreach ($options as $option) { if (!in_array($option, array_merge($transients, $extra))) { if (preg_match("/^_transient_/", $option)) { delete_option($option); } else { $this->delete($option, 'options'); } } } $this->delete('notoptions', 'options'); $this->delete('alloptions', 'options'); unset($options); # restore key transients foreach ($transients as $var) { if (${$var} !== false) { set_transient($var, ${$var}); } } if ($feed_220431e2eb0959fa9c7fcb07c6e22632 !== false) { $var = 'feed_220431e2eb0959fa9c7fcb07c6e22632'; set_transient($var, ${$var}, min(3600, cache_timeout)); $var = 'feed_mod_220431e2eb0959fa9c7fcb07c6e22632'; set_transient($var, time(), min(3600, cache_timeout)); } return true; }
/** * Access this plugin’s working instance * * @wp-hook plugins_loaded * @return object of this class */ public static function get_instance() { NULL === self::$instance and self::$instance = new self(); return self::$instance; }
/** * 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; }
/** * disable_assets() * * @return void **/ static function disable_assets() { update_site_option('asset_cache', 0); sem_cache::flush_assets(); }