Docs: Add missing parameter description in wp_get_post_parent_id()
function DocBlock.
Rename `$post_ID` to `$post` for consistency with other functions. Add unit tests for `wp_get_post_parent_id()`. Props keesiemeijer. Fixes #42797. git-svn-id: https://develop.svn.wordpress.org/trunk@42397 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
14fafe960b
commit
4fbe6e0801
@ -6251,16 +6251,15 @@ function _publish_post_hook( $post_id ) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the post's parent's post_ID
|
||||
* Return the post's parent post ID.
|
||||
*
|
||||
* @since 3.1.0
|
||||
*
|
||||
* @param int $post_ID
|
||||
*
|
||||
* @param int|WP_Post $post Post ID or post object. Defaults to global $post.
|
||||
* @return int|false Post parent ID, otherwise false.
|
||||
*/
|
||||
function wp_get_post_parent_id( $post_ID ) {
|
||||
$post = get_post( $post_ID );
|
||||
function wp_get_post_parent_id( $post ) {
|
||||
$post = get_post( $post );
|
||||
if ( ! $post || is_wp_error( $post ) ) {
|
||||
return false;
|
||||
}
|
||||
|
42
tests/phpunit/tests/post/wpGetPostParentId.php
Normal file
42
tests/phpunit/tests/post/wpGetPostParentId.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group post
|
||||
*/
|
||||
class Tests_Post_WpGetPostParentId extends WP_UnitTestCase {
|
||||
|
||||
public function test_wp_get_post_parent_id_with_post_object() {
|
||||
$p1 = self::factory()->post->create();
|
||||
$p2 = self::factory()->post->create( array( 'post_parent' => $p1 ) );
|
||||
$post = get_post( $p2 );
|
||||
$this->assertTrue( $post instanceof WP_Post );
|
||||
$this->assertEquals( $p1, wp_get_post_parent_id( $post ) );
|
||||
}
|
||||
|
||||
public function test_wp_get_post_parent_id_with_post_id() {
|
||||
$p1 = self::factory()->post->create();
|
||||
$p2 = self::factory()->post->create( array( 'post_parent' => $p1 ) );
|
||||
$this->assertEquals( $p1, wp_get_post_parent_id( $p2 ) );
|
||||
}
|
||||
|
||||
public function test_wp_get_post_parent_id_with_non_existing_id_default_to_global_post_id() {
|
||||
$p1 = self::factory()->post->create();
|
||||
$GLOBALS['post'] = self::factory()->post->create( array( 'post_parent' => $p1 ) );
|
||||
$this->assertEquals( $p1, wp_get_post_parent_id( 0 ) );
|
||||
unset( $GLOBALS['post'] );
|
||||
}
|
||||
|
||||
public function test_wp_get_post_parent_id_with_boolean_default_to_global_post_id() {
|
||||
$p1 = self::factory()->post->create();
|
||||
$GLOBALS['post'] = self::factory()->post->create( array( 'post_parent' => $p1 ) );
|
||||
$this->assertEquals( $p1, wp_get_post_parent_id( false ) );
|
||||
unset( $GLOBALS['post'] );
|
||||
}
|
||||
|
||||
public function test_wp_get_post_parent_id_with_string_default_to_false() {
|
||||
$p1 = self::factory()->post->create();
|
||||
$GLOBALS['post'] = self::factory()->post->create( array( 'post_parent' => $p1 ) );
|
||||
$this->assertFalse( wp_get_post_parent_id( 'string' ) );
|
||||
unset( $GLOBALS['post'] );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user