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