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