PluginProbe
Polylang / 1.5
Polylang v1.5
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.5, at include/olt-manager.php

172 lines 6.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 protected $default_locale;
14 protected $list_textdomains = array(); // all text domains
15 public $labels = array(); // post types and taxonomies labels to translate
16
17 /*
18 * constructor: setups relevant filters
19 *
20 * @since 1.2
21 */
22 public function __construct() {
23 // saves the default locale before we start any language manipulation
24 $this->default_locale = get_locale();
25
26 // filters for text domain management
27 add_filter('override_load_textdomain', array(&$this, 'mofile'), 10, 3);
28 add_filter('gettext', array(&$this, 'gettext'), 10, 3);
29 add_filter('gettext_with_context', array(&$this, 'gettext_with_context'), 10, 4);
30
31 // loads text domains
32 add_action('pll_language_defined', array(&$this, 'load_textdomains'), 2); // after PLL_Frontend::pll_language_defined
33 add_action('pll_no_language_defined', array(&$this, 'load_textdomains'));
34
35 // allows Polylang to be the first plugin loaded ;-)
36 add_filter('pre_update_option_active_plugins', array(&$this, 'make_polylang_first'));
37 add_filter('pre_update_option_active_sitewide_plugins', array(&$this, 'make_polylang_first'));
38 }
39
40 /*
41 * loads text domains
42 *
43 * @since 0.1
44 */
45 public function load_textdomains() {
46 // our override_load_textdomain filter has done its job. let's remove it before calling load_textdomain
47 remove_filter('override_load_textdomain', array(&$this, 'mofile'), 10, 3);
48 remove_filter('gettext', array(&$this, 'gettext'), 10, 3);
49 remove_filter('gettext_with_context', array(&$this, 'gettext_with_context'), 10, 4);
50 $new_locale = get_locale();
51
52 // don't try to save time for en_US as some users have theme written in another language
53 // now we can load all overriden text domains with the right language
54 foreach ($this->list_textdomains as $textdomain) {
55 if (!load_textdomain($textdomain['domain'], str_replace("{$this->default_locale}.mo", "$new_locale.mo", $textdomain['mo']))) {
56 // since WP 3.5 themes may store languages files in /wp-content/languages/themes
57 if (!load_textdomain($textdomain['domain'], WP_LANG_DIR . "/themes/{$textdomain['domain']}-$new_locale.mo")) {
58 // since WP 3.7 plugins may store languages files in /wp-content/languages/plugins
59 load_textdomain($textdomain['domain'], WP_LANG_DIR . "/plugins/{$textdomain['domain']}-$new_locale.mo");
60 }
61 }
62 }
63
64 // translate labels of post types and taxonomies
65 foreach ($GLOBALS['wp_taxonomies'] as $tax)
66 $this->translate_labels($tax);
67 foreach ($GLOBALS['wp_post_types'] as $pt)
68 $this->translate_labels($pt);
69
70 // act only if the language has not been set early (before default textdomain loading and $wp_locale creation
71 if (did_action('after_setup_theme')) {
72 // reinitializes wp_locale for weekdays and months
73 unset($GLOBALS['wp_locale']);
74 $GLOBALS['wp_locale'] = new WP_Locale();
75 }
76
77 // allow plugins to translate text the same way we do for post types and taxonomies labels
78 do_action_ref_array('pll_translate_labels', array(&$this->labels));
79
80 // free memory
81 unset($this->default_locale, $this->list_textdomains, $this->labels);
82 }
83
84 /*
85 * saves all text domains in a table for later usage
86 *
87 * @since 0.1
88 *
89 * @param bool $bool not used
90 * @param string $domain text domain name
91 * @param string $mofile translation file name
92 * @return bool always true
93 */
94 public function mofile($bool, $domain, $mofile) {
95 $this->list_textdomains[] = array ('mo' => $mofile, 'domain' => $domain);
96 return true; // prevents WP loading text domains as we will load them all later
97 }
98
99 /*
100 * saves post types and taxonomies labels for a later usage
101 *
102 * @since 0.9
103 *
104 * @param string $translation not used
105 * @param string $text string to translate
106 * @param string $domain text domain
107 * @return string unmodified $translation
108 */
109 public function gettext($translation, $text, $domain) {
110 if (is_string($text)) // avoid a warning with some buggy plugins which pass an array
111 $this->labels[$text] = array('domain' => $domain);
112 return $translation;
113 }
114
115 /*
116 * saves post types and taxonomies labels for a later usage
117 *
118 * @since 0.9
119 *
120 * @param string $translation not used
121 * @param string $text string to translate
122 * @param string $context some comment to describe the context of string to translate
123 * @param string $domain text domain
124 * @return string unmodified $translation
125 */
126 public function gettext_with_context($translation, $text, $context, $domain) {
127 $this->labels[$text] = array('domain' => $domain, 'context' => $context);
128 return $translation;
129 }
130
131 /*
132 * translates post types and taxonomies labels once the language is known
133 *
134 * @since 0.9
135 *
136 * @param object $type either a post type or a taxonomy
137 */
138 public function translate_labels($type) {
139 // use static array to avoid translating several times the same (default) labels
140 static $translated = array();
141
142 foreach($type->labels as $key => $label) {
143 if (is_string($label) && isset($this->labels[$label])) {
144 if (empty($translated[$label])) {
145 $type->labels->$key = $translated[$label] = isset($this->labels[$label]['context']) ?
146 _x($label, $this->labels[$label]['context'], $this->labels[$label]['domain']) :
147 __($label, $this->labels[$label]['domain']);
148 }
149 else {
150 $type->labels->$key = $translated[$label];
151 }
152 }
153 }
154 }
155
156 /*
157 * allows Polylang to be the first plugin loaded ;-)
158 *
159 * @since 1.2
160 *
161 * @param array $plugins list of active plugins
162 * @return array list of active plugins
163 */
164 public function make_polylang_first($plugins) {
165 if ($key = array_search(POLYLANG_BASENAME, $plugins)) {
166 unset($plugins[$key]);
167 array_unshift($plugins, POLYLANG_BASENAME);
168 }
169 return $plugins;
170 }
171 }
172