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