PluginProbe
Polylang / 2.0
Polylang v2.0
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 2.0, at include/olt-manager.php

211 lines 7.4 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 /**
109 * Fires after the post types and taxonomies labels have been translated
110 * This allows plugins to translate text the same way we do for post types and taxonomies labels
111 *
112 * @since 1.2
113 *
114 * @param array $labels list of strings to trnaslate
115 */
116 do_action_ref_array( 'pll_translate_labels', array( &$this->labels ) );
117
118 // free memory
119 unset( $this->default_locale, $this->list_textdomains, $this->labels );
120 }
121
122 /**
123 * saves all text domains in a table for later usage
124 *
125 * @since 0.1
126 *
127 * @param bool $bool not used
128 * @param string $domain text domain name
129 * @param string $mofile translation file name
130 * @return bool always true
131 */
132 public function mofile( $bool, $domain, $mofile ) {
133 $this->list_textdomains[] = array( 'mo' => $mofile, 'domain' => $domain );
134 return true; // prevents WP loading text domains as we will load them all later
135 }
136
137 /**
138 * saves post types and taxonomies labels for a later usage
139 *
140 * @since 0.9
141 *
142 * @param string $translation not used
143 * @param string $text string to translate
144 * @param string $domain text domain
145 * @return string unmodified $translation
146 */
147 public function gettext( $translation, $text, $domain ) {
148 if ( is_string( $text ) ) { // avoid a warning with some buggy plugins which pass an array
149 $this->labels[ $text ] = array( 'domain' => $domain );
150 }
151 return $translation;
152 }
153
154 /**
155 * saves post types and taxonomies labels for a later usage
156 *
157 * @since 0.9
158 *
159 * @param string $translation not used
160 * @param string $text string to translate
161 * @param string $context some comment to describe the context of string to translate
162 * @param string $domain text domain
163 * @return string unmodified $translation
164 */
165 public function gettext_with_context( $translation, $text, $context, $domain ) {
166 $this->labels[ $text ] = array( 'domain' => $domain, 'context' => $context );
167 return $translation;
168 }
169
170 /**
171 * translates post types and taxonomies labels once the language is known
172 *
173 * @since 0.9
174 *
175 * @param object $type either a post type or a taxonomy
176 */
177 public function translate_labels( $type ) {
178 // use static array to avoid translating several times the same ( default ) labels
179 static $translated = array();
180
181 foreach ( $type->labels as $key => $label ) {
182 if ( is_string( $label ) && isset( $this->labels[ $label ] ) ) {
183 if ( empty( $translated[ $label ] ) ) {
184 $type->labels->$key = $translated[ $label ] = isset( $this->labels[ $label ]['context'] ) ?
185 _x( $label, $this->labels[ $label ]['context'], $this->labels[ $label ]['domain'] ) :
186 __( $label, $this->labels[ $label ]['domain'] );
187 }
188 else {
189 $type->labels->$key = $translated[ $label ];
190 }
191 }
192 }
193 }
194
195 /**
196 * allows Polylang to be the first plugin loaded ;- )
197 *
198 * @since 1.2
199 *
200 * @param array $plugins list of active plugins
201 * @return array list of active plugins
202 */
203 public function make_polylang_first( $plugins ) {
204 if ( $key = array_search( POLYLANG_BASENAME, $plugins ) ) {
205 unset( $plugins[ $key ] );
206 array_unshift( $plugins, POLYLANG_BASENAME );
207 }
208 return $plugins;
209 }
210 }
211