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

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

164 lines 5.7 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'));
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 if ('en_US' != $new_locale) { // save some time for en_US
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
81 // free memory
82 unset($this->default_locale, $this->list_textdomains, $this->labels);
83 }
84
85 /*
86 * saves all text domains in a table for later usage
87 *
88 * @since 0.1
89 *
90 * @param bool $bool not used
91 * @param string $domain text domain name
92 * @param string $mofile translation file name
93 * @return bool always true
94 */
95 public function mofile($bool, $domain, $mofile) {
96 $this->list_textdomains[] = array ('mo' => $mofile, 'domain' => $domain);
97 return true; // prevents WP loading text domains as we will load them all later
98 }
99
100 /*
101 * saves post types and taxonomies labels for a later usage
102 *
103 * @since 0.9
104 *
105 * @param string $translation not used
106 * @param string $text string to translate
107 * @param string $domain text domain
108 * @return string unmodified $translation
109 */
110 public function gettext($translation, $text, $domain) {
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 foreach($type->labels as $key => $label) {
140 if (is_string($label) && isset($this->labels[$label])) {
141 $type->labels->$key = isset($this->labels[$label]['context']) ?
142 _x($label, $this->labels[$label]['context'], $this->labels[$label]['domain']) :
143 __($label, $this->labels[$label]['domain']);
144 }
145 }
146 }
147
148 /*
149 * allows Polylang to be the first plugin loaded ;-)
150 *
151 * @since 1.2
152 *
153 * @param array $plugins list of active plugins
154 * @return array list of active plugins
155 */
156 public function make_polylang_first($plugins) {
157 if ($key = array_search(POLYLANG_BASENAME, $plugins)) {
158 unset($plugins[$key]);
159 array_unshift($plugins, POLYLANG_BASENAME);
160 }
161 return $plugins;
162 }
163 }
164