From 9cc3abc25864756dc02c3ff128ca72434eb2fdf4 Mon Sep 17 00:00:00 2001 From: Jonathan Reeve Date: Wed, 25 Feb 2015 11:17:24 -0500 Subject: [PATCH 1/4] re-enable counts on Docs tab, fixes #1 --- includes/component.php | 4 ++-- includes/integration-groups.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/component.php b/includes/component.php index a87482af..358d48c3 100644 --- a/includes/component.php +++ b/includes/component.php @@ -288,10 +288,10 @@ function set_current_view( $item_type = false ) { function setup_nav( $main_nav = array(), $sub_nav = array() ) { $main_nav = array( - 'name' => bp_docs_get_user_tab_name(), + //'name' => bp_docs_get_user_tab_name(), // Disabled count for now. See https://github.com/boonebgorges/buddypress-docs/issues/261 - //'name' => sprintf( __( 'Docs %d', 'bp-docs' ), bp_docs_get_doc_count( bp_displayed_user_id(), 'user' ) ), + 'name' => sprintf( __( 'Docs %d', 'bp-docs' ), bp_docs_get_doc_count( bp_displayed_user_id(), 'user' ) ), 'slug' => bp_docs_get_docs_slug(), 'position' => 80, 'screen_function' => array( &$this, 'template_loader' ), diff --git a/includes/integration-groups.php b/includes/integration-groups.php index 56c433a3..35edc8cb 100644 --- a/includes/integration-groups.php +++ b/includes/integration-groups.php @@ -75,7 +75,7 @@ function __construct() { // Sneak into the nav before it's rendered to insert the group Doc count. Hooking // to bp_actions because of the craptastic nature of the BP_Group_Extension loader // @todo Temporarily disabled - //add_action( 'bp_actions', array( $this, 'show_doc_count_in_tab' ), 9 ); + add_action( 'bp_actions', array( $this, 'show_doc_count_in_tab' ), 9 ); // Prettify the page title add_filter( 'bp_page_title', array( $this, 'page_title' ) ); From 81d5fb5091bc8ccaa105508b6cac7622c1f77f70 Mon Sep 17 00:00:00 2001 From: Jonathan Reeve Date: Wed, 25 Feb 2015 16:08:36 -0500 Subject: [PATCH 2/4] better updating of docs count for #1 --- includes/integration-groups.php | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/includes/integration-groups.php b/includes/integration-groups.php index 35edc8cb..c07b1b11 100644 --- a/includes/integration-groups.php +++ b/includes/integration-groups.php @@ -60,8 +60,8 @@ function __construct() { add_filter( 'bp_docs_hide_sitewide', array( $this, 'hide_sitewide' ), 10, 5 ); // These functions are used to keep the group Doc count up to date - add_filter( 'bp_docs_doc_saved', array( $this, 'update_doc_count' ) ); - add_filter( 'bp_docs_doc_deleted', array( $this, 'update_doc_count' ) ); + add_action( 'bp_docs_doc_saved', array( $this, 'update_doc_count' ), 5 ); + add_action( 'bp_docs_doc_deleted', array( $this, 'update_doc_count' ), 5 ); // On non-group Doc directories, add a Groups column add_filter( 'bp_docs_loop_additional_th', array( $this, 'groups_th' ), 5 ); @@ -685,12 +685,24 @@ function hide_sitewide( $hide_sitewide, $comment, $doc, $item, $component ) { 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 - bp_docs_update_doc_count( bp_get_current_group_id(), 'group' ); + if ( array_key_exists( 'delete', $_GET ) ) { + // We're deleting a doc. In the deleting context, + // we get useful information from `bp_docs_get_associated_group_id()`. + $doc_id = is_singular() ? get_the_ID() : 0; + $group_id = bp_docs_get_associated_group_id( $doc_id ); + } else if ( array_key_exists( 'group', $_GET ) ) { + // We're creating a doc. In the doc creation context, + // we don't get anything useful from `bp_docs_get_associated_group_id()`, + // so we have to figure out what group we're in by looking at the + // $_GET variable that's passed during this step. + $group_id = BP_Groups_Group::group_exists( $_GET['group'] ); + } else { + // If we're not creating or deleting a document, get outta here! + return; + } + + // Update the doc count for the group, since it has now changed. + bp_docs_update_doc_count( $group_id, 'group' ); } /** From 35d047a03dd4e1625156631d235a2f5b1dad5bb4 Mon Sep 17 00:00:00 2001 From: Jonathan Reeve Date: Wed, 25 Feb 2015 16:12:56 -0500 Subject: [PATCH 3/4] cleaning up --- includes/integration-groups.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/integration-groups.php b/includes/integration-groups.php index c07b1b11..05124efc 100644 --- a/includes/integration-groups.php +++ b/includes/integration-groups.php @@ -60,8 +60,8 @@ function __construct() { add_filter( 'bp_docs_hide_sitewide', array( $this, 'hide_sitewide' ), 10, 5 ); // These functions are used to keep the group Doc count up to date - add_action( 'bp_docs_doc_saved', array( $this, 'update_doc_count' ), 5 ); - add_action( 'bp_docs_doc_deleted', array( $this, 'update_doc_count' ), 5 ); + add_action( 'bp_docs_doc_saved', array( $this, 'update_doc_count' ) ); + add_action( 'bp_docs_doc_deleted', array( $this, 'update_doc_count' ) ); // On non-group Doc directories, add a Groups column add_filter( 'bp_docs_loop_additional_th', array( $this, 'groups_th' ), 5 ); From 4ec4b2c2aa726fa8da96e773639002d3671c55d5 Mon Sep 17 00:00:00 2001 From: Jonathan Reeve Date: Thu, 26 Feb 2015 15:49:55 -0500 Subject: [PATCH 4/4] support for updating doc count on first run --- includes/integration-groups.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/includes/integration-groups.php b/includes/integration-groups.php index 05124efc..9399a6e6 100644 --- a/includes/integration-groups.php +++ b/includes/integration-groups.php @@ -682,7 +682,7 @@ function hide_sitewide( $hide_sitewide, $comment, $doc, $item, $component ) { * @package BuddyPress Docs * @since 1.0.8 */ - function update_doc_count() { + function update_doc_count( $group_id = 0 ) { global $bp; if ( array_key_exists( 'delete', $_GET ) ) { @@ -696,8 +696,15 @@ function update_doc_count() { // so we have to figure out what group we're in by looking at the // $_GET variable that's passed during this step. $group_id = BP_Groups_Group::group_exists( $_GET['group'] ); + } else if ( 0 !== $group_id ) { + // If $group_id is passed through this function, + // that means it's probably being called via show_doc_count_in_tab(). + // This means that the doc count is probably '', which means this is probably + // the first time this function has been run for this group. + // Nothing to do here. Pass along through. } else { - // If we're not creating or deleting a document, get outta here! + // If we're not creating or deleting a document, or updating for the first time, + // get outta here! return; } @@ -849,12 +856,14 @@ function show_doc_count_in_tab() { if ( !empty( $bp->bp_options_nav[$group_slug] ) && !empty( $bp->bp_options_nav[$group_slug][ $docs_slug ] ) ) { $current_tab_name = $bp->bp_options_nav[$group_slug][ $docs_slug ]['name']; - $doc_count = groups_get_groupmeta( $bp->groups->current_group->id, 'bp-docs-count' ); + $group_id = $bp->groups->current_group->id; + + $doc_count = groups_get_groupmeta( $group_id, 'bp-docs-count' ); // For backward compatibility if ( '' === $doc_count ) { - BP_Docs_Groups_Integration::update_doc_count(); - $doc_count = groups_get_groupmeta( $bp->groups->current_group->id, 'bp-docs-count' ); + BP_Docs_Groups_Integration::update_doc_count( $group_id ); + $doc_count = groups_get_groupmeta( $group_id, 'bp-docs-count' ); } $bp->bp_options_nav[$group_slug][ $docs_slug ]['name'] = sprintf( __( '%s %d', 'bp-docs' ), $current_tab_name, $doc_count );