[wp-trac] [WordPress Trac] #29660: Functions in wp_includes/query.php assume non-null return value from get_queried_object

WordPress Trac noreply at wordpress.org
Fri Oct 15 13:55:29 UTC 2021


#29660: Functions in wp_includes/query.php assume non-null return value from
get_queried_object
-------------------------------------------------+-------------------------
 Reporter:  yellyc                               |       Owner:
                                                 |  SergeyBiryukov
     Type:  defect (bug)                         |      Status:  reviewing
 Priority:  normal                               |   Milestone:  5.9
Component:  Query                                |     Version:  4.0
 Severity:  normal                               |  Resolution:
 Keywords:  2nd-opinion has-patch has-unit-      |     Focuses:
  tests                                          |
-------------------------------------------------+-------------------------

Comment (by woji29911):

 Hi everyone, so for now best solution is to replace original code with
 agengineering code from this ticket:
 https://core.trac.wordpress.org/ticket/49024 ? It stops PHP warnings in my
 website, but is it safe to use? Thank you :)



 {{{
 <?php
         /**
          * Is the query for an existing single page?
          *
          * If the $page parameter is specified, this function will
 additionally
          * check if the query is for one of the pages specified.
          *
          * @see WP_Query::is_single()
          * @see WP_Query::is_singular()
          *
          * @since 3.1.0
          *
          * @param int|string|array $page Optional. Page ID, title, slug,
 path, or array of such. Default empty.
          * @return bool Whether the query is for an existing single page.
          */
         public function is_page( $page = '' ) {
                 if ( ! $this->is_page ) {
                         return false;
                 }

                 if ( empty( $page ) ) {
                         return true;
                 }

                 $page_obj = $this->get_queried_object();

                 $page = array_map( 'strval', (array) $page );

                 if ( ! empty( $page_obj ) && in_array( (string)
 $page_obj->ID, $page ) ) {
                         return true;
                 } elseif ( ! empty( $page_obj ) && in_array(
 $page_obj->post_title, $page ) ) {
                         return true;
                 } elseif ( ! empty( $page_obj ) && in_array(
 $page_obj->post_name, $page ) ) {
                         return true;
                 } else {
                         foreach ( $page as $pagepath ) {
                                 if ( ! strpos( $pagepath, '/' ) ) {
                                         continue;
                                 }
                                 $pagepath_obj = get_page_by_path(
 $pagepath );

                                 if ( $pagepath_obj && ( $pagepath_obj->ID
 == $page_obj->ID ) ) {
                                         return true;
                                 }
                         }
                 }

                 return false;
         }
 ?>
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/29660#comment:45>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list