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