PluginProbe
ezCache / 1.2
ezCache v1.2
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 1.2, at includes/FileOptimizer/BaseFileOptimizer.php

113 lines 2.9 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\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 }
113