From 9a84da90c3e55a3ba33780e5bac5b28cb3f2cddc Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 30 Oct 2014 10:24:22 +0000 Subject: [PATCH] Unserialize `get_metadata()` results when 'key' is omitted. Props mattkeys, nacin. Fixes #15030. git-svn-id: https://develop.svn.wordpress.org/trunk@30115 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/meta.php | 7 ++++++- tests/phpunit/tests/meta.php | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/meta.php b/src/wp-includes/meta.php index 1091073a54..f7d875f436 100644 --- a/src/wp-includes/meta.php +++ b/src/wp-includes/meta.php @@ -478,8 +478,13 @@ function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) { $meta_cache = $meta_cache[$object_id]; } - if ( !$meta_key ) + if ( ! $meta_key ) { + foreach ( $meta_cache as &$meta_values ) { + $meta_values = array_map( 'maybe_unserialize', $meta_values ); + } + return $meta_cache; + } if ( isset($meta_cache[$meta_key]) ) { if ( $single ) diff --git a/tests/phpunit/tests/meta.php b/tests/phpunit/tests/meta.php index abf3ba527b..7ad5fb8e90 100644 --- a/tests/phpunit/tests/meta.php +++ b/tests/phpunit/tests/meta.php @@ -263,4 +263,41 @@ class Tests_Meta extends WP_UnitTestCase { $this->assertFalse( update_metadata_by_mid( 'user', array( 1 ), 'meta_new_value' ) ); $this->assertFalse( delete_metadata_by_mid( 'user', array( 1 ) ) ); } + + /** + * @ticket 15030 + */ + public function test_get_metadata_with_empty_key_array_value_should_be_unserialized() { + $data = array( 1, 2 ); + add_metadata( 'user', $this->author->ID, 'foo', $data ); + $found = get_metadata( 'user', $this->author->ID ); + + $this->assertSame( array( $data ), $found['foo'] ); + } + + /** + * @ticket 15030 + */ + public function test_get_metadata_with_empty_key_object_value_should_be_unserialized() { + $data = new stdClass; + $data->foo = 'bar'; + add_metadata( 'user', $this->author->ID, 'foo', $data ); + $found = get_metadata( 'user', $this->author->ID ); + + $this->assertEquals( array( $data ), $found['foo'] ); + } + + /** + * @ticket 15030 + */ + public function test_get_metadata_with_empty_key_nested_array_value_should_be_unserialized() { + $data = array( + array( 1, 2 ), + array( 3, 4 ), + ); + add_metadata( 'user', $this->author->ID, 'foo', $data ); + $found = get_metadata( 'user', $this->author->ID ); + + $this->assertSame( array( $data ), $found['foo'] ); + } }