Skip to content

Instantly share code, notes, and snippets.

@dcavins
Last active February 3, 2020 12:30
Show Gist options
  • Save dcavins/cd0a8f75126ee18eb3867aa0665c5238 to your computer and use it in GitHub Desktop.
Save dcavins/cd0a8f75126ee18eb3867aa0665c5238 to your computer and use it in GitHub Desktop.
Only allow original uploader or site admin to delete attachments.
<?php if ( bp_docs_is_doc_edit() || bp_docs_is_doc_create() ) : ?>
<?php bp_docs_media_buttons( 'doc_content' ) ?>
<?php endif; ?>
<?php
// Maybe you'll want to do something with main doc ID.
$doc_id = get_the_ID();
?>
<ul id="doc-attachments-ul">
<?php foreach ( bp_docs_get_doc_attachments() as $attachment ) : ?>
<?php
$markup = '';
$att_url = bp_docs_get_attachment_url( $attachment->ID );
$att_base = basename( get_attached_file( $attachment->ID ) );
$doc_url = bp_docs_get_doc_link( $attachment->post_parent );
$attachment_ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $att_url );
$attachment_delete_html = '';
// Only allow the original uploader to delete that attachment.
if ( ( current_user_can( 'bp_moderate' ) || bp_loggedin_user_id() == $attachment->post_author ) && ( bp_docs_is_doc_edit() || bp_docs_is_doc_create() ) ) {
$attachment_delete_url = wp_nonce_url( $doc_url, 'bp_docs_delete_attachment_' . $attachment->ID );
$attachment_delete_url = add_query_arg( array(
'delete_attachment' => $attachment->ID,
), $attachment_delete_url );
$attachment_delete_html = sprintf(
'<a href="%s" class="doc-attachment-delete confirm button">%s</a> ',
$attachment_delete_url,
__( 'Delete', 'buddypress' )
);
}
$markup = sprintf(
'<li id="doc-attachment-%d"><span class="doc-attachment-mime-icon doc-attachment-mime-%s"></span><a href="%s" title="%s">%s</a>%s</li>',
$attachment->ID,
$attachment_ext,
$att_url,
esc_attr( $att_base ),
esc_html( $att_base ),
$attachment_delete_html
);
echo $markup;
?>
<?php endforeach; ?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment