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

204 lines 7.2 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
53 return self::$instance;
54 }
55
56 /*
57 * loads text domains
58 *
59 * @since 0.1
60 */
61 public function load_textdomains() {
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 );
64 remove_filter( 'gettext', array( &$this, 'gettext' ), 10, 3 );
65 remove_filter( 'gettext_with_context', array( &$this, 'gettext_with_context' ), 10, 4 );
66 $new_locale = get_locale();
67
68 // don't try to save time for en_US as some users have theme written in another language
69 // now we can load all overriden text domains with the right language
70 if ( ! empty( $this->list_textdomains ) ) {
71 foreach ( $this->list_textdomains as $textdomain ) {
72 if ( ! load_textdomain( $textdomain['domain'], str_replace( "{$this->default_locale}.mo", "$new_locale.mo", $textdomain['mo'] ) ) ) {
73 // since WP 3.5 themes may store languages files in /wp-content/languages/themes
74 if ( ! load_textdomain( $textdomain['domain'], WP_LANG_DIR . "/themes/{$textdomain['domain']}-$new_locale.mo" ) ) {
75 // since WP 3.7 plugins may store languages files in /wp-content/languages/plugins
76 load_textdomain( $textdomain['domain'], WP_LANG_DIR . "/plugins/{$textdomain['domain']}-$new_locale.mo" );
77 }
78 }
79 }
80 }
81
82 // first remove taxonomies and post_types labels that we don't need to translate
83 $taxonomies = array( 'language', 'term_language', 'term_translations', 'post_translations' );
84 $post_types = array( 'polylang_mo' );
85
86 // we don't need to translate core taxonomies and post types labels when setting the language from the url
87 // as they will be translated when registered the second time
88 if ( ! did_action( 'setup_theme' ) ) {
89 $taxonomies = array_merge( array( 'category', 'post_tag', 'nav_menu', 'link_category', 'post_format' ), $taxonomies );
90 $post_types = array_merge( array( 'post', 'page', 'attachment', 'revision', 'nav_menu_item' ), $post_types );
91 }
92
93 // translate labels of post types and taxonomies
94 foreach ( array_diff_key( $GLOBALS['wp_taxonomies'], array_flip( $taxonomies ) ) as $tax ) {
95 $this->translate_labels( $tax );
96 }
97 foreach ( array_diff_key( $GLOBALS['wp_post_types'], array_flip( $post_types ) ) as $pt ) {
98 $this->translate_labels( $pt );
99 }
100
101 // act only if the language has not been set early ( before default textdomain loading and $wp_locale creation )
102 if ( did_action( 'after_setup_theme' ) ) {
103 // reinitializes wp_locale for weekdays and months
104 unset( $GLOBALS['wp_locale'] );
105 $GLOBALS['wp_locale'] = new WP_Locale();
106 }
107
108 // allow plugins to translate text the same way we do for post types and taxonomies labels
109 do_action_ref_array( 'pll_translate_labels', array( &$this->labels ) );
110
111 // free memory
112 unset( $this->default_locale, $this->list_textdomains, $this->labels );
113 }
114
115 /*
116 * saves all text domains in a table for later usage
117 *
118 * @since 0.1
119 *
120 * @param bool $bool not used
121 * @param string $domain text domain name
122 * @param string $mofile translation file name
123 * @return bool always true
124 */
125 public function mofile( $bool, $domain, $mofile ) {
126 $this->list_textdomains[] = array( 'mo' => $mofile, 'domain' => $domain );
127 return true; // prevents WP loading text domains as we will load them all later
128 }
129
130 /*
131 * saves post types and taxonomies labels for a later usage
132 *
133 * @since 0.9
134 *
135 * @param string $translation not used
136 * @param string $text string to translate
137 * @param string $domain text domain
138 * @return string unmodified $translation
139 */
140 public function gettext( $translation, $text, $domain ) {
141 if ( is_string( $text ) ) { // avoid a warning with some buggy plugins which pass an array
142 $this->labels[ $text ] = array( 'domain' => $domain );
143 }
144 return $translation;
145 }
146
147 /*
148 * saves post types and taxonomies labels for a later usage
149 *
150 * @since 0.9
151 *
152 * @param string $translation not used
153 * @param string $text string to translate
154 * @param string $context some comment to describe the context of string to translate
155 * @param string $domain text domain
156 * @return string unmodified $translation
157 */
158 public function gettext_with_context( $translation, $text, $context, $domain ) {
159 $this->labels[ $text ] = array( 'domain' => $domain, 'context' => $context );
160 return $translation;
161 }
162
163 /*
164 * translates post types and taxonomies labels once the language is known
165 *
166 * @since 0.9
167 *
168 * @param object $type either a post type or a taxonomy
169 */
170 public function translate_labels( $type ) {
171 // use static array to avoid translating several times the same ( default ) labels
172 static $translated = array();
173
174 foreach ( $type->labels as $key => $label ) {
175 if ( is_string( $label ) && isset( $this->labels[ $label ] ) ) {
176 if ( empty( $translated[ $label ] ) ) {
177 $type->labels->$key = $translated[ $label ] = isset( $this->labels[ $label ]['context'] ) ?
178 _x( $label, $this->labels[ $label ]['context'], $this->labels[ $label ]['domain'] ) :
179 __( $label, $this->labels[ $label ]['domain'] );
180 }
181 else {
182 $type->labels->$key = $translated[ $label ];
183 }
184 }
185 }
186 }
187
188 /*
189 * allows Polylang to be the first plugin loaded ;- )
190 *
191 * @since 1.2
192 *
193 * @param array $plugins list of active plugins
194 * @return array list of active plugins
195 */
196 public function make_polylang_first( $plugins ) {
197 if ( $key = array_search( POLYLANG_BASENAME, $plugins ) ) {
198 unset( $plugins[ $key ] );
199 array_unshift( $plugins, POLYLANG_BASENAME );
200 }
201 return $plugins;
202 }
203 }
204