PluginProbe
Polylang / 1.3
Polylang v1.3
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.3, at include/api.php

169 lines 5.4 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 && !empty($polylang->settings_page))
118 $polylang->settings_page->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 * returns true if Polylang manages languages and translations for this post type
146 *
147 * @since 1.0.1
148 *
149 * @param string post type name
150 * @return bool
151 */
152 function pll_is_translated_post_type($post_type) {
153 global $polylang;
154 return isset($polylang) && $polylang->model->is_translated_post_type($post_type);
155 }
156
157 /*
158 * returns true if Polylang manages languages and translations for this taxonomy
159 *
160 * @since 1.0.1
161 *
162 * @param string taxonomy name
163 * @return bool
164 */
165 function pll_is_translated_taxonomy($tax) {
166 global $polylang;
167 return isset($polylang) && $polylang->model->is_translated_taxonomy($tax);
168 }
169