PluginProbe
ezCache / trunk
ezCache vtrunk
2.6.5 2.6.4 2.6.2 2.6.3 2.6.1 2.6.0 2.5.6 2.5.5 2.5.4 2.5.3 2.5.2 2.5.1 2.5 2.2.1 2.2.2 trunk 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.3 1.3.1 1.3.10 1.3.11 All 49 releases
ezcache / includes / FileOptimizer / BaseFileOptimizer.php

BaseFileOptimizer.php in ezCache trunk, at includes/FileOptimizer/BaseFileOptimizer.php

123 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
123