Ticket #31723: 31723.1.diff
File 31723.1.diff, 15.6 KB (added by , 10 years ago) |
---|
-
src/wp-includes/query.php
155 155 * @since 3.1.0 156 156 * @uses $wp_query 157 157 * 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. 159 159 * @return bool 160 160 */ 161 function is_post_type_archive( $post_types = '') {161 function is_post_type_archive( $post_types = null ) { 162 162 global $wp_query; 163 163 164 164 if ( ! isset( $wp_query ) ) { … … 176 176 * @since 2.0.0 177 177 * @uses $wp_query 178 178 * 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. 180 180 * @return bool 181 181 */ 182 function is_attachment( $attachment = '') {182 function is_attachment( $attachment = null ) { 183 183 global $wp_query; 184 184 185 185 if ( ! isset( $wp_query ) ) { … … 200 200 * @since 1.5.0 201 201 * @uses $wp_query 202 202 * 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. 204 204 * @return bool 205 205 */ 206 function is_author( $author = '') {206 function is_author( $author = null ) { 207 207 global $wp_query; 208 208 209 209 if ( ! isset( $wp_query ) ) { … … 224 224 * @since 1.5.0 225 225 * @uses $wp_query 226 226 * 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. 228 228 * @return bool 229 229 */ 230 function is_category( $category = '') {230 function is_category( $category = null ) { 231 231 global $wp_query; 232 232 233 233 if ( ! isset( $wp_query ) ) { … … 248 248 * @since 2.3.0 249 249 * @uses $wp_query 250 250 * 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. 252 252 * @return bool 253 253 */ 254 function is_tag( $tag = '') {254 function is_tag( $tag = null ) { 255 255 global $wp_query; 256 256 257 257 if ( ! isset( $wp_query ) ) { … … 276 276 * @since 2.5.0 277 277 * @uses $wp_query 278 278 * 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. 281 281 * @return bool 282 282 */ 283 function is_tax( $taxonomy = '', $term = '') {283 function is_tax( $taxonomy = null, $term = null ) { 284 284 global $wp_query; 285 285 286 286 if ( ! isset( $wp_query ) ) { … … 361 361 * @param string|array $feeds Optional feed types to check. 362 362 * @return bool 363 363 */ 364 function is_feed( $feeds = '') {364 function is_feed( $feeds = null ) { 365 365 global $wp_query; 366 366 367 367 if ( ! isset( $wp_query ) ) { … … 482 482 * @since 1.5.0 483 483 * @uses $wp_query 484 484 * 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. 486 486 * @return bool 487 487 */ 488 function is_page( $page = '') {488 function is_page( $page = null ) { 489 489 global $wp_query; 490 490 491 491 if ( ! isset( $wp_query ) ) { … … 591 591 * @since 1.5.0 592 592 * @uses $wp_query 593 593 * 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. 595 595 * @return bool 596 596 */ 597 function is_single( $post = '') {597 function is_single( $post = null ) { 598 598 global $wp_query; 599 599 600 600 if ( ! isset( $wp_query ) ) { … … 618 618 * @since 1.5.0 619 619 * @uses $wp_query 620 620 * 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. 622 622 * @return bool 623 623 */ 624 function is_singular( $post_types = '') {624 function is_singular( $post_types = null ) { 625 625 global $wp_query; 626 626 627 627 if ( ! isset( $wp_query ) ) { … … 4041 4041 * 4042 4042 * @since 3.1.0 4043 4043 * 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. 4045 4045 * @return bool 4046 4046 */ 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 ) 4049 4049 return (bool) $this->is_post_type_archive; 4050 4050 4051 $post_types = (array) $post_types; 4052 4053 if ( empty( $post_types ) ) 4054 return true; 4055 4051 4056 $post_type = $this->get( 'post_type' ); 4052 4057 if ( is_array( $post_type ) ) 4053 4058 $post_type = reset( $post_type ); 4054 4059 $post_type_object = get_post_type_object( $post_type ); 4055 4060 4056 return in_array( $post_type_object->name, (array) $post_types);4061 return in_array( $post_type_object->name, $post_types, true ); 4057 4062 } 4058 4063 4059 4064 /** … … 4061 4066 * 4062 4067 * @since 3.1.0 4063 4068 * 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. 4065 4070 * @return bool 4066 4071 */ 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; 4070 4075 } 4071 4076 4072 if ( empty( $attachment ) ) { 4077 $attachment = (array) $attachment; 4078 4079 if ( empty( $attachment ) ) 4073 4080 return true; 4074 }4075 4081 4076 $attachment = (array) $attachment;4077 4078 4082 $post_obj = $this->get_queried_object(); 4079 4083 4080 4084 if ( in_array( (string) $post_obj->ID, $attachment ) ) { 4081 4085 return true; 4082 } elseif ( in_array( $post_obj->post_title, $attachment ) ) {4086 } elseif ( in_array( $post_obj->post_title, $attachment, true ) ) { 4083 4087 return true; 4084 } elseif ( in_array( $post_obj->post_name, $attachment ) ) {4088 } elseif ( in_array( $post_obj->post_name, $attachment, true ) ) { 4085 4089 return true; 4086 4090 } 4087 4091 return false; … … 4095 4099 * 4096 4100 * @since 3.1.0 4097 4101 * 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. 4099 4103 * @return bool 4100 4104 */ 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; 4104 4108 4105 if ( empty($author) ) 4109 $author = (array) $author; 4110 4111 if ( empty( $author ) ) 4106 4112 return true; 4107 4113 4108 4114 $author_obj = $this->get_queried_object(); … … 4111 4117 4112 4118 if ( in_array( (string) $author_obj->ID, $author ) ) 4113 4119 return true; 4114 elseif ( in_array( $author_obj->nickname, $author ) )4120 elseif ( in_array( $author_obj->nickname, $author, true ) ) 4115 4121 return true; 4116 elseif ( in_array( $author_obj->user_nicename, $author ) )4122 elseif ( in_array( $author_obj->user_nicename, $author, true ) ) 4117 4123 return true; 4118 4124 4119 4125 return false; … … 4127 4133 * 4128 4134 * @since 3.1.0 4129 4135 * 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. 4131 4137 * @return bool 4132 4138 */ 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; 4136 4142 4137 if ( empty($category) ) 4143 $category = (array) $category; 4144 4145 if ( empty( $category ) ) 4138 4146 return true; 4139 4147 4140 4148 $cat_obj = $this->get_queried_object(); 4141 4149 4142 $category = (array) $category;4143 4144 4150 if ( in_array( (string) $cat_obj->term_id, $category ) ) 4145 4151 return true; 4146 elseif ( in_array( $cat_obj->name, $category ) )4152 elseif ( in_array( $cat_obj->name, $category, true ) ) 4147 4153 return true; 4148 elseif ( in_array( $cat_obj->slug, $category ) )4154 elseif ( in_array( $cat_obj->slug, $category, true ) ) 4149 4155 return true; 4150 4156 4151 4157 return false; … … 4159 4165 * 4160 4166 * @since 3.1.0 4161 4167 * 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. 4163 4169 * @return bool 4164 4170 */ 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; 4168 4174 4175 $tag = (array) $tag; 4176 4169 4177 if ( empty( $tag ) ) 4170 4178 return true; 4171 4179 4172 4180 $tag_obj = $this->get_queried_object(); 4173 4181 4174 $tag = (array) $tag;4175 4176 4182 if ( in_array( (string) $tag_obj->term_id, $tag ) ) 4177 4183 return true; 4178 elseif ( in_array( $tag_obj->name, $tag ) )4184 elseif ( in_array( $tag_obj->name, $tag, true ) ) 4179 4185 return true; 4180 elseif ( in_array( $tag_obj->slug, $tag ) )4186 elseif ( in_array( $tag_obj->slug, $tag, true ) ) 4181 4187 return true; 4182 4188 4183 4189 return false; … … 4195 4201 * 4196 4202 * @since 3.1.0 4197 4203 * 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. 4200 4206 * @return bool 4201 4207 */ 4202 public function is_tax( $taxonomy = '', $term = '') {4208 public function is_tax( $taxonomy = null, $term = null ) { 4203 4209 global $wp_taxonomies; 4204 4210 4205 if ( !$this->is_tax )4206 return false;4211 if ( is_null( $taxonomy ) || !$this->is_tax ) 4212 return (bool) $this->is_tax; 4207 4213 4208 if ( empty( $taxonomy ) )4209 return true;4210 4211 4214 $queried_object = $this->get_queried_object(); 4212 4215 $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy ); 4213 $term_array = (array) $term;4214 4216 4215 4217 // Check that the taxonomy matches. 4216 4218 if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) ) 4217 4219 return false; 4218 4220 4219 4221 // Only a Taxonomy provided. 4220 if ( empty( $term ) )4222 if ( is_null( $term ) ) 4221 4223 return true; 4222 4224 4225 $term_array = (array) $term; 4226 if ( empty( $term_array ) ) 4227 return true; 4228 4223 4229 return isset( $queried_object->term_id ) && 4224 4230 count( array_intersect( 4225 4231 array( $queried_object->term_id, $queried_object->name, $queried_object->slug ), … … 4265 4271 * 4266 4272 * @since 3.1.0 4267 4273 * 4268 * @param string|array $feeds Optional feed types to check.4274 * @param string|array $feeds Optional. Feed types to check. Default null. 4269 4275 * @return bool 4270 4276 */ 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 ) 4273 4279 return (bool) $this->is_feed; 4280 4281 $feeds = (array) $feeds; 4282 4283 if ( empty( $feeds ) ) 4284 return true; 4285 4274 4286 $qv = $this->get( 'feed' ); 4275 4287 if ( 'feed' == $qv ) 4276 4288 $qv = get_default_feed(); 4277 return in_array( $qv, (array) $feeds ); 4289 4290 return in_array( $qv, $feeds, true ); 4278 4291 } 4279 4292 4280 4293 /** … … 4356 4369 * 4357 4370 * @since 3.1.0 4358 4371 * 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. 4360 4373 * @return bool 4361 4374 */ 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; 4365 4378 4379 $page = (array) $page; 4380 4366 4381 if ( empty( $page ) ) 4367 4382 return true; 4368 4383 4369 4384 $page_obj = $this->get_queried_object(); 4370 4385 4371 $page = (array) $page;4372 4373 4386 if ( in_array( (string) $page_obj->ID, $page ) ) { 4374 4387 return true; 4375 } elseif ( in_array( $page_obj->post_title, $page ) ) {4388 } elseif ( in_array( $page_obj->post_title, $page, true ) ) { 4376 4389 return true; 4377 } elseif ( in_array( $page_obj->post_name, $page ) ) {4390 } elseif ( in_array( $page_obj->post_name, $page, true ) ) { 4378 4391 return true; 4379 4392 } else { 4380 4393 foreach ( $page as $pagepath ) { … … 4449 4462 * 4450 4463 * @since 3.1.0 4451 4464 * 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. 4453 4466 * @return bool 4454 4467 */ 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; 4458 4471 4459 if ( empty($post) ) 4472 $post = (array) $post; 4473 4474 if ( empty( $post ) ) 4460 4475 return true; 4461 4476 4462 4477 $post_obj = $this->get_queried_object(); 4463 4478 4464 $post = (array) $post;4465 4466 4479 if ( in_array( (string) $post_obj->ID, $post ) ) { 4467 4480 return true; 4468 } elseif ( in_array( $post_obj->post_title, $post ) ) {4481 } elseif ( in_array( $post_obj->post_title, $post, true ) ) { 4469 4482 return true; 4470 } elseif ( in_array( $post_obj->post_name, $post ) ) {4483 } elseif ( in_array( $post_obj->post_name, $post, true ) ) { 4471 4484 return true; 4472 4485 } else { 4473 4486 foreach ( $post as $postpath ) { … … 4495 4508 * 4496 4509 * @since 3.1.0 4497 4510 * 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. 4499 4512 * @return bool 4500 4513 */ 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 ) 4503 4516 return (bool) $this->is_singular; 4504 4517 4518 $post_types = (array) $post_types; 4519 4520 if ( empty( $post_types ) ) 4521 return true; 4522 4505 4523 $post_obj = $this->get_queried_object(); 4506 4524 4507 return in_array( $post_obj->post_type, (array) $post_types);4525 return in_array( $post_obj->post_type, $post_types, true ); 4508 4526 } 4509 4527 4510 4528 /** -
tests/phpunit/tests/query/conditionals.php
405 405 $this->assertTrue( is_tag( array( $tag->name ) ) ); 406 406 $this->assertTrue( is_tag( array( $tag->slug ) ) ); 407 407 $this->assertTrue( is_tag( array( $tag->term_id ) ) ); 408 $this->assertTrue( is_tag( new StdClass ) ); 408 409 } 409 410 410 411 // 'author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?author_name=$matches[1]&feed=$matches[2]', … … 988 989 $this->assertFalse( is_page_template( array( 'test.php' ) ) ); 989 990 $this->assertTrue( is_page_template( array('test.php', 'example.php') ) ); 990 991 } 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 } 991 1019 }