@@ -14,7 +14,6 @@ public function load(): void {
1414 add_action ( 'wp_enqueue_scripts ' , self ::jsToFooter ( ... ) );
1515 add_action ( 'wp_enqueue_scripts ' , self ::jqueryFromCdn ( ... ) );
1616 add_filter ( 'style_loader_tag ' , self ::addAsyncStyle ( ... ), 10 , 4 );
17- add_filter ( 'script_loader_tag ' , self ::addAsyncScript ( ... ), 10 , 2 );
1817
1918 if ( ! is_admin () ) {
2019 add_filter ( 'script_loader_src ' , self ::setScriptVersion ( ... ), 15 , 2 );
@@ -36,34 +35,6 @@ public static function addAsyncStyle( string $tag, string $handle, string $href,
3635 return $ tag ;
3736 }
3837
39- public static function addAsyncScript ( string $ tag , string $ handle ): string {
40- if ( is_admin () ) {
41- return $ tag ;
42- }
43-
44- $ methods = [
45- 'async ' => ['polyfill.io ' ],
46- 'defer ' => ['google-recaptcha ' ],
47- ];
48-
49- foreach ( $ methods as $ method => $ handlers ) {
50- /** @var string[] $validHandlers */
51- $ validHandlers = apply_filters ( "app_ {$ method }_scripts_handlers " , $ handlers );
52-
53- if ( empty ( $ validHandlers ) ) {
54- continue ;
55- }
56-
57- if ( ! \in_array ( $ handle , $ validHandlers , true ) ) {
58- continue ;
59- }
60-
61- $ tag = str_replace ( ' src ' , " {$ method } src " , $ tag );
62- }
63-
64- return $ tag ;
65- }
66-
6738 public static function setScriptVersion ( string $ scriptSrc , string $ handle ): string {
6839 if ( ! filter_var ( $ scriptSrc , FILTER_VALIDATE_URL , FILTER_FLAG_PATH_REQUIRED ) ) {
6940 return $ scriptSrc ;
@@ -97,57 +68,47 @@ public static function setScriptVersion( string $scriptSrc, string $handle ): st
9768 public static function jqueryFromCdn (): void {
9869 $ registered = wp_scripts ()->registered ;
9970
71+ $ suffix = SCRIPT_DEBUG ? '' : '.min ' ;
72+
10073 $ jqCore = $ registered ['jquery-core ' ];
10174
102- $ jsdelivrUrl = 'https://cdn.jsdelivr.net/npm/jquery-ui@1.12.1 ' ;
75+ $ jqUiCore = $ registered ['jquery-ui-core ' ];
76+ $ jqMigrate = $ registered ['jquery-migrate ' ];
10377
104- foreach ( array_keys ( $ registered ) as $ handle ) {
105- if ( str_starts_with ( ( string ) $ handle , 'jquery-effects- ' ) ) {
106- $ isCore = ' jquery-effects-core ' === $ handle ;
78+ $ jqVer = trim ( ( string ) $ jqCore -> ver , ' -wp ' );
79+ $ jqUiVer = trim ( ( string ) $ jqUiCore -> ver , '-wp ' );
80+ $ jqMigrateVer = trim ( ( string ) $ jqMigrate -> ver , ' -wp ' ) ;
10781
108- $ newUrl = \sprintf (
109- '%s/ui/effect%s.min.js ' ,
110- $ jsdelivrUrl ,
111- $ isCore ? '' : str_replace ( 'jquery-effects- ' , '- ' , (string ) $ handle )
112- );
82+ foreach ( $ registered as $ handle => $ dependency ) {
11383
114- self ::deregisterScript ( (string ) $ handle , $ newUrl );
84+ if ( empty ( $ dependency ->src ) ) {
85+ continue ;
11586 }
11687
117- if ( str_starts_with ( (string ) $ handle , 'jquery-ui- ' ) ) {
118- $ newUrl = match ( $ handle ) {
119- 'jquery-ui-core ' => \sprintf ( '%s/ui/core.min.js ' , $ jsdelivrUrl ),
120- 'jquery-ui-widget ' => \sprintf ( '%s/ui/widget.min.js ' , $ jsdelivrUrl ),
121- default => \sprintf (
122- '%s/ui/widgets/%s.min.js ' ,
123- $ jsdelivrUrl ,
124- str_replace ( 'jquery-ui- ' , '' , (string ) $ handle )
125- ),
126- };
127-
128- self ::deregisterScript ( (string ) $ handle , $ newUrl );
88+ if ( ! str_contains ( $ dependency ->src , '/ ' .WPINC .'/ ' ) ) {
89+ continue ;
12990 }
130- }
13191
132- $ jqVer = trim ( (string ) $ jqCore -> ver , '-wp ' );
92+ $ isJqueryUiCore = str_starts_with ( (string ) $ handle , ' jquery-effects- ' ) || str_starts_with ( ( string ) $ handle , 'jquery-ui- ' );
13393
134- self ::deregisterScript ( $ jqCore ->handle , \sprintf ( 'https://code.jquery.com/jquery-%s.min.js ' , esc_attr ( $ jqVer ) ) );
135- self ::deregisterScript ( 'jquery ' );
94+ if ( ! $ isJqueryUiCore ) {
95+ continue ;
96+ }
13697
137- wp_register_script ( 'jquery ' , false , [$ jqCore ->handle ], $ jqVer , true );
138- }
98+ $ dependency ->src = false ;
99+ }
100+
101+ self ::deregisterScript ( $ jqCore ->handle , \sprintf ( 'https://cdn.jsdelivr.net/npm/jquery@%s/dist/jquery%s.js ' , esc_attr ( $ jqVer ), $ suffix ) );
102+ self ::deregisterScript ( $ jqMigrate ->handle , \sprintf ( 'https://cdn.jsdelivr.net/npm/jquery-migrate@%s/dist/jquery-migrate%s.js ' , esc_attr ( $ jqMigrateVer ), $ suffix ) );
103+ self ::deregisterScript ( $ jqUiCore ->handle , \sprintf ( 'https://cdn.jsdelivr.net/npm/jquery-ui-dist@%s/jquery-ui%s.js ' , esc_attr ( $ jqUiVer ), $ suffix ) );
139104
140- public static function jsToFooter (): void {
141- remove_action ( 'wp_head ' , 'wp_print_scripts ' );
142- remove_action ( 'wp_head ' , 'wp_print_head_scripts ' , 9 );
143- remove_action ( 'wp_head ' , 'wp_enqueue_scripts ' , 1 );
144105 }
145106
146107 public static function deregisterScript ( string $ handle , ?string $ newUrl = null , bool $ enqueue = false ): void {
147108 $ registered = wp_scripts ()->registered ;
148109
149- if ( ! empty ( $ registered [$ handle ] ) ) {
150- $ jsLib = $ registered [$ handle ];
110+ if ( ! empty ( $ registered [ $ handle ] ) ) {
111+ $ jsLib = $ registered [ $ handle ];
151112
152113 wp_dequeue_script ( $ jsLib ->handle );
153114 wp_deregister_script ( $ jsLib ->handle );
@@ -164,8 +125,8 @@ public static function deregisterScript( string $handle, ?string $newUrl = null,
164125 public static function deregisterStyle ( string $ handle , ?string $ newUrl = null , bool $ enqueue = false ): void {
165126 $ registered = wp_styles ()->registered ;
166127
167- if ( ! empty ( $ registered [$ handle ] ) ) {
168- $ cssLib = $ registered [$ handle ];
128+ if ( ! empty ( $ registered [ $ handle ] ) ) {
129+ $ cssLib = $ registered [ $ handle ];
169130
170131 wp_dequeue_style ( $ cssLib ->handle );
171132 wp_deregister_style ( $ cssLib ->handle );
@@ -178,6 +139,12 @@ public static function deregisterStyle( string $handle, ?string $newUrl = null,
178139 }
179140 }
180141
142+ private static function jsToFooter (): void {
143+ remove_action ( 'wp_head ' , 'wp_print_scripts ' );
144+ remove_action ( 'wp_head ' , 'wp_print_head_scripts ' , 9 );
145+ remove_action ( 'wp_head ' , 'wp_enqueue_scripts ' , 1 );
146+ }
147+
181148 private static function prepareScriptFilePath ( string $ scriptSrc ): bool |string {
182149 $ rootDir = app_locate_root_dir ();
183150
0 commit comments