| 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 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 => wether 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 |
| 19 |
* |
| 20 |
* @since 0.5 |
| 21 |
* |
| 22 |
* @param array $args optional |
| 23 |
* @return null|string|array null if displaying, array if raw is requested, string otherwise |
| 24 |
*/ |
| 25 |
function pll_the_languages($args = '') { |
| 26 |
global $polylang; |
| 27 |
if ($polylang instanceof PLL_Frontend && !empty($polylang->links)) { |
| 28 |
$switcher = new PLL_Switcher; |
| 29 |
return $switcher->the_languages($polylang->links, $args); |
| 30 |
} |
| 31 |
return ''; |
| 32 |
} |
| 33 |
|
| 34 |
/* |
| 35 |
* returns the current language |
| 36 |
* |
| 37 |
* @since 0.8.1 |
| 38 |
* |
| 39 |
* @param string $field optional the language field to return 'name', 'locale', defaults to 'slug' |
| 40 |
* @return string the requested field for the current language |
| 41 |
*/ |
| 42 |
function pll_current_language($field = 'slug') { |
| 43 |
global $polylang; |
| 44 |
return isset($polylang->curlang->$field) ? $polylang->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 'name', 'locale', defaults to 'slug' |
| 53 |
* @return string the requested field for the default language |
| 54 |
*/ |
| 55 |
function pll_default_language($field = 'slug') { |
| 56 |
global $polylang; |
| 57 |
return isset($polylang->options['default_lang']) && ($lang = $polylang->model->get_language($polylang->options['default_lang'])) && isset($lang->$field) ? $lang->$field : false; |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
* among the post and its translations, returns the id of the post which is in the language represented by $slug |
| 62 |
* |
| 63 |
* @since 0.5 |
| 64 |
* |
| 65 |
* @param int $post_id post id |
| 66 |
* @param string $slug optional language code, defaults to current language |
| 67 |
* @return int post id of the translation if exists |
| 68 |
*/ |
| 69 |
function pll_get_post($post_id, $slug = '') { |
| 70 |
global $polylang; |
| 71 |
return isset($polylang) && ($slug = $slug ? $slug : pll_current_language()) ? $polylang->model->get_post($post_id, $slug) : null; |
| 72 |
} |
| 73 |
|
| 74 |
/* |
| 75 |
* among the term and its translations, returns the id of the term which is in the language represented by $slug |
| 76 |
* |
| 77 |
* @since 0.5 |
| 78 |
* |
| 79 |
* @param int $term_id term id |
| 80 |
* @param string $slug optional language code, defaults to current language |
| 81 |
* @return int term id of the translation if exists |
| 82 |
*/ |
| 83 |
function pll_get_term($term_id, $slug = '') { |
| 84 |
global $polylang; |
| 85 |
return isset($polylang) && ($slug = $slug ? $slug : pll_current_language()) ? $polylang->model->get_term($term_id, $slug) : null; |
| 86 |
} |
| 87 |
|
| 88 |
/* |
| 89 |
* returns the home url in the current language |
| 90 |
* |
| 91 |
* @since 0.8 |
| 92 |
* |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
function pll_home_url() { |
| 96 |
global $polylang; |
| 97 |
return $polylang instanceof PLL_Frontend && !empty($polylang->links) ? $polylang->links->get_home_url() : home_url('/'); |
| 98 |
} |
| 99 |
|
| 100 |
/* |
| 101 |
* registers a string for translation in the "strings translation" panel |
| 102 |
* |
| 103 |
* @since 0.6 |
| 104 |
* |
| 105 |
* @param string $name a unique name for the string |
| 106 |
* @param string $string the string to register |
| 107 |
* @param string $context optional the group in which the string is registered, defaults to 'polylang' |
| 108 |
* @param bool $multiline optional wether the string table should display a multiline textarea or a single line input, defaults to single line |
| 109 |
*/ |
| 110 |
function pll_register_string($name, $string, $context = 'polylang', $multiline = false) { |
| 111 |
global $polylang; |
| 112 |
if ($polylang instanceof PLL_Admin && !empty($polylang->settings_page)) |
| 113 |
$polylang->settings_page->register_string($name, $string, $context, $multiline); |
| 114 |
} |
| 115 |
|
| 116 |
/* |
| 117 |
* translates a string (previously registered with pll_register_string) |
| 118 |
* |
| 119 |
* @since 0.6 |
| 120 |
* |
| 121 |
* @param string $string the string to translate |
| 122 |
* @return string the string translation in the current language |
| 123 |
*/ |
| 124 |
function pll__($string) { |
| 125 |
return __($string, 'pll_string'); |
| 126 |
} |
| 127 |
|
| 128 |
/* |
| 129 |
* echoes a translated string (previously registered with pll_register_string) |
| 130 |
* |
| 131 |
* @since 0.6 |
| 132 |
* |
| 133 |
* @param string $string the string to translate |
| 134 |
*/ |
| 135 |
function pll_e($string) { |
| 136 |
_e($string, 'pll_string'); |
| 137 |
} |
| 138 |
|
| 139 |
/* |
| 140 |
* returns true if Polylang manages languages and translations for this post type |
| 141 |
* |
| 142 |
* @since 1.0.1 |
| 143 |
* |
| 144 |
* @param string post type name |
| 145 |
* @return bool |
| 146 |
*/ |
| 147 |
function pll_is_translated_post_type($post_type) { |
| 148 |
global $polylang; |
| 149 |
return isset($polylang) && $polylang->model->is_translated_post_type($post_type); |
| 150 |
} |
| 151 |
|
| 152 |
/* |
| 153 |
* returns true if Polylang manages languages and translations for this taxonomy |
| 154 |
* |
| 155 |
* @since 1.0.1 |
| 156 |
* |
| 157 |
* @param string taxonomy name |
| 158 |
* @return bool |
| 159 |
*/ |
| 160 |
function pll_is_translated_taxonomy($tax) { |
| 161 |
global $polylang; |
| 162 |
return isset($polylang) && $polylang->model->is_translated_taxonomy($tax); |
| 163 |
} |
| 164 |
|