Ticket #16802: 16802.2.diff
| File 16802.2.diff, 5.2 KB (added by , 12 years ago) |
|---|
-
src/wp-includes/query.php
diff --git src/wp-includes/query.php src/wp-includes/query.php index cf7b80f..971e75f 100644
class WP_Query { 4311 4311 * 4312 4312 * @since 3.1.0 4313 4313 * 4314 * @param mixed $page Page ID, title, slug, or array of such.4314 * @param mixed $page Page ID, title, slug, path, or array of such. 4315 4315 * @return bool 4316 4316 */ 4317 4317 public function is_page( $page = '' ) { … … class WP_Query { 4325 4325 4326 4326 $page = (array) $page; 4327 4327 4328 if ( in_array( $page_obj->ID, $page ) ) 4328 if ( in_array( $page_obj->ID, $page ) ) { 4329 4329 return true; 4330 elseif ( in_array( $page_obj->post_title, $page ) )4330 } elseif ( in_array( $page_obj->post_title, $page ) ) { 4331 4331 return true; 4332 else if ( in_array( $page_obj->post_name, $page ) )4332 } else if ( in_array( $page_obj->post_name, $page ) ) { 4333 4333 return true; 4334 } else { 4335 foreach ( $page as $pagepath ) { 4336 if ( ! strpos( $pagepath, '/' ) ) { 4337 continue; 4338 } 4339 $pagepath_obj = get_page_by_path( $pagepath ); 4340 4341 if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) { 4342 return true; 4343 } 4344 } 4345 } 4334 4346 4335 4347 return false; 4336 4348 } … … class WP_Query { 4392 4404 * 4393 4405 * @since 3.1.0 4394 4406 * 4395 * @param mixed $post Post ID, title, slug, or array of such.4407 * @param mixed $post Post ID, title, slug, path, or array of such. 4396 4408 * @return bool 4397 4409 */ 4398 4410 public function is_single( $post = '' ) { … … class WP_Query { 4406 4418 4407 4419 $post = (array) $post; 4408 4420 4409 if ( in_array( $post_obj->ID, $post ) ) 4421 if ( in_array( $post_obj->ID, $post ) ) { 4410 4422 return true; 4411 elseif ( in_array( $post_obj->post_title, $post ) )4423 } elseif ( in_array( $post_obj->post_title, $post ) ) { 4412 4424 return true; 4413 elseif ( in_array( $post_obj->post_name, $post ) )4425 } elseif ( in_array( $post_obj->post_name, $post ) ) { 4414 4426 return true; 4427 } else { 4428 foreach ( $post as $postpath ) { 4429 if ( ! strpos( $postpath, '/' ) ) { 4430 continue; 4431 } 4432 $postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type ); 4415 4433 4434 if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) { 4435 return true; 4436 } 4437 } 4438 } 4416 4439 return false; 4417 4440 } 4418 4441 -
tests/phpunit/tests/query/conditionals.php
diff --git tests/phpunit/tests/query/conditionals.php tests/phpunit/tests/query/conditionals.php index 1af50ac..ded9ed7 100644
class Tests_Query_Conditionals extends WP_UnitTestCase { 692 692 $this->assertTrue( is_single( $post->post_name ) ); 693 693 } 694 694 695 function test_is_single_with_parent() { 696 // Use custom hierarchical post type 697 $post_type = rand_str( 20 ); 698 699 register_post_type( $post_type, array( 700 'hierarchical' => true, 701 'rewrite' => true, 702 'has_archive' => true, 703 'public' => true 704 ) ); 705 706 // Create parent and child posts 707 $parent_id = $this->factory->post->create( array( 708 'post_type' => $post_type, 709 'post_name' => 'foo' 710 ) ); 711 712 $post_id = $this->factory->post->create( array( 713 'post_type' => $post_type, 714 'post_name' => 'bar', 715 'post_parent' => $parent_id 716 ) ); 717 718 // Tests 719 $this->go_to( "/?p=$post_id&post_type=$post_type" ); 720 721 $post = get_queried_object(); 722 $q = $GLOBALS['wp_query']; 723 724 $this->assertTrue( is_single() ); 725 $this->assertFalse( $q->is_page ); 726 $this->assertTrue( $q->is_single ); 727 $this->assertFalse( $q->is_attachment ); 728 $this->assertTrue( is_single( $post ) ); 729 $this->assertTrue( is_single( $post->ID ) ); 730 $this->assertTrue( is_single( $post->post_title ) ); 731 $this->assertTrue( is_single( $post->post_name ) ); 732 $this->assertTrue( is_single( 'foo/bar' ) ); 733 $this->assertFalse( is_single( $parent_id ) ); 734 $this->assertFalse( is_single( 'foo/bar/baz' ) ); 735 $this->assertFalse( is_single( 'bar/bar' ) ); 736 $this->assertFalse( is_single( 'foo' ) ); 737 } 738 695 739 function test_is_page() { 696 740 $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) ); 697 741 $this->go_to( "/?page_id=$post_id" ); … … class Tests_Query_Conditionals extends WP_UnitTestCase { 709 753 $this->assertTrue( is_page( $post->post_name ) ); 710 754 } 711 755 756 function test_is_page_with_parent() { 757 $parent_id = $this->factory->post->create( array( 758 'post_type' => 'page', 759 'post_name' => 'foo', 760 ) ); 761 $post_id = $this->factory->post->create( array( 762 'post_type' => 'page', 763 'post_name' => 'bar', 764 'post_parent' => $parent_id, 765 ) ); 766 $this->go_to( "/?page_id=$post_id" ); 767 768 $post = get_queried_object(); 769 $q = $GLOBALS['wp_query']; 770 771 $this->assertTrue( is_page() ); 772 $this->assertFalse( $q->is_single ); 773 $this->assertTrue( $q->is_page ); 774 $this->assertFalse( $q->is_attachment ); 775 $this->assertTrue( is_page( $post ) ); 776 $this->assertTrue( is_page( $post->ID ) ); 777 $this->assertTrue( is_page( $post->post_title ) ); 778 $this->assertTrue( is_page( $post->post_name ) ); 779 $this->assertTrue( is_page( 'foo/bar' ) ); 780 $this->assertFalse( is_page( $parent_id ) ); 781 $this->assertFalse( is_page( 'foo/bar/baz' ) ); 782 $this->assertFalse( is_page( 'bar/bar' ) ); 783 $this->assertFalse( is_page( 'foo' ) ); 784 } 785 712 786 function test_is_attachment() { 713 787 $post_id = $this->factory->post->create( array( 'post_type' => 'attachment' ) ); 714 788 $this->go_to( "/?attachment_id=$post_id" );
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)