Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trac/46660 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/wp-includes/meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,14 @@ function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_
// Compare existing value to new value if no prev value given and the key exists only once.
if ( empty( $prev_value ) ) {
$old_value = get_metadata( $meta_type, $object_id, $meta_key );
if ( count( $old_value ) == 1 ) {
if ( is_array( $old_value ) && count( $old_value ) == 1 ) {
if ( $old_value[0] === $meta_value ) {
return false;
}
} else if ( ! is_array( $old_value ) ){
if ( $old_value === $meta_value ) {
return false;
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/phpunit/tests/meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,20 @@ public function test_get_metadata_with_empty_key_nested_array_value() {
$this->assertSame( array( $value ), $found['foo'] );
}

/**
* @ticket 46660
*/
public function test_update_metadata_for_nonexistant_meta() {
$data = array(
array( 1, 2 ),
array( 3, 4 ),
);
update_metadata( 'user', $this->author->ID, 'non_existant_foo', $data );

$found = get_metadata( 'user', $this->author->ID, 'non_existant_foo' );
$this->assertSame( $found, $data );
}

/** Helpers */

public function updated_meta( $meta_id ) {
Expand Down