/** * 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)); }
/** * 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; }