Changeset 48357 for trunk/src/wp-includes/rest-api.php
- Timestamp:
- 07/07/2020 03:20:34 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api.php
r48307 r48357 1440 1440 1441 1441 /** 1442 * Checks if an array is made up of unique items. 1443 * 1444 * @since 5.5.0 1445 * 1446 * @param array $array The array to check. 1447 * @return bool True if the array contains unique items, false otherwise. 1448 */ 1449 function rest_validate_array_contains_unique_items( $array ) { 1450 $seen = array(); 1451 1452 foreach ( $array as $item ) { 1453 $stabilized = rest_stabilize_value( $item ); 1454 $key = serialize( $stabilized ); 1455 1456 if ( ! isset( $seen[ $key ] ) ) { 1457 $seen[ $key ] = true; 1458 1459 continue; 1460 } 1461 1462 return false; 1463 } 1464 1465 return true; 1466 } 1467 1468 /** 1469 * Stabilizes a value following JSON Schema semantics. 1470 * 1471 * For lists, order is preserved. For objects, properties are reordered alphabetically. 1472 * 1473 * @since 5.5.0 1474 * 1475 * @param mixed $value The value to stabilize. Must already be sanitized. Objects should have been converted to arrays. 1476 * @return mixed The stabilized value. 1477 */ 1478 function rest_stabilize_value( $value ) { 1479 if ( is_scalar( $value ) || is_null( $value ) ) { 1480 return $value; 1481 } 1482 1483 if ( is_object( $value ) ) { 1484 _doing_it_wrong( __FUNCTION__, __( 'Cannot stabilize objects. Convert the object to an array first.' ), '5.5.0' ); 1485 1486 return $value; 1487 } 1488 1489 ksort( $value ); 1490 1491 foreach ( $value as $k => $v ) { 1492 $value[ $k ] = rest_stabilize_value( $v ); 1493 } 1494 1495 return $value; 1496 } 1497 1498 /** 1442 1499 * Validate a value based on a schema. 1443 1500 * … … 1449 1506 * @since 5.5.0 Add the "uuid" and "hex-color" formats. 1450 1507 * Support the "minLength", "maxLength" and "pattern" keywords for strings. 1508 * Support the "minItems", "maxItems" and "uniqueItems" keywords for arrays. 1451 1509 * Validate required properties. 1452 * Support the "minItems" and "maxItems" keywords for arrays.1453 1510 * 1454 1511 * @param mixed $value The value to validate. … … 1493 1550 $value = rest_sanitize_array( $value ); 1494 1551 1495 foreach ( $value as $index => $v ) { 1496 $is_valid = rest_validate_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' ); 1497 if ( is_wp_error( $is_valid ) ) { 1498 return $is_valid; 1552 if ( isset( $args['items'] ) ) { 1553 foreach ( $value as $index => $v ) { 1554 $is_valid = rest_validate_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' ); 1555 if ( is_wp_error( $is_valid ) ) { 1556 return $is_valid; 1557 } 1499 1558 } 1500 1559 } … … 1508 1567 /* translators: 1: Parameter, 2: Number. */ 1509 1568 return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s must contain at most %2$s items.' ), $param, number_format_i18n( $args['maxItems'] ) ) ); 1569 } 1570 1571 if ( ! empty( $args['uniqueItems'] ) && ! rest_validate_array_contains_unique_items( $value ) ) { 1572 /* translators: 1: Parameter */ 1573 return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s has duplicate items.' ), $param ) ); 1510 1574 } 1511 1575 } … … 1719 1783 * @param array $args Schema array to use for sanitization. 1720 1784 * @param string $param The parameter name, used in error messages. 1721 * @return mixed The sanitized value.1785 * @return mixed|WP_Error The sanitized value or a WP_Error instance if the value cannot be safely sanitized. 1722 1786 */ 1723 1787 function rest_sanitize_value_from_schema( $value, $args, $param = '' ) { … … 1751 1815 $value = rest_sanitize_array( $value ); 1752 1816 1753 if ( empty( $args['items'] ) ) { 1754 return $value; 1755 } 1756 1757 foreach ( $value as $index => $v ) { 1758 $value[ $index ] = rest_sanitize_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' ); 1817 if ( ! empty( $args['items'] ) ) { 1818 foreach ( $value as $index => $v ) { 1819 $value[ $index ] = rest_sanitize_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' ); 1820 } 1821 } 1822 1823 if ( ! empty( $args['uniqueItems'] ) && ! rest_validate_array_contains_unique_items( $value ) ) { 1824 /* translators: 1: Parameter */ 1825 return new WP_Error( 'rest_invalid_param', sprintf( __( '%1$s has duplicate items.' ), $param ) ); 1759 1826 } 1760 1827
Note: See TracChangeset
for help on using the changeset viewer.