Conditions | 16 |
Paths | 416 |
Total Lines | 60 |
Lines | 44 |
Ratio | 73.33 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
142 |