PluginProbe
Polylang / 2.2.5
Polylang v2.2.5
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.2.5, at include/language.php

246 lines 8.3 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 * flag_code => code of the flag
23 * flag_url => url of the flag
24 * flag => html img of the flag
25 * custom_flag_url => url of the custom flag if exists, internal use only, moves to flag_url on frontend
26 * custom_flag => html img of the custom flag if exists, internal use only, moves to flag on frontend
27 * home_url => home url in this language
28 * search_url => home url to use in search forms
29 * host => host of this language
30 * mo_id => id of the post storing strings translations
31 * page_on_front => id of the page on front in this language ( set from pll_languages_list filter )
32 * page_for_posts => id of the page for posts in this language ( set from pll_languages_list filter )
33 *
34 * @since 1.2
35 */
36 class PLL_Language {
37 public $term_id, $name, $slug, $term_group, $term_taxonomy_id, $taxonomy, $description, $parent, $count;
38 public $tl_term_id, $tl_term_taxonomy_id, $tl_count;
39 public $locale, $is_rtl;
40 public $flag_url, $flag;
41 public $home_url, $search_url;
42 public $host, $mo_id;
43 public $page_on_front, $page_for_posts;
44
45 /**
46 * constructor: builds a language object given its two corresponding terms in language and term_language taxonomies
47 *
48 * @since 1.2
49 *
50 * @param object|array $language 'language' term or language object properties stored as an array
51 * @param object $term_language Corresponding 'term_language' term
52 */
53 public function __construct( $language, $term_language = null ) {
54 // build the object from all properties stored as an array
55 if ( empty( $term_language ) ) {
56 foreach ( $language as $prop => $value ) {
57 $this->$prop = $value;
58 }
59 }
60
61 // build the object from taxonomies
62 else {
63 foreach ( $language as $prop => $value ) {
64 $this->$prop = in_array( $prop, array( 'term_id', 'term_taxonomy_id', 'count' ) ) ? (int) $language->$prop : $language->$prop;
65 }
66
67 // although it would be convenient here, don't assume the term is shared between taxonomies as it may not be the case in future
68 // http://make.wordpress.org/core/2013/07/28/potential-roadmap-for-taxonomy-meta-and-post-relationships/
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 }
85
86 /**
87 * sets flag_url and flag properties
88 *
89 * @since 1.2
90 */
91 public function set_flag() {
92 $flags['flag']['url'] = '';
93
94 // Polylang builtin flags
95 if ( ! empty( $this->flag_code ) && file_exists( POLYLANG_DIR . ( $file = '/flags/' . $this->flag_code . '.png' ) ) ) {
96 $flags['flag']['url'] = esc_url_raw( plugins_url( $file, POLYLANG_FILE ) );
97
98 // if base64 encoded flags are preferred
99 if ( ! defined( 'PLL_ENCODED_FLAGS' ) || PLL_ENCODED_FLAGS ) {
100 $flags['flag']['src'] = 'data:image/png;base64,' . base64_encode( file_get_contents( POLYLANG_DIR . $file ) );
101 } else {
102 $flags['flag']['src'] = esc_url( plugins_url( $file, POLYLANG_FILE ) );
103 }
104 }
105
106 // custom flags ?
107 if ( file_exists( PLL_LOCAL_DIR . ( $file = '/' . $this->locale . '.png' ) ) || file_exists( PLL_LOCAL_DIR . ( $file = '/' . $this->locale . '.jpg' ) ) ) {
108 $url = content_url( '/polylang' . $file );
109 $flags['custom_flag']['url'] = esc_url_raw( $url );
110 $flags['custom_flag']['src'] = esc_url( $url );
111 }
112
113 /**
114 * Filter the flag title attribute
115 * Defaults to the language name
116 *
117 * @since 0.7
118 *
119 * @param string $title the flag title attribute
120 * @param string $slug the language code
121 * @param string $locale the language locale
122 */
123 $title = apply_filters( 'pll_flag_title', $this->name, $this->slug, $this->locale );
124
125 foreach ( $flags as $key => $flag ) {
126 $this->{$key . '_url'} = empty( $flag['url'] ) ? '' : $flag['url'];
127
128 /**
129 * Filter the html markup of a flag
130 *
131 * @since 1.0.2
132 *
133 * @param string $flag html markup of the flag or empty string
134 * @param string $slug language code
135 */
136 $this->{$key} = apply_filters( 'pll_get_flag', empty( $flag['src'] ) ? '' :
137 sprintf(
138 '<img src="%s" title="%s" alt="%s" />',
139 $flag['src'],
140 esc_attr( $title ),
141 esc_attr( $this->name )
142 ),
143 $this->slug
144 );
145 }
146 }
147
148 /**
149 * replace flag by custom flag
150 * takes care of url scheme
151 *
152 * @since 1.7
153 */
154 public function set_custom_flag() {
155 // overwrite with custom flags on frontend only
156 if ( ! empty( $this->custom_flag ) ) {
157 $this->flag = $this->custom_flag;
158 $this->flag_url = $this->custom_flag_url;
159 unset( $this->custom_flag, $this->custom_flag_url ); // hide this
160 }
161
162 // set url scheme, also for default flags
163 if ( is_ssl() ) {
164 $this->flag = str_replace( 'http://', 'https://', $this->flag );
165 $this->flag_url = str_replace( 'http://', 'https://', $this->flag_url );
166 } else {
167 $this->flag = str_replace( 'https://', 'http://', $this->flag );
168 $this->flag_url = str_replace( 'https://', 'http://', $this->flag_url );
169 }
170 }
171
172 /**
173 * updates post and term count
174 *
175 * @since 1.2
176 */
177 public function update_count() {
178 wp_update_term_count( $this->term_taxonomy_id, 'language' ); // posts count
179 wp_update_term_count( $this->tl_term_taxonomy_id, 'term_language' ); // terms count
180 }
181
182 /**
183 * set home_url and search_url properties
184 *
185 * @since 1.3
186 *
187 * @param string $search_url
188 * @param string $home_url
189 */
190 public function set_home_url( $search_url, $home_url ) {
191 $this->search_url = $search_url;
192 $this->home_url = $home_url;
193 }
194
195 /**
196 * set home_url scheme
197 * this can't be cached across pages
198 *
199 * @since 1.6.4
200 */
201 public function set_home_url_scheme() {
202 if ( is_ssl() ) {
203 $this->home_url = str_replace( 'http://', 'https://', $this->home_url );
204 $this->search_url = str_replace( 'http://', 'https://', $this->search_url );
205 }
206
207 else {
208 $this->home_url = str_replace( 'https://', 'http://', $this->home_url );
209 $this->search_url = str_replace( 'https://', 'http://', $this->search_url );
210 }
211 }
212
213 /**
214 * returns the language locale
215 * converts WP locales to W3C valid locales for display
216 * @see #33511
217 *
218 * @since 1.8
219 *
220 * @param string $filter either 'display' or 'raw', defaults to raw
221 * @return string
222 */
223 public function get_locale( $filter = 'raw' ) {
224 if ( 'display' == $filter ) {
225 static $valid_locales = array(
226 'bel' => 'be',
227 'bre' => 'br',
228 'de_CH_informal' => 'de_CH',
229 'de_DE_formal' => 'de_DE',
230 'dzo' => 'dz',
231 'ido' => 'io',
232 'kin' => 'rw',
233 'oci' => 'oc',
234 'mri' => 'mi',
235 'nl_NL_formal' => 'nl_NL',
236 'roh' => 'rm',
237 'srd' => 'sc',
238 'tuk' => 'tk',
239 );
240 $locale = isset( $valid_locales[ $this->locale ] ) ? $valid_locales[ $this->locale ] : $this->locale;
241 return str_replace( '_', '-', $locale );
242 }
243 return $this->locale;
244 }
245 }
246