Make WordPress Core

Changeset 53858


Ignore:
Timestamp:
08/08/2022 08:20:43 AM (4 years ago)
Author:
audrasjb
Message:

Editor: Safeguard has_blocks() against fatal errors.

This changeset ensures has_blocks() doesn't return a fatal error when $post is not a valid post. If the post can't be retrieved, the function now returns false.

Props Howdy_McGee, costdev, colonelphantom, audrasjb, dlh, peterwilsoncc.
Fixes #55705.

Location:
trunk
Files:
2 edited

Legend:

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

    r53799 r53858  
    426426        if ( ! is_string( $post ) ) {
    427427                $wp_post = get_post( $post );
    428                 if ( $wp_post instanceof WP_Post ) {
    429                         $post = $wp_post->post_content;
    430                 }
     428
     429                if ( ! $wp_post instanceof WP_Post ) {
     430                        return false;
     431                }
     432
     433                $post = $wp_post->post_content;
    431434        }
    432435
  • trunk/tests/phpunit/tests/blocks/register.php

    r53718 r53858  
    554554
    555555        /**
     556         * Tests that `has_blocks()` returns `false` with an invalid post.
     557         *
     558         * @ticket 55705
     559         *
     560         * @covers ::has_blocks
     561         */
     562        public function test_has_blocks_with_invalid_post() {
     563                $a_post = (object) array(
     564                        'ID'     => 55705,
     565                        'filter' => 'display',
     566                );
     567                $this->assertFalse( has_blocks( $a_post ) );
     568        }
     569
     570        /**
    556571         * @ticket 49615
    557572         */
Note: See TracChangeset for help on using the changeset viewer.