Skip to content

Commit 7682e9a

Browse files
committed
Menus: Ensure a WP_Post instance gets passed to get_post_states() in wp_setup_nav_menu_item().
The `get_post_states()` function is also hardened to short-circuit in case a non-`WP_Post` is passed. A test is added to verify this. Developed in #10706 Follow-up to [47211]. Props apedog, josephscott, joemcgill, westonruter. See #49374. Fixes #58932. git-svn-id: https://develop.svn.wordpress.org/trunk@61465 602fd350-edb4-49c9-b593-d223f7449a82
1 parent fd35afa commit 7682e9a

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/wp-admin/includes/template.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,6 +2296,10 @@ function _post_states( $post, $display = true ) {
22962296
*/
22972297
function get_post_states( $post ) {
22982298
$post_states = array();
2299+
if ( ! $post instanceof WP_Post ) {
2300+
return $post_states;
2301+
}
2302+
22992303
$post_status = $_REQUEST['post_status'] ?? '';
23002304

23012305
if ( ! empty( $post->post_password ) ) {

src/wp-includes/nav-menu.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -875,10 +875,12 @@ function wp_setup_nav_menu_item( $menu_item ) {
875875
$menu_item->type_label = $object->labels->singular_name;
876876
// Denote post states for special pages (only in the admin).
877877
if ( function_exists( 'get_post_states' ) ) {
878-
$menu_post = get_post( $menu_item->object_id );
879-
$post_states = get_post_states( $menu_post );
880-
if ( $post_states ) {
881-
$menu_item->type_label = wp_strip_all_tags( implode( ', ', $post_states ) );
878+
$menu_post = get_post( $menu_item->object_id );
879+
if ( $menu_post instanceof WP_Post ) {
880+
$post_states = get_post_states( $menu_post );
881+
if ( $post_states ) {
882+
$menu_item->type_label = wp_strip_all_tags( implode( ', ', $post_states ) );
883+
}
882884
}
883885
}
884886
} else {

tests/phpunit/tests/admin/includesTemplate.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,4 +494,20 @@ public function test_wp_add_dashboard_widget() {
494494
// This doesn't actually get removed due to the invalid priority.
495495
remove_meta_box( 'dashboard2', 'dashboard', 'normal' );
496496
}
497+
498+
/**
499+
* Tests that get_post_states() handles a null value gracefully.
500+
*
501+
* This can happen when get_post() returns null (e.g., when a post
502+
* doesn't exist) and that result is passed to get_post_states()
503+
* without being checked first.
504+
*
505+
* @ticket 58932
506+
*
507+
* @covers ::get_post_states
508+
*/
509+
public function test_get_post_states_with_null_returns_empty_array() {
510+
$result = get_post_states( null );
511+
$this->assertSame( array(), $result, 'get_post_states() should return an empty array when WP_Post is not supplied.' );
512+
}
497513
}

0 commit comments

Comments
 (0)