[wp-trac] [WordPress Trac] #13753: get_page() breaking $post when it's called with empty parameter
WordPress Trac
wp-trac at lists.automattic.com
Mon Jun 7 04:37:17 UTC 2010
#13753: get_page() breaking $post when it's called with empty parameter
--------------------------+-------------------------------------------------
Reporter: shidouhikari | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: 3.0
Component: General | Version: 3.0
Severity: normal | Keywords:
--------------------------+-------------------------------------------------
( get_page() and get_post() are both in \wp-includes\post.php, in 3.0 RC1
they are in lines 2866 and 356 )
As we know, global $post should always be an object, but in a rare
situation it gets broken and stores a post ID.
That happens when the funtion get_page() is called and its first
parameter, &$page, has a value that makes empty($page) true. When that
happens and $GLOBALS[ 'post'] was correcly set earlier and has a valid
$GLOBALS[ 'post']->ID, the function get_page() calls get_post(), so that
it returns the page data formatted according to $output and $filter
parameters.
The problem is that get_post() receives &$post as reference, reference to
the global variable in this case.
In line 370, if $post is an object, it's overridden to its ID, because
it's the ID that will be required later. After that the variable $post is
never touched again, and get_post() returns leaving $GLOBALS[ 'post'] set
as an int refering to the post ID, and not the post object. get_page()
also returns doing nothing about it.
To replicate the bug, put the following code anywhere that will run when
is_singular() is true and $post has already been set (probably by The
Loop), regardless if it's a post or page. I didn't test with custom types.
{{{
<?php
global $post;
$null = null;
echo '<p>$post begin (should be an object of type stdClass):</p>';
var_dump($post);
$post_id = $post->ID;
echo '<p>Saving post ID into $post_id:</p>';
var_dump($post_id);
$getPost = get_post($null);
echo '<p>get_post(null):</p>';
var_dump($getPost);
echo '<p>$post after get_post(null) (should be of type stdClass):</p>';
var_dump($post);
$getPage = get_page($null);
echo '<p>get_page(null):</p>';
var_dump($getPage);
echo '<p>$post after get_page(null) (should be of type stdClass, but now
it\' Integer):</p>';
var_dump($post);
?>
}}}
--
Ticket URL: <http://core.trac.wordpress.org/ticket/13753>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list