From 6cbed78c94b9d8c6a9b4c8b472b88ee0cd56528c Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 24 Jul 2022 13:24:44 +0000 Subject: [PATCH] Posts, Post Types: Check if the post type exists in `wp_insert_post()`. This avoids an `Attempt to read property "cap" on null` PHP warning when checking an unregistered post type's `publish_posts` capability to disallow contributors setting the post slug for pending posts. Follow-up to [9055], [42380]. Props Chouby, mukesh27, rafiahmedd, SergeyBiryukov. Fixes #55877. git-svn-id: https://develop.svn.wordpress.org/trunk@53771 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 12 +++++++----- tests/phpunit/tests/post.php | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index c7dcc13b8d..df509fd114 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -4159,12 +4159,14 @@ function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true ) * * For new posts check the primitive capability, for updates check the meta capability. */ - $post_type_object = get_post_type_object( $post_type ); + if ( 'pending' === $post_status ) { + $post_type_object = get_post_type_object( $post_type ); - if ( ! $update && 'pending' === $post_status && ! current_user_can( $post_type_object->cap->publish_posts ) ) { - $post_name = ''; - } elseif ( $update && 'pending' === $post_status && ! current_user_can( 'publish_post', $post_ID ) ) { - $post_name = ''; + if ( ! $update && $post_type_object && ! current_user_can( $post_type_object->cap->publish_posts ) ) { + $post_name = ''; + } elseif ( $update && ! current_user_can( 'publish_post', $post_ID ) ) { + $post_name = ''; + } } /* diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 108e6ef4a6..e672dafdd7 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -501,6 +501,23 @@ class Tests_Post extends WP_UnitTestCase { $this->assertStringContainsString( 'wptests_pt=' . $p, $post->guid ); } + /** + * @ticket 55877 + * @covers ::wp_insert_post + */ + public function test_wp_insert_post_should_not_trigger_warning_for_pending_posts_with_unknown_cpt() { + $post_id = wp_insert_post( + array( + 'post_title' => 'title', + 'post_type' => 'unknown', + 'post_status' => 'pending', + ) + ); + + $this->assertIsNumeric( $post_id ); + $this->assertGreaterThan( 0, $post_id ); + } + /** * @ticket 20451 */