Skip to:
Content

bbPress.org

Ticket #1799: 1799.14.voice_counts.diff

File 1799.14.voice_counts.diff, 14.8 KB (added by thebrandonallen, 5 years ago)
  • src/includes/admin/tools/repair.php

    diff --git src/includes/admin/tools/repair.php src/includes/admin/tools/repair.php
    index 34cb8b9..e49cf5c 100644
    function bbp_admin_repair_topic_reply_count() { 
    227227 */
    228228function bbp_admin_repair_topic_voice_count() {
    229229
    230         // Define variables
     230        // Define variables.
    231231        $bbp_db    = bbp_db();
    232232        $statement = __( 'Counting the number of voices in each topic… %s', 'bbpress' );
    233233        $result    = __( 'Failed!', 'bbpress' );
    234234
    235         $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_voice_count'";
     235        $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key IN ('_bbp_voice_count', '_bbp_voice_id')";
    236236        if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    237237                return array( 1, sprintf( $statement, $result ) );
    238238        }
    239239
    240         // Post types and status
     240        // Post types and status.
    241241        $tpt = bbp_get_topic_post_type();
    242242        $rpt = bbp_get_reply_post_type();
    243243        $pps = bbp_get_public_status_id();
    244244        $cps = bbp_get_closed_status_id();
    245245
    246         $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
    247                         SELECT `postmeta`.`meta_value`, '_bbp_voice_count', COUNT(DISTINCT `post_author`) as `meta_value`
    248                                 FROM `{$bbp_db->posts}` AS `posts`
    249                                 LEFT JOIN `{$bbp_db->postmeta}` AS `postmeta`
    250                                         ON `posts`.`ID` = `postmeta`.`post_id`
    251                                         AND `postmeta`.`meta_key` = '_bbp_topic_id'
    252                                 WHERE `posts`.`post_type` IN ( '{$tpt}', '{$rpt}' )
    253                                         AND `posts`.`post_status` IN ( '{$pps}', '{$cps}' )
    254                                         AND `posts`.`post_author` != '0'
    255                                 GROUP BY `postmeta`.`meta_value`)";
     246        $voice_id_sql = $bbp_db->prepare( "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) (
     247                        SELECT postmeta.meta_value, '_bbp_voice_id', posts.post_author
     248                                FROM {$bbp_db->posts} AS posts
     249                                LEFT JOIN {$bbp_db->postmeta} AS postmeta
     250                                        ON posts.ID = postmeta.post_id
     251                                        AND postmeta.meta_key = '_bbp_topic_id'
     252                                WHERE posts.post_type IN (%s, %s)
     253                                        AND posts.post_status IN (%s, %s))", $tpt, $rpt, $pps, $cps );
    256254
    257         if ( is_wp_error( $bbp_db->query( $sql ) ) ) {
     255        if ( is_wp_error( $bbp_db->query( $voice_id_sql ) ) ) {
    258256                return array( 2, sprintf( $statement, $result ) );
    259257        }
    260258
     259        $voice_count_sql = "INSERT INTO {$bbp_db->postmeta} (post_id, meta_key, meta_value) (
     260                                SELECT post_id, '_bbp_voice_count', COUNT(DISTINCT meta_value)
     261                                        FROM {$bbp_db->postmeta}
     262                                        WHERE meta_key = '_bbp_voice_id'
     263                                        GROUP BY post_id ORDER BY post_id)";
     264
     265        if ( is_wp_error( $bbp_db->query( $voice_count_sql ) ) ) {
     266                return array( 3, sprintf( $statement, $result ) );
     267        }
     268
    261269        return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    262270}
    263271
  • src/includes/core/actions.php

    diff --git src/includes/core/actions.php src/includes/core/actions.php
    index 734d00d..5f1cf54 100644
    add_action( 'bbp_delete_topic', 'bbp_delete_topic_replies' ); 
    316316add_action( 'bbp_spam_topic',    'bbp_spam_topic_replies'    );
    317317add_action( 'bbp_unspam_topic',  'bbp_unspam_topic_replies'  );
    318318
     319// Voice counts.
     320add_action( 'bbp_new_reply',        'bbp_maybe_increase_topic_voice_count' );
     321add_action( 'bbp_new_topic',        'bbp_maybe_increase_topic_voice_count' );
     322add_action( 'bbp_approved_reply',   'bbp_maybe_increase_topic_voice_count' );
     323add_action( 'bbp_unspammed_reply',  'bbp_maybe_increase_topic_voice_count' );
     324add_action( 'bbp_untrashed_reply',  'bbp_maybe_increase_topic_voice_count' );
     325add_action( 'bbp_unapproved_reply', 'bbp_maybe_decrease_topic_voice_count' );
     326add_action( 'bbp_spammed_reply',    'bbp_maybe_decrease_topic_voice_count' );
     327add_action( 'bbp_trashed_reply',    'bbp_maybe_decrease_topic_voice_count' );
     328
     329// Insert topic/reply voice counts.
     330add_action( 'bbp_insert_topic', 'bbp_maybe_increase_topic_voice_count'        );
     331add_action( 'bbp_insert_reply', 'bbp_maybe_increase_topic_voice_count', 10, 2 );
     332
    319333// User status
    320334// @todo make these sub-actions
    321335add_action( 'make_ham_user',  'bbp_make_ham_user'  );
  • src/includes/core/update.php

    diff --git src/includes/core/update.php src/includes/core/update.php
    index b62cba8..986be53 100644
    function bbp_version_updater() { 
    326326                                bbp_admin_upgrade_user_favorites();
    327327                                bbp_admin_upgrade_user_topic_subscriptions();
    328328                                bbp_admin_upgrade_user_forum_subscriptions();
     329                                bbp_admin_repair_topic_voice_count();
    329330                        }
    330331                }
    331332        }
  • src/includes/replies/functions.php

    diff --git src/includes/replies/functions.php src/includes/replies/functions.php
    index 3296bd1..ee5a1ba 100644
    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                                 // 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 src/includes/topics/functions.php src/includes/topics/functions.php
    index 42c8a3a..94d5094 100644
    function bbp_update_topic( $topic_id = 0, $forum_id = 0, $anonymous_data = false 
    910910                bbp_update_topic_last_active_time   ( $topic_id, $last_active );
    911911                bbp_update_topic_reply_count        ( $topic_id, 0            );
    912912                bbp_update_topic_reply_count_hidden ( $topic_id, 0            );
    913                 bbp_update_topic_voice_count        ( $topic_id               );
    914913
    915914                // Walk up ancestors and do the dirty work
    916915                bbp_update_topic_walker( $topic_id, $last_active, $forum_id, 0, false );
    function bbp_insert_topic_update_counts( $topic_id = 0, $forum_id = 0 ) { 
    26602659        }
    26612660}
    26622661
     2662/**
     2663 * Maybe bump the topic voice count.
     2664 *
     2665 * @since 2.6.0 bbPress (rXXXX)
     2666 *
     2667 * @param int    $topic_id The reply id.
     2668 * @param int    $reply_id The topic id.
     2669 * @param string $action   Expected `increase` or `decrease`.
     2670 *
     2671 * @return void|int The topic voice count.
     2672 */
     2673function bbp_maybe_bump_topic_voice_count( $topic_id = 0, $reply_id = 0, $action = '' ) {
     2674
     2675        // Valid our topic/reply ids.
     2676        $topic_id = bbp_get_topic_id( $topic_id );
     2677        $reply_id = bbp_get_reply_id( $reply_id );
     2678
     2679        // Bail early if we don't have valid ids.
     2680        if ( empty( $topic_id ) && empty( $reply_id ) ) {
     2681                return;
     2682        }
     2683
     2684        // Bail if we don't have a valid action.
     2685        if ( ! in_array( $action, array( 'increase', 'decrease' ), true ) ) {
     2686                return;
     2687        }
     2688
     2689        // Make sure we have a topic id.
     2690        if ( empty( $topic_id ) ) {
     2691                $topic_id = bbp_get_reply_topic_id( $reply_id );
     2692        }
     2693
     2694        // Set some defaults.
     2695        $published = $anonymous = false;
     2696        $author_id = 0;
     2697
     2698        // Possibly update our author and published variables.
     2699        if ( empty( $reply_id ) ) {
     2700                $author_id = bbp_get_topic_author_id( $topic_id );
     2701                $published = bbp_is_topic_published( $topic_id );
     2702        } else {
     2703                $author_id = bbp_get_reply_author_id( $reply_id );
     2704                $published = bbp_is_reply_published( $reply_id );
     2705        }
     2706
     2707        // Get the topic voice ids, and voice count.
     2708        $voice_ids           = bbp_get_topic_voice_ids( $topic_id );
     2709        $current_voice_count = count( array_unique( $voice_ids ) );
     2710
     2711        // If we have a valid author and we're published, update the voice ids array.
     2712        if ( $published && 'increase' === $action ) {
     2713
     2714                // Add the post meta for new voice.
     2715                add_post_meta( $topic_id, '_bbp_voice_id', $author_id );
     2716
     2717                // Add the new author id to the voice ids array.
     2718                $voice_ids[] = $author_id;
     2719
     2720        } elseif ( ! $published && 'decrease' === $action ) {
     2721
     2722                // Add filter to only remove one instance of the `_bbp_voice_id` value.
     2723                $bbp_db = bbp_db();
     2724                $sql = $bbp_db->prepare( "SELECT meta_id FROM {$bbp_db->postmeta} WHERE post_id = %d AND meta_key = '_bbp_voice_id' AND meta_value = %d ORDER BY meta_id LIMIT 1", $topic_id, $author_id );
     2725                $mid = $bbp_db->get_var( $sql );
     2726
     2727                // Remove one instance of the author id from the post meta.
     2728                delete_metadata_by_mid( 'post', $mid );
     2729
     2730                // Remove one occurrence of the author id from the voice ids array.
     2731                $key = array_search( $author_id, $voice_ids, true );
     2732                if ( false !== $key ) {
     2733                        unset( $voice_ids[ $key ] );
     2734                }
     2735        }
     2736
     2737        // Count the unique voices.
     2738        $voice_count = count( array_unique( $voice_ids ) );
     2739
     2740        // Update the back-compat key if the count has changed.
     2741        if ( $voice_count !== $current_voice_count ) {
     2742                update_post_meta( $topic_id, '_bbp_voice_count', $voice_count );
     2743        }
     2744
     2745        /**
     2746         * Filters the return of a voice count bump for a topic.
     2747         *
     2748         * @since 2.6.0 bbPress (rXXXX)
     2749         *
     2750         * @param int    $voice_count The topic voice count.
     2751         * @param array  $voice_ids   Array of voice ids.
     2752         * @param int    $topic_id    The topic id.
     2753         * @param int    $reply_id    The reply id.
     2754         * @param string $action      The action being taken (`increase` or `decrease`).
     2755         */
     2756        return (int) apply_filters( 'bbp_maybe_bump_topic_voice_count', $voice_count, $voice_ids, $topic_id, $reply_id, $action );
     2757}
     2758
     2759/**
     2760 * Maybe increase the topic voice count.
     2761 *
     2762 * @since 2.6.0 bbPress (rXXXX)
     2763 *
     2764 * @param int $topic_id The topic id.
     2765 * @param int $reply_id The reply id.
     2766 *
     2767 * @return void|int The topic voice count.
     2768 */
     2769function bbp_maybe_increase_topic_voice_count( $topic_id = 0, $reply_id = 0 ) {
     2770
     2771        // If it's a reply, then get the topic id, and validate the reply id.
     2772        if ( bbp_is_reply( $topic_id ) ) {
     2773                $reply_id = bbp_get_reply_id( $topic_id );
     2774                $topic_id = bbp_get_reply_topic_id( $reply_id );
     2775
     2776        // If it's a topic, then validate the passed ids.
     2777        } elseif ( bbp_is_topic( $topic_id ) ) {
     2778                $reply_id = bbp_get_reply_id( $reply_id );
     2779                $topic_id = bbp_get_topic_id( $topic_id );
     2780
     2781        // Bail if the passed id isn't a topic or reply.
     2782        } else {
     2783                return;
     2784        }
     2785
     2786        return bbp_maybe_bump_topic_voice_count( $topic_id, $reply_id, 'increase' );
     2787}
     2788
     2789/**
     2790 * Maybe decrease the topic voice count.
     2791 *
     2792 * @since 2.6.0 bbPress (rXXXX)
     2793 *
     2794 * @param int $topic_id The reply id.
     2795 * @param int $reply_id The topic id.
     2796 *
     2797 * @return void|int The topic voice count.
     2798 */
     2799function bbp_maybe_decrease_topic_voice_count( $topic_id = 0, $reply_id = 0 ) {
     2800
     2801        // If it's a reply, then get the topic id, and validate the reply id.
     2802        if ( bbp_is_reply( $topic_id ) ) {
     2803                $reply_id = bbp_get_reply_id( $topic_id );
     2804                $topic_id = bbp_get_reply_topic_id( $reply_id );
     2805
     2806        // If it's a topic, then validate the passed ids.
     2807        } elseif ( bbp_is_topic( $topic_id ) ) {
     2808                $reply_id = bbp_get_reply_id( $reply_id );
     2809                $topic_id = bbp_get_topic_id( $topic_id );
     2810
     2811        // Bail if the passed id isn't a topic or reply.
     2812        } else {
     2813                return;
     2814        }
     2815
     2816        return bbp_maybe_bump_topic_voice_count( $topic_id, $reply_id, 'decrease' );
     2817}
     2818
    26632819/** Topic Updaters ************************************************************/
    26642820
    26652821/**
    function bbp_update_topic_last_reply_id( $topic_id = 0, $reply_id = 0 ) { 
    29523108 * @uses update_post_meta() To update the topic voice count meta
    29533109 * @uses apply_filters() Calls 'bbp_update_topic_voice_count' with the voice
    29543110 *                        count and topic id
    2955  * @return int Voice count
     3111 * @return void|int Voice count.
    29563112 */
    29573113function bbp_update_topic_voice_count( $topic_id = 0 ) {
    29583114
    function bbp_update_topic_voice_count( $topic_id = 0 ) { 
    29663122        }
    29673123
    29683124        // Query the DB to get voices in this topic
    2969         $bbp_db = bbp_db();
    2970         $query  = $bbp_db->prepare( "SELECT COUNT( DISTINCT post_author ) FROM {$bbp_db->posts} WHERE ( post_parent = %d AND post_status = %s AND post_type = %s ) OR ( ID = %d AND post_type = %s )", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() );
    2971         $voices = (int) $bbp_db->get_var( $query );
     3125        $bbp_db    = bbp_db();
     3126        $query     = $bbp_db->prepare( "SELECT post_author FROM {$bbp_db->posts} WHERE ( post_parent = %d AND post_status = %s AND post_type = %s ) OR ( ID = %d AND post_type = %s )", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() );
     3127        $voice_ids = $bbp_db->get_col( $query );
     3128        $voice_ids = array_map( 'intval', (array) $voice_ids );
     3129        $count     = count( array_unique( $voice_ids ) );
     3130
     3131        // Remove all voice id meta for the topic.
     3132        delete_post_meta( $topic_id, '_bbp_voice_id' );
     3133
     3134        // Update the voice ids and count for this topic id.
     3135        foreach( $voice_ids as $voice_id ) {
     3136                add_post_meta( $topic_id, '_bbp_voice_id', $voice_id );
     3137        }
     3138
     3139        // Update the voice count meta.
     3140        update_post_meta( $topic_id, '_bbp_voice_count', $count );
    29723141
    2973         // Update the voice count for this topic id
    2974         update_post_meta( $topic_id, '_bbp_voice_count', $voices );
     3142        $count = apply_filters( 'bbp_update_topic_voice_count', $count, $topic_id );
    29753143
    2976         return (int) apply_filters( 'bbp_update_topic_voice_count', $voices, $topic_id );
     3144        return bbp_number_not_negative( $count );
    29773145}
    29783146
    29793147/**
  • src/includes/topics/template.php

    diff --git src/includes/topics/template.php src/includes/topics/template.php
    index 785bcb3..61bf793 100644
    function bbp_topic_voice_count( $topic_id = 0, $integer = false ) { 
    23902390        }
    23912391
    23922392/**
     2393 * Return an array of topic voice ids.
     2394 *
     2395 * All `post_author` ids of public replies, along with public and closed topics
     2396 * are included in this array. Authors of anonymous topics are counted as one
     2397 * voice. Anonymous replies are not included. Voice ids are stored in the
     2398 * post_meta table as a comma-separated list, and converted to an array before
     2399 * being returned.
     2400 *
     2401 * @since 2.6.0 bbPress (rXXXX)
     2402 *
     2403 * @param int $topic_id The topic id. Required.
     2404 *
     2405 * @return void|array An array of voice ids. False on failure.
     2406 */
     2407function bbp_get_topic_voice_ids( $topic_id = 0 ) {
     2408
     2409        // Validate the topic id.
     2410        $topic_id = bbp_get_topic_id( $topic_id );
     2411
     2412        // Bail if the topic id isn't valid.
     2413        if ( empty( $topic_id ) ) {
     2414                return;
     2415        }
     2416
     2417        // Get the voice id post meta.
     2418        $voice_ids = get_post_meta( $topic_id, '_bbp_voice_id' );
     2419
     2420        /*
     2421         * Clean up the voice ids array. `wp_parse_id_list()` doesn't fit our needs
     2422         * here. We don't want empty strings converted to (int) 0, and we don't want
     2423         * the array to be trimmed to unique values only.
     2424         */
     2425        if ( ! empty( $voice_ids ) ) {
     2426                $voice_ids = array_filter( array_map( 'trim', $voice_ids ), 'strlen' );
     2427                $voice_ids = array_map( 'intval', $voice_ids );
     2428        } else {
     2429                $voice_ids = array();
     2430        }
     2431
     2432        /**
     2433         * Filters the voice ids array for a topic.
     2434         *
     2435         * @since 2.6.0 bbPress (rXXXX)
     2436         *
     2437         * @param array $voice_ids Array of voice ids.
     2438         * @param int   $topic_id  The topic id.
     2439         */
     2440        return (array) apply_filters( 'bbp_get_topic_voice_ids', $voice_ids, $topic_id );
     2441}
     2442
     2443/**
    23932444 * Output a the tags of a topic
    23942445 *
    23952446 * @since 2.0.0 bbPress (r2688)