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