/**
  * URL file filter
  *
  * @param string  $file
  * @return bool
  */
 public function is_file_for_minification($file)
 {
     static $external;
     if (!isset($external)) {
         $external = $this->config->get_array('minify.cache.files');
     }
     foreach ($external as $ext) {
         if (preg_match('#' . Util_Environment::get_url_regexp($ext) . '#', $file)) {
             if ($this->debug) {
                 Minify_Core::log('is_file_for_minification: whilelisted ' . $file);
             }
             return true;
         }
     }
     $file_normalized = Util_Environment::remove_query_all($file);
     $ext = strrchr($file_normalized, '.');
     if ($ext != '.js' && $ext != '.css') {
         if ($this->debug) {
             Minify_Core::log('is_file_for_minification: unknown extension ' . $ext . ' for ' . $file);
         }
         return false;
     }
     if (Util_Environment::is_url($file_normalized)) {
         if ($this->debug) {
             Minify_Core::log('is_file_for_minification: its url ' . $file);
         }
         return false;
     }
     $path = Util_Environment::document_root() . '/' . $file;
     if (!file_exists($path)) {
         if ($this->debug) {
             Minify_Core::log('is_file_for_minification: file doesnt exists ' . $path);
         }
         return false;
     }
     if ($this->debug) {
         Minify_Core::log('is_file_for_minification: true for file ' . $file . ' path ' . $path);
     }
     return true;
 }
Ejemplo n.º 2
0
 /**
  * Normalizes file name for minify
  * Relative to document root!
  *
  * @param string  $file
  * @return string
  */
 public static function url_to_docroot_filename($url)
 {
     $data = array('home_url' => get_home_url(), 'url' => $url);
     $data = apply_filters('w3tc_url_to_docroot_filename', $data);
     $home_url = $data['home_url'];
     $normalized_url = $data['url'];
     $normalized_url = Util_Environment::remove_query_all($normalized_url);
     // cut protocol
     $normalized_url = preg_replace('~^http(s)?://~', '//', $normalized_url);
     $home_url = preg_replace('~^http(s)?://~', '//', $home_url);
     if (substr($normalized_url, 0, strlen($home_url)) != $home_url) {
         // not a home url, return unchanged since cant be
         // converted to filename
         return $url;
     } else {
         $path_relative_to_home = str_replace($home_url, '', $normalized_url);
         $home = set_url_scheme(get_option('home'), 'http');
         $siteurl = set_url_scheme(get_option('siteurl'), 'http');
         $home_path = rtrim(Util_Environment::site_path(), '/');
         // adjust home_path if site is not is home
         if (!empty($home) && 0 !== strcasecmp($home, $siteurl)) {
             // $siteurl - $home
             $wp_path_rel_to_home = rtrim(str_ireplace($home, '', $siteurl), '/');
             if (substr($home_path, -strlen($wp_path_rel_to_home)) == $wp_path_rel_to_home) {
                 $home_path = substr($home_path, 0, -strlen($wp_path_rel_to_home));
             }
         }
         // common encoded characters
         $path_relative_to_home = str_replace('%20', ' ', $path_relative_to_home);
         $full_filename = $home_path . DIRECTORY_SEPARATOR . trim($path_relative_to_home, DIRECTORY_SEPARATOR);
         $docroot = Util_Environment::document_root();
         if (substr($full_filename, 0, strlen($docroot)) == $docroot) {
             $docroot_filename = substr($full_filename, strlen($docroot));
         } else {
             $docroot_filename = $path_relative_to_home;
         }
     }
     // sometimes urls (coming from other plugins/themes)
     // contain multiple "/" like "my-folder//myfile.js" which
     // fails to recognize by filesystem, while url is accessible
     $docroot_filename = str_replace('//', DIRECTORY_SEPARATOR, $docroot_filename);
     return ltrim($docroot_filename, DIRECTORY_SEPARATOR);
 }