Created
March 4, 2015 01:57
-
-
Save dcavins/c91fc6f7d90617b495c6 to your computer and use it in GitHub Desktop.
Unlink BP Docs from Groups
This file contains hidden or 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
diff --git bp-docs.php bp-docs.php | |
index ac74242..e21e733 100644 | |
--- bp-docs.php | |
+++ bp-docs.php | |
@@ -215,6 +215,10 @@ class BP_Docs { | |
// The slug used when deleting a doc | |
if ( !defined( 'BP_DOCS_UNTRASH_SLUG' ) ) | |
define( 'BP_DOCS_UNTRASH_SLUG', 'untrash' ); | |
+ | |
+ // The slug used when removing a doc from a group | |
+ if ( ! defined( 'BP_DOCS_UNLINK_FROM_GROUP_SLUG' ) ) | |
+ define( 'BP_DOCS_UNLINK_FROM_GROUP_SLUG', 'unlink-from-group' ); | |
// The slug used for the Started section of My Docs | |
if ( !defined( 'BP_DOCS_STARTED_SLUG' ) ) | |
@@ -368,6 +372,8 @@ class BP_Docs { | |
add_rewrite_tag( '%%' . BP_DOCS_UNTRASH_SLUG . '%%', '([1]{1,})' ); | |
add_rewrite_tag( '%%' . BP_DOCS_CREATE_SLUG . '%%', '([1]{1,})' ); | |
add_rewrite_tag( '%%' . BP_DOCS_MY_GROUPS_SLUG . '%%', '([1]{1,})' ); | |
+ add_rewrite_tag( '%%' . BP_DOCS_UNLINK_FROM_GROUP_SLUG . '%%', '([1]{1,})' ); | |
+ | |
} | |
/** | |
@@ -409,6 +415,10 @@ class BP_Docs { | |
bp_docs_get_docs_slug() . '/([^/]+)/' . BP_DOCS_UNTRASH_SLUG . '/?$' => | |
'index.php?post_type=' . $this->post_type_name . '&name=' . $wp_rewrite->preg_index( 1 ) . '&' . BP_DOCS_UNTRASH_SLUG . '=1', | |
+ // Unlink from group | |
+ bp_docs_get_docs_slug() . '/([^/]+)/' . BP_DOCS_UNLINK_FROM_GROUP_SLUG . '/?$' => | |
+ 'index.php?post_type=' . $this->post_type_name . '&name=' . $wp_rewrite->preg_index( 1 ) . '&' . BP_DOCS_UNLINK_FROM_GROUP_SLUG . '=1', | |
+ | |
); | |
// Merge Docs rules with existing | |
diff --git includes/component.php includes/component.php | |
index 6c652e6..bd95ad4 100644 | |
--- includes/component.php | |
+++ includes/component.php | |
@@ -527,6 +527,25 @@ class BP_Docs_Component extends BP_Component { | |
bp_core_redirect( bp_docs_get_doc_link( $untrash_doc_id ) ); | |
die(); | |
} | |
+ | |
+ if ( bp_docs_is_doc_read() && ! empty( $_GET[ BP_DOCS_UNLINK_FROM_GROUP_SLUG ] ) && ! empty( $_GET['doc_id'] ) && ! empty( $_GET['group_id'] ) ) { | |
+ check_admin_referer( 'bp_docs_unlink_from_group' ); | |
+ | |
+ $unlink_doc_id = absint( $_GET['doc_id'] ); | |
+ $unlink_group_id = absint( $_GET['group_id'] ); | |
+ | |
+ if ( current_user_can( 'bp_docs_dissociate_from_group', $unlink_group_id ) ) { | |
+ if ( bp_docs_unlink_from_group( $unlink_doc_id, $unlink_group_id ) ) { | |
+ bp_core_add_message( __( 'Doc successfully removed from the group', 'bp-docs' ) ); | |
+ } else { | |
+ bp_core_add_message( __( 'Could not remove Doc from the group.', 'bp-docs' ) ); | |
+ } | |
+ } else { | |
+ bp_core_add_message( __( 'You do not have permission to remove that Doc from this group.', 'bp-docs' ), 'error' ); | |
+ } | |
+ bp_core_redirect( bp_get_group_permalink( groups_get_group( array( 'group_id' => $unlink_group_id ) ) ) . $bp->bp_docs->slug . '/' ); | |
+ die(); | |
+ } | |
} | |
/** | |
diff --git includes/functions.php includes/functions.php | |
index 07b2c7d..2f35e88 100644 | |
--- includes/functions.php | |
+++ includes/functions.php | |
@@ -589,6 +589,34 @@ function bp_docs_save_doc_access_settings( $doc_id ) { | |
} | |
/** | |
+ * Reset doc access settings to "creator" | |
+ * | |
+ * @since 1.9.0 | |
+ * @param int $doc_id The numeric ID of the doc | |
+ * @return void | |
+ */ | |
+function bp_docs_set_doc_access_settings_creator( $doc_id ) { | |
+ if ( empty( $doc_id ) ) { | |
+ return; | |
+ } | |
+ | |
+ // When the doc was made private by group association, but loses that group association, we need to make sure that it doesn't become public. | |
+ $new_settings = array( | |
+ 'read' => 'creator', | |
+ 'edit' => 'creator', | |
+ 'read_comments' => 'creator', | |
+ 'post_comments' => 'creator', | |
+ 'view_history' => 'creator' | |
+ ); | |
+ | |
+ update_post_meta( $doc_id, 'bp_docs_settings', $new_settings ); | |
+ | |
+ // The 'read' setting must also be saved to a taxonomy, for | |
+ // easier directory queries | |
+ bp_docs_update_doc_access( $doc_id, 'creator' ); | |
+} | |
+ | |
+/** | |
* Verifies the settings associated with a given Doc | |
* | |
* @since 1.2 | |
diff --git includes/integration-groups.php includes/integration-groups.php | |
index 04c57ce..f79e8ee 100644 | |
--- includes/integration-groups.php | |
+++ includes/integration-groups.php | |
@@ -67,6 +67,9 @@ class BP_Docs_Groups_Integration { | |
add_filter( 'bp_docs_loop_additional_th', array( $this, 'groups_th' ), 5 ); | |
add_filter( 'bp_docs_loop_additional_td', array( $this, 'groups_td' ), 5 ); | |
+ // On group Doc directories, add the "Unlink from Group" action link | |
+ add_filter( 'bp_docs_doc_action_links', array( $this, 'add_doc_action_unlink_from_group_link' ), 10, 2 ); | |
+ | |
// Update group last active metadata when a doc is created, updated, or saved | |
add_filter( 'bp_docs_after_save', array( $this, 'update_group_last_active' ) ); | |
add_filter( 'bp_docs_before_doc_delete', array( $this, 'update_group_last_active' ) ); | |
@@ -775,6 +778,26 @@ class BP_Docs_Groups_Integration { | |
} | |
/** | |
+ * On group Doc directories, add the "Unlink from Group" action link | |
+ * | |
+ * @package BuddyPress_Docs | |
+ * @subpackage Groups | |
+ * @since 1.9.0 | |
+ */ | |
+ function add_doc_action_unlink_from_group_link( $links, $doc_id ) { | |
+ // Only add this link to the in-group doc directory | |
+ if ( ! $group_id = bp_get_current_group_id() ) { | |
+ return; | |
+ } | |
+ | |
+ if ( current_user_can( 'bp_docs_dissociate_from_group', $group_id ) ) { | |
+ $links[] = '<a href="' . bp_docs_get_unlink_from_group_link( $doc_id, $group_id ) . '" class="unlink-from-group confirm">' . __( 'Unlink from Group', 'bp-docs' ) . '</a>'; | |
+ } | |
+ | |
+ return $links; | |
+ } | |
+ | |
+ /** | |
* Update the current group's last_activity metadata | |
* | |
* @package BuddyPress Docs | |
@@ -1488,6 +1511,66 @@ function bp_docs_set_associated_group_id( $doc_id, $group_id = 0 ) { | |
wp_set_post_terms( $doc_id, $term, bp_docs_get_associated_item_tax_name(), false ); | |
} | |
+/** | |
+ * Process group-doc unlinking requests. | |
+ * Allows group mods & admins to remove docs from groups they moderate. | |
+ * | |
+ * @package BuddyPress Docs | |
+ * @since 1.9.0 | |
+ * | |
+ * @param int $doc_id ID of the doc to remove from the group | |
+ * @param int $group_id ID of the group the doc should be removed from | |
+ * @return bool true if the term is removed | |
+ */ | |
+function bp_docs_unlink_from_group( $doc_id, $group_id = 0 ) { | |
+ if ( $group_id ) { | |
+ $term = bp_docs_get_group_term( $group_id ); | |
+ } | |
+ | |
+ if ( empty( $doc_id ) || empty( $term ) ) { | |
+ return false; | |
+ } | |
+ | |
+ $removed = wp_remove_object_terms( $doc_id, $term, bp_docs_get_associated_item_tax_name() ); | |
+ // wp_remove_object_terms returns true on success, false or WP_Error on failure. | |
+ $retval = ( $removed == true ) ? true : false; | |
+ | |
+ // If the doc is no longer associated with any group, make sure it doesn't become public. | |
+ if ( empty( bp_docs_get_associated_group_id( $doc_id ) ) ) { | |
+ bp_docs_set_doc_access_settings_creator( $doc_id ); | |
+ } | |
+ | |
+ return $retval; | |
+} | |
+ | |
+/** | |
+ * Echo the URL for removing a Doc from a group. | |
+ * | |
+ * @since 1.9.0 | |
+ */ | |
+function bp_docs_unlink_from_group_link( $doc_id = false ) { | |
+ echo bp_docs_get_unlink_from_group_link( $doc_id, $group_id ); | |
+} | |
+ /** | |
+ * Get the URL for removing a Doc from a group. | |
+ * | |
+ * @since 1.9.0 | |
+ * | |
+ * @param int $doc_id ID of the Doc. | |
+ * @param int $group_id ID of the group to unlink from. | |
+ * @return string URL for Doc unlinking. | |
+ */ | |
+ function bp_docs_get_unlink_from_group_link( $doc_id = 0, $group_id = 0 ) { | |
+ $doc_permalink = bp_docs_get_doc_link( $doc_id ); | |
+ | |
+ $unlink_link = wp_nonce_url( add_query_arg( array( | |
+ BP_DOCS_UNLINK_FROM_GROUP_SLUG => '1', | |
+ 'doc_id' => intval( $doc_id ), | |
+ 'group_id' => intval( $group_id ), | |
+ ), $doc_permalink ), 'bp_docs_unlink_from_group' ); | |
+ | |
+ return apply_filters( 'bp_docs_get_unlink_from_group_link', $unlink_link, $doc_permalink, $doc_id, $group_id ); | |
+ } | |
function bp_docs_get_group_term( $group_id ) { | |
$group = groups_get_group( 'group_id=' . intval( $group_id ) ); | |
@@ -1658,6 +1741,34 @@ function bp_docs_groups_map_meta_caps( $caps, $cap, $user_id, $args ) { | |
} | |
break; | |
+ | |
+ case 'bp_docs_dissociate_from_group' : | |
+ if ( isset( $args[0] ) ) { | |
+ $group_id = intval( $args[0] ); | |
+ } elseif ( bp_is_group() ) { | |
+ $group_id = bp_get_current_group_id(); | |
+ } else { | |
+ $group_id = bp_docs_get_associated_group_id( get_the_ID(), $doc ); | |
+ } | |
+ | |
+ if ( empty( $group_id ) ) { | |
+ break; | |
+ } | |
+ | |
+ if ( user_can( $user_id, 'bp_moderate' ) ) { | |
+ return array( 'exist' ); | |
+ } | |
+ | |
+ $caps = array(); | |
+ | |
+ // Group admins or mods should able to remove docs from groups | |
+ if ( groups_is_user_mod( $user_id, $group_id ) || groups_is_user_admin( $user_id, $group_id ) ) { | |
+ $caps[] = 'exist'; | |
+ } else { | |
+ $caps[] = 'do_not_allow'; | |
+ } | |
+ | |
+ break; | |
} | |
return $caps; | |
diff --git includes/templatetags.php includes/templatetags.php | |
index 387d270..0936e05 100644 | |
--- includes/templatetags.php | |
+++ includes/templatetags.php | |
@@ -959,6 +959,8 @@ function bp_docs_doc_action_links() { | |
$links[] = '<a href="' . bp_docs_get_remove_from_trash_link( get_the_ID() ) . '" class="delete confirm">' . __( 'Untrash', 'bp-docs' ) . '</a>'; | |
} | |
+ $links = apply_filters( 'bp_docs_doc_action_links', $links, get_the_ID() ); | |
+ | |
echo implode( ' | ', $links ); | |
} | |
diff --git tests/test-bp-docs.php tests/test-bp-docs.php | |
index 392263d..65ae0e2 100644 | |
--- tests/test-bp-docs.php | |
+++ tests/test-bp-docs.php | |
@@ -269,6 +269,21 @@ class BP_Docs_Tests extends BP_Docs_TestCase { | |
$this->assertEquals( 1, $comment->comment_approved ); | |
} | |
+ /** | |
+ * @group bp_docs_unlink_from_group | |
+ */ | |
+ function test_bp_docs_unlink_from_group() { | |
+ $group = $this->factory->group->create(); | |
+ $doc_id = $this->factory->doc->create( array( | |
+ 'group' => $group, | |
+ ) ); | |
+ | |
+ bp_docs_unlink_from_group( $doc_id, $group ); | |
+ | |
+ $maybe_group_id = bp_docs_get_associated_group_id( $doc_id ); | |
+ | |
+ $this->assertFalse( (bool) $maybe_group_id ); | |
+ } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment