Make WordPress Core

Changeset 61465


Ignore:
Timestamp:
01/10/2026 06:36:40 AM (8 weeks ago)
Author:
westonruter
Message:

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 https://github.com/WordPress/wordpress-develop/pull/10706

Follow-up to [47211].

Props apedog, josephscott, joemcgill, westonruter.
See #49374.
Fixes #58932.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/template.php

    r61463 r61465  
    22972297function 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
  • trunk/src/wp-includes/nav-menu.php

    r61454 r61465  
    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                    }
  • trunk/tests/phpunit/tests/admin/includesTemplate.php

    r60253 r61465  
    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}
Note: See TracChangeset for help on using the changeset viewer.