Make WordPress Core

Ticket #55705: 55705.diff

File 55705.diff, 1.3 KB (added by costdev, 17 months ago)

Adds a _doing_it_wrong() and returns false when get_post() returns null. Includes PHPUnit test.

  • src/wp-includes/blocks.php

    diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php
    index 9df70abaf4..d6f78a7d2d 100644
    a b function unregister_block_type( $name ) { 
    424424function has_blocks( $post = null ) {
    425425        if ( ! is_string( $post ) ) {
    426426                $wp_post = get_post( $post );
     427
     428                if ( null === $wp_post ) {
     429                        _doing_it_wrong(
     430                                __FUNCTION__,
     431                                sprintf(
     432                                        /* translators: %s: The $post variable. */
     433                                        __( '%s is not a valid post.'),
     434                                        '<code>$post</code>'
     435                                ),
     436                                '6.1.0'
     437                        );
     438
     439                        return false;
     440                }
     441
    427442                if ( $wp_post instanceof WP_Post ) {
    428443                        $post = $wp_post->post_content;
    429444                }
  • tests/phpunit/tests/blocks/register.php

    diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php
    index 408927197a..8c5b17493c 100644
    a b class Tests_Blocks_Register extends WP_UnitTestCase { 
    551551                $this->assertFalse( has_blocks( $content ) );
    552552        }
    553553
     554        /**
     555         * @ticket 55705
     556         *
     557         * @expectedIncorrectUsage has_blocks
     558         *
     559         * @covers ::has_blocks
     560         */
     561        public function test_has_blocks_with_invalid_post() {
     562                $a_post = (object) array( 'ID' => 13585, 'filter' => 'display' );
     563                $this->assertFalse( has_blocks( $a_post ) );
     564        }
     565
    554566        /**
    555567         * @ticket 49615
    556568         */