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
← All changes | include/language.php +38 -81 2.82.4 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,25 +89,18 @@
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 );
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 );
113 103 }
114 104
115 105 /**
116 106 * Filter flag informations
@@ -123,34 +113,21 @@
123 113 *
124 114 * @param array $flag Information about the flag
125 115 * @param string $code Flag code
126 116 */
127 - $flag = apply_filters( 'pll_flag', $flag, $code );
117 + $flags['flag'] = apply_filters( 'pll_flag', $flags['flag'], $this->flag_code );
128 118
129 - if ( empty( $flag['src'] ) ) {
119 + if ( empty( $flags['flag']['src'] ) ) {
130 120 // 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
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 ) );
135 123 } else {
136 - $flag['src'] = esc_url( set_url_scheme( $flag['url'], 'relative' ) );
124 + $flags['flag']['src'] = esc_url( set_url_scheme( $flags['flag']['url'], 'relative' ) );
137 125 }
138 126 }
139 127
140 - $flag['url'] = esc_url_raw( $flag['url'] );
128 + $flags['flag']['url'] = esc_url_raw( $flags['flag']['url'] );
141 129
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 130 // Custom flags ?
154 131 $directories = array(
155 132 PLL_LOCAL_DIR,
156 133 get_stylesheet_directory() . '/polylang',
@@ -158,9 +135,9 @@
158 135 );
159 136
160 137 foreach ( $directories as $dir ) {
161 138 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 ) );
139 + $flags['custom_flag']['url'] = site_url( '/' . str_replace( ABSPATH, '', $file ) );
163 140 break;
164 141 }
165 142 }
166 143
@@ -212,9 +189,16 @@
212 189 * @param string $slug language code
213 190 */
214 191 $this->{$key} = apply_filters(
215 192 'pll_get_flag',
216 - self::get_flag_html( $flag, $title, $this->name ),
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 + ),
217 201 $this->slug
218 202 );
219 203 }
220 204 }
@@ -219,46 +203,26 @@
219 203 }
220 204 }
221 205
222 206 /**
223 - * Get HTML code for flag
207 + * Replace flag by custom flag
208 + * Takes care of url scheme
224 209 *
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
210 + * @since 1.7
230 211 */
231 - public static function get_flag_html( $flag, $title = '', $alt = '' ) {
232 - return empty( $flag['src'] ) ? '' : sprintf(
233 - '<img src="%s"%s%s%s%s />',
234 - $flag['src'],
235 - empty( $title ) ? '' : sprintf( ' title="%s"', esc_attr( $title ) ),
236 - empty( $alt ) ? '' : sprintf( ' alt="%s"', esc_attr( $alt ) ),
237 - empty( $flag['width'] ) ? '' : sprintf( ' width="%s"', (int) $flag['width'] ),
238 - empty( $flag['height'] ) ? '' : sprintf( ' height="%s"', (int) $flag['height'] )
239 - );
240 - }
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 + }
241 219
242 - /**
243 - * Returns the html of the custom flag if any, or the default flag otherwise.
244 - *
245 - * @since 2.8
246 - */
247 - public function get_display_flag() {
248 - return empty( $this->custom_flag ) ? $this->flag : $this->custom_flag;
220 + // Set url scheme, also for default flags
221 + $this->flag_url = set_url_scheme( $this->flag_url );
249 222 }
250 223
251 224 /**
252 - * Returns the url of the custom flag if any, or the default flag otherwise.
253 - *
254 - * @since 2.8
255 - */
256 - public function get_display_flag_url() {
257 - return empty( $this->custom_flag_url ) ? $this->flag : $this->custom_flag_url;
258 - }
259 -
260 - /**
261 225 * Updates post and term count
262 226 *
263 227 * @since 1.2
264 228 */
@@ -280,23 +244,16 @@
280 244 $this->home_url = $home_url;
281 245 }
282 246
283 247 /**
284 - * Sets the scheme of the home url and the flag urls
248 + * Set home_url scheme
249 + * this can't be cached across pages
285 250 *
286 - * This can't be cached across pages.
287 - *
288 - * @since 2.8
251 + * @since 1.6.4
289 252 */
290 - public function set_url_scheme() {
253 + public function set_home_url_scheme() {
291 254 $this->home_url = set_url_scheme( $this->home_url );
292 255 $this->search_url = set_url_scheme( $this->search_url );
293 -
294 - // Set url scheme, also for the flags.
295 - $this->flag_url = set_url_scheme( $this->flag_url );
296 - if ( ! empty( $this->custom_flag_url ) ) {
297 - $this->custom_flag_url = set_url_scheme( $this->custom_flag_url );
298 - }
299 256 }
300 257
301 258 /**
302 259 * Returns the language locale