From 6f15251aa487aef1aa825d48e870cd7db0f311fc Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 4 Feb 2020 22:13:15 +0000 Subject: [PATCH] Posts, Post Types: Fail gracefully when checking mapped cap against unregistered post status. With `map_meta_cap` enabled for a post type, the `read_post` capability for posts with a public status is supposed to be mapped to the post type's `read` capability. When a post is left in the database after the post status is no longer present, and WP does a `read_post` check against it, a PHP notice was thrown, and the cap check always failed. As a more graceful fallback, the cap is now mapped onto `edit_others_posts`, which allows highly privileged users to be able to access orphaned content. A `_doing_it_wrong()` notice is also added, so that developers and site administrators are aware that the cap mapping is failing in the absence of the registered post status. Follow-up to [34091], which introduced a similar approach to checking mapped caps against an unregistered post type. Props roytanck, SergeyBiryukov. Fixes #48653. git-svn-id: https://develop.svn.wordpress.org/trunk@47178 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/capabilities.php | 7 +++++++ tests/phpunit/tests/user/capabilities.php | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/wp-includes/capabilities.php b/src/wp-includes/capabilities.php index 38287dc7c3..f05c504cc1 100644 --- a/src/wp-includes/capabilities.php +++ b/src/wp-includes/capabilities.php @@ -241,6 +241,13 @@ function map_meta_cap( $cap, $user_id, ...$args ) { } $status_obj = get_post_status_object( $post->post_status ); + if ( ! $status_obj ) { + /* translators: 1: Post status, 2: Capability name. */ + _doing_it_wrong( __FUNCTION__, sprintf( __( 'The post status %1$s is not registered, so it may not be reliable to check the capability "%2$s" against a post with that status.' ), $post->post_status, $cap ), '5.4.0' ); + $caps[] = 'edit_others_posts'; + break; + } + if ( $status_obj->public ) { $caps[] = $post_type->cap->read; break; diff --git a/tests/phpunit/tests/user/capabilities.php b/tests/phpunit/tests/user/capabilities.php index 4e4ea12035..a5b7f3c55d 100644 --- a/tests/phpunit/tests/user/capabilities.php +++ b/tests/phpunit/tests/user/capabilities.php @@ -1773,6 +1773,29 @@ class Tests_User_Capabilities extends WP_UnitTestCase { } } + /** + * @ticket 48653 + * @expectedIncorrectUsage map_meta_cap + */ + function test_require_edit_others_posts_if_post_status_doesnt_exist() { + register_post_status( 'existed' ); + $post_id = self::factory()->post->create( array( 'post_status' => 'existed' ) ); + _unregister_post_status( 'existed' ); + + $subscriber_id = self::$users['subscriber']->ID; + $editor_id = self::$users['editor']->ID; + + foreach ( array( 'read_post', 'read_page' ) as $cap ) { + wp_set_current_user( $subscriber_id ); + $this->assertSame( array( 'edit_others_posts' ), map_meta_cap( $cap, $subscriber_id, $post_id ) ); + $this->assertFalse( current_user_can( $cap, $post_id ) ); + + wp_set_current_user( $editor_id ); + $this->assertSame( array( 'edit_others_posts' ), map_meta_cap( $cap, $editor_id, $post_id ) ); + $this->assertTrue( current_user_can( $cap, $post_id ) ); + } + } + /** * @ticket 17253 */