Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/modules
File: photon-cdn.php
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
[0] Fix | Delete
/**
[1] Fix | Delete
* Module Name: Asset CDN
[2] Fix | Delete
* Module Description: Serve static files like CSS and JS from Jetpack’s global CDN for faster load times.
[3] Fix | Delete
* Sort Order: 26
[4] Fix | Delete
* Recommendation Order: 1
[5] Fix | Delete
* First Introduced: 6.6
[6] Fix | Delete
* Requires Connection: No
[7] Fix | Delete
* Auto Activate: No
[8] Fix | Delete
* Module Tags: Photos and Videos, Appearance, Recommended
[9] Fix | Delete
* Feature: Recommended, Appearance
[10] Fix | Delete
* Additional Search Queries: site accelerator, accelerate, static, assets, javascript, css, files, performance, cdn, bandwidth, content delivery network, pagespeed, combine js, optimize css
[11] Fix | Delete
*
[12] Fix | Delete
* @package automattic/jetpack
[13] Fix | Delete
*/
[14] Fix | Delete
[15] Fix | Delete
use Automattic\Jetpack\Assets;
[16] Fix | Delete
[17] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[18] Fix | Delete
exit( 0 );
[19] Fix | Delete
}
[20] Fix | Delete
[21] Fix | Delete
$GLOBALS['concatenate_scripts'] = false; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
[22] Fix | Delete
[23] Fix | Delete
Assets::add_resource_hint( '//c0.wp.com', 'preconnect' );
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Asset CDN module main class file.
[27] Fix | Delete
*/
[28] Fix | Delete
class Jetpack_Photon_Static_Assets_CDN {
[29] Fix | Delete
const CDN = 'https://c0.wp.com/';
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Sets up action handlers needed for Jetpack CDN.
[33] Fix | Delete
*/
[34] Fix | Delete
public static function go() {
[35] Fix | Delete
add_action( 'wp_print_scripts', array( __CLASS__, 'cdnize_assets' ) );
[36] Fix | Delete
add_action( 'wp_print_styles', array( __CLASS__, 'cdnize_assets' ) );
[37] Fix | Delete
add_action( 'admin_print_scripts', array( __CLASS__, 'cdnize_assets' ) );
[38] Fix | Delete
add_action( 'admin_print_styles', array( __CLASS__, 'cdnize_assets' ) );
[39] Fix | Delete
add_action( 'wp_footer', array( __CLASS__, 'cdnize_assets' ) );
[40] Fix | Delete
add_filter( 'load_script_textdomain_relative_path', array( __CLASS__, 'fix_script_relative_path' ), 10, 2 );
[41] Fix | Delete
add_filter( 'load_script_translation_file', array( __CLASS__, 'fix_local_script_translation_path' ), 10, 3 );
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* Sets up CDN URLs for assets that are enqueued by the WordPress Core.
[46] Fix | Delete
*/
[47] Fix | Delete
public static function cdnize_assets() {
[48] Fix | Delete
global $wp_scripts, $wp_styles, $wp_version;
[49] Fix | Delete
[50] Fix | Delete
/*
[51] Fix | Delete
* Short-circuit if AMP since not relevant as custom JS is not allowed and CSS is inlined.
[52] Fix | Delete
* Note that it is not suitable to use the jetpack_force_disable_site_accelerator filter for this
[53] Fix | Delete
* because it will be applied before the wp action, which is the point at which the queried object
[54] Fix | Delete
* is available and we know whether the response will be AMP or not. This is particularly important
[55] Fix | Delete
* for AMP-first (native AMP) pages where there are no AMP-specific URLs.
[56] Fix | Delete
*/
[57] Fix | Delete
if ( class_exists( Jetpack_AMP_Support::class ) && Jetpack_AMP_Support::is_amp_request() ) {
[58] Fix | Delete
return;
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* Filters Jetpack CDN's Core version number and locale. Can be used to override the values
[63] Fix | Delete
* that Jetpack uses to retrieve assets. Expects the values to be returned in an array.
[64] Fix | Delete
*
[65] Fix | Delete
* @module photon-cdn
[66] Fix | Delete
*
[67] Fix | Delete
* @since 6.6.0
[68] Fix | Delete
*
[69] Fix | Delete
* @param array $values array( $version = core assets version, i.e. 4.9.8, $locale = desired locale )
[70] Fix | Delete
*/
[71] Fix | Delete
list( $version, $locale ) = apply_filters( // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
[72] Fix | Delete
'jetpack_cdn_core_version_and_locale',
[73] Fix | Delete
array( $wp_version, get_locale() )
[74] Fix | Delete
);
[75] Fix | Delete
[76] Fix | Delete
if ( self::is_public_version( $version ) ) {
[77] Fix | Delete
$site_url = trailingslashit( site_url() );
[78] Fix | Delete
if ( $wp_scripts instanceof WP_Scripts && is_array( $wp_scripts->registered ) ) {
[79] Fix | Delete
foreach ( $wp_scripts->registered as $handle => $thing ) {
[80] Fix | Delete
if ( wp_startswith( $thing->src, self::CDN ) ) {
[81] Fix | Delete
continue;
[82] Fix | Delete
}
[83] Fix | Delete
if ( ! is_string( $thing->src ) ) {
[84] Fix | Delete
continue;
[85] Fix | Delete
}
[86] Fix | Delete
$src = ltrim( str_replace( $site_url, '', $thing->src ), '/' );
[87] Fix | Delete
if ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ), true ) ) {
[88] Fix | Delete
$wp_scripts->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src );
[89] Fix | Delete
$wp_scripts->registered[ $handle ]->ver = null;
[90] Fix | Delete
}
[91] Fix | Delete
}
[92] Fix | Delete
}
[93] Fix | Delete
if ( $wp_styles instanceof WP_Styles && is_array( $wp_styles->registered ) ) {
[94] Fix | Delete
foreach ( $wp_styles->registered as $handle => $thing ) {
[95] Fix | Delete
if ( wp_startswith( $thing->src, self::CDN ) ) {
[96] Fix | Delete
continue;
[97] Fix | Delete
}
[98] Fix | Delete
if ( ! is_string( $thing->src ) ) {
[99] Fix | Delete
continue;
[100] Fix | Delete
}
[101] Fix | Delete
$src = ltrim( str_replace( $site_url, '', $thing->src ), '/' );
[102] Fix | Delete
if ( self::is_js_or_css_file( $src ) && in_array( substr( $src, 0, 9 ), array( 'wp-admin/', 'wp-includ' ), true ) ) {
[103] Fix | Delete
$wp_styles->registered[ $handle ]->src = sprintf( self::CDN . 'c/%1$s/%2$s', $version, $src );
[104] Fix | Delete
$wp_styles->registered[ $handle ]->ver = null;
[105] Fix | Delete
}
[106] Fix | Delete
}
[107] Fix | Delete
}
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
self::cdnize_plugin_assets( 'jetpack', JETPACK__VERSION );
[111] Fix | Delete
if ( class_exists( 'WooCommerce' ) ) {
[112] Fix | Delete
self::cdnize_plugin_assets( 'woocommerce', WC_VERSION );
[113] Fix | Delete
}
[114] Fix | Delete
}
[115] Fix | Delete
[116] Fix | Delete
/**
[117] Fix | Delete
* Ensure use of the correct relative path when determining the JavaScript file names.
[118] Fix | Delete
*
[119] Fix | Delete
* @param string $relative The relative path of the script. False if it could not be determined.
[120] Fix | Delete
* @param string $src The full source url of the script.
[121] Fix | Delete
* @return string The expected relative path for the CDN-ed URL.
[122] Fix | Delete
*/
[123] Fix | Delete
public static function fix_script_relative_path( $relative, $src ) {
[124] Fix | Delete
[125] Fix | Delete
// Note relevant in AMP responses. See note above.
[126] Fix | Delete
if ( class_exists( Jetpack_AMP_Support::class ) && Jetpack_AMP_Support::is_amp_request() ) {
[127] Fix | Delete
return $relative;
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
$strpos = strpos( $src, '/wp-includes/' );
[131] Fix | Delete
[132] Fix | Delete
// We only treat URLs that have wp-includes in them. Cases like language textdomains
[133] Fix | Delete
// can also use this filter, they don't need to be touched because they are local paths.
[134] Fix | Delete
if ( false !== $strpos ) {
[135] Fix | Delete
return substr( $src, 1 + $strpos );
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
// Get the local path from a URL which was CDN'ed by cdnize_plugin_assets().
[139] Fix | Delete
if ( preg_match( '#^' . preg_quote( self::CDN, '#' ) . 'p/[^/]+/[^/]+/(.*)$#', $src, $m ) ) {
[140] Fix | Delete
return $m[1];
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
return $relative;
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
/**
[147] Fix | Delete
* Ensure use of the correct local path when loading the JavaScript translation file for a CDN'ed asset.
[148] Fix | Delete
*
[149] Fix | Delete
* @param string|false $file Path to the translation file to load. False if there isn't one.
[150] Fix | Delete
* @param string $handle The script handle.
[151] Fix | Delete
* @param string $domain The text domain.
[152] Fix | Delete
*
[153] Fix | Delete
* @return string The transformed local languages path.
[154] Fix | Delete
*/
[155] Fix | Delete
public static function fix_local_script_translation_path( $file, $handle, $domain ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
[156] Fix | Delete
global $wp_scripts;
[157] Fix | Delete
[158] Fix | Delete
// This is a rewritten plugin URL, so load the language file from the plugins path.
[159] Fix | Delete
if ( $file && isset( $wp_scripts->registered[ $handle ] ) && wp_startswith( $wp_scripts->registered[ $handle ]->src, self::CDN . 'p' ) ) {
[160] Fix | Delete
return WP_LANG_DIR . '/plugins/' . basename( $file );
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
return $file;
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
/**
[167] Fix | Delete
* Sets up CDN URLs for supported plugin assets.
[168] Fix | Delete
*
[169] Fix | Delete
* @param String $plugin_slug plugin slug string.
[170] Fix | Delete
* @param String $current_version plugin version string.
[171] Fix | Delete
* @return null|bool
[172] Fix | Delete
*/
[173] Fix | Delete
public static function cdnize_plugin_assets( $plugin_slug, $current_version ) {
[174] Fix | Delete
global $wp_scripts, $wp_styles;
[175] Fix | Delete
[176] Fix | Delete
/**
[177] Fix | Delete
* Filters Jetpack CDN's plugin slug and version number. Can be used to override the values
[178] Fix | Delete
* that Jetpack uses to retrieve assets. For example, when testing a development version of Jetpack
[179] Fix | Delete
* the assets are not yet published, so you may need to override the version value to either
[180] Fix | Delete
* trunk, or the latest available version. Expects the values to be returned in an array.
[181] Fix | Delete
*
[182] Fix | Delete
* @module photon-cdn
[183] Fix | Delete
*
[184] Fix | Delete
* @since 6.6.0
[185] Fix | Delete
*
[186] Fix | Delete
* @param array $values array( $slug = the plugin repository slug, i.e. jetpack, $version = the plugin version, i.e. 6.6 )
[187] Fix | Delete
*/
[188] Fix | Delete
list( $plugin_slug, $current_version ) = apply_filters(
[189] Fix | Delete
'jetpack_cdn_plugin_slug_and_version',
[190] Fix | Delete
array( $plugin_slug, $current_version )
[191] Fix | Delete
);
[192] Fix | Delete
[193] Fix | Delete
$assets = self::get_plugin_assets( $plugin_slug, $current_version );
[194] Fix | Delete
$plugin_directory_url = plugins_url() . '/' . $plugin_slug . '/';
[195] Fix | Delete
[196] Fix | Delete
if ( is_wp_error( $assets ) || ! is_array( $assets ) ) {
[197] Fix | Delete
return false;
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
if ( $wp_scripts instanceof WP_Scripts && is_array( $wp_scripts->registered ) ) {
[201] Fix | Delete
foreach ( $wp_scripts->registered as $handle => $thing ) {
[202] Fix | Delete
if ( wp_startswith( $thing->src, self::CDN ) ) {
[203] Fix | Delete
continue;
[204] Fix | Delete
}
[205] Fix | Delete
if ( wp_startswith( $thing->src, $plugin_directory_url ) ) {
[206] Fix | Delete
$local_path = substr( $thing->src, strlen( $plugin_directory_url ) );
[207] Fix | Delete
if ( in_array( $local_path, $assets, true ) ) {
[208] Fix | Delete
$wp_scripts->registered[ $handle ]->src = sprintf( self::CDN . 'p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path );
[209] Fix | Delete
$wp_scripts->registered[ $handle ]->ver = null;
[210] Fix | Delete
}
[211] Fix | Delete
}
[212] Fix | Delete
}
[213] Fix | Delete
}
[214] Fix | Delete
if ( $wp_styles instanceof WP_Styles && is_array( $wp_styles->registered ) ) {
[215] Fix | Delete
foreach ( $wp_styles->registered as $handle => $thing ) {
[216] Fix | Delete
if ( wp_startswith( $thing->src, self::CDN ) ) {
[217] Fix | Delete
continue;
[218] Fix | Delete
}
[219] Fix | Delete
if ( wp_startswith( $thing->src, $plugin_directory_url ) ) {
[220] Fix | Delete
$local_path = substr( $thing->src, strlen( $plugin_directory_url ) );
[221] Fix | Delete
if ( in_array( $local_path, $assets, true ) ) {
[222] Fix | Delete
$wp_styles->registered[ $handle ]->src = sprintf( self::CDN . 'p/%1$s/%2$s/%3$s', $plugin_slug, $current_version, $local_path );
[223] Fix | Delete
$wp_styles->registered[ $handle ]->ver = null;
[224] Fix | Delete
}
[225] Fix | Delete
}
[226] Fix | Delete
}
[227] Fix | Delete
}
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
/**
[231] Fix | Delete
* Returns cdn-able assets for a given plugin.
[232] Fix | Delete
*
[233] Fix | Delete
* @param string $plugin plugin slug string.
[234] Fix | Delete
* @param string $version plugin version number string.
[235] Fix | Delete
* @return array|bool Will return false if not a public version.
[236] Fix | Delete
*/
[237] Fix | Delete
public static function get_plugin_assets( $plugin, $version ) {
[238] Fix | Delete
if ( 'jetpack' === $plugin && JETPACK__VERSION === $version ) {
[239] Fix | Delete
if ( ! self::is_public_version( $version ) || ! file_exists( JETPACK__PLUGIN_DIR . 'modules/photon-cdn/jetpack-manifest.php' ) ) {
[240] Fix | Delete
return false;
[241] Fix | Delete
}
[242] Fix | Delete
[243] Fix | Delete
$assets = array(); // The variable will be redefined in the included file.
[244] Fix | Delete
[245] Fix | Delete
include JETPACK__PLUGIN_DIR . 'modules/photon-cdn/jetpack-manifest.php';
[246] Fix | Delete
return $assets;
[247] Fix | Delete
}
[248] Fix | Delete
[249] Fix | Delete
/**
[250] Fix | Delete
* Used for other plugins to provide their bundled assets via filter to
[251] Fix | Delete
* prevent the need of storing them in an option or an external api request
[252] Fix | Delete
* to w.org.
[253] Fix | Delete
*
[254] Fix | Delete
* @module photon-cdn
[255] Fix | Delete
*
[256] Fix | Delete
* @since 6.6.0
[257] Fix | Delete
*
[258] Fix | Delete
* @param array $assets The assets array for the plugin.
[259] Fix | Delete
* @param string $version The version of the plugin being requested.
[260] Fix | Delete
*/
[261] Fix | Delete
$assets = apply_filters( "jetpack_cdn_plugin_assets-{$plugin}", null, $version ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
[262] Fix | Delete
if ( is_array( $assets ) ) {
[263] Fix | Delete
return $assets;
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
if ( ! self::is_public_version( $version ) ) {
[267] Fix | Delete
return false;
[268] Fix | Delete
}
[269] Fix | Delete
[270] Fix | Delete
$cache = Jetpack_Options::get_option( 'static_asset_cdn_files', array() );
[271] Fix | Delete
if ( isset( $cache[ $plugin ][ $version ] ) ) {
[272] Fix | Delete
if ( is_array( $cache[ $plugin ][ $version ] ) ) {
[273] Fix | Delete
return $cache[ $plugin ][ $version ];
[274] Fix | Delete
}
[275] Fix | Delete
if ( is_numeric( $cache[ $plugin ][ $version ] ) ) {
[276] Fix | Delete
// Cache an empty result for up to 24h.
[277] Fix | Delete
if ( (int) $cache[ $plugin ][ $version ] + DAY_IN_SECONDS > time() ) {
[278] Fix | Delete
return array();
[279] Fix | Delete
}
[280] Fix | Delete
}
[281] Fix | Delete
}
[282] Fix | Delete
[283] Fix | Delete
$url = sprintf( 'http://downloads.wordpress.org/plugin-checksums/%s/%s.json', $plugin, $version );
[284] Fix | Delete
[285] Fix | Delete
if ( wp_http_supports( array( 'ssl' ) ) ) {
[286] Fix | Delete
$url = set_url_scheme( $url, 'https' );
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
$response = wp_remote_get( $url );
[290] Fix | Delete
[291] Fix | Delete
$body = trim( wp_remote_retrieve_body( $response ) );
[292] Fix | Delete
$body = json_decode( $body, true );
[293] Fix | Delete
[294] Fix | Delete
$return = time();
[295] Fix | Delete
if ( is_array( $body ) ) {
[296] Fix | Delete
$return = array_filter( array_keys( $body['files'] ), array( __CLASS__, 'is_js_or_css_file' ) );
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
$cache[ $plugin ] = array();
[300] Fix | Delete
$cache[ $plugin ][ $version ] = $return;
[301] Fix | Delete
Jetpack_Options::update_option( 'static_asset_cdn_files', $cache, true );
[302] Fix | Delete
[303] Fix | Delete
return $return;
[304] Fix | Delete
}
[305] Fix | Delete
[306] Fix | Delete
/**
[307] Fix | Delete
* Checks a path whether it is a JS or CSS file.
[308] Fix | Delete
*
[309] Fix | Delete
* @param String $path file path.
[310] Fix | Delete
* @return Boolean whether the file is a JS or CSS.
[311] Fix | Delete
*/
[312] Fix | Delete
public static function is_js_or_css_file( $path ) {
[313] Fix | Delete
return ( ! str_contains( $path, '?' ) ) && in_array( substr( $path, -3 ), array( 'css', '.js' ), true );
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
/**
[317] Fix | Delete
* Checks whether the version string indicates a production version.
[318] Fix | Delete
*
[319] Fix | Delete
* @param String $version the version string.
[320] Fix | Delete
* @param Boolean $include_beta_and_rc whether to count beta and RC versions as production.
[321] Fix | Delete
* @return Boolean
[322] Fix | Delete
*/
[323] Fix | Delete
public static function is_public_version( $version, $include_beta_and_rc = false ) {
[324] Fix | Delete
if ( preg_match( '/^\d+(\.\d+)+$/', $version ) ) {
[325] Fix | Delete
/** Example matches: `1`, `1.2`, `1.2.3`. */
[326] Fix | Delete
return true;
[327] Fix | Delete
} elseif ( $include_beta_and_rc && preg_match( '/^\d+(\.\d+)+(-(beta|rc|pressable)\d?)$/i', $version ) ) {
[328] Fix | Delete
/** Example matches: `1.2.3`, `1.2.3-beta`, `1.2.3-pressable`, `1.2.3-beta1`, `1.2.3-rc`, `1.2.3-rc2`. */
[329] Fix | Delete
return true;
[330] Fix | Delete
}
[331] Fix | Delete
// Unrecognized version.
[332] Fix | Delete
return false;
[333] Fix | Delete
}
[334] Fix | Delete
}
[335] Fix | Delete
/**
[336] Fix | Delete
* Allow plugins to short-circuit the Asset CDN, even when the module is on.
[337] Fix | Delete
*
[338] Fix | Delete
* @module photon-cdn
[339] Fix | Delete
*
[340] Fix | Delete
* @since 6.7.0
[341] Fix | Delete
*
[342] Fix | Delete
* @param false bool Should the Asset CDN be blocked? False by default.
[343] Fix | Delete
*/
[344] Fix | Delete
if ( true !== apply_filters( 'jetpack_force_disable_site_accelerator', false ) ) {
[345] Fix | Delete
Jetpack_Photon_Static_Assets_CDN::go();
[346] Fix | Delete
}
[347] Fix | Delete
[348] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function