| @@ -1,5 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | /** |
| 4 | 7 | * It is best practice that plugins do nothing before plugins_loaded is fired |
| 5 | 8 | * so it is what Polylang intends to do |
| @@ -9,14 +12,37 @@ | ||
| 9 | 12 | * |
| 10 | 13 | * @since 1.2 |
| 11 | 14 | */ |
| 12 | 15 | class PLL_OLT_Manager { |
| 13 | - protected static $instance; // For singleton | |
| 16 | + /** | |
| 17 | + * Singleton instance | |
| 18 | + * | |
| 19 | + * @var PLL_OLT_Manager | |
| 20 | + */ | |
| 21 | + protected static $instance; | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * Stores the default site locale before it is modified. | |
| 25 | + * | |
| 26 | + * @var string | |
| 27 | + */ | |
| 14 | 28 | protected $default_locale; |
| 15 | - protected $list_textdomains = array(); // All text domains | |
| 16 | - public $labels = array(); // Post types and taxonomies labels to translate | |
| 17 | 29 | |
| 18 | 30 | /** |
| 31 | + * Stores all loaded text domains and mo files. | |
| 32 | + * | |
| 33 | + * @var string[][] | |
| 34 | + */ | |
| 35 | + protected $list_textdomains = array(); | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * Stores post types an taxonomies labels to translate. | |
| 39 | + * | |
| 40 | + * @var string[][] | |
| 41 | + */ | |
| 42 | + public $labels = array(); | |
| 43 | + | |
| 44 | + /** | |
| 19 | 45 | * Constructor: setups relevant filters |
| 20 | 46 | * |
| 21 | 47 | * @since 1.2 |
| 22 | 48 | */ |
| @@ -24,11 +50,10 @@ | ||
| 24 | 50 | // Allows Polylang to be the first plugin loaded ;-) |
| 25 | 51 | add_filter( 'pre_update_option_active_plugins', array( $this, 'make_polylang_first' ) ); |
| 26 | 52 | add_filter( 'pre_update_option_active_sitewide_plugins', array( $this, 'make_polylang_first' ) ); |
| 27 | 53 | |
| 28 | - // Overriding load text domain only on front since WP 4.7 | |
| 29 | - // FIXME test get_user_locale for backward compatibility with WP < 4.7 | |
| 30 | - if ( is_admin() && ! Polylang::is_ajax_on_front() && function_exists( 'get_user_locale' ) ) { | |
| 54 | + // Overriding load text domain only on front since WP 4.7. | |
| 55 | + if ( is_admin() && ! Polylang::is_ajax_on_front() ) { | |
| 31 | 56 | return; |
| 32 | 57 | } |
| 33 | 58 | |
| 34 | 59 | // Saves the default locale before we start any language manipulation |
| @@ -62,8 +87,10 @@ | ||
| 62 | 87 | /** |
| 63 | 88 | * Loads text domains |
| 64 | 89 | * |
| 65 | 90 | * @since 0.1 |
| 91 | + * | |
| 92 | + * @return void | |
| 66 | 93 | */ |
| 67 | 94 | public function load_textdomains() { |
| 68 | 95 | // Our load_textdomain_mofile filter has done its job. let's remove it before calling load_textdomain |
| 69 | 96 | remove_filter( 'load_textdomain_mofile', array( $this, 'load_textdomain_mofile' ), 10, 2 ); |
| @@ -73,14 +100,15 @@ | ||
| 73 | 100 | |
| 74 | 101 | // Don't try to save time for en_US as some users have theme written in another language |
| 75 | 102 | // Now we can load all overridden text domains with the right language |
| 76 | 103 | if ( ! empty( $this->list_textdomains ) ) { |
| 77 | - | |
| 78 | - // Since WP 4.7 we need to reset the internal cache of _get_path_to_translation when switching from any locale to en_US | |
| 79 | - // See WP_Locale_Switcher::change_locale() | |
| 80 | - // FIXME test _get_path_to_translation for backward compatibility with WP < 4.7 | |
| 81 | - if ( function_exists( '_get_path_to_translation' ) ) { | |
| 82 | - _get_path_to_translation( null, true ); | |
| 104 | + /* | |
| 105 | + * FIXME: Backward compatibility with WP < 5.6 | |
| 106 | + * From WP 4.7 to 5.5, we need to reset the internal cache of _get_path_to_translation when switching from any locale to en_US. | |
| 107 | + * See WP_Locale_Switcher::change_locale() | |
| 108 | + */ | |
| 109 | + if ( ! class_exists( 'WP_Textdomain_Registry' ) && function_exists( '_get_path_to_translation' ) ) { | |
| 110 | + _get_path_to_translation( '', true ); | |
| 83 | 111 | } |
| 84 | 112 | |
| 85 | 113 | foreach ( $this->list_textdomains as $textdomain ) { |
| 86 | 114 | // Since WP 4.6, plugins translations are first loaded from wp-content/languages |
| @@ -134,40 +162,25 @@ | ||
| 134 | 162 | unset( $this->default_locale, $this->list_textdomains, $this->labels ); |
| 135 | 163 | } |
| 136 | 164 | |
| 137 | 165 | /** |
| 138 | - * FIXME: Backward compatibility with Polylang for WooCommerce < 0.3.4 | |
| 139 | - * To remove in Polylang 2.1 | |
| 166 | + * Saves all text domains in a table for later usage. | |
| 167 | + * It replaces the 'override_load_textdomain' filter previously used. | |
| 140 | 168 | * |
| 141 | - * @since 0.1 | |
| 142 | - * | |
| 143 | - * @param bool $bool not used | |
| 144 | - * @param string $domain text domain name | |
| 145 | - * @param string $mofile translation file name | |
| 146 | - * @return bool | |
| 147 | - */ | |
| 148 | - public function mofile( $bool, $domain, $mofile ) { | |
| 149 | - return $bool; | |
| 150 | - } | |
| 151 | - | |
| 152 | - /** | |
| 153 | - * Saves all text domains in a table for later usage | |
| 154 | - * It replaces the 'override_load_textdomain' filter used since 0.1 | |
| 155 | - * | |
| 156 | 169 | * @since 2.0.4 |
| 157 | 170 | * |
| 158 | - * @param string $mofile translation file name | |
| 159 | - * @param string $domain text domain name | |
| 160 | - * @return bool | |
| 171 | + * @param string $mofile The translation file name. | |
| 172 | + * @param string $domain The text domain name. | |
| 173 | + * @return string | |
| 161 | 174 | */ |
| 162 | 175 | public function load_textdomain_mofile( $mofile, $domain ) { |
| 163 | - // On multisite, 2 files are sharing the same domain so we need to distinguish them | |
| 176 | + // On multisite, 2 files are sharing the same domain so we need to distinguish them. | |
| 164 | 177 | if ( 'default' === $domain && false !== strpos( $mofile, '/ms-' ) ) { |
| 165 | 178 | $this->list_textdomains['ms-default'] = array( 'mo' => $mofile, 'domain' => $domain ); |
| 166 | 179 | } else { |
| 167 | 180 | $this->list_textdomains[ $domain ] = array( 'mo' => $mofile, 'domain' => $domain ); |
| 168 | 181 | } |
| 169 | - return ''; // Hack to prevent WP loading text domains as we will load them all later | |
| 182 | + return ''; // Hack to prevent WP loading text domains as we will load them all later. | |
| 170 | 183 | } |
| 171 | 184 | |
| 172 | 185 | /** |
| 173 | 186 | * Saves post types and taxonomies labels for a later usage |
| @@ -202,19 +215,20 @@ | ||
| 202 | 215 | return $translation; |
| 203 | 216 | } |
| 204 | 217 | |
| 205 | 218 | /** |
| 206 | - * Translates post types and taxonomies labels once the language is known | |
| 219 | + * Translates post types and taxonomies labels once the language is known. | |
| 207 | 220 | * |
| 208 | 221 | * @since 0.9 |
| 209 | 222 | * |
| 210 | - * @param object $type either a post type or a taxonomy | |
| 223 | + * @param WP_Post_Type|WP_Taxonomy $type Either a post type or a taxonomy. | |
| 224 | + * @return void | |
| 211 | 225 | */ |
| 212 | 226 | public function translate_labels( $type ) { |
| 213 | 227 | // Use static array to avoid translating several times the same ( default ) labels |
| 214 | 228 | static $translated = array(); |
| 215 | 229 | |
| 216 | - foreach ( $type->labels as $key => $label ) { | |
| 230 | + foreach ( (array) $type->labels as $key => $label ) { | |
| 217 | 231 | if ( is_string( $label ) && isset( $this->labels[ $label ] ) ) { |
| 218 | 232 | if ( empty( $translated[ $label ] ) ) { |
| 219 | 233 | // PHPCS:disable WordPress.WP.I18n |
| 220 | 234 | $type->labels->$key = $translated[ $label ] = isset( $this->labels[ $label ]['context'] ) ? |
| @@ -229,14 +243,14 @@ | ||
| 229 | 243 | } |
| 230 | 244 | } |
| 231 | 245 | |
| 232 | 246 | /** |
| 233 | - * Allows Polylang to be the first plugin loaded ;- ) | |
| 247 | + * Allows Polylang to be the first plugin loaded ;-). | |
| 234 | 248 | * |
| 235 | 249 | * @since 1.2 |
| 236 | 250 | * |
| 237 | - * @param array $plugins list of active plugins | |
| 238 | - * @return array list of active plugins | |
| 251 | + * @param string[] $plugins List of active plugins. | |
| 252 | + * @return string[] List of active plugins. | |
| 239 | 253 | */ |
| 240 | 254 | public function make_polylang_first( $plugins ) { |
| 241 | 255 | if ( $key = array_search( POLYLANG_BASENAME, $plugins ) ) { |
| 242 | 256 | unset( $plugins[ $key ] ); |