forked from Automattic/page-optimize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcat-css.php
224 lines (188 loc) · 7.32 KB
/
concat-css.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
require_once __DIR__ . '/dependency-path-mapping.php';
require_once __DIR__ . '/utils.php';
if ( ! defined( 'ALLOW_GZIP_COMPRESSION' ) ) {
define( 'ALLOW_GZIP_COMPRESSION', true );
}
class Page_Optimize_CSS_Concat extends WP_Styles {
private $dependency_path_mapping;
private $old_styles;
public $allow_gzip_compression;
function __construct( $styles ) {
if ( empty( $styles ) || ! ( $styles instanceof WP_Styles ) ) {
$this->old_styles = new WP_Styles();
} else {
$this->old_styles = $styles;
}
// Unset all the object properties except our private copy of the styles object.
// We have to unset everything so that the overload methods talk to $this->old_styles->whatever
// instead of $this->whatever.
foreach ( array_keys( get_object_vars( $this ) ) as $key ) {
if ( 'old_styles' === $key ) {
continue;
}
unset( $this->$key );
}
$this->dependency_path_mapping = new Page_Optimize_Dependency_Path_Mapping(
apply_filters( 'page_optimize_site_url', $this->base_url )
);
}
function do_items( $handles = false, $group = false ) {
$handles = false === $handles ? $this->queue : (array) $handles;
$stylesheets = array();
$siteurl = apply_filters( 'page_optimize_site_url', $this->base_url );
$this->all_deps( $handles );
$stylesheet_group_index = 0;
// Merge CSS into a single file
$concat_group = 'concat';
// Concat group on top (first array element gets processed earlier)
$stylesheets[ $concat_group ] = array();
foreach ( $this->to_do as $key => $handle ) {
$obj = $this->registered[ $handle ];
$obj->src = apply_filters( 'style_loader_src', $obj->src, $obj->handle );
// Core is kind of broken and returns "true" for src of "colors" handle
// http://core.trac.wordpress.org/attachment/ticket/16827/colors-hacked-fixed.diff
// http://core.trac.wordpress.org/ticket/20729
$css_url = $obj->src;
if ( 'colors' == $obj->handle && true === $css_url ) {
$css_url = wp_style_loader_src( $css_url, $obj->handle );
}
$css_url_parsed = parse_url( $obj->src );
$extra = $obj->extra;
// Don't concat by default
$do_concat = false;
// Only try to concat static css files
if ( false !== strpos( $css_url_parsed['path'], '.css' ) ) {
$do_concat = true;
} else {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
echo sprintf( "\n<!-- No Concat CSS %s => Maybe Not Static File %s -->\n", esc_html( $handle ), esc_html( $obj->src ) );
}
}
// Don't try to concat styles which are loaded conditionally (like IE stuff)
if ( isset( $extra['conditional'] ) ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
echo sprintf( "\n<!-- No Concat CSS %s => Has Conditional -->\n", esc_html( $handle ) );
}
$do_concat = false;
}
// Don't concat rtl stuff for now until concat supports it correctly
if ( $do_concat && 'rtl' === $this->text_direction && ! empty( $extra['rtl'] ) ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
echo sprintf( "\n<!-- No Concat CSS %s => Is RTL -->\n", esc_html( $handle ) );
}
$do_concat = false;
}
// Don't try to concat externally hosted scripts
$is_internal_uri = $this->dependency_path_mapping->is_internal_uri( $css_url );
if ( $do_concat && ! $is_internal_uri ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
echo sprintf( "\n<!-- No Concat CSS %s => External URL: %s -->\n", esc_html( $handle ), esc_url( $css_url ) );
}
$do_concat = false;
}
if ( $do_concat ) {
// Resolve paths and concat styles that exist in the filesystem
$css_realpath = $this->dependency_path_mapping->dependency_src_to_fs_path( $css_url );
if ( false === $css_realpath ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
echo sprintf( "\n<!-- No Concat CSS %s => Invalid Path %s -->\n", esc_html( $handle ), esc_html( $css_realpath ) );
}
$do_concat = false;
}
}
// Skip concating CSS from exclusion list
$exclude_list = page_optimize_css_exclude_list();
foreach ( $exclude_list as $exclude ) {
if ( $do_concat && $handle === $exclude ) {
$do_concat = false;
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
echo sprintf( "\n<!-- No Concat CSS %s => Excluded option -->\n", esc_html( $handle ) );
}
}
}
// Allow plugins to disable concatenation of certain stylesheets.
if ( $do_concat && ! apply_filters( 'css_do_concat', $do_concat, $handle ) ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
echo sprintf( "\n<!-- No Concat CSS %s => Filtered `false` -->\n", esc_html( $handle ) );
}
}
$do_concat = apply_filters( 'css_do_concat', $do_concat, $handle );
if ( true === $do_concat ) {
$media = $obj->args;
if ( empty( $media ) ) {
$media = 'all';
}
$stylesheets[ $concat_group ][ $media ][ $handle ] = $css_url_parsed['path'];
$this->done[] = $handle;
} else {
$stylesheet_group_index ++;
$stylesheets[ $stylesheet_group_index ]['noconcat'][] = $handle;
$stylesheet_group_index ++;
}
unset( $this->to_do[ $key ] );
}
foreach ( $stylesheets as $idx => $stylesheets_group ) {
foreach ( $stylesheets_group as $media => $css ) {
if ( 'noconcat' == $media ) {
foreach ( $css as $handle ) {
if ( $this->do_item( $handle, $group ) ) {
$this->done[] = $handle;
}
}
continue;
} elseif ( count( $css ) > 1 ) {
$fs_paths = array();
foreach ( $css as $css_uri_path ) {
$fs_paths[] = $this->dependency_path_mapping->uri_path_to_fs_path( $css_uri_path );
}
$mtime = max( array_map( 'filemtime', $fs_paths ) );
if ( page_optimize_use_concat_base_dir() ) {
$path_str = implode( ',', array_map( 'page_optimize_remove_concat_base_prefix', $fs_paths ) );
} else {
$path_str = implode( ',', $css );
}
$path_str = "$path_str?m=$mtime";
if ( $this->allow_gzip_compression ) {
$path_64 = base64_encode( gzcompress( $path_str ) );
if ( strlen( $path_str ) > ( strlen( $path_64 ) + 1 ) ) {
$path_str = '-' . $path_64;
}
}
$href = $siteurl . "/_static/??" . $path_str;
} else {
$href = Page_Optimize_Utils::cache_bust_mtime( current( $css ), $siteurl );
}
$handles = array_keys( $css );
$css_id = "$media-css-" . md5( $href );
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
echo apply_filters( 'page_optimize_style_loader_tag', "<link data-handles='" . esc_attr( implode( ',', $handles ) ) . "' rel='stylesheet' id='$css_id' href='$href' type='text/css' media='$media' />\n", $handles, $href, $media );
} else {
echo apply_filters( 'page_optimize_style_loader_tag', "<link rel='stylesheet' id='$css_id' href='$href' type='text/css' media='$media' />\n", $handles, $href, $media );
}
array_map( array( $this, 'print_inline_style' ), array_keys( $css ) );
}
}
return $this->done;
}
function __isset( $key ) {
return isset( $this->old_styles->$key );
}
function __unset( $key ) {
unset( $this->old_styles->$key );
}
function &__get( $key ) {
return $this->old_styles->$key;
}
function __set( $key, $value ) {
$this->old_styles->$key = $value;
}
}
function page_optimize_css_concat_init() {
global $wp_styles;
$wp_styles = new Page_Optimize_CSS_Concat( $wp_styles );
$wp_styles->allow_gzip_compression = ALLOW_GZIP_COMPRESSION;
}
if ( page_optimize_should_concat_css() ) {
add_action( 'init', 'page_optimize_css_concat_init' );
}