Skip to content

Commit f9f8d0e

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

2 files changed

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

0 commit comments

Comments
 (0)