Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/mlaa/buddypress-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanReeve committed Oct 31, 2014
2 parents 8454fbf + dbbc406 commit 1c6c243
Show file tree
Hide file tree
Showing 28 changed files with 1,722 additions and 458 deletions.
15 changes: 8 additions & 7 deletions bp-docs.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ function load_doc_extras() {
$this->hierarchy = new BP_Docs_Hierarchy;

// Don't load the History component if post revisions are disabled
if ( defined( 'WP_POST_REVISIONS' ) && WP_POST_REVISIONS ) {
$wp_post_revisions = defined( 'WP_POST_REVISIONS' ) && WP_POST_REVISIONS;
$bp_docs_revisions = defined( 'BP_DOCS_REVISIONS' ) && BP_DOCS_REVISIONS;
if ( $wp_post_revisions || $bp_docs_revisions ) {
require_once( BP_DOCS_INCLUDES_PATH . 'addon-history.php' );
$this->history = new BP_Docs_History;
}
Expand Down Expand Up @@ -525,25 +527,24 @@ public function remove_make_trash_public( $posts ) {
* Protects group docs from unauthorized access
*
* @since 1.2
* @uses bp_docs_current_user_can() This does most of the heavy lifting
*/
function protect_doc_access() {
// What is the user trying to do?
if ( bp_docs_is_doc_read() ) {
$action = 'read';
$action = 'bp_docs_read';
} else if ( bp_docs_is_doc_create() ) {
$action = 'create';
$action = 'bp_docs_create';
} else if ( bp_docs_is_doc_edit() ) {
$action = 'edit';
$action = 'bp_docs_edit';
} else if ( bp_docs_is_doc_history() ) {
$action = 'view_history';
$action = 'bp_docs_view_history';
}

if ( ! isset( $action ) ) {
return;
}

if ( ! bp_docs_current_user_can( $action ) ) {
if ( ! current_user_can( $action ) ) {
$redirect_to = bp_docs_get_doc_link();

bp_core_no_access( array(
Expand Down
12 changes: 6 additions & 6 deletions includes/addon-history.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ function setup_action() {
case 'restore' :
if ( !$this->revision = wp_get_post_revision( $this->revision_id ) )
break;
if ( !bp_docs_current_user_can( 'edit' ) )
if ( ! current_user_can( 'bp_docs_edit' ) )
break;
if ( !$post = get_post( $this->revision->post_parent ) )
break;

// Revisions disabled and we're not looking at an autosave
if ( ( ! WP_POST_REVISIONS || !post_type_supports( $post->post_type, 'revisions') ) && !wp_is_post_autosave( $this->revision ) ) {
if ( ! wp_revisions_enabled( $post ) && !wp_is_post_autosave( $this->revision ) ) {
$redirect = 'edit.php?post_type=' . $post->post_type;
break;
}
Expand Down Expand Up @@ -138,7 +138,7 @@ function setup_action() {
else
break; // Don't diff two unrelated revisions

if ( ! WP_POST_REVISIONS || !post_type_supports( $post->post_type, 'revisions' ) ) { // Revisions disabled
if ( ! wp_revisions_enabled( $post ) ) { // Revisions disabled

if (
// we're not looking at an autosave
Expand Down Expand Up @@ -184,7 +184,7 @@ function setup_action() {
break;

// Revisions disabled and we're not looking at an autosave
if ( ( ! WP_POST_REVISIONS || !post_type_supports($post->post_type, 'revisions') ) && !wp_is_post_autosave( $this->revision ) ) {
if ( ! wp_revisions_enabled( $post ) && !wp_is_post_autosave( $this->revision ) ) {
$redirect = 'edit.php?post_type=' . $post->post_type;
break;
}
Expand Down Expand Up @@ -364,7 +364,7 @@ function bp_docs_list_post_revisions( $post_id = 0, $args = null ) {

$rows = $right_checked = '';
$class = false;
$can_edit_post = bp_docs_current_user_can( 'edit' );
$can_edit_post = current_user_can( 'bp_docs_edit' );
foreach ( $revisions as $revision ) {
if ( 'revision' === $type && wp_is_post_autosave( $revision ) )
continue;
Expand Down Expand Up @@ -453,7 +453,7 @@ function bp_docs_list_post_revisions( $post_id = 0, $args = null ) {
* @since 1.1.4
*/
function bp_docs_history_tab() {
if ( bp_docs_current_user_can( 'view_history' ) ) : ?>
if ( current_user_can( 'bp_docs_view_history' ) ) : ?>
<li<?php if ( bp_docs_is_doc_history() ) : ?> class="current"<?php endif ?>>
<a href="<?php echo bp_docs_get_doc_link() . BP_DOCS_HISTORY_SLUG ?>"><?php _e( 'History', 'bp-docs' ) ?></a>
</li>
Expand Down
15 changes: 13 additions & 2 deletions includes/addon-taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,20 @@ function bp_docs_post_tags_meta_box() {
$tax_name = esc_attr( $taxonomy );
$taxonomy = get_taxonomy( $taxonomy );

$terms = bp_docs_is_existing_doc() ? get_terms_to_edit( get_the_ID(), $bp->bp_docs->docs_tag_tax_name ) : '';
// If this is a failed submission, use the value from the POST cookie
if ( ! empty( buddypress()->bp_docs->submitted_data->{$tax_name} ) ) {
$terms = buddypress()->bp_docs->submitted_data->{$tax_name};

// If it's an existing Doc, look up the terms
} else if ( bp_docs_is_existing_doc() ) {
$terms = get_terms_to_edit( get_the_ID(), $bp->bp_docs->docs_tag_tax_name );

// Otherwise nothing to show
} else {
$terms = '';
}
?>
<textarea name="<?php echo "$tax_name"; ?>" class="the-tags" id="tax-input-<?php echo $tax_name; ?>"><?php echo $terms; // textarea_escaped by esc_attr() ?></textarea>
<textarea name="<?php echo "$tax_name"; ?>" class="the-tags" id="tax-input-<?php echo $tax_name; ?>"><?php echo esc_textarea( $terms ); ?></textarea>
<?php
}

Expand Down
53 changes: 4 additions & 49 deletions includes/addon-wikitext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,10 @@ class BP_Docs_Wikitext {
* @package BuddyPress Docs
* @since 1.2
*/
function __construct() {
public function __construct() {
add_filter( 'the_content', array( $this, 'bracket_links' ) );
}

/**
* PHP 4 constructor
*
* @package BuddyPress Docs
* @since 1.2
*/
function bp_docs_wikitext() {
$this->__construct();
}

/**
* Detects wiki-style bracket linking
*
Expand All @@ -30,15 +20,13 @@ function bp_docs_wikitext() {
*/
function bracket_links( $content ) {
// Don't do this on a non-Doc
global $post;

if ( empty( $post->post_type ) || $post->post_type != bp_docs_get_post_type_name() ) {
if ( ! bp_docs_is_existing_doc() ) {
return $content;
}

// Find the text enclosed in double brackets.
// Letters, numbers, spaces, parentheses, pipes
$pattern = '|\[\[([a-zA-Z\s0-9\(\)\|]+?)\]\]|';
$pattern = '|\[\[([a-zA-Z\s0-9\-\(\)\|]+?)\]\]|';
$content = preg_replace_callback( $pattern, array( $this, 'process_bracket_content' ), $content );

return $content;
Expand Down Expand Up @@ -70,39 +58,6 @@ function process_bracket_content( $match ) {
$link_text = $link_page = $match[1];
}

// Exclude docs from other groups. Todo: move this out

// Query for all the current group's docs
if ( isset( $bp->groups->current_group->id ) ) {
$query_args = array(
'tax_query' => array(
array(
'taxonomy' => $bp->bp_docs->associated_item_tax_name,
'terms' => array( $bp->groups->current_group->id ),
'field' => 'name',
'operator' => 'IN',
'include_children' => false
),
),
'post_type' => $bp->bp_docs->post_type_name,
'showposts' => '-1'
);
}

$this_group_docs = new WP_Query( $query_args );

$this_group_doc_ids = array();
foreach( $this_group_docs->posts as $gpost ) {
$this_group_doc_ids[] = $gpost->ID;
}

if ( !empty( $this_group_doc_ids ) ) {
$in_clause = " AND $wpdb->posts.ID IN (" . implode(',', $this_group_doc_ids ) . ")";
} else {
$in_clause = '';
}


// Look for a page with this title. WP_Query does not allow this for some reason
$docs = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_title = %s AND post_type = %s {$in_clause}", $link_page, bp_docs_get_post_type_name() ) );

Expand All @@ -123,7 +78,7 @@ function process_bracket_content( $match ) {
$class = 'existing-doc';
} else {
// If no result is found, create a link to the edit page
$permalink = add_query_arg( 'create_title', urlencode( $link_page ), bp_docs_get_item_docs_link() . BP_DOCS_CREATE_SLUG );
$permalink = add_query_arg( 'create_title', urlencode( $link_page ), bp_docs_get_create_link() );
$class = 'nonexistent-doc';
}

Expand Down
45 changes: 22 additions & 23 deletions includes/attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public function catch_delete_request() {
return;
}

if ( ! bp_docs_current_user_can( 'edit' ) ) {
if ( ! current_user_can( 'bp_docs_edit' ) ) {
return;
}

Expand Down Expand Up @@ -382,6 +382,11 @@ function mod_upload_dir( $uploads ) {
function enqueue_scripts() {
if ( bp_docs_is_doc_edit() || bp_docs_is_doc_create() ) {
wp_enqueue_script( 'bp-docs-attachments', plugins_url( BP_DOCS_PLUGIN_SLUG . '/includes/js/attachments.js' ), array( 'media-editor', 'media-views' ), false, true );

wp_localize_script( 'bp-docs-attachments', 'bp_docs_attachments', array(
'upload_title' => __( 'Upload File', 'bp-docs' ),
'upload_button' => __( 'OK', 'bp-docs' ),
) );
}
}

Expand Down Expand Up @@ -589,7 +594,7 @@ public static function filter_markup() {
}

/**
* Give users the 'upload_files' cap, when appropriate
* Give users the 'edit_post' and 'upload_files' cap, when appropriate
*
* @since 1.4
*
Expand All @@ -600,7 +605,7 @@ public static function filter_markup() {
* @return array $caps
*/
public static function map_meta_cap( $caps, $cap, $user_id, $args ) {
if ( 'upload_files' !== $cap ) {
if ( 'upload_files' !== $cap && 'edit_post' !== $cap ) {
return $caps;
}

Expand All @@ -615,24 +620,18 @@ public static function map_meta_cap( $caps, $cap, $user_id, $args ) {
$is_ajax = isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] && 'async-upload.php' === substr( $_SERVER['REQUEST_URI'], strrpos( $_SERVER['REQUEST_URI'], '/' ) + 1 );

if ( $is_ajax ) {
// Clean up referer
$referer = $_SERVER['HTTP_REFERER'];
$qp = strpos( $referer, '?' );
if ( false !== $qp ) {
$referer = substr( $referer, 0, $qp );
}
$referer = trailingslashit( $referer );

// Existing Doc
$item_id = self::get_doc_id_from_url( $referer );
if ( $item_id ) {
$item = get_post( $item_id );
$is_doc = bp_docs_get_post_type_name() === $item->post_type;
}

// Create Doc
if ( ! $is_doc ) {
$is_doc = $referer === bp_docs_get_create_link();
// WordPress sends the 'media-form' nonce, which we use
// as an initial screen
$nonce = isset( $_REQUEST['_wpnonce'] ) ? stripslashes( $_REQUEST['_wpnonce'] ) : '';
$post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : '';

if ( wp_verify_nonce( $nonce, 'media-form' ) && $post_id ) {
$post = get_post( $post_id );

// The dummy Doc created during the Create
// process should pass this test, in addition to
// existing Docs
$is_doc = isset( $post->post_type ) && bp_docs_get_post_type_name() === $post->post_type;
}
} else {
$is_doc = bp_docs_is_existing_doc() || bp_docs_is_doc_create();
Expand All @@ -651,7 +650,7 @@ public static function map_meta_cap( $caps, $cap, $user_id, $args ) {
}

/**
* Make sure the current user has the 'edit_post' cap, when appropriate
* Make sure the current user has the 'edit_post' and 'upload_files' caps, when appropriate
*
* We do the necessary permissions checks in self::map_meta_cap(). If
* the checks pass, then we can blindly hook this filter without doing
Expand All @@ -662,7 +661,7 @@ public static function map_meta_cap( $caps, $cap, $user_id, $args ) {
* @since 1.4
*/
public static function map_meta_cap_supp( $caps, $cap, $user_id, $args ) {
if ( 'edit_post' !== $cap ) {
if ( 'upload_files' !== $cap && 'edit_post' !== $cap ) {
return $caps;
}

Expand Down
Loading

0 comments on commit 1c6c243

Please sign in to comment.