Make WordPress Core

Ticket #31723: 31723.1.diff

File 31723.1.diff, 15.6 KB (added by jipmoors, 10 years ago)

applied to all functions + added unit tests for invalid data

  • src/wp-includes/query.php

     
    155155 * @since 3.1.0
    156156 * @uses $wp_query
    157157 *
    158  * @param mixed $post_types Optional. Post type or array of posts types to check against.
     158 * @param mixed $post_types Optional. Post type or array of posts types to check against. Default null.
    159159 * @return bool
    160160 */
    161 function is_post_type_archive( $post_types = '' ) {
     161function is_post_type_archive( $post_types = null ) {
    162162        global $wp_query;
    163163
    164164        if ( ! isset( $wp_query ) ) {
     
    176176 * @since 2.0.0
    177177 * @uses $wp_query
    178178 *
    179  * @param int|string|array|object $attachment Attachment ID, title, slug, or array of such.
     179 * @param mixed $attachment Optional. Attachment ID, title, slug, or array of such. Default null.
    180180 * @return bool
    181181 */
    182 function is_attachment( $attachment = '' ) {
     182function is_attachment( $attachment = null ) {
    183183        global $wp_query;
    184184
    185185        if ( ! isset( $wp_query ) ) {
     
    200200 * @since 1.5.0
    201201 * @uses $wp_query
    202202 *
    203  * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
     203 * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames. Defualt null.
    204204 * @return bool
    205205 */
    206 function is_author( $author = '' ) {
     206function is_author( $author = null ) {
    207207        global $wp_query;
    208208
    209209        if ( ! isset( $wp_query ) ) {
     
    224224 * @since 1.5.0
    225225 * @uses $wp_query
    226226 *
    227  * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
     227 * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs. Default null.
    228228 * @return bool
    229229 */
    230 function is_category( $category = '' ) {
     230function is_category( $category = null ) {
    231231        global $wp_query;
    232232
    233233        if ( ! isset( $wp_query ) ) {
     
    248248 * @since 2.3.0
    249249 * @uses $wp_query
    250250 *
    251  * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
     251 * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs. Default null.
    252252 * @return bool
    253253 */
    254 function is_tag( $tag = '' ) {
     254function is_tag( $tag = null ) {
    255255        global $wp_query;
    256256
    257257        if ( ! isset( $wp_query ) ) {
     
    276276 * @since 2.5.0
    277277 * @uses $wp_query
    278278 *
    279  * @param string|array $taxonomy Optional. Taxonomy slug or slugs.
    280  * @param int|string|array $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
     279 * @param mixed $taxonomy Optional. Taxonomy slug or slugs. Default null.
     280 * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs. Default null.
    281281 * @return bool
    282282 */
    283 function is_tax( $taxonomy = '', $term = '' ) {
     283function is_tax( $taxonomy = null, $term = null ) {
    284284        global $wp_query;
    285285
    286286        if ( ! isset( $wp_query ) ) {
     
    361361 * @param string|array $feeds Optional feed types to check.
    362362 * @return bool
    363363 */
    364 function is_feed( $feeds = '' ) {
     364function is_feed( $feeds = null ) {
    365365        global $wp_query;
    366366
    367367        if ( ! isset( $wp_query ) ) {
     
    482482 * @since 1.5.0
    483483 * @uses $wp_query
    484484 *
    485  * @param mixed $page Page ID, title, slug, or array of such.
     485 * @param mixed $page Optional. Page ID, title, slug, or array of such. Default null.
    486486 * @return bool
    487487 */
    488 function is_page( $page = '' ) {
     488function is_page( $page = null ) {
    489489        global $wp_query;
    490490
    491491        if ( ! isset( $wp_query ) ) {
     
    591591 * @since 1.5.0
    592592 * @uses $wp_query
    593593 *
    594  * @param mixed $post Post ID, title, slug, or array of such.
     594 * @param mixed $post Optional. Post ID, title, slug, or array of such. Default null.
    595595 * @return bool
    596596 */
    597 function is_single( $post = '' ) {
     597function is_single( $post = null ) {
    598598        global $wp_query;
    599599
    600600        if ( ! isset( $wp_query ) ) {
     
    618618 * @since 1.5.0
    619619 * @uses $wp_query
    620620 *
    621  * @param mixed $post_types Optional. Post Type or array of Post Types
     621 * @param mixed $post_types Optional. Post Type or array of Post Types. Default null.
    622622 * @return bool
    623623 */
    624 function is_singular( $post_types = '' ) {
     624function is_singular( $post_types = null ) {
    625625        global $wp_query;
    626626
    627627        if ( ! isset( $wp_query ) ) {
     
    40414041         *
    40424042         * @since 3.1.0
    40434043         *
    4044          * @param mixed $post_types Optional. Post type or array of posts types to check against.
     4044         * @param mixed $post_types Optional. Post type or array of posts types to check against. Default null.
    40454045         * @return bool
    40464046         */
    4047         public function is_post_type_archive( $post_types = '' ) {
    4048                 if ( empty( $post_types ) || ! $this->is_post_type_archive )
     4047        public function is_post_type_archive( $post_types = null ) {
     4048                if ( is_null( $post_types ) || ! $this->is_post_type_archive )
    40494049                        return (bool) $this->is_post_type_archive;
    40504050
     4051                $post_types = (array) $post_types;
     4052
     4053                if ( empty( $post_types ) )
     4054                        return true;
     4055
    40514056                $post_type = $this->get( 'post_type' );
    40524057                if ( is_array( $post_type ) )
    40534058                        $post_type = reset( $post_type );
    40544059                $post_type_object = get_post_type_object( $post_type );
    40554060
    4056                 return in_array( $post_type_object->name, (array) $post_types );
     4061                return in_array( $post_type_object->name, $post_types, true );
    40574062        }
    40584063
    40594064        /**
     
    40614066         *
    40624067         * @since 3.1.0
    40634068         *
    4064          * @param mixed $attachment Attachment ID, title, slug, or array of such.
     4069         * @param mixed $attachment Optional. Attachment ID, title, slug, or array of such. Default null.
    40654070         * @return bool
    40664071         */
    4067         public function is_attachment( $attachment = '' ) {
    4068                 if ( ! $this->is_attachment ) {
    4069                         return false;
     4072        public function is_attachment( $attachment = null ) {
     4073                if ( is_null( $attachment ) || ! $this->is_attachment ) {
     4074                        return (bool) $this->is_attachment;
    40704075                }
    40714076
    4072                 if ( empty( $attachment ) ) {
     4077                $attachment = (array) $attachment;
     4078
     4079                if ( empty( $attachment ) )
    40734080                        return true;
    4074                 }
    40754081
    4076                 $attachment = (array) $attachment;
    4077 
    40784082                $post_obj = $this->get_queried_object();
    40794083
    40804084                if ( in_array( (string) $post_obj->ID, $attachment ) ) {
    40814085                        return true;
    4082                 } elseif ( in_array( $post_obj->post_title, $attachment ) ) {
     4086                } elseif ( in_array( $post_obj->post_title, $attachment, true ) ) {
    40834087                        return true;
    4084                 } elseif ( in_array( $post_obj->post_name, $attachment ) ) {
     4088                } elseif ( in_array( $post_obj->post_name, $attachment, true ) ) {
    40854089                        return true;
    40864090                }
    40874091                return false;
     
    40954099         *
    40964100         * @since 3.1.0
    40974101         *
    4098          * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
     4102         * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames. Default null.
    40994103         * @return bool
    41004104         */
    4101         public function is_author( $author = '' ) {
    4102                 if ( !$this->is_author )
    4103                         return false;
     4105        public function is_author( $author = null ) {
     4106                if ( is_null( $author ) || !$this->is_author )
     4107                        return (bool) $this->is_author;
    41044108
    4105                 if ( empty($author) )
     4109                $author = (array) $author;
     4110
     4111                if ( empty( $author ) )
    41064112                        return true;
    41074113
    41084114                $author_obj = $this->get_queried_object();
     
    41114117
    41124118                if ( in_array( (string) $author_obj->ID, $author ) )
    41134119                        return true;
    4114                 elseif ( in_array( $author_obj->nickname, $author ) )
     4120                elseif ( in_array( $author_obj->nickname, $author, true ) )
    41154121                        return true;
    4116                 elseif ( in_array( $author_obj->user_nicename, $author ) )
     4122                elseif ( in_array( $author_obj->user_nicename, $author, true ) )
    41174123                        return true;
    41184124
    41194125                return false;
     
    41274133         *
    41284134         * @since 3.1.0
    41294135         *
    4130          * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
     4136         * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs. Default null.
    41314137         * @return bool
    41324138         */
    4133         public function is_category( $category = '' ) {
    4134                 if ( !$this->is_category )
    4135                         return false;
     4139        public function is_category( $category = null ) {
     4140                if ( is_null( $category ) || !$this->is_category )
     4141                        return (bool) $this->is_category;
    41364142
    4137                 if ( empty($category) )
     4143                $category = (array) $category;
     4144
     4145                if ( empty( $category ) )
    41384146                        return true;
    41394147
    41404148                $cat_obj = $this->get_queried_object();
    41414149
    4142                 $category = (array) $category;
    4143 
    41444150                if ( in_array( (string) $cat_obj->term_id, $category ) )
    41454151                        return true;
    4146                 elseif ( in_array( $cat_obj->name, $category ) )
     4152                elseif ( in_array( $cat_obj->name, $category, true ) )
    41474153                        return true;
    4148                 elseif ( in_array( $cat_obj->slug, $category ) )
     4154                elseif ( in_array( $cat_obj->slug, $category, true ) )
    41494155                        return true;
    41504156
    41514157                return false;
     
    41594165         *
    41604166         * @since 3.1.0
    41614167         *
    4162          * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
     4168         * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs. Default null.
    41634169         * @return bool
    41644170         */
    4165         public function is_tag( $tag = '' ) {
    4166                 if ( ! $this->is_tag )
    4167                         return false;
     4171        public function is_tag( $tag = null ) {
     4172                if ( is_null( $tag ) || ! $this->is_tag )
     4173                        return (bool) $this->is_tag;
    41684174
     4175                $tag = (array) $tag;
     4176
    41694177                if ( empty( $tag ) )
    41704178                        return true;
    41714179
    41724180                $tag_obj = $this->get_queried_object();
    41734181
    4174                 $tag = (array) $tag;
    4175 
    41764182                if ( in_array( (string) $tag_obj->term_id, $tag ) )
    41774183                        return true;
    4178                 elseif ( in_array( $tag_obj->name, $tag ) )
     4184                elseif ( in_array( $tag_obj->name, $tag, true ) )
    41794185                        return true;
    4180                 elseif ( in_array( $tag_obj->slug, $tag ) )
     4186                elseif ( in_array( $tag_obj->slug, $tag, true ) )
    41814187                        return true;
    41824188
    41834189                return false;
     
    41954201         *
    41964202         * @since 3.1.0
    41974203         *
    4198          * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
    4199          * @param mixed $term     Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
     4204         * @param mixed $taxonomy Optional. Taxonomy slug or slugs. Default null.
     4205         * @param mixed $term     Optional. Term ID, name, slug or array of Term IDs, names, and slugs. Default null.
    42004206         * @return bool
    42014207         */
    4202         public function is_tax( $taxonomy = '', $term = '' ) {
     4208        public function is_tax( $taxonomy = null, $term = null ) {
    42034209                global $wp_taxonomies;
    42044210
    4205                 if ( !$this->is_tax )
    4206                         return false;
     4211                if ( is_null( $taxonomy ) || !$this->is_tax )
     4212                        return (bool) $this->is_tax;
    42074213
    4208                 if ( empty( $taxonomy ) )
    4209                         return true;
    4210 
    42114214                $queried_object = $this->get_queried_object();
    42124215                $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
    4213                 $term_array = (array) $term;
    42144216
    42154217                // Check that the taxonomy matches.
    42164218                if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) )
    42174219                        return false;
    42184220
    42194221                // Only a Taxonomy provided.
    4220                 if ( empty( $term ) )
     4222                if ( is_null( $term ) )
    42214223                        return true;
    42224224
     4225                $term_array = (array) $term;
     4226                if ( empty( $term_array ) )
     4227                        return true;
     4228
    42234229                return isset( $queried_object->term_id ) &&
    42244230                        count( array_intersect(
    42254231                                array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
     
    42654271         *
    42664272         * @since 3.1.0
    42674273         *
    4268          * @param string|array $feeds Optional feed types to check.
     4274         * @param string|array $feeds Optional. Feed types to check. Default null.
    42694275         * @return bool
    42704276         */
    4271         public function is_feed( $feeds = '' ) {
    4272                 if ( empty( $feeds ) || ! $this->is_feed )
     4277        public function is_feed( $feeds = null ) {
     4278                if ( is_null( $feeds ) || ! $this->is_feed )
    42734279                        return (bool) $this->is_feed;
     4280
     4281                $feeds = (array) $feeds;
     4282
     4283                if ( empty( $feeds ) )
     4284                        return true;
     4285
    42744286                $qv = $this->get( 'feed' );
    42754287                if ( 'feed' == $qv )
    42764288                        $qv = get_default_feed();
    4277                 return in_array( $qv, (array) $feeds );
     4289
     4290                return in_array( $qv, $feeds, true );
    42784291        }
    42794292
    42804293        /**
     
    43564369         *
    43574370         * @since 3.1.0
    43584371         *
    4359          * @param mixed $page Page ID, title, slug, path, or array of such.
     4372         * @param mixed $page Optional. Page ID, title, slug, path, or array of such. Default null.
    43604373         * @return bool
    43614374         */
    4362         public function is_page( $page = '' ) {
    4363                 if ( !$this->is_page )
    4364                         return false;
     4375        public function is_page( $page = null ) {
     4376                if ( is_null( $page ) || !$this->is_page )
     4377                        return (bool) $this->is_page;
    43654378
     4379                $page = (array) $page;
     4380
    43664381                if ( empty( $page ) )
    43674382                        return true;
    43684383
    43694384                $page_obj = $this->get_queried_object();
    43704385
    4371                 $page = (array) $page;
    4372 
    43734386                if ( in_array( (string) $page_obj->ID, $page ) ) {
    43744387                        return true;
    4375                 } elseif ( in_array( $page_obj->post_title, $page ) ) {
     4388                } elseif ( in_array( $page_obj->post_title, $page, true ) ) {
    43764389                        return true;
    4377                 } elseif ( in_array( $page_obj->post_name, $page ) ) {
     4390                } elseif ( in_array( $page_obj->post_name, $page, true ) ) {
    43784391                        return true;
    43794392                } else {
    43804393                        foreach ( $page as $pagepath ) {
     
    44494462         *
    44504463         * @since 3.1.0
    44514464         *
    4452          * @param mixed $post Post ID, title, slug, path, or array of such.
     4465         * @param mixed $post Optional. Post ID, title, slug, path, or array of such. Default null.
    44534466         * @return bool
    44544467         */
    4455         public function is_single( $post = '' ) {
    4456                 if ( !$this->is_single )
    4457                         return false;
     4468        public function is_single( $post = null ) {
     4469                if ( is_null( $post ) || !$this->is_single )
     4470                        return (bool) $this->is_single;
    44584471
    4459                 if ( empty($post) )
     4472                $post = (array) $post;
     4473
     4474                if ( empty( $post ) )
    44604475                        return true;
    44614476
    44624477                $post_obj = $this->get_queried_object();
    44634478
    4464                 $post = (array) $post;
    4465 
    44664479                if ( in_array( (string) $post_obj->ID, $post ) ) {
    44674480                        return true;
    4468                 } elseif ( in_array( $post_obj->post_title, $post ) ) {
     4481                } elseif ( in_array( $post_obj->post_title, $post, true ) ) {
    44694482                        return true;
    4470                 } elseif ( in_array( $post_obj->post_name, $post ) ) {
     4483                } elseif ( in_array( $post_obj->post_name, $post, true ) ) {
    44714484                        return true;
    44724485                } else {
    44734486                        foreach ( $post as $postpath ) {
     
    44954508         *
    44964509         * @since 3.1.0
    44974510         *
    4498          * @param mixed $post_types Optional. Post Type or array of Post Types
     4511         * @param mixed $post_types Optional. Post Type or array of Post Types. Default null.
    44994512         * @return bool
    45004513         */
    4501         public function is_singular( $post_types = '' ) {
    4502                 if ( empty( $post_types ) || !$this->is_singular )
     4514        public function is_singular( $post_types = null ) {
     4515                if ( is_null( $post_types ) || !$this->is_singular )
    45034516                        return (bool) $this->is_singular;
    45044517
     4518                $post_types = (array) $post_types;
     4519
     4520                if ( empty( $post_types ) )
     4521                        return true;
     4522
    45054523                $post_obj = $this->get_queried_object();
    45064524
    4507                 return in_array( $post_obj->post_type, (array) $post_types );
     4525                return in_array( $post_obj->post_type, $post_types, true );
    45084526        }
    45094527
    45104528        /**
  • tests/phpunit/tests/query/conditionals.php

     
    405405                $this->assertTrue( is_tag( array( $tag->name ) ) );
    406406                $this->assertTrue( is_tag( array( $tag->slug ) ) );
    407407                $this->assertTrue( is_tag( array( $tag->term_id ) ) );
     408                $this->assertTrue( is_tag( new StdClass ) );
    408409        }
    409410
    410411        // 'author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?author_name=$matches[1]&feed=$matches[2]',
     
    988989                $this->assertFalse( is_page_template( array( 'test.php' ) ) );
    989990                $this->assertTrue( is_page_template( array('test.php', 'example.php') ) );
    990991        }
     992
     993        /**
     994         * @ticket 31723
     995         */
     996        function test_is__functions_bad_input() {
     997                // all these functions are structured the same way
     998                $functions = array(
     999                        'page',
     1000                        'tag',
     1001                        'single',
     1002                        'feed',
     1003                        'singular',
     1004                        'post_type_archive',
     1005                        'attachment',
     1006                        'author',
     1007                        'category',
     1008                );
     1009
     1010                foreach ( $functions as $function ) {
     1011                        $this->assertFalse( call_user_func( 'is_' . $function, '' ) );
     1012                        $this->assertFalse( call_user_func( 'is_' . $function, false ) );
     1013                        $this->assertFalse( call_user_func( 'is_' . $function, -1 ) );
     1014                        $this->assertFalse( call_user_func( 'is_' . $function, 0.1 ) );
     1015                        $this->assertFalse( call_user_func( 'is_' . $function, array( array() ) ) );
     1016                }
     1017
     1018        }
    9911019}