Ticket #43941: 43941.10.diff
File 43941.10.diff, 11.7 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 * … … 1165 1214 'object_subtype' => '', 1166 1215 'type' => 'string', 1167 1216 'description' => '', 1217 'default' => '', 1168 1218 'single' => false, 1169 1219 'sanitize_callback' => null, 1170 1220 'auth_callback' => null, … … 1202 1252 * @param string $meta_key Meta key. 1203 1253 */ 1204 1254 $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); 1255 unset( $defaults['default'] ); 1205 1256 $args = wp_parse_args( $args, $defaults ); 1206 1257 1207 1258 // Require an item schema when registering array meta. … … 1241 1292 } 1242 1293 } 1243 1294 1295 if ( array_key_exists( 'default', $args ) ) { 1296 if ( false === $args['single'] && ! wp_is_numeric_array( $args['default'] ) ) { 1297 $args['default'] = array( $args['default'] ); 1298 } 1299 add_filter( "default_{$object_type}_metadata", 'filter_default_metadata', 10, 5 ); 1300 } 1301 1244 1302 // Global registry only contains meta keys registered with the array of arguments added in 4.6.0. 1245 1303 if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) { 1246 1304 unset( $args['object_subtype'] ); … … 1473 1531 */ 1474 1532 return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id ); 1475 1533 } 1534 1535 /** 1536 * Filter into default_{$object_type}_metadata and add in default value. 1537 * 1538 * @since 5.5.0 1539 * 1540 * @param mixed $value Current value passed to filter. 1541 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). 1542 1543 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 1544 * the specified object. 1545 * @param bool $single Optional, default is false. 1546 * If true, return only the first value of the specified meta_key. 1547 * This parameter has no effect if meta_key is not specified. 1548 * @param int $object_id ID of the object metadata is for 1549 * 1550 * @return mixed Single metadata default, or array of defaults 1551 */ 1552 function filter_default_metadata( $value, $meta_type, $meta_key, $single, $object_id ) { 1553 if ( wp_installing() ) { 1554 return $value; 1555 } 1556 1557 $metadata = get_registered_meta_keys( $meta_type ); 1558 1559 if ( ! isset( $metadata[ $meta_key ] ) ) { 1560 $sub_type = get_object_subtype( $meta_type, $object_id ); 1561 $metadata = get_registered_meta_keys( $meta_type, $sub_type ); 1562 if ( ! isset( $metadata[ $meta_key ] ) ) { 1563 return $value; 1564 } 1565 } 1566 1567 if ( $single ) { 1568 if ( $metadata[ $meta_key ]['single'] ) { 1569 $value = $metadata[ $meta_key ]['default']; 1570 } else { 1571 $value = $metadata[ $meta_key ]['default'][0]; 1572 } 1573 } else { 1574 $value = $metadata[ $meta_key ]['default']; 1575 } 1576 1577 return $value; 1578 } -
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' ),