[wp-trac] [WordPress Trac] #24674: WP_Query::is_page() should use stricter comparison

WordPress Trac noreply at wordpress.org
Tue Jul 2 15:07:16 UTC 2013


#24674: WP_Query::is_page() should use stricter comparison
--------------------------+-----------------------------
 Reporter:  clifgriffin   |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  3.5.2
 Severity:  minor         |   Keywords:  has-patch
--------------------------+-----------------------------
 In WP_Query::is_page(), the following evaluations are made to determine if
 the queried object matches the supplied page property:

 {{{
                 if ( in_array( $page_obj->ID, $page ) )
                         return true;
                 elseif ( in_array( $page_obj->post_title, $page ) )
                         return true;
                 else if ( in_array( $page_obj->post_name, $page ) )
                         return true;
 }}}

 This works fine, as long as $page_obj->ID is not zero.  If it is zero, the
 function will return true in every case.

 For example:
 {{{
 $test = array('about-us');

 if( in_array(0, $test) ) {
         // this will always be true
 }
 }}}

 To fix this, I'd recommend using strict mode for in_array, at least for ID
 comparisons:
 {{{
                 if ( in_array( $page_obj->ID, $page, true ) )
                         return true;
                 elseif ( in_array( $page_obj->post_title, $page ) )
                         return true;
                 else if ( in_array( $page_obj->post_name, $page ) )
                         return true;
 }}}

 It may seem unlikely that $page_obj->ID would ever be zero, but consider
 that some more complex plugins use virtual pages, or otherwise override
 the queried object.  In this case, 0 is the only valid value that also
 won't result in a collision with an existing real post.

 This seems like a harmless change that will make is_page() return more
 reliable results, with few if any side effects.

--
Ticket URL: <http://core.trac.wordpress.org/ticket/24674>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list