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