- Timestamp:
- 10/24/2022 06:35:30 PM (2 years ago)
- Location:
- branches/6.1
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/6.1
-
branches/6.1/src/wp-includes/class-wp-textdomain-registry.php
r54133 r54682 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 }
Note: See TracChangeset
for help on using the changeset viewer.