| @@ -7,28 +7,58 @@ | ||
| 7 | 7 | * A language object is made of two terms in 'language' and 'term_language' taxonomies. |
| 8 | 8 | * Manipulating only one object per language instead of two terms should make things easier. |
| 9 | 9 | * |
| 10 | 10 | * @since 1.2 |
| 11 | + * @immutable | |
| 12 | + * | |
| 13 | + * @phpstan-type LanguagePropData array{ | |
| 14 | + * term_id: positive-int, | |
| 15 | + * term_taxonomy_id: positive-int, | |
| 16 | + * count: int<0, max> | |
| 17 | + * } | |
| 18 | + * @phpstan-type LanguageData array{ | |
| 19 | + * term_props: array{ | |
| 20 | + * language: LanguagePropData, | |
| 21 | + * }&array<non-empty-string, LanguagePropData>, | |
| 22 | + * name: non-empty-string, | |
| 23 | + * slug: non-empty-string, | |
| 24 | + * locale: non-empty-string, | |
| 25 | + * w3c: non-empty-string, | |
| 26 | + * flag_code: non-empty-string, | |
| 27 | + * term_group: int, | |
| 28 | + * is_rtl: int<0, 1>, | |
| 29 | + * facebook?: string, | |
| 30 | + * home_url: non-empty-string, | |
| 31 | + * search_url: non-empty-string, | |
| 32 | + * host: non-empty-string, | |
| 33 | + * flag_url: non-empty-string, | |
| 34 | + * flag: non-empty-string, | |
| 35 | + * custom_flag_url?: string, | |
| 36 | + * custom_flag?: string, | |
| 37 | + * page_on_front: int<0, max>, | |
| 38 | + * page_for_posts: int<0, max>, | |
| 39 | + * active: bool, | |
| 40 | + * fallbacks?: array<non-empty-string>, | |
| 41 | + * is_default: bool | |
| 42 | + * } | |
| 11 | 43 | */ |
| 12 | -class PLL_Language { | |
| 13 | - /** | |
| 14 | - * Id of the term in 'language' taxonomy. | |
| 15 | - * | |
| 16 | - * @var int | |
| 17 | - */ | |
| 18 | - public $term_id; | |
| 44 | +class PLL_Language extends PLL_Language_Deprecated { | |
| 19 | 45 | |
| 20 | 46 | /** |
| 21 | 47 | * Language name. Ex: English. |
| 22 | 48 | * |
| 23 | 49 | * @var string |
| 50 | + * | |
| 51 | + * @phpstan-var non-empty-string | |
| 24 | 52 | */ |
| 25 | 53 | public $name; |
| 26 | 54 | |
| 27 | 55 | /** |
| 28 | - * Language code used in url. Ex: en. | |
| 56 | + * Language code used in URL. Ex: en. | |
| 29 | 57 | * |
| 30 | 58 | * @var string |
| 59 | + * | |
| 60 | + * @phpstan-var non-empty-string | |
| 31 | 61 | */ |
| 32 | 62 | public $slug; |
| 33 | 63 | |
| 34 | 64 | /** |
| @@ -38,46 +68,24 @@ | ||
| 38 | 68 | */ |
| 39 | 69 | public $term_group; |
| 40 | 70 | |
| 41 | 71 | /** |
| 42 | - * Term taxonomy id in 'language' taxonomy. | |
| 72 | + * ID of the term in 'language' taxonomy. | |
| 73 | + * Duplicated from `$this->term_props['language']['term_id'], | |
| 74 | + * but kept to facilitate the use of it. | |
| 43 | 75 | * |
| 44 | 76 | * @var int |
| 45 | - */ | |
| 46 | - public $term_taxonomy_id; | |
| 47 | - | |
| 48 | - /** | |
| 49 | - * Number of posts and pages in that language. | |
| 50 | 77 | * |
| 51 | - * @var int | |
| 78 | + * @phpstan-var int<1, max> | |
| 52 | 79 | */ |
| 53 | - public $count; | |
| 80 | + public $term_id; | |
| 54 | 81 | |
| 55 | 82 | /** |
| 56 | - * Id of the term in 'term_language' taxonomy. | |
| 57 | - * | |
| 58 | - * @var int | |
| 59 | - */ | |
| 60 | - public $tl_term_id; | |
| 61 | - | |
| 62 | - /** | |
| 63 | - * Term taxonomy id in 'term_language' taxonomy. | |
| 64 | - * | |
| 65 | - * @var int | |
| 66 | - */ | |
| 67 | - public $tl_term_taxonomy_id; | |
| 68 | - | |
| 69 | - /** | |
| 70 | - * Number of terms in that language. | |
| 71 | - * | |
| 72 | - * @var int | |
| 73 | - */ | |
| 74 | - public $tl_count; | |
| 75 | - | |
| 76 | - /** | |
| 77 | 83 | * WordPress language locale. Ex: en_US. |
| 78 | 84 | * |
| 79 | 85 | * @var string |
| 86 | + * | |
| 87 | + * @phpstan-var non-empty-string | |
| 80 | 88 | */ |
| 81 | 89 | public $locale; |
| 82 | 90 | |
| 83 | 91 | /** |
| @@ -83,8 +91,10 @@ | ||
| 83 | 91 | /** |
| 84 | 92 | * 1 if the language is rtl, 0 otherwise. |
| 85 | 93 | * |
| 86 | 94 | * @var int |
| 95 | + * | |
| 96 | + * @phpstan-var int<0, 1> | |
| 87 | 97 | */ |
| 88 | 98 | public $is_rtl; |
| 89 | 99 | |
| 90 | 100 | /** |
| @@ -89,9 +99,11 @@ | ||
| 89 | 99 | |
| 90 | 100 | /** |
| 91 | 101 | * W3C locale. |
| 92 | 102 | * |
| 93 | - * @var string. | |
| 103 | + * @var string | |
| 104 | + * | |
| 105 | + * @phpstan-var non-empty-string | |
| 94 | 106 | */ |
| 95 | 107 | public $w3c; |
| 96 | 108 | |
| 97 | 109 | /** |
| @@ -96,142 +108,257 @@ | ||
| 96 | 108 | |
| 97 | 109 | /** |
| 98 | 110 | * Facebook locale. |
| 99 | 111 | * |
| 100 | - * @var string. | |
| 112 | + * @var string | |
| 101 | 113 | */ |
| 102 | - public $facebook; | |
| 114 | + public $facebook = ''; | |
| 103 | 115 | |
| 104 | 116 | /** |
| 105 | - * Home url in this language. | |
| 117 | + * Home URL in this language. | |
| 106 | 118 | * |
| 107 | 119 | * @var string |
| 120 | + * | |
| 121 | + * @phpstan-var non-empty-string | |
| 108 | 122 | */ |
| 109 | - public $home_url; | |
| 123 | + private $home_url; | |
| 110 | 124 | |
| 111 | 125 | /** |
| 112 | - * Home url to use in search forms. | |
| 126 | + * Home URL to use in search forms. | |
| 113 | 127 | * |
| 114 | 128 | * @var string |
| 129 | + * | |
| 130 | + * @phpstan-var non-empty-string | |
| 115 | 131 | */ |
| 116 | - public $search_url; | |
| 132 | + private $search_url; | |
| 117 | 133 | |
| 118 | 134 | /** |
| 119 | 135 | * Host corresponding to this language. |
| 120 | 136 | * |
| 121 | 137 | * @var string |
| 138 | + * | |
| 139 | + * @phpstan-var non-empty-string | |
| 122 | 140 | */ |
| 123 | 141 | public $host; |
| 124 | 142 | |
| 125 | 143 | /** |
| 126 | - * Id of the post storing strings translations. | |
| 144 | + * ID of the page on front in this language (set from pll_additional_language_data filter). | |
| 127 | 145 | * |
| 128 | 146 | * @var int |
| 147 | + * | |
| 148 | + * @phpstan-var int<0, max> | |
| 129 | 149 | */ |
| 130 | - public $mo_id; | |
| 150 | + public $page_on_front = 0; | |
| 131 | 151 | |
| 132 | 152 | /** |
| 133 | - * Id of the page on front in this language ( set from pll_languages_list filter ). | |
| 153 | + * ID of the page for posts in this language (set from pll_additional_language_data filter). | |
| 134 | 154 | * |
| 135 | 155 | * @var int |
| 136 | - */ | |
| 137 | - public $page_on_front; | |
| 138 | - | |
| 139 | - /** | |
| 140 | - * Id of the page for posts in this language ( set from pll_languages_list filter ). | |
| 141 | 156 | * |
| 142 | - * @var int | |
| 157 | + * @phpstan-var int<0, max> | |
| 143 | 158 | */ |
| 144 | - public $page_for_posts; | |
| 159 | + public $page_for_posts = 0; | |
| 145 | 160 | |
| 146 | 161 | /** |
| 147 | 162 | * Code of the flag. |
| 148 | 163 | * |
| 149 | 164 | * @var string |
| 165 | + * | |
| 166 | + * @phpstan-var non-empty-string | |
| 150 | 167 | */ |
| 151 | 168 | public $flag_code; |
| 152 | 169 | |
| 153 | 170 | /** |
| 154 | - * Url of the flag. | |
| 171 | + * URL of the flag. Always set to the main domain. | |
| 155 | 172 | * |
| 156 | 173 | * @var string |
| 174 | + * | |
| 175 | + * @phpstan-var non-empty-string | |
| 157 | 176 | */ |
| 158 | 177 | public $flag_url; |
| 159 | 178 | |
| 160 | 179 | /** |
| 161 | - * Html markup of the flag. | |
| 180 | + * HTML markup of the flag. | |
| 162 | 181 | * |
| 163 | 182 | * @var string |
| 183 | + * | |
| 184 | + * @phpstan-var non-empty-string | |
| 164 | 185 | */ |
| 165 | 186 | public $flag; |
| 166 | 187 | |
| 167 | 188 | /** |
| 168 | - * Url of the custom flag if it exists. | |
| 189 | + * URL of the custom flag if it exists. Always set to the main domain. | |
| 169 | 190 | * |
| 170 | 191 | * @var string |
| 171 | 192 | */ |
| 172 | - public $custom_flag_url; | |
| 193 | + public $custom_flag_url = ''; | |
| 173 | 194 | |
| 174 | 195 | /** |
| 175 | - * Html markup of the custom flag if it exists. | |
| 196 | + * HTML markup of the custom flag if it exists. | |
| 176 | 197 | * |
| 177 | 198 | * @var string |
| 178 | 199 | */ |
| 179 | - public $custom_flag; | |
| 200 | + public $custom_flag = ''; | |
| 180 | 201 | |
| 181 | 202 | /** |
| 182 | - * Constructor: builds a language object given its two corresponding terms in 'language' and 'term_language' taxonomies. | |
| 203 | + * Whether or not the language is active. Default `true`. | |
| 183 | 204 | * |
| 205 | + * @var bool | |
| 206 | + */ | |
| 207 | + public $active = true; | |
| 208 | + | |
| 209 | + /** | |
| 210 | + * List of WordPress language locales. Ex: array( 'en_GB' ). | |
| 211 | + * | |
| 212 | + * @var string[] | |
| 213 | + * | |
| 214 | + * @phpstan-var list<non-empty-string> | |
| 215 | + */ | |
| 216 | + public $fallbacks = array(); | |
| 217 | + | |
| 218 | + /** | |
| 219 | + * Whether the language is the default one. | |
| 220 | + * | |
| 221 | + * @var bool | |
| 222 | + */ | |
| 223 | + public $is_default; | |
| 224 | + | |
| 225 | + /** | |
| 226 | + * Stores language term properties (like term IDs and counts) for each language taxonomy (`language`, | |
| 227 | + * `term_language`, etc). | |
| 228 | + * This stores the values of the properties `$term_id` + `$term_taxonomy_id` + `$count` (`language`), `$tl_term_id` | |
| 229 | + * + `$tl_term_taxonomy_id` + `$tl_count` (`term_language`), and the `term_id` + `term_taxonomy_id` + `count` for | |
| 230 | + * other language taxonomies. | |
| 231 | + * | |
| 232 | + * @var array[] Array keys are language term names. | |
| 233 | + * | |
| 234 | + * @example array( | |
| 235 | + * 'language' => array( | |
| 236 | + * 'term_id' => 7, | |
| 237 | + * 'term_taxonomy_id' => 8, | |
| 238 | + * 'count' => 11, | |
| 239 | + * ), | |
| 240 | + * 'term_language' => array( | |
| 241 | + * 'term_id' => 11, | |
| 242 | + * 'term_taxonomy_id' => 12, | |
| 243 | + * 'count' => 6, | |
| 244 | + * ), | |
| 245 | + * 'foo_language' => array( | |
| 246 | + * 'term_id' => 33, | |
| 247 | + * 'term_taxonomy_id' => 34, | |
| 248 | + * 'count' => 0, | |
| 249 | + * ), | |
| 250 | + * ) | |
| 251 | + * | |
| 252 | + * @phpstan-var array{ | |
| 253 | + * language: LanguagePropData, | |
| 254 | + * } | |
| 255 | + * &array<non-empty-string, LanguagePropData> | |
| 256 | + */ | |
| 257 | + protected $term_props; | |
| 258 | + | |
| 259 | + /** | |
| 260 | + * Constructor: builds a language object given the corresponding data. | |
| 261 | + * | |
| 184 | 262 | * @since 1.2 |
| 263 | + * @since 3.4 Only accepts one argument. | |
| 185 | 264 | * |
| 186 | - * @param WP_Term|array $language Term in 'language' taxonomy or language object properties stored as an array. | |
| 187 | - * @param WP_Term $term_language Corresponding 'term_language' term. | |
| 265 | + * @param array $language_data { | |
| 266 | + * Language object properties stored as an array. | |
| 267 | + * | |
| 268 | + * @type array[] $term_props An array of language term properties. Array keys are language taxonomy names | |
| 269 | + * (`language` and `term_language` are mandatory), array values are arrays of | |
| 270 | + * language term properties (`term_id`, `term_taxonomy_id`, and `count`). | |
| 271 | + * @type string $name Language name. Ex: English. | |
| 272 | + * @type string $slug Language code used in URL. Ex: en. | |
| 273 | + * @type string $locale WordPress language locale. Ex: en_US. | |
| 274 | + * @type string $w3c W3C locale. | |
| 275 | + * @type string $flag_code Code of the flag. | |
| 276 | + * @type int $term_group Order of the language when displayed in a list of languages. | |
| 277 | + * @type int $is_rtl `1` if the language is rtl, `0` otherwise. | |
| 278 | + * @type string $facebook Optional. Facebook locale. | |
| 279 | + * @type string $home_url Home URL in this language. | |
| 280 | + * @type string $search_url Home URL to use in search forms. | |
| 281 | + * @type string $host Host corresponding to this language. | |
| 282 | + * @type string $flag_url URL of the flag. | |
| 283 | + * @type string $flag HTML markup of the flag. | |
| 284 | + * @type string $custom_flag_url Optional. URL of the custom flag if it exists. | |
| 285 | + * @type string $custom_flag Optional. HTML markup of the custom flag if it exists. | |
| 286 | + * @type int $page_on_front Optional. ID of the page on front in this language. | |
| 287 | + * @type int $page_for_posts Optional. ID of the page for posts in this language. | |
| 288 | + * @type bool $active Whether or not the language is active. Default `true`. | |
| 289 | + * @type string[] $fallbacks List of WordPress language locales. Ex: array( 'en_GB' ). | |
| 290 | + * @type bool $is_default Whether or not the language is the default one. | |
| 291 | + * } | |
| 292 | + * | |
| 293 | + * @phpstan-param LanguageData $language_data | |
| 188 | 294 | */ |
| 189 | - public function __construct( $language, $term_language = null ) { | |
| 190 | - // Build the object from all properties stored as an array. | |
| 191 | - if ( is_array( $language ) ) { | |
| 192 | - foreach ( $language as $prop => $value ) { | |
| 193 | - $this->$prop = $value; | |
| 194 | - } | |
| 295 | + public function __construct( array $language_data ) { | |
| 296 | + foreach ( $language_data as $prop => $value ) { | |
| 297 | + $this->$prop = $value; | |
| 195 | 298 | } |
| 196 | 299 | |
| 197 | - // Build the object from taxonomy terms. | |
| 198 | - elseif ( ! empty( $term_language ) ) { | |
| 199 | - $this->term_id = (int) $language->term_id; | |
| 200 | - $this->name = $language->name; | |
| 201 | - $this->slug = $language->slug; | |
| 202 | - $this->term_group = (int) $language->term_group; | |
| 203 | - $this->term_taxonomy_id = (int) $language->term_taxonomy_id; | |
| 204 | - $this->count = (int) $language->count; | |
| 300 | + $this->term_id = $this->term_props['language']['term_id']; | |
| 301 | + } | |
| 205 | 302 | |
| 206 | - $this->tl_term_id = (int) $term_language->term_id; | |
| 207 | - $this->tl_term_taxonomy_id = (int) $term_language->term_taxonomy_id; | |
| 208 | - $this->tl_count = (int) $term_language->count; | |
| 303 | + /** | |
| 304 | + * Returns a language term property value (term ID, term taxonomy ID, or count). | |
| 305 | + * | |
| 306 | + * @since 3.4 | |
| 307 | + * | |
| 308 | + * @param string $taxonomy_name Name of the taxonomy. | |
| 309 | + * @param string $prop_name Name of the property: 'term_taxonomy_id', 'term_id', 'count'. | |
| 310 | + * @return int | |
| 311 | + * | |
| 312 | + * @phpstan-param non-empty-string $taxonomy_name | |
| 313 | + * @phpstan-param 'term_taxonomy_id'|'term_id'|'count' $prop_name | |
| 314 | + * @phpstan-return int<0, max> | |
| 315 | + */ | |
| 316 | + public function get_tax_prop( $taxonomy_name, $prop_name ) { | |
| 317 | + return $this->term_props[ $taxonomy_name ][ $prop_name ] ?? 0; | |
| 318 | + } | |
| 209 | 319 | |
| 210 | - // The description field can contain any property. | |
| 211 | - $description = maybe_unserialize( $language->description ); | |
| 212 | - foreach ( $description as $prop => $value ) { | |
| 213 | - 'rtl' == $prop ? $this->is_rtl = $value : $this->$prop = $value; | |
| 214 | - } | |
| 320 | + /** | |
| 321 | + * Returns the language term props for all content types. | |
| 322 | + * | |
| 323 | + * @since 3.4 | |
| 324 | + * | |
| 325 | + * @param string $property Name of the field to return. An empty string to return them all. | |
| 326 | + * @return (int[]|int)[] Array keys are taxonomy names, array values depend of `$property`. | |
| 327 | + * | |
| 328 | + * @phpstan-param 'term_taxonomy_id'|'term_id'|'count'|'' $property | |
| 329 | + * @phpstan-return array<non-empty-string, ( | |
| 330 | + * $property is non-empty-string ? | |
| 331 | + * ( | |
| 332 | + * $property is 'count' ? | |
| 333 | + * int<0, max> : | |
| 334 | + * positive-int | |
| 335 | + * ) : | |
| 336 | + * LanguagePropData | |
| 337 | + * )> | |
| 338 | + */ | |
| 339 | + public function get_tax_props( $property = '' ) { | |
| 340 | + if ( empty( $property ) ) { | |
| 341 | + return $this->term_props; | |
| 342 | + } | |
| 215 | 343 | |
| 216 | - $this->mo_id = PLL_MO::get_id( $this ); | |
| 344 | + $term_props = array(); | |
| 217 | 345 | |
| 218 | - $languages = include POLYLANG_DIR . '/settings/languages.php'; | |
| 219 | - $this->w3c = isset( $languages[ $this->locale ]['w3c'] ) ? $languages[ $this->locale ]['w3c'] : str_replace( '_', '-', $this->locale ); | |
| 220 | - if ( isset( $languages[ $this->locale ]['facebook'] ) ) { | |
| 221 | - $this->facebook = $languages[ $this->locale ]['facebook']; | |
| 222 | - } | |
| 346 | + foreach ( $this->term_props as $taxonomy_name => $props ) { | |
| 347 | + $term_props[ $taxonomy_name ] = $props[ $property ]; | |
| 223 | 348 | } |
| 349 | + | |
| 350 | + return $term_props; | |
| 224 | 351 | } |
| 225 | 352 | |
| 226 | 353 | /** |
| 227 | - * Get the flag informations: | |
| 354 | + * Returns the flag information. | |
| 228 | 355 | * |
| 229 | 356 | * @since 2.6 |
| 230 | 357 | * |
| 231 | 358 | * @param string $code Flag code. |
| 232 | 359 | * @return array { |
| 233 | - * Flag informations. | |
| 360 | + * Flag information. | |
| 234 | 361 | * |
| 235 | 362 | * @type string $url Flag url. |
| 236 | 363 | * @type string $src Optional, src attribute value if different of the url, for example if base64 encoded. |
| 237 | 364 | * @type int $width Optional, flag width in pixels. |
| @@ -236,26 +363,39 @@ | ||
| 236 | 363 | * @type string $src Optional, src attribute value if different of the url, for example if base64 encoded. |
| 237 | 364 | * @type int $width Optional, flag width in pixels. |
| 238 | 365 | * @type int $height Optional, flag height in pixels. |
| 239 | 366 | * } |
| 367 | + * | |
| 368 | + * @phpstan-return array{ | |
| 369 | + * url: string, | |
| 370 | + * src: string, | |
| 371 | + * width?: positive-int, | |
| 372 | + * height?: positive-int | |
| 373 | + * } | |
| 240 | 374 | */ |
| 241 | - public static function get_flag_informations( $code ) { | |
| 242 | - $flag = array( 'url' => '' ); | |
| 375 | + public static function get_flag_information( $code ) { | |
| 376 | + $default_flag = array( | |
| 377 | + 'url' => '', | |
| 378 | + 'src' => '', | |
| 379 | + ); | |
| 243 | 380 | |
| 244 | 381 | // Polylang builtin flags. |
| 245 | - if ( ! empty( $code ) && file_exists( POLYLANG_DIR . ( $file = '/flags/' . $code . '.png' ) ) ) { | |
| 246 | - $flag['url'] = plugins_url( $file, POLYLANG_FILE ); | |
| 382 | + if ( ! empty( $code ) && is_readable( POLYLANG_DIR . ( $file = '/flags/' . $code . '.png' ) ) ) { | |
| 383 | + $default_flag['url'] = plugins_url( $file, POLYLANG_FILE ); | |
| 247 | 384 | |
| 248 | 385 | // If base64 encoded flags are preferred. |
| 249 | - if ( ! defined( 'PLL_ENCODED_FLAGS' ) || PLL_ENCODED_FLAGS ) { | |
| 250 | - list( $flag['width'], $flag['height'] ) = getimagesize( POLYLANG_DIR . $file ); | |
| 251 | - $file_contents = file_get_contents( POLYLANG_DIR . $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 252 | - $flag['src'] = 'data:image/png;base64,' . base64_encode( $file_contents ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode | |
| 386 | + if ( pll_get_constant( 'PLL_ENCODED_FLAGS', true ) ) { | |
| 387 | + $imagesize = getimagesize( POLYLANG_DIR . $file ); | |
| 388 | + if ( is_array( $imagesize ) ) { | |
| 389 | + list( $default_flag['width'], $default_flag['height'] ) = $imagesize; | |
| 390 | + } | |
| 391 | + $file_contents = file_get_contents( POLYLANG_DIR . $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 392 | + $default_flag['src'] = 'data:image/png;base64,' . base64_encode( $file_contents ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode | |
| 253 | 393 | } |
| 254 | 394 | } |
| 255 | 395 | |
| 256 | 396 | /** |
| 257 | - * Filters flag informations: | |
| 397 | + * Filters flag information: | |
| 258 | 398 | * |
| 259 | 399 | * @since 2.4 |
| 260 | 400 | * |
| 261 | 401 | * @param array $flag { |
| @@ -267,13 +407,13 @@ | ||
| 267 | 407 | * @type int $height Optional, flag height in pixels. |
| 268 | 408 | * } |
| 269 | 409 | * @param string $code Flag code. |
| 270 | 410 | */ |
| 271 | - $flag = apply_filters( 'pll_flag', $flag, $code ); | |
| 411 | + $flag = apply_filters( 'pll_flag', $default_flag, $code ); | |
| 272 | 412 | |
| 273 | - $flag['url'] = esc_url_raw( $flag['url'] ); | |
| 413 | + $flag['url'] = sanitize_url( $flag['url'] ); | |
| 274 | 414 | |
| 275 | - if ( empty( $flag['src'] ) ) { | |
| 415 | + if ( empty( $flag['src'] ) || ( $flag['src'] === $default_flag['src'] && $flag['url'] !== $default_flag['url'] ) ) { | |
| 276 | 416 | $flag['src'] = esc_url( set_url_scheme( $flag['url'], 'relative' ) ); |
| 277 | 417 | } |
| 278 | 418 | |
| 279 | 419 | return $flag; |
| @@ -279,92 +419,10 @@ | ||
| 279 | 419 | return $flag; |
| 280 | 420 | } |
| 281 | 421 | |
| 282 | 422 | /** |
| 283 | - * Sets flag_url and flag properties. | |
| 423 | + * Returns HTML code for flag. | |
| 284 | 424 | * |
| 285 | - * @since 1.2 | |
| 286 | - * | |
| 287 | - * @return void | |
| 288 | - */ | |
| 289 | - public function set_flag() { | |
| 290 | - $flags = array( 'flag' => self::get_flag_informations( $this->flag_code ) ); | |
| 291 | - | |
| 292 | - // Custom flags? | |
| 293 | - $directories = array( | |
| 294 | - PLL_LOCAL_DIR, | |
| 295 | - get_stylesheet_directory() . '/polylang', | |
| 296 | - get_template_directory() . '/polylang', | |
| 297 | - ); | |
| 298 | - | |
| 299 | - foreach ( $directories as $dir ) { | |
| 300 | - if ( file_exists( $file = "{$dir}/{$this->locale}.png" ) || file_exists( $file = "{$dir}/{$this->locale}.jpg" ) || file_exists( $file = "{$dir}/{$this->locale}.svg" ) ) { | |
| 301 | - $flags['custom_flag']['url'] = content_url( '/' . str_replace( WP_CONTENT_DIR, '', $file ) ); | |
| 302 | - break; | |
| 303 | - } | |
| 304 | - } | |
| 305 | - | |
| 306 | - /** | |
| 307 | - * Filters the custom flag informations. | |
| 308 | - * | |
| 309 | - * @param array $flag { | |
| 310 | - * Information about the custom flag. | |
| 311 | - * | |
| 312 | - * @type string $url Flag url. | |
| 313 | - * @type string $src Optional, src attribute value if different of the url, for example if base64 encoded. | |
| 314 | - * @type int $width Optional, flag width in pixels. | |
| 315 | - * @type int $height Optional, flag height in pixels. | |
| 316 | - * } | |
| 317 | - * @param string $code Flag code. | |
| 318 | - * | |
| 319 | - * @since 2.4 | |
| 320 | - */ | |
| 321 | - $flags['custom_flag'] = apply_filters( 'pll_custom_flag', empty( $flags['custom_flag'] ) ? null : $flags['custom_flag'], $this->flag_code ); | |
| 322 | - | |
| 323 | - if ( ! empty( $flags['custom_flag']['url'] ) ) { | |
| 324 | - if ( empty( $flags['custom_flag']['src'] ) ) { | |
| 325 | - $flags['custom_flag']['src'] = esc_url( set_url_scheme( $flags['custom_flag']['url'], 'relative' ) ); | |
| 326 | - } | |
| 327 | - | |
| 328 | - $flags['custom_flag']['url'] = esc_url_raw( $flags['custom_flag']['url'] ); | |
| 329 | - } else { | |
| 330 | - unset( $flags['custom_flag'] ); | |
| 331 | - } | |
| 332 | - | |
| 333 | - /** | |
| 334 | - * Filters the flag title attribute. | |
| 335 | - * Defaults to the language name. | |
| 336 | - * | |
| 337 | - * @since 0.7 | |
| 338 | - * | |
| 339 | - * @param string $title The flag title attribute. | |
| 340 | - * @param string $slug The language code. | |
| 341 | - * @param string $locale The language locale. | |
| 342 | - */ | |
| 343 | - $title = apply_filters( 'pll_flag_title', $this->name, $this->slug, $this->locale ); | |
| 344 | - | |
| 345 | - foreach ( $flags as $key => $flag ) { | |
| 346 | - $this->{$key . '_url'} = empty( $flag['url'] ) ? '' : $flag['url']; | |
| 347 | - | |
| 348 | - /** | |
| 349 | - * Filters the html markup of a flag. | |
| 350 | - * | |
| 351 | - * @since 1.0.2 | |
| 352 | - * | |
| 353 | - * @param string $flag Html markup of the flag or empty string. | |
| 354 | - * @param string $slug Language code. | |
| 355 | - */ | |
| 356 | - $this->{$key} = apply_filters( | |
| 357 | - 'pll_get_flag', | |
| 358 | - self::get_flag_html( $flag, $title, $this->name ), | |
| 359 | - $this->slug | |
| 360 | - ); | |
| 361 | - } | |
| 362 | - } | |
| 363 | - | |
| 364 | - /** | |
| 365 | - * Get HTML code for flag. | |
| 366 | - * | |
| 367 | 425 | * @since 2.7 |
| 368 | 426 | * |
| 369 | 427 | * @param array $flag Flag properties: src, width and height. |
| 370 | 428 | * @param string $title Optional title attribute. |
| @@ -369,8 +427,14 @@ | ||
| 369 | 427 | * @param array $flag Flag properties: src, width and height. |
| 370 | 428 | * @param string $title Optional title attribute. |
| 371 | 429 | * @param string $alt Optional alt attribute. |
| 372 | 430 | * @return string |
| 431 | + * | |
| 432 | + * @phpstan-param array{ | |
| 433 | + * src: string, | |
| 434 | + * width?: int|numeric-string, | |
| 435 | + * height?: int|numeric-string | |
| 436 | + * } $flag | |
| 373 | 437 | */ |
| 374 | 438 | public static function get_flag_html( $flag, $title = '', $alt = '' ) { |
| 375 | 439 | if ( empty( $flag['src'] ) ) { |
| 376 | 440 | return ''; |
| @@ -406,13 +470,24 @@ | ||
| 406 | 470 | /** |
| 407 | 471 | * Returns the html of the custom flag if any, or the default flag otherwise. |
| 408 | 472 | * |
| 409 | 473 | * @since 2.8 |
| 474 | + * @since 3.5.3 Added the `$alt` parameter. | |
| 410 | 475 | * |
| 476 | + * @param string $alt Whether or not the alternative text should be set. Accepts 'alt' and 'no-alt'. | |
| 477 | + * | |
| 411 | 478 | * @return string |
| 479 | + * | |
| 480 | + * @phpstan-param 'alt'|'no-alt' $alt | |
| 412 | 481 | */ |
| 413 | - public function get_display_flag() { | |
| 414 | - return empty( $this->custom_flag ) ? $this->flag : $this->custom_flag; | |
| 482 | + public function get_display_flag( $alt = 'alt' ) { | |
| 483 | + $flag = empty( $this->custom_flag ) ? $this->flag : $this->custom_flag; | |
| 484 | + | |
| 485 | + if ( 'alt' === $alt ) { | |
| 486 | + return $flag; | |
| 487 | + } | |
| 488 | + | |
| 489 | + return (string) preg_replace( '/(?<=\salt=\")([^"]+)(?=\")/', '', $flag ); | |
| 415 | 490 | } |
| 416 | 491 | |
| 417 | 492 | /** |
| 418 | 493 | * Returns the url of the custom flag if any, or the default flag otherwise. |
| @@ -421,9 +496,19 @@ | ||
| 421 | 496 | * |
| 422 | 497 | * @return string |
| 423 | 498 | */ |
| 424 | 499 | public function get_display_flag_url() { |
| 425 | - return empty( $this->custom_flag_url ) ? $this->flag_url : $this->custom_flag_url; | |
| 500 | + $flag_url = empty( $this->custom_flag_url ) ? $this->flag_url : $this->custom_flag_url; | |
| 501 | + | |
| 502 | + /** | |
| 503 | + * Filters `flag_url` property. | |
| 504 | + * | |
| 505 | + * @since 3.4.4 | |
| 506 | + * | |
| 507 | + * @param string $flag_url Flag URL. | |
| 508 | + * @param PLL_Language $language Current `PLL_language` instance. | |
| 509 | + */ | |
| 510 | + return apply_filters( 'pll_language_flag_url', $flag_url, $this ); | |
| 426 | 511 | } |
| 427 | 512 | |
| 428 | 513 | /** |
| 429 | 514 | * Updates post and term count. |
| @@ -432,55 +517,154 @@ | ||
| 432 | 517 | * |
| 433 | 518 | * @return void |
| 434 | 519 | */ |
| 435 | 520 | public function update_count() { |
| 436 | - wp_update_term_count( $this->term_taxonomy_id, 'language' ); // Posts count. | |
| 437 | - wp_update_term_count( $this->tl_term_taxonomy_id, 'term_language' ); // Terms count. | |
| 521 | + foreach ( $this->term_props as $taxonomy => $props ) { | |
| 522 | + wp_update_term_count( $props['term_taxonomy_id'], $taxonomy ); | |
| 523 | + } | |
| 438 | 524 | } |
| 439 | 525 | |
| 440 | 526 | /** |
| 441 | - * Set home_url and search_url properties. | |
| 527 | + * Returns the language locale. | |
| 528 | + * Converts WP locales to W3C valid locales for display. | |
| 442 | 529 | * |
| 443 | - * @since 1.3 | |
| 530 | + * @since 1.8 | |
| 444 | 531 | * |
| 445 | - * @param string $search_url Home url to use in search forms. | |
| 446 | - * @param string $home_url Home url. | |
| 447 | - * @return void | |
| 532 | + * @param string $filter Either 'display' or 'raw', defaults to raw. | |
| 533 | + * @return string | |
| 534 | + * | |
| 535 | + * @phpstan-param 'display'|'raw' $filter | |
| 536 | + * @phpstan-return non-empty-string | |
| 448 | 537 | */ |
| 449 | - public function set_home_url( $search_url, $home_url ) { | |
| 450 | - $this->search_url = $search_url; | |
| 451 | - $this->home_url = $home_url; | |
| 538 | + public function get_locale( $filter = 'raw' ) { | |
| 539 | + return 'display' === $filter ? $this->w3c : $this->locale; | |
| 452 | 540 | } |
| 453 | 541 | |
| 454 | 542 | /** |
| 455 | - * Sets the scheme of the home url and the flag urls. | |
| 543 | + * Returns the values of this instance's properties, which can be filtered if required. | |
| 456 | 544 | * |
| 457 | - * This can't be cached across pages. | |
| 545 | + * @since 3.4 | |
| 458 | 546 | * |
| 459 | - * @since 2.8 | |
| 547 | + * @param string $context Whether or not properties should be filtered. Accepts `db` or `display`. | |
| 548 | + * Default to `display` which filters some properties. | |
| 460 | 549 | * |
| 461 | - * @return void | |
| 550 | + * @return array Array of language object properties. | |
| 551 | + * | |
| 552 | + * @phpstan-return LanguageData | |
| 462 | 553 | */ |
| 463 | - public function set_url_scheme() { | |
| 464 | - $this->home_url = set_url_scheme( $this->home_url ); | |
| 465 | - $this->search_url = set_url_scheme( $this->search_url ); | |
| 554 | + public function to_array( $context = 'display' ) { | |
| 555 | + $language = get_object_vars( $this ); | |
| 466 | 556 | |
| 467 | - // Set url scheme, also for the flags. | |
| 468 | - $this->flag_url = set_url_scheme( $this->flag_url ); | |
| 469 | - if ( ! empty( $this->custom_flag_url ) ) { | |
| 470 | - $this->custom_flag_url = set_url_scheme( $this->custom_flag_url ); | |
| 557 | + if ( 'db' !== $context ) { | |
| 558 | + $language['home_url'] = $this->get_home_url(); | |
| 559 | + $language['search_url'] = $this->get_search_url(); | |
| 471 | 560 | } |
| 561 | + | |
| 562 | + /** @phpstan-var LanguageData $language */ | |
| 563 | + return $language; | |
| 472 | 564 | } |
| 473 | 565 | |
| 474 | 566 | /** |
| 475 | - * Returns the language locale. | |
| 476 | - * Converts WP locales to W3C valid locales for display. | |
| 567 | + * Converts current `PLL_language` into a `stdClass` object. Mostly used to allow dynamic properties. | |
| 477 | 568 | * |
| 478 | - * @since 1.8 | |
| 569 | + * @since 3.4 | |
| 479 | 570 | * |
| 480 | - * @param string $filter Either 'display' or 'raw', defaults to raw. | |
| 481 | - * @return string | |
| 571 | + * @return stdClass Converted `PLL_Language` object. | |
| 482 | 572 | */ |
| 483 | - public function get_locale( $filter = 'raw' ) { | |
| 484 | - return 'display' === $filter ? $this->w3c : $this->locale; | |
| 573 | + public function to_std_class() { | |
| 574 | + return (object) $this->to_array(); | |
| 575 | + } | |
| 576 | + | |
| 577 | + /** | |
| 578 | + * Returns a predefined HTML flag. | |
| 579 | + * | |
| 580 | + * @since 3.4 | |
| 581 | + * | |
| 582 | + * @param string $flag_code Flag code to render. | |
| 583 | + * @return string HTML code for the flag. | |
| 584 | + */ | |
| 585 | + public static function get_predefined_flag( $flag_code ) { | |
| 586 | + $flag = self::get_flag_information( $flag_code ); | |
| 587 | + | |
| 588 | + return self::get_flag_html( $flag ); | |
| 589 | + } | |
| 590 | + | |
| 591 | + /** | |
| 592 | + * Returns language's home URL. Takes care to render it dynamically if no cache is allowed. | |
| 593 | + * | |
| 594 | + * @since 3.4 | |
| 595 | + * | |
| 596 | + * @return string Language home URL. | |
| 597 | + */ | |
| 598 | + public function get_home_url() { | |
| 599 | + if ( ! pll_get_constant( 'PLL_CACHE_LANGUAGES', true ) || ! pll_get_constant( 'PLL_CACHE_HOME_URL', true ) ) { | |
| 600 | + /** | |
| 601 | + * Filters current `PLL_Language` instance `home_url` property. | |
| 602 | + * | |
| 603 | + * @since 3.4.4 | |
| 604 | + * | |
| 605 | + * @param string $home_url The `home_url` prop. | |
| 606 | + * @param array $language Current Array of `PLL_Language` properties. | |
| 607 | + */ | |
| 608 | + return apply_filters( 'pll_language_home_url', $this->home_url, $this->to_array( 'db' ) ); | |
| 609 | + } | |
| 610 | + | |
| 611 | + return $this->home_url; | |
| 612 | + } | |
| 613 | + | |
| 614 | + /** | |
| 615 | + * Returns language's search URL. Takes care to render it dynamically if no cache is allowed. | |
| 616 | + * | |
| 617 | + * @since 3.4 | |
| 618 | + * | |
| 619 | + * @return string Language search URL. | |
| 620 | + */ | |
| 621 | + public function get_search_url() { | |
| 622 | + if ( ! pll_get_constant( 'PLL_CACHE_LANGUAGES', true ) || ! pll_get_constant( 'PLL_CACHE_HOME_URL', true ) ) { | |
| 623 | + /** | |
| 624 | + * Filters current `PLL_Language` instance `search_url` property. | |
| 625 | + * | |
| 626 | + * @since 3.4.4 | |
| 627 | + * | |
| 628 | + * @param string $search_url The `search_url` prop. | |
| 629 | + * @param array $language Current Array of `PLL_Language` properties. | |
| 630 | + */ | |
| 631 | + return apply_filters( 'pll_language_search_url', $this->search_url, $this->to_array( 'db' ) ); | |
| 632 | + } | |
| 633 | + | |
| 634 | + return $this->search_url; | |
| 635 | + } | |
| 636 | + | |
| 637 | + /** | |
| 638 | + * Returns the value of a language property. | |
| 639 | + * This is handy to get a property's value without worrying about triggering a deprecation warning or anything. | |
| 640 | + * | |
| 641 | + * @since 3.4 | |
| 642 | + * | |
| 643 | + * @param string $property A property name. A composite value can be used for language term property values, in the | |
| 644 | + * form of `{language_taxonomy_name}:{property_name}` (see {@see PLL_Language::get_tax_prop()} | |
| 645 | + * for the possible values). Ex: `term_language:term_taxonomy_id`. | |
| 646 | + * @return string|int|bool|string[] The requested property for the language, `false` if the property doesn't exist. | |
| 647 | + * | |
| 648 | + * @phpstan-return ( | |
| 649 | + * $property is 'slug' ? non-empty-string : string|int|bool|list<non-empty-string> | |
| 650 | + * ) | |
| 651 | + */ | |
| 652 | + public function get_prop( $property ) { | |
| 653 | + // Deprecated property. | |
| 654 | + if ( $this->is_deprecated_term_property( $property ) ) { | |
| 655 | + return $this->get_deprecated_term_property( $property ); | |
| 656 | + } | |
| 657 | + | |
| 658 | + if ( $this->is_deprecated_url_property( $property ) ) { | |
| 659 | + return $this->get_deprecated_url_property( $property ); | |
| 660 | + } | |
| 661 | + | |
| 662 | + // Composite property like 'term_language:term_taxonomy_id'. | |
| 663 | + if ( preg_match( '/^(?<tax>.{1,32}):(?<field>term_id|term_taxonomy_id|count)$/', $property, $matches ) ) { | |
| 664 | + /** @var array{tax:non-empty-string, field:'term_id'|'term_taxonomy_id'|'count'} $matches */ | |
| 665 | + return $this->get_tax_prop( $matches['tax'], $matches['field'] ); | |
| 666 | + } | |
| 667 | + | |
| 668 | + return $this->$property ?? false; | |
| 485 | 669 | } |
| 486 | 670 | } |