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