PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 2.2.1
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v2.2.1
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
powered-cache / includes / classes / Optimizer / CSS.php

CSS.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 2.2.1, at includes/classes/Optimizer/CSS.php

294 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CSS contact
4 *
5 * @package PoweredCache\Optimizer
6 */
7
8 //phpcs:disable Squiz.Commenting.VariableComment.Missing
9 //phpcs:disable Generic.Commenting.DocComment.MissingShort
10 //phpcs:disable Squiz.Commenting.FunctionComment.MissingParamName
11 //phpcs:disable Squiz.Commenting.FunctionComment.MissingParamTag
12
13 namespace PoweredCache\Optimizer;
14
15 use PoweredCache\CDN;
16 use \WP_Styles as WP_Styles;
17
18 /**
19 * Class CSS
20 */
21 class CSS extends WP_Styles {
22 private $old_styles;
23 public $allow_gzip_compression;
24 public $do_minify;
25 public $enable_cdn;
26
27 /**
28 * CSS constructor.
29 *
30 * @param object $styles WP_Styles
31 */
32 public function __construct( $styles ) {
33 if ( empty( $styles ) || ! ( $styles instanceof WP_Styles ) ) {
34 $this->old_styles = new WP_Styles();
35 } else {
36 $this->old_styles = $styles;
37 }
38
39 // Unset all the object properties except our private copy of the styles object.
40 // We have to unset everything so that the overload methods talk to $this->old_styles->whatever
41 // instead of $this->whatever.
42 foreach ( array_keys( get_object_vars( $this ) ) as $key ) {
43 if ( 'old_styles' === $key ) {
44 continue;
45 }
46 unset( $this->$key );
47 }
48 }
49
50 /**
51 * Prep items for concat
52 *
53 * @param bool $handles CSS handler
54 * @param bool $group CSS group
55 *
56 * @return string[]
57 */
58 public function do_items( $handles = false, $group = false ) {
59 $abspath = wp_normalize_path( ABSPATH );
60 $handles = false === $handles ? $this->queue : (array) $handles;
61 $stylesheets = array();
62 /**
63 * Filters site url
64 *
65 * @hook powered_cache_fo_site_url
66 *
67 * @param {string} $base_url Site URL.
68 *
69 * @return {string} New value.
70 * @since 2.0
71 */
72 $siteurl = apply_filters( 'powered_cache_fo_site_url', $this->base_url );
73 $this->all_deps( $handles );
74
75 $stylesheet_group_index = 0;
76 foreach ( $this->to_do as $key => $handle ) {
77 $obj = $this->registered[ $handle ];
78
79 $obj->src = apply_filters( 'style_loader_src', $obj->src, $obj->handle );
80
81 // Core is kind of broken and returns "true" for src of "colors" handle
82 // http://core.trac.wordpress.org/attachment/ticket/16827/colors-hacked-fixed.diff
83 // http://core.trac.wordpress.org/ticket/20729
84 $css_url = $obj->src;
85 if ( 'colors' === $obj->handle && true === $css_url ) {
86 $css_url = wp_style_loader_src( $css_url, $obj->handle );
87 }
88
89 $css_url_parsed = wp_parse_url( $obj->src );
90 $extra = $obj->extra;
91
92 // Don't concat by default
93 $do_concat = false;
94
95 // Only try to concat static css files
96 if ( false !== strpos( $css_url_parsed['path'], '.css' ) ) {
97 $do_concat = true;
98 }
99
100 // Don't try to concat styles which are loaded conditionally (like IE stuff)
101 if ( isset( $extra['conditional'] ) ) {
102 $do_concat = false;
103 }
104
105 // Don't concat rtl stuff for now until concat supports it correctly
106 if ( 'rtl' === $this->text_direction && ! empty( $extra['rtl'] ) ) {
107 $do_concat = false;
108 }
109
110 // Don't try to concat externally hosted scripts
111 $is_internal_url = Helper::is_internal_url( $css_url, $siteurl );
112 if ( ! $is_internal_url ) {
113 $do_concat = false;
114 }
115
116 // Concat and canonicalize the paths only for
117 // existing scripts that aren't outside $abspath
118 $css_realpath = Helper::realpath( $css_url, $siteurl );
119 if ( ! $css_realpath || 0 !== strpos( $css_realpath, $abspath ) ) {
120 $do_concat = false;
121 } else {
122 $css_url_parsed['path'] = substr( $css_realpath, strlen( $abspath ) - 1 );
123 }
124
125 if ( Helper::is_excluded_css( $css_url ) ) {
126 $do_concat = false;
127 }
128
129 /**
130 * Allow plugins to disable concatenation of certain stylesheets.
131 *
132 * @hook powered_cache_fo_css_do_concat
133 *
134 * @param {boolean} $do_concat Contact status.
135 * @param {string} $handle Handle of registered CSS item.
136 *
137 * @return {boolean} New value.
138 * @since 2.0
139 */
140 $do_concat = apply_filters( 'powered_cache_fo_css_do_concat', $do_concat, $handle );
141
142 if ( true === $do_concat ) {
143 $media = $obj->args;
144 if ( empty( $media ) ) {
145 $media = 'all';
146 }
147 if ( ! isset( $stylesheets[ $stylesheet_group_index ] ) || ( isset( $stylesheets[ $stylesheet_group_index ] ) && ! is_array( $stylesheets[ $stylesheet_group_index ] ) ) ) {
148 $stylesheets[ $stylesheet_group_index ] = array();
149 }
150
151 $stylesheets[ $stylesheet_group_index ][ $media ][ $handle ] = $css_url_parsed['path'];
152 $this->done[] = $handle;
153 } else {
154 $stylesheet_group_index ++;
155 $stylesheets[ $stylesheet_group_index ]['noconcat'][] = $handle;
156 $stylesheet_group_index ++;
157 }
158 unset( $this->to_do[ $key ] );
159 }
160
161 foreach ( $stylesheets as $idx => $stylesheets_group ) {
162 foreach ( $stylesheets_group as $media => $css ) {
163 if ( 'noconcat' === $media ) {
164
165 foreach ( $css as $handle ) {
166 if ( $this->do_item( $handle, $group ) ) {
167 $this->done[] = $handle;
168 }
169 }
170 continue;
171 } elseif ( count( $css ) > 1 ) {
172 $paths = array_map(
173 function ( $url ) use ( $abspath ) {
174 return $abspath . $url;
175 },
176 $css
177 );
178 $mtime = max( array_map( 'filemtime', $paths ) );
179 $path_str = implode( ',', $css ) . "?m={$mtime}";
180
181 if ( $this->allow_gzip_compression ) {
182 $path_64 = base64_encode( gzcompress( $path_str ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
183 if ( strlen( $path_str ) > ( strlen( $path_64 ) + 1 ) ) {
184 $path_str = '-' . $path_64;
185 }
186 }
187
188 $href = Helper::get_optimized_url( $path_str, $this->do_minify );
189 } else {
190 if ( $this->do_minify ) {
191 $href = Helper::get_optimized_url( current( $css ), $this->do_minify );
192 } else {
193 $href = $this->cache_bust_mtime( $siteurl . current( $css ), $siteurl );
194 }
195 }
196
197 $handles = array_keys( $css );
198 $css_tag = "<link rel='stylesheet' id='$media-css-$idx' href='$href' type='text/css' media='$media' />\n"; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
199 /**
200 * Filters combined CSS tag.
201 *
202 * @hook powered_cache_fo_style_loader_tag
203 *
204 * @param {string} $css_tag CSS tag.
205 * @param {array} $handles The list of CSS handles.
206 * @param {string} $href CSS URL.
207 * @param {string} $media Media type.
208 *
209 * @return {string} New value.
210 * @since 2.0
211 */
212 echo apply_filters( 'powered_cache_fo_style_loader_tag', $css_tag, $handles, $href, $media ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
213 array_map( array( $this, 'print_inline_style' ), array_keys( $css ) );
214 }
215 }
216
217 return $this->done;
218 }
219
220 /**
221 * Bust cache
222 *
223 * @param string $url CSS URL
224 * @param string $siteurl Site URL
225 *
226 * @return string
227 */
228 public function cache_bust_mtime( $url, $siteurl ) {
229 if ( strpos( $url, '?m=' ) ) {
230 return $url;
231 }
232
233 $parts = wp_parse_url( $url );
234 if ( ! isset( $parts['path'] ) || empty( $parts['path'] ) ) {
235 return $url;
236 }
237
238 $file = Helper::realpath( $url, $siteurl );
239
240 $mtime = false;
241 if ( file_exists( $file ) ) {
242 $mtime = filemtime( $file );
243 }
244
245 if ( ! $mtime ) {
246 return $url;
247 }
248
249 if ( false === strpos( $url, '?' ) ) {
250 $q = '';
251 } else {
252 list( $url, $q ) = explode( '?', $url, 2 );
253 if ( strlen( $q ) ) {
254 $q = '&amp;' . $q;
255 }
256 }
257
258 return "$url?m={$mtime}g{$q}";
259 }
260
261 /**
262 * @param $key
263 *
264 * @return bool
265 */
266 public function __isset( $key ) {
267 return isset( $this->old_styles->$key );
268 }
269
270 /**
271 * @param $key
272 */
273 public function __unset( $key ) {
274 unset( $this->old_styles->$key );
275 }
276
277 /**
278 * @param $key
279 *
280 * @return mixed
281 */
282 public function &__get( $key ) {
283 return $this->old_styles->$key;
284 }
285
286 /**
287 * @param $key
288 * @param $value
289 */
290 public function __set( $key, $value ) {
291 $this->old_styles->$key = $value;
292 }
293 }
294