PluginProbe
Polylang / 1.6.2
Polylang v1.6.2
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / api.php

api.php in Polylang 1.6.2, at include/api.php

307 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @param string $lang language code (optional on frontend)
94 * @return string
95 */
96 function pll_home_url($lang = '') {
97 global $polylang;
98
99 if (empty($lang))
100 $lang = pll_current_language();
101
102 return isset($polylang) && !empty($polylang->links) && !empty($lang) ? $polylang->links->get_home_url($lang) : home_url('/');
103 }
104
105 /*
106 * registers a string for translation in the "strings translation" panel
107 *
108 * @since 0.6
109 *
110 * @param string $name a unique name for the string
111 * @param string $string the string to register
112 * @param string $context optional the group in which the string is registered, defaults to 'polylang'
113 * @param bool $multiline optional wether the string table should display a multiline textarea or a single line input, defaults to single line
114 */
115 function pll_register_string($name, $string, $context = 'polylang', $multiline = false) {
116 global $polylang;
117 if ($polylang instanceof PLL_Admin)
118 PLL_Admin_Strings::register_string($name, $string, $context, $multiline);
119 }
120
121 /*
122 * translates a string (previously registered with pll_register_string)
123 *
124 * @since 0.6
125 *
126 * @param string $string the string to translate
127 * @return string the string translation in the current language
128 */
129 function pll__($string) {
130 return __($string, 'pll_string');
131 }
132
133 /*
134 * echoes a translated string (previously registered with pll_register_string)
135 *
136 * @since 0.6
137 *
138 * @param string $string the string to translate
139 */
140 function pll_e($string) {
141 _e($string, 'pll_string');
142 }
143
144 /*
145 * translates a string (previously registered with pll_register_string)
146 *
147 * @since 1.5.4
148 *
149 * @param string $string the string to translate
150 * @param string $lang language code
151 * @return string the string translation in the requested language
152 */
153 function pll_translate_string($string, $lang) {
154 if (pll_current_language() == $lang)
155 return pll__($string);
156
157 static $mo = array();
158
159 if (empty($mo[$lang])) {
160 $mo[$lang] = new PLL_MO();
161 $mo[$lang]->import_from_db($GLOBALS['polylang']->model->get_language($lang));
162 }
163
164 return $mo[$lang]->translate($string);
165 }
166
167 /*
168 * returns true if Polylang manages languages and translations for this post type
169 *
170 * @since 1.0.1
171 *
172 * @param string post type name
173 * @return bool
174 */
175 function pll_is_translated_post_type($post_type) {
176 global $polylang;
177 return isset($polylang) && $polylang->model->is_translated_post_type($post_type);
178 }
179
180 /*
181 * returns true if Polylang manages languages and translations for this taxonomy
182 *
183 * @since 1.0.1
184 *
185 * @param string taxonomy name
186 * @return bool
187 */
188 function pll_is_translated_taxonomy($tax) {
189 global $polylang;
190 return isset($polylang) && $polylang->model->is_translated_taxonomy($tax);
191 }
192
193 /*
194 * returns the list of available languages
195 *
196 * list of parameters accepted in $args:
197 *
198 * hide_empty => hides languages with no posts if set to true (defaults to false)
199 * fields => return only that field if set (see PLL_Language for a list of fields)
200 *
201 * @since 1.5
202 *
203 * @param array $args list of parameters
204 * @return array
205 */
206 function pll_languages_list($args = array()) {
207 global $polylang;
208 $args = wp_parse_args($args, array('fields' => 'slug'));
209 return isset($polylang) ? $polylang->model->get_languages_list($args) : false;
210 }
211
212 /*
213 * set the post language
214 *
215 * @since 1.5
216 *
217 * @param int $post_id post id
218 * @param string $lang language code
219 */
220 function pll_set_post_language($id, $lang) {
221 global $polylang;
222 if (isset($polylang))
223 $polylang->model->set_post_language($id, $lang);
224 }
225
226 /*
227 * set the term language
228 *
229 * @since 1.5
230 *
231 * @param int $id term id
232 * @param string $lang language code
233 */
234 function pll_set_term_language($id, $lang) {
235 global $polylang;
236 if (isset($polylang))
237 $polylang->model->set_term_language($id, $lang);
238 }
239
240 /*
241 * save posts translations
242 *
243 * @since 1.5
244 *
245 * @param array $arr an associative array of translations with language code as key and post id as value
246 */
247 function pll_save_post_translations($arr) {
248 global $polylang;
249 if (isset($polylang))
250 $polylang->model->save_translations('post', reset($arr), $arr);
251 }
252
253 /*
254 * save terms translations
255 *
256 * @since 1.5
257 *
258 * @param array $arr an associative array of translations with language code as key and term id as value
259 */
260 function pll_save_term_translations($arr) {
261 global $polylang;
262 if (isset($polylang))
263 $polylang->model->save_translations('term', reset($arr), $arr);
264 }
265
266 /*
267 * returns the post language
268 *
269 * @since 1.5.4
270 *
271 * @param int $post_id
272 * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug'
273 * @return bool|string the requested field for the post language, false if no language is associated to that post
274 */
275 function pll_get_post_language($post_id, $field = 'slug') {
276 global $polylang;
277 return isset($polylang) && ($lang = $polylang->model->get_post_language($post_id)) ? $lang->$field : false;
278 }
279
280 /*
281 * returns the term language
282 *
283 * @since 1.5.4
284 *
285 * @param int $term_id
286 * @param string $field optional the language field to return 'name', 'locale', defaults to 'slug'
287 * @return bool|string the requested field for the term language, false if no language is associated to that term
288 */
289 function pll_get_term_language($term_id, $field = 'slug') {
290 global $polylang;
291 return isset($polylang) && ($lang = $polylang->model->get_term_language($term_id)) ? $lang->$field : false;
292 }
293
294 /*
295 * count posts in a language
296 *
297 * @since 1.5
298 *
299 * @param string $lang language code
300 * @param array $args (accepted keys: post_type, m, year, monthnum, day, author, author_name, post_format)
301 * @return int posts count
302 */
303 function pll_count_posts($lang, $args = array()) {
304 global $polylang;
305 return isset($polylang) ? $polylang->model->count_posts($polylang->model->get_language($lang), $args) : false;
306 }
307