/**
  * @group user_can_associate_doc_with_group
  * @expectedDeprecated user_can_associate_doc_with_group
  */
 public function test_associate_with_group_admin()
 {
     $g = $this->factory->group->create();
     $d = $this->factory->doc->create(array('group' => $g));
     groups_update_groupmeta($g, 'bp-docs', array('can-create' => 'admin'));
     $u1 = $this->factory->user->create();
     $this->add_user_to_group($u1, $g);
     $this->assertFalse(BP_Docs_Groups_Integration::user_can_associate_doc_with_group($u1, $g));
     $u2 = $this->factory->user->create();
     $this->add_user_to_group($u2, $g);
     $gm2 = new BP_Groups_Member($u2, $g);
     $gm2->promote('mod');
     $this->assertFalse(BP_Docs_Groups_Integration::user_can_associate_doc_with_group($u2, $g));
     $u3 = $this->factory->user->create();
     $this->add_user_to_group($u3, $g);
     $gm3 = new BP_Groups_Member($u3, $g);
     $gm3->promote('admin');
     $this->assertTrue(BP_Docs_Groups_Integration::user_can_associate_doc_with_group($u3, $g));
 }
 /**
  * Show the Doc count in the group tab
  *
  * Because of a few annoying facts about the BuddyPress Group Extension API (the way it's
  * hooked into WP's load order, the fact that it doesn't differentiate between regular
  * group tabs and Admin subtabs, etc), the only way to do this is through some ugly hackery.
  *
  * The function contains a backward compatibility clause, which should only be invoked when
  * you're coming from an instance of BP Docs that didn't have this feature (or a new group).
  *
  * The way that the nav item is keyed in bp_options_nav (i.e. by group slug rather than by
  * BP_GROUPS_SLUG) means that it probably won't work for BP 1.2.x. It should degrade
  * gracefully.
  *
  * NOTE: This function is currently disabled, because of changes in Docs 1.2+
  * @todo Fix
  *
  * @since 1.0.8
  */
 function show_doc_count_in_tab()
 {
     global $bp;
     // Get the group slug, which will be the key for the nav item
     if (!empty($bp->groups->current_group->slug)) {
         $group_slug = $bp->groups->current_group->slug;
     } else {
         return;
     }
     // This will probably only work on BP 1.3+
     $docs_slug = bp_docs_get_docs_slug();
     if (!empty($bp->bp_options_nav[$group_slug]) && !empty($bp->bp_options_nav[$group_slug][$docs_slug])) {
         $current_tab_name = $bp->bp_options_nav[$group_slug][$docs_slug]['name'];
         $doc_count = groups_get_groupmeta($bp->groups->current_group->id, 'bp-docs-count');
         // For backward compatibility
         if ('' === $doc_count) {
             BP_Docs_Groups_Integration::update_doc_count();
             $doc_count = groups_get_groupmeta($bp->groups->current_group->id, 'bp-docs-count');
         }
         $bp->bp_options_nav[$group_slug][$docs_slug]['name'] = sprintf(__('%s <span>%d</span>', 'bp-docs'), $current_tab_name, $doc_count);
     }
 }
Esempio n. 3
0
/**
 * Outputs the markup for the Associated Group settings section
 *
 * @since 1.2
 */
function bp_docs_doc_associated_group_markup()
{
    // First, try to set the preselected group by looking at the URL params
    $selected_group_slug = isset($_GET['group']) ? $_GET['group'] : '';
    $selected_group = BP_Groups_Group::get_id_from_slug($selected_group_slug);
    if ($selected_group && !BP_Docs_Groups_Integration::user_can_associate_doc_with_group(bp_loggedin_user_id(), $selected_group)) {
        $selected_group = 0;
    }
    // If the selected group is still 0, see if there's something in the db
    if (!$selected_group && is_single()) {
        $selected_group = bp_docs_get_associated_group_id(get_the_ID());
    }
    $groups_args = array('per_page' => false, 'populate_extras' => false);
    if (!bp_current_user_can('bp_moderate')) {
        $groups_args['user_id'] = bp_loggedin_user_id();
    }
    // Populate the $groups_template global
    global $groups_template;
    bp_has_groups($groups_args);
    ?>
	<tr>
		<td class="desc-column">
			<label for="associated_group_id"><?php 
    _e('Which group should this Doc be associated with?', 'bp-docs');
    ?>
</label>
			<span class="description"><?php 
    _e('(Optional) Note that the Access settings available for this Doc may be limited by the privacy settings of the group you choose.', 'bp-docs');
    ?>
</span>
		</td>

		<td class="content-column">
			<select name="associated_group_id" id="associated_group_id">
				<option value=""><?php 
    _e('None', 'bp-docs');
    ?>
</option>
				<?php 
    foreach ($groups_template->groups as $g) {
        ?>
					<option value="<?php 
        echo esc_attr($g->id);
        ?>
" <?php 
        selected($selected_group, $g->id);
        ?>
><?php 
        echo esc_html($g->name);
        ?>
</option>
				<?php 
    }
    ?>
			</select>

			<div id="associated_group_summary">
				<?php 
    bp_docs_associated_group_summary();
    ?>
			</div>
		</td>
	</tr>
	<?php 
}
Esempio n. 4
0
 /**
  * Saves a doc.
  *
  * This method handles saving for both new and existing docs. It detects the difference by
  * looking for the presence of $this->doc_slug
  *
  * @package BuddyPress Docs
  * @since 1.0-beta
  */
 function save($args = false)
 {
     global $bp;
     // bbPress plays naughty with revision saving
     add_action('pre_post_update', 'wp_save_post_revision');
     // Get the required taxonomy items associated with the group. We only run this
     // on a save because it requires extra database hits.
     $this->setup_terms();
     // Set up the default value for the result message
     $results = array('message' => __('Unknown error. Please try again.', 'bp-docs'), 'redirect' => 'create');
     // Backward compatibility. Had to change to doc_content to work with wp_editor
     $doc_content = '';
     if (isset($_POST['doc_content'])) {
         $doc_content = $_POST['doc_content'];
     } else {
         if (isset($_POST['doc']['content'])) {
             $doc_content = $_POST['doc']['content'];
         }
     }
     // Check group associations
     // @todo Move into group integration piece
     if (bp_is_active('groups')) {
         $associated_group_id = isset($_POST['associated_group_id']) ? intval($_POST['associated_group_id']) : 0;
         if ($associated_group_id && !BP_Docs_Groups_Integration::user_can_associate_doc_with_group(bp_loggedin_user_id(), $associated_group_id)) {
             bp_core_add_message(__('You are not allowed to associate a Doc with that group.', 'bp-docs'), 'error');
             bp_core_redirect(bp_docs_get_create_link());
         }
     }
     if (empty($_POST['doc']['title']) || empty($doc_content)) {
         // Both the title and the content fields are required
         $result['message'] = __('Both the title and the content fields are required.', 'bp-docs');
         $result['redirect'] = $this->current_view;
     } else {
         // If both the title and content fields are filled in, we can proceed
         $defaults = array('post_type' => $this->post_type_name, 'post_title' => $_POST['doc']['title'], 'post_name' => isset($_POST['doc']['permalink']) ? sanitize_title($_POST['doc']['permalink']) : sanitize_title($_POST['doc']['title']), 'post_content' => stripslashes(sanitize_post_field('post_content', $doc_content, 0, 'db')), 'post_status' => 'publish');
         $r = wp_parse_args($args, $defaults);
         if (empty($this->doc_slug)) {
             $this->is_new_doc = true;
             $r['post_author'] = bp_loggedin_user_id();
             // This is a new doc
             if (!($post_id = wp_insert_post($r))) {
                 $result['message'] = __('There was an error when creating the doc.', 'bp-docs');
                 $result['redirect'] = 'create';
             } else {
                 $this->doc_id = $post_id;
                 $the_doc = get_post($this->doc_id);
                 $this->doc_slug = $the_doc->post_name;
                 // A normal, successful save
                 $result['message'] = __('Doc successfully created!', 'bp-docs');
                 $result['redirect'] = 'single';
             }
         } else {
             $this->is_new_doc = false;
             $doc = get_queried_object();
             $this->doc_id = $doc->ID;
             $r['ID'] = $this->doc_id;
             // Make sure the post_name is set
             if (empty($r['post_name'])) {
                 $r['post_name'] = sanitize_title($r['post_title']);
             }
             // Make sure the post_name is unique
             $r['post_name'] = wp_unique_post_slug($r['post_name'], $this->doc_id, $r['post_status'], $this->post_type_name, $doc->post_parent);
             $this->doc_slug = $r['post_name'];
             if (!wp_update_post($r)) {
                 $result['message'] = __('There was an error when saving the doc.', 'bp-docs');
                 $result['redirect'] = 'edit';
             } else {
                 // Remove the edit lock
                 delete_post_meta($this->doc_id, '_edit_lock');
                 // When the post has been autosaved, we need to leave a
                 // special success message
                 if (!empty($_POST['is_auto']) && $_POST['is_auto']) {
                     $result['message'] = __('You idled a bit too long while in Edit mode. In order to allow others to edit the doc you were working on, your changes have been autosaved. Click the Edit button to return to Edit mode.', 'bp-docs');
                 } else {
                     // A normal, successful save
                     $result['message'] = __('Doc successfully edited!', 'bp-docs');
                 }
                 $result['redirect'] = 'single';
             }
             $post_id = $this->doc_id;
         }
     }
     // Add to a group, if necessary
     if (isset($associated_group_id)) {
         bp_docs_set_associated_group_id($post_id, $associated_group_id);
     }
     // Make sure the current user is added as one of the authors
     wp_set_post_terms($post_id, $this->user_term_id, $this->associated_item_tax_name, true);
     // Save the last editor id. We'll use this to create an activity item
     update_post_meta($this->doc_id, 'bp_docs_last_editor', bp_loggedin_user_id());
     // Save settings
     $settings = !empty($_POST['settings']) ? $_POST['settings'] : array();
     $verified_settings = bp_docs_verify_settings($settings, $post_id, bp_loggedin_user_id());
     $new_settings = array();
     foreach ($verified_settings as $verified_setting_name => $verified_setting) {
         $new_settings[$verified_setting_name] = $verified_setting['verified_value'];
         if ($verified_setting['verified_value'] != $verified_setting['original_value']) {
             $result['message'] = __('Your Doc was successfully saved, but some of your access settings have been changed to match the Doc\'s permissions.', 'bp-docs');
         }
     }
     update_post_meta($this->doc_id, 'bp_docs_settings', $new_settings);
     // The 'read' setting must also be saved to a taxonomy, for
     // easier directory queries
     $read_setting = isset($new_settings['read']) ? $new_settings['read'] : 'anyone';
     bp_docs_update_doc_access($this->doc_id, $read_setting);
     // Increment the revision count
     $revision_count = get_post_meta($this->doc_id, 'bp_docs_revision_count', true);
     update_post_meta($this->doc_id, 'bp_docs_revision_count', intval($revision_count) + 1);
     // Provide a custom hook for plugins and optional components.
     // WP's default save_post isn't enough, because we need something that fires
     // only when we save from the front end (for things like taxonomies, which
     // the WP admin handles automatically)
     do_action('bp_docs_doc_saved', $this);
     $message_type = $result['redirect'] == 'single' ? 'success' : 'error';
     $redirect_url = trailingslashit(bp_get_root_domain() . '/' . BP_DOCS_SLUG);
     if ($result['redirect'] == 'single') {
         $redirect_url .= $this->doc_slug;
     } else {
         if ($result['redirect'] == 'edit') {
             $redirect_url .= $this->doc_slug . '/' . BP_DOCS_EDIT_SLUG;
         } else {
             if ($result['redirect'] == 'create') {
                 $redirect_url .= BP_DOCS_CREATE_SLUG;
             }
         }
     }
     $retval = array('message_type' => $message_type, 'message' => $result['message'], 'redirect_url' => $redirect_url);
     return $retval;
 }
/**
 * Outputs the markup for the Associated Group settings section
 *
 * @since 1.2
 */
function bp_docs_doc_associated_group_markup()
{
    // First, try to set the preselected group by looking at the URL params
    $selected_group_slug = isset($_GET['group']) ? $_GET['group'] : '';
    // Support for BP Group Hierarchy
    if (false !== ($slash = strrpos($selected_group_slug, '/'))) {
        $selected_group_slug = substr($selected_group_slug, $slash + 1);
    }
    $selected_group = BP_Groups_Group::get_id_from_slug($selected_group_slug);
    if ($selected_group && !BP_Docs_Groups_Integration::user_can_associate_doc_with_group(bp_loggedin_user_id(), $selected_group)) {
        $selected_group = 0;
    }
    // If the selected group is still 0, see if there's something in the db
    if (!$selected_group && is_singular()) {
        $selected_group = bp_docs_get_associated_group_id(get_the_ID());
    }
    $groups_args = array('per_page' => false, 'populate_extras' => false);
    if (!bp_current_user_can('bp_moderate')) {
        $groups_args['user_id'] = bp_loggedin_user_id();
    }
    // Populate the $groups_template global
    global $groups_template;
    bp_has_groups($groups_args);
    // Filter out the groups where associate_with permissions forbid
    $removed = 0;
    foreach ($groups_template->groups as $gtg_key => $gtg) {
        $this_group_settings = groups_get_groupmeta($gtg->id, 'bp-docs');
        if (isset($this_group_settings['can-create']) && in_array($this_group_settings['can-create'], array('admin', 'mod'))) {
            $is_admin = groups_is_user_admin(bp_loggedin_user_id(), $gtg->id);
            if ('mod' == $this_group_settings['can-create']) {
                $is_mod = groups_is_user_mod(bp_loggedin_user_id(), $gtg->id);
                $remove = !$is_mod && !$is_admin;
            } else {
                $remove = !$is_admin;
            }
            if ($remove) {
                unset($groups_template->groups[$gtg_key]);
                $removed++;
            }
        }
    }
    // cleanup, if necessary from filter above
    if ($removed) {
        $groups_template->groups = array_values($groups_template->groups);
        $groups_template->group_count = $groups_template->group_count - $removed;
        $groups_template->total_group_count = $groups_template->total_group_count - $removed;
    }
    ?>
	<tr>
		<td class="desc-column">
			<label for="associated_group_id"><?php 
    _e('Which group should this Doc be associated with?', 'bp-docs');
    ?>
</label>
			<span class="description"><?php 
    _e('(Optional) Note that the Access settings available for this Doc may be limited by the privacy settings of the group you choose.', 'bp-docs');
    ?>
</span>
		</td>

		<td class="content-column">
			<select name="associated_group_id" id="associated_group_id">
				<option value=""><?php 
    _e('None', 'bp-docs');
    ?>
</option>
				<?php 
    foreach ($groups_template->groups as $g) {
        ?>
					<option value="<?php 
        echo esc_attr($g->id);
        ?>
" <?php 
        selected($selected_group, $g->id);
        ?>
><?php 
        echo esc_html($g->name);
        ?>
</option>
				<?php 
    }
    ?>
			</select>

			<div id="associated_group_summary">
				<?php 
    bp_docs_associated_group_summary();
    ?>
			</div>
		</td>
	</tr>
	<?php 
}