Fix for translation chunks not generated on plugin activation (https://github.com/woocommerce/woocommerce-admin/pull/6028)
* Fix for translation chunks not generated on plugin activation * Add support for woocommerce_activated_plugin hook to work as embedded package * Update readme.txt * Make sure woocommerce/woocommerce-admin#5062 does not regress by adding checks before accessing files * Consolidate duplicated codes
This commit is contained in:
parent
52197af57e
commit
e8e791dc0b
|
@ -94,7 +94,7 @@ Release and roadmap notes are available on the [WooCommerce Developers Blog](htt
|
||||||
- Add: Note for users coming from Calypso. #6030
|
- Add: Note for users coming from Calypso. #6030
|
||||||
- Enhancement: Add an "unread" indicator to inbox messages. #6047
|
- Enhancement: Add an "unread" indicator to inbox messages. #6047
|
||||||
- Tweak: update the content for the ChooseNiche note. #6048
|
- Tweak: update the content for the ChooseNiche note. #6048
|
||||||
|
- Fix: Generate JSON translation chunks on plugin activation #6028
|
||||||
|
|
||||||
== Changelog ==
|
== Changelog ==
|
||||||
|
|
||||||
|
|
|
@ -98,6 +98,25 @@ class Loader {
|
||||||
|
|
||||||
// Combine JSON translation files (from chunks) when language packs are updated.
|
// Combine JSON translation files (from chunks) when language packs are updated.
|
||||||
add_action( 'upgrader_process_complete', array( __CLASS__, 'combine_translation_chunk_files' ), 10, 2 );
|
add_action( 'upgrader_process_complete', array( __CLASS__, 'combine_translation_chunk_files' ), 10, 2 );
|
||||||
|
|
||||||
|
// Handler for WooCommerce and WooCommerce Admin plugin activation.
|
||||||
|
add_action( 'woocommerce_activated_plugin', array( __CLASS__, 'activated_plugin' ) );
|
||||||
|
add_action( 'activated_plugin', array( __CLASS__, 'activated_plugin' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run when plugin is activated (can be WooCommerce or WooCommerce Admin).
|
||||||
|
*
|
||||||
|
* @param string $filename Activated plugin filename.
|
||||||
|
*/
|
||||||
|
public static function activated_plugin( $filename ) {
|
||||||
|
$plugin_domain = explode( '/', plugin_basename( __FILE__ ) )[0];
|
||||||
|
$activated_plugin_domain = explode( '/', $filename )[0];
|
||||||
|
|
||||||
|
// Ensure we're only running only on activation hook that originates from our plugin.
|
||||||
|
if ( $plugin_domain === $activated_plugin_domain ) {
|
||||||
|
self::generate_translation_strings();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -665,6 +684,63 @@ class Loader {
|
||||||
return $combined_translation_data;
|
return $combined_translation_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Combine translation chunks when plugin is activated.
|
||||||
|
*
|
||||||
|
* This function combines JSON translation data auto-extracted by GlotPress
|
||||||
|
* from Webpack-generated JS chunks into a single file. This is necessary
|
||||||
|
* since the JS chunks are not known to WordPress via wp_register_script()
|
||||||
|
* and wp_set_script_translations().
|
||||||
|
*/
|
||||||
|
public static function generate_translation_strings() {
|
||||||
|
$plugin_domain = explode( '/', plugin_basename( __FILE__ ) )[0];
|
||||||
|
$locale = determine_locale();
|
||||||
|
$lang_dir = WP_LANG_DIR . '/plugins/';
|
||||||
|
|
||||||
|
// Bail early if not localized.
|
||||||
|
if ( 'en_US' === $locale ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! function_exists( 'get_filesystem_method' ) ) {
|
||||||
|
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
$access_type = get_filesystem_method();
|
||||||
|
if ( 'direct' === $access_type ) {
|
||||||
|
\WP_Filesystem();
|
||||||
|
self::build_and_save_translations( $lang_dir, $plugin_domain, $locale );
|
||||||
|
} else {
|
||||||
|
// I'm reluctant to add support for other filesystems here as it would require
|
||||||
|
// user's input on activating plugin - which I don't think is common.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Combine and save translations for a specific locale.
|
||||||
|
*
|
||||||
|
* Note that this assumes \WP_Filesystem is already initialized with write access.
|
||||||
|
*
|
||||||
|
* @param string $language_dir Path to language files.
|
||||||
|
* @param string $plugin_domain Text domain.
|
||||||
|
* @param string $locale Locale being retrieved.
|
||||||
|
*/
|
||||||
|
public static function build_and_save_translations( $language_dir, $plugin_domain, $locale ) {
|
||||||
|
global $wp_filesystem;
|
||||||
|
$translations_from_chunks = self::get_translation_chunk_data( $language_dir, $plugin_domain, $locale );
|
||||||
|
|
||||||
|
if ( empty( $translations_from_chunks ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$cache_filename = self::get_combined_translation_filename( $plugin_domain, $locale );
|
||||||
|
$chunk_translations_json = wp_json_encode( $translations_from_chunks );
|
||||||
|
|
||||||
|
// Cache combined translations strings to a file.
|
||||||
|
$wp_filesystem->put_contents( $language_dir . $cache_filename, $chunk_translations_json );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Combine translation chunks when files are updated.
|
* Combine translation chunks when files are updated.
|
||||||
*
|
*
|
||||||
|
@ -677,10 +753,6 @@ class Loader {
|
||||||
* @param array $hook_extra Info about the upgraded language packs.
|
* @param array $hook_extra Info about the upgraded language packs.
|
||||||
*/
|
*/
|
||||||
public static function combine_translation_chunk_files( $instance, $hook_extra ) {
|
public static function combine_translation_chunk_files( $instance, $hook_extra ) {
|
||||||
// So long as this function is hooked to the 'upgrader_process_complete' action,
|
|
||||||
// the filesystem object should be hooked up.
|
|
||||||
global $wp_filesystem;
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
! is_a( $instance, 'Language_Pack_Upgrader' ) ||
|
! is_a( $instance, 'Language_Pack_Upgrader' ) ||
|
||||||
! isset( $hook_extra['translations'] ) ||
|
! isset( $hook_extra['translations'] ) ||
|
||||||
|
@ -706,17 +778,9 @@ class Loader {
|
||||||
|
|
||||||
// Build combined translation files for all updated locales.
|
// Build combined translation files for all updated locales.
|
||||||
foreach ( $locales as $locale ) {
|
foreach ( $locales as $locale ) {
|
||||||
$translations_from_chunks = self::get_translation_chunk_data( $language_dir, $plugin_domain, $locale );
|
// So long as this function is hooked to the 'upgrader_process_complete' action,
|
||||||
|
// WP_Filesystem should be hooked up to be able to call build_and_save_translations.
|
||||||
if ( empty( $translations_from_chunks ) ) {
|
self::build_and_save_translations( $language_dir, $plugin_domain, $locale );
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$cache_filename = self::get_combined_translation_filename( $plugin_domain, $locale );
|
|
||||||
$chunk_translations_json = wp_json_encode( $translations_from_chunks );
|
|
||||||
|
|
||||||
// Cache combined translations strings to a file.
|
|
||||||
$wp_filesystem->put_contents( $language_dir . $cache_filename, $chunk_translations_json );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue