Make WordPress Core

Ticket #35913: 35913.diff

File 35913.diff, 6.6 KB (added by boonebgorges, 9 years ago)
  • src/wp-includes/query.php

    diff --git src/wp-includes/query.php src/wp-includes/query.php
    index 70d0237..be4ceac 100644
    class WP_Query { 
    41534153        }
    41544154
    41554155        /**
     4156         * Helper for `is_{$object_type}` methods.
     4157         *
     4158         * Checks to see whether the currently queried object is of the proper type
     4159         * and is identical to `$object`, if provided.
     4160         *
     4161         * @param string           $flag             The `WP_Query` flag to check for object-type status (eg 'is_page').
     4162         * @param int|string|array $object           Object identifier, as passed to the parent function.
     4163         * @param array            $prop_list        List of `$object` properties to compare against the currently
     4164         *                                           queried object. Property names should be provided in order of
     4165         *                                           precedence (ID first).
     4166         * @param bool             $get_page_by_path Whether to query the database (using get_page_by_path())
     4167         *                                           if matches fail using available `$object` information.
     4168         * @return bool
     4169         */
     4170        protected function is_object_type( $flag, $object, $prop_list, $get_page_by_path = false ) {
     4171                if ( empty( $this->{$flag} ) ) {
     4172                        return false;
     4173                }
     4174
     4175                if ( empty( $object ) ) {
     4176                        return true;
     4177                }
     4178
     4179                $object = array_map( 'strval', (array) $object );
     4180
     4181                $post_obj = $this->get_queried_object();
     4182
     4183                foreach ( $prop_list as $prop ) {
     4184                        $prop_value = isset( $post_obj->{$prop} ) ? (string) $post_obj->{$prop} : null;
     4185
     4186                        if ( null !== $prop_value && in_array( $prop_value, $object ) ) {
     4187                                return true;
     4188                        }
     4189                }
     4190
     4191                if ( $get_page_by_path ) {
     4192                        foreach ( $object as $objectpath ) {
     4193                                if ( ! strpos( $objectpath, '/' ) ) {
     4194                                        continue;
     4195                                }
     4196                                $objectpath_obj = get_page_by_path( $objectpath, OBJECT, $post_obj->post_type );
     4197
     4198                                if ( $objectpath_obj && ( $objectpath_obj->ID == $post_obj->ID ) ) {
     4199                                        return true;
     4200                                }
     4201                        }
     4202                }
     4203
     4204                return false;
     4205        }
     4206
     4207        /**
    41564208         * Is the query for an existing archive page?
    41574209         *
    41584210         * Month, Year, Category, Author, Post Type archive...
    class WP_Query { 
    41944246         * @return bool
    41954247         */
    41964248        public function is_attachment( $attachment = '' ) {
    4197                 if ( ! $this->is_attachment ) {
    4198                         return false;
    4199                 }
    4200 
    4201                 if ( empty( $attachment ) ) {
    4202                         return true;
    4203                 }
    4204 
    4205                 $attachment = array_map( 'strval', (array) $attachment );
    4206 
    4207                 $post_obj = $this->get_queried_object();
    4208 
    4209                 if ( in_array( (string) $post_obj->ID, $attachment ) ) {
    4210                         return true;
    4211                 } elseif ( in_array( $post_obj->post_title, $attachment ) ) {
    4212                         return true;
    4213                 } elseif ( in_array( $post_obj->post_name, $attachment ) ) {
    4214                         return true;
    4215                 }
    4216                 return false;
     4249                return $this->is_object_type( 'is_attachment', $attachment, array( 'ID', 'post_title', 'post_name' ) );
    42174250        }
    42184251
    42194252        /**
    class WP_Query { 
    42284261         * @return bool
    42294262         */
    42304263        public function is_author( $author = '' ) {
    4231                 if ( !$this->is_author )
    4232                         return false;
    4233 
    4234                 if ( empty($author) )
    4235                         return true;
    4236 
    4237                 $author_obj = $this->get_queried_object();
    4238 
    4239                 $author = array_map( 'strval', (array) $author );
    4240 
    4241                 if ( in_array( (string) $author_obj->ID, $author ) )
    4242                         return true;
    4243                 elseif ( in_array( $author_obj->nickname, $author ) )
    4244                         return true;
    4245                 elseif ( in_array( $author_obj->user_nicename, $author ) )
    4246                         return true;
    4247 
    4248                 return false;
     4264                return $this->is_object_type( 'is_author', $author, array( 'ID', 'nickname', 'user_nicename' ) );
    42494265        }
    42504266
    42514267        /**
    class WP_Query { 
    42604276         * @return bool
    42614277         */
    42624278        public function is_category( $category = '' ) {
    4263                 if ( !$this->is_category )
    4264                         return false;
    4265 
    4266                 if ( empty($category) )
    4267                         return true;
    4268 
    4269                 $cat_obj = $this->get_queried_object();
    4270 
    4271                 $category = array_map( 'strval', (array) $category );
    4272 
    4273                 if ( in_array( (string) $cat_obj->term_id, $category ) )
    4274                         return true;
    4275                 elseif ( in_array( $cat_obj->name, $category ) )
    4276                         return true;
    4277                 elseif ( in_array( $cat_obj->slug, $category ) )
    4278                         return true;
    4279 
    4280                 return false;
     4279                return $this->is_object_type( 'is_category', $category, array( 'term_id', 'name', 'slug' ) );
    42814280        }
    42824281
    42834282        /**
    class WP_Query { 
    42924291         * @return bool
    42934292         */
    42944293        public function is_tag( $tag = '' ) {
    4295                 if ( ! $this->is_tag )
    4296                         return false;
    4297 
    4298                 if ( empty( $tag ) )
    4299                         return true;
    4300 
    4301                 $tag_obj = $this->get_queried_object();
    4302 
    4303                 $tag = array_map( 'strval', (array) $tag );
    4304 
    4305                 if ( in_array( (string) $tag_obj->term_id, $tag ) )
    4306                         return true;
    4307                 elseif ( in_array( $tag_obj->name, $tag ) )
    4308                         return true;
    4309                 elseif ( in_array( $tag_obj->slug, $tag ) )
    4310                         return true;
    4311 
    4312                 return false;
     4294                return $this->is_object_type( 'is_tag', $tag, array( 'term_id', 'name', 'slug' ) );
    43134295        }
    43144296
    43154297        /**
    class WP_Query { 
    44944476         * @return bool Whether the query is for an existing single page.
    44954477         */
    44964478        public function is_page( $page = '' ) {
    4497                 if ( !$this->is_page )
    4498                         return false;
    4499 
    4500                 if ( empty( $page ) )
    4501                         return true;
    4502 
    4503                 $page_obj = $this->get_queried_object();
    4504 
    4505                 $page = array_map( 'strval', (array) $page );
    4506 
    4507                 if ( in_array( (string) $page_obj->ID, $page ) ) {
    4508                         return true;
    4509                 } elseif ( in_array( $page_obj->post_title, $page ) ) {
    4510                         return true;
    4511                 } elseif ( in_array( $page_obj->post_name, $page ) ) {
    4512                         return true;
    4513                 } else {
    4514                         foreach ( $page as $pagepath ) {
    4515                                 if ( ! strpos( $pagepath, '/' ) ) {
    4516                                         continue;
    4517                                 }
    4518                                 $pagepath_obj = get_page_by_path( $pagepath );
    4519 
    4520                                 if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) {
    4521                                         return true;
    4522                                 }
    4523                         }
    4524                 }
    4525 
    4526                 return false;
     4479                return $this->is_object_type( 'is_page', $page, array( 'ID', 'post_title', 'post_name' ), true );
    45274480        }
    45284481
    45294482        /**
    class WP_Query { 
    45874540         * @return bool Whether the query is for an existing single post.
    45884541         */
    45894542        public function is_single( $post = '' ) {
    4590                 if ( !$this->is_single )
    4591                         return false;
    4592 
    4593                 if ( empty($post) )
    4594                         return true;
    4595 
    4596                 $post_obj = $this->get_queried_object();
    4597 
    4598                 $post = array_map( 'strval', (array) $post );
    4599 
    4600                 if ( in_array( (string) $post_obj->ID, $post ) ) {
    4601                         return true;
    4602                 } elseif ( in_array( $post_obj->post_title, $post ) ) {
    4603                         return true;
    4604                 } elseif ( in_array( $post_obj->post_name, $post ) ) {
    4605                         return true;
    4606                 } else {
    4607                         foreach ( $post as $postpath ) {
    4608                                 if ( ! strpos( $postpath, '/' ) ) {
    4609                                         continue;
    4610                                 }
    4611                                 $postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type );
    4612 
    4613                                 if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) {
    4614                                         return true;
    4615                                 }
    4616                         }
    4617                 }
    4618                 return false;
     4543                return $this->is_object_type( 'is_single', $post, array( 'ID', 'post_title', 'post_name' ), true );
    46194544        }
    46204545
    46214546        /**