Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 99
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Concatenate_CSS
0.00% covered (danger)
0.00%
0 / 99
0.00% covered (danger)
0.00%
0 / 7
3080
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
 do_items
0.00% covered (danger)
0.00%
0 / 77
0.00% covered (danger)
0.00%
0 / 1
1892
 print_style_tag
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 __isset
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __unset
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __get
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __set
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Automattic\Jetpack_Boost\Lib\Minify;
4
5use WP_Styles;
6
7// Disable complaints about enqueuing stylesheets, as this class alters the way enqueuing them works.
8// phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
9
10/**
11 * Replacement for, and subclass of WP_Styles - used to control the way that styles are enqueued and output.
12 */
13class Concatenate_CSS extends WP_Styles {
14    private $dependency_path_mapping;
15    private $old_styles;
16
17    public $allow_gzip_compression;
18
19    public function __construct( $styles ) {
20        if ( empty( $styles ) || ! ( $styles instanceof WP_Styles ) ) {
21            $this->old_styles = new WP_Styles();
22        } else {
23            $this->old_styles = $styles;
24        }
25
26        // Unset all the object properties except our private copy of the styles object.
27        // We have to unset everything so that the overload methods talk to $this->old_styles->whatever
28        // instead of $this->whatever.
29        foreach ( array_keys( get_object_vars( $this ) ) as $key ) {
30            if ( 'old_styles' === $key ) {
31                continue;
32            }
33            unset( $this->$key );
34        }
35
36        $this->dependency_path_mapping = new Dependency_Path_Mapping(
37            /**
38             * Filter the URL of the site the plugin will be concatenating CSS or JS on
39             *
40             * @param string $url URL of the page with CSS or JS to concatonate.
41             *
42             * @since   1.0.0
43             */
44            apply_filters( 'page_optimize_site_url', $this->base_url )
45        );
46    }
47
48    public function do_items( $handles = false, $group = false ) {
49        $handles     = false === $handles ? $this->queue : (array) $handles;
50        $stylesheets = array();
51        /**
52         * Filter the URL of the site the plugin will be concatenating CSS or JS on
53         *
54         * @param string $url URL of the page with CSS or JS to concatonate.
55         *
56         * @since   1.0.0
57         */
58        $siteurl = apply_filters( 'page_optimize_site_url', $this->base_url );
59
60        $this->all_deps( $handles );
61
62        $stylesheet_group_index = 0;
63        // Merge CSS into a single file
64        $concat_group = 'concat';
65        // Concat group on top (first array element gets processed earlier)
66        $stylesheets[ $concat_group ] = array();
67
68        foreach ( $this->to_do as $key => $handle ) {
69            $obj = $this->registered[ $handle ];
70            /**
71             * Filter the style source URL
72             *
73             * @param string $url URL of the page with CSS or JS to concatonate.
74             * @param string $handle handle to CSS file.
75             *
76             * @since   1.0.0
77             */
78            $obj->src = apply_filters( 'style_loader_src', $obj->src, $obj->handle );
79
80            // Core is kind of broken and returns "true" for src of "colors" handle
81            // http://core.trac.wordpress.org/attachment/ticket/16827/colors-hacked-fixed.diff
82            // http://core.trac.wordpress.org/ticket/20729
83            $css_url = $obj->src;
84            if ( 'colors' === $obj->handle && true === $css_url ) {
85                $css_url = wp_style_loader_src( $css_url, $obj->handle );
86            }
87
88            $css_url        = jetpack_boost_enqueued_to_absolute_url( $css_url );
89            $css_url_parsed = wp_parse_url( $css_url );
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 ( str_contains( $css_url_parsed['path'], '.css' ) ) {
97                $do_concat = true;
98            } elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
99                    printf( "\n<!-- No Concat CSS %s => Maybe Not Static File %s -->\n", esc_html( $handle ), esc_html( $obj->src ) );
100            }
101
102            // Don't try to concat styles which are loaded conditionally (like IE stuff)
103            if ( isset( $extra['conditional'] ) ) {
104                if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
105                    printf( "\n<!-- No Concat CSS %s => Has Conditional -->\n", esc_html( $handle ) );
106                }
107                $do_concat = false;
108            }
109
110            // Don't concat rtl stuff for now until concat supports it correctly
111            if ( $do_concat && 'rtl' === $this->text_direction && ! empty( $extra['rtl'] ) ) {
112                if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
113                    printf( "\n<!-- No Concat CSS %s => Is RTL -->\n", esc_html( $handle ) );
114                }
115                $do_concat = false;
116            }
117
118            // Don't try to concat externally hosted scripts
119            $is_internal_uri = $this->dependency_path_mapping->is_internal_uri( $css_url );
120            if ( $do_concat && ! $is_internal_uri ) {
121                if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
122                    printf( "\n<!-- No Concat CSS %s => External URL: %s -->\n", esc_html( $handle ), esc_url( $css_url ) );
123                }
124                $do_concat = false;
125            }
126
127            if ( $do_concat ) {
128                // Resolve paths and concat styles that exist in the filesystem
129                $css_realpath = $this->dependency_path_mapping->dependency_src_to_fs_path( $css_url );
130                if ( false === $css_realpath ) {
131                    if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
132                        printf( "\n<!-- No Concat CSS %s => Invalid Path %s -->\n", esc_html( $handle ), esc_html( $css_realpath ) );
133                    }
134                    $do_concat = false;
135                }
136            }
137
138            // Skip concating CSS from exclusion list
139            $exclude_list = jetpack_boost_page_optimize_css_exclude_list();
140            foreach ( $exclude_list as $exclude ) {
141                if ( $do_concat && $handle === $exclude ) {
142                    $do_concat = false;
143                    if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
144                        printf( "\n<!-- No Concat CSS %s => Excluded option -->\n", esc_html( $handle ) );
145                    }
146                }
147            }
148
149            /**
150             * Filter that allows plugins to disable concatenation of certain stylesheets.
151             *
152             * @param bool $do_concat if true, then perform concatenation
153             * @param string $handle handle to CSS file
154             *
155             * @since   1.0.0
156             */
157            if ( $do_concat && ! apply_filters( 'css_do_concat', $do_concat, $handle ) ) {
158                if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
159                    printf( "\n<!-- No Concat CSS %s => Filtered `false` -->\n", esc_html( $handle ) );
160                }
161            }
162            /**
163             * Filter that allows plugins to disable concatenation of certain stylesheets.
164             *
165             * @param bool $do_concat if true, then perform concatenation
166             * @param string $handle handle to CSS file
167             *
168             * @since   1.0.0
169             */
170            $do_concat = apply_filters( 'css_do_concat', $do_concat, $handle );
171
172            if ( true === $do_concat ) {
173                $media = $obj->args;
174                if ( empty( $media ) ) {
175                    $media = 'all';
176                }
177
178                $stylesheets[ $concat_group ][ $media ][ $handle ] = $css_url_parsed['path'];
179                $this->done[]                                      = $handle;
180            } else {
181                ++$stylesheet_group_index;
182                $stylesheets[ $stylesheet_group_index ]['noconcat'][] = $handle;
183                ++$stylesheet_group_index;
184            }
185            unset( $this->to_do[ $key ] );
186        }
187
188        foreach ( $stylesheets as $_idx => $stylesheets_group ) {
189            foreach ( $stylesheets_group as $media => $css ) {
190                $href = '';
191                if ( 'noconcat' === $media ) {
192                    foreach ( $css as $handle ) {
193                        if ( $this->do_item( $handle, $group ) ) {
194                            $this->done[] = $handle;
195                        }
196                    }
197                    continue;
198                } elseif ( count( $css ) > 0 ) {
199                    // Split the CSS into groups of max files, we are also chunking the handles to match the CSS groups and depending on order to be maintained.
200                    $css_groups = array_chunk( $css, jetpack_boost_minify_concat_max_files(), true );
201
202                    foreach ( $css_groups as $css_group ) {
203                        $file_name = jetpack_boost_page_optimize_generate_concat_path( $css_group, $this->dependency_path_mapping );
204
205                        if ( get_site_option( 'jetpack_boost_static_minification' ) ) {
206                            $href = jetpack_boost_get_minify_url( $file_name . '.min.css' );
207                        } else {
208                            $href = $siteurl . jetpack_boost_get_static_prefix() . '??' . $file_name;
209                        }
210
211                        $this->print_style_tag( $href, array_keys( $css_group ), $media );
212                    }
213                }
214            }
215        }
216
217        return $this->done;
218    }
219
220    private function print_style_tag( $href, $handles, $media ) {
221        $css_id = sanitize_title_with_dashes( $media ) . '-css-' . md5( $href );
222        if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
223            $style_tag = "<link data-handles='" . esc_attr( implode( ',', $handles ) ) . "' rel='stylesheet' id='$css_id' href='$href' type='text/css' media='$media' />";
224        } else {
225            $style_tag = "<link rel='stylesheet' id='$css_id' href='$href' type='text/css' media='$media' />";
226        }
227
228        /**
229         * Filter the style loader HTML tag for page optimize.
230         *
231         * @param string $style_tag style loader tag
232         * @param array $handles handles of CSS files
233         * @param string $href link to CSS file
234         * @param string $media media attribute of the link.
235         *
236         * @since   1.0.0
237         */
238        $style_tag = apply_filters( 'page_optimize_style_loader_tag', $style_tag, $handles, $href, $media );
239
240        /**
241         * Filter the stylesheet tag. For example: making it deferred when using Critical CSS.
242         *
243         * @param string $style_tag stylesheet tag
244         * @param array $handles handles of CSS files
245         * @param string $href link to CSS file
246         * @param string $media media attribute of the link.
247         *
248         * @since   1.0.0
249         */
250        $style_tag = apply_filters( 'style_loader_tag', $style_tag, implode( ',', $handles ), $href, $media );
251
252        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
253        echo $style_tag . "\n";
254
255        array_map( array( $this, 'print_inline_style' ), $handles );
256    }
257
258    public function __isset( $key ) {
259        return isset( $this->old_styles->$key );
260    }
261
262    public function __unset( $key ) {
263        unset( $this->old_styles->$key );
264    }
265
266    public function &__get( $key ) {
267        return $this->old_styles->$key;
268    }
269
270    public function __set( $key, $value ) {
271        $this->old_styles->$key = $value;
272    }
273}