WordPress.org

Make WordPress Core

Opened 9 months ago

Last modified 6 weeks ago

#39992 new defect (bug)

Bug in get_the_content

Reported by: olik9 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7.2
Component: Posts, Post Types Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

In get_the_content $post is not checked if it's null, after

<?php
$post = get_post();


and then on line:

<?php
if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) )

as you see, $post->post_content is queried. Then if $post is null, the program falls.

A solution can be to check:

<?php
$post = get_post();
if ( ! $post ) { return ''; }
//else

and then continue.

Thank you for everything! :)

Attachments (4)

39992.patch (399 bytes) - added by 1naveengiri 9 months ago.
Fixed in this patch.
39992.2.patch (398 bytes) - added by 1naveengiri 9 months ago.
39992.3.diff (1.1 KB) - added by birgire 6 weeks ago.
39992.4.diff (1.1 KB) - added by birgire 6 weeks ago.
Here we use assertEmpty() instead of assertEquals()

Download all attachments as: .zip

Change History (11)

#1 @SergeyBiryukov
9 months ago

  • Component changed from General to Posts, Post Types

#2 @swissspidy
9 months ago

  • Keywords needs-patch added

@1naveengiri
9 months ago

Fixed in this patch.

#3 @1naveengiri
9 months ago

  • Keywords has-patch added; needs-patch removed

#4 @1naveengiri
8 months ago

Hi team,
any update on above Bug.

#5 @swissspidy
7 months ago

  • Keywords needs-unit-tests added

@birgire
6 weeks ago

#6 @birgire
6 weeks ago

The empty post object check

$post = get_post( $post );
if ( empty( $post ) ) {
    return '';
}

is for example used in get_the_excerpt().

Surprisingly there's no explicit test for get_the_content().

It's attempting to write a test that's just a single line:

$this->assertEquals( '', get_the_content() );

as there's no global $post object available within that test method.

But I wanted to show the intention explicitly with:

unset( $GLOBALS['post'] );
$this->assertEquals( '', get_the_content() );

and this is the test in 39992.3.diff.

I did consider writing a much more verbose test, with a check if the global post is set, then store it in a temp, do the unset and then restore it afterwards (cleanup). But since there are currently no global post objects flying around in the test class, I skipped that path.

Last edited 6 weeks ago by birgire (previous) (diff)

#7 @birgire
6 weeks ago

  • Keywords has-unit-tests added; needs-unit-tests removed

@birgire
6 weeks ago

Here we use assertEmpty() instead of assertEquals()

Note: See TracTickets for help on using tickets.