/** * Remove script tags from the source * * @param string $content * @param array $files * @return void */ function remove_scripts(&$content, $files) { $regexps = array(); $home_url_regexp = Util_Environment::home_url_regexp(); $path = ''; if (Util_Environment::is_wpmu() && !Util_Environment::is_wpmu_subdomain()) { $path = ltrim(Util_Environment::home_url_uri(), '/'); } foreach ($files as $file) { if ($path && strpos($file, $path) === 0) { $file = substr($file, strlen($path)); } $this->replaced_scripts[] = $file; if (Util_Environment::is_url($file) && !preg_match('~' . $home_url_regexp . '~i', $file)) { // external JS files $regexps[] = Util_Environment::preg_quote($file); } else { // local JS files $file = ltrim($file, '/'); if (home_url() == site_url() && ltrim(Util_Environment::site_url_uri(), '/') && strpos($file, ltrim(Util_Environment::site_url_uri(), '/')) === 0) { $file = str_replace(ltrim(Util_Environment::site_url_uri(), '/'), '', $file); } $file = ltrim(preg_replace('~' . $home_url_regexp . '~i', '', $file), '/\\'); $regexps[] = '(' . $home_url_regexp . ')?/?' . Util_Environment::preg_quote($file); } } foreach ($regexps as $regexp) { $content = preg_replace('~<script\\s+[^<>]*src=["\']?' . $regexp . '["\']?[^<>]*>\\s*</script>~Uis', '', $content); } }
/** * Normalizes file name * * Relative to site root! * * @param string $file * @return string */ public static function normalize_file($file) { if (Util_Environment::is_url($file)) { if (strstr($file, '?') === false) { $home_url_regexp = '~' . Util_Environment::home_url_regexp() . '~i'; $file = preg_replace($home_url_regexp, '', $file); } } if (!Util_Environment::is_url($file)) { $file = Util_Environment::normalize_path($file); $file = str_replace(Util_Environment::site_root(), '', $file); $file = ltrim($file, '/'); } return $file; }