From 376afed1ef9af221730fbd10159caa7c22ceac9a Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Tue, 25 Feb 2020 18:07:37 +0100 Subject: [PATCH 1/3] Ensure correct translation loading when loading assets via CDN --- modules/photon-cdn.php | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/modules/photon-cdn.php b/modules/photon-cdn.php index 697344bd80430..98ed1c815cfa2 100644 --- a/modules/photon-cdn.php +++ b/modules/photon-cdn.php @@ -31,6 +31,7 @@ public static function go() { add_action( 'admin_print_styles', array( __CLASS__, 'cdnize_assets' ) ); add_action( 'wp_footer', array( __CLASS__, 'cdnize_assets' ) ); add_filter( 'load_script_textdomain_relative_path', array( __CLASS__, 'fix_script_relative_path' ), 10, 2 ); + add_filter( 'load_script_translation_file', array( __CLASS__, 'fix_local_script_translation_path' ), 10, 3 ); } /** @@ -113,10 +114,32 @@ public static function fix_script_relative_path( $relative, $src ) { // We only treat URLs that have wp-includes in them. Cases like language textdomains // can also use this filter, they don't need to be touched because they are local paths. - if ( false === $strpos ) { - return $relative; + if ( false !== $strpos ) { + return substr( $src, 1 + $strpos ); + } + + // Reverse cdnize_plugin_assets for loading the path + if ( preg_match( '#' . preg_quote( self::CDN, '#' ) . 'p/[^/]+/[^/]+/(.*)$#', $src, $m ) ) { + return $m[1]; + } + + return $relative; + } + + /** + * Ensure use of the correct relative path when loading the JavaScript translation file. + * + * @param string $file The path that's going to be loaded. + * @param string $handle The script handle. + * @param string $domain The text domain. + * @return string The transformed local languages path. + */ + public static function fix_local_script_translation_path( $file, $handle, $domain ) { + switch ( $handle ) { + case 'jetpack-blocks-editor': + return str_replace( 'wp-content/languages/jetpack', 'wp-content/languages/plugins/jetpack', $file ); } - return substr( $src, 1 + $strpos ); + return $file; } /** From 2423a61ee60c8099f98edafcd14f3bee6160b482 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Wed, 26 Feb 2020 10:17:40 +0100 Subject: [PATCH 2/3] Improve local path rewriting --- modules/photon-cdn.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/modules/photon-cdn.php b/modules/photon-cdn.php index 98ed1c815cfa2..9c936cc773538 100644 --- a/modules/photon-cdn.php +++ b/modules/photon-cdn.php @@ -127,7 +127,7 @@ public static function fix_script_relative_path( $relative, $src ) { } /** - * Ensure use of the correct relative path when loading the JavaScript translation file. + * Ensure use of the correct local path when loading the JavaScript translation file for a CDN'ed asset. * * @param string $file The path that's going to be loaded. * @param string $handle The script handle. @@ -135,9 +135,10 @@ public static function fix_script_relative_path( $relative, $src ) { * @return string The transformed local languages path. */ public static function fix_local_script_translation_path( $file, $handle, $domain ) { - switch ( $handle ) { - case 'jetpack-blocks-editor': - return str_replace( 'wp-content/languages/jetpack', 'wp-content/languages/plugins/jetpack', $file ); + global $wp_scripts; + // This is a rewritten plugin URL, so load the language file from the plugins path. + if ( isset( $wp_scripts->registered[ $handle ] ) && wp_startswith( $wp_scripts->registered[ $handle ]->src, self::CDN . 'p' ) ) { + return WP_LANG_DIR . '/plugins/' . basename( $file ); } return $file; } From 434cf50c4698ee09064921b1b581cc22c6abd02a Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Wed, 26 Feb 2020 10:35:52 +0100 Subject: [PATCH 3/3] typo --- modules/photon-cdn.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/photon-cdn.php b/modules/photon-cdn.php index 9c936cc773538..27b8db1ada1b1 100644 --- a/modules/photon-cdn.php +++ b/modules/photon-cdn.php @@ -118,8 +118,8 @@ public static function fix_script_relative_path( $relative, $src ) { return substr( $src, 1 + $strpos ); } - // Reverse cdnize_plugin_assets for loading the path - if ( preg_match( '#' . preg_quote( self::CDN, '#' ) . 'p/[^/]+/[^/]+/(.*)$#', $src, $m ) ) { + // Get the local path from a URL which was CDN'ed by cdnize_plugin_assets(). + if ( preg_match( '#^' . preg_quote( self::CDN, '#' ) . 'p/[^/]+/[^/]+/(.*)$#', $src, $m ) ) { return $m[1]; }