-
-
Save danielbachhuber/18850d571c5dce419f8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Filter map meta cap to do whatever custom caps we need | |
*/ | |
public function filter_map_meta_cap( $caps, $cap, $user_id, $args ) { | |
global $pagenow; | |
switch ( $cap ) { | |
case 'edit_post': | |
case 'edit_others_posts': | |
$post_obj = false; | |
if ( 'edit_post' === $cap ) { | |
$post_obj = Post::get_by_post_id( $args[0] ); | |
/* Allow original authors to 'edit_others_posts' when updating | |
* Addresses core bug in _wp_translate_postdata() | |
* | |
* @see https://core.trac.wordpress.org/ticket/30452 | |
*/ | |
} else if ( 'edit_others_posts' === $cap | |
&& 'post.php' === $pagenow | |
&& ! empty( $_POST['post_ID'] ) ) { | |
$post_obj = Post::get_by_post_id( (int) $_POST['post_ID'] ); | |
} | |
if ( ! $post_obj ) { | |
break; | |
} | |
$post_type = get_post_type_object( $post_obj->get_post_type() ); | |
// Allow first authors to always edit the post | |
if ( $post_obj->get_first_author_id() && $user_id == $post_obj->get_first_author_id() ) { | |
// Don't require editing others' posts | |
if ( false !== ( $key = array_search( $post_type->cap->edit_others_posts, $caps ) ) ) { | |
unset( $caps[ $key ] ); | |
} | |
// If the post is published... | |
if ( 'publish' == $post_obj->get_status() ) { | |
$caps[] = $post_type->cap->edit_published_posts; | |
} elseif ( 'trash' == $post_obj->get_status() ) { | |
if ( 'publish' == get_post_meta( $post_obj->get_id(), '_wp_trash_meta_status', true ) ) { | |
$caps[] = $post_type->cap->edit_published_posts; | |
} | |
} else { | |
// If the post is draft... | |
$caps[] = $post_type->cap->edit_posts; | |
} | |
} | |
break; | |
} | |
return $caps; | |
} |
@danielbachhuber @jerclarke I kept hitting this issue and thinking it was my problem. Can't count how much good code I threw away just trying to solve this. Thank you for posting about it all... This was a huge help!
For anyone else banging their head over this, I've modified the gist slightly if it helps get you a step closer: https://gist.github.com/brandonjp/bf8a2ef3cab014b5ae3dba3e510bca2d
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this! Just a note to anyone else who sees this: You'll need WP 4.2.x or higher for this to work with post previews because of an unrelated bug that triggers PHP notices when users without edit_others_posts preview someone else's post (as they usually would in this context).