PluginProbe
Polylang / 2.4
Polylang v2.4
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 / language.php

language.php in Polylang 2.4, at include/language.php

271 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * A language object is made of two terms in 'language' and 'term_language' taxonomies
5 * manipulating only one object per language instead of two terms should make things easier
6 *
7 * Properties:
8 * term_id => id of term in 'language' taxonomy
9 * name => language name. Ex: English
10 * slug => language code used in url. Ex: en
11 * term_group => order of the language when displayed in a list of languages
12 * term_taxonomy_id => term taxonomy id in 'language' taxonomy
13 * taxonomy => 'language'
14 * description => language locale for backward compatibility
15 * parent => 0 / not used
16 * count => number of posts and pages in that language
17 * tl_term_id => id of the term in 'term_language' taxonomy
18 * tl_term_taxonomy_id => term taxonomy id in 'term_language' taxonomy
19 * tl_count => number of terms in that language ( not used by Polylang )
20 * locale => WordPress language locale. Ex: en_US
21 * is_rtl => 1 if the language is rtl
22 * w3c => W3C locale
23 * flag_code => code of the flag
24 * flag_url => url of the flag
25 * flag => html img of the flag
26 * custom_flag_url => url of the custom flag if exists, internal use only, moves to flag_url on frontend
27 * custom_flag => html img of the custom flag if exists, internal use only, moves to flag on frontend
28 * home_url => home url in this language
29 * search_url => home url to use in search forms
30 * host => host of this language
31 * mo_id => id of the post storing strings translations
32 * page_on_front => id of the page on front in this language ( set from pll_languages_list filter )
33 * page_for_posts => id of the page for posts in this language ( set from pll_languages_list filter )
34 *
35 * @since 1.2
36 */
37 class PLL_Language {
38 public $term_id, $name, $slug, $term_group, $term_taxonomy_id, $taxonomy, $description, $parent, $count;
39 public $tl_term_id, $tl_term_taxonomy_id, $tl_count;
40 public $locale, $is_rtl;
41 public $w3c, $facebook;
42 public $flag_url, $flag;
43 public $home_url, $search_url;
44 public $host, $mo_id;
45 public $page_on_front, $page_for_posts;
46
47 /**
48 * Constructor: builds a language object given its two corresponding terms in language and term_language taxonomies
49 *
50 * @since 1.2
51 *
52 * @param object|array $language 'language' term or language object properties stored as an array
53 * @param object $term_language Corresponding 'term_language' term
54 */
55 public function __construct( $language, $term_language = null ) {
56 // Build the object from all properties stored as an array
57 if ( empty( $term_language ) ) {
58 foreach ( $language as $prop => $value ) {
59 $this->$prop = $value;
60 }
61 }
62
63 // Build the object from taxonomies
64 else {
65 foreach ( $language as $prop => $value ) {
66 $this->$prop = in_array( $prop, array( 'term_id', 'term_taxonomy_id', 'count' ) ) ? (int) $language->$prop : $language->$prop;
67 }
68
69 $this->tl_term_id = (int) $term_language->term_id;
70 $this->tl_term_taxonomy_id = (int) $term_language->term_taxonomy_id;
71 $this->tl_count = (int) $term_language->count;
72
73 // The description field can contain any property
74 // Backward compatibility for is_rtl
75 $description = maybe_unserialize( $language->description );
76 foreach ( $description as $prop => $value ) {
77 'rtl' == $prop ? $this->is_rtl = $value : $this->$prop = $value;
78 }
79
80 $this->description = &$this->locale; // Backward compatibility with Polylang < 1.2
81
82 $this->mo_id = PLL_MO::get_id( $this );
83
84 include PLL_SETTINGS_INC . '/languages.php';
85 $this->w3c = isset( $languages[ $this->locale ]['w3c'] ) ? $languages[ $this->locale ]['w3c'] : str_replace( '_', '-', $this->locale );
86 if ( isset( $languages[ $this->locale ]['facebook'] ) ) {
87 $this->facebook = $languages[ $this->locale ]['facebook'];
88 }
89 }
90 }
91
92 /**
93 * Sets flag_url and flag properties
94 *
95 * @since 1.2
96 */
97 public function set_flag() {
98 $flags['flag']['url'] = '';
99
100 // Polylang builtin flags
101 if ( ! empty( $this->flag_code ) && file_exists( POLYLANG_DIR . ( $file = '/flags/' . $this->flag_code . '.png' ) ) ) {
102 $flags['flag']['url'] = $_url = plugins_url( $file, POLYLANG_FILE );
103 }
104
105 /**
106 * Filter flag informations
107 * 'url' => Flag url
108 * 'src' => Optional, src attribute value if different of the url, for example if base64 encoded
109 * 'width' => Optional, flag width in pixels
110 * 'height' => Optional, flag height in pixels
111 *
112 * @since 2.4
113 *
114 * @param array $flag Information about the flag
115 * @param string $code Flag code
116 */
117 $flags['flag'] = apply_filters( 'pll_flag', $flags['flag'], $this->flag_code );
118
119 if ( empty( $flags['flag']['src'] ) ) {
120 // If using predefined flags and base64 encoded flags are preferred
121 if ( $flags['flag']['url'] === $_url && ( ! defined( 'PLL_ENCODED_FLAGS' ) || PLL_ENCODED_FLAGS ) ) {
122 $flags['flag']['src'] = 'data:image/png;base64,' . base64_encode( file_get_contents( POLYLANG_DIR . $file ) );
123 } else {
124 $flags['flag']['src'] = esc_url( set_url_scheme( $flags['flag']['url'], 'relative' ) );
125 }
126 }
127
128 $flags['flag']['url'] = esc_url_raw( $flags['flag']['url'] );
129
130 // Custom flags ?
131 $directories = array(
132 PLL_LOCAL_DIR,
133 get_stylesheet_directory() . '/polylang',
134 get_template_directory() . '/polylang',
135 );
136
137 foreach ( $directories as $dir ) {
138 if ( file_exists( $file = "{$dir}/{$this->locale}.png" ) || file_exists( $file = "{$dir}/{$this->locale}.jpg" ) || file_exists( $file = "{$dir}/{$this->locale}.svg" ) ) {
139 $flags['custom_flag']['url'] = site_url( '/' . str_replace( ABSPATH, '', $file ) );
140 break;
141 }
142 }
143
144 /**
145 * Filter the custom flag informations
146 * 'url' => Flag url
147 * 'src' => Optional, src attribute value if different of the url, for example if base64 encoded
148 * 'width' => Optional, flag width in pixels
149 * 'height' => Optional, flag height in pixels
150 *
151 * @since 2.4
152 *
153 * @param array $flag Information about the custom flag
154 * @param string $code Flag code
155 */
156 $flags['custom_flag'] = apply_filters( 'pll_custom_flag', empty( $flags['custom_flag'] ) ? null : $flags['custom_flag'], $this->flag_code );
157
158 if ( ! empty( $flags['custom_flag']['url'] ) ) {
159 if ( empty( $flags['custom_flag']['src'] ) ) {
160 $flags['custom_flag']['src'] = esc_url( set_url_scheme( $flags['custom_flag']['url'], 'relative' ) );
161 }
162
163 $flags['custom_flag']['url'] = esc_url_raw( $flags['custom_flag']['url'] );
164 } else {
165 unset( $flags['custom_flag'] );
166 }
167
168 /**
169 * Filter the flag title attribute
170 * Defaults to the language name
171 *
172 * @since 0.7
173 *
174 * @param string $title the flag title attribute
175 * @param string $slug the language code
176 * @param string $locale the language locale
177 */
178 $title = apply_filters( 'pll_flag_title', $this->name, $this->slug, $this->locale );
179
180 foreach ( $flags as $key => $flag ) {
181 $this->{$key . '_url'} = empty( $flag['url'] ) ? '' : $flag['url'];
182
183 /**
184 * Filter the html markup of a flag
185 *
186 * @since 1.0.2
187 *
188 * @param string $flag html markup of the flag or empty string
189 * @param string $slug language code
190 */
191 $this->{$key} = apply_filters(
192 'pll_get_flag',
193 empty( $flag['src'] ) ? '' : sprintf(
194 '<img src="%s" title="%s" alt="%s"%s%s />',
195 $flag['src'],
196 esc_attr( $title ),
197 esc_attr( $this->name ),
198 empty( $flag['width'] ) ? '' : sprintf( ' width="%s"', (int) $flag['width'] ),
199 empty( $flag['height'] ) ? '' : sprintf( ' height="%s"', (int) $flag['height'] )
200 ),
201 $this->slug
202 );
203 }
204 }
205
206 /**
207 * Replace flag by custom flag
208 * Takes care of url scheme
209 *
210 * @since 1.7
211 */
212 public function set_custom_flag() {
213 // Overwrite with custom flags on frontend only
214 if ( ! empty( $this->custom_flag ) ) {
215 $this->flag = $this->custom_flag;
216 $this->flag_url = $this->custom_flag_url;
217 unset( $this->custom_flag, $this->custom_flag_url ); // hide this
218 }
219
220 // Set url scheme, also for default flags
221 $this->flag_url = set_url_scheme( $this->flag_url );
222 }
223
224 /**
225 * Updates post and term count
226 *
227 * @since 1.2
228 */
229 public function update_count() {
230 wp_update_term_count( $this->term_taxonomy_id, 'language' ); // posts count
231 wp_update_term_count( $this->tl_term_taxonomy_id, 'term_language' ); // terms count
232 }
233
234 /**
235 * Set home_url and search_url properties
236 *
237 * @since 1.3
238 *
239 * @param string $search_url
240 * @param string $home_url
241 */
242 public function set_home_url( $search_url, $home_url ) {
243 $this->search_url = $search_url;
244 $this->home_url = $home_url;
245 }
246
247 /**
248 * Set home_url scheme
249 * this can't be cached across pages
250 *
251 * @since 1.6.4
252 */
253 public function set_home_url_scheme() {
254 $this->home_url = set_url_scheme( $this->home_url );
255 $this->search_url = set_url_scheme( $this->search_url );
256 }
257
258 /**
259 * Returns the language locale
260 * Converts WP locales to W3C valid locales for display
261 *
262 * @since 1.8
263 *
264 * @param string $filter either 'display' or 'raw', defaults to raw
265 * @return string
266 */
267 public function get_locale( $filter = 'raw' ) {
268 return 'display' === $filter ? $this->w3c : $this->locale;
269 }
270 }
271