From 9c586a3b301f2f850df54af928da50104ce14632 Mon Sep 17 00:00:00 2001 From: Boone B Gorges Date: Mon, 30 Jun 2014 20:32:51 -0400 Subject: [PATCH] Allow Docs history to be enabled even when WP_POST_REVISIONS is set to false Define 'BP_DOCS_REVISIONS' constant to override WP_POST_REVISIONS settings for the bp_doc post type. Fixes #281 --- bp-docs.php | 4 +++- includes/addon-history.php | 6 +++--- includes/caps.php | 16 +++++++++++----- includes/functions.php | 24 ++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/bp-docs.php b/bp-docs.php index a9b379e4..ac742425 100644 --- a/bp-docs.php +++ b/bp-docs.php @@ -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; } diff --git a/includes/addon-history.php b/includes/addon-history.php index 6a2f92ac..09c32546 100644 --- a/includes/addon-history.php +++ b/includes/addon-history.php @@ -104,7 +104,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; } @@ -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 @@ -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; } diff --git a/includes/caps.php b/includes/caps.php index b614cd92..7794f507 100644 --- a/includes/caps.php +++ b/includes/caps.php @@ -58,17 +58,23 @@ function bp_docs_map_meta_caps( $caps, $cap, $user_id, $args ) { // Reset all caps. We bake from scratch $caps = array(); - // Admins can do everything - if ( user_can( $user_id, 'bp_moderate' ) ) { - return array( 'exist' ); - } - $doc = bp_docs_get_doc_for_caps( $args ); if ( empty( $doc ) ) { break; } + // Special case: view_history requires post revisions + // @todo Move this to addon-history + if ( 'bp_docs_view_history' === $cap && ! wp_revisions_enabled( $doc ) ) { + return array( 'do_not_allow' ); + } + + // Admins can do everything + if ( user_can( $user_id, 'bp_moderate' ) ) { + return array( 'exist' ); + } + $doc_settings = bp_docs_get_doc_settings( $doc->ID ); // Caps are stored without the 'bp_docs_' prefix, diff --git a/includes/functions.php b/includes/functions.php index 275083a9..0b25fbc0 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -770,3 +770,27 @@ function bp_docs_get_doc_ids_accessible_to_current_user() { $items_sql = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = %s AND ID NOT IN $exclude_sql", bp_docs_get_post_type_name() ); return $wpdb->get_col( $items_sql ); } + +/** + * Determine how many revisions to retain for Docs. + * + * @since 1.8 + * + * @return int + */ +function bp_docs_revisions_to_keep( $num, $post ) { + if ( bp_docs_get_post_type_name() !== $post->post_type ) { + return $num; + } + + if ( defined( 'BP_DOCS_REVISIONS' ) ) { + if ( true === BP_DOCS_REVISIONS ) { + $num = -1; + } else { + $num = intval( BP_DOCS_REVISIONS ); + } + } + + return intval( $num ); +} +add_filter( 'wp_revisions_to_keep', 'bp_docs_revisions_to_keep', 10, 2 );