/** * @param W3_Config $config * @param W3_Config $config_master * @param bool $do_merge if to merge with network main site * @return string */ public static function get_wordpress_appname($config, $config_master, $do_merge = true) { if (w3_is_network()) { if ($config_master->get_boolean('newrelic.use_network_wide_id')) { $appname = $config_master->get_string('newrelic.appname'); } else { $merge = $config->get_boolean('newrelic.merge_with_network'); $merge_name = ''; if ($do_merge && $merge && w3_get_blog_id() != 0) { $merge_name = ';' . $config_master->get_string('newrelic.appname'); } if (w3_get_blog_id() != 0 && !$config->get_boolean('common.force_master')) { $appname = $config->get_string('newrelic.appname', ''); if (empty($appname)) { $prefix = $config->get_string('newrelic.appname_prefix'); $appname = $prefix . trim(w3_get_home_domain() . w3_get_site_path(), '/'); } } else { if (w3_get_blog_id() != 0) { $prefix = $config->get_string('newrelic.appname_prefix'); $appname = $prefix . trim(w3_get_home_domain() . w3_get_site_path(), '/'); } else { $appname = $config->get_string('newrelic.appname'); } } $appname = $appname . $merge_name; } } else { $appname = $config->get_string('newrelic.appname'); } return $appname; }
/** * Returns full urls for frontpage including older pages * @param int $limit_post_pages default is 10 * @return array */ function get_frontpage_urls($limit_post_pages = 10) { if (!isset($this->frontpage_urls[$limit_post_pages])) { $front_page = get_option('show_on_front'); $full_urls = array(); $home_path = w3_get_home_path(); $site_path = w3_get_site_path(); $full_urls[] = $this->home_url . '/'; if ($site_path != $home_path) { $full_urls[] = w3_get_site_url() . '/'; } if ($front_page == 'posts') { $full_urls = array_merge($full_urls, $this->get_older_pages($home_path, $limit_post_pages)); } $this->frontpage_urls[$limit_post_pages] = $full_urls; } return $this->frontpage_urls[$limit_post_pages]; }
/** * Translates remote file to local file * * @param string $file * @return string */ function w3_translate_file($file) { if (!w3_is_url($file)) { $file = '/' . ltrim($file, '/'); $regexp = '~^' . w3_preg_quote(w3_get_site_path()) . '~'; $file = preg_replace($regexp, w3_get_base_path(), $file); $file = ltrim($file, '/'); } return $file; }
/** * Returns page key * * @param string $request_uri * @param string $compression * @return string */ function _get_page_key($request_uri, $compression) { if ($this->_config->get_string('pgcache.engine') == 'file_pgcache') { $key = preg_replace('~[/\\\\]+~', '/', $key); $key = preg_replace('~\\?.*$~', '', $request_uri); $key = str_replace(w3_get_site_path(), '/', $key); $key = str_replace('/index.php', '/', $key); if ($key == '') { $key = '/'; } if (substr($key, -1) == '/') { $key .= '_default_.html'; } $key = ltrim($key, '/'); if (!empty($compression)) { $key .= '.' . $compression; } } else { $blogname = w3_get_blogname(); if ($blogname == '') { $blogname = $_SERVER['HTTP_HOST']; } $key = sprintf('w3tc_%s_page_%s', md5($blogname), md5($request_uri)); if (!empty($compression)) { $key .= '_' . $compression; } } return $key; }
/** * Returns server info * * @return array */ function get_server_info() { global $wp_version, $wp_db_version, $wpdb; $wordpress_plugins = get_plugins(); $wordpress_plugins_active = array(); foreach ($wordpress_plugins as $wordpress_plugin_file => $wordpress_plugin) { if (is_plugin_active($wordpress_plugin_file)) { $wordpress_plugins_active[$wordpress_plugin_file] = $wordpress_plugin; } } $mysql_version = $wpdb->get_var('SELECT VERSION()'); $mysql_variables_result = (array) $wpdb->get_results('SHOW VARIABLES', ARRAY_N); $mysql_variables = array(); foreach ($mysql_variables_result as $mysql_variables_row) { $mysql_variables[$mysql_variables_row[0]] = $mysql_variables_row[1]; } $server_info = array('w3tc' => array('version' => W3TC_VERSION, 'server' => !empty($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : 'Unknown', 'dir' => W3TC_DIR, 'cache_dir' => W3TC_CACHE_DIR, 'blog_id' => w3_get_blog_id(), 'document_root' => w3_get_document_root(), 'home_root' => w3_get_home_root(), 'site_root' => w3_get_site_root(), 'base_path' => w3_get_base_path(), 'home_path' => w3_get_home_path(), 'site_path' => w3_get_site_path()), 'wp' => array('version' => $wp_version, 'db_version' => $wp_db_version, 'abspath' => ABSPATH, 'home' => get_option('home'), 'siteurl' => get_option('siteurl'), 'email' => get_option('admin_email'), 'upload_info' => (array) w3_upload_info(), 'theme' => w3tc_get_current_theme(), 'wp_cache' => defined('WP_CACHE') && WP_CACHE ? 'true' : 'false', 'plugins' => $wordpress_plugins_active), 'mysql' => array('version' => $mysql_version, 'variables' => $mysql_variables)); return $server_info; }
/** * Returns the sitepath for multisite subfolder or subdomain path for multisite subdomain * @return string */ private function _get_multisite_url_identifier() { if (defined('DOMAIN_MAPPING') && DOMAIN_MAPPING) { $parsedUrl = parse_url(w3_get_site_url()); return $parsedUrl['host']; } elseif (w3_is_subdomain_install()) { $parsedUrl = parse_url(w3_get_domain_url()); $urlparts = explode('.', $parsedUrl['host']); if (sizeof($urlparts) > 2) { $subdomain = array_shift($urlparts); return trim($subdomain, '/'); } } return trim(w3_get_site_path(), '/'); }
/** * Returns minify groups * @param string $group * @param string $type * @return array */ function get_groups($group, $type) { $result = array(); switch ($type) { case 'css': $groups = $this->_config->get_array('minify.css.groups'); break; case 'js': $groups = $this->_config->get_array('minify.js.groups'); break; default: return $result; } if (isset($groups['default'])) { $locations = (array) $groups['default']; } else { $locations = array(); } if ($group != 'default' && isset($groups[$group])) { $locations = array_merge_recursive($locations, (array) $groups[$group]); } foreach ($locations as $location => $config) { if (!empty($config['files'])) { foreach ((array) $config['files'] as $file) { if (w3_is_url($file)) { if ($precached_file = $this->_precache_file($file, $type)) { $result[$location][$file] = $precached_file; } } else { if (w3_get_blog_id()) { // for WPMU we have to remove blog path $file = str_replace(w3_get_site_path(), '', $file); } $result[$location][$file] = '//' . $file; } } } } return $result; }
/** * Returns minify groups * * @param string $theme * @param string $template * @param string $type * @return array */ function get_groups($theme, $template, $type) { $result = array(); switch ($type) { case 'css': $groups = $this->_config->get_array('minify.css.groups'); break; case 'js': $groups = $this->_config->get_array('minify.js.groups'); break; default: return $result; } if (isset($groups[$theme]['default'])) { $locations = (array) $groups[$theme]['default']; } else { $locations = array(); } if ($template != 'default' && isset($groups[$theme][$template])) { $locations = array_merge_recursive($locations, (array) $groups[$theme][$template]); } foreach ($locations as $location => $config) { if (!empty($config['files'])) { foreach ((array) $config['files'] as $file) { $file = w3_normalize_file_minify2($file); if (w3_is_url($file)) { $precached_file = $this->_precache_file($file, $type); if ($precached_file) { $result[$location][$file] = $precached_file; } else { $this->error(sprintf('Unable to cache remote file: "%s"', $file)); } } else { if (!w3_is_multisite() && strpos(trailingslashit(WP_CONTENT_DIR), trailingslashit(w3_get_site_root())) !== false) { $file = ltrim(w3_get_site_path(), '/') . str_replace(ltrim(w3_get_site_path(), '/'), '', $file); } $path = w3_get_document_root() . '/' . $file; if (file_exists($path)) { $result[$location][$file] = '//' . $file; } else { $this->error(sprintf('File "%s" doesn\'t exist', $path)); } } } } } return $result; }
public function w3tc_loader_file_data() { $filename = W3TC_WP_LOADER; $data = "\n<?php\n if (W3TC_WP_LOADING)\n require_once '" . w3_get_document_root() . '/' . trim(w3_get_site_path(), "/") . "/wp-load.php';\n"; return array('filename' => $filename, 'data' => $data); }
/** * Exports custom files to CDN * * @return array */ function get_files_custom() { $files = array(); $document_root = w3_get_document_root(); $custom_files = $this->_config->get_array('cdn.custom.files'); $custom_files = array_map('w3_parse_path', $custom_files); $site_root = w3_get_site_root(); $path = w3_get_site_path(); if (strstr(WP_CONTENT_DIR, w3_get_site_root()) === false) { $site_root = w3_get_document_root(); $path = ''; } $content_path = trim(substr(WP_CONTENT_DIR, strlen($site_root)), '/\\'); $wp_content_folder = basename(WP_CONTENT_DIR); foreach ($custom_files as $custom_file) { if ($custom_file != '') { $custom_file = w3_normalize_file($custom_file); if (!w3_is_multisite()) { $dir = trim(dirname($path . $custom_file), '/\\'); $rel_path = trim(dirname($custom_file), '/\\'); } else { $rel_path = $dir = trim(dirname($custom_file), '/\\'); } if (strpos($dir, '<currentblog>') != false) { $rel_path = $dir = str_replace('<currentblog>', 'blogs.dir/' . w3_get_blog_id(), $dir); } if ($content_path && $content_path != $wp_content_folder) { $dir = str_replace($wp_content_folder, $content_path, $dir); $rel_path = str_replace($wp_content_folder, $content_path, $rel_path); } if ($dir == '.') { $rel_path = $dir = ''; } $mask = basename($custom_file); $files = array_merge($files, $this->search_files($document_root . '/' . $dir, $rel_path, $mask)); } } return $files; }
/** * Hook into WordPress SEO sitemap image filter. * @param $uri * @return string */ function wpseo_cdn_filter($uri) { $site_path = w3_get_site_path(); $domain_url_regexp = w3_get_domain_url_regexp(); $cdn = $this->_get_common()->get_cdn(); $parsed = parse_url($uri); $path = $parsed['path']; $remote_path = $this->_get_common()->uri_to_cdn_uri($path); $new_url = $cdn->format_url($remote_path); return $new_url; }
/** * Translates URL to local path * @param string $url * @return string */ function w3_translate_file($url) { if (!w3_is_url($url)) { $url = w3_get_domain_url() . '/' . ltrim($url, '/\\'); } $site_url_regexp = '~' . w3_get_site_url_regexp() . '~i'; if (preg_match($site_url_regexp, $url) && strstr($url, '?') === false) { $url = preg_replace($site_url_regexp, '', $url); $url = w3_get_site_path() . ltrim($url, '/\\'); } $url = ltrim($url, '/'); return $url; }
/** * Verify that WordPress install folder is part of WP_CONTENT_DIR path * @return bool */ public function should_create_wp_loader_file() { if (defined('DONOTVERIFY_WP_LOADER') && DONOTVERIFY_WP_LOADER) { return false; } if (w3_get_site_path() != '/' && strpos(WP_PLUGIN_DIR, w3_get_site_path()) === false) { return true; } return false; }
/** * Generates rules for WP dir * * @return string */ function generate_rules_core() { global $w3_reserved_blognames; /** * Auto reject cookies */ $reject_cookies = array('comment_author', 'wp-postpass'); /** * Auto reject URIs */ $reject_uris = array('\\/wp-admin\\/', '\\/xmlrpc.php', '\\/wp-(app|cron|login|register).php'); /** * Reject cache for logged in users */ if ($this->_config->get_boolean('pgcache.reject.logged')) { $reject_cookies = array_merge($reject_cookies, array('wordpress_[a-f0-9]+', 'wordpress_logged_in')); } /** * Reject cache for home page */ if (!$this->_config->get_boolean('pgcache.cache.home')) { $reject_uris[] = '^(\\/|\\/index.php)$'; } /** * Reject cache for feeds */ if (!$this->_config->get_boolean('pgcache.cache.feed')) { $reject_uris[] = 'feed'; } /** * Custom config */ $reject_cookies = array_merge($reject_cookies, $this->_config->get_array('pgcache.reject.cookie')); $reject_uris = array_merge($reject_uris, $this->_config->get_array('pgcache.reject.uri')); $reject_user_agents = $this->_config->get_array('pgcache.reject.ua'); $accept_files = $this->_config->get_array('pgcache.accept.files'); /** * WPMU support */ $is_wpmu = w3_is_wpmu(); $is_vhost = w3_is_vhost(); /** * Generate directives */ $rules = ''; $rules .= "# BEGIN W3 Total Cache\n"; $setenvif_rules = ''; if ($is_wpmu) { $setenvif_rules .= " SetEnvIfNoCase Host ^(www\\.)?([a-z0-9\\-\\.]+\\.[a-z]+)\\.?(:[0-9]+)?\$ DOMAIN=\$2\n"; if (!$is_vhost) { $setenvif_rules .= " SetEnvIfNoCase Request_URI ^" . w3_get_site_path() . "([a-z0-9\\-]+)/ BLOGNAME=\$1\n"; } } $compression = $this->_config->get_string('pgcache.compression'); if ($compression != '') { $compressions = array(); if (stristr($compression, 'gzip') !== false) { $compressions[] = 'gzip'; } if (stristr($compression, 'deflate') !== false) { $compressions[] = 'deflate'; } if (count($compressions)) { $setenvif_rules .= " SetEnvIfNoCase Accept-Encoding (" . implode('|', $compressions) . ") APPEND_EXT=.\$1\n"; } } if ($setenvif_rules != '') { $rules .= "<IfModule mod_setenvif.c>\n" . ($setenvif_rules .= "</IfModule>\n"); } $rules .= "<IfModule mod_rewrite.c>\n"; $rules .= " RewriteEngine On\n"; $mobile_redirect = $this->_config->get_string('pgcache.mobile.redirect'); if ($mobile_redirect != '') { $mobile_agents = $this->_config->get_array('pgcache.mobile.agents'); $rules .= " RewriteCond %{HTTP_USER_AGENT} (" . implode('|', array_map('w3_preg_quote', $mobile_agents)) . ") [NC]\n"; $rules .= " RewriteRule .* " . $mobile_redirect . " [R,L]\n"; } $rules .= " RewriteCond %{REQUEST_URI} \\/\$\n"; $rules .= " RewriteCond %{REQUEST_URI} !(" . implode('|', $reject_uris) . ")"; if (count($accept_files)) { $rules .= " [OR]\n RewriteCond %{REQUEST_URI} (" . implode('|', array_map('w3_preg_quote', $accept_files)) . ") [NC]\n"; } else { $rules .= "\n"; } $rules .= " RewriteCond %{REQUEST_METHOD} !=POST\n"; $rules .= " RewriteCond %{QUERY_STRING} =\"\"\n"; $rules .= " RewriteCond %{HTTP_COOKIE} !(" . implode('|', array_map('w3_preg_quote', $reject_cookies)) . ") [NC]\n"; if (count($reject_user_agents)) { $rules .= " RewriteCond %{HTTP_USER_AGENT} !(" . implode('|', array_map('w3_preg_quote', $reject_user_agents)) . ") [NC]\n"; } if ($is_wpmu) { if ($is_vhost) { $replacement = '/w3tc-%{ENV:DOMAIN}/'; } else { $rules .= " RewriteCond %{ENV:BLOGNAME} !^(" . implode('|', $w3_reserved_blognames) . ")\$\n"; $rules .= " RewriteCond %{ENV:BLOGNAME} !-f\n"; $rules .= " RewriteCond %{ENV:BLOGNAME} !-d\n"; $replacement = '/w3tc-%{ENV:BLOGNAME}.%{ENV:DOMAIN}/'; } $cache_dir = preg_replace('~/w3tc.*/~U', $replacement, W3TC_CACHE_FILE_PGCACHE_DIR, 1); } else { $cache_dir = W3TC_CACHE_FILE_PGCACHE_DIR; } $rules .= " RewriteCond " . w3_path($cache_dir) . "/\$1/_default_.html%{ENV:APPEND_EXT} -f\n"; $rules .= " RewriteRule (.*) " . str_replace(ABSPATH, '', $cache_dir) . "/\$1/_default_.html%{ENV:APPEND_EXT} [L]\n"; $rules .= "</IfModule>\n"; $rules .= "# END W3 Total Cache\n\n"; if (!$this->check_rules_wp()) { $rules .= "# BEGIN WordPress\n"; $rules .= "<IfModule mod_rewrite.c>\n"; $rules .= " RewriteEngine On\n"; $rules .= " RewriteCond %{REQUEST_FILENAME} !-f\n"; $rules .= " RewriteCond %{REQUEST_FILENAME} !-d\n"; $rules .= " RewriteRule .* index.php [L]\n"; $rules .= "</IfModule>\n"; $rules .= "# END WordPress\n\n"; } return $rules; }
/** * Link replace callback * * @param array $matches * @return string */ function link_replace_callback($matches) { global $wpdb; static $queue = null, $reject_files = null; if (in_array($matches[2], $this->replaced_urls)) { return $matches[0]; } if ($queue === null) { $sql = sprintf('SELECT remote_path FROM %s', $wpdb->prefix . W3TC_CDN_TABLE_QUEUE); $queue = $wpdb->get_col($sql); } if ($reject_files === null) { $reject_files = $this->_config->get_array('cdn.reject.files'); } /** * Don't replace links that are in the queue */ if (in_array(ltrim($matches[4], '/'), $queue)) { return $matches[0]; } /** * Don't replace link for rejected files */ foreach ($reject_files as $reject_file) { if ($reject_file != '') { $reject_file = w3_normalize_file($reject_file); $reject_file_regexp = '~^' . $this->get_regexp_by_mask($reject_file) . '$~i'; if (preg_match($reject_file_regexp, $matches[4])) { return $matches[0]; } } } /** * Do replacement */ $path = '/' . w3_get_site_path() . $matches[4]; $cdn =& $this->get_cdn(); $url = $cdn->format_url($path); if (!$url) { return $matches[0]; } $this->replaced_urls[] = $matches[2]; $replacement = sprintf('%s%s', $matches[1], $url); return $replacement; }
/** * Flushes post cache * * @param integer $post_id * @return boolean */ function flush_post($post_id = null) { if (!$post_id) { $post_id = $this->_detect_post_id(); } if ($post_id) { $uris = array(); $domain_url = w3_get_domain_url(); $feeds = $this->_config->get_array('pgcache.purge.feed.types'); if ($this->_config->get_boolean('pgcache.purge.terms') || $this->_config->get_boolean('pgcache.purge.feed.terms')) { $taxonomies = get_post_taxonomies($post_id); $terms = nxt_get_post_terms($post_id, $taxonomies); } switch (true) { case $this->_config->get_boolean('pgcache.purge.author'): case $this->_config->get_boolean('pgcache.purge.archive.daily'): case $this->_config->get_boolean('pgcache.purge.archive.monthly'): case $this->_config->get_boolean('pgcache.purge.archive.yearly'): case $this->_config->get_boolean('pgcache.purge.feed.author'): $post = get_post($post_id); } /** * Home URL */ if ($this->_config->get_boolean('pgcache.purge.home')) { $home_path = w3_get_home_path(); $site_path = w3_get_site_path(); $uris[] = $home_path; if ($site_path != $home_path) { $uris[] = $site_path; } } /** * Post URL */ if ($this->_config->get_boolean('pgcache.purge.post')) { $post_link = post_permalink($post_id); $post_uri = str_replace($domain_url, '', $post_link); $uris[] = $post_uri; } /** * Post comments URLs */ if ($this->_config->get_boolean('pgcache.purge.comments') && function_exists('get_comments_pagenum_link')) { $comments_number = get_comments_number($post_id); $comments_per_page = get_option('comments_per_page'); $comments_pages_number = @ceil($comments_number / $comments_per_page); for ($pagenum = 1; $pagenum <= $comments_pages_number; $pagenum++) { $comments_pagenum_link = $this->_get_comments_pagenum_link($post_id, $pagenum); $comments_pagenum_uri = str_replace($domain_url, '', $comments_pagenum_link); $uris[] = $comments_pagenum_uri; } } /** * Post author URLs */ if ($this->_config->get_boolean('pgcache.purge.author') && $post) { $posts_number = count_user_posts($post->post_author); $posts_per_page = get_option('posts_per_page'); $posts_pages_number = @ceil($posts_number / $posts_per_page); $author_link = get_author_link(false, $post->post_author); $author_uri = str_replace($domain_url, '', $author_link); for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) { $author_pagenum_link = $this->_get_pagenum_link($author_uri, $pagenum); $author_pagenum_uri = str_replace($domain_url, '', $author_pagenum_link); $uris[] = $author_pagenum_uri; } } /** * Post terms URLs */ if ($this->_config->get_boolean('pgcache.purge.terms')) { $posts_per_page = get_option('posts_per_page'); foreach ($terms as $term) { $term_link = get_term_link($term, $term->taxonomy); $term_uri = str_replace($domain_url, '', $term_link); $posts_pages_number = @ceil($term->count / $posts_per_page); for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) { $term_pagenum_link = $this->_get_pagenum_link($term_uri, $pagenum); $term_pagenum_uri = str_replace($domain_url, '', $term_pagenum_link); $uris[] = $term_pagenum_uri; } } } /** * Daily archive URLs */ if ($this->_config->get_boolean('pgcache.purge.archive.daily') && $post) { $post_date = strtotime($post->post_date); $post_year = gmdate('Y', $post_date); $post_month = gmdate('m', $post_date); $post_day = gmdate('d', $post_date); $posts_per_page = get_option('posts_per_page'); $posts_number = $this->_get_archive_posts_count($post_year, $post_month, $post_day); $posts_pages_number = @ceil($posts_number / $posts_per_page); $day_link = get_day_link($post_year, $post_month, $post_day); $day_uri = str_replace($domain_url, '', $day_link); for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) { $day_pagenum_link = $this->_get_pagenum_link($day_uri, $pagenum); $day_pagenum_uri = str_replace($domain_url, '', $day_pagenum_link); $uris[] = $day_pagenum_uri; } } /** * Monthly archive URLs */ if ($this->_config->get_boolean('pgcache.purge.archive.monthly') && $post) { $post_date = strtotime($post->post_date); $post_year = gmdate('Y', $post_date); $post_month = gmdate('m', $post_date); $posts_per_page = get_option('posts_per_page'); $posts_number = $this->_get_archive_posts_count($post_year, $post_month); $posts_pages_number = @ceil($posts_number / $posts_per_page); $month_link = get_month_link($post_year, $post_month); $month_uri = str_replace($domain_url, '', $month_link); for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) { $month_pagenum_link = $this->_get_pagenum_link($month_uri, $pagenum); $month_pagenum_uri = str_replace($domain_url, '', $month_pagenum_link); $uris[] = $month_pagenum_uri; } } /** * Yearly archive URLs */ if ($this->_config->get_boolean('pgcache.purge.archive.yearly') && $post) { $post_date = strtotime($post->post_date); $post_year = gmdate('Y', $post_date); $posts_per_page = get_option('posts_per_page'); $posts_number = $this->_get_archive_posts_count($post_year); $posts_pages_number = @ceil($posts_number / $posts_per_page); $year_link = get_year_link($post_year); $year_uri = str_replace($domain_url, '', $year_link); for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) { $year_pagenum_link = $this->_get_pagenum_link($year_uri, $pagenum); $year_pagenum_uri = str_replace($domain_url, '', $year_pagenum_link); $uris[] = $year_pagenum_uri; } } /** * Feed URLs */ if ($this->_config->get_boolean('pgcache.purge.feed.blog')) { foreach ($feeds as $feed) { $feed_link = get_feed_link($feed); $feed_uri = str_replace($domain_url, '', $feed_link); $uris[] = $feed_uri; } } if ($this->_config->get_boolean('pgcache.purge.feed.comments')) { foreach ($feeds as $feed) { $post_comments_feed_link = get_post_comments_feed_link($post_id, $feed); $post_comments_feed_uri = str_replace($domain_url, '', $post_comments_feed_link); $uris[] = $post_comments_feed_uri; } } if ($this->_config->get_boolean('pgcache.purge.feed.author') && $post) { foreach ($feeds as $feed) { $author_feed_link = get_author_feed_link($post->post_author, $feed); $author_feed_uri = str_replace($domain_url, '', $author_feed_link); $uris[] = $author_feed_uri; } } if ($this->_config->get_boolean('pgcache.purge.feed.terms')) { foreach ($terms as $term) { foreach ($feeds as $feed) { $term_feed_link = get_term_feed_link($term->term_id, $term->taxonomy, $feed); $term_feed_uri = str_replace($domain_url, '', $term_feed_link); $uris[] = $term_feed_uri; } } } /** * Flush cache */ if (count($uris)) { $cache =& $this->_get_cache(); $mobile_groups = $this->_get_mobile_groups(); $referrer_groups = $this->_get_referrer_groups(); $encryptions = $this->_get_encryptions(); $compressions = $this->_get_compressions(); foreach ($uris as $uri) { foreach ($mobile_groups as $mobile_group) { foreach ($referrer_groups as $referrer_group) { foreach ($encryptions as $encryption) { foreach ($compressions as $compression) { $page_key = $this->_get_page_key($uri, $mobile_group, $referrer_group, $encryption, $compression); $cache->delete($page_key); } } } } } /** * Purge varnish servers */ if ($this->_config->get_boolean('varnish.enabled')) { $varnish =& w3_instance('W3_Varnish'); foreach ($uris as $uri) { $varnish->purge($uri); } } } return true; } return false; }
/** * Rewrite a file relative URI as root relative * * <code> * Minify_CSS_UriRewriter::rewriteRelative( * '../img/hello.gif' * , '/home/user/www/css' // path of CSS file * , '/home/user/www' // doc root * ); * // returns '/img/hello.gif' * * // example where static files are stored in a symlinked directory * Minify_CSS_UriRewriter::rewriteRelative( * 'hello.gif' * , '/var/staticFiles/theme' * , '/home/user/www' * , array('/home/user/www/static' => '/var/staticFiles') * ); * // returns '/static/theme/hello.gif' * </code> * * @param string $uri file relative URI * * @param string $realCurrentDir realpath of the current file's directory. * * @param string $realDocRoot realpath of the site document root. * * @param array $symlinks (default = array()) If the file is stored in * a symlink-ed directory, provide an array of link paths to * real target paths, where the link paths "appear" to be within the document * root. E.g.: * <code> * array('/home/foo/www/not/real/path' => '/real/target/path') // unix * array('C:\\htdocs\\not\\real' => 'D:\\real\\target\\path') // Windows * </code> * * @return string */ private static function _rewriteRelative($uri, $realCurrentDir, $realDocRoot, $symlinks = array()) { if ('/' === $uri[0]) { // root-relative return $uri; } // prepend path with current dir separator (OS-independent) $path = strtr($realCurrentDir, '/', DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . strtr($uri, '/', DIRECTORY_SEPARATOR); self::$debugText .= "file-relative URI : {$uri}\n" . "path prepended : {$path}\n"; // "unresolve" a symlink back to doc root foreach ($symlinks as $link => $target) { if (0 === strpos($path, $target)) { // replace $target with $link $path = $link . substr($path, strlen($target)); self::$debugText .= "symlink unresolved : {$path}\n"; break; } } // strip doc root $path = substr($path, strlen($realDocRoot)); self::$debugText .= "docroot stripped : {$path}\n"; // fix to root-relative URI $uri = strtr($path, '/\\', '//'); // remove /./ and /../ where possible $uri = str_replace('/./', '/', $uri); // inspired by patch from Oleg Cherniy do { $uri = preg_replace('@/[^/]+/\\.\\./@', '/', $uri, 1, $changed); } while ($changed); self::$debugText .= "traversals removed : {$uri}\n\n"; $uri = preg_replace('~^' . w3_preg_quote(w3_get_base_path()) . '~', w3_get_site_path(), $uri); return $uri; }
/** * Exports min files to CDN * * @return array */ function get_files_minify() { $files = array(); if (W3TC_PHP5 && $this->_config->get_boolean('minify.rewrite') && (!$this->_config->get_boolean('minify.auto') || w3_is_cdn_mirror($this->_config->get_string('cdn.engine')))) { require_once W3TC_INC_DIR . '/functions/http.php'; $minify =& w3_instance('W3_Plugin_Minify'); $document_root = w3_get_document_root(); $site_root = w3_get_site_root(); $minify_root = w3_path(W3TC_CACHE_FILE_MINIFY_DIR); $minify_path = ltrim(str_replace($site_root, rtrim(w3_get_site_path(), '/'), $minify_root), '/'); $urls = $minify->get_urls(); if ($this->_config->get_string('minify.engine') == 'file') { foreach ($urls as $url) { w3_http_get($url); } $files = $this->search_files($minify_root, $minify_path, '*.css;*.js'); } else { foreach ($urls as $url) { $file = w3_normalize_file_minify($url); $file = w3_translate_file($file); if (!w3_is_url($file)) { $file = $document_root . '/' . $file; $file = ltrim(str_replace($minify_root, '', $file), '/'); $dir = dirname($file); if ($dir) { w3_mkdir($dir, 0777, $minify_root); } if (w3_download($url, $minify_root . '/' . $file) !== false) { $files[] = $minify_path . '/' . $file; } } } } } return $files; }
/** * Remove script tags from the source * * @param string $content * @param array $files * @return void */ function remove_scripts(&$content, $files) { $regexps = array(); $home_url_regexp = w3_get_home_url_regexp(); $path = ''; if (w3_is_network() && !w3_is_subdomain_install()) { $path = ltrim(w3_get_home_path(), '/'); } foreach ($files as $file) { if ($path && strpos($file, $path) === 0) { $file = substr($file, strlen($path)); } $this->replaced_scripts[] = $file; if (w3_is_url($file) && !preg_match('~' . $home_url_regexp . '~i', $file)) { // external JS files $regexps[] = w3_preg_quote($file); } else { // local JS files $file = ltrim($file, '/'); if (ltrim(w3_get_site_path(), '/') && strpos($file, ltrim(w3_get_site_path(), '/')) === 0) { $file = str_replace(ltrim(w3_get_site_path(), '/'), '', $file); } $file = ltrim(preg_replace('~' . $home_url_regexp . '~i', '', $file), '/\\'); $regexps[] = '(' . $home_url_regexp . ')?/?' . w3_preg_quote($file); } } foreach ($regexps as $regexp) { $content = preg_replace('~<script\\s+[^<>]*src=["\']?' . $regexp . '["\']?[^<>]*>\\s*</script>~Uis', '', $content); } }
private function _replace_folder_placeholders($file) { static $content_dir, $plugin_dir, $upload_dir; if (empty($content_dir)) { $content_dir = str_replace(w3_get_document_root(), '', WP_CONTENT_DIR); $content_dir = substr($content_dir, strlen(w3_get_site_path())); $content_dir = trim($content_dir, '/'); if (defined('WP_PLUGIN_DIR')) { $plugin_dir = str_replace(w3_get_document_root(), '', WP_PLUGIN_DIR); $plugin_dir = trim($plugin_dir, '/'); } else { $plugin_dir = str_replace(w3_get_document_root(), '', WP_CONTENT_DIR . '/plugins'); $plugin_dir = trim($plugin_dir, '/'); } $upload_dir = wp_upload_dir(); $upload_dir = str_replace(w3_get_document_root(), '', $upload_dir['basedir']); $upload_dir = trim($upload_dir, '/'); } $file = str_replace('{wp_content_dir}', $content_dir, $file); $file = str_replace('{plugins_dir}', $plugin_dir, $file); $file = str_replace('{uploads_dir}', $upload_dir, $file); return $file; }