PluginProbe
Polylang / 1.7.4
Polylang v1.7.4
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
polylang / include / olt-manager.php

olt-manager.php in Polylang 1.7.4, at include/olt-manager.php

198 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * it is best practice that plugins do nothing before plugins_loaded is fired
5 * so it is what Polylang intends to do
6 * but some plugins load their text domain as soon as loaded, thus before plugins_loaded is fired
7 * this class differs text domain loading until the language is defined
8 * either in a plugins_loaded action or in a wp action (when the language is set from content on frontend)
9 *
10 * @since 1.2
11 */
12 class PLL_OLT_Manager {
13 static protected $instance; // for singleton
14 protected $default_locale;
15 protected $list_textdomains = array(); // all text domains
16 public $labels = array(); // post types and taxonomies labels to translate
17
18 /*
19 * constructor: setups relevant filters
20 *
21 * @since 1.2
22 */
23 public function __construct() {
24 // saves the default locale before we start any language manipulation
25 $this->default_locale = get_locale();
26
27 // filters for text domain management
28 add_filter('override_load_textdomain', array(&$this, 'mofile'), 10, 3);
29 add_filter('gettext', array(&$this, 'gettext'), 10, 3);
30 add_filter('gettext_with_context', array(&$this, 'gettext_with_context'), 10, 4);
31
32 // loads text domains
33 add_action('pll_language_defined', array(&$this, 'load_textdomains'), 2); // after PLL_Frontend::pll_language_defined
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'));
39 }
40
41 /*
42 * access to the single instance of the class
43 *
44 * @since 1.7
45 *
46 * @return object
47 */
48 static public function instance() {
49 if (empty(self::$instance))
50 self::$instance = new self();
51
52 return self::$instance;
53 }
54
55 /*
56 * loads text domains
57 *
58 * @since 0.1
59 */
60 public function load_textdomains() {
61 // our override_load_textdomain filter has done its job. let's remove it before calling load_textdomain
62 remove_filter('override_load_textdomain', array(&$this, 'mofile'), 10, 3);
63 remove_filter('gettext', array(&$this, 'gettext'), 10, 3);
64 remove_filter('gettext_with_context', array(&$this, 'gettext_with_context'), 10, 4);
65 $new_locale = get_locale();
66
67 // don't try to save time for en_US as some users have theme written in another language
68 // now we can load all overriden text domains with the right language
69 foreach ($this->list_textdomains as $textdomain) {
70 if (!load_textdomain($textdomain['domain'], str_replace("{$this->default_locale}.mo", "$new_locale.mo", $textdomain['mo']))) {
71 // since WP 3.5 themes may store languages files in /wp-content/languages/themes
72 if (!load_textdomain($textdomain['domain'], WP_LANG_DIR . "/themes/{$textdomain['domain']}-$new_locale.mo")) {
73 // since WP 3.7 plugins may store languages files in /wp-content/languages/plugins
74 load_textdomain($textdomain['domain'], WP_LANG_DIR . "/plugins/{$textdomain['domain']}-$new_locale.mo");
75 }
76 }
77 }
78
79 // first remove taxonomies and post_types labels that we don't need to translate
80 $taxonomies = array('language', 'term_language', 'term_translations', 'post_translations');
81 $post_types = array('polylang_mo');
82
83 // we don't need to translate core taxonomies and post types labels when setting the language from the url
84 // as they will be translated when registered the second time
85 if (!did_action('setup_theme')) {
86 $taxonomies = array_merge(array('category', 'post_tag', 'nav_menu', 'link_category', 'post_format'), $taxonomies);
87 $post_types = array_merge(array('post', 'page', 'attachment', 'revision', 'nav_menu_item'), $post_types);
88 }
89
90 // translate labels of post types and taxonomies
91 foreach (array_diff_key($GLOBALS['wp_taxonomies'], array_flip($taxonomies)) as $tax)
92 $this->translate_labels($tax);
93 foreach (array_diff_key($GLOBALS['wp_post_types'], array_flip($post_types)) as $pt)
94 $this->translate_labels($pt);
95
96 // act only if the language has not been set early (before default textdomain loading and $wp_locale creation)
97 if (did_action('after_setup_theme')) {
98 // reinitializes wp_locale for weekdays and months
99 unset($GLOBALS['wp_locale']);
100 $GLOBALS['wp_locale'] = new WP_Locale();
101 }
102
103 // allow plugins to translate text the same way we do for post types and taxonomies labels
104 do_action_ref_array('pll_translate_labels', array(&$this->labels));
105
106 // free memory
107 unset($this->default_locale, $this->list_textdomains, $this->labels);
108 }
109
110 /*
111 * saves all text domains in a table for later usage
112 *
113 * @since 0.1
114 *
115 * @param bool $bool not used
116 * @param string $domain text domain name
117 * @param string $mofile translation file name
118 * @return bool always true
119 */
120 public function mofile($bool, $domain, $mofile) {
121 $this->list_textdomains[] = array ('mo' => $mofile, 'domain' => $domain);
122 return true; // prevents WP loading text domains as we will load them all later
123 }
124
125 /*
126 * saves post types and taxonomies labels for a later usage
127 *
128 * @since 0.9
129 *
130 * @param string $translation not used
131 * @param string $text string to translate
132 * @param string $domain text domain
133 * @return string unmodified $translation
134 */
135 public function gettext($translation, $text, $domain) {
136 if (is_string($text)) // avoid a warning with some buggy plugins which pass an array
137 $this->labels[$text] = array('domain' => $domain);
138 return $translation;
139 }
140
141 /*
142 * saves post types and taxonomies labels for a later usage
143 *
144 * @since 0.9
145 *
146 * @param string $translation not used
147 * @param string $text string to translate
148 * @param string $context some comment to describe the context of string to translate
149 * @param string $domain text domain
150 * @return string unmodified $translation
151 */
152 public function gettext_with_context($translation, $text, $context, $domain) {
153 $this->labels[$text] = array('domain' => $domain, 'context' => $context);
154 return $translation;
155 }
156
157 /*
158 * translates post types and taxonomies labels once the language is known
159 *
160 * @since 0.9
161 *
162 * @param object $type either a post type or a taxonomy
163 */
164 public function translate_labels($type) {
165 // use static array to avoid translating several times the same (default) labels
166 static $translated = array();
167
168 foreach($type->labels as $key => $label) {
169 if (is_string($label) && isset($this->labels[$label])) {
170 if (empty($translated[$label])) {
171 $type->labels->$key = $translated[$label] = isset($this->labels[$label]['context']) ?
172 _x($label, $this->labels[$label]['context'], $this->labels[$label]['domain']) :
173 __($label, $this->labels[$label]['domain']);
174 }
175 else {
176 $type->labels->$key = $translated[$label];
177 }
178 }
179 }
180 }
181
182 /*
183 * allows Polylang to be the first plugin loaded ;-)
184 *
185 * @since 1.2
186 *
187 * @param array $plugins list of active plugins
188 * @return array list of active plugins
189 */
190 public function make_polylang_first($plugins) {
191 if ($key = array_search(POLYLANG_BASENAME, $plugins)) {
192 unset($plugins[$key]);
193 array_unshift($plugins, POLYLANG_BASENAME);
194 }
195 return $plugins;
196 }
197 }
198