Skip to content

Instantly share code, notes, and snippets.

@adamsilverstein
Created October 30, 2020 15:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamsilverstein/3eee29ea1370d0e119bb929a331e9f50 to your computer and use it in GitHub Desktop.
Save adamsilverstein/3eee29ea1370d0e119bb929a331e9f50 to your computer and use it in GitHub Desktop.
<?php
/**
* Ensures Site Kit loads languages correctly.
*
* @author Adam Silverstein, Google
* @license Apache 2.0
* @copyright 2020 Google Inc.
*
* @wordpress-plugin
* Plugin Name: Site Kit Language Loader
* Description: Ensures Site Kit loads languages correctly.
* Version: 1.0.0
* Author: Adam Silverstein, Google
* License: Apache License 2.0
* License URI: https://www.apache.org/licenses/LICENSE-2.0
*/
add_action( 'plugins_loaded' , function() {
add_filter(
'load_script_textdomain_relative_path',
function( $relative, $src ) {
$rel = find_script_by_src( $src );
if ( $rel ) {
return 'dist/assets/js/' . $rel;
}
return $relative;
}, 10, 2
);
} );
function find_script_by_src( $src ) {
$assets = array(
"googlesitekit-vendor" => "googlesitekit-vendor.7f2f98d6cb7f1a6a3ca1.js",
"chunk-googlesitekit-adminbar~chunk-googlesitekit-setup-wizard-proxy" => "chunk-googlesitekit-adminbar~chunk-googlesitekit-setup-wizard-proxy-39bf2f5072bd0d1cdc96.js",
"chunk-googlesitekit-adminbar" => "chunk-googlesitekit-adminbar-0ef0a2edcaa68b6bf889.js",
"chunk-googlesitekit-setup-wizard" => "chunk-googlesitekit-setup-wizard-b0f9d8b73c453976e6a9.js",
"chunk-googlesitekit-setup-wizard-proxy" => "chunk-googlesitekit-setup-wizard-proxy-8d48e04604d76303faab.js",
"chunk-googlesitekit-setup-wrapper" => "chunk-googlesitekit-setup-wrapper-b9314dcca2640fc8a733.js",
"googlesitekit-activation" => "googlesitekit-activation.de7352254b7eb5c2e9b4.js",
"googlesitekit-adminbar-loader" => "googlesitekit-adminbar-loader.f74a1f6f0ee0050978a7.js",
"googlesitekit-api" => "googlesitekit-api.75cd39617a79d1c3894c.js",
"googlesitekit-base" => "googlesitekit-base.45fd96d13d7b9aba817e.js",
"googlesitekit-dashboard" => "googlesitekit-dashboard.41176eb144adcaf84380.js",
"googlesitekit-dashboard-details" => "googlesitekit-dashboard-details.b64fe3da0aa6e3ea7b34.js",
"googlesitekit-dashboard-splash" => "googlesitekit-dashboard-splash.329f8f4845069dcaca2e.js",
"googlesitekit-data" => "googlesitekit-data.3cf4912774bcdfe610b6.js",
"googlesitekit-datastore-forms" => "googlesitekit-datastore-forms.da48f0fdbca81be0df27.js",
"googlesitekit-datastore-site" => "googlesitekit-datastore-site.6aa153ca7f37808c3fa5.js",
"googlesitekit-datastore-user" => "googlesitekit-datastore-user.d1aaf50869324aa5849f.js",
"googlesitekit-module" => "googlesitekit-module.6f77021725498ecbdbfe.js",
"googlesitekit-modules" => "googlesitekit-modules.eeb37a29652bc73560c7.js",
"googlesitekit-modules-adsense" => "googlesitekit-modules-adsense.1079e1f43b37bd5458ae.js",
"googlesitekit-modules-analytics" => "googlesitekit-modules-analytics.8c9e943ed260ef398943.js",
"googlesitekit-modules-optimize" => "googlesitekit-modules-optimize.75e073afd46873c251ec.js",
"googlesitekit-modules-pagespeed-insights" => "googlesitekit-modules-pagespeed-insights.a2dfd08f7a775bfdac80.js",
"googlesitekit-modules-search-console" => "googlesitekit-modules-search-console.b6558d4b1f24fdec2b23.js",
"googlesitekit-modules-tagmanager" => "googlesitekit-modules-tagmanager.518bfe25b0a944312e93.js",
"googlesitekit-settings" => "googlesitekit-settings.b010fdef904b76a7b333.js",
"googlesitekit-user-input" => "googlesitekit-user-input.6fc048dcbe8eb371025e.js",
"googlesitekit-widgets" => "googlesitekit-widgets.c62195b08a5b0a665106.js",
"googlesitekit-wp-dashboard" => "googlesitekit-wp-dashboard.1b7af0a71c2a6cab2e8f.js",
"pagead2.ads" => "pagead2.ads.1098a210f4359760dc87.js",
"vendors~chunk-googlesitekit-adminbar" => "vendors~chunk-googlesitekit-adminbar-7a072713532ba6a23f7d.js",
"vendors~chunk-googlesitekit-setup-wizard-proxy" => "vendors~chunk-googlesitekit-setup-wizard-proxy-63f24f08ad3c2ca81616.js",
);
global $wp_scripts;
$clear_test = preg_replace( '/.[^.]*\..[^.]*$/', '', $src );
foreach( $wp_scripts->registered as $reg ) {
// remove HASH.js from file paths.
$clear_src = preg_replace( '/.[^.]*\..[^.]*$/', '', $reg->src );
if ( $clear_src === $clear_test ) {
if ( $assets[ $reg->handle ] ) {
return $assets[ $reg->handle ];
}
}
}
return false;
}
@felixarntz
Copy link

@adamsilverstein Can you update this for the latest 1.20.0 release please?

	array(
		"googlesitekit-vendor" => "googlesitekit-vendor.e83b9bd244c78c4132c2.js",
		"googlesitekit-activation" => "googlesitekit-activation.173d01c116942791be20.js",
		"googlesitekit-adminbar" => "googlesitekit-adminbar.45d91a863eee1a9cf73b.js",
		"googlesitekit-api" => "googlesitekit-api.ab5c4f3a29589c0a79d4.js",
		"googlesitekit-base" => "googlesitekit-base.3d1779443c21b0a3bb5e.js",
		"googlesitekit-dashboard" => "googlesitekit-dashboard.8374a71cc8f439ca1dad.js",
		"googlesitekit-dashboard-details" => "googlesitekit-dashboard-details.550792e3101137d4f96b.js",
		"googlesitekit-dashboard-splash" => "googlesitekit-dashboard-splash.471731318d5ab3dd6fa3.js",
		"googlesitekit-data" => "googlesitekit-data.bb8e8f68a057b3d0cdf6.js",
		"googlesitekit-datastore-forms" => "googlesitekit-datastore-forms.6336c417cd64e00c1f41.js",
		"googlesitekit-datastore-site" => "googlesitekit-datastore-site.a57eec9620817b1f3ca1.js",
		"googlesitekit-datastore-user" => "googlesitekit-datastore-user.90b10f3d64e35f0a0734.js",
		"googlesitekit-module" => "googlesitekit-module.1de3128126ff4eb39c26.js",
		"googlesitekit-modules" => "googlesitekit-modules.dca68fc2dbcec3ef4fc2.js",
		"googlesitekit-modules-adsense" => "googlesitekit-modules-adsense.7855e733e9dfc511a980.js",
		"googlesitekit-modules-analytics" => "googlesitekit-modules-analytics.dddf6a327d2b0e2eb2c9.js",
		"googlesitekit-modules-optimize" => "googlesitekit-modules-optimize.fcc42322e8efa578c2c4.js",
		"googlesitekit-modules-pagespeed-insights" => "googlesitekit-modules-pagespeed-insights.a3dbf11ed1277355b334.js",
		"googlesitekit-modules-search-console" => "googlesitekit-modules-search-console.28e27a057191b7d632a3.js",
		"googlesitekit-modules-tagmanager" => "googlesitekit-modules-tagmanager.ab3313d24d726e368ccd.js",
		"googlesitekit-settings" => "googlesitekit-settings.08e988e1f939c10ec910.js",
		"googlesitekit-user-input" => "googlesitekit-user-input.a1b8fc80e7c0ca5a669f.js",
		"googlesitekit-widgets" => "googlesitekit-widgets.54b53f74e461092c8e42.js",
		"googlesitekit-wp-dashboard" => "googlesitekit-wp-dashboard.c5ade2a5b7e6d9157023.js",
		"pagead2.ads" => "pagead2.ads.db62f47245f6a1bbf2b2.js",
	)

May be worth to put both in there and allow to rely on different version translations, but we're probably not gonna maintain this and just use it for current QA, so that should be fine. If we want a long-term solution, we'll need to improve this so that it pulls this information from the plugin repository SVN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment