PluginProbe
Polylang / 2.3.6
Polylang v2.3.6
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 +45 -59 3.02.3.6 View file →
@@ -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() && function_exists( 'get_user_locale' ) ) {
56 31 return;
57 32 }
58 33
59 34 // Saves the default locale before we start any language manipulation
@@ -63,11 +38,13 @@
63 38 add_filter( 'load_textdomain_mofile', array( $this, 'load_textdomain_mofile' ), 10, 2 );
64 39 add_filter( 'gettext', array( $this, 'gettext' ), 10, 3 );
65 40 add_filter( 'gettext_with_context', array( $this, 'gettext_with_context' ), 10, 4 );
66 41
67 - // Loads text domains
68 - add_action( 'pll_language_defined', array( $this, 'load_textdomains' ), 2 ); // After PLL_Frontend::pll_language_defined
69 - add_action( 'pll_no_language_defined', array( $this, 'load_textdomains' ) );
42 + if ( ! Polylang::is_ajax_on_front() ) {
43 + // Loads text domains
44 + add_action( 'pll_language_defined', array( $this, 'load_textdomains' ), 2 ); // After PLL_Frontend::pll_language_defined
45 + add_action( 'pll_no_language_defined', array( $this, 'load_textdomains' ) );
46 + }
70 47 }
71 48
72 49 /**
73 50 * Access to the single instance of the class
@@ -75,9 +52,9 @@
75 52 * @since 1.7
76 53 *
77 54 * @return object
78 55 */
79 - public static function instance() {
56 + static public function instance() {
80 57 if ( empty( self::$instance ) ) {
81 58 self::$instance = new self();
82 59 }
83 60
@@ -87,10 +64,8 @@
87 64 /**
88 65 * Loads text domains
89 66 *
90 67 * @since 0.1
91 - *
92 - * @return void
93 68 */
94 69 public function load_textdomains() {
95 70 // Our load_textdomain_mofile filter has done its job. let's remove it before calling load_textdomain
96 71 remove_filter( 'load_textdomain_mofile', array( $this, 'load_textdomain_mofile' ), 10, 2 );
@@ -100,15 +75,14 @@
100 75
101 76 // Don't try to save time for en_US as some users have theme written in another language
102 77 // Now we can load all overridden text domains with the right language
103 78 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 );
79 +
80 + // Since WP 4.7 we need to reset the internal cache of _get_path_to_translation when switching from any locale to en_US
81 + // See WP_Locale_Switcher::change_locale()
82 + // FIXME test _get_path_to_translation for backward compatibility with WP < 4.7
83 + if ( function_exists( '_get_path_to_translation' ) ) {
84 + _get_path_to_translation( null, true );
111 85 }
112 86
113 87 foreach ( $this->list_textdomains as $textdomain ) {
114 88 // Since WP 4.6, plugins translations are first loaded from wp-content/languages
@@ -162,25 +136,40 @@
162 136 unset( $this->default_locale, $this->list_textdomains, $this->labels );
163 137 }
164 138
165 139 /**
166 - * Saves all text domains in a table for later usage.
167 - * It replaces the 'override_load_textdomain' filter previously used.
140 + * FIXME: Backward compatibility with Polylang for WooCommerce < 0.3.4
141 + * To remove in Polylang 2.1
168 142 *
143 + * @since 0.1
144 + *
145 + * @param bool $bool not used
146 + * @param string $domain text domain name
147 + * @param string $mofile translation file name
148 + * @return bool
149 + */
150 + public function mofile( $bool, $domain, $mofile ) {
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 + *
169 158 * @since 2.0.4
170 159 *
171 - * @param string $mofile The translation file name.
172 - * @param string $domain The text domain name.
173 - * @return string
160 + * @param string $mofile translation file name
161 + * @param string $domain text domain name
162 + * @return bool
174 163 */
175 164 public function load_textdomain_mofile( $mofile, $domain ) {
176 - // On multisite, 2 files are sharing the same domain so we need to distinguish them.
165 + // On multisite, 2 files are sharing the same domain so we need to distinguish them
177 166 if ( 'default' === $domain && false !== strpos( $mofile, '/ms-' ) ) {
178 167 $this->list_textdomains['ms-default'] = array( 'mo' => $mofile, 'domain' => $domain );
179 168 } else {
180 169 $this->list_textdomains[ $domain ] = array( 'mo' => $mofile, 'domain' => $domain );
181 170 }
182 - return ''; // Hack to prevent WP loading text domains as we will load them all later.
171 + return ''; // Hack to prevent WP loading text domains as we will load them all later
183 172 }
184 173
185 174 /**
186 175 * Saves post types and taxonomies labels for a later usage
@@ -215,14 +204,13 @@
215 204 return $translation;
216 205 }
217 206
218 207 /**
219 - * Translates post types and taxonomies labels once the language is known.
208 + * Translates post types and taxonomies labels once the language is known
220 209 *
221 210 * @since 0.9
222 211 *
223 - * @param WP_Post_Type|WP_Taxonomy $type Either a post type or a taxonomy.
224 - * @return void
212 + * @param object $type either a post type or a taxonomy
225 213 */
226 214 public function translate_labels( $type ) {
227 215 // Use static array to avoid translating several times the same ( default ) labels
228 216 static $translated = array();
@@ -229,13 +217,11 @@
229 217
230 218 foreach ( $type->labels as $key => $label ) {
231 219 if ( is_string( $label ) && isset( $this->labels[ $label ] ) ) {
232 220 if ( empty( $translated[ $label ] ) ) {
233 - // PHPCS:disable WordPress.WP.I18n
234 221 $type->labels->$key = $translated[ $label ] = isset( $this->labels[ $label ]['context'] ) ?
235 222 _x( $label, $this->labels[ $label ]['context'], $this->labels[ $label ]['domain'] ) :
236 223 __( $label, $this->labels[ $label ]['domain'] );
237 - // PHPCS:enable
238 224 }
239 225 else {
240 226 $type->labels->$key = $translated[ $label ];
241 227 }
@@ -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 ] );