From 729bb10f31d67430b0efbf366c5c8bd2cb18c237 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Mon, 1 Feb 2021 21:20:44 +0000 Subject: [PATCH] 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 --- src/wp-includes/post-template.php | 25 +++++++++++ tests/phpunit/tests/post/template.php | 61 +++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index 0ad352c328..cdc2341c04 100644 --- a/src/wp-includes/post-template.php +++ b/src/wp-includes/post-template.php @@ -1963,3 +1963,28 @@ function wp_list_post_revisions( $post_id = 0, $type = 'all' ) { echo $rows; echo ''; } + +/** + * Retrieves the parent post object for the given post. + * + * @since 5.7.0 + * + * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post. + * @return WP_Post|null Parent post object, or null if there isn't one. + */ +function get_parent_post( $post = null ) { + $wp_post = get_post( $post ); + return ! empty( $wp_post->post_parent ) ? get_post( $wp_post->post_parent ) : null; +} + +/** + * Returns whether the given post has a parent post. + * + * @since 5.7.0 + * + * @param int|WP_Post|null $post Optional. Post ID or WP_Post object. Default is global $post. + * @return bool Whether the post has a parent post. + */ +function has_parent_post( $post = null ) { + return (bool) get_parent_post( $post ); +} diff --git a/tests/phpunit/tests/post/template.php b/tests/phpunit/tests/post/template.php index f1bcaebcf0..a9d3fc1704 100644 --- a/tests/phpunit/tests/post/template.php +++ b/tests/phpunit/tests/post/template.php @@ -452,4 +452,65 @@ NO; $this->assertRegExp( '/>|<\/li> '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 ); + } }