Changeset 48402
- Timestamp:
- 07/07/2020 08:45:55 PM (4 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/meta.php
r48214 r48402 211 211 // Compare existing value to new value if no prev value given and the key exists only once. 212 212 if ( empty( $prev_value ) ) { 213 $old_value = get_metadata ( $meta_type, $object_id, $meta_key );214 if ( count( $old_value )== 1 ) {213 $old_value = get_metadata_raw( $meta_type, $object_id, $meta_key ); 214 if ( is_countable( $old_value ) && count( $old_value ) === 1 ) { 215 215 if ( $old_value[0] === $meta_value ) { 216 216 return false; … … 486 486 * If there's a problem with the parameters passed to the function, boolean `false` is returned. 487 487 * 488 * @since 2.9.0488 * @since 5.5.0 489 489 * 490 490 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', … … 497 497 * @return mixed The metadata value or array of values. See description above for further details. 498 498 */ 499 function get_metadata ( $meta_type, $object_id, $meta_key = '', $single = false ) {499 function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) { 500 500 if ( ! $meta_type || ! is_numeric( $object_id ) ) { 501 501 return false; … … 554 554 } 555 555 556 return null; 557 } 558 559 /** 560 * Retrieves raw metadata for the specified object. 561 * 562 * @since 2.9.0 563 * @uses get_metadata_raw() 564 * 565 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 566 * 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. 570 * @param bool $single Optional. If true, return only the first value of the specified meta_key. 571 * 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 597 */ 598 function get_metadata_default( $meta_type, $meta_key, $single = false, $object_id = 0 ) { 556 599 if ( $single ) { 557 return'';600 $value = ''; 558 601 } else { 559 return array(); 560 } 602 $value = array(); 603 } 604 605 /** 606 * Filter the default value a specified object. 607 * 608 * @since 5.5.0 609 * 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. 616 */ 617 $value = apply_filters( "default_{$meta_type}_metadata", $value, $meta_type, $meta_key, $single, $object_id ); 618 619 if ( ! $single && ! wp_is_numeric_array( $value ) ) { 620 $value = array( $value ); 621 } 622 623 return $value; 561 624 } 562 625 … … 1164 1227 * @type string $description A description of the data attached to this meta key. 1165 1228 * @type bool $single Whether the meta key has one value per object, or an array of values per object. 1229 * @type mixed $default The default value returned from {@see get_metadata()} if no value has been set yet. 1230 * When using a non-single meta key, the default value is for the first entry. In other 1231 * words, when calling {@see get_metadata()} with `$single` set to `false`, the default 1232 * value given here will be wrapped in an array. 1166 1233 * @type string $sanitize_callback A function or method to call when sanitizing `$meta_key` data. 1167 1234 * @type string $auth_callback Optional. A function or method to call when performing edit_post_meta, … … 1189 1256 'type' => 'string', 1190 1257 'description' => '', 1258 'default' => '', 1191 1259 'single' => false, 1192 1260 'sanitize_callback' => null, … … 1226 1294 */ 1227 1295 $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); 1296 unset( $defaults['default'] ); 1228 1297 $args = wp_parse_args( $args, $defaults ); 1229 1298 … … 1262 1331 } else { 1263 1332 add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 ); 1333 } 1334 } 1335 1336 if ( array_key_exists( 'default', $args ) ) { 1337 $schema = $args; 1338 if ( is_array( $args['show_in_rest'] ) && isset( $args['show_in_rest']['schema'] ) ) { 1339 $schema = array_merge( $schema, $args['show_in_rest']['schema'] ); 1340 } 1341 1342 $check = rest_validate_value_from_schema( $args['default'], $schema ); 1343 if ( is_wp_error( $check ) ) { 1344 _doing_it_wrong( __FUNCTION__, __( 'When registering a default meta value the data must match the type provided.' ), '5.5.0' ); 1345 1346 return false; 1347 } 1348 1349 if ( ! has_filter( "default_{$object_type}_metadata", 'filter_default_metadata' ) ) { 1350 add_filter( "default_{$object_type}_metadata", 'filter_default_metadata', 10, 5 ); 1264 1351 } 1265 1352 } … … 1497 1584 return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id ); 1498 1585 } 1586 1587 /** 1588 * Filter into default_{$object_type}_metadata and add in default value. 1589 * 1590 * @since 5.5.0 1591 * 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 for 1596 * 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 for 1601 * 1602 * @return mixed Single metadata default, or array of defaults 1603 */ 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
r47943 r48402 80 80 $name = $args['name']; 81 81 $all_values = get_metadata( $this->get_meta_type(), $object_id, $meta_key, false ); 82 83 82 if ( $args['single'] ) { 84 83 if ( empty( $all_values ) ) { -
trunk/tests/phpunit/tests/meta/registerMeta.php
r46586 r48402 505 505 } 506 506 507 /** 508 * @ticket 43941 509 * @dataProvider data_get_default_data 510 */ 511 public function test_get_default_value( $args, $single, $expected ) { 512 513 $object_type = 'post'; 514 $meta_key = 'registered_key1'; 515 register_meta( 516 $object_type, 517 $meta_key, 518 $args 519 ); 520 521 $object_property_name = $object_type . '_id'; 522 $object_id = self::$$object_property_name; 523 $default_value = get_metadata_default( $object_type, $meta_key, $single, $object_id ); 524 $this->assertSame( $default_value, $expected ); 525 526 // Check for default value. 527 $value = get_metadata( $object_type, $object_id, $meta_key, $single ); 528 $this->assertSame( $value, $expected ); 529 530 // Set value to check default is not being returned by mistake. 531 $meta_value = 'dibble'; 532 update_metadata( $object_type, $object_id, $meta_key, $meta_value ); 533 $value = get_metadata( $object_type, $object_id, $meta_key, true ); 534 $this->assertSame( $value, $meta_value ); 535 536 // Delete meta, make sure the default is returned. 537 delete_metadata( $object_type, $object_id, $meta_key ); 538 $value = get_metadata( $object_type, $object_id, $meta_key, $single ); 539 $this->assertSame( $value, $expected ); 540 541 // Set other meta key, to make sure other keys are not effects. 542 $meta_value = 'hibble'; 543 $meta_key = 'unregistered_key1'; 544 $value = get_metadata( $object_type, $object_id, $meta_key, true ); 545 $this->assertSame( $value, '' ); 546 update_metadata( $object_type, $object_id, $meta_key, $meta_value ); 547 $value = get_metadata( $object_type, $object_id, $meta_key, true ); 548 $this->assertSame( $value, $meta_value ); 549 550 } 551 552 /** 553 * @ticket 43941 554 * @dataProvider data_get_invalid_default_data 555 */ 556 public function test_get_invalid_default_value( $args, $single, $expected ) { 557 $this->setExpectedIncorrectUsage( 'register_meta' ); 558 $object_type = 'post'; 559 $meta_key = 'registered_key1'; 560 $register = register_meta( 561 $object_type, 562 $meta_key, 563 $args 564 ); 565 566 $this->assertFalse( $register ); 567 568 $object_property_name = $object_type . '_id'; 569 $object_id = self::$$object_property_name; 570 $default_value = get_metadata_default( $object_type, $meta_key, $single, $object_id ); 571 $this->assertSame( $default_value, $expected ); 572 } 573 507 574 public function filter_get_object_subtype_for_customtype( $subtype, $object_id ) { 508 575 if ( 1 === ( $object_id % 2 ) ) { … … 511 578 512 579 return 'even'; 580 } 581 582 public function data_get_default_data() { 583 return array( 584 'single string key with single ask ' => array( 585 array( 586 'single' => true, 587 'default' => 'wibble', 588 ), 589 true, 590 'wibble', 591 ), 592 'single string key with multiple ask' => array( 593 array( 594 'single' => true, 595 'default' => 'wibble', 596 ), 597 false, 598 array( 'wibble' ), 599 ), 600 'multiple string key with single ask' => array( 601 array( 602 'single' => false, 603 'default' => 'wibble', 604 ), 605 true, 606 'wibble', 607 ), 608 'multiple string key with multiple ask' => array( 609 array( 610 'single' => false, 611 'default' => 'wibble', 612 ), 613 false, 614 array( 'wibble' ), 615 ), 616 'single array key with multiple ask' => array( 617 array( 618 'single' => true, 619 'type' => 'array', 620 'show_in_rest' => array( 621 'schema' => array( 622 'type' => 'array', 623 'items' => array( 624 'type' => 'string', 625 ), 626 ), 627 ), 628 'default' => array( 'wibble' ), 629 ), 630 false, 631 array( array( 'wibble' ) ), 632 ), 633 'single string key with single ask for sub type' => array( 634 array( 635 'single' => true, 636 'object_subtype' => 'page', 637 'default' => 'wibble', 638 ), 639 true, 640 'wibble', 641 ), 642 'single string key with multiple ask for sub type' => array( 643 array( 644 'single' => true, 645 'object_subtype' => 'page', 646 'default' => 'wibble', 647 ), 648 false, 649 array( 'wibble' ), 650 ), 651 'single array key with multiple ask for sub type' => array( 652 array( 653 'single' => true, 654 'object_subtype' => 'page', 655 'show_in_rest' => array( 656 'schema' => array( 657 'type' => 'array', 658 'items' => array( 659 'type' => 'string', 660 ), 661 ), 662 ), 663 'default' => array( 'wibble' ), 664 ), 665 false, 666 array( array( 'wibble' ) ), 667 ), 668 669 // types 670 'single object key with single ask' => array( 671 array( 672 'single' => true, 673 'show_in_rest' => array( 674 'schema' => array( 675 'type' => 'object', 676 'properties' => array( 677 'wibble' => array( 678 'type' => 'string', 679 ), 680 ), 681 ), 682 ), 683 'type' => 'object', 684 'default' => array( 'wibble' => 'dibble' ), 685 ), 686 true, 687 array( 'wibble' => 'dibble' ), 688 ), 689 'single object key with multiple ask' => array( 690 array( 691 'single' => true, 692 'show_in_rest' => array( 693 'schema' => array( 694 'type' => 'object', 695 'properties' => array( 696 'wibble' => array( 697 'type' => 'string', 698 ), 699 ), 700 ), 701 ), 702 'type' => 'object', 703 'default' => array( 'wibble' => 'dibble' ), 704 ), 705 false, 706 array( array( 'wibble' => 'dibble' ) ), 707 ), 708 'multiple object key with single ask' => array( 709 array( 710 'show_in_rest' => array( 711 'schema' => array( 712 'type' => 'object', 713 'properties' => array( 714 'wibble' => array( 715 'type' => 'string', 716 ), 717 ), 718 ), 719 ), 720 'type' => 'object', 721 'single' => false, 722 'default' => array( 'wibble' => 'dibble' ), 723 ), 724 true, 725 array( 'wibble' => 'dibble' ), 726 ), 727 'multiple object key with multiple ask' => array( 728 array( 729 'show_in_rest' => array( 730 'schema' => array( 731 'type' => 'object', 732 'properties' => array( 733 'wibble' => array( 734 'type' => 'string', 735 ), 736 ), 737 ), 738 ), 739 'type' => 'object', 740 'single' => false, 741 'default' => array( 'wibble' => 'dibble' ), 742 ), 743 false, 744 array( array( 'wibble' => 'dibble' ) ), 745 ), 746 'single array key with multiple ask part two' => array( 747 array( 748 'show_in_rest' => array( 749 'schema' => array( 750 'type' => 'array', 751 'items' => array( 752 'type' => 'string', 753 ), 754 ), 755 ), 756 'single' => true, 757 'type' => 'array', 758 'default' => array( 'dibble' ), 759 ), 760 false, 761 array( array( 'dibble' ) ), 762 ), 763 'multiple array with multiple ask' => array( 764 array( 765 'show_in_rest' => array( 766 'schema' => array( 767 'type' => 'array', 768 'items' => array( 769 'type' => 'string', 770 ), 771 ), 772 ), 773 'single' => false, 774 'type' => 'array', 775 'default' => array( 'dibble' ), 776 ), 777 false, 778 array( array( 'dibble' ) ), 779 ), 780 'single array with single ask' => array( 781 array( 782 'show_in_rest' => array( 783 'schema' => array( 784 'type' => 'array', 785 'items' => array( 786 'type' => 'string', 787 ), 788 ), 789 ), 790 'single' => true, 791 'type' => 'array', 792 'default' => array( 'dibble' ), 793 ), 794 true, 795 array( 'dibble' ), 796 ), 797 798 'multiple array with single ask' => array( 799 array( 800 'show_in_rest' => array( 801 'schema' => array( 802 'type' => 'array', 803 'items' => array( 804 'type' => 'string', 805 ), 806 ), 807 ), 808 'single' => false, 809 'type' => 'array', 810 'default' => array( 'dibble' ), 811 ), 812 true, 813 array( 'dibble' ), 814 ), 815 816 'single boolean with single ask' => array( 817 array( 818 'single' => true, 819 'type' => 'boolean', 820 'default' => true, 821 ), 822 true, 823 true, 824 ), 825 'multiple boolean with single ask' => array( 826 array( 827 'single' => false, 828 'type' => 'boolean', 829 'default' => true, 830 ), 831 true, 832 true, 833 ), 834 'single boolean with multiple ask' => array( 835 array( 836 'single' => true, 837 'type' => 'boolean', 838 'default' => true, 839 ), 840 false, 841 array( true ), 842 ), 843 'multiple boolean with multiple ask' => array( 844 array( 845 'single' => false, 846 'type' => 'boolean', 847 'default' => true, 848 ), 849 false, 850 array( true ), 851 ), 852 853 'single integer with single ask' => array( 854 array( 855 'single' => true, 856 'type' => 'integer', 857 'default' => 123, 858 ), 859 true, 860 123, 861 ), 862 'multiple integer with single ask' => array( 863 array( 864 'single' => false, 865 'type' => 'integer', 866 'default' => 123, 867 ), 868 true, 869 123, 870 ), 871 'single integer with multiple ask' => array( 872 array( 873 'single' => true, 874 'type' => 'integer', 875 'default' => 123, 876 ), 877 false, 878 array( 123 ), 879 ), 880 'multiple integer with multiple ask' => array( 881 array( 882 'single' => false, 883 'type' => 'integer', 884 'default' => 123, 885 ), 886 false, 887 array( 123 ), 888 ), 889 'single array of objects with multiple ask' => array( 890 array( 891 'type' => 'array', 892 'single' => true, 893 'show_in_rest' => array( 894 'schema' => array( 895 'type' => 'array', 896 'items' => array( 897 'type' => 'object', 898 'properties' => array( 899 'name' => array( 900 'type' => 'string', 901 ), 902 ), 903 ), 904 ), 905 ), 906 'default' => array( 907 array( 908 'name' => 'Kirk', 909 ), 910 ), 911 ), 912 false, 913 array( 914 array( 915 array( 916 'name' => 'Kirk', 917 ), 918 ), 919 ), 920 ), 921 ); 922 } 923 924 public function data_get_invalid_default_data() { 925 return array( 926 array( 927 array( 928 'single' => true, 929 'type' => 'boolean', 930 'default' => 123, 931 ), 932 true, 933 '', 934 ), 935 array( 936 array( 937 'single' => false, 938 'type' => 'boolean', 939 'default' => 123, 940 ), 941 true, 942 '', 943 ), 944 array( 945 array( 946 'single' => true, 947 'type' => 'boolean', 948 'default' => 123, 949 ), 950 false, 951 array(), 952 ), 953 array( 954 array( 955 'single' => false, 956 'type' => 'boolean', 957 'default' => 123, 958 ), 959 false, 960 array(), 961 ), 962 963 array( 964 array( 965 'single' => true, 966 'type' => 'integer', 967 'default' => 'wibble', 968 ), 969 true, 970 '', 971 ), 972 array( 973 array( 974 'single' => false, 975 'type' => 'integer', 976 'default' => 'wibble', 977 ), 978 true, 979 '', 980 ), 981 array( 982 array( 983 'single' => true, 984 'type' => 'integer', 985 'default' => 'wibble', 986 ), 987 false, 988 array(), 989 ), 990 array( 991 array( 992 'single' => false, 993 'type' => 'integer', 994 'default' => 'wibble', 995 ), 996 false, 997 array(), 998 ), 999 array( 1000 array( 1001 'single' => false, 1002 'type' => 'integer', 1003 'default' => array( 123, 'wibble' ), 1004 ), 1005 false, 1006 array(), 1007 ), 1008 array( 1009 array( 1010 'single' => false, 1011 'type' => 'integer', 1012 'default' => array( 123, array() ), 1013 ), 1014 false, 1015 array(), 1016 ), 1017 array( 1018 array( 1019 'single' => false, 1020 'type' => 'array', 1021 'show_in_rest' => array( 1022 'schema' => array( 1023 'type' => 'array', 1024 'items' => array( 1025 'type' => 'string', 1026 ), 1027 ), 1028 ), 1029 'default' => array( array( 123, 456 ), array( 'string' ) ), 1030 ), 1031 false, 1032 array(), 1033 ), 1034 array( 1035 array( 1036 'single' => true, 1037 'type' => 'array', 1038 'show_in_rest' => array( 1039 'schema' => array( 1040 'type' => 'array', 1041 'items' => array( 1042 'type' => 'string', 1043 ), 1044 ), 1045 ), 1046 'default' => array( array( 123, 456 ), array( 'string' ) ), 1047 ), 1048 true, 1049 '', 1050 ), 1051 array( 1052 array( 1053 'show_in_rest' => array( 1054 'schema' => array( 1055 'type' => 'object', 1056 'properties' => array( 1057 'my_prop' => array( 1058 'type' => 'string', 1059 ), 1060 'my_required_prop' => array( 1061 'type' => 'string', 1062 ), 1063 ), 1064 'required' => array( 'my_required_prop' ), 1065 ), 1066 ), 1067 'type' => 'object', 1068 'single' => true, 1069 'default' => array( 'my_prop' => 'hibble' ), 1070 ), 1071 true, 1072 '', 1073 ), 1074 ); 513 1075 } 514 1076 -
trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php
r48190 r48402 232 232 'type' => 'string', 233 233 'show_in_rest' => true, 234 ) 235 ); 236 237 register_post_meta( 238 'post', 239 'with_default', 240 array( 241 'type' => 'string', 242 'single' => true, 243 'show_in_rest' => true, 244 'default' => 'Goodnight Moon', 234 245 ) 235 246 ); … … 2787 2798 2788 2799 /** 2800 * @ticket 43941 2801 * @dataProvider data_get_default_data 2802 */ 2803 public function test_get_default_value( $args, $expected ) { 2804 $object_type = 'post'; 2805 $meta_key = 'registered_key1'; 2806 $registered = register_meta( 2807 $object_type, 2808 $meta_key, 2809 $args 2810 ); 2811 2812 $this->assertTrue( $registered ); 2813 2814 // Check for default value. 2815 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); 2816 $response = rest_get_server()->dispatch( $request ); 2817 2818 $this->assertEquals( 200, $response->get_status() ); 2819 2820 $data = $response->get_data(); 2821 $this->assertArrayHasKey( 'meta', $data ); 2822 2823 $meta = (array) $data['meta']; 2824 $this->assertArrayHasKey( $meta_key, $meta ); 2825 $this->assertEquals( $expected, $meta[ $meta_key ] ); 2826 } 2827 2828 public function data_get_default_data() { 2829 return array( 2830 array( 2831 array( 2832 'show_in_rest' => true, 2833 'single' => true, 2834 'default' => 'wibble', 2835 ), 2836 'wibble', 2837 ), 2838 array( 2839 array( 2840 'show_in_rest' => true, 2841 'single' => false, 2842 'default' => 'wibble', 2843 ), 2844 array( 'wibble' ), 2845 ), 2846 array( 2847 array( 2848 'show_in_rest' => true, 2849 'single' => true, 2850 'object_subtype' => 'post', 2851 'default' => 'wibble', 2852 ), 2853 'wibble', 2854 ), 2855 array( 2856 array( 2857 'show_in_rest' => true, 2858 'single' => false, 2859 'object_subtype' => 'post', 2860 'default' => 'wibble', 2861 ), 2862 array( 'wibble' ), 2863 ), 2864 array( 2865 array( 2866 'single' => true, 2867 'show_in_rest' => array( 2868 'schema' => array( 2869 'type' => 'object', 2870 'properties' => array( 2871 'wibble' => array( 2872 'type' => 'string', 2873 ), 2874 ), 2875 ), 2876 ), 2877 'type' => 'object', 2878 'default' => array( 'wibble' => 'dibble' ), 2879 ), 2880 array( 'wibble' => 'dibble' ), 2881 ), 2882 array( 2883 array( 2884 'show_in_rest' => array( 2885 'schema' => array( 2886 'type' => 'object', 2887 'properties' => array( 2888 'wibble' => array( 2889 'type' => 'string', 2890 ), 2891 ), 2892 ), 2893 ), 2894 'type' => 'object', 2895 'single' => false, 2896 'default' => array( 'wibble' => 'dibble' ), 2897 ), 2898 array( array( 'wibble' => 'dibble' ) ), 2899 ), 2900 2901 array( 2902 array( 2903 'show_in_rest' => array( 2904 'schema' => array( 2905 'type' => 'array', 2906 'items' => array( 2907 'type' => 'string', 2908 ), 2909 ), 2910 ), 2911 'single' => true, 2912 'type' => 'array', 2913 'default' => array( 'dibble' ), 2914 ), 2915 array( 'dibble' ), 2916 ), 2917 array( 2918 array( 2919 'show_in_rest' => array( 2920 'schema' => array( 2921 'type' => 'array', 2922 'items' => array( 2923 'type' => 'string', 2924 ), 2925 ), 2926 ), 2927 'single' => false, 2928 'type' => 'array', 2929 'default' => array( 'dibble' ), 2930 ), 2931 array( array( 'dibble' ) ), 2932 ), 2933 'array of objects' => array( 2934 array( 2935 'type' => 'array', 2936 'single' => true, 2937 'show_in_rest' => array( 2938 'schema' => array( 2939 'type' => 'array', 2940 'items' => array( 2941 'type' => 'object', 2942 'properties' => array( 2943 'name' => array( 2944 'type' => 'string', 2945 ), 2946 ), 2947 ), 2948 ), 2949 ), 2950 'default' => array( 2951 array( 2952 'name' => 'Kirk', 2953 ), 2954 ), 2955 ), 2956 array( 2957 array( 2958 'name' => 'Kirk', 2959 ), 2960 ), 2961 ), 2962 ); 2963 } 2964 2965 /** 2966 * @ticket 43941 2967 */ 2968 public function test_set_default_in_schema() { 2969 register_post_meta( 2970 'post', 2971 'greeting', 2972 array( 2973 'type' => 'string', 2974 'single' => true, 2975 'show_in_rest' => array( 2976 'schema' => array( 2977 'default' => 'Hello World', 2978 ), 2979 ), 2980 ) 2981 ); 2982 2983 $response = rest_do_request( '/wp/v2/posts/' . self::$post_id ); 2984 $this->assertEquals( 'Hello World', $response->get_data()['meta']['greeting'] ); 2985 } 2986 2987 /** 2988 * @ticket 43941 2989 */ 2990 public function test_default_is_added_to_schema() { 2991 $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ); 2992 $response = rest_do_request( $request ); 2993 2994 $schema = $response->get_data()['schema']['properties']['meta']['properties']['with_default']; 2995 $this->assertArrayHasKey( 'default', $schema ); 2996 $this->assertEquals( 'Goodnight Moon', $schema['default'] ); 2997 } 2998 2999 /** 2789 3000 * Internal function used to disable an insert query which 2790 3001 * will trigger a wpdb error for testing purposes. -
trunk/tests/phpunit/tests/rest-api/rest-term-meta-fields.php
r46586 r48402 1257 1257 1258 1258 /** 1259 * @ticket 43941 1260 */ 1261 public function test_get_default_value() { 1262 $meta_key = 'registered_key1'; 1263 register_term_meta( 1264 'category', 1265 $meta_key, 1266 array( 1267 'single' => true, 1268 'type' => 'string', 1269 'default' => 'Goodbye', 1270 'show_in_rest' => true, 1271 ) 1272 ); 1273 1274 // Check for default value. 1275 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/categories/%d', self::$category_id ) ); 1276 $response = rest_get_server()->dispatch( $request ); 1277 1278 $this->assertEquals( 200, $response->get_status() ); 1279 1280 $data = $response->get_data(); 1281 $this->assertArrayHasKey( 'meta', $data ); 1282 1283 $meta = (array) $data['meta']; 1284 $this->assertArrayHasKey( $meta_key, $meta ); 1285 $this->assertSame( 'Goodbye', $meta[ $meta_key ] ); 1286 } 1287 1288 /** 1259 1289 * Internal function used to disable an insert query which 1260 1290 * will trigger a wpdb error for testing purposes. -
trunk/tests/phpunit/tests/rest-api/rest-users-controller.php
r48196 r48402 2891 2891 } 2892 2892 2893 /** 2894 * @ticket 43941 2895 * @dataProvider data_get_default_data 2896 */ 2897 public function test_get_default_value( $args, $expected ) { 2898 wp_set_current_user( self::$user ); 2899 2900 $object_type = 'user'; 2901 $meta_key = 'registered_key1'; 2902 register_meta( 2903 $object_type, 2904 $meta_key, 2905 $args 2906 ); 2907 2908 // Check for default value. 2909 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/users/%d', self::$user ) ); 2910 $response = rest_get_server()->dispatch( $request ); 2911 2912 $this->assertEquals( 200, $response->get_status() ); 2913 2914 $data = $response->get_data(); 2915 $this->assertArrayHasKey( 'meta', $data ); 2916 2917 $meta = (array) $data['meta']; 2918 $this->assertArrayHasKey( $meta_key, $meta ); 2919 $this->assertEquals( $expected, $meta[ $meta_key ] ); 2920 } 2921 2922 public function data_get_default_data() { 2923 return array( 2924 array( 2925 array( 2926 'show_in_rest' => true, 2927 'single' => true, 2928 'default' => 'wibble', 2929 ), 2930 'wibble', 2931 ), 2932 array( 2933 array( 2934 'show_in_rest' => true, 2935 'single' => false, 2936 'default' => 'wibble', 2937 ), 2938 array( 'wibble' ), 2939 ), 2940 array( 2941 array( 2942 'single' => true, 2943 'show_in_rest' => array( 2944 'schema' => array( 2945 'type' => 'object', 2946 'properties' => array( 2947 'wibble' => array( 2948 'type' => 'string', 2949 ), 2950 ), 2951 ), 2952 ), 2953 'type' => 'object', 2954 'default' => array( 'wibble' => 'dibble' ), 2955 ), 2956 array( 'wibble' => 'dibble' ), 2957 ), 2958 array( 2959 array( 2960 'show_in_rest' => array( 2961 'schema' => array( 2962 'type' => 'object', 2963 'properties' => array( 2964 'wibble' => array( 2965 'type' => 'string', 2966 ), 2967 ), 2968 ), 2969 ), 2970 'type' => 'object', 2971 'single' => false, 2972 'default' => array( 'wibble' => 'dibble' ), 2973 ), 2974 array( array( 'wibble' => 'dibble' ) ), 2975 ), 2976 2977 array( 2978 array( 2979 'show_in_rest' => array( 2980 'schema' => array( 2981 'type' => 'array', 2982 'items' => array( 2983 'type' => 'string', 2984 ), 2985 ), 2986 ), 2987 'single' => true, 2988 'type' => 'array', 2989 'default' => array( 'dibble' ), 2990 ), 2991 array( 'dibble' ), 2992 ), 2993 array( 2994 array( 2995 'show_in_rest' => array( 2996 'schema' => array( 2997 'type' => 'array', 2998 'items' => array( 2999 'type' => 'string', 3000 ), 3001 ), 3002 ), 3003 'single' => false, 3004 'type' => 'array', 3005 'default' => array( 'dibble' ), 3006 ), 3007 array( array( 'dibble' ) ), 3008 ), 3009 ); 3010 } 3011 2893 3012 public function additional_field_get_callback( $object ) { 2894 3013 return get_user_meta( $object['id'], 'my_custom_int', true );
Note: See TracChangeset
for help on using the changeset viewer.