Ticket #43941: 43941.9.diff
File 43941.9.diff, 8.9 KB (added by , 5 years ago) |
---|
-
src/wp-includes/meta.php
206 206 207 207 // Compare existing value to new value if no prev value given and the key exists only once. 208 208 if ( empty( $prev_value ) ) { 209 remove_filter( "default_{$meta_type}_metadata", 'filter_default_metadata', 10, 5 ); 209 210 $old_value = get_metadata( $meta_type, $object_id, $meta_key ); 211 add_filter( "default_{$meta_type}_metadata", 'filter_default_metadata', 10, 5 ); 210 212 if ( count( $old_value ) == 1 ) { 211 213 if ( $old_value[0] === $meta_value ) { 212 214 return false; … … 542 544 } 543 545 } 544 546 547 return get_metadata_default( $meta_type, $meta_key, $single, $object_id ); 548 } 549 550 /** 551 * Retrieve metadata data default for the specified object. 552 * 553 * @since 5.4.0 554 * 555 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). 556 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 557 * the specified object. 558 * @param bool $single Optional, default is false. 559 * If true, return only the first value of the specified meta_key. 560 * This parameter has no effect if meta_key is not specified. 561 * @param int $object_id Optional, default is 0. 562 * ID of the object metadata is for 563 * @return mixed Single metadata value, or array of values 564 */ 565 function get_metadata_default( $meta_type, $meta_key, $single = false, $object_id = 0 ) { 545 566 if ( $single ) { 546 return'';567 $value = ''; 547 568 } else { 548 returnarray();569 $value = array(); 549 570 } 571 572 /** 573 * Filter the default value a specified object. 574 * 575 * @since 5.4.0 576 * 577 * @param array|string $value The value should return - a single metadata value, 578 * or an array of values. 579 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). 580 * @param string $meta_key Meta key. 581 * @param bool $single Whether to return only the first value of the specified $meta_key. 582 * @param int $object_id Object ID. 583 */ 584 $value = apply_filters( "default_{$meta_type}_metadata", $value, $meta_type, $meta_key, $single, $object_id ); 585 586 return $value; 550 587 } 551 588 552 589 /** … … 1229 1266 } 1230 1267 } 1231 1268 1269 if ( false === $args['single'] && ! wp_is_numeric_array( $args['default'] ) ) { 1270 unset( $args['default'] ); 1271 } 1272 1273 if ( array_key_exists( 'default', $args ) ) { 1274 add_filter( "default_{$object_type}_metadata", 'filter_default_metadata', 10, 5 ); 1275 } 1276 1232 1277 // Global registry only contains meta keys registered with the array of arguments added in 4.6.0. 1233 1278 if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) { 1234 1279 unset( $args['object_subtype'] ); … … 1457 1502 */ 1458 1503 return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id ); 1459 1504 } 1505 1506 /** 1507 * Filter into default_{$object_type}_metadata and add in default value. 1508 * 1509 * @since 5.4.0 1510 * 1511 * @param mixed $value Current value passed to filter. 1512 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). 1513 1514 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 1515 * the specified object. 1516 * @param bool $single Optional, default is false. 1517 * If true, return only the first value of the specified meta_key. 1518 * This parameter has no effect if meta_key is not specified. 1519 * @param int $object_id ID of the object metadata is for 1520 * 1521 * @return mixed Single metadata default, or array of defaults 1522 */ 1523 function filter_default_metadata( $value, $meta_type, $meta_key, $single, $object_id ) { 1524 if ( wp_installing() ) { 1525 return $value; 1526 } 1527 1528 $metadata = get_registered_meta_keys( $meta_type ); 1529 if ( ! isset( $metadata[ $meta_key ] ) ) { 1530 $sub_type = get_object_subtype( $meta_type, $object_id ); 1531 $metadata = get_registered_meta_keys( $meta_type, $sub_type ); 1532 if ( ! isset( $metadata[ $meta_key ] ) ) { 1533 return $value; 1534 } 1535 } 1536 1537 if ( $single ) { 1538 if ( $metadata[ $meta_key ]['single'] ) { 1539 $value = $metadata[ $meta_key ]['default']; 1540 } else { 1541 $value = $metadata[ $meta_key ]['default'][0]; 1542 } 1543 } else { 1544 $value = $metadata[ $meta_key ]['default']; 1545 } 1546 1547 return $value; 1548 } -
tests/phpunit/tests/meta/registerMeta.php
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 524 // Check for default value. 525 $value = get_metadata( $object_type, $object_id, $meta_key, $single ); 526 $this->assertSame( $value, $expected ); 527 528 // Set value to check default is not being returned by mistake. 529 $meta_value = 'dibble'; 530 update_metadata( $object_type, $object_id, $meta_key, $meta_value ); 531 $value = get_metadata( $object_type, $object_id, $meta_key, true ); 532 $this->assertSame( $value, $meta_value ); 533 534 // Delete meta, make sure the default is returned. 535 delete_metadata( $object_type, $object_id, $meta_key ); 536 $value = get_metadata( $object_type, $object_id, $meta_key, $single ); 537 $this->assertSame( $value, $expected ); 538 539 // Set other meta key, to make sure other keys are not effects. 540 $meta_value = 'hibble'; 541 $meta_key = 'unregistered_key1'; 542 $value = get_metadata( $object_type, $object_id, $meta_key, true ); 543 $this->assertSame( $value, '' ); 544 update_metadata( $object_type, $object_id, $meta_key, $meta_value ); 545 $value = get_metadata( $object_type, $object_id, $meta_key, true ); 546 $this->assertSame( $value, $meta_value ); 547 548 } 549 507 550 public function filter_get_object_subtype_for_customtype( $subtype, $object_id ) { 508 551 if ( 1 === ( $object_id % 2 ) ) { 509 552 return 'odd'; … … 512 555 return 'even'; 513 556 } 514 557 558 public function data_get_default_data() { 559 return array( 560 array( 561 array( 562 'single' => true, 563 'default' => 'wibble', 564 ), 565 true, 566 'wibble', 567 ), 568 array( 569 array( 570 'single' => true, 571 'default' => 'wibble', 572 ), 573 false, 574 array( 'wibble' ), 575 ), 576 array( 577 array( 578 'single' => true, 579 'default' => array( 'wibble' ), 580 ), 581 true, 582 array( 'wibble' ), 583 ), 584 array( 585 array( 586 'single' => true, 587 'default' => array( 'wibble' ), 588 ), 589 false, 590 array( array( 'wibble' ) ), 591 ), 592 array( 593 array( 594 'single' => false, 595 'default' => 'wibble', 596 ), 597 true, 598 '', 599 ), 600 array( 601 array( 602 'single' => false, 603 'default' => 'wibble', 604 ), 605 false, 606 array(), 607 ), 608 array( 609 array( 610 'single' => false, 611 'default' => array( 'wibble' ), 612 ), 613 true, 614 'wibble', 615 ), 616 array( 617 array( 618 'single' => false, 619 'default' => array( 'wibble' ), 620 ), 621 false, 622 array( 'wibble' ), 623 ), 624 array( 625 array( 626 'single' => true, 627 'object_subtype' => 'page', 628 'default' => 'wibble', 629 ), 630 true, 631 'wibble', 632 ), 633 array( 634 array( 635 'single' => true, 636 'object_subtype' => 'page', 637 'default' => 'wibble', 638 ), 639 false, 640 array( 'wibble' ), 641 ), 642 array( 643 array( 644 'single' => true, 645 'object_subtype' => 'page', 646 'default' => array( 'wibble' ), 647 ), 648 true, 649 array( 'wibble' ), 650 ), 651 array( 652 array( 653 'single' => true, 654 'object_subtype' => 'page', 655 'default' => array( 'wibble' ), 656 ), 657 false, 658 array( array( 'wibble' ) ), 659 ), 660 array( 661 array( 662 'single' => true, 663 'object_subtype' => 'post', 664 'default' => 'wibble', 665 ), 666 true, 667 '', 668 ), 669 array( 670 array( 671 'single' => true, 672 'object_subtype' => 'post', 673 'default' => 'wibble', 674 ), 675 false, 676 array(), 677 ), 678 array( 679 array( 680 'single' => true, 681 'object_subtype' => 'post', 682 'default' => array( 'wibble' ), 683 ), 684 true, 685 '', 686 ), 687 array( 688 array( 689 'single' => true, 690 'object_subtype' => 'post', 691 'default' => array( 'wibble' ), 692 ), 693 false, 694 array(), 695 ), 696 ); 697 } 698 515 699 public function data_get_types_and_subtypes() { 516 700 return array( 517 701 array( 'post', 'page' ),