PluginProbe
Polylang / 3.7.7
Polylang v3.7.7
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 +382 -134 2.83.7.7 View file →
@@ -1,115 +1,165 @@
1 1 <?php
2 2 /**
3 + * The Polylang public API.
4 + *
3 5 * @package Polylang
4 6 */
5 7
6 8 /**
7 - * Template tag: displays the language switcher
9 + * Template tag: displays the language switcher.
10 + * The function does nothing if used outside the frontend.
8 11 *
9 - * List of parameters accepted in $args:
10 - *
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', defaults to name
17 - * force_home => forces linking to the home page is set to 1, defaults to 0
18 - * hide_if_no_translation => hides the link if there is no translation if set to 1, defaults to 0
19 - * hide_current => hides the current language if set to 1, defaults to 0
20 - * post_id => if not null, link to translations of post defined by post_id, defaults to null
21 - * 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 - *
24 12 * @api
25 13 * @since 0.5
26 14 *
27 - * @param array $args optional
28 - * @return null|string|array null if displaying, array if raw is requested, string otherwise
15 + * @param array $args {
16 + * Optional array of arguments.
17 + *
18 + * @type int $dropdown The list is displayed as dropdown if set to 1, defaults to 0.
19 + * @type int $echo Echoes the list if set to 1, defaults to 1.
20 + * @type int $hide_if_empty Hides languages with no posts ( or pages ) if set to 1, defaults to 1.
21 + * @type int $show_flags Displays flags if set to 1, defaults to 0.
22 + * @type int $show_names Shows language names if set to 1, defaults to 1.
23 + * @type string $display_names_as Whether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name.
24 + * @type int $force_home Will always link to the homepage in the translated language if set to 1, defaults to 0.
25 + * @type int $hide_if_no_translation Hides the link if there is no translation if set to 1, defaults to 0.
26 + * @type int $hide_current Hides the current language if set to 1, defaults to 0.
27 + * @type int $post_id Returns links to the translations of the post defined by post_id if set, defaults to not set.
28 + * @type int $raw Return a raw array instead of html markup if set to 1, defaults to 0.
29 + * @type string $item_spacing Whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to 'preserve'.
30 + * }
31 + * @return string|array Either the html markup of the switcher or the raw elements to build a custom language switcher.
29 32 */
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 );
33 +function pll_the_languages( $args = array() ) {
34 + if ( empty( PLL()->links ) ) {
35 + return empty( $args['raw'] ) ? '' : array();
34 36 }
35 - return '';
37 +
38 + $switcher = new PLL_Switcher();
39 + return $switcher->the_languages( PLL()->links, $args );
36 40 }
37 41
38 42 /**
39 - * Returns the current language on frontend
40 - * Returns the language set in admin language filter on backend ( false if set to all languages )
43 + * Returns the current language on frontend.
44 + * Returns the language set in admin language filter on backend (false if set to all languages).
41 45 *
42 46 * @api
43 47 * @since 0.8.1
48 + * @since 3.4 Accepts composite values.
44 49 *
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
50 + * @param string $field Optional, the language field to return (@see PLL_Language), defaults to `'slug'`.
51 + * Pass `\OBJECT` constant to get the language object. A composite value can be used for language
52 + * term property values, in the form of `{language_taxonomy_name}:{property_name}` (see
53 + * {@see PLL_Language::get_tax_prop()} for the possible values). Ex: `term_language:term_taxonomy_id`.
54 + * @return string|int|bool|string[]|PLL_Language The requested field or object for the current language, `false` if the field isn't set or if current language doesn't exist yet.
55 + *
56 + * @phpstan-return (
57 + * $field is \OBJECT ? PLL_Language : (
58 + * $field is 'slug' ? non-empty-string : string|int|bool|list<non-empty-string>
59 + * )
60 + * )|false
47 61 */
48 62 function pll_current_language( $field = 'slug' ) {
49 - if ( OBJECT === $field ) {
63 + if ( empty( PLL()->curlang ) ) {
64 + return false;
65 + }
66 +
67 + if ( \OBJECT === $field ) {
50 68 return PLL()->curlang;
51 69 }
52 - return isset( PLL()->curlang->$field ) ? PLL()->curlang->$field : false;
70 +
71 + return PLL()->curlang->get_prop( $field );
53 72 }
54 73
55 74 /**
56 - * Returns the default language
75 + * Returns the default language.
57 76 *
58 77 * @api
59 78 * @since 1.0
79 + * @since 3.4 Accepts composite values.
60 80 *
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
81 + * @param string $field Optional, the language field to return (@see PLL_Language), defaults to `'slug'`.
82 + * Pass `\OBJECT` constant to get the language object. A composite value can be used for language
83 + * term property values, in the form of `{language_taxonomy_name}:{property_name}` (see
84 + * {@see PLL_Language::get_tax_prop()} for the possible values). Ex: `term_language:term_taxonomy_id`.
85 + * @return string|int|bool|string[]|PLL_Language The requested field or object for the default language, `false` if the field isn't set or if default language doesn't exist yet.
86 + *
87 + * @phpstan-return (
88 + * $field is \OBJECT ? PLL_Language : (
89 + * $field is 'slug' ? non-empty-string : string|int|bool|list<non-empty-string>
90 + * )
91 + * )|false
63 92 */
64 93 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 - }
94 + $lang = PLL()->model->get_default_language();
95 +
96 + if ( empty( $lang ) ) {
97 + return false;
73 98 }
74 - return false;
99 +
100 + if ( \OBJECT === $field ) {
101 + return $lang;
102 + }
103 +
104 + return $lang->get_prop( $field );
75 105 }
76 106
77 107 /**
78 - * Among the post and its translations, returns the id of the post which is in the language represented by $slug
108 + * Among the post and its translations, returns the ID of the post which is in the language represented by $lang.
79 109 *
80 110 * @api
81 111 * @since 0.5
112 + * @since 3.4 Returns `0` instead of `false` if not translated or if the post has no language.
113 + * @since 3.4 $lang accepts `PLL_Language` or string.
82 114 *
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
115 + * @param int $post_id Post ID.
116 + * @param PLL_Language|string $lang Optional language (object or slug), defaults to the current language.
117 + * @return int The translation post ID if exists. 0 if not translated, the post has no language or if the language doesn't exist.
118 + *
119 + * @phpstan-return int<0, max>
86 120 */
87 -function pll_get_post( $post_id, $slug = '' ) {
88 - return ( $slug = $slug ? $slug : pll_current_language() ) ? PLL()->model->post->get( $post_id, $slug ) : null;
121 +function pll_get_post( $post_id, $lang = '' ) {
122 + $lang = $lang ?: pll_current_language();
123 +
124 + if ( empty( $lang ) ) {
125 + return 0;
126 + }
127 +
128 + return PLL()->model->post->get( $post_id, $lang );
89 129 }
90 130
91 131 /**
92 - * Among the term and its translations, returns the id of the term which is in the language represented by $slug
132 + * Among the term and its translations, returns the ID of the term which is in the language represented by $lang.
93 133 *
94 134 * @api
95 135 * @since 0.5
136 + * @since 3.4 Returns `0` instead of `false` if not translated or if the term has no language.
137 + * @since 3.4 $lang accepts PLL_Language or string.
96 138 *
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
139 + * @param int $term_id Term ID.
140 + * @param PLL_Language|string $lang Optional language (object or slug), defaults to the current language.
141 + * @return int The translation term ID if exists. 0 if not translated, the term has no language or if the language doesn't exist.
142 + *
143 + * @phpstan-return int<0, max>
100 144 */
101 -function pll_get_term( $term_id, $slug = '' ) {
102 - return ( $slug = $slug ? $slug : pll_current_language() ) ? PLL()->model->term->get( $term_id, $slug ) : null;
145 +function pll_get_term( $term_id, $lang = '' ) {
146 + $lang = $lang ?: pll_current_language();
147 +
148 + if ( empty( $lang ) ) {
149 + return 0;
150 + }
151 +
152 + return PLL()->model->term->get( $term_id, $lang );
103 153 }
104 154
105 155 /**
106 - * Returns the home url in the current language
156 + * Returns the home url in a language.
107 157 *
108 158 * @api
109 159 * @since 0.8
110 160 *
111 - * @param string $lang language code ( optional on frontend )
161 + * @param string $lang Optional language code, defaults to the current language.
112 162 * @return string
113 163 */
114 164 function pll_home_url( $lang = '' ) {
115 165 if ( empty( $lang ) ) {
@@ -115,23 +165,29 @@
115 165 if ( empty( $lang ) ) {
116 166 $lang = pll_current_language();
117 167 }
118 168
119 - return empty( $lang ) ? home_url( '/' ) : PLL()->links->get_home_url( $lang );
169 + if ( empty( $lang ) || empty( PLL()->links ) ) {
170 + return home_url( '/' );
171 + }
172 +
173 + return PLL()->links->get_home_url( $lang );
120 174 }
121 175
122 176 /**
123 - * Registers a string for translation in the "strings translation" panel
177 + * Registers a string for translation in the "strings translation" panel.
124 178 *
125 179 * @api
126 180 * @since 0.6
127 181 *
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
182 + * @param string $name A unique name for the string.
183 + * @param string $string The string to register.
184 + * @param string $context Optional, the group in which the string is registered, defaults to 'polylang'.
185 + * @param bool $multiline Optional, true if the string table should display a multiline textarea,
186 + * false if should display a single line input, defaults to false.
187 + * @return void
132 188 */
133 -function pll_register_string( $name, $string, $context = 'polylang', $multiline = false ) {
189 +function pll_register_string( $name, $string, $context = 'Polylang', $multiline = false ) {
134 190 if ( PLL() instanceof PLL_Admin_Base ) {
135 191 PLL_Admin_Strings::register_string( $name, $string, $context, $multiline );
136 192 }
137 193 }
@@ -136,18 +192,22 @@
136 192 }
137 193 }
138 194
139 195 /**
140 - * Translates a string ( previously registered with pll_register_string )
196 + * Translates a string ( previously registered with pll_register_string ).
141 197 *
142 198 * @api
143 199 * @since 0.6
144 200 *
145 - * @param string $string the string to translate
146 - * @return string the string translation in the current language
201 + * @param string $string The string to translate.
202 + * @return string The string translated in the current language.
147 203 */
148 204 function pll__( $string ) {
149 - return is_scalar( $string ) ? __( $string, 'pll_string' ) : $string; // PHPCS:ignore WordPress.WP.I18n
205 + if ( ! is_scalar( $string ) || '' === $string ) {
206 + return $string;
207 + }
208 +
209 + return __( $string, 'pll_string' ); // PHPCS:ignore WordPress.WP.I18n
150 210 }
151 211
152 212 /**
153 213 * Translates a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML output.
@@ -154,10 +214,10 @@
154 214 *
155 215 * @api
156 216 * @since 2.1
157 217 *
158 - * @param string $string the string to translate
159 - * @return string translation in the current language
218 + * @param string $string The string to translate.
219 + * @return string The string translated in the current language.
160 220 */
161 221 function pll_esc_html__( $string ) {
162 222 return esc_html( pll__( $string ) );
163 223 }
@@ -167,10 +227,10 @@
167 227 *
168 228 * @api
169 229 * @since 2.1
170 230 *
171 - * @param string $string The string to translate
172 - * @return string
231 + * @param string $string The string to translate.
232 + * @return string The string translated in the current language.
173 233 */
174 234 function pll_esc_attr__( $string ) {
175 235 return esc_attr( pll__( $string ) );
176 236 }
@@ -181,9 +241,10 @@
181 241 *
182 242 * @api
183 243 * @since 0.6
184 244 *
185 - * @param string $string The string to translate
245 + * @param string $string The string to translate.
246 + * @return void
186 247 */
187 248 function pll_e( $string ) {
188 249 echo pll__( $string ); // phpcs:ignore
189 250 }
@@ -193,9 +254,10 @@
193 254 *
194 255 * @api
195 256 * @since 2.1
196 257 *
197 - * @param string $string The string to translate
258 + * @param string $string The string to translate.
259 + * @return void
198 260 */
199 261 function pll_esc_html_e( $string ) {
200 262 echo pll_esc_html__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
201 263 }
@@ -205,9 +267,10 @@
205 267 *
206 268 * @api
207 269 * @since 2.1
208 270 *
209 - * @param string $string The string to translate
271 + * @param string $string The string to translate.
272 + * @return void
210 273 */
211 274 function pll_esc_attr_e( $string ) {
212 275 echo pll_esc_attr__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
213 276 }
@@ -212,48 +275,45 @@
212 275 echo pll_esc_attr__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput
213 276 }
214 277
215 278 /**
216 - * Translates a string ( previously registered with pll_register_string )
279 + * Translates a string ( previously registered with pll_register_string ).
217 280 *
218 281 * @api
219 282 * @since 1.5.4
220 283 *
221 - * @param string $string the string to translate
222 - * @param string $lang language code
223 - * @return string the string translation in the requested language
284 + * @param string $string The string to translate.
285 + * @param string $lang Language code.
286 + * @return string The string translated in the requested language.
224 287 */
225 288 function pll_translate_string( $string, $lang ) {
226 - if ( PLL() instanceof PLL_Frontend && pll_current_language() == $lang ) {
289 + if ( PLL() instanceof PLL_Frontend && pll_current_language() === $lang ) {
227 290 return pll__( $string );
228 291 }
229 292
230 - if ( ! is_scalar( $string ) ) {
293 + if ( ! is_scalar( $string ) || '' === $string ) {
231 294 return $string;
232 295 }
233 296
234 - static $cache; // Cache object to avoid loading the same translations object several times
297 + $lang = PLL()->model->get_language( $lang );
235 298
236 - if ( empty( $cache ) ) {
237 - $cache = new PLL_Cache();
299 + if ( empty( $lang ) ) {
300 + return $string;
238 301 }
239 302
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 - }
303 + $mo = new PLL_MO();
304 + $mo->import_from_db( $lang );
245 305
246 306 return $mo->translate( $string );
247 307 }
248 308
249 309 /**
250 - * Returns true if Polylang manages languages and translations for this post type
310 + * Returns true if Polylang manages languages and translations for this post type.
251 311 *
252 312 * @api
253 313 * @since 1.0.1
254 314 *
255 - * @param string $post_type Post type name
315 + * @param string $post_type Post type name.
256 316 * @return bool
257 317 */
258 318 function pll_is_translated_post_type( $post_type ) {
259 319 return PLL()->model->is_translated_post_type( $post_type );
@@ -259,14 +319,14 @@
259 319 return PLL()->model->is_translated_post_type( $post_type );
260 320 }
261 321
262 322 /**
263 - * Returns true if Polylang manages languages and translations for this taxonomy
323 + * Returns true if Polylang manages languages and translations for this taxonomy.
264 324 *
265 325 * @api
266 326 * @since 1.0.1
267 327 *
268 - * @param string $tax Taxonomy name
328 + * @param string $tax Taxonomy name.
269 329 * @return bool
270 330 */
271 331 function pll_is_translated_taxonomy( $tax ) {
272 332 return PLL()->model->is_translated_taxonomy( $tax );
@@ -272,20 +332,20 @@
272 332 return PLL()->model->is_translated_taxonomy( $tax );
273 333 }
274 334
275 335 /**
276 - * Returns the list of available languages
336 + * Returns the list of available languages.
277 337 *
278 - * List of parameters accepted in $args:
279 - *
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 )
282 - *
283 338 * @api
284 339 * @since 1.5
285 340 *
286 - * @param array $args list of parameters
287 - * @return array
341 + * @param array $args {
342 + * Optional array of arguments.
343 + *
344 + * @type bool $hide_empty Hides languages with no posts if set to true ( defaults to false ).
345 + * @type string $fields Return only that field if set ( @see PLL_Language for a list of fields ), defaults to 'slug'.
346 + * }
347 + * @return string[]
288 348 */
289 349 function pll_languages_list( $args = array() ) {
290 350 $args = wp_parse_args( $args, array( 'fields' => 'slug' ) );
291 351 return PLL()->model->get_languages_list( $args );
@@ -291,43 +351,60 @@
291 351 return PLL()->model->get_languages_list( $args );
292 352 }
293 353
294 354 /**
295 - * Set the post language
355 + * Sets the post language.
296 356 *
297 357 * @api
298 358 * @since 1.5
359 + * @since 3.4 $lang accepts PLL_Language or string.
360 + * @since 3.4 Returns a boolean.
299 361 *
300 - * @param int $id post id
301 - * @param string $lang language code
362 + * @param int $id Post ID.
363 + * @param PLL_Language|string $lang Language (object or slug).
364 + * @return bool True when successfully assigned. False otherwise (or if the given language is already assigned to
365 + * the post).
302 366 */
303 367 function pll_set_post_language( $id, $lang ) {
304 - PLL()->model->post->set_language( $id, $lang );
368 + return PLL()->model->post->set_language( $id, $lang );
305 369 }
306 370
307 371 /**
308 - * Set the term language
372 + * Sets the term language.
309 373 *
310 374 * @api
311 375 * @since 1.5
376 + * @since 3.4 $lang accepts PLL_Language or string.
377 + * @since 3.4 Returns a boolean.
312 378 *
313 - * @param int $id term id
314 - * @param string $lang language code
379 + * @param int $id Term ID.
380 + * @param PLL_Language|string $lang Language (object or slug).
381 + * @return bool True when successfully assigned. False otherwise (or if the given language is already assigned to
382 + * the term).
315 383 */
316 384 function pll_set_term_language( $id, $lang ) {
317 - PLL()->model->term->set_language( $id, $lang );
385 + return PLL()->model->term->set_language( $id, $lang );
318 386 }
319 387
320 388 /**
321 - * Save posts translations
389 + * Save posts translations.
322 390 *
323 391 * @api
324 392 * @since 1.5
393 + * @since 3.4 Returns an associative array of translations.
325 394 *
326 - * @param array $arr an associative array of translations with language code as key and post id as value
395 + * @param int[] $arr An associative array of translations with language code as key and post ID as value.
396 + * @return int[] An associative array with language codes as key and post IDs as values.
397 + *
398 + * @phpstan-return array<non-empty-string, positive-int>
327 399 */
328 400 function pll_save_post_translations( $arr ) {
329 - PLL()->model->post->save_translations( reset( $arr ), $arr );
401 + $id = reset( $arr );
402 + if ( $id ) {
403 + return PLL()->model->post->save_translations( $id, $arr );
404 + }
405 +
406 + return array();
330 407 }
331 408
332 409 /**
333 410 * Save terms translations
@@ -333,51 +410,94 @@
333 410 * Save terms translations
334 411 *
335 412 * @api
336 413 * @since 1.5
414 + * @since 3.4 Returns an associative array of translations.
337 415 *
338 - * @param array $arr an associative array of translations with language code as key and term id as value
416 + * @param int[] $arr An associative array of translations with language code as key and term ID as value.
417 + * @return int[] An associative array with language codes as key and term IDs as values.
418 + *
419 + * @phpstan-return array<non-empty-string, positive-int>
339 420 */
340 421 function pll_save_term_translations( $arr ) {
341 - PLL()->model->term->save_translations( reset( $arr ), $arr );
422 + $id = reset( $arr );
423 + if ( $id ) {
424 + return PLL()->model->term->save_translations( $id, $arr );
425 + }
426 +
427 + return array();
342 428 }
343 429
344 430 /**
345 - * Returns the post language
431 + * Returns the post language.
346 432 *
347 433 * @api
348 434 * @since 1.5.4
435 + * @since 3.4 Accepts composite values for `$field`.
349 436 *
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
437 + * @param int $post_id Post ID.
438 + * @param string $field Optional, the language field to return (@see PLL_Language), defaults to `'slug'`.
439 + * Pass `\OBJECT` constant to get the language object. A composite value can be used for language
440 + * term property values, in the form of `{language_taxonomy_name}:{property_name}` (see
441 + * {@see PLL_Language::get_tax_prop()} for the possible values). Ex: `term_language:term_taxonomy_id`.
442 + * @return string|int|bool|string[]|PLL_Language The requested field or object for the post language, `false` if no language is associated to that post.
443 + *
444 + * @phpstan-return (
445 + * $field is \OBJECT ? PLL_Language : (
446 + * $field is 'slug' ? non-empty-string : string|int|bool|list<non-empty-string>
447 + * )
448 + * )|false
353 449 */
354 450 function pll_get_post_language( $post_id, $field = 'slug' ) {
355 - return ( $lang = PLL()->model->post->get_language( $post_id ) ) ? $lang->$field : false;
451 + $lang = PLL()->model->post->get_language( $post_id );
452 +
453 + if ( empty( $lang ) || \OBJECT === $field ) {
454 + return $lang;
455 + }
456 +
457 + return $lang->get_prop( $field );
356 458 }
357 459
358 460 /**
359 - * Returns the term language
461 + * Returns the term language.
360 462 *
361 463 * @api
362 464 * @since 1.5.4
465 + * @since 3.4 Accepts composite values for `$field`.
363 466 *
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
467 + * @param int $term_id Term ID.
468 + * @param string $field Optional, the language field to return (@see PLL_Language), defaults to `'slug'`.
469 + * Pass `\OBJECT` constant to get the language object. A composite value can be used for language
470 + * term property values, in the form of `{language_taxonomy_name}:{property_name}` (see
471 + * {@see PLL_Language::get_tax_prop()} for the possible values). Ex: `term_language:term_taxonomy_id`.
472 + * @return string|int|bool|string[]|PLL_Language The requested field or object for the post language, `false` if no language is associated to that term.
473 + *
474 + * @phpstan-return (
475 + * $field is \OBJECT ? PLL_Language : (
476 + * $field is 'slug' ? non-empty-string : string|int|bool|list<non-empty-string>
477 + * )
478 + * )|false
367 479 */
368 480 function pll_get_term_language( $term_id, $field = 'slug' ) {
369 - return ( $lang = PLL()->model->term->get_language( $term_id ) ) ? $lang->$field : false;
481 + $lang = PLL()->model->term->get_language( $term_id );
482 +
483 + if ( empty( $lang ) || \OBJECT === $field ) {
484 + return $lang;
485 + }
486 +
487 + return $lang->get_prop( $field );
370 488 }
371 489
372 490 /**
373 - * Returns an array of translations of a post
491 + * Returns an array of translations of a post.
374 492 *
375 493 * @api
376 494 * @since 1.8
377 495 *
378 - * @param int $post_id
379 - * @return array an associative array of translations with language code as key and translation post_id as value
496 + * @param int $post_id Post ID.
497 + * @return int[] An associative array of translations with language code as key and translation post ID as value.
498 + *
499 + * @phpstan-return array<non-empty-string, positive-int>
380 500 */
381 501 function pll_get_post_translations( $post_id ) {
382 502 return PLL()->model->post->get_translations( $post_id );
383 503 }
@@ -382,15 +502,17 @@
382 502 return PLL()->model->post->get_translations( $post_id );
383 503 }
384 504
385 505 /**
386 - * Returns an array of translations of a term
506 + * Returns an array of translations of a term.
387 507 *
388 508 * @api
389 509 * @since 1.8
390 510 *
391 - * @param int $term_id
392 - * @return array an associative array of translations with language code as key and translation term_id as value
511 + * @param int $term_id Term ID.
512 + * @return int[] An associative array of translations with language code as key and translation term ID as value.
513 + *
514 + * @phpstan-return array<non-empty-string, positive-int>
393 515 */
394 516 function pll_get_term_translations( $term_id ) {
395 517 return PLL()->model->term->get_translations( $term_id );
396 518 }
@@ -395,27 +517,153 @@
395 517 return PLL()->model->term->get_translations( $term_id );
396 518 }
397 519
398 520 /**
399 - * Count posts in a language
521 + * Counts posts in a language.
400 522 *
401 523 * @api
402 524 * @since 1.5
403 525 *
404 526 * @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 ).
527 + * @param array $args {
528 + * Optional array of arguments.
529 + *
530 + * @type string $post_type Post type.
531 + * @type int $m YearMonth ( ex: 201307 ).
532 + * @type int $year 4 digit year.
533 + * @type int $monthnum Month number (from 1 to 12).
534 + * @type int $day Day of the month (from 1 to 31).
535 + * @type int $author Author id.
536 + * @type string $author_name Author nicename.
537 + * @type string $post_format Post format.
538 + * @type string $post_status Post status.
539 + * }
406 540 * @return int Posts count.
407 541 */
408 542 function pll_count_posts( $lang, $args = array() ) {
409 - return PLL()->model->count_posts( PLL()->model->get_language( $lang ), $args );
543 + $lang = PLL()->model->get_language( $lang );
544 +
545 + if ( empty( $lang ) ) {
546 + return 0;
547 + }
548 +
549 + return PLL()->model->count_posts( $lang, $args );
410 550 }
411 551
412 552 /**
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
553 + * Wraps `wp_insert_post` with language feature.
416 554 *
555 + * @since 3.7
556 + *
557 + * @param array $postarr {
558 + * An array of elements that make up a post to insert.
559 + * @See https://developer.wordpress.org/reference/functions/wp_insert_post/ wp_insert_post() for accepted arguments.
560 + *
561 + * @type string[] $translations The translation group to assign to the post with language slug as keys and post ID as values.
562 + * }
563 + * @param PLL_Language|string $language The post language object or slug.
564 + * @return int|WP_Error The post ID on success. The value `WP_Error` on failure.
565 + */
566 +function pll_insert_post( array $postarr, $language ) {
567 + $language = PLL()->model->get_language( $language );
568 +
569 + if ( ! $language instanceof PLL_Language ) {
570 + return new WP_Error( 'invalid_language', __( 'Please provide a valid language.', 'polylang' ) );
571 + }
572 +
573 + return PLL()->model->post->insert( $postarr, $language );
574 +}
575 +
576 +/**
577 + * Wraps `wp_insert_term` with language feature.
578 + *
579 + * @since 3.7
580 + *
581 + * @param string $term The term name to add.
582 + * @param string $taxonomy The taxonomy to which to add the term.
583 + * @param PLL_Language|string $language The term language object or slug.
584 + * @param array $args {
585 + * Optional. Array of arguments for inserting a term.
586 + *
587 + * @type string $alias_of Slug of the term to make this term an alias of.
588 + * Default empty string. Accepts a term slug.
589 + * @type string $description The term description. Default empty string.
590 + * @type int $parent The id of the parent term. Default 0.
591 + * @type string $slug The term slug to use. Default empty string.
592 + * @type string[] $translations The translation group to assign to the term with language slug as keys and `term_id` as values.
593 + * }
594 + * @return array|WP_Error {
595 + * An array of the new term data, `WP_Error` otherwise.
596 + *
597 + * @type int $term_id The new term ID.
598 + * @type int|string $term_taxonomy_id The new term taxonomy ID. Can be a numeric string.
599 + * }
600 + */
601 +function pll_insert_term( string $term, string $taxonomy, $language, array $args = array() ) {
602 + $language = PLL()->model->get_language( $language );
603 +
604 + if ( ! $language instanceof PLL_Language ) {
605 + return new WP_Error( 'invalid_language', __( 'Please provide a valid language.', 'polylang' ) );
606 + }
607 +
608 + return PLL()->model->term->insert( $term, $taxonomy, $language, $args );
609 +}
610 +
611 +/**
612 + * Wraps `wp_update_post` with language feature.
613 + *
614 + * @since 3.7
615 + *
616 + * @param array $postarr {
617 + * Optional. An array of elements that make up a post to update.
618 + * @See https://developer.wordpress.org/reference/functions/wp_insert_post/ wp_insert_post() for accepted arguments.
619 + *
620 + * @type PLL_Language|string $lang The post language object or slug.
621 + * @type string[] $translations The translation group to assign to the post with language slug as keys and post ID as values.
622 + * }
623 + * @return int|WP_Error The post ID on success. The value `WP_Error` on failure.
624 + */
625 +function pll_update_post( array $postarr ) {
626 + return PLL()->model->post->update( $postarr );
627 +}
628 +
629 +/**
630 + * Wraps `wp_update_term` with language feature.
631 + *
632 + * @since 3.7
633 + *
634 + * @param int $term_id The ID of the term.
635 + * @param array $args {
636 + * Optional. Array of arguments for updating a term.
637 + *
638 + * @type string $alias_of Slug of the term to make this term an alias of.
639 + * Default empty string. Accepts a term slug.
640 + * @type string $description The term description. Default empty string.
641 + * @type int $parent The id of the parent term. Default 0.
642 + * @type string $slug The term slug to use. Default empty string.
643 + * @type string $name The term name.
644 + * @type PLL_Language|string $lang The term language object or slug.
645 + * @type string[] $translations The translation group to assign to the term with language slug as keys and `term_id` as values.
646 + * }
647 + * @return array|WP_Error {
648 + * An array containing the `term_id` and `term_taxonomy_id`, `WP_Error` otherwise.
649 + *
650 + * @type int $term_id The new term ID.
651 + * @type int|string $term_taxonomy_id The new term taxonomy ID. Can be a numeric string.
652 + * }
653 + */
654 +function pll_update_term( int $term_id, array $args = array() ) {
655 + return PLL()->model->term->update( $term_id, $args );
656 +}
657 +
658 +/**
659 + * Allows to access the Polylang instance.
660 + * However, it is always preferable to use API functions
661 + * as internal methods may be changed without prior notice.
662 + *
417 663 * @since 1.8
664 + *
665 + * @return PLL_Frontend|PLL_Admin|PLL_Settings|PLL_REST_Request
418 666 */
419 667 function PLL() { // PHPCS:ignore WordPress.NamingConventions.ValidFunctionName
420 668 return $GLOBALS['polylang'];
421 669 }