1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Module Name: Photon |
4
|
|
|
* Module Description: Serve images from our servers |
5
|
|
|
* Jumpstart Description: Mirrors and serves your images from our free and fast image CDN, improving your site’s performance with no additional load on your servers. |
6
|
|
|
* Sort Order: 25 |
7
|
|
|
* Recommendation Order: 1 |
8
|
|
|
* First Introduced: 2.0 |
9
|
|
|
* Requires Connection: Yes |
10
|
|
|
* Auto Activate: No |
11
|
|
|
* Module Tags: Photos and Videos, Appearance, Recommended |
12
|
|
|
* Feature: Recommended, Jumpstart, Appearance |
13
|
|
|
* Additional Search Queries: photon, image, cdn, performance, speed |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
Jetpack::dns_prefetch( array( |
17
|
|
|
'//i0.wp.com', |
18
|
|
|
'//i1.wp.com', |
19
|
|
|
'//i2.wp.com', |
20
|
|
|
'//c0.wp.com', |
21
|
|
|
) ); |
22
|
|
|
|
23
|
|
|
Jetpack_Photon::instance(); |
24
|
|
|
|
25
|
|
|
class Jetpack_Photon_Static_Assets_CDN { |
26
|
|
|
public static function go() { |
27
|
|
|
add_action( 'wp_print_scripts', array( __CLASS__, 'cdnize_assets' ) ); |
28
|
|
|
add_action( 'wp_print_styles', array( __CLASS__, 'cdnize_assets' ) ); |
29
|
|
|
add_action( 'wp_footer', array( __CLASS__, 'cdnize_assets' ) ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function cdnize_assets() { |
33
|
|
|
global $wp_scripts, $wp_styles, $wp_version; |
34
|
|
|
|
35
|
|
|
$known_core_files = self::get_core_checksums(); |
|
|
|
|
36
|
|
|
$site_url = trailingslashit( site_url() ); |
37
|
|
View Code Duplication |
foreach ( $wp_scripts->registered as $handle => $thing ) { |
38
|
|
|
if ( wp_startswith( $thing->src, 'https://c0.wp.com/' ) ) { |
39
|
|
|
continue; |
40
|
|
|
} |
41
|
|
|
$src = ltrim( str_replace( $site_url, '', $thing->src ), '/' ); |
42
|
|
|
if ( in_array( $src, $known_core_files ) ) { |
43
|
|
|
$wp_scripts->registered[ $handle ]->src = sprintf('https://c0.wp.com/c/%1$s/%2$s', $wp_version, $src ); |
44
|
|
|
$wp_scripts->registered[ $handle ]->ver = null; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
View Code Duplication |
foreach ( $wp_styles->registered as $handle => $thing ) { |
48
|
|
|
if ( wp_startswith( $thing->src, 'https://c0.wp.com/' ) ) { |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
$src = ltrim( str_replace( $site_url, '', $thing->src ), '/' ); |
52
|
|
|
if ( in_array( $src, $known_core_files ) ) { |
53
|
|
|
$wp_styles->registered[ $handle ]->src = sprintf('https://c0.wp.com/c/%1$s/%2$s', $wp_version, $src ); |
54
|
|
|
$wp_styles->registered[ $handle ]->ver = null; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
self::cdnize_plugin_assets( 'jetpack', JETPACK__VERSION ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public static function cdnize_plugin_assets( $plugin_slug, $current_version ) { |
62
|
|
|
global $wp_scripts, $wp_styles; |
63
|
|
|
|
64
|
|
|
$assets = self::get_plugin_assets( $plugin_slug, $current_version ); |
65
|
|
|
$plugin_directory_url = plugins_url() . '/' . $plugin_slug . '/'; |
66
|
|
|
|
67
|
|
|
if ( is_wp_error( $assets ) ) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
foreach ( $wp_scripts->registered as $handle => $thing ) { |
72
|
|
|
if ( wp_startswith( $thing->src, 'https://c0.wp.com/' ) ) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
if ( wp_startswith( $thing->src, $plugin_directory_url ) ) { |
76
|
|
|
$local_path = substr( $thing->src, strlen( $plugin_directory_url ) ); |
77
|
|
|
if ( in_array( $local_path, $assets ) ) { |
78
|
|
|
$wp_scripts->registered[ $handle ]->src = sprintf('https://c0.wp.com/p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path ); |
79
|
|
|
$wp_scripts->registered[ $handle ]->ver = null; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
View Code Duplication |
foreach ( $wp_styles->registered as $handle => $thing ) { |
84
|
|
|
if ( wp_startswith( $thing->src, 'https://c0.wp.com/' ) ) { |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
if ( wp_startswith( $thing->src, $plugin_directory_url ) ) { |
88
|
|
|
$local_path = substr( $thing->src, strlen( $plugin_directory_url ) ); |
89
|
|
|
if ( in_array( $local_path, $assets ) ) { |
90
|
|
|
$wp_styles->registered[ $handle ]->src = sprintf('https://c0.wp.com/p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path ); |
91
|
|
|
$wp_styles->registered[ $handle ]->ver = null; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns cdn-able assets for core. |
99
|
|
|
* |
100
|
|
|
* @param null $version |
101
|
|
|
* @param null $locale |
102
|
|
|
* @return array|bool |
103
|
|
|
*/ |
104
|
|
|
public static function get_core_assets( $version = null, $locale = null ) { |
105
|
|
|
if ( empty( $version ) ) { |
106
|
|
|
$version = $GLOBALS['wp_version']; |
107
|
|
|
} |
108
|
|
|
if ( empty( $locale ) ) { |
109
|
|
|
$locale = get_locale(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$cache = Jetpack_Options::get_option( 'static_asset_cdn_files', array() ); |
113
|
|
|
if ( isset( $cache['core'][ $version ][ $locale ] ) ) { |
114
|
|
|
return $cache['core'][ $version ][ $locale ]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
require_once( ABSPATH . 'wp-admin/includes/update.php' ); |
118
|
|
|
$checksums = get_core_checksums( $version, $locale ); |
119
|
|
|
|
120
|
|
|
$return = array_filter( array_keys( $checksums ), array( __CLASS__, 'is_js_or_css_file' ) ); |
121
|
|
|
|
122
|
|
|
if ( ! isset( $cache['core'][ $version ] ) ) { |
123
|
|
|
$cache['core'] = array(); |
124
|
|
|
$cache['core'][ $version ] = array(); |
125
|
|
|
} |
126
|
|
|
$cache['core'][ $version ][ $locale ] = $return; |
127
|
|
|
Jetpack_Options::update_option( 'static_asset_cdn_files', $cache, true ); |
|
|
|
|
128
|
|
|
|
129
|
|
|
return $return; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns cdn-able assets for a given plugin. |
134
|
|
|
* |
135
|
|
|
* @param string $plugin |
136
|
|
|
* @param string $version |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
public static function get_plugin_assets( $plugin, $version ) { |
140
|
|
|
$cache = Jetpack_Options::get_option( 'static_asset_cdn_files', array() ); |
141
|
|
|
if ( isset( $cache[ $plugin ][ $version ] ) ) { |
142
|
|
|
return $cache[ $plugin ][ $version ]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$url = sprintf( 'http://downloads.wordpress.org/plugin-checksums/%s/%s.json', $plugin, $version ); |
146
|
|
|
|
147
|
|
|
if ( wp_http_supports( array( 'ssl' ) ) ) { |
148
|
|
|
$url = set_url_scheme( $url, 'https' ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$response = wp_remote_get( $url ); |
152
|
|
|
|
153
|
|
|
$body = trim( wp_remote_retrieve_body( $response ) ); |
154
|
|
|
$body = json_decode( $body, true ); |
155
|
|
|
|
156
|
|
|
$return = array_filter( array_keys( $body['files'] ), array( __CLASS__, 'is_js_or_css_file' ) ); |
157
|
|
|
|
158
|
|
|
$cache[ $plugin ] = array(); |
159
|
|
|
$cache[ $plugin ][ $version ] = $return; |
160
|
|
|
Jetpack_Options::update_option( 'static_asset_cdn_files', $cache, true ); |
|
|
|
|
161
|
|
|
|
162
|
|
|
return $return; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public static function is_js_or_css_file( $path ) { |
166
|
|
|
return in_array( substr( $path, -3 ), array( 'css', '.js' ) ); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
Jetpack_Photon_Static_Assets_CDN::go(); |
170
|
|
|
|
171
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.