mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-01 19:24:34 +00:00
Posts, Post Types: Introduce new functions for determining if a post has a parent (has_post_parent()) and to fetch the post parent (get_post_parent()).
These functions are simple but reduce the logic needed in themes and plugins. Props ramiy, sebastian.pisula, birgire, audrasjb, xkon Fixes #33045 git-svn-id: https://develop.svn.wordpress.org/trunk@50127 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -452,4 +452,65 @@ NO;
|
||||
$this->assertRegExp( '/><li.*>|<\/li></U', $menu );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 33045
|
||||
*/
|
||||
public function test_get_parent_post() {
|
||||
$post = array(
|
||||
'post_status' => 'publish',
|
||||
'post_type' => 'page',
|
||||
);
|
||||
|
||||
// Insert two initial posts.
|
||||
$parent_id = self::factory()->post->create( $post );
|
||||
$child_id = self::factory()->post->create( $post );
|
||||
|
||||
// Test if child get_parent_post() post returns Null by default.
|
||||
$parent = get_parent_post( $child_id );
|
||||
$this->assertNull( $parent );
|
||||
|
||||
// Update child post with a parent.
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => $child_id,
|
||||
'post_parent' => $parent_id,
|
||||
)
|
||||
);
|
||||
|
||||
// Test if child get_parent_post() post returns the parent object.
|
||||
$parent = get_parent_post( $child_id );
|
||||
$this->assertNotNull( $parent );
|
||||
$this->assertSame( $parent_id, $parent->ID );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 33045
|
||||
*/
|
||||
public function test_has_parent_post() {
|
||||
$post = array(
|
||||
'post_status' => 'publish',
|
||||
'post_type' => 'page',
|
||||
);
|
||||
|
||||
// Insert two initial posts.
|
||||
$parent_id = self::factory()->post->create( $post );
|
||||
$child_id = self::factory()->post->create( $post );
|
||||
|
||||
// Test if child has_parent_post() post returns False by default.
|
||||
$parent = has_parent_post( $child_id );
|
||||
$this->assertFalse( $parent );
|
||||
|
||||
// Update child post with a parent.
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => $child_id,
|
||||
'post_parent' => $parent_id,
|
||||
)
|
||||
);
|
||||
|
||||
// Test if child has_parent_post() returns True.
|
||||
$parent = has_parent_post( $child_id );
|
||||
$this->assertTrue( $parent );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user