/**
  * Setup params from the $_GET global
  *
  * Does some sanity checks along the way
  *
  * @package BuddyPress Docs
  * @since 1.1
  */
 function setup_params()
 {
     global $bp;
     $actions = array('restore', 'diff', 'view');
     $this->action = !empty($_GET['action']) && in_array($_GET['action'], $actions) ? $_GET['action'] : 'view';
     $this->left = !empty($_GET['left']) ? (int) $_GET['left'] : false;
     $this->right = !empty($_GET['right']) ? (int) $_GET['right'] : false;
     // Try to get the revision id out of the URL. If it's not provided, default to the
     // current post
     $this->revision_id = !empty($_GET['revision']) ? (int) $_GET['revision'] : false;
     if (!$this->revision_id) {
         if (empty($bp->bp_docs->current_post)) {
             $bp->bp_docs->current_post = bp_docs_get_current_doc();
         }
         $this->revision_id = !empty($bp->bp_docs->current_post->ID) ? $bp->bp_docs->current_post->ID : false;
     }
 }
 /**
  * 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;
 }
 /**
  * Make the page title nice and pretty
  *
  * @since 1.1.4
  *
  * @param str The title string passed by bp_page_title
  * @return str The Doc-ified title
  */
 function page_title($title)
 {
     global $bp;
     if (!empty($bp->action_variables)) {
         $title = explode(' | ', $title);
         // Get rid of the Docs title with Doc count (see buggy
         // show_doc_count_in_tab()) and replace with Docs
         array_pop($title);
         $title[] = __('Docs', 'bp-docs');
         $doc = bp_docs_get_current_doc();
         if (empty($doc->post_title)) {
             // If post_title is empty, this is a New Doc screen
             $title[] = __('New Doc', 'bp-docs');
         } else {
             // Add the post title
             $title[] = $doc->post_title;
             if (isset($bp->action_variables[1])) {
                 if (BP_DOCS_EDIT_SLUG == $bp->action_variables[1]) {
                     $title[] = __('Edit', 'bp-docs');
                 } else {
                     if (BP_DOCS_HISTORY_SLUG == $bp->action_variables[1]) {
                         $title[] = __('History', 'bp-docs');
                     }
                 }
             }
         }
         $title = implode(' | ', $title);
     }
     return apply_filters('bp_docs_page_title', $title);
 }
 /**
  * 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);
     }
 }
Beispiel #5
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;
     }
 }
 /**
  * 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
  *
  * @since 1.0-beta
  */
 function save($args = false)
 {
     global $bp, $wp_rewrite;
     // 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
     // This group id is only used to check whether the user can associate the doc with the group.
     $associated_group_id = isset($_POST['associated_group_id']) ? intval($_POST['associated_group_id']) : null;
     if (bp_is_active('groups')) {
         if (!empty($associated_group_id) && !current_user_can('bp_docs_associate_with_group', $associated_group_id)) {
             $retval = array('message_type' => 'error', 'message' => __('You are not allowed to associate a Doc with that group.', 'bp-docs'), 'redirect_url' => bp_docs_get_create_link());
             return $retval;
         }
     }
     if (empty($_POST['doc']['title'])) {
         // The title field is required
         $result['message'] = __('The title field is required.', 'bp-docs');
         $result['redirect'] = !empty($this->doc_slug) ? 'edit' : 'create';
     } else {
         $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' => 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();
             // If there's a 'doc_id' value in the POST, use
             // the autodraft as a starting point
             if (isset($_POST['doc_id']) && 0 != $_POST['doc_id']) {
                 $post_id = (int) $_POST['doc_id'];
                 $r['ID'] = $post_id;
                 wp_update_post($r);
             } else {
                 $post_id = wp_insert_post($r);
             }
             if (!$post_id) {
                 $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 = bp_docs_get_current_doc();
             $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'];
             // Save pre-update post data, for comparison by callbacks.
             $this->previous_revision = clone $doc;
             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');
                 delete_post_meta($this->doc_id, '_bp_docs_last_pinged');
                 // 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;
         }
     }
     // If the Doc was successfully created, run some more stuff
     if (!empty($post_id)) {
         // Add to a group, if necessary
         if (!is_null($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
         bp_docs_save_doc_access_settings($this->doc_id);
         // 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);
     do_action('bp_docs_after_save', $this->doc_id);
     $message_type = $result['redirect'] == 'single' ? 'success' : 'error';
     // Stuff data into a cookie so it can be accessed on next page load
     if ('error' === $message_type) {
         setcookie('bp-docs-submit-data', json_encode($_POST), time() + 30, '/');
     }
     $redirect_base = trailingslashit(bp_get_root_domain());
     if ($wp_rewrite->using_index_permalinks()) {
         $redirect_base .= 'index.php/';
     }
     $redirect_url = apply_filters('bp_docs_post_save_redirect_base', trailingslashit($redirect_base . bp_docs_get_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;
 }
Beispiel #7
0
/**
 * Determine whether a given user can do something with a given doc
 *
 * @package BuddyPress Docs
 * @since 1.0-beta
 *
 * @param str $action Optional. The action being queried. Eg 'edit', 'read_comments', 'manage'
 * @param int $user_id Optional. Unique user id for the user being tested. Defaults to logged-in ID
 * @param int $doc_id Optional. Unique doc id. Defaults to doc currently being viewed
 */
function bp_docs_user_can($action = 'edit', $user_id = false, $doc_id = false)
{
    global $bp, $post;
    if (!$user_id) {
        $user_id = bp_loggedin_user_id();
    }
    // Only certain actions are checked against doc_ids
    $need_doc_ids_actions = apply_filters('bp_docs_need_doc_ids_actions', array('edit', 'manage', 'view_history', 'read', 'read_comments', 'post_comments'));
    $doc_id = false;
    if (in_array($action, $need_doc_ids_actions)) {
        if (!$doc_id) {
            if (!empty($post->ID)) {
                $doc_id = $post->ID;
                $doc = $post;
            } else {
                $doc = bp_docs_get_current_doc();
                if (isset($doc->ID)) {
                    $doc_id = $doc->ID;
                }
            }
        }
    }
    $user_can = false;
    if (!empty($doc)) {
        $doc_settings = get_post_meta($doc_id, 'bp_docs_settings', true);
        $the_setting = isset($doc_settings[$action]) ? $doc_settings[$action] : '';
        if (empty($the_setting)) {
            $the_setting = 'anyone';
        }
        switch ($the_setting) {
            case 'anyone':
                $user_can = true;
                break;
            case 'loggedin':
                $user_can = is_user_logged_in();
                break;
            case 'creator':
                $user_can = $doc->post_author == $user_id;
                break;
                // Do nothing with other settings - they are passed through
        }
    } else {
        if ('create' == $action) {
            // In the case of Doc creation, this value gets passed through
            // to other components
            $user_can = is_user_logged_in();
        }
    }
    if ($user_id) {
        if (is_super_admin()) {
            // Super admin always gets to edit. What a big shot
            $user_can = true;
        } else {
            // Filter this so that groups-integration and other plugins can give their
            // own rules. Done inside the conditional so that plugins don't have to
            // worry about the is_super_admin() check
            $user_can = apply_filters('bp_docs_user_can', $user_can, $action, $user_id, $doc_id);
        }
    }
    return $user_can;
}
/**
 * Determine whether a given user can do something with a given doc
 *
 * @package BuddyPress Docs
 * @since 1.0-beta
 *
 * @param str $action Optional. The action being queried. Eg 'edit', 'read_comments', 'manage'
 * @param int $user_id Optional. Unique user id for the user being tested. Defaults to logged-in ID
 * @param int $doc_id Optional. Unique doc id. Defaults to doc currently being viewed
 */
function bp_docs_user_can($action = 'edit', $user_id = false, $doc_id = false)
{
    global $bp, $post;
    if (false === $user_id) {
        $user_id = bp_loggedin_user_id();
    }
    // Grant all permissions on documents being created, as long as the
    // user is logged in
    if ($user_id && false === $doc_id && bp_docs_is_doc_create()) {
        return true;
    }
    if (!$doc_id) {
        if (!empty($post->ID) && bp_docs_get_post_type_name() === $post->post_type) {
            $doc_id = $post->ID;
            $doc = $post;
        } else {
            $doc = bp_docs_get_current_doc();
            if (isset($doc->ID)) {
                $doc_id = $doc->ID;
            }
        }
    }
    $user_can = false;
    if ('create' === $action) {
        // In the case of Doc creation, this value gets passed through
        // to other components
        $user_can = 0 != $user_id;
    } else {
        if (!empty($doc)) {
            $doc_settings = bp_docs_get_doc_settings($doc_id);
            $the_setting = isset($doc_settings[$action]) ? $doc_settings[$action] : '';
            if (empty($the_setting)) {
                $the_setting = 'anyone';
            }
            switch ($the_setting) {
                case 'anyone':
                    $user_can = true;
                    break;
                case 'loggedin':
                    $user_can = 0 != $user_id;
                    break;
                case 'creator':
                    $user_can = $doc->post_author == $user_id;
                    break;
                    // Do nothing with other settings - they are passed through
            }
        }
    }
    if ($user_id) {
        if (is_super_admin($user_id)) {
            // Super admin always gets to edit. What a big shot
            $user_can = true;
        } else {
            // Filter this so that groups-integration and other plugins can give their
            // own rules. Done inside the conditional so that plugins don't have to
            // worry about the is_super_admin() check
            $user_can = apply_filters('bp_docs_user_can', $user_can, $action, $user_id, $doc_id);
        }
    }
    return $user_can;
}
 /**
  * When looking at a single doc, this adds the appropriate subnav item.
  *
  * Other navigation items are added in BP_Docs_Component. We add this item here because it's
  * not until later in the load order when we can be certain whether we're viewing a
  * single Doc.
  *
  * @since 1.2
  */
 function setup_single_doc_subnav()
 {
     global $bp;
     if (bp_is_user() && !empty($bp->bp_docs->current_view) && in_array($bp->bp_docs->current_view, array('single', 'edit', 'history', 'delete'))) {
         $doc = bp_docs_get_current_doc();
         if (!empty($doc)) {
             bp_core_new_subnav_item(array('name' => $doc->post_title, 'slug' => $doc->post_name, 'parent_url' => trailingslashit(bp_loggedin_user_domain() . bp_docs_get_slug()), 'parent_slug' => bp_docs_get_slug(), 'screen_function' => array($bp->bp_docs, 'template_loader'), 'position' => 30, 'user_has_access' => true));
         }
     }
 }
/**
 * Are we looking at an existing doc?
 *
 * @package BuddyPress Docs
 * @since 1.0-beta
 *
 * @return bool True if it's an existing doc
 */
function bp_docs_is_existing_doc()
{
    global $bp;
    if (empty($bp->bp_docs->current_post)) {
        $bp->bp_docs->current_post = bp_docs_get_current_doc();
    }
    if (empty($bp->bp_docs->current_post)) {
        return false;
    }
    return true;
}