PluginProbe
Polylang / 1.8.5
Polylang v1.8.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 1.8.5, at include/language.php

224 lines 7.7 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 $this->set_flag();
84 }
85 }
86
87 /*
88 * sets flag_url and flag properties
89 *
90 * @since 1.2
91 */
92 public function set_flag() {
93 $flags['flag']['url'] = '';
94
95 // Polylang builtin flags
96 if ( ! empty( $this->flag_code ) && file_exists( POLYLANG_DIR . ( $file = '/flags/' . $this->flag_code . '.png' ) ) ) {
97 $flags['flag']['url'] = esc_url_raw( POLYLANG_URL . $file );
98
99 // if base64 encoded flags are preferred
100 if ( ! defined( 'PLL_ENCODED_FLAGS' ) || PLL_ENCODED_FLAGS ) {
101 $flags['flag']['src'] = 'data:image/png;base64,' . base64_encode( file_get_contents( POLYLANG_DIR . $file ) );
102 } else {
103 $flags['flag']['src'] = esc_url( POLYLANG_URL . $file );
104 }
105 }
106
107 // custom flags ?
108 if ( file_exists( PLL_LOCAL_DIR . ( $file = '/' . $this->locale . '.png' ) ) || file_exists( PLL_LOCAL_DIR . ( $file = '/' . $this->locale . '.jpg' ) ) ) {
109 $flags['custom_flag']['url'] = esc_url_raw( PLL_LOCAL_URL . $file );
110 $flags['custom_flag']['src'] = esc_url( PLL_LOCAL_URL . $file );
111 }
112
113 foreach ( $flags as $key => $flag ) {
114 $this->{$key . '_url'} = empty( $flag['url'] ) ? '' : $flag['url'];
115
116 $this->{$key} = apply_filters( 'pll_get_flag', empty( $flag['src'] ) ? '' :
117 sprintf(
118 '<img src="%s" title="%s" alt="%s" />',
119 $flag['src'],
120 esc_attr( apply_filters( 'pll_flag_title', $this->name, $this->slug, $this->locale ) ),
121 esc_attr( $this->name )
122 ),
123 $this->slug
124 );
125 }
126 }
127
128 /*
129 * replace flag by custom flag
130 * takes care of url scheme
131 *
132 * @since 1.7
133 */
134 public function set_custom_flag() {
135 // overwrite with custom flags on frontend only
136 if ( ! empty( $this->custom_flag ) ) {
137 $this->flag = $this->custom_flag;
138 $this->flag_url = $this->custom_flag_url;
139 unset( $this->custom_flag, $this->custom_flag_url ); // hide this
140 }
141
142 // set url scheme, also for default flags
143 if ( is_ssl() ) {
144 $this->flag = str_replace( 'http://', 'https://', $this->flag );
145 $this->flag_url = str_replace( 'http://', 'https://', $this->flag_url );
146 } else {
147 $this->flag = str_replace( 'https://', 'http://', $this->flag );
148 $this->flag_url = str_replace( 'https://', 'http://', $this->flag_url );
149 }
150 }
151
152 /*
153 * updates post and term count
154 *
155 * @since 1.2
156 */
157 public function update_count() {
158 wp_update_term_count( $this->term_taxonomy_id, 'language' ); // posts count
159 wp_update_term_count( $this->tl_term_taxonomy_id, 'term_language' ); // terms count
160 }
161
162 /*
163 * set home_url and search_url properties
164 *
165 * @since 1.3
166 *
167 * @param string $search_url
168 * @param string $home_url
169 */
170 public function set_home_url( $search_url, $home_url ) {
171 $this->search_url = $search_url;
172 $this->home_url = $home_url;
173 }
174
175 /*
176 * set home_url scheme
177 * this can't be cached accross pages
178 *
179 * @since 1.6.4
180 */
181 public function set_home_url_scheme() {
182 if ( is_ssl() ) {
183 $this->home_url = str_replace( 'http://', 'https://', $this->home_url );
184 $this->search_url = str_replace( 'http://', 'https://', $this->search_url );
185 }
186
187 else {
188 $this->home_url = str_replace( 'https://', 'http://', $this->home_url );
189 $this->search_url = str_replace( 'https://', 'http://', $this->search_url );
190 }
191 }
192
193 /*
194 * returns the language locale
195 * converts WP locales to W3C valid locales for display
196 * @see #33511
197 *
198 * @since 1.8
199 *
200 * @param string $filter either 'display' or 'raw', defaults to raw
201 * @return string
202 */
203 public function get_locale( $filter = 'raw' ) {
204 if ( 'display' == $filter ) {
205 static $valid_locales = array(
206 'bel' => 'be',
207 'bre' => 'br',
208 'de_DE_formal' => 'de_DE',
209 'dzo' => 'dz',
210 'ido' => 'io',
211 'kin' => 'rw',
212 'oci' => 'oc',
213 'mri' => 'mi',
214 'roh' => 'rm',
215 'srd' => 'sc',
216 'tuk' => 'tk',
217 );
218 $locale = isset( $valid_locales[ $this->locale ] ) ? $valid_locales[ $this->locale ] : $this->locale;
219 return str_replace( '_', '-', $locale );
220 }
221 return $this->locale;
222 }
223 }
224