Changeset 54669
- Timestamp:
- 10/24/2022 10:01:01 AM (2 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-textdomain-registry.php
r54133 r54669 34 34 35 35 /** 36 * List of domains and their custom language directory paths. 37 * 38 * @see load_plugin_textdomain() 39 * @see load_theme_textdomain() 40 * 41 * @since 6.1.0 42 * 43 * @var array 44 */ 45 protected $custom_paths = array(); 46 47 /** 36 48 * Holds a cached list of available .mo files to improve performance. 37 49 * … … 43 55 44 56 /** 45 * Returns the MO filepath for a specific domain and locale.57 * Returns the languages directory path for a specific domain and locale. 46 58 * 47 59 * @since 6.1.0 … … 63 75 * Determines whether any MO file paths are available for the domain. 64 76 * 77 * This is the case if a path has been set for the current locale, 78 * or if there is no information stored yet, in which case 79 * {@see _load_textdomain_just_in_time()} will fetch the information first. 80 * 65 81 * @since 6.1.0 66 82 * … … 69 85 */ 70 86 public function has( $domain ) { 71 return ! empty( $this-> all[ $domain ] );87 return ! empty( $this->current[ $domain ] ) || empty( $this->all[ $domain ] ); 72 88 } 73 89 74 90 /** 75 * Returns the current (most recent) MO file path for a specific domain. 76 * 77 * @since 6.1.0 78 * 79 * @param string $domain Text domain. 80 * @return string|false Current MO file path or false if there is none available. 81 */ 82 public function get_current( $domain ) { 83 if ( isset( $this->current[ $domain ] ) ) { 84 return $this->current[ $domain ]; 85 } 86 87 return false; 88 } 89 90 /** 91 * Sets the MO file path for a specific domain and locale. 91 * Sets the language directory path for a specific domain and locale. 92 92 * 93 93 * Also sets the 'current' property for direct access … … 106 106 107 107 /** 108 * Resets the registry state.108 * Sets the custom path to the plugin's/theme's languages directory. 109 109 * 110 * @since 6.1.0 110 * Used by {@see load_plugin_textdomain()} and {@see load_theme_textdomain()}. 111 * 112 * @param string $domain Text domain. 113 * @param string $path Language directory path. 111 114 */ 112 public function reset() { 113 $this->cached_mo_files = null; 114 $this->all = array(); 115 $this->current = array(); 115 public function set_custom_path( $domain, $path ) { 116 $this->custom_paths[ $domain ] = untrailingslashit( $path ); 116 117 } 117 118 118 119 /** 119 * Gets the path to a translation file in the languages directory for the current locale. 120 * Gets the path to the language directory for the current locale. 121 * 122 * Checks the plugins and themes language directories as well as any 123 * custom directory set via {@see load_plugin_textdomain()} or {@see load_theme_textdomain()}. 120 124 * 121 125 * @since 6.1.0 122 126 * 127 * @see _get_path_to_translation_from_lang_dir() 128 * 123 129 * @param string $domain Text domain. 124 130 * @param string $locale Locale. 125 * @return string|false MO filepath or false if there is none available.131 * @return string|false Language directory path or false if there is none available. 126 132 */ 127 133 private function get_path_from_lang_dir( $domain, $locale ) { 128 if ( null === $this->cached_mo_files ) { 129 $this->set_cached_mo_files(); 134 $locations = array( 135 WP_LANG_DIR . '/plugins', 136 WP_LANG_DIR . '/themes', 137 ); 138 139 if ( isset( $this->custom_paths[ $domain ] ) ) { 140 $locations[] = $this->custom_paths[ $domain ]; 130 141 } 131 142 132 $mofile = " {$domain}-{$locale}.mo";143 $mofile = "$domain-$locale.mo"; 133 144 134 $path = WP_LANG_DIR . '/plugins/' . $mofile; 145 foreach ( $locations as $location ) { 146 if ( ! isset( $this->cached_mo_files[ $location ] ) ) { 147 $this->set_cached_mo_files( $location ); 148 } 135 149 136 if ( in_array( $path, $this->cached_mo_files, true ) ) { 137 $path = WP_LANG_DIR . '/plugins/'; 138 $this->set( $domain, $locale, $path ); 150 $path = $location . '/' . $mofile; 139 151 140 return $path; 152 if ( in_array( $path, $this->cached_mo_files[ $location ], true ) ) { 153 $this->set( $domain, $locale, $location ); 154 155 return trailingslashit( $location ); 156 } 141 157 } 142 158 143 $path = WP_LANG_DIR . '/themes/' . $mofile; 144 if ( in_array( $path, $this->cached_mo_files, true ) ) { 145 $path = WP_LANG_DIR . '/themes/'; 159 // If no path is found for the given locale and a custom path has been set 160 // using load_plugin_textdomain/load_theme_textdomain, use that one. 161 if ( 'en_US' !== $locale && isset( $this->custom_paths[ $domain ] ) ) { 162 $path = trailingslashit( $this->custom_paths[ $domain ] ); 146 163 $this->set( $domain, $locale, $path ); 147 148 164 return $path; 149 }150 151 // If no path is found for the given locale, check if an entry for the default152 // en_US locale exists. This is the case when e.g. using load_plugin_textdomain153 // with a custom path.154 if ( 'en_US' !== $locale && isset( $this->all[ $domain ]['en_US'] ) ) {155 $this->set( $domain, $locale, $this->all[ $domain ]['en_US'] );156 return $this->all[ $domain ]['en_US'];157 165 } 158 166 … … 163 171 164 172 /** 165 * Reads and caches all available MO files from the plugins and themes language directories.173 * Reads and caches all available MO files from a given directory. 166 174 * 167 175 * @since 6.1.0 176 * 177 * @param string $path Language directory path. 168 178 */ 169 pr otected function set_cached_mo_files() {170 $this->cached_mo_files = array();179 private function set_cached_mo_files( $path ) { 180 $this->cached_mo_files[ $path ] = array(); 171 181 172 $locations = array( 173 WP_LANG_DIR . '/plugins', 174 WP_LANG_DIR . '/themes', 175 ); 182 $mo_files = glob( $path . '/*.mo' ); 176 183 177 foreach ( $locations as $location ) { 178 $mo_files = glob( $location . '/*.mo' ); 179 180 if ( $mo_files ) { 181 $this->cached_mo_files = array_merge( $this->cached_mo_files, $mo_files ); 182 } 184 if ( $mo_files ) { 185 $this->cached_mo_files[ $path ] = $mo_files; 183 186 } 184 187 } -
trunk/src/wp-includes/l10n.php
r54351 r54669 935 935 } 936 936 937 $wp_textdomain_registry->set ( $domain, $locale, $path );937 $wp_textdomain_registry->set_custom_path( $domain, $path ); 938 938 939 939 return load_textdomain( $domain, $path . '/' . $mofile, $locale ); … … 969 969 $path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' ); 970 970 971 $wp_textdomain_registry->set ( $domain, $locale, $path );971 $wp_textdomain_registry->set_custom_path( $domain, $path ); 972 972 973 973 return load_textdomain( $domain, $path . '/' . $mofile, $locale ); … … 1017 1017 } 1018 1018 1019 $wp_textdomain_registry->set ( $domain, $locale, $path );1019 $wp_textdomain_registry->set_custom_path( $domain, $path ); 1020 1020 1021 1021 return load_textdomain( $domain, $path . '/' . $locale . '.mo', $locale ); … … 1266 1266 } 1267 1267 1268 if ( $wp_textdomain_registry->has( $domain ) && ! $wp_textdomain_registry->get_current( $domain ) ) {1268 if ( ! $wp_textdomain_registry->has( $domain ) ) { 1269 1269 return false; 1270 1270 } -
trunk/tests/phpunit/tests/l10n/loadTextdomain.php
r53874 r54669 29 29 global $wp_textdomain_registry; 30 30 31 $wp_textdomain_registry ->reset();31 $wp_textdomain_registry = new WP_Textdomain_Registry(); 32 32 } 33 33 … … 37 37 global $wp_textdomain_registry; 38 38 39 $wp_textdomain_registry ->reset();39 $wp_textdomain_registry = new WP_Textdomain_Registry(); 40 40 41 41 parent::tear_down(); -
trunk/tests/phpunit/tests/l10n/loadTextdomainJustInTime.php
r53874 r54669 9 9 protected $theme_root; 10 10 protected static $user_id; 11 private $locale_count;12 11 13 12 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { … … 25 24 $this->theme_root = DIR_TESTDATA . '/themedir1'; 26 25 $this->orig_theme_dir = $GLOBALS['wp_theme_directories']; 27 $this->locale_count = 0;28 26 29 27 // /themes is necessary as theme.php functions assume /themes is the root if there is only one root. … … 38 36 global $wp_textdomain_registry; 39 37 40 $wp_textdomain_registry ->reset();38 $wp_textdomain_registry = new WP_Textdomain_Registry(); 41 39 } 42 40 … … 49 47 global $wp_textdomain_registry; 50 48 51 $wp_textdomain_registry ->reset();49 $wp_textdomain_registry = new WP_Textdomain_Registry(); 52 50 53 51 parent::tear_down(); … … 263 261 $textdomain = 'foo-bar-baz'; 264 262 265 add_filter( 'locale', array( $this, '_filter_locale_count' ) ); 263 $filter = new MockAction(); 264 add_filter( 'locale', array( $filter, 'filter' ) ); 266 265 267 266 __( 'Foo', $textdomain ); … … 271 270 __( 'Foo Bar Baz', $textdomain ); 272 271 273 remove_filter( 'locale', array( $this, '_filter_locale_count' ) );274 275 272 $this->assertFalse( is_textdomain_loaded( $textdomain ) ); 276 $this->assertSame( 1, $this->locale_count ); 277 } 278 279 public function _filter_locale_count( $locale ) { 280 ++$this->locale_count; 281 282 return $locale; 273 $this->assertSame( 1, $filter->get_call_count() ); 274 } 275 276 /** 277 * @ticket 37997 278 * @ticket 39210 279 * 280 * @covers ::_load_textdomain_just_in_time 281 */ 282 public function test_get_locale_is_called_only_once_per_textdomain_with_custom_lang_dir() { 283 load_plugin_textdomain( 'custom-internationalized-plugin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); 284 285 $textdomain = 'custom-internationalized-plugin'; 286 287 $filter = new MockAction(); 288 add_filter( 'locale', array( $filter, 'filter' ) ); 289 290 __( 'Foo', $textdomain ); 291 __( 'Bar', $textdomain ); 292 __( 'Baz', $textdomain ); 293 __( 'Foo Bar', $textdomain ); 294 __( 'Foo Bar Baz', $textdomain ); 295 296 $this->assertFalse( is_textdomain_loaded( $textdomain ) ); 297 $this->assertSame( 1, $filter->get_call_count() ); 283 298 } 284 299 } -
trunk/tests/phpunit/tests/l10n/wpLocaleSwitcher.php
r54088 r54669 28 28 global $wp_textdomain_registry; 29 29 30 $wp_textdomain_registry ->reset();30 $wp_textdomain_registry = new WP_Textdomain_Registry(); 31 31 } 32 32 … … 37 37 global $wp_textdomain_registry; 38 38 39 $wp_textdomain_registry ->reset();39 $wp_textdomain_registry = new WP_Textdomain_Registry(); 40 40 41 41 parent::tear_down(); … … 479 479 require_once DIR_TESTDATA . '/plugins/custom-internationalized-plugin/custom-internationalized-plugin.php'; 480 480 481 $actual = custom_i18n_plugin_test(); 482 483 switch_to_locale( 'es_ES' ); 484 481 485 $registry_value = $wp_textdomain_registry->get( 'custom-internationalized-plugin', determine_locale() ); 482 486 483 $actual = custom_i18n_plugin_test();484 485 switch_to_locale( 'es_ES' );486 487 switch_to_locale( 'de_DE' ); 487 488 … … 518 519 require_once get_stylesheet_directory() . '/functions.php'; 519 520 521 $actual = custom_i18n_theme_test(); 522 523 switch_to_locale( 'es_ES' ); 524 520 525 $registry_value = $wp_textdomain_registry->get( 'custom-internationalized-theme', determine_locale() ); 521 526 522 $actual = custom_i18n_theme_test();523 524 switch_to_locale( 'es_ES' );525 527 switch_to_locale( 'de_DE' ); 526 528
Note: See TracChangeset
for help on using the changeset viewer.