Skip to:
Content

bbPress.org

Ticket #3068: 3068.1.voice_count_bump_functions.diff

File 3068.1.voice_count_bump_functions.diff, 7.3 KB (added by thebrandonallen, 8 years ago)
  • src/includes/core/actions.php

    diff --git a/src/includes/core/actions.php b/src/includes/core/actions.php
    index 734d00d..36cc78d 100644
    a b add_action( 'bbp_spam_reply', 'bbp_decrease_user_reply_count' ); 
    309309add_action( 'bbp_insert_topic', 'bbp_insert_topic_update_counts', 10, 2 );
    310310add_action( 'bbp_insert_reply', 'bbp_insert_reply_update_counts', 10, 3 );
    311311
     312// Update topic voice counts.
     313add_action( 'bbp_new_reply',        'bbp_maybe_increase_topic_voice_count' );
     314add_action( 'bbp_trashed_reply',    'bbp_maybe_decrease_topic_voice_count' );
     315add_action( 'bbp_untrashed_reply',  'bbp_maybe_increase_topic_voice_count' );
     316add_action( 'bbp_spammed_reply',    'bbp_maybe_decrease_topic_voice_count' );
     317add_action( 'bbp_unspammed_reply',  'bbp_maybe_increase_topic_voice_count' );
     318add_action( 'bbp_approved_reply',   'bbp_maybe_increase_topic_voice_count' );
     319add_action( 'bbp_unapproved_reply', 'bbp_maybe_decrease_topic_voice_count' );
     320
     321// Insert reply voice counts.
     322add_action( 'bbp_insert_reply', 'bbp_maybe_increase_topic_voice_count' );
     323
    312324// Topic status transition helpers for replies
    313325add_action( 'bbp_trash_topic',   'bbp_trash_topic_replies'   );
    314326add_action( 'bbp_untrash_topic', 'bbp_untrash_topic_replies' );
  • src/includes/replies/functions.php

    diff --git a/src/includes/replies/functions.php b/src/includes/replies/functions.php
    index 2cd7875..8a1d7c7 100644
    a b function bbp_update_reply_walker( $reply_id, $last_active_time = '', $forum_id = 
    10181018                                // See https://bbpress.trac.wordpress.org/ticket/2838
    10191019                                bbp_update_topic_last_active_time( $ancestor, $topic_last_active_time );
    10201020
    1021                                 // Always update voice counts
    1022                                 bbp_update_topic_voice_count( $ancestor );
    1023 
    10241021                                // Only update reply count if we're deleting a reply, or in the dashboard.
    10251022                                if ( in_array( current_filter(), array( 'bbp_deleted_reply', 'save_post' ), true ) ) {
    10261023                                        bbp_update_topic_reply_count(        $ancestor );
    10271024                                        bbp_update_topic_reply_count_hidden( $ancestor );
     1025                                        bbp_update_topic_voice_count(        $ancestor );
    10281026                                }
    10291027
    10301028                        // Forum meta relating to most recent topic
  • src/includes/topics/functions.php

    diff --git a/src/includes/topics/functions.php b/src/includes/topics/functions.php
    index 60889c5..c366ab0 100644
    a b function bbp_insert_topic_update_counts( $topic_id = 0, $forum_id = 0 ) { 
    26592659        }
    26602660}
    26612661
     2662/**
     2663 * Bump the voice count of a topic.
     2664 *
     2665 * @since x.x.x bbPress (rXXXX)
     2666 *
     2667 * @param int $topic_id   Optional. Topic id.
     2668 * @param int $difference Optional. Default 1.
     2669 *
     2670 * @return int Topic voice count.
     2671 */
     2672function bbp_bump_topic_voice_count( $topic_id = 0, $difference = 1 ) {
     2673
     2674        // Bail if no bump.
     2675        if ( empty( $difference ) ) {
     2676                return false;
     2677        }
     2678
     2679        // Get counts.
     2680        $topic_id    = bbp_get_topic_id( $topic_id );
     2681        $voice_count = bbp_get_topic_voice_count( $topic_id, true );
     2682        $difference  = (int) $difference;
     2683        $new_count   = (int) ( $voice_count + $difference );
     2684
     2685        // Update this topic id's voice count.
     2686        update_post_meta( $topic_id, '_bbp_voice_count', $new_count );
     2687
     2688        return (int) apply_filters( 'bbp_bump_topic_voice_count', $new_count, $topic_id, $difference );
     2689}
     2690
     2691/**
     2692 * Increase the voice count of a topic by one.
     2693 *
     2694 * @since x.x.x bbPress (rXXXX)
     2695 *
     2696 * @param int $topic_id The topic id.
     2697 *
     2698 * @return void
     2699 */
     2700function bbp_increase_topic_voice_count( $topic_id = 0 ) {
     2701
     2702        // Bail early if no id is passed.
     2703        if ( empty( $topic_id ) ) {
     2704                return;
     2705        }
     2706
     2707        // If it's a reply, get the topic id.
     2708        if ( bbp_is_reply( $topic_id ) ) {
     2709                $topic_id = bbp_get_reply_topic_id( $topic_id );
     2710        }
     2711
     2712        bbp_bump_topic_voice_count( $topic_id, 1 );
     2713}
     2714
     2715/**
     2716 * Decrease the voice count of a topic by one.
     2717 *
     2718 * @since x.x.x bbPress (rXXXX)
     2719 *
     2720 * @param int $topic_id The topic id.
     2721 *
     2722 * @return void
     2723 */
     2724function bbp_decrease_topic_voice_count( $topic_id = 0 ) {
     2725
     2726        // Bail early if no id is passed.
     2727        if ( empty( $topic_id ) ) {
     2728                return;
     2729        }
     2730
     2731        // If it's a reply, get the topic id.
     2732        if ( bbp_is_reply( $topic_id ) ) {
     2733                $topic_id = bbp_get_reply_topic_id( $topic_id );
     2734        }
     2735
     2736        bbp_bump_topic_voice_count( $topic_id, -1 );
     2737}
     2738
    26622739/** Topic Updaters ************************************************************/
    26632740
    26642741/**
    function bbp_update_topic_voice_count( $topic_id = 0 ) { 
    29933070}
    29943071
    29953072/**
     3073 * Maybe update the topic voice count.
     3074 *
     3075 * Runs additional checks over `bbp_update_topic_voice_count()` to only
     3076 * add/remove meta entries when absolutely necessary.
     3077 *
     3078 * @since x.x.x bbPress (rXXXX)
     3079 *
     3080 * @param int    $topic_id The topic id (or reply id).
     3081 * @param string $action   The action. Expects `increase` or `decrease`.
     3082 *
     3083 * @return void
     3084 */
     3085function bbp_maybe_update_topic_voice_count( $topic_id = 0, $action = '' ) {
     3086
     3087        // Make sure we have a valid action.
     3088        if ( ! in_array( $action, array( 'increase', 'decrease' ) ) ) {
     3089                return;
     3090        }
     3091
     3092        // If it's a reply, then get the parent (topic id).
     3093        if ( bbp_is_reply( $topic_id ) ) {
     3094                $author_id = bbp_get_reply_author_id( $topic_id );
     3095                $published = bbp_is_reply_published( $topic_id );
     3096                $topic_id  = bbp_get_reply_topic_id( $topic_id );
     3097        } elseif ( bbp_is_topic( $topic_id ) ) {
     3098                $author_id = bbp_get_topic_author_id( $topic_id );
     3099                $published = true;
     3100                $topic_id  = bbp_get_topic_id( $topic_id );
     3101        } else {
     3102                return;
     3103        }
     3104
     3105        // Get the current voice count.
     3106        $voice_count = bbp_get_topic_voice_count( $topic_id, true );
     3107
     3108        // Attempt to add an engagement.
     3109        if ( 'increase' === $action && $published ) {
     3110                bbp_add_user_engagement( $author_id, $topic_id );
     3111
     3112        // If we're decreasing, make sure the reply author isn't the topic author.
     3113        } elseif ( $author_id !== bbp_get_topic_author_id( $topic_id ) ) {
     3114
     3115                // Check for other replies by the user.
     3116                $replies = new WP_Query( array(
     3117                        'author'        => $author_id,
     3118                        'fields'        => 'ids',
     3119                        'nopaging'      => true,
     3120                        'no_found_rows' => true,
     3121                        'post_parent'   => $topic_id,
     3122                        'post_status'   => bbp_get_public_status_id(),
     3123                        'post_type'     => bbp_get_reply_post_type(),
     3124                ) );
     3125
     3126                // Remove the user engagement if you no more replies.
     3127                if ( 0 === ( count( $replies->posts ) ) ) {
     3128                        bbp_remove_user_engagement( $author_id, $topic_id );
     3129                }
     3130        }
     3131
     3132        // Get the engagements count, and calculate the difference.
     3133        $engagements_count = count( bbp_get_topic_engagements( $topic_id ) );
     3134        $difference        = (int) ( $engagements_count - $voice_count );
     3135
     3136        bbp_bump_topic_voice_count( $topic_id, $difference );
     3137}
     3138
     3139/**
     3140 * Maybe increase the topic voice count.
     3141 *
     3142 * @since x.x.x bbPress (rXXXX)
     3143 *
     3144 * @param int $topic_id The topic id (or reply id).
     3145 *
     3146 * @return void
     3147 */
     3148function bbp_maybe_increase_topic_voice_count( $topic_id = 0 ) {
     3149
     3150        if ( empty( $topic_id ) ) {
     3151                return;
     3152        }
     3153
     3154        bbp_maybe_update_topic_voice_count( $topic_id, 'increase' );
     3155}
     3156
     3157/**
     3158 * Maybe decrease the topic voice count.
     3159 *
     3160 * @since x.x.x bbPress (rXXXX)
     3161 *
     3162 * @param int $topic_id The topic id (or reply id).
     3163 *
     3164 * @return void
     3165 */
     3166function bbp_maybe_decrease_topic_voice_count( $topic_id = 0 ) {
     3167
     3168        if ( empty( $topic_id ) ) {
     3169                return;
     3170        }
     3171
     3172        bbp_maybe_update_topic_voice_count( $topic_id, 'decrease' );
     3173}
     3174
     3175/**
    29963176 * Adjust the total anonymous reply count of a topic
    29973177 *
    29983178 * @since 2.0.0 bbPress (r2567)