PluginProbe
Polylang / 2.3
Polylang v2.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
← All changes | include/language.php +49 -154 2.92.3 View file →
@@ -1,8 +1,5 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 4 * A language object is made of two terms in 'language' and 'term_language' taxonomies
8 5 * manipulating only one object per language instead of two terms should make things easier
@@ -83,9 +80,9 @@
83 80 $this->description = &$this->locale; // Backward compatibility with Polylang < 1.2
84 81
85 82 $this->mo_id = PLL_MO::get_id( $this );
86 83
87 - $languages = include POLYLANG_DIR . '/settings/languages.php';
84 + include PLL_SETTINGS_INC . '/languages.php';
88 85 $this->w3c = isset( $languages[ $this->locale ]['w3c'] ) ? $languages[ $this->locale ]['w3c'] : str_replace( '_', '-', $this->locale );
89 86 if ( isset( $languages[ $this->locale ]['facebook'] ) ) {
90 87 $this->facebook = $languages[ $this->locale ]['facebook'];
91 88 }
@@ -92,104 +89,35 @@
92 89 }
93 90 }
94 91
95 92 /**
96 - * Get the flag informations
97 - * 'url' => Flag url
98 - * 'src' => Optional, src attribute value if different of the url, for example if base64 encoded
99 - * 'width' => Optional, flag width in pixels
100 - * 'height' => Optional, flag height in pixels
93 + * sets flag_url and flag properties
101 94 *
102 - * @since 2.6
103 - *
104 - * @param string $code Flag code.
105 - * @return array Flag informations.
95 + * @since 1.2
106 96 */
107 - public static function get_flag_informations( $code ) {
108 - $flag = array( 'url' => '' );
97 + public function set_flag() {
98 + $flags['flag']['url'] = '';
109 99
110 100 // Polylang builtin flags
111 - if ( ! empty( $code ) && file_exists( POLYLANG_DIR . ( $file = '/flags/' . $code . '.png' ) ) ) {
112 - $flag['url'] = $_url = plugins_url( $file, POLYLANG_FILE );
113 - }
101 + if ( ! empty( $this->flag_code ) && file_exists( POLYLANG_DIR . ( $file = '/flags/' . $this->flag_code . '.png' ) ) ) {
102 + $flags['flag']['url'] = esc_url_raw( plugins_url( $file, POLYLANG_FILE ) );
114 103
115 - /**
116 - * Filter flag informations
117 - * 'url' => Flag url
118 - * 'src' => Optional, src attribute value if different of the url, for example if base64 encoded
119 - * 'width' => Optional, flag width in pixels
120 - * 'height' => Optional, flag height in pixels
121 - *
122 - * @since 2.4
123 - *
124 - * @param array $flag Information about the flag
125 - * @param string $code Flag code
126 - */
127 - $flag = apply_filters( 'pll_flag', $flag, $code );
128 -
129 - if ( empty( $flag['src'] ) ) {
130 - // If using predefined flags and base64 encoded flags are preferred
131 - if ( isset( $_url ) && $flag['url'] === $_url && ( ! defined( 'PLL_ENCODED_FLAGS' ) || PLL_ENCODED_FLAGS ) ) {
132 - list( $flag['width'], $flag['height'] ) = getimagesize( POLYLANG_DIR . $file );
133 - $file_contents = file_get_contents( POLYLANG_DIR . $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
134 - $flag['src'] = 'data:image/png;base64,' . base64_encode( $file_contents ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
104 + // If base64 encoded flags are preferred
105 + if ( ! defined( 'PLL_ENCODED_FLAGS' ) || PLL_ENCODED_FLAGS ) {
106 + $flags['flag']['src'] = 'data:image/png;base64,' . base64_encode( file_get_contents( POLYLANG_DIR . $file ) );
135 107 } else {
136 - $flag['src'] = esc_url( set_url_scheme( $flag['url'], 'relative' ) );
108 + $flags['flag']['src'] = esc_url( plugins_url( $file, POLYLANG_FILE ) );
137 109 }
138 110 }
139 111
140 - $flag['url'] = esc_url_raw( $flag['url'] );
141 -
142 - return $flag;
143 - }
144 -
145 - /**
146 - * Sets flag_url and flag properties
147 - *
148 - * @since 1.2
149 - */
150 - public function set_flag() {
151 - $flags = array( 'flag' => self::get_flag_informations( $this->flag_code ) );
152 -
153 112 // Custom flags ?
154 - $directories = array(
155 - PLL_LOCAL_DIR,
156 - get_stylesheet_directory() . '/polylang',
157 - get_template_directory() . '/polylang',
158 - );
159 -
160 - foreach ( $directories as $dir ) {
161 - if ( file_exists( $file = "{$dir}/{$this->locale}.png" ) || file_exists( $file = "{$dir}/{$this->locale}.jpg" ) || file_exists( $file = "{$dir}/{$this->locale}.svg" ) ) {
162 - $flags['custom_flag']['url'] = content_url( '/' . str_replace( WP_CONTENT_DIR, '', $file ) );
163 - break;
164 - }
113 + if ( file_exists( PLL_LOCAL_DIR . ( $file = '/' . $this->locale . '.png' ) ) || file_exists( PLL_LOCAL_DIR . ( $file = '/' . $this->locale . '.jpg' ) ) ) {
114 + $url = content_url( '/polylang' . $file );
115 + $flags['custom_flag']['url'] = esc_url_raw( $url );
116 + $flags['custom_flag']['src'] = esc_url( $url );
165 117 }
166 118
167 119 /**
168 - * Filter the custom flag informations
169 - * 'url' => Flag url
170 - * 'src' => Optional, src attribute value if different of the url, for example if base64 encoded
171 - * 'width' => Optional, flag width in pixels
172 - * 'height' => Optional, flag height in pixels
173 - *
174 - * @since 2.4
175 - *
176 - * @param array $flag Information about the custom flag
177 - * @param string $code Flag code
178 - */
179 - $flags['custom_flag'] = apply_filters( 'pll_custom_flag', empty( $flags['custom_flag'] ) ? null : $flags['custom_flag'], $this->flag_code );
180 -
181 - if ( ! empty( $flags['custom_flag']['url'] ) ) {
182 - if ( empty( $flags['custom_flag']['src'] ) ) {
183 - $flags['custom_flag']['src'] = esc_url( set_url_scheme( $flags['custom_flag']['url'], 'relative' ) );
184 - }
185 -
186 - $flags['custom_flag']['url'] = esc_url_raw( $flags['custom_flag']['url'] );
187 - } else {
188 - unset( $flags['custom_flag'] );
189 - }
190 -
191 - /**
192 120 * Filter the flag title attribute
193 121 * Defaults to the language name
194 122 *
195 123 * @since 0.7
@@ -210,11 +138,15 @@
210 138 *
211 139 * @param string $flag html markup of the flag or empty string
212 140 * @param string $slug language code
213 141 */
214 - $this->{$key} = apply_filters(
215 - 'pll_get_flag',
216 - self::get_flag_html( $flag, $title, $this->name ),
142 + $this->{$key} = apply_filters( 'pll_get_flag', empty( $flag['src'] ) ? '' :
143 + sprintf(
144 + '<img src="%s" title="%s" alt="%s" />',
145 + $flag['src'],
146 + esc_attr( $title ),
147 + esc_attr( $this->name )
148 + ),
217 149 $this->slug
218 150 );
219 151 }
220 152 }
@@ -219,69 +151,32 @@
219 151 }
220 152 }
221 153
222 154 /**
223 - * Get HTML code for flag
155 + * Replace flag by custom flag
156 + * Takes care of url scheme
224 157 *
225 - * @since 2.7
226 - *
227 - * @param array $flag flag properties: src, width and height
228 - * @param string $title optional title attribute
229 - * @param string $alt optional alt attribute
158 + * @since 1.7
230 159 */
231 - public static function get_flag_html( $flag, $title = '', $alt = '' ) {
232 - if ( empty( $flag['src'] ) ) {
233 - return '';
160 + public function set_custom_flag() {
161 + // Overwrite with custom flags on frontend only
162 + if ( ! empty( $this->custom_flag ) ) {
163 + $this->flag = $this->custom_flag;
164 + $this->flag_url = $this->custom_flag_url;
165 + unset( $this->custom_flag, $this->custom_flag_url ); // hide this
234 166 }
235 167
236 - $title_attr = empty( $title ) ? '' : sprintf( ' title="%s"', esc_attr( $title ) );
237 - $alt_attr = empty( $alt ) ? '' : sprintf( ' alt="%s"', esc_attr( $alt ) );
238 - $width_attr = empty( $flag['width'] ) ? '' : sprintf( ' width="%s"', (int) $flag['width'] );
239 - $height_attr = empty( $flag['height'] ) ? '' : sprintf( ' height="%s"', (int) $flag['height'] );
240 -
241 - $style = '';
242 - $sizes = array_intersect_key( $flag, array_flip( array( 'width', 'height' ) ) );
243 -
244 - if ( ! empty( $sizes ) ) {
245 - array_walk(
246 - $sizes,
247 - function ( &$value, $key ) {
248 - $value = sprintf( '%s: %dpx;', esc_attr( $key ), (int) $value );
249 - }
250 - );
251 - $style = sprintf( ' style="%s"', implode( ' ', $sizes ) );
168 + // Set url scheme, also for default flags
169 + if ( is_ssl() ) {
170 + $this->flag = str_replace( 'http://', 'https://', $this->flag );
171 + $this->flag_url = str_replace( 'http://', 'https://', $this->flag_url );
172 + } else {
173 + $this->flag = str_replace( 'https://', 'http://', $this->flag );
174 + $this->flag_url = str_replace( 'https://', 'http://', $this->flag_url );
252 175 }
253 -
254 - return sprintf(
255 - '<img src="%s"%s%s%s%s%s />',
256 - $flag['src'],
257 - $title_attr,
258 - $alt_attr,
259 - $width_attr,
260 - $height_attr,
261 - $style
262 - );
263 176 }
264 177
265 178 /**
266 - * Returns the html of the custom flag if any, or the default flag otherwise.
267 - *
268 - * @since 2.8
269 - */
270 - public function get_display_flag() {
271 - return empty( $this->custom_flag ) ? $this->flag : $this->custom_flag;
272 - }
273 -
274 - /**
275 - * Returns the url of the custom flag if any, or the default flag otherwise.
276 - *
277 - * @since 2.8
278 - */
279 - public function get_display_flag_url() {
280 - return empty( $this->custom_flag_url ) ? $this->flag_url : $this->custom_flag_url;
281 - }
282 -
283 - /**
284 179 * Updates post and term count
285 180 *
286 181 * @since 1.2
287 182 */
@@ -303,22 +198,22 @@
303 198 $this->home_url = $home_url;
304 199 }
305 200
306 201 /**
307 - * Sets the scheme of the home url and the flag urls
202 + * Set home_url scheme
203 + * this can't be cached across pages
308 204 *
309 - * This can't be cached across pages.
310 - *
311 - * @since 2.8
205 + * @since 1.6.4
312 206 */
313 - public function set_url_scheme() {
314 - $this->home_url = set_url_scheme( $this->home_url );
315 - $this->search_url = set_url_scheme( $this->search_url );
207 + public function set_home_url_scheme() {
208 + if ( is_ssl() ) {
209 + $this->home_url = str_replace( 'http://', 'https://', $this->home_url );
210 + $this->search_url = str_replace( 'http://', 'https://', $this->search_url );
211 + }
316 212
317 - // Set url scheme, also for the flags.
318 - $this->flag_url = set_url_scheme( $this->flag_url );
319 - if ( ! empty( $this->custom_flag_url ) ) {
320 - $this->custom_flag_url = set_url_scheme( $this->custom_flag_url );
213 + else {
214 + $this->home_url = str_replace( 'https://', 'http://', $this->home_url );
215 + $this->search_url = str_replace( 'https://', 'http://', $this->search_url );
321 216 }
322 217 }
323 218
324 219 /**