-
Notifications
You must be signed in to change notification settings - Fork 0
/
defer-async-loader.php
148 lines (130 loc) · 2.89 KB
/
defer-async-loader.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
<?php
/**
* Figuren_Theater Theming Defer_Async_Loader.
*
* Add async & defer attribute to asset links.
*
* Enqueue your scripts as normal,
* and simply add the #asyncload, or #deferload string
* to any script you want to async or defer.
*
* @package figuren-theater/theming/defer_async_loader
*/
namespace Figuren_Theater\Theming\Defer_Async_Loader;
use function add_action;
use function add_filter;
use function apply_filters;
use function is_admin;
/**
* Bootstrap module, when enabled.
*
* Async and Defer assets with PHP in WordPress.
* 08/2019
* https://uncoverwp.com/course/async-defer-assets-in-wordpress/
*/
function bootstrap() {
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\\load', 0 );
}
function load() : void {
if (is_admin())
return;
add_filter('script_loader_tag', __NAMESPACE__ . '\\load_async', 5, 3);
add_filter('script_loader_tag', __NAMESPACE__ . '\\load_defered', 5, 3);
}
/**
* This function is simply looking for the #asyncload string,
* and if found, appending async='async' to the URL.
*
*/
function load_async( string $tag, string $handle, string $src) : string {
// if this is alrady done, do nothing and return original $tag
if ( strpos( $tag, 'defer' ) || strpos( $tag, 'async' ) )
return $tag;
$scripts_to_async = array_flip(
apply_filters(
__NAMESPACE__ . '\\scripts_to_async',
[]
)
);
if (
// manage all third-party-assets,
// we havent control over
isset($scripts_to_async[ $handle ] )
||
//
strpos( $src, '#asyncload')
)
{
return str_replace(
[
' src',
'#asyncload',
],
[
' async src',
'',
],
$tag
);
}
return $tag;
}
/**
* This function is simply looking for the #deferload string,
* and if found, appending defer='defer' to the URL.
*
*/
function load_defered( string $tag, string $handle, string $src) : string {
// if this is alrady done, do nothing and return original $tag
if ( strpos( $tag, 'defer' ) || strpos( $tag, 'async' ) )
return $tag;
$scripts_to_defer = array_flip(
apply_filters(
__NAMESPACE__ . '\\scripts_to_defer', [
//
// 'jquery',
// 'jquery-core',
//
// 'cookie-notice-front',
//
// 'contact-form-7',
//
'mediaelement-core',
'mediaelement-migrate',
'mediaelement-vimeo',
'wp-mediaelement',
//
// 'photoswipe-lib',
// 'photoswipe-ui-default',
// 'photoswipe',
]
)
);
if (
// manage all third-party-assets,
// we havent control over
isset($scripts_to_defer[ $handle ] )
||
// or use it directly as a temporary appendix
// on registered scripts (and styles)
strpos( $src, '#deferload')
)
{
// returns the html-tag for calling the asset
// including an attr of 'defer'
// in case the URL contained our temporary
// appendix, it will get removed
return str_replace(
[
' src',
'#deferload',
],
[
' defer src',
'',
],
$tag
);
}
return $tag;
}