Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure correct translation loading when loading assets via CDN #14797

Merged
merged 3 commits into from
Mar 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions modules/photon-cdn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -113,10 +114,33 @@ 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 );
}

// 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];
}

return $relative;
}

/**
* 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.
* @param string $domain The text domain.
* @return string The transformed local languages path.
*/
public static function fix_local_script_translation_path( $file, $handle, $domain ) {
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 substr( $src, 1 + $strpos );
return $file;
}

/**
Expand Down