From 6a49f8f81e92de250d0b127035ff9db7d4dc20e4 Mon Sep 17 00:00:00 2001 From: David Cavins Date: Wed, 3 Jun 2015 15:23:13 -0500 Subject: [PATCH 1/2] Assume BP Docs is not enabled unless explicitly enabled. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When BuddyPress Docs, via `bp_docs_get_group_settings()`, cannot find the “bp-docs” groupmeta entry for a group, it assumes that the plugin is active for the group. This causes poor behavior when the groupmeta doesn’t exist. An example is when the plugin is activated after groups are created. Also remove some extra settings checks that are not needed because default values are set in `bp_docs_get_group_settings()`. --- includes/integration-groups.php | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/includes/integration-groups.php b/includes/integration-groups.php index 27410013..0b41ecec 100644 --- a/includes/integration-groups.php +++ b/includes/integration-groups.php @@ -359,11 +359,6 @@ function user_can( $user_can, $action, $user_id, $doc_id = false ) { case 'associate_with_group' : $group_settings = bp_docs_get_group_settings( $group_id ); - // Provide a default value for legacy backpat - if ( empty( $group_settings['can-create'] ) ) { - $group_settings['can-create'] = 'member'; - } - if ( !empty( $group_settings['can-create'] ) ) { switch ( $group_settings['can-create'] ) { case 'admin' : @@ -536,14 +531,12 @@ public static function user_can_associate_doc_with_group( $user_id, $group_id ) return false; } - // Check against group settings. Default to 'member' - // @todo Abstract default settings out better + // Check against group settings. $group_settings = bp_docs_get_group_settings( $group_id ); - $can_create = isset( $group_settings['can-create'] ) ? $group_settings['can-create'] : 'member'; - if ( 'admin' == $can_create ) { + if ( 'admin' == $group_settings['can-create'] ) { return (bool) groups_is_user_admin( $user_id, $group_id ); - } else if ( 'mod' == $can_create ) { + } else if ( 'mod' == $group_settings['can-create'] ) { return groups_is_user_admin( $user_id, $group_id ) || groups_is_user_mod( $user_id, $group_id ); } @@ -1625,7 +1618,7 @@ function bp_docs_get_group_settings( $group_id ) { } $parsed_settings = wp_parse_args( $settings, array( - 'group-enable' => 1, + 'group-enable' => 0, 'can-create' => 'member', ) ); From 35d870b0c5589a1948def463bca258e7aa1e8171 Mon Sep 17 00:00:00 2001 From: David Cavins Date: Wed, 2 Dec 2015 11:57:17 -0600 Subject: [PATCH 2/2] Add test demonstrating issue #460. When no groupmeta is saved for a group, we assume that bp_docs is enabled, which seems odd. --- tests/test-bp-docs.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test-bp-docs.php b/tests/test-bp-docs.php index fbff22f9..0a6ebd59 100644 --- a/tests/test-bp-docs.php +++ b/tests/test-bp-docs.php @@ -320,6 +320,27 @@ function test_bp_docs_remove_group_related_doc_access_settings() { $this->assertEqualSetsWithIndex( $expected_settings, $modified_settings ); } + + /** + * @see issue #460 + */ + public function test_bp_docs_is_docs_enabled_for_group_should_not_assume_enabled() { + $group = $this->factory->group->create(); + $doc_id = $this->factory->doc->create( array( 'group' => $group ) ); + + $settings = array( + 'group-enable' => 1, + 'can-create' => 'member', + ); + groups_update_groupmeta( $group, 'bp-docs', $settings ); + + $this->assertTrue( bp_docs_is_docs_enabled_for_group( $group ) ); + + // What happens when no group meta is saved for this group? + groups_delete_groupmeta( $group, 'bp-docs', $settings ); + + $this->assertFalse( bp_docs_is_docs_enabled_for_group( $group ) ); + } }