This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAutomaticAssetsManager.class.php
298 lines (262 loc) · 8.13 KB
/
AutomaticAssetsManager.class.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
* Contains AutomaticAssetsManager class
*/
/**
* Manages the assets
*
* Adds the possibility to load a javascript or a css
* in the middle in a template part without
* having the need to check if the asset can be
* loaded in the head or in the foot of the page.
*
* Uses ob_start and a placeholder for the assets.
* It's a singleton beacause it has to be run only once
*
* @author etessore
* @version 1.0.0
* @package classes
*
*/
class AutomaticAssetsManager {
private static $instance = null;
/**
* manages the list of css and js
* @var arrary
*/
private $assets = array('css' => array(), 'js' => array());
/**
* stores infos on path and uri
* @var array
*/
private $base_dir = array();
/**
* Stores the status for this feature
* @var boolean true if the feature is enabled
*/
private $status = false;
/**
* Placeholder for the css
* @var string
*/
const CSS_MARKER = '<!-- ###WPU### place here the css -->';
/**
* Placeholder for the top javascripts
* @var stirng
*/
const TOP_JS_MARKER = '<!-- ###WPU### place here the top js -->';
/**
* Placeholder for the bottom javascript
* @var string
*/
const BOTTOM_JS_MARKER = '<!-- ###WPU### place here the bottom js -->';
/**
* Register some assets and hooks into WP
*/
private function __construct(){
$this
->hook()
->enable_automatic_manager();
}
/**
* Retrieves the singleton instance for this feature
* @return AutomaticAssetsManager unique instance
*/
public static function get_instance(){
if(is_null(self::$instance)){
self::$instance = new self;
}
return self::$instance;
}
/**
* Adds a javascript to the current set
* @see @link http://codex.wordpress.org/Function_Reference/wp_register_script
* @param string $handle Script name
* @param string $src Script url
* @param array $deps (optional) Array of script names on which this script depends
* @param string|bool $ver (optional) Script version (used for cache busting), set to NULL to disable
* @param bool $in_footer (optional) Whether to enqueue the script before </head> or before </body>
* @return DefaultAssets $this for chainability
*/
public function add_js($handle, $src, $deps = array(), $ver = null, $in_footer = false){
$this->assets['js'][$handle] = array(
'handle' => $handle,
'src' => $src,
'deps' => $deps,
'ver' => $ver,
'in_footer' => $in_footer
);
return $this;
}
/**
* Adds a css to the current set
* @see @link http://codex.wordpress.org/Function_Reference/wp_register_style
* @param string $handle Name of the stylesheet.
* @param string|bool $src Path to the stylesheet from the root directory of WordPress. Example: '/css/mystyle.css'.
* @param array $deps Array of handles of any stylesheet that this stylesheet depends on.
* (Stylesheets that must be loaded before this stylesheet.) Pass an empty array if there are no dependencies.
* @param string|bool $ver String specifying the stylesheet version number. Set to NULL to disable.
* Used to ensure that the correct version is sent to the client regardless of caching.
* @param string $media The media for which this stylesheet has been defined.
* @return DefaultAssets $this for chainability
*/
public function add_css($handle, $src, $deps = array(), $ver = null, $media = false){
$this->assets['css'][$handle] = array(
'handle' => $handle,
'src' => $src,
'deps' => $deps,
'ver' => $ver,
'media' => $media
);
return $this;
}
/**
* Hooks the assets registration into wordpress init hook
* @return DefaultAssets $this for chainability
*/
public function hook(){
if(!is_admin()){
add_action('wp_enqueue_scripts', array(&$this, 'callback'), 8);
}
return $this;
}
/**
* Enables the loading of assets from template part
* @return DefaultAssets $this for chainability
*/
public function enable_automatic_manager(){
if(!$this->status && !is_admin()){
remove_action('wp_head', 'wp_print_styles', 8);
add_action('wp_head', array(&$this, 'on_wp_print_styles'));
add_action('wp_head', array(&$this, 'on_wp_print_scripts'));
add_action('wp_footer', array(&$this, 'on_wp_print_footer_scripts'));
add_action('shutdown', array(&$this, 'obstart_replace_assets_marker'), -11);
add_action('init', array(&$this, 'obstart_init'));
}
$this->status = true;
return $this;
}
/**
* Called by wordpress right before the page start
*/
public function obstart_init(){
ob_start();
}
/**
* Disables the loading of assets from template part
* @return DefaultAssets $this for chainability
*/
public function disable_automatic_manager(){
remove_action('wp_head', array(&$this, 'on_wp_print_styles'));
remove_action('wp_head', array(&$this, 'on_wp_print_scripts'));
remove_action('wp_footer', array(&$this, 'on_wp_print_footer_scripts'));
remove_action('shutdown', array(&$this, 'obstart_replace_assets_marker'), -11);
remove_action('init', array(&$this, 'obstart_init'));
$this->status = false;
return $this;
}
/**
* Prints a marker for the top css
*/
public function on_wp_print_styles(){ echo "\n".self::CSS_MARKER."\n"; }
/**
* Prints a marker for the top javascript
*/
public function on_wp_print_scripts(){ echo "\n".self::TOP_JS_MARKER."\n"; }
/**
* Prints a marker for the bottom javascript
*/
public function on_wp_print_footer_scripts(){ echo "\n".self::BOTTOM_JS_MARKER."\n"; }
/**
* Replaces the temporarily markers inserted
* on first page execution with the real
* list of html markup for the needed assets
*/
public function obstart_replace_assets_marker(){
$this->disable_automatic_manager();
$html = ob_get_clean();
foreach((array)ThemeHelpers::$assets['css'] as $handle) wp_enqueue_style($handle);
foreach((array)ThemeHelpers::$assets['js'] as $handle) wp_enqueue_script($handle);
$css_render = '';
$top_js_render = '';
$bottom_js_render = '';
ob_start();
wp_print_styles();
$css_render = ob_get_clean();
ob_start();
do_action('wp_print_scripts');
$top_js_render = ob_get_clean();
ob_start();
do_action('wp_print_footer_scripts');
$bottom_js_render = ob_get_clean();/**/
echo str_replace(
array(self::CSS_MARKER, self::TOP_JS_MARKER, self::BOTTOM_JS_MARKER),
array($css_render, $top_js_render, $bottom_js_render),
$html
);
}
/**
* This is called on wordpress init
* Tries to load asset from the child theme,
* if the asset file doesn't exists it loads
* from the parent theme
*/
public function callback(){
foreach($this->assets['js'] as $asset){
wp_deregister_script($asset['handle']);
if(file_exists(get_stylesheet_directory().$asset['src'])){
wp_register_script(
$asset['handle'],
get_stylesheet_directory_uri().$asset['src'],
$asset['deps'],
$asset['ver'],
$asset['in_footer']
);
} elseif(file_exists(get_template_directory().$asset['src'])) {
wp_register_script(
$asset['handle'],
get_template_directory_uri().$asset['src'],
$asset['deps'],
$asset['ver'],
$asset['in_footer']
);
} else {
wp_register_script(
$asset['handle'],
$asset['src'],
$asset['deps'],
$asset['ver'],
$asset['in_footer']
);
}
}
foreach($this->assets['css'] as $asset){
wp_deregister_style($asset['handle']);
if(file_exists(get_stylesheet_directory().$asset['src'])){
wp_register_style(
$asset['handle'],
get_stylesheet_directory_uri().$asset['src'],
$asset['deps'],
$asset['ver'],
$asset['media']
);
} elseif(file_exists(get_template_directory().$asset['src'])) {
wp_register_style(
$asset['handle'],
get_template_directory_uri().$asset['src'],
$asset['deps'],
$asset['ver'],
$asset['media']
);
} else {
wp_register_style(
$asset['handle'],
$asset['src'],
$asset['deps'],
$asset['ver'],
$asset['media']
);
}
}
}
}