/** * Post an activity item when a comment is posted to a doc. * * @since 1.0-beta * * @param obj $comment_id The id of the comment that's just been saved * @return int $activity_id The id number of the activity created */ function bp_docs_post_comment_activity($comment_id) { if (empty($comment_id)) { return false; } $comment = get_comment($comment_id); $doc = !empty($comment->comment_post_ID) ? get_post($comment->comment_post_ID) : false; if (empty($doc)) { return false; } // Only continue if this is a BP Docs post if ($doc->post_type != bp_docs_get_post_type_name()) { return; } $doc_id = !empty($doc->ID) ? $doc->ID : false; if (!$doc_id) { return false; } // Make sure that BP doesn't record this comment with its native functions remove_action('comment_post', 'bp_blogs_record_comment', 10, 2); // Until better individual activity item privacy controls are available in BP, // comments will only be shown in the activity stream if "Who can read comments on // this doc?" is set to "Anyone", "Logged-in Users" or "Group members" $doc_settings = bp_docs_get_doc_settings($doc_id); if (!empty($doc_settings['read_comments']) && !in_array($doc_settings['read_comments'], array('anyone', 'loggedin', 'group-members'))) { return false; } // See if we're associated with a group $group_id = bp_is_active('groups') ? bp_docs_get_associated_group_id($doc_id) : 0; if ($group_id) { $component = 'groups'; $item = $group_id; } else { $component = 'bp_docs'; $item = 0; } // Set the action. Filterable so that other integration pieces can alter it $action = ''; $commenter = get_user_by('email', $comment->comment_author_email); $commenter_id = !empty($commenter->ID) ? $commenter->ID : false; // Since BP Docs only allows member comments, the following should never happen if (!$commenter_id) { return false; } $user_link = bp_core_get_userlink($commenter_id); $doc_url = bp_docs_get_doc_link($doc_id); $comment_url = $doc_url . '#comment-' . $comment->comment_ID; $comment_link = '<a href="' . $comment_url . '">' . $doc->post_title . '</a>'; $action = sprintf(__('%1$s commented on the doc %2$s', 'bp-docs'), $user_link, $comment_link); $action = apply_filters('bp_docs_comment_activity_action', $action, $user_link, $comment_link, $component, $item); // Set the type, to be used in activity filtering $type = 'bp_doc_comment'; $hide_sitewide = bp_docs_hide_sitewide_for_doc($doc_id); $args = array('user_id' => $commenter_id, 'action' => $action, 'content' => $comment->comment_content, 'primary_link' => $comment_url, 'component' => $component, 'type' => $type, 'item_id' => $item, 'secondary_item_id' => $comment_id, 'recorded_time' => bp_core_current_time(), 'hide_sitewide' => apply_filters('bp_docs_hide_sitewide', $hide_sitewide, $comment, $doc, $item, $component)); do_action('bp_docs_before_comment_activity_save', $args); $activity_id = bp_activity_add(apply_filters('bp_docs_comment_activity_args', $args)); do_action('bp_docs_after_comment_activity_save', $activity_id, $args); return $activity_id; }
public function test_bp_docs_get_associated_group_id_should_hit_term_cache() { global $wpdb; $g = $this->factory->group->create(); $d = $this->factory->doc->create(array('group' => $g)); $this->assertEquals($g, bp_docs_get_associated_group_id($d)); $num_queries = $wpdb->num_queries; $this->assertEquals($g, bp_docs_get_associated_group_id($d)); $this->assertSame($num_queries, $wpdb->num_queries); }
/** * copied from boss theme functions because of code error * * @return int */ function rw_bp_doc_single_group_id($return_dummy = true) { $group_id = false; if (function_exists('bp_is_active') && bp_is_active('groups')) { if (bp_docs_is_doc_create()) { $group_slug = isset($_GET['group']) ? $_GET['group'] : ''; if ($group_slug) { global $bp, $wpdb; $group_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM {$bp->groups->table_name} WHERE slug=%s", $group_slug)); } if (!$group_id) { if ($return_dummy) { $group_id = 99999999; } } return $group_id; } $doc_group_ids = bp_docs_get_associated_group_id(get_the_ID(), false, true); $doc_groups = array(); foreach ($doc_group_ids as $dgid) { $maybe_group = groups_get_group($dgid); // since buddypress 2.7 param should be integer // Don't show hidden groups if the // current user is not a member if (isset($maybe_group->status) && 'hidden' === $maybe_group->status) { // @todo this is slow if (!current_user_can('bp_moderate') && !groups_is_user_member(bp_loggedin_user_id(), $dgid)) { continue; } } if (!empty($maybe_group->name)) { $doc_groups[] = $dgid; } } if (!empty($doc_groups) && count($doc_groups) == 1) { $group_id = $doc_groups[0]; } } if (!$group_id) { if ($return_dummy) { $group_id = 99999999; } } return $group_id; }
/** * figure out the current used buddypress group_id * * @since 0.1 * @access public * @returns int $group_id */ public function bd_docs_get_current_group_id() { $group_id = false; if (bp_docs_is_bp_docs_page() && NULL !== bp_docs_get_current_doc()) { $group_id = bp_docs_get_associated_group_id(get_the_ID()); } else { $path = $_SERVER['REQUEST_URI']; $p_arr = explode('/', $path); if (isset($p_arr[1]) && $p_arr[1] == bp_get_groups_root_slug()) { $slug = $p_arr[2]; $group_id = BP_Groups_Group::get_id_from_slug($slug); } else { $u = parse_url(wp_get_referer()); $path = $u['path']; $p_arr = explode('/', $path); if (isset($p_arr[1]) && $p_arr[1] == bp_get_groups_root_slug()) { $slug = $p_arr[2]; $group_id = BP_Groups_Group::get_id_from_slug($slug); } } } return $group_id; }
/** * @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); }
/** * Update the current group's last_activity metadata * * @since 1.1.8 */ function update_group_last_active($doc_id) { $group = intval(bp_docs_get_associated_group_id($doc_id)); if ($group) { groups_update_groupmeta($group, 'last_activity', bp_core_current_time()); } }
/** * Markup for the Doc Permissions snapshot * * Markup is built inline. Someday I may abstract it. In the meantime, suck a lemon * * @since 1.2 */ function bp_docs_doc_permissions_snapshot($args = array()) { $html = ''; $defaults = array('summary_before_content' => '', 'summary_after_content' => ''); $args = wp_parse_args($args, $defaults); extract($args, EXTR_SKIP); if (bp_is_active('groups')) { $doc_group_ids = bp_docs_get_associated_group_id(get_the_ID(), false, true); $doc_groups = array(); foreach ($doc_group_ids as $dgid) { $maybe_group = groups_get_group('group_id=' . $dgid); // Don't show hidden groups if the // current user is not a member if (isset($maybe_group->status) && 'hidden' === $maybe_group->status) { // @todo this is slow if (!current_user_can('bp_moderate') && !groups_is_user_member(bp_loggedin_user_id(), $dgid)) { continue; } } if (!empty($maybe_group->name)) { $doc_groups[] = $maybe_group; } } // First set up the Group snapshot, if there is one if (!empty($doc_groups)) { $group_link = bp_get_group_permalink($doc_groups[0]); $html .= '<div id="doc-group-summary">'; $html .= $summary_before_content; $html .= '<span>' . __('Group: ', 'bp-docs') . '</span>'; $html .= sprintf(__(' %s', 'bp-docs'), '<a href="' . $group_link . '">' . bp_core_fetch_avatar('item_id=' . $doc_groups[0]->id . '&object=group&type=thumb&width=25&height=25') . '</a> ' . '<a href="' . $group_link . '">' . esc_html($doc_groups[0]->name) . '</a>'); $html .= $summary_after_content; $html .= '</div>'; } // we'll need a list of comma-separated group names $group_names = implode(', ', wp_list_pluck($doc_groups, 'name')); } $levels = array('anyone' => __('Anyone', 'bp-docs'), 'loggedin' => __('Logged-in Users', 'bp-docs'), 'friends' => __('My Friends', 'bp-docs'), 'creator' => __('The Doc author only', 'bp-docs'), 'no-one' => __('Just Me', 'bp-docs')); if (bp_is_active('groups')) { $levels['group-members'] = sprintf(__('Members of: %s', 'bp-docs'), $group_names); $levels['admins-mods'] = sprintf(__('Admins and mods of the group %s', 'bp-docs'), $group_names); } if (get_the_author_meta('ID') == bp_loggedin_user_id()) { $levels['creator'] = __('The Doc author only (that\'s you!)', 'bp-docs'); } $settings = bp_docs_get_doc_settings(); // Read $read_class = bp_docs_get_permissions_css_class($settings['read']); $read_text = sprintf(__('This Doc can be read by: <strong>%s</strong>', 'bp-docs'), $levels[$settings['read']]); // Edit $edit_class = bp_docs_get_permissions_css_class($settings['edit']); $edit_text = sprintf(__('This Doc can be edited by: <strong>%s</strong>', 'bp-docs'), $levels[$settings['edit']]); // Read Comments $read_comments_class = bp_docs_get_permissions_css_class($settings['read_comments']); $read_comments_text = sprintf(__('Comments are visible to: <strong>%s</strong>', 'bp-docs'), $levels[$settings['read_comments']]); // Post Comments $post_comments_class = bp_docs_get_permissions_css_class($settings['post_comments']); $post_comments_text = sprintf(__('Comments can be posted by: <strong>%s</strong>', 'bp-docs'), $levels[$settings['post_comments']]); // View History $view_history_class = bp_docs_get_permissions_css_class($settings['view_history']); $view_history_text = sprintf(__('History can be viewed by: <strong>%s</strong>', 'bp-docs'), $levels[$settings['view_history']]); // Calculate summary // Summary works like this: // 'public' - all read_ items set to 'anyone', all others to 'anyone' or 'loggedin' // 'private' - everything set to 'admins-mods', 'creator', 'no-one', 'friends', or 'group-members' where the associated group is non-public // 'limited' - everything else $anyone_count = 0; $private_count = 0; $public_settings = array('read' => 'anyone', 'edit' => 'loggedin', 'read_comments' => 'anyone', 'post_comments' => 'loggedin', 'view_history' => 'anyone'); foreach ($settings as $l => $v) { if ('anyone' == $v || isset($public_settings[$l]) && $public_settings[$l] == $v) { $anyone_count++; } else { if (in_array($v, array('admins-mods', 'creator', 'no-one', 'friends', 'group-members'))) { if ('group-members' == $v) { if (!isset($group_status)) { $group_status = 'foo'; // todo } if ('public' != $group_status) { $private_count++; } } else { $private_count++; } } } } $settings_count = count($public_settings); if ($settings_count == $private_count) { $summary = 'private'; $summary_label = __('Private', 'bp-docs'); } else { if ($settings_count == $anyone_count) { $summary = 'public'; $summary_label = __('Public', 'bp-docs'); } else { $summary = 'limited'; $summary_label = __('Limited', 'bp-docs'); } } $html .= '<div id="doc-permissions-summary" class="doc-' . $summary . '">'; $html .= $summary_before_content; $html .= sprintf(__('Access: <strong>%s</strong>', 'bp-docs'), $summary_label); $html .= '<a href="#" class="doc-permissions-toggle" id="doc-permissions-more">' . __('Show Details', 'bp-docs') . '</a>'; $html .= $summary_after_content; $html .= '</div>'; $html .= '<div id="doc-permissions-details">'; $html .= '<ul>'; $html .= '<li class="bp-docs-can-read ' . $read_class . '"><span class="bp-docs-level-icon"></span>' . '<span class="perms-text">' . $read_text . '</span></li>'; $html .= '<li class="bp-docs-can-edit ' . $edit_class . '"><span class="bp-docs-level-icon"></span>' . '<span class="perms-text">' . $edit_text . '</span></li>'; $html .= '<li class="bp-docs-can-read_comments ' . $read_comments_class . '"><span class="bp-docs-level-icon"></span>' . '<span class="perms-text">' . $read_comments_text . '</span></li>'; $html .= '<li class="bp-docs-can-post_comments ' . $post_comments_class . '"><span class="bp-docs-level-icon"></span>' . '<span class="perms-text">' . $post_comments_text . '</span></li>'; $html .= '<li class="bp-docs-can-view_history ' . $view_history_class . '"><span class="bp-docs-level-icon"></span>' . '<span class="perms-text">' . $view_history_text . '</span></li>'; $html .= '</ul>'; if (current_user_can('bp_docs_manage')) { $html .= '<a href="' . bp_docs_get_doc_edit_link() . '#doc-settings" id="doc-permissions-edit">' . __('Edit', 'bp-docs') . '</a>'; } $html .= '<a href="#" class="doc-permissions-toggle" id="doc-permissions-less">' . __('Hide Details', 'bp-docs') . '</a>'; $html .= '</div>'; echo $html; }
/** * Add the meta box to the edit page. * * @since 1.9 */ function bp_docs_folders_meta_box() { $doc_id = get_the_ID(); $associated_group_id = bp_is_active('groups') ? bp_docs_get_associated_group_id($doc_id) : 0; if (!$associated_group_id && isset($_GET['group'])) { $group_id = BP_Groups_Group::get_id_from_slug(urldecode($_GET['group'])); if (current_user_can('bp_docs_associate_with_group', $group_id)) { $associated_group_id = $group_id; } } // On the Create screen, respect the 'folder' $_GET param if (bp_docs_is_doc_create()) { $folder_id = bp_docs_get_current_folder_id(); } else { $folder_id = bp_docs_get_doc_folder($doc_id); } ?> <div id="doc-folders" class="doc-meta-box"> <div class="toggleable <?php bp_docs_toggleable_open_or_closed_class(); ?> "> <p id="folders-toggle-edit" class="toggle-switch"> <span class="hide-if-js toggle-link-no-js"><?php _e('Folders', 'bp-docs'); ?> </span> <a class="hide-if-no-js toggle-link" id="folders-toggle-link" href="#"><span class="show-pane plus-or-minus"></span><span class="toggle-title"><?php _e('Folders', 'bp-docs'); ?> </span></a> </p> <div class="toggle-content"> <table class="toggle-table" id="toggle-table-tags"> <tr> <td class="desc-column"> <label for="bp_docs_tag"><?php _e('Select a folder for this Doc.', 'bp-docs'); ?> </label> </td> <td> <div class="existing-or-new-selector"> <input type="radio" name="existing-or-new-folder" id="use-existing-folder" value="existing" checked="checked" /> <label for="use-existing-folder" class="radio-label"><?php _e('Use an existing folder', 'bp-docs'); ?> </label><br /> <div class="selector-content"> <?php bp_docs_folder_selector(array('name' => 'bp-docs-folder', 'id' => 'bp-docs-folder', 'group_id' => $associated_group_id, 'selected' => $folder_id)); ?> </div> </div> <div class="existing-or-new-selector" id="new-folder-block"> <input type="radio" name="existing-or-new-folder" id="create-new-folder" value="new" /> <label for="create-new-folder" class="radio-label"><?php _e('Create a new folder', 'bp-docs'); ?> </label> <div class="selector-content"> <?php bp_docs_create_new_folder_markup(array('group_id' => $associated_group_id, 'selected' => $associated_group_id)); ?> </div><!-- .selector-content --> </div> </td> </tr> </table> </div> </div> </div> <?php }
function bp_docs_update_doc_access($doc_id, $access_setting = 'anyone') { $doc = get_post($doc_id); if (!$doc || is_wp_error($doc)) { return false; } // Convert the access setting to a WP taxonomy term switch ($access_setting) { case 'anyone': case 'loggedin': $access_term = 'bp_docs_access_' . $access_setting; break; case 'group-members': case 'admins-mods': $associated_group = bp_docs_get_associated_group_id($doc_id); $access_term = 'group-members' == $access_setting ? bp_docs_get_access_term_group_member($associated_group) : bp_docs_get_access_term_group_adminmod($associated_group); break; case 'creator': case 'no-one': // @todo Don't know how these are different $access_term = bp_docs_get_access_term_user($doc->post_author); break; } if (isset($access_term)) { $retval = wp_set_post_terms($doc_id, $access_term, bp_docs_get_access_tax_name()); } if (empty($retval) || is_wp_error($retval)) { return false; } else { return true; } }
function bp_docs_upgrade_1_2($udata = array()) { global $wpdb; $url_base = admin_url(add_query_arg(array('post_type' => bp_docs_get_post_type_name(), 'page' => 'bp-docs-upgrade'), 'edit.php')); if (empty($udata['total'])) { $udata['total'] = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = %s", bp_docs_get_post_type_name())); } if (!isset($udata['done'])) { $udata['done'] = 0; } if (empty($udata['group_terms_migrated'])) { $tn = bp_docs_get_associated_item_tax_name(); // Get the group parent term $group_parent_term = term_exists('group', $tn); // Get all the group terms if ($group_parent_term) { // Delete the cached children terms, for good measure delete_option($tn . '_children'); $group_terms = get_terms($tn, array('parent' => intval($group_parent_term['term_id']))); foreach ($group_terms as $group_term) { // Concatenate new term slugs $new_desc = sprintf(__('Docs associated with the group %s', 'bp-docs'), $group_term->description); $new_slug = 'bp_docs_associated_group_' . $group_term->name; $new_name = $group_term->description; wp_update_term($group_term->term_id, $tn, array('description' => $new_desc, 'slug' => $new_slug, 'name' => $new_name, 'parent' => 0)); } } // Store that we're done $udata['group_terms_migrated'] = 1; $udata['message'] = __('Group terms migrated. Now migrating Doc access terms....', 'bp-docs'); $udata['refresh_url'] = add_query_arg(array('do_upgrade' => '1', '_wpnonce' => wp_create_nonce('bp-docs-upgrade')), $url_base); $udata['total'] = 0; } else { if (intval($udata['done']) < intval($udata['total'])) { $counter = 0; while ($counter < 5) { $next_doc_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND ID > %d LIMIT 1", bp_docs_get_post_type_name(), intval($udata['last']))); if (!$next_doc_id) { $udata['done'] = $udata['total']; $all_done = true; break; } // Set the 'read' setting to a taxonomy $doc_settings = get_post_meta($next_doc_id, 'bp_docs_settings', true); if (isset($doc_settings['read'])) { $read_setting = $doc_settings['read']; } else { $group = groups_get_group('group_id=' . bp_docs_get_associated_group_id($next_doc_id)); if (!empty($group->status) && 'public' != $group->status) { $read_setting = 'group-members'; // Sanitize settings as well foreach ($doc_settings as $doc_settings_key => $doc_settings_value) { if (in_array($doc_settings_value, array('anyone', 'loggedin'))) { $doc_settings[$doc_settings_key] = 'group-members'; } } $doc_settings['read'] = 'group-members'; update_post_meta($next_doc_id, 'bp_docs_settings', $doc_settings); } else { $read_setting = 'anyone'; } } bp_docs_update_doc_access($next_doc_id, $read_setting); // Count the total number of edits $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'revision' AND post_status = 'inherit' AND post_parent = %d", $next_doc_id)); update_post_meta($next_doc_id, 'bp_docs_revision_count', $count + 1); $counter++; $udata['done']++; $udata['last'] = $next_doc_id; $udata['message'] = sprintf(__('Migrated %s of %s Docs. Migrating....', 'bp-docs'), $udata['done'], $udata['total']); $udata['refresh_url'] = add_query_arg(array('do_upgrade' => '1', '_wpnonce' => wp_create_nonce('bp-docs-upgrade')), $url_base); } } else { $all_done = true; $udata['refresh_url'] = add_query_arg(array('bp_docs_upgraded' => 1), admin_url()); } } if (isset($all_done)) { bp_update_option('_bp_docs_done_upgrade_1_2', 1); } return $udata; }
/** * Markup for the Groups <td> on the docs loop * * @package BuddyPress_Docs * @subpackage Groups * @since 1.2 */ function groups_td() { global $bp; // Don't show on single group pages // @todo - When multiple group associations are supported, this should be added if (bp_is_group()) { return; } $groups = (array) bp_docs_get_associated_group_id(get_the_ID(), false, true); $groups = array_unique($groups); // just in case ?> <td class="groups-cell"> <?php if (!empty($groups)) { ?> <ul> <?php foreach ($groups as $group_id) { ?> <?php $group = groups_get_group(array('group_id' => $group_id)); $group_permalink = bp_get_group_permalink($group); ?> <li><a href="<?php echo $group_permalink; ?> "> <?php echo bp_core_fetch_avatar(array('item_id' => $group_id, 'object' => 'group', 'type' => 'thumb', 'avatar_dir' => 'group-avatars', 'width' => '30', 'height' => '30', 'title' => $group->name)); ?> <?php echo $group->name; ?> </a></li> <?php } ?> </ul> <?php } ?> </td> <?php }
function test_delete_group_association() { $group = $this->factory->group->create(); $doc_id = $this->factory->doc->create(array('group' => $group)); $permalink = get_permalink($doc_id); $this->go_to($permalink); // Just to be sure $_POST['associated_group_id'] = ''; // We need this dummy $_POST data to make the save go through. Ugh $doc = $this->factory->doc->get_object_by_id($doc_id); $_POST['doc_id'] = $doc_id; $_POST['doc_content'] = $doc->post_content; $_POST['doc']['title'] = $doc->post_title; $query = new BP_Docs_Query(); $query->save(); $maybe_group_id = bp_docs_get_associated_group_id($doc_id); $this->assertFalse((bool) $maybe_group_id); }