Ticket #43941: 43941.11.diff
File 43941.11.diff, 12.5 KB (added by , 5 years ago) |
---|
-
src/wp-includes/meta.php
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8
205 205 206 206 // Compare existing value to new value if no prev value given and the key exists only once. 207 207 if ( empty( $prev_value ) ) { 208 $old_value = get_metadata( $meta_type, $object_id, $meta_key );208 $old_value = get_metadata( $meta_type, $object_id, $meta_key, false, true ); 209 209 if ( count( $old_value ) == 1 ) { 210 210 if ( $old_value[0] === $meta_value ) { 211 211 return false; … … 476 476 * Retrieves metadata for the specified object. 477 477 * 478 478 * @since 2.9.0 479 * @since 5.5.0 Added the `$unfiltered` parameter. 479 480 * 480 481 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 481 482 * or any other object type with an associated meta table. … … 484 485 * the specified object. Default empty. 485 486 * @param bool $single Optional. If true, return only the first value of the specified meta_key. 486 487 * This parameter has no effect if meta_key is not specified. Default false. 488 * @param bool $unfiltered Optional. Whether to apply filters. Default false. 487 489 * @return mixed Single metadata value, or array of values 488 490 */ 489 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {491 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false, $unfiltered = false ) { 490 492 if ( ! $meta_type || ! is_numeric( $object_id ) ) { 491 493 return false; 492 494 } … … 543 545 } 544 546 } 545 547 546 if ( $single ) { 547 return ''; 548 } else { 549 return array(); 550 } 551 } 548 if ( $unfiltered ) { 549 if ( $single ) { 550 return ''; 551 } else { 552 return array(); 553 } 554 } 552 555 556 return get_metadata_default( $meta_type, $meta_key, $single, $object_id ); 557 } 558 559 /** 560 * Retrieve metadata data default for the specified object. 561 * 562 * @since 5.5.0 563 * 564 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). 565 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 566 * the specified object. 567 * @param bool $single Optional, default is false. 568 * If true, return only the first value of the specified meta_key. 569 * This parameter has no effect if meta_key is not specified. 570 * @param int $object_id Optional, default is 0. 571 * ID of the object metadata is for 572 * @return mixed Single metadata value, or array of values 573 */ 574 function get_metadata_default( $meta_type, $meta_key, $single = false, $object_id = 0 ) { 575 if ( $single ) { 576 $value = ''; 577 } else { 578 $value = array(); 579 } 580 581 /** 582 * Filter the default value a specified object. 583 * 584 * @since 5.5.0 585 * 586 * @param array|string $value The value should return - a single metadata value, 587 * or an array of values. 588 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). 589 * @param string $meta_key Meta key. 590 * @param bool $single Whether to return only the first value of the specified $meta_key. 591 * @param int $object_id Object ID. 592 */ 593 $value = apply_filters( "default_{$meta_type}_metadata", $value, $meta_type, $meta_key, $single, $object_id ); 594 595 if ( ! $single && ! wp_is_numeric_array( $value ) ) { 596 $value = array( $value ); 597 } 598 599 return $value; 600 } 601 553 602 /** 554 603 * Determines if a meta key is set for a given object. 555 604 * … … 1139 1188 * @type string $type The type of data associated with this meta key. 1140 1189 * Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'. 1141 1190 * @type string $description A description of the data attached to this meta key. 1191 * @type mixed $default Default value when calling `get_metadata()`. 1142 1192 * @type bool $single Whether the meta key has one value per object, or an array of values per object. 1143 1193 * @type string $sanitize_callback A function or method to call when sanitizing `$meta_key` data. 1144 1194 * @type string $auth_callback Optional. A function or method to call when performing edit_post_meta, … … 1165 1215 'object_subtype' => '', 1166 1216 'type' => 'string', 1167 1217 'description' => '', 1218 'default' => '', 1168 1219 'single' => false, 1169 1220 'sanitize_callback' => null, 1170 1221 'auth_callback' => null, … … 1202 1253 * @param string $meta_key Meta key. 1203 1254 */ 1204 1255 $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); 1256 unset( $defaults['default'] ); 1205 1257 $args = wp_parse_args( $args, $defaults ); 1206 1258 1207 1259 // Require an item schema when registering array meta. … … 1241 1293 } 1242 1294 } 1243 1295 1296 if ( array_key_exists( 'default', $args ) ) { 1297 if ( false === $args['single'] && ! wp_is_numeric_array( $args['default'] ) ) { 1298 $args['default'] = array( $args['default'] ); 1299 } 1300 add_filter( "default_{$object_type}_metadata", 'filter_default_metadata', 10, 5 ); 1301 } 1302 1244 1303 // Global registry only contains meta keys registered with the array of arguments added in 4.6.0. 1245 1304 if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) { 1246 1305 unset( $args['object_subtype'] ); … … 1473 1532 */ 1474 1533 return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id ); 1475 1534 } 1535 1536 /** 1537 * Filter into default_{$object_type}_metadata and add in default value. 1538 * 1539 * @since 5.5.0 1540 * 1541 * @param mixed $value Current value passed to filter. 1542 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). 1543 1544 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 1545 * the specified object. 1546 * @param bool $single Optional, default is false. 1547 * If true, return only the first value of the specified meta_key. 1548 * This parameter has no effect if meta_key is not specified. 1549 * @param int $object_id ID of the object metadata is for 1550 * 1551 * @return mixed Single metadata default, or array of defaults 1552 */ 1553 function filter_default_metadata( $value, $meta_type, $meta_key, $single, $object_id ) { 1554 if ( wp_installing() ) { 1555 return $value; 1556 } 1557 1558 $metadata = get_registered_meta_keys( $meta_type ); 1559 1560 if ( ! isset( $metadata[ $meta_key ] ) ) { 1561 $sub_type = get_object_subtype( $meta_type, $object_id ); 1562 $metadata = get_registered_meta_keys( $meta_type, $sub_type ); 1563 if ( ! isset( $metadata[ $meta_key ] ) ) { 1564 return $value; 1565 } 1566 } 1567 1568 if ( $single ) { 1569 if ( $metadata[ $meta_key ]['single'] ) { 1570 $value = $metadata[ $meta_key ]['default']; 1571 } else { 1572 $value = $metadata[ $meta_key ]['default'][0]; 1573 } 1574 } else { 1575 $value = $metadata[ $meta_key ]['default']; 1576 } 1577 1578 return $value; 1579 } -
tests/phpunit/tests/meta/registerMeta.php
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8
504 504 $this->assertSame( 'even', $subtype_for_4 ); 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 507 552 public function filter_get_object_subtype_for_customtype( $subtype, $object_id ) { 508 553 if ( 1 === ( $object_id % 2 ) ) { 509 554 return 'odd'; … … 512 557 return 'even'; 513 558 } 514 559 560 public function data_get_default_data() { 561 return array( 562 array( 563 array( 564 'single' => true, 565 'default' => 'wibble', 566 ), 567 true, 568 'wibble', 569 ), 570 array( 571 array( 572 'single' => true, 573 'default' => 'wibble', 574 ), 575 false, 576 array( 'wibble' ), 577 ), 578 array( 579 array( 580 'single' => true, 581 'default' => array( 'wibble' ), 582 ), 583 true, 584 array( 'wibble' ), 585 ), 586 array( 587 array( 588 'single' => true, 589 'default' => array( 'wibble' ), 590 ), 591 false, 592 array( 'wibble' ), 593 ), 594 array( 595 array( 596 'single' => false, 597 'default' => 'wibble', 598 ), 599 true, 600 'wibble', 601 ), 602 array( 603 array( 604 'single' => false, 605 'default' => 'wibble', 606 ), 607 false, 608 array( 'wibble' ), 609 ), 610 array( 611 array( 612 'single' => false, 613 'default' => array( 'wibble' ), 614 ), 615 true, 616 'wibble', 617 ), 618 array( 619 array( 620 'single' => false, 621 'default' => array( 'wibble' ), 622 ), 623 false, 624 array( 'wibble' ), 625 ), 626 array( 627 array( 628 'single' => true, 629 'object_subtype' => 'page', 630 'default' => 'wibble', 631 ), 632 true, 633 'wibble', 634 ), 635 array( 636 array( 637 'single' => true, 638 'object_subtype' => 'page', 639 'default' => 'wibble', 640 ), 641 false, 642 array( 'wibble' ), 643 ), 644 array( 645 array( 646 'single' => true, 647 'object_subtype' => 'page', 648 'default' => array( 'wibble' ), 649 ), 650 true, 651 array( 'wibble' ), 652 ), 653 array( 654 array( 655 'single' => true, 656 'object_subtype' => 'page', 657 'default' => array( 'wibble' ), 658 ), 659 false, 660 array( 'wibble' ), 661 ), 662 array( 663 array( 664 'single' => true, 665 'object_subtype' => 'post', 666 'default' => 'wibble', 667 ), 668 true, 669 '', 670 ), 671 array( 672 array( 673 'single' => true, 674 'object_subtype' => 'post', 675 'default' => 'wibble', 676 ), 677 false, 678 array(), 679 ), 680 array( 681 array( 682 'single' => true, 683 'object_subtype' => 'post', 684 'default' => array( 'wibble' ), 685 ), 686 true, 687 '', 688 ), 689 array( 690 array( 691 'single' => true, 692 'object_subtype' => 'post', 693 'default' => array( 'wibble' ), 694 ), 695 false, 696 array(), 697 ), 698 array( 699 array( 700 'single' => true, 701 'default' => array( 'wibble' => 'dibble' ), 702 ), 703 true, 704 array( 'wibble' => 'dibble' ), 705 ), 706 array( 707 array( 708 'single' => true, 709 'default' => array( 'wibble' => 'dibble' ), 710 ), 711 false, 712 array( 713 array( 'wibble' => 'dibble' ), 714 ), 715 ), 716 array( 717 array( 718 'single' => false, 719 'default' => array( 'wibble' => 'dibble' ), 720 ), 721 true, 722 array( 'wibble' => 'dibble' ), 723 ), 724 array( 725 array( 726 'single' => false, 727 'default' => array( 'wibble' => 'dibble' ), 728 ), 729 false, 730 array( 731 array( 'wibble' => 'dibble' ), 732 ), 733 ), 734 ); 735 } 736 515 737 public function data_get_types_and_subtypes() { 516 738 return array( 517 739 array( 'post', 'page' ),