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
This commit is contained in:
parent
0bfacbe3b2
commit
6f15251aa4
@ -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;
|
||||
|
@ -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
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user