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