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