Changeset 48411
- Timestamp:
- 07/09/2020 11:24:43 AM (5 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/meta.php
r48408 r48411 57 57 * Short-circuits adding metadata of a specific type. 58 58 * 59 * The dynamic portion of the hook, `$meta_type`, refers to the meta 60 * object type (comment, post, term, or user). Returning a non-null value61 * will effectively short-circuit the function.59 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type 60 * (post, comment, term, user, or any other type with an associated meta table). 61 * Returning a non-null value will effectively short-circuit the function. 62 62 * 63 63 * @since 3.1.0 … … 90 90 * Fires immediately before meta of a specific type is added. 91 91 * 92 * The dynamic portion of the hook, `$meta_type`, refers to the meta 93 * object type (comment, post, term, or user).92 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type 93 * (post, comment, term, user, or any other type with an associated meta table). 94 94 * 95 95 * @since 3.1.0 … … 121 121 * Fires immediately after meta of a specific type is added. 122 122 * 123 * The dynamic portion of the hook, `$meta_type`, refers to the meta 124 * object type (comment, post, term, or user).123 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type 124 * (post, comment, term, user, or any other type with an associated meta table). 125 125 * 126 126 * @since 2.9.0 … … 190 190 * Short-circuits updating metadata of a specific type. 191 191 * 192 * The dynamic portion of the hook, `$meta_type`, refers to the meta 193 * object type (comment, post, term, or user). Returning a non-null value194 * will effectively short-circuit the function.192 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type 193 * (post, comment, term, user, or any other type with an associated meta table). 194 * Returning a non-null value will effectively short-circuit the function. 195 195 * 196 196 * @since 3.1.0 … … 242 242 * Fires immediately before updating metadata of a specific type. 243 243 * 244 * The dynamic portion of the hook, `$meta_type`, refers to the meta 245 * object type (comment, post, term, or user).244 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type 245 * (post, comment, term, user, or any other type with an associated meta table). 246 246 * 247 247 * @since 2.9.0 … … 281 281 * Fires immediately after updating metadata of a specific type. 282 282 * 283 * The dynamic portion of the hook, `$meta_type`, refers to the meta 284 * object type (comment, post, term, or user).283 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type 284 * (post, comment, term, user, or any other type with an associated meta table). 285 285 * 286 286 * @since 2.9.0 … … 361 361 * Short-circuits deleting metadata of a specific type. 362 362 * 363 * The dynamic portion of the hook, `$meta_type`, refers to the meta 364 * object type (comment, post, term, or user). Returning a non-null value365 * will effectively short-circuit the function.363 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type 364 * (post, comment, term, user, or any other type with an associated meta table). 365 * Returning a non-null value will effectively short-circuit the function. 366 366 * 367 367 * @since 3.1.0 … … 409 409 * Fires immediately before deleting metadata of a specific type. 410 410 * 411 * The dynamic portion of the hook, `$meta_type`, refers to the meta 412 * object type (comment, post, term, or user).411 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type 412 * (post, comment, term, user, or any other type with an associated meta table). 413 413 * 414 414 * @since 3.1.0 … … 452 452 * Fires immediately after deleting metadata of a specific type. 453 453 * 454 * The dynamic portion of the hook name, `$meta_type`, refers to the meta455 * object type (comment, post, term, or user).454 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type 455 * (post, comment, term, user, or any other type with an associated meta table). 456 456 * 457 457 * @since 2.9.0 … … 482 482 * Retrieves the value of a metadata field for the specified object type and ID. 483 483 * 484 * If the meta field exists, a single value is returned if `$single` is true, or an array of values if it's false. 485 * If the meta field does not exist, an empty string is returned if `$single` is true, or an empty array if it's false. 486 * If there's a problem with the parameters passed to the function, boolean `false` is returned. 487 * 488 * @since 5.5.0 484 * If the meta field exists, a single value is returned if `$single` is true, 485 * or an array of values if it's false. 486 * 487 * If the meta field does not exist, the result depends on get_metadata_default(). 488 * By default, an empty string is returned if `$single` is true, or an empty array if it's false. 489 * 490 * @since 2.9.0 491 * 492 * @see get_metadata_raw() 493 * @see get_metadata_default() 489 494 * 490 495 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', … … 495 500 * @param bool $single Optional. If true, return only the first value of the specified meta_key. 496 501 * This parameter has no effect if meta_key is not specified. Default false. 497 * @return mixed The metadata value or array of values. See description above for further details. 502 * @return mixed Single metadata value, or array of values. 503 * False if there's a problem with the parameters passed to the function. 504 */ 505 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) { 506 $value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single ); 507 if ( ! is_null( $value ) ) { 508 return $value; 509 } 510 511 return get_metadata_default( $meta_type, $meta_key, $single, $object_id ); 512 } 513 514 /** 515 * Retrieves raw metadata value for the specified object. 516 * 517 * @since 5.5.0 518 * 519 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 520 * or any other object type with an associated meta table. 521 * @param int $object_id ID of the object metadata is for. 522 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 523 * the specified object. Default empty. 524 * @param bool $single Optional. If true, return only the first value of the specified meta_key. 525 * This parameter has no effect if meta_key is not specified. Default false. 526 * @return mixed Single metadata value, or array of values. Null if the value does not exist. 527 * False if there's a problem with the parameters passed to the function. 498 528 */ 499 529 function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) { … … 510 540 * Short-circuits the return value of a meta field. 511 541 * 512 * The dynamic portion of the hook name, `$meta_type`, refers to the513 * object type (comment, post, term, or user). Returning a non-null value514 * will short-circuit the return value.542 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type 543 * (post, comment, term, user, or any other type with an associated meta table). 544 * Returning a non-null value will effectively short-circuit the function. 515 545 * 516 546 * @since 3.1.0 … … 520 550 * @param int $object_id ID of the object metadata is for. 521 551 * @param string $meta_key Metadata key. 522 * @param bool $single Whether to return only the first value of the specified $meta_key.552 * @param bool $single Whether to return only the first value of the specified `$meta_key`. 523 553 */ 524 554 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single ); … … 558 588 559 589 /** 560 * Retrieves raw metadata for the specified object. 561 * 562 * @since 2.9.0 563 * @uses get_metadata_raw() 590 * Retrieves default metadata value for the specified object. 591 * 592 * By default, an empty string is returned if `$single` is true, or an empty array if it's false. 593 * 594 * @since 5.5.0 564 595 * 565 596 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 566 597 * or any other object type with an associated meta table. 567 * @param int $object_id ID of the object metadata is for. 568 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 569 * the specified object. Default empty. 598 * @param string $meta_key Metadata key. 570 599 * @param bool $single Optional. If true, return only the first value of the specified meta_key. 571 600 * This parameter has no effect if meta_key is not specified. Default false. 572 * @return mixed Single metadata value, or array of values 573 */ 574 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) { 575 $value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single ); 576 if ( ! is_null( $value ) ) { 577 return $value; 578 } 579 580 return get_metadata_default( $meta_type, $meta_key, $single, $object_id ); 581 } 582 583 /** 584 * Retrieve metadata data default for the specified object. 585 * 586 * @since 5.5.0 587 * 588 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). 589 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 590 * the specified object. 591 * @param bool $single Optional, default is false. 592 * If true, return only the first value of the specified meta_key. 593 * This parameter has no effect if meta_key is not specified. 594 * @param int $object_id Optional, default is 0. 595 * ID of the object metadata is for 596 * @return mixed Single metadata value, or array of values 601 * @param int $object_id Optional. ID of the object metadata is for. Default 0. 602 * @return mixed Single metadata value, or array of values. 597 603 */ 598 604 function get_metadata_default( $meta_type, $meta_key, $single = false, $object_id = 0 ) { … … 604 610 605 611 /** 606 * Filter the default value a specified object. 612 * Filter the default value for a specified object. 613 * 614 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type 615 * (post, comment, term, user, or any other type with an associated meta table). 607 616 * 608 617 * @since 5.5.0 609 618 * 610 * @param array|string $value The value should return - a single metadata value, 611 * or an array of values. 612 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). 613 * @param string $meta_key Meta key. 614 * @param bool $single Whether to return only the first value of the specified $meta_key. 615 * @param int $object_id Object ID. 619 * @param mixed $value The value to return, either a single metadata value or an array 620 * of values depending on the value of `$single`. 621 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 622 * or any other object type with an associated meta table. 623 * @param string $meta_key Metadata key. 624 * @param bool $single Whether to return only the first value of the specified `$meta_key`. 625 * @param int $object_id ID of the object metadata is for. 616 626 */ 617 627 $value = apply_filters( "default_{$meta_type}_metadata", $value, $meta_type, $meta_key, $single, $object_id ); … … 705 715 } 706 716 707 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';708 709 717 /** 710 718 * Short-circuits the return value when fetching a meta field by meta ID. 711 719 * 712 * The dynamic portion of the hook name, `$meta_type`, refers to the713 * object type (comment, post, term, or user). Returning a non-null value714 * will short-circuit the return value.720 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type 721 * (post, comment, term, user, or any other type with an associated meta table). 722 * Returning a non-null value will effectively short-circuit the function. 715 723 * 716 724 * @since 5.0.0 … … 724 732 } 725 733 734 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 735 726 736 $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) ); 727 737 … … 775 785 * Short-circuits updating metadata of a specific type by meta ID. 776 786 * 777 * The dynamic portion of the hook, `$meta_type`, refers to the meta 778 * object type (comment, post, term, or user). Returning a non-null value779 * will effectively short-circuit the function.787 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type 788 * (post, comment, term, user, or any other type with an associated meta table). 789 * Returning a non-null value will effectively short-circuit the function. 780 790 * 781 791 * @since 5.0.0 … … 891 901 * Short-circuits deleting metadata of a specific type by meta ID. 892 902 * 893 * The dynamic portion of the hook, `$meta_type`, refers to the meta 894 * object type (comment, post, term, or user). Returning a non-null value895 * will effectively short-circuit the function.903 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type 904 * (post, comment, term, user, or any other type with an associated meta table). 905 * Returning a non-null value will effectively short-circuit the function. 896 906 * 897 907 * @since 5.0.0 … … 996 1006 * Short-circuits updating the metadata cache of a specific type. 997 1007 * 998 * The dynamic portion of the hook, `$meta_type`, refers to the meta 999 * object type (comment, post, term, or user). Returning a non-null value1000 * will effectively short-circuit the function.1008 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type 1009 * (post, comment, term, user, or any other type with an associated meta table). 1010 * Returning a non-null value will effectively short-circuit the function. 1001 1011 * 1002 1012 * @since 5.0.0 … … 1365 1375 1366 1376 /** 1377 * Filters into default_{$object_type}_metadata and adds in default value. 1378 * 1379 * @since 5.5.0 1380 * 1381 * @param mixed $value Current value passed to filter. 1382 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1383 * or any other object type with an associated meta table. 1384 * @param string $meta_key Metadata key. 1385 * @param bool $single If true, return only the first value of the specified meta_key. 1386 * This parameter has no effect if meta_key is not specified. 1387 * @param int $object_id ID of the object metadata is for. 1388 * @return mixed Single metadata default, or array of defaults. 1389 */ 1390 function filter_default_metadata( $value, $meta_type, $meta_key, $single, $object_id ) { 1391 global $wp_meta_keys; 1392 1393 if ( wp_installing() ) { 1394 return $value; 1395 } 1396 1397 if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $meta_type ] ) ) { 1398 return $value; 1399 } 1400 1401 $defaults = array(); 1402 foreach ( $wp_meta_keys[ $meta_type ] as $sub_type => $meta_data ) { 1403 foreach ( $meta_data as $_meta_key => $args ) { 1404 if ( $_meta_key === $meta_key && array_key_exists( 'default', $args ) ) { 1405 $defaults[ $sub_type ] = $args; 1406 } 1407 } 1408 } 1409 1410 if ( ! $defaults ) { 1411 return $value; 1412 } 1413 1414 // If this meta type does not have sub types, then the default is keyed as an empty string. 1415 if ( isset( $defaults[''] ) ) { 1416 $metadata = $defaults['']; 1417 } else { 1418 $sub_type = get_object_subtype( $meta_type, $object_id ); 1419 if ( ! isset( $defaults[ $sub_type ] ) ) { 1420 return $value; 1421 } 1422 $metadata = $defaults[ $sub_type ]; 1423 } 1424 1425 if ( $single ) { 1426 $value = $metadata['default']; 1427 } else { 1428 $value = array( $metadata['default'] ); 1429 } 1430 1431 return $value; 1432 } 1433 1434 /** 1367 1435 * Checks if a meta key is registered. 1368 1436 * … … 1574 1642 * Filters the object subtype identifier for a non-standard object type. 1575 1643 * 1576 * The dynamic portion of the hook, `$object_type`, refers to the object1577 * type (post, comment, term, or user).1644 * The dynamic portion of the hook, `$object_type`, refers to the meta object type 1645 * (post, comment, term, user, or any other type with an associated meta table). 1578 1646 * 1579 1647 * @since 4.9.8 … … 1584 1652 return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id ); 1585 1653 } 1586 1587 /**1588 * Filter into default_{$object_type}_metadata and add in default value.1589 *1590 * @since 5.5.01591 *1592 * @param mixed $value Current value passed to filter.1593 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).1594 1595 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for1596 * the specified object.1597 * @param bool $single Optional, default is false.1598 * If true, return only the first value of the specified meta_key.1599 * This parameter has no effect if meta_key is not specified.1600 * @param int $object_id ID of the object metadata is for1601 *1602 * @return mixed Single metadata default, or array of defaults1603 */1604 function filter_default_metadata( $value, $meta_type, $meta_key, $single, $object_id ) {1605 global $wp_meta_keys;1606 1607 if ( wp_installing() ) {1608 return $value;1609 }1610 1611 if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $meta_type ] ) ) {1612 return $value;1613 }1614 1615 $defaults = array();1616 foreach ( $wp_meta_keys[ $meta_type ] as $sub_type => $meta_data ) {1617 foreach ( $meta_data as $_meta_key => $args ) {1618 if ( $_meta_key === $meta_key && array_key_exists( 'default', $args ) ) {1619 $defaults[ $sub_type ] = $args;1620 }1621 }1622 }1623 1624 if ( ! $defaults ) {1625 return $value;1626 }1627 1628 // If this meta type does not have sub types, then the default is keyed as an empty string.1629 if ( isset( $defaults[''] ) ) {1630 $metadata = $defaults[''];1631 } else {1632 $sub_type = get_object_subtype( $meta_type, $object_id );1633 if ( ! isset( $defaults[ $sub_type ] ) ) {1634 return $value;1635 }1636 $metadata = $defaults[ $sub_type ];1637 }1638 1639 if ( $single ) {1640 $value = $metadata['default'];1641 } else {1642 $value = array( $metadata['default'] );1643 }1644 1645 return $value;1646 } -
trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php
r48402 r48411 80 80 $name = $args['name']; 81 81 $all_values = get_metadata( $this->get_meta_type(), $object_id, $meta_key, false ); 82 82 83 if ( $args['single'] ) { 83 84 if ( empty( $all_values ) ) { -
trunk/tests/phpunit/tests/meta/registerMeta.php
r48402 r48411 667 667 ), 668 668 669 // types669 // Types. 670 670 'single object key with single ask' => array( 671 671 array(
Note: See TracChangeset
for help on using the changeset viewer.