Opened 8 years ago
Last modified 7 years 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)
Change History (11)
#6
@
7 years 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.
Fixed in this patch.