From 0f1673557b6e0a1e629d50d555fd2f3638d3ecbb Mon Sep 17 00:00:00 2001 From: liaisontw Date: Fri, 24 Apr 2026 12:27:37 +0800 Subject: [PATCH] Post: Ensure get_post_custom_values() handles non-array returns gracefully. --- src/wp-includes/post.php | 4 + tests/phpunit/tests/post/getPostCustom.php | 99 ++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 tests/phpunit/tests/post/getPostCustom.php diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 88deb1090fc5c..766b35cdb5b9c 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -2889,6 +2889,10 @@ function get_post_custom_values( $key = '', $post_id = 0 ) { $custom = get_post_custom( $post_id ); + if ( ! is_array( $custom ) ) { + return null; + } + return $custom[ $key ] ?? null; } diff --git a/tests/phpunit/tests/post/getPostCustom.php b/tests/phpunit/tests/post/getPostCustom.php new file mode 100644 index 0000000000000..77d3ed087928d --- /dev/null +++ b/tests/phpunit/tests/post/getPostCustom.php @@ -0,0 +1,99 @@ +post->create(); + add_post_meta( self::$post_id, 'test_key_1', 'value_a' ); + add_post_meta( self::$post_id, 'test_key_1', 'value_b' ); + add_post_meta( self::$post_id, 'test_key_2', 'value_c' ); + } + + /** + * Test get_post_custom() happy path. + */ + public function test_get_post_custom_happy_path() { + $custom = get_post_custom( self::$post_id ); + + $this->assertIsArray( $custom ); + $this->assertArrayHasKey( 'test_key_1', $custom ); + $this->assertCount( 2, $custom['test_key_1'] ); + $this->assertSame( array( 'value_a', 'value_b' ), $custom['test_key_1'] ); + } + + /** + * Test get_post_custom_keys() happy path. + */ + public function test_get_post_custom_keys_happy_path() { + $keys = get_post_custom_keys( self::$post_id ); + + $this->assertIsArray( $keys ); + $this->assertContains( 'test_key_1', $keys ); + $this->assertContains( 'test_key_2', $keys ); + } + + /** + * Test get_post_custom_values() happy path. + */ + public function test_get_post_custom_values_happy_path() { + $values = get_post_custom_values( 'test_key_1', self::$post_id ); + + $this->assertIsArray( $values ); + $this->assertSame( array( 'value_a', 'value_b' ), $values ); + } + + /** + * Test functions with non-existent post ID (The core of ticket #65044). + * + * @ticket 65044 + */ + public function test_custom_functions_with_invalid_post_id() { + $invalid_id = 99999; + + // get_post_custom returns empty array for non-existent post in current WP + $this->assertSame( array(), get_post_custom( $invalid_id ) ); + + // get_post_custom_keys returns null/void if array is empty/invalid + $this->assertNull( get_post_custom_keys( $invalid_id ) ); + + // get_post_custom_values should return null (Fix for #65044) + $this->assertNull( get_post_custom_values( 'test_key_1', $invalid_id ) ); + } + + /** + * Test get_post_custom_values() with empty key. + * + * @ticket 65044 + */ + public function test_get_post_custom_values_empty_key() { + $this->assertNull( get_post_custom_values( '', self::$post_id ) ); + $this->assertNull( get_post_custom_values( null, self::$post_id ) ); + } + + /** + * Test behavior when post_id is 0 and no global post is set. + */ + public function test_custom_functions_with_zero_id_no_global_post() { + // Ensure global post is null + $GLOBALS['post'] = null; + + $custom = get_post_custom( 0 ); + $this->assertEmpty( $custom, 'get_post_custom(0) should return an empty value (false or empty array).' ); + $this->assertNull( get_post_custom_keys( 0 ) ); + $this->assertNull( get_post_custom_values( 'test_key_1', 0 ) ); + } + + /** + * Test get_post_custom_values() when key does not exist. + */ + public function test_get_post_custom_values_non_existent_key() { + $this->assertNull( get_post_custom_values( 'non_existent_key', self::$post_id ) ); + } +}