PluginProbe
Polylang / 2.0.3
Polylang v2.0.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 +21 -57 2.82.0.3 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,9 +9,9 @@
12 9 *
13 10 * @since 1.2
14 11 */
15 12 class PLL_OLT_Manager {
16 - protected static $instance; // For singleton
13 + static protected $instance; // For singleton
17 14 protected $default_locale;
18 15 protected $list_textdomains = array(); // All text domains
19 16 public $labels = array(); // Post types and taxonomies labels to translate
20 17
@@ -23,23 +20,13 @@
23 20 *
24 21 * @since 1.2
25 22 */
26 23 public function __construct() {
27 - // Allows Polylang to be the first plugin loaded ;-)
28 - add_filter( 'pre_update_option_active_plugins', array( $this, 'make_polylang_first' ) );
29 - add_filter( 'pre_update_option_active_sitewide_plugins', array( $this, 'make_polylang_first' ) );
30 -
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' ) ) {
34 - return;
35 - }
36 -
37 24 // Saves the default locale before we start any language manipulation
38 25 $this->default_locale = get_locale();
39 26
40 27 // Filters for text domain management
41 - add_filter( 'load_textdomain_mofile', array( $this, 'load_textdomain_mofile' ), 10, 2 );
28 + add_filter( 'override_load_textdomain', array( $this, 'mofile' ), 10, 3 );
42 29 add_filter( 'gettext', array( $this, 'gettext' ), 10, 3 );
43 30 add_filter( 'gettext_with_context', array( $this, 'gettext_with_context' ), 10, 4 );
44 31
45 32 // Loads text domains
@@ -44,8 +31,12 @@
44 31
45 32 // Loads text domains
46 33 add_action( 'pll_language_defined', array( $this, 'load_textdomains' ), 2 ); // After PLL_Frontend::pll_language_defined
47 34 add_action( 'pll_no_language_defined', array( $this, 'load_textdomains' ) );
35 +
36 + // Allows Polylang to be the first plugin loaded ;-)
37 + add_filter( 'pre_update_option_active_plugins', array( $this, 'make_polylang_first' ) );
38 + add_filter( 'pre_update_option_active_sitewide_plugins', array( $this, 'make_polylang_first' ) );
48 39 }
49 40
50 41 /**
51 42 * Access to the single instance of the class
@@ -53,9 +44,9 @@
53 44 * @since 1.7
54 45 *
55 46 * @return object
56 47 */
57 - public static function instance() {
48 + static public function instance() {
58 49 if ( empty( self::$instance ) ) {
59 50 self::$instance = new self();
60 51 }
61 52
@@ -67,27 +58,19 @@
67 58 *
68 59 * @since 0.1
69 60 */
70 61 public function load_textdomains() {
71 - // 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 );
62 + // Our override_load_textdomain filter has done its job. let's remove it before calling load_textdomain
63 + remove_filter( 'override_load_textdomain', array( $this, 'mofile' ), 10, 3 );
73 64 remove_filter( 'gettext', array( $this, 'gettext' ), 10, 3 );
74 65 remove_filter( 'gettext_with_context', array( $this, 'gettext_with_context' ), 10, 4 );
75 66 $new_locale = get_locale();
76 67
77 68 // Don't try to save time for en_US as some users have theme written in another language
78 - // Now we can load all overridden text domains with the right language
69 + // Now we can load all overriden text domains with the right language
79 70 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 );
86 - }
87 -
88 71 foreach ( $this->list_textdomains as $textdomain ) {
89 - // Since WP 4.6, plugins translations are first loaded from wp-content/languages
72 + // FIXME: Since WP 4.6, plugins translations are first loaded from wp-content/languages
90 73 if ( ! load_textdomain( $textdomain['domain'], str_replace( "{$this->default_locale}.mo", "$new_locale.mo", $textdomain['mo'] ) ) ) {
91 74 // Since WP 3.5 themes may store languages files in /wp-content/languages/themes
92 75 if ( ! load_textdomain( $textdomain['domain'], WP_LANG_DIR . "/themes/{$textdomain['domain']}-$new_locale.mo" ) ) {
93 76 // Since WP 3.7 plugins may store languages files in /wp-content/languages/plugins
@@ -97,16 +80,16 @@
97 80 }
98 81 }
99 82
100 83 // First remove taxonomies and post_types labels that we don't need to translate
101 - $taxonomies = get_taxonomies( array( '_pll' => true ) );
102 - $post_types = get_post_types( array( '_pll' => true ) );
84 + $taxonomies = array( 'language', 'term_language', 'term_translations', 'post_translations' );
85 + $post_types = array( 'polylang_mo' );
103 86
104 87 // We don't need to translate core taxonomies and post types labels when setting the language from the url
105 88 // As they will be translated when registered the second time
106 89 if ( ! did_action( 'setup_theme' ) ) {
107 - $taxonomies = array_merge( get_taxonomies( array( '_builtin' => true ) ), $taxonomies );
108 - $post_types = array_merge( get_post_types( array( '_builtin' => true ) ), $post_types );
90 + $taxonomies = array_merge( array( 'category', 'post_tag', 'nav_menu', 'link_category', 'post_format' ), $taxonomies );
91 + $post_types = array_merge( array( 'post', 'page', 'attachment', 'revision', 'nav_menu_item' ), $post_types );
109 92 }
110 93
111 94 // Translate labels of post types and taxonomies
112 95 foreach ( array_diff_key( $GLOBALS['wp_taxonomies'], array_flip( $taxonomies ) ) as $tax ) {
@@ -137,10 +120,9 @@
137 120 unset( $this->default_locale, $this->list_textdomains, $this->labels );
138 121 }
139 122
140 123 /**
141 - * FIXME: Backward compatibility with Polylang for WooCommerce < 0.3.4
142 - * To remove in Polylang 2.1
124 + * Saves all text domains in a table for later usage
143 125 *
144 126 * @since 0.1
145 127 *
146 128 * @param bool $bool not used
@@ -148,32 +130,16 @@
148 130 * @param string $mofile translation file name
149 131 * @return bool
150 132 */
151 133 public function mofile( $bool, $domain, $mofile ) {
152 - return $bool;
134 + $this->list_textdomains[ $domain ] = array( 'mo' => $mofile, 'domain' => $domain );
135 + // Prevents WP loading text domains as we will load them all later
136 + // FIXME backward compatibility with WP < 4.6. See #34213
137 + // true for WP < 4.6, false for WP 4.6+ as we need to keep memory of the location of the language file inside the plugin directory
138 + return version_compare( $GLOBALS['wp_version'], '4.6-alpha', '<' );
153 139 }
154 140
155 141 /**
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 - * @since 2.0.4
160 - *
161 - * @param string $mofile translation file name
162 - * @param string $domain text domain name
163 - * @return bool
164 - */
165 - public function load_textdomain_mofile( $mofile, $domain ) {
166 - // On multisite, 2 files are sharing the same domain so we need to distinguish them
167 - if ( 'default' === $domain && false !== strpos( $mofile, '/ms-' ) ) {
168 - $this->list_textdomains['ms-default'] = array( 'mo' => $mofile, 'domain' => $domain );
169 - } else {
170 - $this->list_textdomains[ $domain ] = array( 'mo' => $mofile, 'domain' => $domain );
171 - }
172 - return ''; // Hack to prevent WP loading text domains as we will load them all later
173 - }
174 -
175 - /**
176 142 * Saves post types and taxonomies labels for a later usage
177 143 *
178 144 * @since 0.9
179 145 *
@@ -218,13 +184,11 @@
218 184
219 185 foreach ( $type->labels as $key => $label ) {
220 186 if ( is_string( $label ) && isset( $this->labels[ $label ] ) ) {
221 187 if ( empty( $translated[ $label ] ) ) {
222 - // PHPCS:disable WordPress.WP.I18n
223 188 $type->labels->$key = $translated[ $label ] = isset( $this->labels[ $label ]['context'] ) ?
224 189 _x( $label, $this->labels[ $label ]['context'], $this->labels[ $label ]['domain'] ) :
225 190 __( $label, $this->labels[ $label ]['domain'] );
226 - // PHPCS:enable
227 191 }
228 192 else {
229 193 $type->labels->$key = $translated[ $label ];
230 194 }