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
← All changes | include/api.php +123 -289 2.81.5 View file →
@@ -1,421 +1,255 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 -/**
7 - * Template tag: displays the language switcher
3 +/*
4 + * template tag: displays the language switcher
8 5 *
9 - * List of parameters accepted in $args:
6 + * list of parameters accepted in $args:
10 7 *
11 8 * dropdown => displays a dropdown if set to 1, defaults to 0
12 - * echo => echoes the switcher if set to 1 ( default )
13 - * hide_if_empty => hides languages with no posts ( or pages ) if set to 1 ( default )
9 + * echo => echoes the the switcher if set to 1 (default)
10 + * hide_if_empty => hides languages with no posts (or pages) if set to 1 (default)
14 11 * show_flags => shows flags if set to 1, defaults to 0
15 - * show_names => shows languages names if set to 1 ( default )
16 - * display_names_as => whether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name
12 + * show_names => shows languages names if set to 1 (default)
13 + * display_names_as => wether to display the language name or code. valid options are 'slug' and 'name'
17 14 * force_home => forces linking to the home page is set to 1, defaults to 0
18 15 * hide_if_no_translation => hides the link if there is no translation if set to 1, defaults to 0
19 16 * hide_current => hides the current language if set to 1, defaults to 0
20 17 * post_id => if not null, link to translations of post defined by post_id, defaults to null
21 18 * raw => set this to true to build your own custom language switcher, defaults to 0
22 - * item_spacing => whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to preserve
23 19 *
24 - * @api
25 20 * @since 0.5
26 21 *
27 22 * @param array $args optional
28 23 * @return null|string|array null if displaying, array if raw is requested, string otherwise
29 24 */
30 -function pll_the_languages( $args = '' ) {
31 - if ( PLL() instanceof PLL_Frontend ) {
32 - $switcher = new PLL_Switcher();
33 - return $switcher->the_languages( PLL()->links, $args );
25 +function pll_the_languages($args = '') {
26 + global $polylang;
27 + if ($polylang instanceof PLL_Frontend && !empty($polylang->links)) {
28 + $switcher = new PLL_Switcher;
29 + return $switcher->the_languages($polylang->links, $args);
34 30 }
35 31 return '';
36 32 }
37 33
38 -/**
39 - * Returns the current language on frontend
40 - * Returns the language set in admin language filter on backend ( false if set to all languages )
34 +/*
35 + * returns the current language
41 36 *
42 - * @api
43 37 * @since 0.8.1
44 38 *
45 - * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug', pass OBJECT constant to get the language object.
46 - * @return string|PLL_Language|bool The requested field for the current language
39 + * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug'
40 + * @return string the requested field for the current language
47 41 */
48 -function pll_current_language( $field = 'slug' ) {
49 - if ( OBJECT === $field ) {
50 - return PLL()->curlang;
51 - }
52 - return isset( PLL()->curlang->$field ) ? PLL()->curlang->$field : false;
42 +function pll_current_language($field = 'slug') {
43 + global $polylang;
44 + return isset($polylang->curlang->$field) ? $polylang->curlang->$field : false;
53 45 }
54 46
55 -/**
56 - * Returns the default language
47 +/*
48 + * returns the default language
57 49 *
58 - * @api
59 50 * @since 1.0
60 51 *
61 - * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug', pass OBJECT constant to get the language object.
62 - * @return string|PLL_Language|bool The requested field for the default language
52 + * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug'
53 + * @return string the requested field for the default language
63 54 */
64 -function pll_default_language( $field = 'slug' ) {
65 - if ( isset( PLL()->options['default_lang'] ) ) {
66 - $lang = PLL()->model->get_language( PLL()->options['default_lang'] );
67 - if ( $lang ) {
68 - if ( OBJECT === $field ) {
69 - return $lang;
70 - }
71 - return isset( $lang->$field ) ? $lang->$field : false;
72 - }
73 - }
74 - return false;
55 +function pll_default_language($field = 'slug') {
56 + global $polylang;
57 + return isset($polylang->options['default_lang']) && ($lang = $polylang->model->get_language($polylang->options['default_lang'])) && isset($lang->$field) ? $lang->$field : false;
75 58 }
76 59
77 -/**
78 - * Among the post and its translations, returns the id of the post which is in the language represented by $slug
60 +/*
61 + * among the post and its translations, returns the id of the post which is in the language represented by $slug
79 62 *
80 - * @api
81 63 * @since 0.5
82 64 *
83 - * @param int $post_id post id
84 - * @param string $slug optional language code, defaults to current language
85 - * @return int|false|null post id of the translation if exists, false otherwise, null if the current language is not defined yet
65 + * @param int $post_id post id
66 + * @param string $slug optional language code, defaults to current language
67 + * @return int post id of the translation if exists
86 68 */
87 -function pll_get_post( $post_id, $slug = '' ) {
88 - return ( $slug = $slug ? $slug : pll_current_language() ) ? PLL()->model->post->get( $post_id, $slug ) : null;
69 +function pll_get_post($post_id, $slug = '') {
70 + global $polylang;
71 + return isset($polylang) && ($slug = $slug ? $slug : pll_current_language()) ? $polylang->model->get_post($post_id, $slug) : null;
89 72 }
90 73
91 -/**
92 - * Among the term and its translations, returns the id of the term which is in the language represented by $slug
74 +/*
75 + * among the term and its translations, returns the id of the term which is in the language represented by $slug
93 76 *
94 - * @api
95 77 * @since 0.5
96 78 *
97 - * @param int $term_id term id
98 - * @param string $slug optional language code, defaults to current language
99 - * @return int|false|null term id of the translation if exists, false otherwise, null if the current language is not defined yet
79 + * @param int $term_id term id
80 + * @param string $slug optional language code, defaults to current language
81 + * @return int term id of the translation if exists
100 82 */
101 -function pll_get_term( $term_id, $slug = '' ) {
102 - return ( $slug = $slug ? $slug : pll_current_language() ) ? PLL()->model->term->get( $term_id, $slug ) : null;
83 +function pll_get_term($term_id, $slug = '') {
84 + global $polylang;
85 + return isset($polylang) && ($slug = $slug ? $slug : pll_current_language()) ? $polylang->model->get_term($term_id, $slug) : null;
103 86 }
104 87
105 -/**
106 - * Returns the home url in the current language
88 +/*
89 + * returns the home url in the current language
107 90 *
108 - * @api
109 91 * @since 0.8
110 92 *
111 - * @param string $lang language code ( optional on frontend )
93 + * @param string $lang language code (optional on frontend)
112 94 * @return string
113 95 */
114 -function pll_home_url( $lang = '' ) {
115 - if ( empty( $lang ) ) {
96 +function pll_home_url($lang = '') {
97 + global $polylang;
98 +
99 + if (empty($lang))
116 100 $lang = pll_current_language();
117 - }
118 101
119 - return empty( $lang ) ? home_url( '/' ) : PLL()->links->get_home_url( $lang );
102 + return isset($polylang) && !empty($polylang->links) && !empty($lang) ? $polylang->links->get_home_url($lang) : home_url('/');
120 103 }
121 104
122 -/**
123 - * Registers a string for translation in the "strings translation" panel
105 +/*
106 + * registers a string for translation in the "strings translation" panel
124 107 *
125 - * @api
126 108 * @since 0.6
127 109 *
128 - * @param string $name a unique name for the string
129 - * @param string $string the string to register
130 - * @param string $context optional the group in which the string is registered, defaults to 'polylang'
131 - * @param bool $multiline optional whether the string table should display a multiline textarea or a single line input, defaults to single line
110 + * @param string $name a unique name for the string
111 + * @param string $string the string to register
112 + * @param string $context optional the group in which the string is registered, defaults to 'polylang'
113 + * @param bool $multiline optional wether the string table should display a multiline textarea or a single line input, defaults to single line
132 114 */
133 -function pll_register_string( $name, $string, $context = 'polylang', $multiline = false ) {
134 - if ( PLL() instanceof PLL_Admin_Base ) {
135 - PLL_Admin_Strings::register_string( $name, $string, $context, $multiline );
136 - }
115 +function pll_register_string($name, $string, $context = 'polylang', $multiline = false) {
116 + global $polylang;
117 + if ($polylang instanceof PLL_Admin && !empty($polylang->settings_page))
118 + $polylang->settings_page->register_string($name, $string, $context, $multiline);
137 119 }
138 120
139 -/**
140 - * Translates a string ( previously registered with pll_register_string )
121 +/*
122 + * translates a string (previously registered with pll_register_string)
141 123 *
142 - * @api
143 124 * @since 0.6
144 125 *
145 126 * @param string $string the string to translate
146 127 * @return string the string translation in the current language
147 128 */
148 -function pll__( $string ) {
149 - return is_scalar( $string ) ? __( $string, 'pll_string' ) : $string; // PHPCS:ignore WordPress.WP.I18n
129 +function pll__($string) {
130 + return __($string, 'pll_string');
150 131 }
151 132
152 -/**
153 - * Translates a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML output.
133 +/*
134 + * echoes a translated string (previously registered with pll_register_string)
154 135 *
155 - * @api
156 - * @since 2.1
157 - *
158 - * @param string $string the string to translate
159 - * @return string translation in the current language
160 - */
161 -function pll_esc_html__( $string ) {
162 - return esc_html( pll__( $string ) );
163 -}
164 -
165 -/**
166 - * Translates a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML attributes.
167 - *
168 - * @api
169 - * @since 2.1
170 - *
171 - * @param string $string The string to translate
172 - * @return string
173 - */
174 -function pll_esc_attr__( $string ) {
175 - return esc_attr( pll__( $string ) );
176 -}
177 -
178 -/**
179 - * Echoes a translated string ( previously registered with pll_register_string )
180 - * It is an equivalent of _e() and is not escaped.
181 - *
182 - * @api
183 136 * @since 0.6
184 137 *
185 - * @param string $string The string to translate
186 - */
187 -function pll_e( $string ) {
188 - echo pll__( $string ); // phpcs:ignore
189 -}
190 -
191 -/**
192 - * Echoes a translated string ( previously registered with pll_register_string ) and escapes it for safe use in HTML output.
193 - *
194 - * @api
195 - * @since 2.1
196 - *
197 - * @param string $string The string to translate
198 - */
199 -function pll_esc_html_e( $string ) {
200 - echo pll_esc_html__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
201 -}
202 -
203 -/**
204 - * Echoes a translated a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML attributes.
205 - *
206 - * @api
207 - * @since 2.1
208 - *
209 - * @param string $string The string to translate
210 - */
211 -function pll_esc_attr_e( $string ) {
212 - echo pll_esc_attr__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
213 -}
214 -
215 -/**
216 - * Translates a string ( previously registered with pll_register_string )
217 - *
218 - * @api
219 - * @since 1.5.4
220 - *
221 138 * @param string $string the string to translate
222 - * @param string $lang language code
223 - * @return string the string translation in the requested language
224 139 */
225 -function pll_translate_string( $string, $lang ) {
226 - if ( PLL() instanceof PLL_Frontend && pll_current_language() == $lang ) {
227 - return pll__( $string );
228 - }
229 -
230 - if ( ! is_scalar( $string ) ) {
231 - return $string;
232 - }
233 -
234 - static $cache; // Cache object to avoid loading the same translations object several times
235 -
236 - if ( empty( $cache ) ) {
237 - $cache = new PLL_Cache();
238 - }
239 -
240 - if ( false === $mo = $cache->get( $lang ) ) {
241 - $mo = new PLL_MO();
242 - $mo->import_from_db( PLL()->model->get_language( $lang ) );
243 - $cache->set( $lang, $mo );
244 - }
245 -
246 - return $mo->translate( $string );
140 +function pll_e($string) {
141 + _e($string, 'pll_string');
247 142 }
248 143
249 -/**
250 - * Returns true if Polylang manages languages and translations for this post type
144 +/*
145 + * returns true if Polylang manages languages and translations for this post type
251 146 *
252 - * @api
253 147 * @since 1.0.1
254 148 *
255 - * @param string $post_type Post type name
149 + * @param string post type name
256 150 * @return bool
257 151 */
258 -function pll_is_translated_post_type( $post_type ) {
259 - return PLL()->model->is_translated_post_type( $post_type );
152 +function pll_is_translated_post_type($post_type) {
153 + global $polylang;
154 + return isset($polylang) && $polylang->model->is_translated_post_type($post_type);
260 155 }
261 156
262 -/**
263 - * Returns true if Polylang manages languages and translations for this taxonomy
157 +/*
158 + * returns true if Polylang manages languages and translations for this taxonomy
264 159 *
265 - * @api
266 160 * @since 1.0.1
267 161 *
268 - * @param string $tax Taxonomy name
162 + * @param string taxonomy name
269 163 * @return bool
270 164 */
271 -function pll_is_translated_taxonomy( $tax ) {
272 - return PLL()->model->is_translated_taxonomy( $tax );
165 +function pll_is_translated_taxonomy($tax) {
166 + global $polylang;
167 + return isset($polylang) && $polylang->model->is_translated_taxonomy($tax);
273 168 }
274 169
275 -/**
276 - * Returns the list of available languages
170 +/*
171 + * returns the list of available languages
277 172 *
278 - * List of parameters accepted in $args:
173 + * list of parameters accepted in $args:
279 174 *
280 - * hide_empty => hides languages with no posts if set to true ( defaults to false )
281 - * fields => return only that field if set ( see PLL_Language for a list of fields )
175 + * hide_empty => hides languages with no posts if set to true (defaults to false)
176 + * fields => return only that field if set (see PLL_Language for a list of fields)
282 177 *
283 - * @api
284 178 * @since 1.5
285 179 *
286 180 * @param array $args list of parameters
287 181 * @return array
288 182 */
289 -function pll_languages_list( $args = array() ) {
290 - $args = wp_parse_args( $args, array( 'fields' => 'slug' ) );
291 - return PLL()->model->get_languages_list( $args );
183 +function pll_languages_list($args = array()) {
184 + global $polylang;
185 + $args = wp_parse_args($args, array('fields' => 'slug'));
186 + return isset($polylang) ? $polylang->model->get_languages_list($args) : false;
292 187 }
293 188
294 -/**
295 - * Set the post language
189 +/*
190 + * set the post language
296 191 *
297 - * @api
298 192 * @since 1.5
299 193 *
300 - * @param int $id post id
194 + * @param int $post_id post id
301 195 * @param string $lang language code
302 196 */
303 -function pll_set_post_language( $id, $lang ) {
304 - PLL()->model->post->set_language( $id, $lang );
197 +function pll_set_post_language($id, $lang) {
198 + global $polylang;
199 + if (isset($polylang))
200 + $polylang->model->set_post_language($id, $lang);
305 201 }
306 202
307 -/**
308 - * Set the term language
203 +/*
204 + * set the term language
309 205 *
310 - * @api
311 206 * @since 1.5
312 207 *
313 - * @param int $id term id
208 + * @param int $id term id
314 209 * @param string $lang language code
315 210 */
316 -function pll_set_term_language( $id, $lang ) {
317 - PLL()->model->term->set_language( $id, $lang );
211 +function pll_set_term_language($id, $lang) {
212 + global $polylang;
213 + if (isset($polylang))
214 + $polylang->model->set_term_language($id, $lang);
318 215 }
319 216
320 -/**
321 - * Save posts translations
217 +/*
218 + * save posts translations
322 219 *
323 - * @api
324 220 * @since 1.5
325 221 *
326 222 * @param array $arr an associative array of translations with language code as key and post id as value
327 223 */
328 -function pll_save_post_translations( $arr ) {
329 - PLL()->model->post->save_translations( reset( $arr ), $arr );
224 +function pll_save_post_translations($arr) {
225 + global $polylang;
226 + if (isset($polylang))
227 + $polylang->model->save_translations('post', reset($arr), $arr);
330 228 }
331 229
332 -/**
333 - * Save terms translations
230 +/*
231 + * save terms translations
334 232 *
335 - * @api
336 233 * @since 1.5
337 234 *
338 235 * @param array $arr an associative array of translations with language code as key and term id as value
339 236 */
340 -function pll_save_term_translations( $arr ) {
341 - PLL()->model->term->save_translations( reset( $arr ), $arr );
237 +function pll_save_term_translations($arr) {
238 + global $polylang;
239 + if (isset($polylang))
240 + $polylang->model->save_translations('term', reset($arr), $arr);
342 241 }
343 242
344 -/**
345 - * Returns the post language
243 +/*
244 + * count posts in a language
346 245 *
347 - * @api
348 - * @since 1.5.4
349 - *
350 - * @param int $post_id
351 - * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug'
352 - * @return bool|string The requested field for the post language, false if no language is associated to that post
353 - */
354 -function pll_get_post_language( $post_id, $field = 'slug' ) {
355 - return ( $lang = PLL()->model->post->get_language( $post_id ) ) ? $lang->$field : false;
356 -}
357 -
358 -/**
359 - * Returns the term language
360 - *
361 - * @api
362 - * @since 1.5.4
363 - *
364 - * @param int $term_id
365 - * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug'
366 - * @return bool|string The requested field for the term language, false if no language is associated to that term
367 - */
368 -function pll_get_term_language( $term_id, $field = 'slug' ) {
369 - return ( $lang = PLL()->model->term->get_language( $term_id ) ) ? $lang->$field : false;
370 -}
371 -
372 -/**
373 - * Returns an array of translations of a post
374 - *
375 - * @api
376 - * @since 1.8
377 - *
378 - * @param int $post_id
379 - * @return array an associative array of translations with language code as key and translation post_id as value
380 - */
381 -function pll_get_post_translations( $post_id ) {
382 - return PLL()->model->post->get_translations( $post_id );
383 -}
384 -
385 -/**
386 - * Returns an array of translations of a term
387 - *
388 - * @api
389 - * @since 1.8
390 - *
391 - * @param int $term_id
392 - * @return array an associative array of translations with language code as key and translation term_id as value
393 - */
394 -function pll_get_term_translations( $term_id ) {
395 - return PLL()->model->term->get_translations( $term_id );
396 -}
397 -
398 -/**
399 - * Count posts in a language
400 - *
401 - * @api
402 246 * @since 1.5
403 247 *
404 - * @param string $lang Language code.
405 - * @param array $args WP_Query arguments ( accepted keys: post_type, m, year, monthnum, day, author, author_name, post_format, post_status ).
406 - * @return int Posts count.
248 + * @param string $lang language code
249 + * @param array $args (accepted keys: post_type, m, year, monthnum, day, author, author_name, post_format)
250 + * @return int posts count
407 251 */
408 -function pll_count_posts( $lang, $args = array() ) {
409 - return PLL()->model->count_posts( PLL()->model->get_language( $lang ), $args );
410 -}
411 -
412 -/**
413 - * Allows to access the Polylang instance
414 - * It is always preferable to use API functions
415 - * Internal methods may be changed without prior notice
416 - *
417 - * @since 1.8
418 - */
419 -function PLL() { // PHPCS:ignore WordPress.NamingConventions.ValidFunctionName
420 - return $GLOBALS['polylang'];
252 +function pll_count_posts($lang, $args) {
253 + global $polylang;
254 + return isset($polylang) ? $polylang->model->count_posts($polylang->model->get_language($lang)) : false;
421 255 }