| @@ -1,112 +1,122 @@ | ||
| 1 | -<?php | |
| 2 | -namespace Upress\EzCache\FileOptimizer; | |
| 3 | - | |
| 4 | -use Upress\EzCache\Cache; | |
| 5 | -use Upress\EzCache\ThirdParty\Minify_CSS_UriRewriter; | |
| 6 | - | |
| 7 | -abstract class BaseFileOptimizer { | |
| 8 | - static $FILE_TYPE = ''; | |
| 9 | - | |
| 10 | - function optimize( $html ) { | |
| 11 | - return $html; | |
| 12 | - } | |
| 13 | - | |
| 14 | - /** | |
| 15 | - * Check if the files is on a different host | |
| 16 | - * | |
| 17 | - * @param string $url | |
| 18 | - * | |
| 19 | - * @return bool | |
| 20 | - */ | |
| 21 | - function is_external_file( $url ) { | |
| 22 | - $file = wp_parse_url( $url ); | |
| 23 | - $wp_content = wp_parse_url( WP_CONTENT_URL ); | |
| 24 | - | |
| 25 | - // URL has domain and domain is not part of the internal domains. | |
| 26 | - if ( isset( $file['host'] ) && ! empty( $file['host'] ) && $file['host'] != $wp_content['host'] ) { | |
| 27 | - return true; | |
| 28 | - } | |
| 29 | - | |
| 30 | - // URL has no domain and doesn't contain the WP_CONTENT path or wp-includes. | |
| 31 | - if ( ! isset( $file['host'] ) && ! preg_match( '#(' . $wp_content['path'] . '|wp-includes)#', $file['path'] ) ) { | |
| 32 | - return true; | |
| 33 | - } | |
| 34 | - | |
| 35 | - return false; | |
| 36 | - } | |
| 37 | - | |
| 38 | - /** | |
| 39 | - * Get a path to the file by URL ignoring the query string | |
| 40 | - * | |
| 41 | - * @param string $url | |
| 42 | - * | |
| 43 | - * @return bool|string | |
| 44 | - */ | |
| 45 | - function get_file_path( $url ) { | |
| 46 | - return Cache::url_to_path( strtok( $url, '?' ) ); | |
| 47 | - } | |
| 48 | - | |
| 49 | - /** | |
| 50 | - * Determines if it is a file excluded from minification | |
| 51 | - * | |
| 52 | - * @param array $tag Tag corresponding to a JS file. | |
| 53 | - * | |
| 54 | - * @return bool True if it is a file excluded, false otherwise | |
| 55 | - */ | |
| 56 | - function is_minify_excluded_file( $tag ) { | |
| 57 | - // File should not be minified. | |
| 58 | - if ( false !== strpos( $tag[0], 'data-minify=' ) || false !== strpos( $tag[0], 'data-no-minify=' ) ) { | |
| 59 | - return true; | |
| 60 | - } | |
| 61 | - | |
| 62 | - $file_path = $this->get_url_component( $tag[2], PHP_URL_PATH ); | |
| 63 | - | |
| 64 | - if ( static::$FILE_TYPE && pathinfo( $file_path, PATHINFO_EXTENSION ) !== static::$FILE_TYPE ) { | |
| 65 | - return true; | |
| 66 | - } | |
| 67 | - | |
| 68 | - $excluded_files = apply_filters( 'ezcache_excluded_minify_files', [] ); | |
| 69 | - if ( ! empty( $excluded_files ) ) { | |
| 70 | - if ( preg_match( '/^(' . preg_quote( $excluded_files, '/' ) . ')$/', $file_path ) ) { | |
| 71 | - return true; | |
| 72 | - } | |
| 73 | - } | |
| 74 | - | |
| 75 | - return false; | |
| 76 | - } | |
| 77 | - | |
| 78 | - /** | |
| 79 | - * Finds nodes matching the pattern in the HTML | |
| 80 | - * | |
| 81 | - * @param string $pattern Pattern to match. | |
| 82 | - * @param string $html HTML content. | |
| 83 | - * | |
| 84 | - * @return bool|array | |
| 85 | - */ | |
| 86 | - protected function find( $pattern, $html ) { | |
| 87 | - $html_nocomments = preg_replace( '/<!--(.*)-->/Uis', '', $html ); | |
| 88 | - preg_match_all( '/' . $pattern . '/Umsi', $html_nocomments, $matches, PREG_SET_ORDER ); | |
| 89 | - | |
| 90 | - if ( empty( $matches ) ) { | |
| 91 | - return false; | |
| 92 | - } | |
| 93 | - | |
| 94 | - return $matches; | |
| 95 | - } | |
| 96 | - | |
| 97 | - /** | |
| 98 | - * Rewrites the paths inside the CSS file content | |
| 99 | - * | |
| 100 | - * @param string $file File path. | |
| 101 | - * @param string $content File content. | |
| 102 | - * | |
| 103 | - * @return string | |
| 104 | - */ | |
| 105 | - public function rewrite_paths( $file, $content ) { | |
| 106 | - return Minify_CSS_UriRewriter::rewrite( $content, dirname( $file ) ); | |
| 107 | - } | |
| 108 | - | |
| 109 | - public function get_url_component( $url, $component = -1 ) { | |
| 110 | - return _get_component_from_parsed_url_array( wp_parse_url( $url ), $component ); | |
| 111 | - } | |
| 112 | -} | |
| 1 | +<?php | |
| 2 | +namespace Upress\EzCache\FileOptimizer; | |
| 3 | + | |
| 4 | +use Upress\EzCache\Cache; | |
| 5 | +use Upress\EzCache\Settings; | |
| 6 | +use Upress\EzCache\ThirdParty\Minify_CSS_UriRewriter; | |
| 7 | + | |
| 8 | +abstract class BaseFileOptimizer { | |
| 9 | + static $FILE_TYPE = ''; | |
| 10 | + | |
| 11 | + function optimize( $html ) { | |
| 12 | + return $html; | |
| 13 | + } | |
| 14 | + | |
| 15 | + /** | |
| 16 | + * Check if the files is on a different host | |
| 17 | + * | |
| 18 | + * @param string $url | |
| 19 | + * | |
| 20 | + * @return bool | |
| 21 | + */ | |
| 22 | + function is_external_file( $url ) { | |
| 23 | + $file = wp_parse_url( $url ); | |
| 24 | + $site = wp_parse_url( site_url() ); | |
| 25 | + | |
| 26 | + // URL has domain and domain is not part of the internal domains. | |
| 27 | + if ( isset( $file['host'] ) && ! empty( $file['host'] ) && $file['host'] != $site['host'] ) { | |
| 28 | + return true; | |
| 29 | + } | |
| 30 | + | |
| 31 | + if ( ! isset( $site['path'] ) ) { | |
| 32 | + $site['path'] = ''; | |
| 33 | + } | |
| 34 | + | |
| 35 | + // URL has no domain and doesn't contain the WP_CONTENT path or wp-includes. | |
| 36 | + if ( ! isset( $file['host'] ) && ! preg_match( '#(' . $site['path'] . '|wp-includes)#', $file['path'] ) ) { | |
| 37 | + return true; | |
| 38 | + } | |
| 39 | + | |
| 40 | + return false; | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Get a path to the file by URL ignoring the query string | |
| 45 | + * | |
| 46 | + * @param string $url | |
| 47 | + * | |
| 48 | + * @return bool|string | |
| 49 | + */ | |
| 50 | + function get_file_path( $url ) { | |
| 51 | + return Cache::url_to_path( strtok( $url, '?' ) ); | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Determines if it is a file excluded from minification | |
| 56 | + * | |
| 57 | + * @param array $tag Tag corresponding to a JS file. | |
| 58 | + * | |
| 59 | + * @return bool True if it is a file excluded, false otherwise | |
| 60 | + */ | |
| 61 | + function is_minify_excluded_file( $tag ) { | |
| 62 | + // File should not be minified. | |
| 63 | + if ( false !== strpos( $tag[0], 'data-minify=' ) || false !== strpos( $tag[0], 'data-no-minify=' ) ) { | |
| 64 | + return true; | |
| 65 | + } | |
| 66 | + | |
| 67 | + $file_path = $this->get_url_component( $tag[2], PHP_URL_PATH ); | |
| 68 | + | |
| 69 | + if ( static::$FILE_TYPE && pathinfo( $file_path, PATHINFO_EXTENSION ) !== static::$FILE_TYPE ) { | |
| 70 | + return true; | |
| 71 | + } | |
| 72 | + | |
| 73 | + $excluded_files = Settings::get_settings()->excluded_minify_files; | |
| 74 | + $excluded_files = preg_split( "/\\r\\n|\\r|\\n/u", trim( $excluded_files ), -1, PREG_SPLIT_NO_EMPTY ); | |
| 75 | + $excluded_files = array_filter( $excluded_files ); | |
| 76 | + $excluded_files = array_merge( $excluded_files, ['elementor/css/post-'] ); | |
| 77 | + $excluded_files = array_unique( $excluded_files ); | |
| 78 | + $excluded_files = apply_filters( 'ezcache_excluded_minify_files', $excluded_files ); | |
| 79 | + foreach ( $excluded_files as $file ) { | |
| 80 | + if ( preg_match( '/' . preg_quote( $file, '/' ) . '/', $file_path ) ) { | |
| 81 | + return true; | |
| 82 | + } | |
| 83 | + } | |
| 84 | + | |
| 85 | + return false; | |
| 86 | + } | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * Finds nodes matching the pattern in the HTML | |
| 90 | + * | |
| 91 | + * @param string $pattern Pattern to match. | |
| 92 | + * @param string $html HTML content. | |
| 93 | + * | |
| 94 | + * @return bool|array | |
| 95 | + */ | |
| 96 | + protected function find( $pattern, $html ) { | |
| 97 | + $html_nocomments = preg_replace( '/<!--(.*)-->/uUis', '', $html ); | |
| 98 | + preg_match_all( '/' . $pattern . '/uUmsi', $html_nocomments, $matches, PREG_SET_ORDER ); | |
| 99 | + | |
| 100 | + if ( empty( $matches ) ) { | |
| 101 | + return false; | |
| 102 | + } | |
| 103 | + | |
| 104 | + return $matches; | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * Rewrites the paths inside the CSS file content | |
| 109 | + * | |
| 110 | + * @param string $file File path. | |
| 111 | + * @param string $content File content. | |
| 112 | + * | |
| 113 | + * @return string | |
| 114 | + */ | |
| 115 | + public function rewrite_paths( $file, $content ) { | |
| 116 | + return Minify_CSS_UriRewriter::rewrite( $content, dirname( $file ) ); | |
| 117 | + } | |
| 118 | + | |
| 119 | + public function get_url_component( $url, $component = -1 ) { | |
| 120 | + return _get_component_from_parsed_url_array( wp_parse_url( $url ), $component ); | |
| 121 | + } | |
| 122 | +} | |