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 ( isset( $known_core_files[ $src ] ) ) { |
43
|
|
|
$wp_scripts->registered[ $handle ]->src = sprintf('https://c0.wp.com/core/%1$s/%2$s', $wp_version, $src ); |
44
|
|
|
wp_script_add_data( $handle, 'integrity', 'md5-' . base64_encode( $known_core_files[ $src ] ) ); |
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 ( isset( $known_core_files[ $src ] ) ) { |
53
|
|
|
$wp_styles->registered[ $handle ]->src = sprintf('https://c0.wp.com/core/%1$s/%2$s', $wp_version, $src ); |
54
|
|
|
wp_style_add_data( $handle, 'integrity', 'md5-' . base64_encode( $known_core_files[ $src ] ) ); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$jetpack_version = JETPACK__VERSION; |
59
|
|
|
|
60
|
|
|
// If Jetpack is running a known version that we have the assets CDN'd for... |
61
|
|
|
// @todo: Abstract this out to make it easy to run for multiple plugins, like WooCommerce. |
62
|
|
|
if ( in_array( $jetpack_version, self::get_plugin_versions( 'jetpack' ) ) ) { |
63
|
|
|
$jetpack_asset_hashes = self::get_plugin_checksums( $jetpack_version, 'jetpack' ); |
64
|
|
|
$jetpack_directory_url = plugins_url( '/', JETPACK__PLUGIN_FILE ); |
65
|
|
|
|
66
|
|
View Code Duplication |
foreach ( $wp_scripts->registered as $handle => $thing ) { |
67
|
|
|
if ( wp_startswith( $thing->src, 'https://c0.wp.com/' ) ) { |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
if ( wp_startswith( $thing->src, $jetpack_directory_url ) ) { |
71
|
|
|
$local_path = substr( $thing->src, strlen( $jetpack_directory_url ) ); |
72
|
|
|
if ( isset( $jetpack_asset_hashes[ $local_path ] ) ) { |
73
|
|
|
$wp_scripts->registered[ $handle ]->src = sprintf('https://c0.wp.com/p/jetpack/%1$s/%2$s', $jetpack_version, $local_path ); |
74
|
|
|
wp_script_add_data( $handle, 'integrity', 'sha256-' . base64_encode( $jetpack_asset_hashes[ $local_path ] ) ); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
View Code Duplication |
foreach ( $wp_styles->registered as $handle => $thing ) { |
79
|
|
|
if ( wp_startswith( $thing->src, 'https://c0.wp.com/' ) ) { |
80
|
|
|
continue; |
81
|
|
|
} |
82
|
|
|
if ( wp_startswith( $thing->src, $jetpack_directory_url ) ) { |
83
|
|
|
$local_path = substr( $thing->src, strlen( $jetpack_directory_url ) ); |
84
|
|
|
if ( isset( $jetpack_asset_hashes[ $local_path ] ) ) { |
85
|
|
|
$wp_styles->registered[ $handle ]->src = sprintf('https://c0.wp.com/p/jetpack/%1$s/%2$s', $jetpack_version, $local_path ); |
86
|
|
|
wp_style_add_data( $handle, 'integrity', 'sha256-' . base64_encode( $jetpack_asset_hashes[ $local_path ] ) ); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public static function get_core_checksums( $version = null, $locale = null ) { |
94
|
|
|
if ( empty( $version ) ) { |
95
|
|
|
$version = $GLOBALS['wp_version']; |
96
|
|
|
} |
97
|
|
|
if ( empty( $locale ) ) { |
98
|
|
|
$locale = get_locale(); |
99
|
|
|
} |
100
|
|
|
require_once( ABSPATH . 'wp-admin/includes/update.php' ); |
101
|
|
|
return get_core_checksums( $version, $locale ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public static function get_plugin_versions( $plugin = 'jetpack' ) { |
105
|
|
|
$response = wp_remote_get( sprintf( 'https://api.wordpress.org/plugins/info/1.0/%s.json', esc_url( $plugin ) ) ); |
106
|
|
|
$body = trim( wp_remote_retrieve_body( $response ) ); |
107
|
|
|
$body = json_decode( $body, true ); |
108
|
|
|
return array_keys( $body['versions'] ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns SHA-256 checksums |
113
|
|
|
*/ |
114
|
|
|
public static function get_plugin_checksums( $version = null, $plugin = 'jetpack' ) { |
115
|
|
|
if ( empty( $version ) ) { |
116
|
|
|
$version = JETPACK__VERSION; |
117
|
|
|
} |
118
|
|
|
$url = sprintf( 'http://downloads.wordpress.org/plugin-checksums/%s/%s.json', $plugin, $version ); |
119
|
|
|
|
120
|
|
|
if ( wp_http_supports( array( 'ssl' ) ) ) { |
121
|
|
|
$url = set_url_scheme( $url, 'https' ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$response = wp_remote_get( $url ); |
125
|
|
|
|
126
|
|
|
$body = trim( wp_remote_retrieve_body( $response ) ); |
127
|
|
|
$body = json_decode( $body, true ); |
128
|
|
|
|
129
|
|
|
$return = array(); |
130
|
|
|
|
131
|
|
|
foreach ( $body['files'] as $file => $hashes ) { |
132
|
|
|
if ( in_array( substr( $file, -3 ), array( 'css', '.js' ) ) ) { |
133
|
|
|
$return[ $file ] = $hashes['sha256']; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $return; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
Jetpack_Photon_Static_Assets_CDN::go(); |
141
|
|
|
|
142
|
|
|
|