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_head', array( __CLASS__, 'cdnize_assets' ) ); |
28
|
|
|
add_action( 'wp_footer', array( __CLASS__, 'cdnize_assets' ) ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function cdnize_assets() { |
32
|
|
|
global $wp_scripts, $wp_styles; |
33
|
|
|
|
34
|
|
|
// $known_core_files = self::get_core_checksums(); |
35
|
|
|
|
36
|
|
|
$jetpack_asset_hashes = self::get_jetpack_checksums(); |
37
|
|
|
$jetpack_directory_url = plugins_url( '/', JETPACK__PLUGIN_FILE ); |
38
|
|
|
|
39
|
|
|
foreach ( $wp_scripts->registered as $handle => $thing ) { |
40
|
|
|
if ( wp_startswith( $thing->src, $jetpack_directory_url ) ) { |
41
|
|
|
$local_path = substr( $thing->src, strlen( $jetpack_directory_url ) ); |
42
|
|
|
if ( isset( $jetpack_asset_hashes[ $local_path ] ) ) { |
43
|
|
|
$wp_scripts->registered[ $handle ]->src = sprintf( 'https://c0.wp.com/p/jetpack/%1$s/%2$s', JETPACK__VERSION, $local_path ); |
44
|
|
|
wp_script_add_data( $handle, 'integrity', 'sha256-' . base64_encode( $jetpack_asset_hashes[ $local_path ] ) ); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function get_core_checksums() { |
51
|
|
|
global $wp_version; |
52
|
|
|
require_once( ABSPATH . 'wp-admin/includes/update.php' ); |
53
|
|
|
return get_core_checksums( $wp_version, get_locale() ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns SHA-256 checksums |
58
|
|
|
*/ |
59
|
|
|
public static function get_jetpack_checksums() { |
60
|
|
|
$url = sprintf( 'http://downloads.wordpress.org/plugin-checksums/jetpack/%s.json', '6.4.2' /* JETPACK__VERSION */ ); |
61
|
|
|
|
62
|
|
|
if ( wp_http_supports( array( 'ssl' ) ) ) { |
63
|
|
|
$url = set_url_scheme( $url, 'https' ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$response = wp_remote_get( $url ); |
67
|
|
|
|
68
|
|
|
$body = trim( wp_remote_retrieve_body( $response ) ); |
69
|
|
|
$body = json_decode( $body, true ); |
70
|
|
|
|
71
|
|
|
$return = array(); |
72
|
|
|
|
73
|
|
|
foreach ( $body['files'] as $file => $hashes ) { |
74
|
|
|
if ( in_array( substr( $file, -3 ), array( 'css', '.js' ) ) ) { |
75
|
|
|
$return[ $file ] = $hashes['sha256']; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $return; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
Jetpack_Photon_Static_Assets_CDN::go(); |
83
|
|
|
|
84
|
|
|
|