Skip to content

Commit 7e9f7a4

Browse files
committed
Post: Ensure get_post_custom_values() handles non-array returns gracefully.
1 parent 4d3b0b9 commit 7e9f7a4

2 files changed

Lines changed: 98 additions & 0 deletions

File tree

src/wp-includes/post.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,6 +2889,10 @@ function get_post_custom_values( $key = '', $post_id = 0 ) {
28892889

28902890
$custom = get_post_custom( $post_id );
28912891

2892+
if ( ! is_array( $custom ) ) {
2893+
return null;
2894+
}
2895+
28922896
return $custom[ $key ] ?? null;
28932897
}
28942898

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/**
4+
* @group post
5+
*/
6+
class Tests_Post_GetPostCustom extends WP_UnitTestCase {
7+
8+
protected static $post_id;
9+
10+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
11+
self::$post_id = $factory->post->create();
12+
add_post_meta( self::$post_id, 'test_key_1', 'value_a' );
13+
add_post_meta( self::$post_id, 'test_key_1', 'value_b' );
14+
add_post_meta( self::$post_id, 'test_key_2', 'value_c' );
15+
}
16+
17+
/**
18+
* Test get_post_custom() happy path.
19+
*/
20+
public function test_get_post_custom_happy_path() {
21+
$custom = get_post_custom( self::$post_id );
22+
23+
$this->assertIsArray( $custom );
24+
$this->assertArrayHasKey( 'test_key_1', $custom );
25+
$this->assertCount( 2, $custom['test_key_1'] );
26+
$this->assertSame( array( 'value_a', 'value_b' ), $custom['test_key_1'] );
27+
}
28+
29+
/**
30+
* Test get_post_custom_keys() happy path.
31+
*/
32+
public function test_get_post_custom_keys_happy_path() {
33+
$keys = get_post_custom_keys( self::$post_id );
34+
35+
$this->assertIsArray( $keys );
36+
$this->assertContains( 'test_key_1', $keys );
37+
$this->assertContains( 'test_key_2', $keys );
38+
}
39+
40+
/**
41+
* Test get_post_custom_values() happy path.
42+
*/
43+
public function test_get_post_custom_values_happy_path() {
44+
$values = get_post_custom_values( 'test_key_1', self::$post_id );
45+
46+
$this->assertIsArray( $values );
47+
$this->assertSame( array( 'value_a', 'value_b' ), $values );
48+
}
49+
50+
/**
51+
* @ticket 65044
52+
* Test functions with non-existent post ID (The core of ticket #65044).
53+
*/
54+
public function test_custom_functions_with_invalid_post_id() {
55+
$invalid_id = 99999;
56+
57+
// get_post_custom returns empty array for non-existent post in current WP
58+
$this->assertSame( array(), get_post_custom( $invalid_id ) );
59+
60+
// get_post_custom_keys returns null/void if array is empty/invalid
61+
$this->assertNull( get_post_custom_keys( $invalid_id ) );
62+
63+
// get_post_custom_values should return null (Fix for #65044)
64+
$this->assertNull( get_post_custom_values( 'test_key_1', $invalid_id ) );
65+
}
66+
67+
/**
68+
* @ticket 65044
69+
* Test get_post_custom_values() with empty key.
70+
*/
71+
public function test_get_post_custom_values_empty_key() {
72+
$this->assertNull( get_post_custom_values( '', self::$post_id ) );
73+
$this->assertNull( get_post_custom_values( null, self::$post_id ) );
74+
}
75+
76+
/**
77+
* Test behavior when post_id is 0 and no global post is set.
78+
*/
79+
public function test_custom_functions_with_zero_id_no_global_post() {
80+
// Ensure global post is null
81+
$GLOBALS['post'] = null;
82+
83+
$custom = get_post_custom( 0 );
84+
$this->assertEmpty( $custom, 'get_post_custom(0) should return an empty value (false or empty array).' );
85+
$this->assertNull( get_post_custom_keys( 0 ) );
86+
$this->assertNull( get_post_custom_values( 'test_key_1', 0 ) );
87+
}
88+
/**
89+
* Test get_post_custom_values() when key does not exist.
90+
*/
91+
public function test_get_post_custom_values_non_existent_key() {
92+
$this->assertNull( get_post_custom_values( 'non_existent_key', self::$post_id ) );
93+
}
94+
}

0 commit comments

Comments
 (0)