Completed
Push — feature/assets-cdn ( 88f413...dcae6a )
by George
15:52 queued 08:55
created

Jetpack_Photon_Static_Assets_CDN::cdnize_assets()   B

Complexity

Conditions 10
Paths 26

Size

Total Lines 38

Duplication

Lines 24
Ratio 63.16 %

Importance

Changes 0
Metric Value
cc 10
nc 26
nop 0
dl 24
loc 38
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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