-
Notifications
You must be signed in to change notification settings - Fork 1
/
vralle-jquery-cdn.php
112 lines (96 loc) · 2.7 KB
/
vralle-jquery-cdn.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
<?php
/**
* Plugin Name: vralle.jQuery-CDN
* Plugin URI: https://github.com/vralle/vralle-jquery-cdn
* Description: A modern way to load jQuery from CDN with a local fallback
* Version: 1.0.0
* Author: V.Ralle
* Author URI: https://github.com/vralle/
* License: MIT
* GitHub Plugin URI: https://github.com/vralle/vralle-jquery-cdn
* Requires WP: 4.6
* Requires PHP: 5.6
*
* @package vralle-jquery-cdn
*/
namespace VralleJqueryCdn;
use function \add_action;
use function \add_filter;
use function \apply_filters;
use function \esc_url;
use function \is_admin;
use function \sprintf;
use function \wp_add_inline_script;
use function \wp_deregister_script;
use function \wp_register_script;
use function \wp_scripts;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\\init' );
/**
* Register all of the needed hooks and actions.
*/
function init() {
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\\enqueue_scripts', 11 );
add_filter( 'script_loader_tag', __NAMESPACE__ . '\\make_tag', 10, 3 );
}
/**
* Load jQuery from Google CDN with a local fallback
*/
function enqueue_scripts() {
if ( is_admin() ) {
return;
}
$jquery_script = wp_scripts()->registered['jquery-core'];
$jquery_src = $jquery_script->src;
$jquery_ver = $jquery_script->ver;
/**
* CDN version of jQuery
*
* @param string Current jQuery version.
* @param string $jquery_ver The jQuery version used by WordPress.
*/
$cdn_ver = apply_filters( 'vralle_jquery_cdn_ver', '3.5.1', $jquery_ver );
wp_deregister_script( 'jquery-core' );
wp_deregister_script( 'jquery' );
wp_register_script(
'jquery-core',
'https://ajax.googleapis.com/ajax/libs/jquery/' . $cdn_ver . '/jquery.min.js',
array(),
$cdn_ver,
true
);
wp_register_script( 'jquery', false, array( 'jquery-core' ), $cdn_ver, true );
$site_url = site_url();
if ( ! $site_url ) {
$site_url = wp_guess_url();
}
$fallback = sprintf(
'(window.jQuery && jQuery.noConflict()) || document.write(\'<script src=\"%s\"><\/script>\')',
esc_url( $site_url . $jquery_src )
);
wp_add_inline_script( 'jquery', $fallback );
}
/**
* Make HTML tag
*
* @param string $tag The <script> tag for the enqueued script.
* @param string $handle The script's registered handle.
* @param string $src The script's source URL.
*
* @return string The script tag
*/
function make_tag( $tag, $handle, $src ) {
if ( is_admin() ) {
return $tag;
}
if ( 'jquery-core' === $handle ) {
$tag = sprintf(
'<script src="%s" crossorigin="anonymous"></script>',
esc_url( $src )
);
}
return $tag;
}