Ticket #43941: 43941.15.diff
File 43941.15.diff, 13.6 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 );209 if ( count( $old_value ) == 1 ) {208 $old_value = get_metadata_raw( $meta_type, $object_id, $meta_key ); 209 if ( is_countable( $old_value ) && count( $old_value ) == 1 ) { 210 210 if ( $old_value[0] === $meta_value ) { 211 211 return false; 212 212 } … … 473 473 } 474 474 475 475 /** 476 * Retrieves metadata for the specified object.476 * Retrieves raw metadata for the specified object. 477 477 * 478 * @since 2.9.0478 * @since 5.5.0 479 479 * 480 480 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 481 481 * or any other object type with an associated meta table. … … 486 486 * This parameter has no effect if meta_key is not specified. Default false. 487 487 * @return mixed Single metadata value, or array of values 488 488 */ 489 function get_metadata ( $meta_type, $object_id, $meta_key = '', $single = false ) {489 function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) { 490 490 if ( ! $meta_type || ! is_numeric( $object_id ) ) { 491 491 return false; 492 492 } … … 543 543 } 544 544 } 545 545 546 return null; 547 } 548 549 /** 550 * Retrieves raw metadata for the specified object. 551 * 552 * @since 2.9.0 553 * @uses get_metadata_raw() 554 * 555 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 556 * or any other object type with an associated meta table. 557 * @param int $object_id ID of the object metadata is for. 558 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 559 * the specified object. Default empty. 560 * @param bool $single Optional. If true, return only the first value of the specified meta_key. 561 * This parameter has no effect if meta_key is not specified. Default false. 562 * @return mixed Single metadata value, or array of values 563 */ 564 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) { 565 $value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single ); 566 if ( ! is_null( $value ) ) { 567 return $value; 568 } 569 570 return get_metadata_default( $meta_type, $meta_key, $single, $object_id ); 571 } 572 573 /** 574 * Retrieve metadata data default for the specified object. 575 * 576 * @since 5.5.0 577 * 578 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). 579 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 580 * the specified object. 581 * @param bool $single Optional, default is false. 582 * If true, return only the first value of the specified meta_key. 583 * This parameter has no effect if meta_key is not specified. 584 * @param int $object_id Optional, default is 0. 585 * ID of the object metadata is for 586 * @return mixed Single metadata value, or array of values 587 */ 588 function get_metadata_default( $meta_type, $meta_key, $single = false, $object_id = 0 ) { 546 589 if ( $single ) { 547 return'';590 $value = ''; 548 591 } else { 549 returnarray();592 $value = array(); 550 593 } 594 595 /** 596 * Filter the default value a specified object. 597 * 598 * @since 5.5.0 599 * 600 * @param array|string $value The value should return - a single metadata value, 601 * or an array of values. 602 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). 603 * @param string $meta_key Meta key. 604 * @param bool $single Whether to return only the first value of the specified $meta_key. 605 * @param int $object_id Object ID. 606 */ 607 $value = apply_filters( "default_{$meta_type}_metadata", $value, $meta_type, $meta_key, $single, $object_id ); 608 609 if ( ! $single && ! wp_is_numeric_array( $value ) ) { 610 $value = array( $value ); 611 } 612 613 return $value; 551 614 } 552 615 553 616 /** … … 1139 1202 * @type string $type The type of data associated with this meta key. 1140 1203 * Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'. 1141 1204 * @type string $description A description of the data attached to this meta key. 1205 * @type mixed $default Default value when calling `get_metadata()`. 1142 1206 * @type bool $single Whether the meta key has one value per object, or an array of values per object. 1143 1207 * @type string $sanitize_callback A function or method to call when sanitizing `$meta_key` data. 1144 1208 * @type string $auth_callback Optional. A function or method to call when performing edit_post_meta, … … 1165 1229 'object_subtype' => '', 1166 1230 'type' => 'string', 1167 1231 'description' => '', 1232 'default' => '', 1168 1233 'single' => false, 1169 1234 'sanitize_callback' => null, 1170 1235 'auth_callback' => null, … … 1202 1267 * @param string $meta_key Meta key. 1203 1268 */ 1204 1269 $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); 1270 unset( $defaults['default'] ); 1205 1271 $args = wp_parse_args( $args, $defaults ); 1206 1272 1207 1273 // Require an item schema when registering array meta. … … 1241 1307 } 1242 1308 } 1243 1309 1310 if ( array_key_exists( 'default', $args ) ) { 1311 if ( false === $args['single'] && ! wp_is_numeric_array( $args['default'] ) ) { 1312 $args['default'] = array( $args['default'] ); 1313 } 1314 if ( ! has_filter( "default_{$object_type}_metadata", 'filter_default_metadata' ) ) { 1315 add_filter( "default_{$object_type}_metadata", 'filter_default_metadata', 10, 5 ); 1316 } 1317 } 1318 1244 1319 // Global registry only contains meta keys registered with the array of arguments added in 4.6.0. 1245 1320 if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) { 1246 1321 unset( $args['object_subtype'] ); … … 1473 1548 */ 1474 1549 return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id ); 1475 1550 } 1551 1552 /** 1553 * Filter into default_{$object_type}_metadata and add in default value. 1554 * 1555 * @since 5.5.0 1556 * 1557 * @param mixed $value Current value passed to filter. 1558 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). 1559 1560 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 1561 * the specified object. 1562 * @param bool $single Optional, default is false. 1563 * If true, return only the first value of the specified meta_key. 1564 * This parameter has no effect if meta_key is not specified. 1565 * @param int $object_id ID of the object metadata is for 1566 * 1567 * @return mixed Single metadata default, or array of defaults 1568 */ 1569 function filter_default_metadata( $value, $meta_type, $meta_key, $single, $object_id ) { 1570 global $wp_meta_keys; 1571 1572 if ( wp_installing() ) { 1573 return $value; 1574 } 1575 1576 if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $meta_type ] ) ) { 1577 return $value; 1578 } 1579 1580 $defaults = array(); 1581 foreach ( $wp_meta_keys[ $meta_type ] as $sub_type => $meta_data ) { 1582 foreach ( $meta_data as $_meta_key => $args ) { 1583 if ( $_meta_key === $meta_key && isset( $args['default'] ) ) { 1584 $defaults[ $sub_type ] = $args; 1585 } 1586 } 1587 } 1588 1589 if ( ! $defaults ) { 1590 return $value; 1591 } 1592 1593 if ( isset( $defaults[''] ) ) { 1594 $metadata = $defaults['']; 1595 } else { 1596 $sub_type = get_object_subtype( $meta_type, $object_id ); 1597 if ( ! isset( $defaults[ $sub_type ] ) ) { 1598 return $value; 1599 } 1600 $metadata = $defaults[ $sub_type ]; 1601 } 1602 1603 if ( $single ) { 1604 if ( $metadata['single'] ) { 1605 $value = $metadata['default']; 1606 } else { 1607 $value = $metadata['default'][0]; 1608 } 1609 } else { 1610 $value = $metadata['default']; 1611 } 1612 1613 return $value; 1614 } -
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' ),