-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses-puzzler.php
665 lines (488 loc) · 19.7 KB
/
classes-puzzler.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
<?php
trait PUZZLER_Trait {
public static $cacheDir = 'cache';
private $fileFullPath = '';
private $_mapStateTemplate = "/** ## %s ## **/\n";
private $_mapStateDigest = '';
/**
* Import data from WP_Scripts/WP_Styles in Puzzler object
* @param $object
* @throws Exception
*/
public function import( $object ) {
if ( ! $object instanceof WP_Dependencies ) {
throw new Exception( __( 'You must import only WP_Scripts/WP_Styles object!' , 'puzzler' ) );
}
$props = get_object_vars( $object );
if ( empty( $props ) ) throw new Exception( 'The ' . get_class( $object ) . ' object is empty!');
foreach ( $props as $key => $value) {
if ( property_exists( $this, $key ) ) {
$this->$key = $value;
}
}
}
/**
* Override do_items of WP_Scripts/WP_Styles
*
* @param bool|array $handles
* @param bool $group (0 - header , 1 - footer)
* @return array
*/
public function do_items( $handles = false, $group = false ) {
$group = (int)$group;
$handles = false === $handles ? $this->queue : (array) $handles;
$this->all_deps( $handles );
/**
* Convert late styles in fair footer group ( By default WP consider it as head group )
* and
* Print footer styles by default ( without combining )
*/
if ( $this instanceof WP_Styles ) {
$this->puzzler_styles_late2foot( $group );
$this->puzzler_lazyload_starter( $group );
if ( $this->puzzler_styles_foot_do_default( $group ) ) return $this->done;
}
/**
* Set map states of handles.
* It for efficient detecting of changes in puzzler_check_change()
*
*/
if ( 0 === $group ) {
$this->puzzler_set_map_state();
}
// -- prepare full file name by depending of group
$this->puzzler_prepare_file_name( $group );
// -- processing of external handles (not local)
$this->puzzler_process_external( $group );
// -- print all extra data of group
$this->puzzler_print_extra( $group );
// -- check changes of handles and combine
if ( $this->puzzler_check_change( $group ) ) {
$this->puzzler_combine( $group );
}
$this->puzzler_print_tag( $group );
return $this->done;
}
/**
* Set state (hash) for map ( order, group, src, some extra data ) of scripts/styles enqueued by wp_enqueue_,,,().
* It for efficient auto detect changes
*/
protected function puzzler_set_map_state() {
$hash = 'NONE';
if ( ! empty( $this->to_do ) ) {
$data = array();
foreach( $this->to_do as $key => $handle ) {
$data[$handle] = array(
'group' => $this->groups[$handle],
'src' => $this->registered[$handle]->src
);
if ( $this instanceof WP_Styles ) {
$data[$handle]['args'] = $this->registered[$handle]->args;
$data[$handle]['extra'] = $this->registered[$handle]->extra;
}
}
$hash = md5( serialize( $data ) );
}
$this->_mapStateDigest = sprintf( $this->_mapStateTemplate , $hash );
}
/**
* Preparing full path aggregate file by depending of group (header/footer)
* @param $group (0 - header , 1 - footer)
*
*/
protected function puzzler_prepare_file_name( $group ) {
$this->fileFullPath = WP_CONTENT_DIR . '/' . static::$cacheDir . '/' ;
$this->fileFullPath .= ( 0 === $group ) ? $this->fileNameHeader : $this->fileNameFooter;
}
/**
* Processing external script handles.
* Probably handle is external..e.g. from http://another-resource.com/js/external.js...
*
* @param $group (0 - header , 1 - footer)
*/
protected function puzzler_process_external( $group ) {
foreach( $this->to_do as $key => $handle ) {
if ( ! in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) {
// -- processing items only by current group
if ( $this->groups[$handle] !== $group ) {
continue;
}
// -- processing items without source
if ( ! $this->registered[$handle]->src ) {
$this->done[] = $handle;
unset( $this->to_do[$key] );
continue;
}
// -- do behavior by default
if ( ! $this->puzzler_get_src_local( $this->registered[$handle]->src ) ) {
if ( $this->do_item( $handle, $group ) ) {
$this->done[] = $handle;
unset( $this->to_do[$key] );
}
}
}
}
}
/**
* Checks (content handles and map states) changes within group
* @param $group (0 - header , 1 - footer)
* @return bool : true - changes happened, false - no.
*/
protected function puzzler_check_change( $group ) {
if ( empty ( $this->to_do ) ) return false;
// -- check change by time
foreach( $this->to_do as $key => $handle ) {
// -- processing items only by current group
if ( $this->groups[$handle] !== $group ) {
continue;
}
if ( filemtime( $this->puzzler_get_src_local( $this->registered[$handle]->src ) ) > (int)@filemtime( $this->fileFullPath ) ) {
return true;
}
}
// -- check change by map state
$script = @fopen( $this->fileFullPath , "r");
$line = '';
if ( $script ) {
for( $i=0; $i < 2; $i++) {
$line = fgets( $script );
}
fclose( $script );
}
list( $new_map ) = sscanf( $this->_mapStateDigest , $this->_mapStateTemplate );
list( $old_map ) = sscanf( $line , $this->_mapStateTemplate );
return ( $new_map !== $old_map ) ? true : false;
}
/**
* Combine handles and save aggregate file
* @param $group (0 - header , 1 - footer)
*/
protected function puzzler_combine( $group ) {
$this->concat = "/** Combined by WP Puzzler plugin at " . current_time( 'mysql' ) . " **/\n";
$this->concat .= $this->_mapStateDigest;
foreach( $this->to_do as $key => $handle ) {
// -- processing items only by current group
if ( $this->groups[$handle] !== $group ) {
continue;
}
$this->puzzler_add_handle_content( $handle );
$this->done[] = $handle;
unset( $this->to_do[$key] );
}
file_put_contents( $this->fileFullPath, $this->concat );
}
/**
* Get full src ( include version ) for script/link tags
* @return string
*/
protected function puzzler_get_src_tag() {
$fix_win = str_replace("\\", "/", $this->fileFullPath);
$ver = md5_file( $fix_win );
$src_half = str_replace( WP_CONTENT_DIR , '', $this->fileFullPath );
return $this->content_url . $src_half . '?' . $ver;
}
/**
* Detect local exist handle by src ( from wp_enqueue...() )
*
* @param $src
* @return bool|string - false for not exist | full local path
*/
protected function puzzler_get_src_local ( $src ) {
$src = str_replace( "\\" , "/" , $src );
$src_root_wp = ABSPATH . $src;
$src_other = ABSPATH . str_replace( $this->base_url, '' , $src );
if ( file_exists( $src_other ) ) {
return $src_other;
}
if ( file_exists( $src_root_wp ) ) {
return $src_root_wp;
}
if ( file_exists( $src ) ) {
return $src;
}
return false;
}
}
class PUZZLER_Scripts extends WP_Scripts {
use PUZZLER_Trait;
public $fileNameHeader = 'all-header.js';
public $fileNameFooter = 'all-footer.js';
private $_asyncHead;
private $_asyncFoot;
/**
* Set settings of async (defer) load scripts
*/
public function __construct() {
$settings = get_option( 'puzzler_settings' , puzzler_get_default_settings() );
$this->_asyncHead = $settings['HScriptsAsync'];
$this->_asyncFoot = $settings['FScriptsAsync'];
}
/**
* Print some extra data before aggregate script tag depending by group
* @param $group (0 - header , 1 - footer)
*/
protected function puzzler_print_extra ( $group ) {
foreach( $this->to_do as $key => $handle ) {
// -- processing items only by current group
if ( $this->groups[$handle] !== $group ) {
continue;
}
$obj = $this->registered[$handle];
$cond_before = $cond_after = '';
$conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : false;
if ( $conditional ) {
$cond_before = "<!--[if {$conditional}]>\n";
$cond_after = "<![endif]-->\n";
}
$has_conditional_data = $conditional && $this->get_data( $handle, 'data' );
if ( $has_conditional_data ) {
echo $cond_before;
}
$this->print_extra_script( $handle );
if ( $has_conditional_data ) {
echo $cond_after;
}
}
}
/**
* Add handle content to aggregate string
* @param $handle
*/
protected function puzzler_add_handle_content ( $handle ) {
$obj = $this->registered[$handle];
$src = $obj->src;
$this->concat .= "\n" . file_get_contents( $this->puzzler_get_src_local( $src ) ) . "\n";
}
/**
* Print script tag depending by group
* @param $group (0 - header , 1 - footer)
*/
protected function puzzler_print_tag( $group ) {
if ( ! in_array( $group, $this->groups ) ) {
return;
}
$async = '';
if ( ( 0 === $group && $this->_asyncHead ) || ( 1 === $group && $this->_asyncFoot ) ) {
$async = "defer='defer'";
}
$src = $this->puzzler_get_src_tag();
echo "<script {$async} type='text/javascript' src='$src'></script>\n";
}
}
class PUZZLER_Styles extends WP_Styles {
use PUZZLER_Trait;
public $fileNameHeader = 'all-header.css';
public $fileNameFooter = 'all-footer.css';
private $_lazyHead;
private $_lazyFoot;
// -- temporarily storage head styles for separate late styles
private $_headStyles;
/**
* Set settings of lazy load styles
*/
public function __construct() {
$settings = get_option( 'puzzler_settings' , puzzler_get_default_settings() );
$this->_lazyHead = $settings['HStylesLazy'];
$this->_lazyFoot = $settings['FStylesLazy'];
}
/**
* Pass styles only with media='all' and without extra data, else do behavior by default.
* For increased compatibility.
* @param $group (0 - header , 1 - footer)
*/
protected function puzzler_print_extra ( $group ) {
foreach( $this->to_do as $key => $handle ) {
// -- processing items only by current group
if ( $this->groups[$handle] !== $group ) {
continue;
}
$obj = $this->registered[$handle];
/**
* Processing only styles without alternative stylesheets, titles, conditionals *
* and
* with media = 'all'
*
*/
$media = ( isset($obj->args) && 'all' == $obj->args ) ? true : false;
$alt = ( isset($obj->extra['alt']) && $obj->extra['alt'] ) ? false : true;
$title = ( isset($obj->extra['title']) ) ? false : true;
$cond = ( isset($obj->extra['conditional'] ) && $obj->extra['conditional'] ) ? false : true;
if ( ! $media || ! $alt || ! $title || ! $cond) {
if ( parent::do_item( $handle ) ) {
$this->done[] = $handle;
unset( $this->to_do[$key] );
}
}
}
}
/**
* Convert late styles to fair footer group. WP by default considers it as header group.
* @param $group (0 - header , 1 - footer)
*/
protected function puzzler_styles_late2foot ( $group ) {
if ( 0 === $group ) {
$this->_headStyles = $this->to_do;
}
if ( 1 === $group ) {
$diff = array_diff( $this->to_do , $this->_headStyles);
foreach( $diff as $key => $handle ) {
$this->groups[$handle] = 1;
}
}
}
/**
* For all late styles (now footer group) do default behavior with lazy load opportunity
* @param $group (0 - header , 1 - footer)
* @return bool
*/
protected function puzzler_styles_foot_do_default ( $group ) {
if ( 1 !== $group ) {
return false;
}
foreach( $this->to_do as $key => $handle ) {
// -- processing items only by current group
if ( $this->groups[$handle] !== $group ) {
continue;
}
if ( parent::do_item( $handle ) ) {
$this->done[] = $handle;
unset( $this->to_do[$key] );
}
}
return true;
}
/**
* Insert JS starter for lazy load css
* @param $group (0 - header , 1 - footer)
*/
protected function puzzler_lazyload_starter( $group ) {
$lazy_starter = "\n";
remove_all_filters( 'style_loader_tag' );
if ( $this->_lazyHead && 0 === $group && in_array( $group , $this->groups ) ) {
add_filter('style_loader_tag', array( $this, 'puzzler_styles_lazy_tag' ) );
$lazy_starter="<script>var lazyHead=function(){for(var e=document.getElementsByTagName('head')[0],a=e.getElementsByTagName('link'),t=0;t<a.length;t++)a[t].outerHTML=a[t].outerHTML.replace(/lazy/g,'href')};window.addEventListener('load',lazyHead);</script>\n";
}
if ( $this->_lazyFoot && 1 === $group && in_array( $group , $this->groups ) ) {
add_filter('style_loader_tag', array( $this, 'puzzler_styles_lazy_tag' ) );
$lazy_starter="<script>var lazyFoot=function(){for(var e=document.getElementsByTagName('body')[0],a=e.getElementsByTagName('link'),t=0;t<a.length;t++)a[t].outerHTML=a[t].outerHTML.replace(/lazy/g,'href')};var raf=requestAnimationFrame||mozRequestAnimationFrame||webkitRequestAnimationFrame||msRequestAnimationFrame;raf?raf(lazyFoot):window.addEventListener('load',lazyFoot);</script>\n";
}
echo $lazy_starter;
}
/**
* Hook for replace 'href' attr in 'link' tag for lazy load styles
* @param $tag
* @return mixed
*/
public function puzzler_styles_lazy_tag ( $tag ) {
$lazy_style = str_replace( 'href' , 'lazy', $tag );
return $lazy_style;
}
/**
* Add handle content to aggregate string and fix internal stylesheet links ( url, src )
* @param $handle
*/
protected function puzzler_add_handle_content ( $handle ) {
global $src_dir;
$obj = $this->registered[$handle];
$src = $obj->src;
$src_local = $this->puzzler_get_src_local( $src );
$src_dir = dirname( $src_local );
$raw_content = file_get_contents( $src_local );
// -- change internal stylesheet links url-based
$half_content = preg_replace_callback('/url\s*\([\'\"]?([^)\'\"]+)[\'\"]?\)/iu', array( $this, 'puzzler_cb_internal_links_url') , $raw_content);
// -- change internal stylesheet links src-based
$content = preg_replace_callback('/src\s*=\s*[\'\"]([^\'\"]+)[\'\"]/iu', array( $this, 'puzzler_cb_internal_links_src') , $half_content);
$this->concat .= "\n" . $content . "\n";
// -- check exist inline styles
$inline = $this->get_data( $handle, 'after' );
if ( empty( $inline ) ) return;
$inline_style = implode( "\n", $inline );
$this->concat .= $inline_style;
$this->concat .= "\n";
}
/**
* Print link tag depending by group
* @param $group (0 - header , 1 - footer)
*/
protected function puzzler_print_tag( $group ) {
if ( ! in_array( $group, $this->groups ) ) {
return;
}
$src = $this->puzzler_get_src_tag();
$href = 'href';
if ( ( 0 === $group && $this->_lazyHead ) || ( 1 === $group && $this->_lazyFoot ) ) {
$href = 'lazy';
}
echo "<link rel='stylesheet' {$href}='$src' type='text/css' media='all' />\n";
}
/**
* Callback from @puzzler_add_handle_content() for fix internal stylesheet links ( url )
* @param $matches
* @return string
*/
private function puzzler_cb_internal_links_url( $matches ) {
global $src_dir;
if ( 0 === strpos( $matches[1] , 'http') || 0 === strpos( $matches[1] , 'data') ) return $matches[0];
$dirty_path = $src_dir .'/'. $matches[1];
preg_match('/[?#].+$/iu', $dirty_path, $params);
$params = ( empty($params) ) ? '' : $params[0];
$clear_path = str_replace( $params, '' , $dirty_path );
$real_path = realpath( $clear_path );
$replace = $this->getRelativePath( $this->fileFullPath , $real_path ) . $params;
return "url({$replace})";
}
/**
* Callback from @puzzler_add_handle_content() for fix internal stylesheet links ( src )
* @param $matches
* @return string
*/
private function puzzler_cb_internal_links_src( $matches ) {
global $src_dir;
$dirty_path = $src_dir .'/'. $matches[1];
preg_match('/[?#].+$/iu', $dirty_path, $params);
$params = ( empty($params) ) ? '' : $params[0];
$clear_path = str_replace( $params, '' , $dirty_path );
$real_path = realpath( $clear_path );
$replace = $this->getRelativePath( $this->fileFullPath , $real_path ) . $params;
return "src='{$replace}'";
}
/**
* This func is taken
* from Symfony URL generator https://github.com/symfony/Routing/blob/master/Generator/UrlGenerator.php
*
* @param $basePath
* @param $targetPath
* @return string
*/
private function getRelativePath( $basePath, $targetPath )
{
// -- windows paths fix
$basePath = str_replace( "\\" , "/" , $basePath );
$targetPath = str_replace( "\\" , "/" , $targetPath );
$pattern = '/(\D):/iu';
$replacement = "/$1";
$targetPath = preg_replace($pattern, $replacement, $targetPath);
$basePath = preg_replace($pattern, $replacement, $basePath);
// --
if ($basePath === $targetPath) {
return '';
}
$sourceDirs = explode('/', isset($basePath[0]) && '/' === $basePath[0] ? substr($basePath, 1) : $basePath);
$targetDirs = explode('/', isset($targetPath[0]) && '/' === $targetPath[0] ? substr($targetPath, 1) : $targetPath);
array_pop($sourceDirs);
$targetFile = array_pop($targetDirs);
foreach ($sourceDirs as $i => $dir) {
if (isset($targetDirs[$i]) && $dir === $targetDirs[$i]) {
unset($sourceDirs[$i], $targetDirs[$i]);
} else {
break;
}
}
$targetDirs[] = $targetFile;
$path = str_repeat('../', count($sourceDirs)).implode('/', $targetDirs);
return '' === $path || '/' === $path[0]
|| false !== ($colonPos = strpos($path, ':')) && ($colonPos < ($slashPos = strpos($path, '/')) || false === $slashPos)
? "./$path" : $path;
}
}
?>