| @@ -5,19 +5,18 @@ | ||
| 5 | 5 | * |
| 6 | 6 | * List of parameters accepted in $args: |
| 7 | 7 | * |
| 8 | 8 | * dropdown => displays a dropdown if set to 1, defaults to 0 |
| 9 | - * echo => echoes the switcher if set to 1 ( default ) | |
| 9 | + * echo => echoes the the switcher if set to 1 ( default ) | |
| 10 | 10 | * hide_if_empty => hides languages with no posts ( or pages ) if set to 1 ( default ) |
| 11 | 11 | * show_flags => shows flags if set to 1, defaults to 0 |
| 12 | 12 | * show_names => shows languages names if set to 1 ( default ) |
| 13 | - * 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' | |
| 14 | 14 | * force_home => forces linking to the home page is set to 1, defaults to 0 |
| 15 | 15 | * hide_if_no_translation => hides the link if there is no translation if set to 1, defaults to 0 |
| 16 | 16 | * hide_current => hides the current language if set to 1, defaults to 0 |
| 17 | 17 | * post_id => if not null, link to translations of post defined by post_id, defaults to null |
| 18 | 18 | * raw => set this to true to build your own custom language switcher, defaults to 0 |
| 19 | - * item_spacing => whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to preserve | |
| 20 | 19 | * |
| 21 | 20 | * @since 0.5 |
| 22 | 21 | * |
| 23 | 22 | * @param array $args optional |
| @@ -24,9 +23,9 @@ | ||
| 24 | 23 | * @return null|string|array null if displaying, array if raw is requested, string otherwise |
| 25 | 24 | */ |
| 26 | 25 | function pll_the_languages( $args = '' ) { |
| 27 | 26 | if ( PLL() instanceof PLL_Frontend ) { |
| 28 | - $switcher = new PLL_Switcher(); | |
| 27 | + $switcher = new PLL_Switcher; | |
| 29 | 28 | return $switcher->the_languages( PLL()->links, $args ); |
| 30 | 29 | } |
| 31 | 30 | return ''; |
| 32 | 31 | } |
| @@ -36,15 +35,12 @@ | ||
| 36 | 35 | * Returns the language set in admin language filter on backend ( false if set to all languages ) |
| 37 | 36 | * |
| 38 | 37 | * @since 0.8.1 |
| 39 | 38 | * |
| 40 | - * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug', pass OBJECT constant to get the language object. | |
| 41 | - * @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 | |
| 42 | 41 | */ |
| 43 | 42 | function pll_current_language( $field = 'slug' ) { |
| 44 | - if ( OBJECT === $field ) { | |
| 45 | - return PLL()->curlang; | |
| 46 | - } | |
| 47 | 43 | return isset( PLL()->curlang->$field ) ? PLL()->curlang->$field : false; |
| 48 | 44 | } |
| 49 | 45 | |
| 50 | 46 | /** |
| @@ -51,22 +47,13 @@ | ||
| 51 | 47 | * Returns the default language |
| 52 | 48 | * |
| 53 | 49 | * @since 1.0 |
| 54 | 50 | * |
| 55 | - * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug', pass OBJECT constant to get the language object. | |
| 56 | - * @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 | |
| 57 | 53 | */ |
| 58 | 54 | function pll_default_language( $field = 'slug' ) { |
| 59 | - if ( isset( PLL()->options['default_lang'] ) ) { | |
| 60 | - $lang = PLL()->model->get_language( PLL()->options['default_lang'] ); | |
| 61 | - if ( $lang ) { | |
| 62 | - if ( OBJECT === $field ) { | |
| 63 | - return $lang; | |
| 64 | - } | |
| 65 | - return isset( $lang->$field ) ? $lang->$field : false; | |
| 66 | - } | |
| 67 | - } | |
| 68 | - return false; | |
| 55 | + return isset( PLL()->options['default_lang'] ) && ( $lang = PLL()->model->get_language( PLL()->options['default_lang'] ) ) && isset( $lang->$field ) ? $lang->$field : false; | |
| 69 | 56 | } |
| 70 | 57 | |
| 71 | 58 | /** |
| 72 | 59 | * Among the post and its translations, returns the id of the post which is in the language represented by $slug |
| @@ -117,9 +104,9 @@ | ||
| 117 | 104 | * |
| 118 | 105 | * @param string $name a unique name for the string |
| 119 | 106 | * @param string $string the string to register |
| 120 | 107 | * @param string $context optional the group in which the string is registered, defaults to 'polylang' |
| 121 | - * @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 | |
| 122 | 109 | */ |
| 123 | 110 | function pll_register_string( $name, $string, $context = 'polylang', $multiline = false ) { |
| 124 | 111 | if ( PLL() instanceof PLL_Admin_Base ) { |
| 125 | 112 | PLL_Admin_Strings::register_string( $name, $string, $context, $multiline ); |
| @@ -134,9 +121,13 @@ | ||
| 134 | 121 | * @param string $string the string to translate |
| 135 | 122 | * @return string the string translation in the current language |
| 136 | 123 | */ |
| 137 | 124 | function pll__( $string ) { |
| 138 | - return is_scalar( $string ) ? __( $string, 'pll_string' ) : $string; // PHPCS:ignore WordPress.WP.I18n | |
| 125 | + if ( ! did_action( 'pll_language_defined' ) || ! is_scalar( $string ) ) { // No need for translation | |
| 126 | + return $string; | |
| 127 | + } | |
| 128 | + | |
| 129 | + return __( $string, 'pll_string' ); | |
| 139 | 130 | } |
| 140 | 131 | |
| 141 | 132 | /** |
| 142 | 133 | * Translates a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML output. |
| @@ -154,9 +145,9 @@ | ||
| 154 | 145 | * Translates a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML attributes. |
| 155 | 146 | * |
| 156 | 147 | * @since 2.1 |
| 157 | 148 | * |
| 158 | - * @param string $string The string to translate | |
| 149 | + * @param $string | |
| 159 | 150 | * @return string |
| 160 | 151 | */ |
| 161 | 152 | function pll_esc_attr__( $string ) { |
| 162 | 153 | return esc_attr( pll__( $string ) ); |
| @@ -163,16 +154,15 @@ | ||
| 163 | 154 | } |
| 164 | 155 | |
| 165 | 156 | /** |
| 166 | 157 | * Echoes a translated string ( previously registered with pll_register_string ) |
| 167 | - * It is an equivalent of _e() and is not escaped. | |
| 168 | 158 | * |
| 169 | 159 | * @since 0.6 |
| 170 | 160 | * |
| 171 | - * @param string $string The string to translate | |
| 161 | + * @param string $string the string to translate | |
| 172 | 162 | */ |
| 173 | 163 | function pll_e( $string ) { |
| 174 | - echo pll__( $string ); // phpcs:ignore | |
| 164 | + echo pll__( $string ); | |
| 175 | 165 | } |
| 176 | 166 | |
| 177 | 167 | /** |
| 178 | 168 | * Echoes a translated string ( previously registered with pll_register_string ) and escapes it for safe use in HTML output. |
| @@ -178,12 +168,12 @@ | ||
| 178 | 168 | * Echoes a translated string ( previously registered with pll_register_string ) and escapes it for safe use in HTML output. |
| 179 | 169 | * |
| 180 | 170 | * @since 2.1 |
| 181 | 171 | * |
| 182 | - * @param string $string The string to translate | |
| 172 | + * @param string $string the string to translate | |
| 183 | 173 | */ |
| 184 | 174 | function pll_esc_html_e( $string ) { |
| 185 | - echo pll_esc_html__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput | |
| 175 | + echo pll_esc_html__( $string ); | |
| 186 | 176 | } |
| 187 | 177 | |
| 188 | 178 | /** |
| 189 | 179 | * Echoes a translated a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML attributes. |
| @@ -189,12 +179,12 @@ | ||
| 189 | 179 | * Echoes a translated a string ( previously registered with pll_register_string ) and escapes it for safe use in HTML attributes. |
| 190 | 180 | * |
| 191 | 181 | * @since 2.1 |
| 192 | 182 | * |
| 193 | - * @param string $string The string to translate | |
| 183 | + * @param $string | |
| 194 | 184 | */ |
| 195 | 185 | function pll_esc_attr_e( $string ) { |
| 196 | - echo pll_esc_attr__( $string ); // phpcs:ignore WordPress.Security.EscapeOutput | |
| 186 | + echo pll_esc_attr__( $string ); | |
| 197 | 187 | } |
| 198 | 188 | |
| 199 | 189 | /** |
| 200 | 190 | * Translates a string ( previously registered with pll_register_string ) |
| @@ -205,9 +195,9 @@ | ||
| 205 | 195 | * @param string $lang language code |
| 206 | 196 | * @return string the string translation in the requested language |
| 207 | 197 | */ |
| 208 | 198 | function pll_translate_string( $string, $lang ) { |
| 209 | - if ( PLL() instanceof PLL_Frontend && pll_current_language() == $lang ) { | |
| 199 | + if ( pll_current_language() == $lang ) { | |
| 210 | 200 | return pll__( $string ); |
| 211 | 201 | } |
| 212 | 202 | |
| 213 | 203 | if ( ! is_scalar( $string ) ) { |
| @@ -233,9 +223,9 @@ | ||
| 233 | 223 | * Returns true if Polylang manages languages and translations for this post type |
| 234 | 224 | * |
| 235 | 225 | * @since 1.0.1 |
| 236 | 226 | * |
| 237 | - * @param string $post_type Post type name | |
| 227 | + * @param string post type name | |
| 238 | 228 | * @return bool |
| 239 | 229 | */ |
| 240 | 230 | function pll_is_translated_post_type( $post_type ) { |
| 241 | 231 | return PLL()->model->is_translated_post_type( $post_type ); |
| @@ -245,9 +235,9 @@ | ||
| 245 | 235 | * Returns true if Polylang manages languages and translations for this taxonomy |
| 246 | 236 | * |
| 247 | 237 | * @since 1.0.1 |
| 248 | 238 | * |
| 249 | - * @param string $tax Taxonomy name | |
| 239 | + * @param string taxonomy name | |
| 250 | 240 | * @return bool |
| 251 | 241 | */ |
| 252 | 242 | function pll_is_translated_taxonomy( $tax ) { |
| 253 | 243 | return PLL()->model->is_translated_taxonomy( $tax ); |
| @@ -322,10 +312,10 @@ | ||
| 322 | 312 | * |
| 323 | 313 | * @since 1.5.4 |
| 324 | 314 | * |
| 325 | 315 | * @param int $post_id |
| 326 | - * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug' | |
| 327 | - * @return bool|string The requested field for the post language, false if no language is associated to that post | |
| 316 | + * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug' | |
| 317 | + * @return bool|string the requested field for the post language, false if no language is associated to that post | |
| 328 | 318 | */ |
| 329 | 319 | function pll_get_post_language( $post_id, $field = 'slug' ) { |
| 330 | 320 | return ( $lang = PLL()->model->post->get_language( $post_id ) ) ? $lang->$field : false; |
| 331 | 321 | } |
| @@ -335,10 +325,10 @@ | ||
| 335 | 325 | * |
| 336 | 326 | * @since 1.5.4 |
| 337 | 327 | * |
| 338 | 328 | * @param int $term_id |
| 339 | - * @param string $field Optional, the language field to return ( see PLL_Language ), defaults to 'slug' | |
| 340 | - * @return bool|string The requested field for the term language, false if no language is associated to that term | |
| 329 | + * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug' | |
| 330 | + * @return bool|string the requested field for the term language, false if no language is associated to that term | |
| 341 | 331 | */ |
| 342 | 332 | function pll_get_term_language( $term_id, $field = 'slug' ) { |
| 343 | 333 | return ( $lang = PLL()->model->term->get_language( $term_id ) ) ? $lang->$field : false; |
| 344 | 334 | } |
| @@ -371,11 +361,11 @@ | ||
| 371 | 361 | * Count posts in a language |
| 372 | 362 | * |
| 373 | 363 | * @since 1.5 |
| 374 | 364 | * |
| 375 | - * @param string $lang Language code. | |
| 376 | - * @param array $args WP_Query arguments ( accepted keys: post_type, m, year, monthnum, day, author, author_name, post_format, post_status ). | |
| 377 | - * @return int Posts count. | |
| 365 | + * @param string $lang language code | |
| 366 | + * @param array $args ( accepted keys: post_type, m, year, monthnum, day, author, author_name, post_format ) | |
| 367 | + * @return int posts count | |
| 378 | 368 | */ |
| 379 | 369 | function pll_count_posts( $lang, $args = array() ) { |
| 380 | 370 | return PLL()->model->count_posts( PLL()->model->get_language( $lang ), $args ); |
| 381 | 371 | } |
| @@ -386,7 +376,7 @@ | ||
| 386 | 376 | * Internal methods may be changed without prior notice |
| 387 | 377 | * |
| 388 | 378 | * @since 1.8 |
| 389 | 379 | */ |
| 390 | -function PLL() { // PHPCS:ignore WordPress.NamingConventions.ValidFunctionName | |
| 380 | +function PLL() { | |
| 391 | 381 | return $GLOBALS['polylang']; |
| 392 | 382 | } |