Skip to content

Instantly share code, notes, and snippets.

@dcavins
Last active June 17, 2021 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcavins/da1f817d4ebb319af073587c727bf6ab to your computer and use it in GitHub Desktop.
Save dcavins/da1f817d4ebb319af073587c727bf6ab to your computer and use it in GitHub Desktop.
Change BP Docs capabilities so that group admins and mods can read and edit any doc associated with the group.
<?php
/**
* Allow group admins and mods to read and edit any BP Doc
* associated with a group they moderate.
* Note: this will allow direct access at `/docs/doc-title/` but
* the docs will not be listed in the group docs directory if the
* access level "should" prevent the user from accessing it.
* So it's of limited usefulness.
*
* @param string[] $caps Primitive capabilities required of the user.
* @param string $cap Capability being checked.
* @param int $user_id The user ID.
* @param array $args Adds context to the capability check, typically
* starting with an object ID.
*/
function dc_custom_groups_map_meta_caps( $caps, $cap, $user_id, $args ) {
switch ( $cap ) {
case 'bp_docs_read' :
case 'bp_docs_edit' :
case 'bp_docs_view_history' :
case 'bp_docs_manage' :
case 'bp_docs_read_comments' :
case 'bp_docs_post_comments' :
$doc = bp_docs_get_doc_for_caps( $args );
if ( empty( $doc ) ) {
break;
}
$group_id = bp_docs_get_associated_group_id( $doc->ID, $doc );
// If not associated with a group, nothing to do here
if ( ! $group_id ) {
break;
}
if ( groups_is_user_admin( $user_id, $group_id ) || groups_is_user_mod( $user_id, $group_id ) ) {
$caps = array( 'exist' );
}
}
return $caps;
}
add_filter( 'bp_docs_map_meta_caps', 'dc_custom_groups_map_meta_caps', 15, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment