PluginProbe
Polylang / 3.3
Polylang v3.3
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
← All changes | include/olt-manager.php +55 -44 2.83.3 View file →
@@ -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,25 +87,28 @@
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 - // Since WP 4.7 we need to reset the internal cache of _get_path_to_translation when switching from any locale to en_US
82 - // See WP_Locale_Switcher::change_locale()
83 - // FIXME test _get_path_to_translation for backward compatibility with WP < 4.7
84 - if ( function_exists( '_get_path_to_translation' ) ) {
85 - _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 );
86 111 }
87 112
88 113 foreach ( $this->list_textdomains as $textdomain ) {
89 114 // Since WP 4.6, plugins translations are first loaded from wp-content/languages
@@ -137,40 +162,25 @@
137 162 unset( $this->default_locale, $this->list_textdomains, $this->labels );
138 163 }
139 164
140 165 /**
141 - * FIXME: Backward compatibility with Polylang for WooCommerce < 0.3.4
142 - * 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.
143 168 *
144 - * @since 0.1
145 - *
146 - * @param bool $bool not used
147 - * @param string $domain text domain name
148 - * @param string $mofile translation file name
149 - * @return bool
150 - */
151 - public function mofile( $bool, $domain, $mofile ) {
152 - return $bool;
153 - }
154 -
155 - /**
156 - * Saves all text domains in a table for later usage
157 - * It replaces the 'override_load_textdomain' filter used since 0.1
158 - *
159 169 * @since 2.0.4
160 170 *
161 - * @param string $mofile translation file name
162 - * @param string $domain text domain name
163 - * @return bool
171 + * @param string $mofile The translation file name.
172 + * @param string $domain The text domain name.
173 + * @return string
164 174 */
165 175 public function load_textdomain_mofile( $mofile, $domain ) {
166 - // 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.
167 177 if ( 'default' === $domain && false !== strpos( $mofile, '/ms-' ) ) {
168 178 $this->list_textdomains['ms-default'] = array( 'mo' => $mofile, 'domain' => $domain );
169 179 } else {
170 180 $this->list_textdomains[ $domain ] = array( 'mo' => $mofile, 'domain' => $domain );
171 181 }
172 - 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.
173 183 }
174 184
175 185 /**
176 186 * Saves post types and taxonomies labels for a later usage
@@ -205,19 +215,20 @@
205 215 return $translation;
206 216 }
207 217
208 218 /**
209 - * Translates post types and taxonomies labels once the language is known
219 + * Translates post types and taxonomies labels once the language is known.
210 220 *
211 221 * @since 0.9
212 222 *
213 - * @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
214 225 */
215 226 public function translate_labels( $type ) {
216 227 // Use static array to avoid translating several times the same ( default ) labels
217 228 static $translated = array();
218 229
219 - foreach ( $type->labels as $key => $label ) {
230 + foreach ( (array) $type->labels as $key => $label ) {
220 231 if ( is_string( $label ) && isset( $this->labels[ $label ] ) ) {
221 232 if ( empty( $translated[ $label ] ) ) {
222 233 // PHPCS:disable WordPress.WP.I18n
223 234 $type->labels->$key = $translated[ $label ] = isset( $this->labels[ $label ]['context'] ) ?
@@ -232,14 +243,14 @@
232 243 }
233 244 }
234 245
235 246 /**
236 - * Allows Polylang to be the first plugin loaded ;- )
247 + * Allows Polylang to be the first plugin loaded ;-).
237 248 *
238 249 * @since 1.2
239 250 *
240 - * @param array $plugins list of active plugins
241 - * @return array list of active plugins
251 + * @param string[] $plugins List of active plugins.
252 + * @return string[] List of active plugins.
242 253 */
243 254 public function make_polylang_first( $plugins ) {
244 255 if ( $key = array_search( POLYLANG_BASENAME, $plugins ) ) {
245 256 unset( $plugins[ $key ] );