← All changes
|
includes/admin/settings/class-settings-sanitize.php
+164
-9
4.4.3
→
4.5.1
View file →
| @@ -73,15 +73,33 @@ | ||
| 73 | 73 | return $default_value; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | - * Miscellaneous sanitize function | |
| 77 | + * Fallback for field types that declare no sanitize callback of their own. | |
| 78 | 78 | * |
| 79 | 79 | * @param mixed $value Setting Value. |
| 80 | - * @return string Sanitized value. | |
| 80 | + * @return mixed Sanitized value. | |
| 81 | 81 | */ |
| 82 | 82 | public function sanitize_missing( $value ) { |
| 83 | - return $value; | |
| 83 | + if ( is_array( $value ) ) { | |
| 84 | + $sanitized = array(); | |
| 85 | + | |
| 86 | + foreach ( $value as $key => $item ) { | |
| 87 | + $sanitized[ sanitize_text_field( (string) $key ) ] = $this->sanitize_missing( $item ); | |
| 88 | + } | |
| 89 | + | |
| 90 | + return $sanitized; | |
| 91 | + } | |
| 92 | + | |
| 93 | + if ( is_bool( $value ) || is_int( $value ) || is_float( $value ) ) { | |
| 94 | + return $value; | |
| 95 | + } | |
| 96 | + | |
| 97 | + if ( is_object( $value ) || is_null( $value ) ) { | |
| 98 | + return ''; | |
| 99 | + } | |
| 100 | + | |
| 101 | + return sanitize_text_field( wp_unslash( (string) $value ) ); | |
| 84 | 102 | } |
| 85 | 103 | |
| 86 | 104 | /** |
| 87 | 105 | * Sanitize text fields |
| @@ -148,8 +166,12 @@ | ||
| 148 | 166 | * @return string Sanitized value |
| 149 | 167 | */ |
| 150 | 168 | public function sanitize_textarea_field( $value ) { |
| 151 | 169 | |
| 170 | + if ( ! current_user_can( 'unfiltered_html' ) ) { | |
| 171 | + return wp_kses_post( wp_unslash( $value ) ); | |
| 172 | + } | |
| 173 | + | |
| 152 | 174 | global $allowedposttags; |
| 153 | 175 | |
| 154 | 176 | // We need more tags to allow for script and style. |
| 155 | 177 | $moretags = array( |
| @@ -271,8 +293,145 @@ | ||
| 271 | 293 | return esc_url_raw( $value ); |
| 272 | 294 | } |
| 273 | 295 | |
| 274 | 296 | /** |
| 297 | + * Sanitize file fields, which hold the URL picked in the media browser. | |
| 298 | + * | |
| 299 | + * @param string $value The field value. | |
| 300 | + * @return string Sanitized value | |
| 301 | + */ | |
| 302 | + public function sanitize_file_field( $value ) { | |
| 303 | + return esc_url_raw( wp_unslash( $value ) ); | |
| 304 | + } | |
| 305 | + | |
| 306 | + /** | |
| 307 | + * Sanitize password fields. | |
| 308 | + * | |
| 309 | + * @param string $value The field value. | |
| 310 | + * @return string Sanitized value | |
| 311 | + */ | |
| 312 | + public function sanitize_password_field( $value ) { | |
| 313 | + return sanitize_text_field( wp_unslash( $value ) ); | |
| 314 | + } | |
| 315 | + | |
| 316 | + /** | |
| 317 | + * Sanitize WYSIWYG fields. | |
| 318 | + * | |
| 319 | + * @param string $value The field value. | |
| 320 | + * @return string Sanitized value | |
| 321 | + */ | |
| 322 | + public function sanitize_wysiwyg_field( $value ) { | |
| 323 | + return wp_kses_post( wp_unslash( $value ) ); | |
| 324 | + } | |
| 325 | + | |
| 326 | + /** | |
| 327 | + * Sanitize HTML fields. | |
| 328 | + * | |
| 329 | + * @param string $value The field value. | |
| 330 | + * @return string Sanitized value | |
| 331 | + */ | |
| 332 | + public function sanitize_html_field( $value ) { | |
| 333 | + return $this->sanitize_textarea_field( $value ); | |
| 334 | + } | |
| 335 | + | |
| 336 | + /** | |
| 337 | + * Sanitize CSS fields. | |
| 338 | + * | |
| 339 | + * @param string $value The field value. | |
| 340 | + * @return string Sanitized value | |
| 341 | + */ | |
| 342 | + public function sanitize_css_field( $value ) { | |
| 343 | + return wp_strip_all_tags( wp_unslash( $value ) ); | |
| 344 | + } | |
| 345 | + | |
| 346 | + /** | |
| 347 | + * Sanitize radio fields against the options the field actually offers. | |
| 348 | + * | |
| 349 | + * @param mixed $value The field value. | |
| 350 | + * @param array $field Field configuration array. | |
| 351 | + * @return string Sanitized value | |
| 352 | + */ | |
| 353 | + public function sanitize_radio_field( $value, $field = array() ) { | |
| 354 | + return $this->sanitize_choice( $value, array_keys( (array) ( $field['options'] ?? array() ) ), $field ); | |
| 355 | + } | |
| 356 | + | |
| 357 | + /** | |
| 358 | + * Sanitize select fields against the options the field actually offers. | |
| 359 | + * | |
| 360 | + * @param mixed $value The field value. | |
| 361 | + * @param array $field Field configuration array. | |
| 362 | + * @return string Sanitized value | |
| 363 | + */ | |
| 364 | + public function sanitize_select_field( $value, $field = array() ) { | |
| 365 | + return $this->sanitize_choice( $value, array_keys( (array) ( $field['options'] ?? array() ) ), $field ); | |
| 366 | + } | |
| 367 | + | |
| 368 | + /** | |
| 369 | + * Sanitize radio fields that carry a description per option. | |
| 370 | + * | |
| 371 | + * @param mixed $value The field value. | |
| 372 | + * @param array $field Field configuration array. | |
| 373 | + * @return string Sanitized value | |
| 374 | + */ | |
| 375 | + public function sanitize_radiodesc_field( $value, $field = array() ) { | |
| 376 | + $allowed = array(); | |
| 377 | + | |
| 378 | + foreach ( (array) ( $field['options'] ?? array() ) as $option ) { | |
| 379 | + if ( isset( $option['id'] ) ) { | |
| 380 | + $allowed[] = $option['id']; | |
| 381 | + } | |
| 382 | + } | |
| 383 | + | |
| 384 | + return $this->sanitize_choice( $value, $allowed, $field ); | |
| 385 | + } | |
| 386 | + | |
| 387 | + /** | |
| 388 | + * Sanitize thumbnail size fields. | |
| 389 | + * | |
| 390 | + * @param mixed $value The field value. | |
| 391 | + * @param array $field Field configuration array. | |
| 392 | + * @return string Sanitized value | |
| 393 | + */ | |
| 394 | + public function sanitize_thumbsizes_field( $value, $field = array() ) { | |
| 395 | + $allowed = array_keys( (array) ( $field['options'] ?? array() ) ); | |
| 396 | + | |
| 397 | + // The form injects this size at render time, so it is never in the registered options. | |
| 398 | + $allowed[] = $this->prefix . '_thumbnail'; | |
| 399 | + | |
| 400 | + return $this->sanitize_choice( $value, $allowed, $field ); | |
| 401 | + } | |
| 402 | + | |
| 403 | + /** | |
| 404 | + * Restrict a value to a list of allowed choices. | |
| 405 | + * | |
| 406 | + * @param mixed $value The field value. | |
| 407 | + * @param array $allowed Allowed choices. | |
| 408 | + * @param array $field Field configuration array. | |
| 409 | + * @return string Sanitized value | |
| 410 | + */ | |
| 411 | + protected function sanitize_choice( $value, $allowed, $field = array() ) { | |
| 412 | + $value = sanitize_text_field( wp_unslash( (string) $value ) ); | |
| 413 | + $allowed = array_map( 'strval', (array) $allowed ); | |
| 414 | + | |
| 415 | + if ( in_array( $value, $allowed, true ) ) { | |
| 416 | + return $value; | |
| 417 | + } | |
| 418 | + | |
| 419 | + // The select callback prints option values through sanitize_key(). | |
| 420 | + foreach ( $allowed as $choice ) { | |
| 421 | + if ( sanitize_key( $choice ) === $value ) { | |
| 422 | + return $choice; | |
| 423 | + } | |
| 424 | + } | |
| 425 | + | |
| 426 | + if ( isset( $field['default'] ) ) { | |
| 427 | + return (string) $field['default']; | |
| 428 | + } | |
| 429 | + | |
| 430 | + return empty( $allowed ) ? '' : reset( $allowed ); | |
| 431 | + } | |
| 432 | + | |
| 433 | + /** | |
| 275 | 434 | * Sanitize sensitive fields. |
| 276 | 435 | * |
| 277 | 436 | * @param string $value The field value. |
| 278 | 437 | * @param string|array $key The field key. |
| @@ -415,13 +574,9 @@ | ||
| 415 | 574 | |
| 416 | 575 | /** |
| 417 | 576 | * Find repeater rows that fail their own required-field rules. |
| 418 | 577 | * |
| 419 | - * A subfield is required when its own config sets `required => true`. A repeater | |
| 420 | - * can additionally set `required_one_of => array( subfield_id, ... )` on itself to | |
| 421 | - * require at least one of several alternative subfields per row (e.g. a post OR a | |
| 422 | - * URL). Purely structural - it returns what is wrong, not a human message, so it | |
| 423 | - * carries no i18n and can be reused unchanged by any plugin that copies this file. | |
| 578 | + * Purely structural - returns what is wrong, not a human message, so it carries no i18n. | |
| 424 | 579 | * |
| 425 | 580 | * @param array $rows Sanitized repeater rows, as returned by sanitize_repeater_field(). |
| 426 | 581 | * @param array $field Repeater field configuration. |
| 427 | 582 | * @return array Map of row index => issue, where issue may have a 'missing' key |
| @@ -544,9 +699,9 @@ | ||
| 544 | 699 | return $output; |
| 545 | 700 | } |
| 546 | 701 | |
| 547 | 702 | /** |
| 548 | - * Processes category/taxonomy slugs and adds a new element to the settings array containing the term taxonomy IDs. | |
| 703 | + * Resolve taxonomy slugs to term taxonomy IDs. | |
| 549 | 704 | * |
| 550 | 705 | * @param array $settings The settings array containing the taxonomy slugs to sanitize. |
| 551 | 706 | * @param string $source_key The key in the settings array containing the slugs. Pattern is Name (taxonomy:term_taxonomy_id). |
| 552 | 707 | * @param string $target_key The key in the settings array to store the sanitized term taxonomy IDs. |