/**
 * Get a list of an item's docs for display in the parent dropdown
 *
 * @package BuddyPress Docs
 * @since 1.0-beta
 */
function bp_docs_edit_parent_dropdown()
{
    global $bp;
    // Get the item docs to use as Include arguments
    $q = new BP_Docs_Query();
    $q->current_view = 'list';
    $qt = $q->build_query();
    // Make sure we don't limit the posts displayed
    $qt['showposts'] = -1;
    // Order them by name, no matter what
    $qt['orderby'] = 'post_title';
    $qt['order'] = 'ASC';
    $include_posts = new WP_Query($qt);
    $include = array();
    if ($include_posts->have_posts()) {
        while ($include_posts->have_posts()) {
            $include_posts->the_post();
            $include[] = get_the_ID();
        }
    }
    // Exclude the current doc, if this is 'edit' and not 'create' mode
    $exclude = !empty($bp->bp_docs->current_post->ID) ? array($bp->bp_docs->current_post->ID) : false;
    // Highlight the existing parent doc, if any
    $parent = !empty($bp->bp_docs->current_post->post_parent) ? $bp->bp_docs->current_post->post_parent : false;
    $pages = wp_dropdown_pages(array('post_type' => $bp->bp_docs->post_type_name, 'exclude' => $exclude, 'include' => $include, 'selected' => $parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)', 'bp-docs'), 'sort_column' => 'menu_order, post_title', 'echo' => 0));
    echo $pages;
}
/**
 * Returns true if the current page is a BP Docs edit or create page (used to load JS)
 *
 * @package BuddyPress Docs
 * @since 1.0-beta
 *
 * @returns bool
 */
function bp_docs_is_wiki_edit_page()
{
    global $bp;
    $item_type = BP_Docs_Query::get_item_type();
    $current_view = BP_Docs_Query::get_current_view($item_type);
    return apply_filters('bp_docs_is_wiki_edit_page', $is_wiki_edit_page);
}
 /**
  * @group BP_Docs_Query
  */
 function test_bp_docs_query_null_group()
 {
     $g = $this->factory->group->create();
     $d1 = $this->factory->doc->create(array('group' => $g));
     $d2 = $this->factory->doc->create();
     $q = new BP_Docs_Query(array('group_id' => array()));
     // Remove access protection for the moment because I'm lazy
     remove_action('pre_get_posts', 'bp_docs_general_access_protection', 28);
     $wp_query = $q->get_wp_query();
     add_action('pre_get_posts', 'bp_docs_general_access_protection', 28);
     $found = wp_list_pluck($wp_query->posts, 'ID');
     $this->assertSame($found, array($d2));
 }
/**
 * Get a list of an item's docs for display in the parent dropdown
 *
 * @package BuddyPress Docs
 * @since 1.0-beta
 */
function bp_docs_edit_parent_dropdown()
{
    global $bp;
    $include = array();
    $doc_query_builder = new BP_Docs_Query(array('doc_slug' => false, 'posts_per_page' => -1));
    $doc_query = $doc_query_builder->get_wp_query();
    if ($doc_query->have_posts()) {
        while ($doc_query->have_posts()) {
            $doc_query->the_post();
            $include[] = get_the_ID();
        }
    }
    $current_doc = get_queried_object();
    $exclude = $parent = false;
    if (isset($current_doc->post_type) && bp_docs_get_post_type_name() === $current_doc->post_type) {
        $exclude = array($current_doc->ID);
        $parent = $current_doc->post_parent;
    }
    $pages = wp_dropdown_pages(array('post_type' => $bp->bp_docs->post_type_name, 'exclude' => $exclude, 'include' => $include, 'selected' => $parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)', 'bp-docs'), 'sort_column' => 'menu_order, post_title', 'echo' => 0));
    echo $pages;
}
 /**
  * Catches page loads, determines what to do, and sends users on their merry way
  *
  * @package BuddyPress Docs
  * @since 1.0-beta
  */
 function catch_page_load()
 {
     global $bp;
     if (!empty($_POST['doc-edit-submit'])) {
         $this_doc = new BP_Docs_Query();
         $this_doc->save();
     }
     if (!empty($_POST['docs-filter-submit'])) {
         $this->handle_filters();
     }
     // If this is the edit screen, ensure that the user can edit the
     // doc before querying, and redirect if necessary
     if (!empty($bp->bp_docs->current_view) && 'edit' == $bp->bp_docs->current_view) {
         if (bp_docs_current_user_can('edit')) {
             $doc = bp_docs_get_current_doc();
             // The user can edit, so we check for edit locks
             // Because we're not using WP autosave at the moment, ensure that
             // the lock interval always returns as in process
             add_filter('wp_check_post_lock_window', create_function(false, 'return time();'));
             $lock = wp_check_post_lock($doc->ID);
             if ($lock) {
                 bp_core_add_message(sprintf(__('This doc is currently being edited by %s. To prevent overwrites, you cannot edit until that user has finished. Please try again in a few minutes.', 'bp-docs'), bp_core_get_user_displayname($lock)), 'error');
                 $group_permalink = bp_get_group_permalink($bp->groups->current_group);
                 $doc_slug = $bp->bp_docs->doc_slug;
                 // Redirect back to the non-edit view of this document
                 bp_core_redirect($group_permalink . $bp->bp_docs->slug . '/' . $doc_slug);
             }
         } else {
             // The user does not have edit permission. Redirect.
             bp_core_add_message(__('You do not have permission to edit the doc.', 'bp-docs'), 'error');
             $group_permalink = bp_get_group_permalink($bp->groups->current_group);
             $doc_slug = $bp->bp_docs->doc_slug;
             // Redirect back to the non-edit view of this document
             bp_core_redirect($group_permalink . $bp->bp_docs->slug . '/' . $doc_slug);
         }
     }
     if (!empty($bp->bp_docs->current_view) && 'create' == $bp->bp_docs->current_view) {
         if (!bp_docs_current_user_can('create')) {
             // The user does not have edit permission. Redirect.
             bp_core_add_message(__('You do not have permission to create a Doc in this group.', 'bp-docs'), 'error');
             $group_permalink = bp_get_group_permalink($bp->groups->current_group);
             // Redirect back to the Doc list view
             bp_core_redirect($group_permalink . $bp->bp_docs->slug . '/');
         }
     }
     if (!empty($bp->bp_docs->current_view) && 'history' == $bp->bp_docs->current_view) {
         if (!bp_docs_current_user_can('view_history')) {
             // The user does not have edit permission. Redirect.
             bp_core_add_message(__('You do not have permission to view this Doc\'s history.', 'bp-docs'), 'error');
             $doc = bp_docs_get_current_doc();
             $redirect = bp_docs_get_doc_link($doc->ID);
             // Redirect back to the Doc list view
             bp_core_redirect($redirect);
         }
     }
     // Cancel edit lock
     if (!empty($_GET['bpd_action']) && $_GET['bpd_action'] == 'cancel_edit_lock') {
         // Check the nonce
         check_admin_referer('bp_docs_cancel_edit_lock');
         // Todo: make this part of the perms system
         if (is_super_admin() || bp_group_is_admin()) {
             $doc = bp_docs_get_current_doc();
             // Todo: get this into a proper method as well, blech
             delete_post_meta($doc->ID, '_edit_lock');
             bp_core_add_message(__('Lock successfully removed', 'bp-docs'));
             bp_core_redirect(bp_docs_get_doc_link($doc->ID));
         }
     }
     // Cancel edit
     // Have to have a catcher for this so the edit lock can be removed
     if (!empty($_GET['bpd_action']) && $_GET['bpd_action'] == 'cancel_edit') {
         $doc = bp_docs_get_current_doc();
         // Todo: get this into a proper method as well, blech
         delete_post_meta($doc->ID, '_edit_lock');
         bp_core_redirect(bp_docs_get_doc_link($doc->ID));
     }
     // Todo: get this into a proper method
     if ($bp->bp_docs->current_view == 'delete') {
         check_admin_referer('bp_docs_delete');
         if (bp_docs_current_user_can('manage')) {
             $the_doc_args = array('name' => $bp->action_variables[0], 'post_type' => $bp->bp_docs->post_type_name);
             $the_docs = get_posts($the_doc_args);
             $doc_id = $the_docs[0]->ID;
             do_action('bp_docs_before_doc_delete', $doc_id);
             $delete_args = array('ID' => $doc_id, 'post_status' => 'trash');
             wp_update_post($delete_args);
             do_action('bp_docs_doc_deleted', $delete_args);
             bp_core_add_message(__('Doc successfully deleted!', 'bp-docs'));
         } else {
             bp_core_add_message(__('You do not have permission to delete that doc.', 'bp-docs'), 'error');
         }
         // todo: abstract this out so I don't have to call group permalink here
         $redirect_url = bp_get_group_permalink($bp->groups->current_group) . $bp->bp_docs->slug . '/';
         bp_core_redirect($redirect_url);
     }
 }
Exemple #6
0
 /**
  * Catches page loads, determines what to do, and sends users on their merry way
  *
  * @package BuddyPress Docs
  * @since 1.0-beta
  * @todo This needs a ton of cleanup
  */
 function catch_page_load()
 {
     global $bp;
     if (!empty($_POST['doc-edit-submit'])) {
         check_admin_referer('bp_docs_save');
         $this_doc = new BP_Docs_Query();
         $result = $this_doc->save();
         bp_core_add_message($result['message'], $result['message_type']);
         bp_core_redirect(trailingslashit($result['redirect_url']));
     }
     if (!empty($_POST['docs-filter-submit'])) {
         $this->handle_filters();
     }
     // If this is the edit screen, ensure that the user can edit the
     // doc before querying, and redirect if necessary
     if (bp_docs_is_doc_edit()) {
         if (current_user_can('bp_docs_edit')) {
             $doc = bp_docs_get_current_doc();
             // The user can edit, so we check for edit locks
             // Because we're not using WP autosave at the moment, ensure that
             // the lock interval always returns as in process
             add_filter('wp_check_post_lock_window', create_function(false, 'return time();'));
             $lock = bp_docs_check_post_lock($doc->ID);
             if ($lock) {
                 bp_core_add_message(sprintf(__('This doc is currently being edited by %s. To prevent overwrites, you cannot edit until that user has finished. Please try again in a few minutes.', 'bp-docs'), bp_core_get_user_displayname($lock)), 'error');
                 // Redirect back to the non-edit view of this document
                 bp_core_redirect(bp_docs_get_doc_link($doc->ID));
                 die;
             }
         } else {
             if (function_exists('bp_core_no_access') && !is_user_logged_in()) {
                 bp_core_no_access();
             }
             // The user does not have edit permission. Redirect.
             bp_core_add_message(__('You do not have permission to edit the doc.', 'bp-docs'), 'error');
             // Redirect back to the non-edit view of this document
             bp_core_redirect(bp_docs_get_doc_link($doc->ID));
             die;
         }
     }
     if (bp_docs_is_doc_create()) {
         if (!current_user_can('bp_docs_create')) {
             // The user does not have edit permission. Redirect.
             if (function_exists('bp_core_no_access') && !is_user_logged_in()) {
                 bp_core_no_access();
             }
             bp_core_add_message(__('You do not have permission to create a Doc in this group.', 'bp-docs'), 'error');
             $group_permalink = bp_get_group_permalink($bp->groups->current_group);
             // Redirect back to the Doc list view
             bp_core_redirect($group_permalink . $bp->bp_docs->slug . '/');
             die;
         }
     }
     if (!empty($bp->bp_docs->current_view) && 'history' == $bp->bp_docs->current_view) {
         if (!current_user_can('bp_docs_view_history')) {
             // The user does not have edit permission. Redirect.
             if (function_exists('bp_core_no_access') && !is_user_logged_in()) {
                 bp_core_no_access();
             }
             bp_core_add_message(__('You do not have permission to view this Doc\'s history.', 'bp-docs'), 'error');
             $doc = bp_docs_get_current_doc();
             $redirect = bp_docs_get_doc_link($doc->ID);
             // Redirect back to the Doc list view
             bp_core_redirect($redirect);
             die;
         }
     }
     // Cancel edit lock
     if (!empty($_GET['bpd_action']) && $_GET['bpd_action'] == 'cancel_edit_lock') {
         // Check the nonce
         check_admin_referer('bp_docs_cancel_edit_lock');
         // Todo: make this part of the perms system
         if (is_super_admin() || bp_group_is_admin()) {
             $doc = bp_docs_get_current_doc();
             // Todo: get this into a proper method as well, blech
             delete_post_meta($doc->ID, '_bp_docs_last_pinged');
             bp_core_add_message(__('Lock successfully removed', 'bp-docs'));
             bp_core_redirect(bp_docs_get_doc_link($doc->ID));
             die;
         }
     }
     // Cancel edit
     // Have to have a catcher for this so the edit lock can be removed
     if (!empty($_GET['bpd_action']) && $_GET['bpd_action'] == 'cancel_edit') {
         $doc = bp_docs_get_current_doc();
         // Todo: get this into a proper method as well, blech
         delete_post_meta($doc->ID, '_bp_docs_last_pinged');
         bp_core_redirect(bp_docs_get_doc_link($doc->ID));
         die;
     }
     // Todo: get this into a proper method
     if (bp_docs_is_doc_read() && !empty($_GET['delete'])) {
         check_admin_referer('bp_docs_delete');
         if (current_user_can('bp_docs_manage')) {
             $delete_doc_id = get_queried_object_id();
             if (bp_docs_trash_doc($delete_doc_id)) {
                 bp_core_add_message(__('Doc successfully deleted!', 'bp-docs'));
             } else {
                 bp_core_add_message(__('Could not delete doc.', 'bp-docs'));
             }
         } else {
             bp_core_add_message(__('You do not have permission to delete that doc.', 'bp-docs'), 'error');
         }
         bp_core_redirect(home_url(bp_docs_get_docs_slug()));
         die;
     }
     if (bp_docs_is_doc_read() && !empty($_GET['untrash']) && !empty($_GET['doc_id'])) {
         check_admin_referer('bp_docs_untrash');
         $untrash_doc_id = absint($_GET['doc_id']);
         if (current_user_can('bp_docs_manage', $untrash_doc_id)) {
             if (bp_docs_untrash_doc($untrash_doc_id)) {
                 bp_core_add_message(__('Doc successfully removed from Trash!', 'bp-docs'));
             } else {
                 bp_core_add_message(__('Could not remove Doc from Trash.', 'bp-docs'));
             }
         } else {
             bp_core_add_message(__('You do not have permission to remove that Doc from the Trash.', 'bp-docs'), 'error');
         }
         bp_core_redirect(bp_docs_get_doc_link($untrash_doc_id));
         die;
     }
 }
 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);
 }
/**
 * Get a list of an item's docs for display in the parent dropdown
 *
 * @package BuddyPress Docs
 * @since 1.0-beta
 */
function bp_docs_edit_parent_dropdown()
{
    global $bp;
    $include = array();
    $query_args = apply_filters('bp_docs_parent_dropdown_query_args', array('doc_slug' => false, 'posts_per_page' => -1));
    $doc_query_builder = new BP_Docs_Query($query_args);
    $doc_query = $doc_query_builder->get_wp_query();
    if ($doc_query->have_posts()) {
        while ($doc_query->have_posts()) {
            $doc_query->the_post();
            $include[] = get_the_ID();
        }
    }
    $current_doc = get_queried_object();
    $exclude = $parent = false;
    // If this is a failed submission, use the value from the POST cookie
    if (!empty(buddypress()->bp_docs->submitted_data->parent_id)) {
        $parent = intval(buddypress()->bp_docs->submitted_data->parent_id);
    } else {
        if (isset($current_doc->post_type) && bp_docs_get_post_type_name() === $current_doc->post_type) {
            $exclude = array($current_doc->ID);
            $parent = $current_doc->post_parent;
        }
    }
    $pages = wp_dropdown_pages(array('post_type' => $bp->bp_docs->post_type_name, 'exclude' => $exclude, 'include' => $include, 'selected' => $parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)', 'bp-docs'), 'sort_column' => 'menu_order, post_title', 'echo' => 0));
    echo $pages;
}
Exemple #9
0
function bp_docs_define_tiny_mce()
{
    BP_Docs_Query::define_wp_tiny_mce();
}
 /**
  * Gets the list of terms used by a user's docs
  *
  * At the moment, this method (and the next one) assumes that you want the terms of the
  * displayed user. At some point, that should be abstracted a bit.
  *
  * @package BuddyPress_Docs
  * @subpackage Users
  * @since 1.2
  *
  * @return array $terms
  */
 function get_user_terms($terms = array())
 {
     global $wpdb;
     if (!bp_is_user()) {
         return $terms;
     }
     $query_args = array('post_type' => bp_docs_get_post_type_name(), 'update_meta_cache' => false, 'update_term_cache' => true, 'showposts' => '-1', 'posts_per_page' => '-1');
     if (bp_docs_is_edited_by()) {
         $query_args['post__in'] = BP_Docs_Query::get_edited_by_post_ids_for_user(bp_displayed_user_id());
         $query_args['post_status'] = array('publish');
     } else {
         if (bp_docs_is_started_by()) {
             $query_args['author'] = bp_displayed_user_id();
             $query_args['post_status'] = array('publish', 'trash');
         } else {
             // Just in case
             $query_args['post__in'] = array(0);
         }
     }
     $user_doc_query = new WP_Query($query_args);
     $terms = array();
     foreach ($user_doc_query->posts as $p) {
         $p_terms = wp_get_post_terms($p->ID, buddypress()->bp_docs->docs_tag_tax_name);
         foreach ($p_terms as $p_term) {
             if (!isset($terms[$p_term->slug])) {
                 $terms[$p_term->slug] = array('name' => $p_term->name, 'posts' => array());
             }
             if (!in_array($p->ID, $terms[$p_term->slug]['posts'])) {
                 $terms[$p_term->slug]['posts'][] = $p->ID;
             }
         }
     }
     foreach ($terms as &$t) {
         $t['count'] = count($t['posts']);
     }
     if (empty($terms)) {
         $terms = array();
     }
     return apply_filters('bp_docs_taxonomy_get_user_terms', $terms);
 }
 /**
  * Update the groupmeta containing the current group's Docs count.
  *
  * Instead of incrementing, which has the potential to be error-prone, I do a fresh query
  * on each Doc save to get an accurate count. This adds some overhead, but Doc editing is
  * rare enough that it shouldn't be a huge issue.
  *
  * @package BuddyPress Docs
  * @since 1.0.8
  */
 function update_doc_count()
 {
     global $bp;
     // If this is not a group Doc, skip it
     if (!bp_is_group()) {
         return;
     }
     // Get a fresh doc count for the group
     // Set up the arguments
     $doc_count = new BP_Docs_Query();
     $query = $doc_count->build_query();
     // Fire the query
     $this_group_docs = new WP_Query($query);
     $this_group_docs_count = $this_group_docs->found_posts;
     // BP has a stupid bug that makes it delete groupmeta when it equals 0. We'll save
     // a string instead of zero to work around this
     if (!$this_group_docs_count) {
         $this_group_docs_count = '0';
     }
     // Save the count
     groups_update_groupmeta($bp->groups->current_group->id, 'bp-docs-count', $this_group_docs_count);
 }
 public function test_edit_activity_should_not_be_created_for_unchanged_revision()
 {
     // We have to do unholy things to make this testable.
     $old_post = $_POST;
     $doc = $this->factory->doc->create(array('post_content' => 'foo', 'post_title' => 'Test Doc', 'post_name' => 'test-doc'));
     $this->current_doc = get_post($doc);
     add_filter('bp_docs_get_current_doc', array($this, 'filter_current_doc'));
     $_POST = array('doc' => array('title' => 'Test Doc'), 'doc_content' => 'foo', 'ID' => $doc);
     $q = new BP_Docs_Query();
     $q->doc_slug = 'test-doc';
     $q->save();
     remove_filter('bp_docs_get_current_doc', array($this, 'filter_current_doc'));
     $_POST = $old_post;
     $found = bp_activity_get(array('show_hidden' => 1, 'filter' => array('action' => 'bp_doc_edited', 'secondary_id' => $doc)));
     $this->assertSame(array(), $found['activities']);
 }
Exemple #13
0
/**
 * Get a list of an item's docs for display in the parent dropdown
 *
 * @package BuddyPress Docs
 * @since 1.0-beta
 */
function bp_docs_edit_parent_dropdown()
{
    global $bp;
    $include = array();
    $doc_query_builder = new BP_Docs_Query(array('doc_slug' => false, 'posts_per_page' => -1));
    $doc_query = $doc_query_builder->get_wp_query();
    if ($doc_query->have_posts()) {
        while ($doc_query->have_posts()) {
            $doc_query->the_post();
            $include[] = get_the_ID();
        }
    }
    // Exclude the current doc, if this is 'edit' and not 'create' mode
    $exclude = !empty($bp->bp_docs->current_post->ID) ? array($bp->bp_docs->current_post->ID) : false;
    // Highlight the existing parent doc, if any
    $parent = !empty($bp->bp_docs->current_post->post_parent) ? $bp->bp_docs->current_post->post_parent : false;
    $pages = wp_dropdown_pages(array('post_type' => $bp->bp_docs->post_type_name, 'exclude' => $exclude, 'include' => $include, 'selected' => $parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)', 'bp-docs'), 'sort_column' => 'menu_order, post_title', 'echo' => 0));
    echo $pages;
}