_data ) ) { continue; } $reference_file = $chunk_data['source']; // Only combine "app" files (not scripts registered with WP). if ( false === strpos( $reference_file, WC_ADMIN_DIST_JS_FOLDER . 'app/index.js' ) && false === strpos( $reference_file, WC_ADMIN_DIST_JS_FOLDER . 'chunks/' ) ) { continue; } if ( empty( $combined_translation_data ) ) { // Use the first translation file as the base structure. $combined_translation_data = $chunk_data; } else { // Combine all messages from all chunk files. $combined_translation_data['locale_data']['woocommerce'] = array_merge( $combined_translation_data['locale_data']['woocommerce'], $chunk_data['locale_data']['woocommerce'] ); } } // Remove inaccurate reference comment. unset( $combined_translation_data['source'] ); return $combined_translation_data; } /** * Find and combine translation chunk files. * * Only targets files that aren't represented by a registered script (e.g. not passed to wp_register_script()). * * @param string $lang_dir Path to language files. * @param string $domain Text domain. * @param string $locale Locale being retrieved. * @return array Combined translation chunk data. */ private function get_translation_chunk_data( $lang_dir, $domain, $locale ) { // So long as this function is called during the 'upgrader_process_complete' action, // the filesystem object should be hooked up. global $wp_filesystem; // Grab all JSON files in the current language pack. $json_i18n_filenames = glob( $lang_dir . $domain . '-' . $locale . '-*.json' ); $combined_translation_data = array(); if ( false === $json_i18n_filenames ) { return $combined_translation_data; } // Use first JSON file to determine file format. This check is required due to // file format difference between official language files and user translated files. $format_determine_file = reset( $json_i18n_filenames ); if ( ! $wp_filesystem->is_readable( $format_determine_file ) ) { return $combined_translation_data; } $file_contents = $wp_filesystem->get_contents( $format_determine_file ); $format_determine_data = \json_decode( $file_contents, true ); if ( empty( $format_determine_data ) ) { return $combined_translation_data; } if ( isset( $format_determine_data['comment'] ) ) { return $this->combine_official_translation_chunks( $json_i18n_filenames ); } elseif ( isset( $format_determine_data['source'] ) ) { return $this->combine_user_translation_chunks( $json_i18n_filenames ); } else { return $combined_translation_data; } } /** * 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. */ private function build_and_save_translations( $language_dir, $plugin_domain, $locale ) { global $wp_filesystem; $translations_from_chunks = $this->get_translation_chunk_data( $language_dir, $plugin_domain, $locale ); if ( empty( $translations_from_chunks ) ) { return; } $cache_filename = $this->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 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(). */ private 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(); $this->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; } } /** * Loads the required translation scripts on the correct pages. */ public function potentially_load_translation_script_file() { if ( ! PageController::is_admin_or_embed_page() ) { return; } // Grab translation strings from Webpack-generated chunks. add_filter( 'load_script_translation_file', array( $this, 'load_script_translation_file' ), 10, 3 ); } /** * Load translation strings from language packs for dynamic imports. * * @param string $file File location for the script being translated. * @param string $handle Script handle. * @param string $domain Text domain. * * @return string New file location for the script being translated. */ public function load_script_translation_file( $file, $handle, $domain ) { // Make sure the main app script is being loaded. if ( WC_ADMIN_APP !== $handle ) { return $file; } // Make sure we're handing the correct domain (could be woocommerce or woocommerce-admin). $plugin_domain = explode( '/', plugin_basename( __FILE__ ) )[0]; if ( $plugin_domain !== $domain ) { return $file; } $locale = determine_locale(); $cache_filename = $this->get_combined_translation_filename( $domain, $locale ); return WP_LANG_DIR . '/plugins/' . $cache_filename; } /** * Run when plugin is activated (can be WooCommerce or WooCommerce Admin). * * @param string $filename Activated plugin filename. */ public function potentially_generate_translation_strings( $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 ) { $this->generate_translation_strings(); } } /** * Combine translation chunks when files are updated. * * This function combines JSON translation data auto-extracted by GlotPress * from Webpack-generated JS chunks into a single file that can be used in * subsequent requests. This is necessary since the JS chunks are not known * to WordPress via wp_register_script() and wp_set_script_translations(). * * @param Language_Pack_Upgrader $instance Upgrader instance. * @param array $hook_extra Info about the upgraded language packs. */ public function combine_translation_chunk_files( $instance, $hook_extra ) { if ( ! is_a( $instance, 'Language_Pack_Upgrader' ) || ! isset( $hook_extra['translations'] ) || ! is_array( $hook_extra['translations'] ) ) { return; } // Make sure we're handing the correct domain (could be woocommerce or woocommerce-admin). $plugin_domain = explode( '/', plugin_basename( __FILE__ ) )[0]; $locales = array(); $language_dir = WP_LANG_DIR . '/plugins/'; // Gather the locales that were updated in this operation. foreach ( $hook_extra['translations'] as $translation ) { if ( 'plugin' === $translation['type'] && $plugin_domain === $translation['slug'] ) { $locales[] = $translation['language']; } } // Build combined translation files for all updated locales. foreach ( $locales as $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. $this->build_and_save_translations( $language_dir, $plugin_domain, $locale ); } } }
Fatal error: Uncaught Error: Class "Automattic\WooCommerce\Internal\Admin\Translations" not found in /htdocs/wp-content/plugins/woocommerce/src/Internal/Admin/Loader.php:67 Stack trace: #0 /htdocs/wp-content/plugins/woocommerce/src/Internal/Admin/Loader.php(55): Automattic\WooCommerce\Internal\Admin\Loader->__construct() #1 /htdocs/wp-content/plugins/woocommerce/src/Internal/Admin/FeaturePlugin.php(192): Automattic\WooCommerce\Internal\Admin\Loader::get_instance() #2 /htdocs/wp-content/plugins/woocommerce/src/Internal/Admin/FeaturePlugin.php(97): Automattic\WooCommerce\Internal\Admin\FeaturePlugin->hooks() #3 /htdocs/wp-content/plugins/woocommerce/src/Internal/Admin/FeaturePlugin.php(81): Automattic\WooCommerce\Internal\Admin\FeaturePlugin->on_plugins_loaded() #4 /htdocs/wp-content/plugins/woocommerce/src/Admin/Composer/Package.php(65): Automattic\WooCommerce\Internal\Admin\FeaturePlugin->init() #5 [internal function]: Automattic\WooCommerce\Admin\Composer\Package::init() #6 /htdocs/wp-content/plugins/woocommerce/src/Packages.php(128): call_user_func(Array) #7 /htdocs/wp-content/plugins/woocommerce/src/Packages.php(64): Automattic\WooCommerce\Packages::initialize_packages() #8 /htdocs/wp-includes/class-wp-hook.php(324): Automattic\WooCommerce\Packages::on_init('') #9 /htdocs/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array) #10 /htdocs/wp-includes/plugin.php(517): WP_Hook->do_action(Array) #11 /htdocs/wp-settings.php(559): do_action('plugins_loaded') #12 /htdocs/wp-config.php(85): require_once('/htdocs/wp-sett...') #13 /htdocs/wp-load.php(50): require_once('/htdocs/wp-conf...') #14 /htdocs/wp-blog-header.php(13): require_once('/htdocs/wp-load...') #15 /htdocs/index.php(17): require('/htdocs/wp-blog...') #16 {main} thrown in /htdocs/wp-content/plugins/woocommerce/src/Internal/Admin/Loader.php on line 67