Skip to:
Content

bbPress.org

Ticket #1799: 1799.13.voice_counts.patch

File 1799.13.voice_counts.patch, 24.2 KB (added by thebrandonallen, 9 years ago)
  • src/includes/admin/tools.php

    diff --git src/includes/admin/tools.php src/includes/admin/tools.php
    index 729dc81..7457717 100644
    function bbp_admin_repair_topic_voice_count() { 
    10261026        $statement = __( 'Counting the number of voices in each topic… %s', 'bbpress' );
    10271027        $result    = __( 'Failed!', 'bbpress' );
    10281028
    1029         $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_voice_count';";
     1029        // Delete our old counts and ids.
     1030        $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` IN ( '_bbp_voice_count', '_bbp_voice_ids' );";
    10301031        if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    10311032                return array( 1, sprintf( $statement, $result ) );
    10321033        }
    function bbp_admin_repair_topic_voice_count() { 
    10371038        $pps = bbp_get_public_status_id();
    10381039        $cps = bbp_get_closed_status_id();
    10391040
    1040         $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
    1041                         SELECT `postmeta`.`meta_value`, '_bbp_voice_count', COUNT(DISTINCT `post_author`) as `meta_value`
    1042                                 FROM `{$bbp_db->posts}` AS `posts`
    1043                                 LEFT JOIN `{$bbp_db->postmeta}` AS `postmeta`
    1044                                         ON `posts`.`ID` = `postmeta`.`post_id`
    1045                                         AND `postmeta`.`meta_key` = '_bbp_topic_id'
    1046                                 WHERE `posts`.`post_type` IN ( '{$tpt}', '{$rpt}' )
    1047                                         AND `posts`.`post_status` IN ( '{$pps}', '{$cps}' )
    1048                                         AND `posts`.`post_author` != '0'
    1049                                 GROUP BY `postmeta`.`meta_value`);";
     1041        $sql = "SELECT `postmeta`.`meta_value` AS `id`, COUNT(DISTINCT `post_author`) AS `count`, GROUP_CONCAT( `post_author` ) AS `voices`
     1042                        FROM `{$bbp_db->posts}` AS `posts`
     1043                        LEFT JOIN `{$bbp_db->postmeta}` AS `postmeta`
     1044                                ON `posts`.`ID` = `postmeta`.`post_id`
     1045                                AND `postmeta`.`meta_key` = '_bbp_topic_id'
     1046                        WHERE ( `posts`.`post_type` = '{$tpt}' AND `posts`.`post_status` IN ( '{$pps}', '{$cps}' ) )
     1047                                OR ( `posts`.`post_type` = '{$rpt}' AND `posts`.`post_status` = '{$pps}' AND `posts`.`post_author` != '0' )
     1048                        GROUP BY `postmeta`.`meta_value`;";
    10501049
    1051         if ( is_wp_error( $bbp_db->query( $sql ) ) ) {
     1050        // Get our counts and voice ids.
     1051        $results = $bbp_db->get_results( $sql );
     1052
     1053        if ( is_wp_error( $results ) ) {
    10521054                return array( 2, sprintf( $statement, $result ) );
    10531055        }
    10541056
     1057        // Set up our MySQL VALUES groups.
     1058        $values = array();
     1059        foreach ( $results as $key => $result ) {
     1060
     1061                // If we don't have a valid post id, bail.
     1062                if ( empty( $result->id ) ) {
     1063                        // Unset results item to manage memory.
     1064                        unset( $results[ $key ] );
     1065                        continue;
     1066                }
     1067
     1068                $values[] = "( {$result->id}, '_bbp_voice_count', '{$result->count}' )";
     1069                $values[] = "( {$result->id}, '_bbp_voice_ids', '{$result->voices}' )";
     1070
     1071                // Unset results item to manage memory.
     1072                unset( $results[ $key ] );
     1073        }
     1074
     1075        // Insert our meta roles.
     1076        $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) VALUES " . implode( ',', $values ) . ";";
     1077
     1078        // Unset values array to manage memory. Probably not needed, but can't hurt.
     1079        unset( $values );
     1080
     1081        if ( is_wp_error( $bbp_db->query( $sql ) ) ) {
     1082                return array( 3, sprintf( $statement, $result ) );
     1083        }
     1084
    10551085        return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    10561086}
    10571087
  • src/includes/core/actions.php

    diff --git src/includes/core/actions.php src/includes/core/actions.php
    index c9efadb..29bc88a 100644
    add_action( 'bbp_delete_topic', 'bbp_delete_topic_replies' ); 
    319319add_action( 'bbp_spam_topic',    'bbp_spam_topic_replies'    );
    320320add_action( 'bbp_unspam_topic',  'bbp_unspam_topic_replies'  );
    321321
     322// Voice counts.
     323add_action( 'bbp_new_reply',        'bbp_maybe_increase_topic_voice_count' );
     324add_action( 'bbp_new_topic',        'bbp_maybe_increase_topic_voice_count' );
     325add_action( 'bbp_approved_reply',   'bbp_maybe_increase_topic_voice_count' );
     326add_action( 'bbp_unspammed_reply',  'bbp_maybe_increase_topic_voice_count' );
     327add_action( 'bbp_untrashed_reply',  'bbp_maybe_increase_topic_voice_count' );
     328add_action( 'bbp_unapproved_reply', 'bbp_maybe_decrease_topic_voice_count' );
     329add_action( 'bbp_spammed_reply',    'bbp_maybe_decrease_topic_voice_count' );
     330add_action( 'bbp_trashed_reply',    'bbp_maybe_decrease_topic_voice_count' );
     331
     332// Insert topic/reply voice counts.
     333add_action( 'bbp_insert_topic', 'bbp_maybe_increase_topic_voice_count'        );
     334add_action( 'bbp_insert_reply', 'bbp_maybe_increase_topic_voice_count', 10, 2 );
     335
    322336// User status
    323337// @todo make these sub-actions
    324338add_action( 'make_ham_user',  'bbp_make_ham_user'  );
  • src/includes/replies/functions.php

    diff --git src/includes/replies/functions.php src/includes/replies/functions.php
    index bb66f52..31e8024 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 1f5089e..97e82ce 100644
    function bbp_insert_topic_update_counts( $topic_id = 0, $forum_id = 0 ) { 
    26062606        }
    26072607}
    26082608
     2609/**
     2610 * Maybe bump the topic voice count.
     2611 *
     2612 * @since 2.6.0 bbPress (rXXXX)
     2613 *
     2614 * @param int    $topic_id The reply id.
     2615 * @param int    $reply_id The topic id.
     2616 * @param string $action   Expected `increase` or `decrease`.
     2617 *
     2618 * @uses bbp_get_topic_id() To validate the topic id.
     2619 * @uses bbp_get_reply_id() To validate the reply id.
     2620 * @uses bbp_get_reply_topic_id() To get the reply's topic id.
     2621 * @uses get_post_field() To get `the post_author` field.
     2622 * @uses bbp_is_reply_published() To determine if the reply is published.
     2623 * @uses bbp_is_topic_published() To determine if the topic is published.
     2624 * @uses bbp_get_topic_voice_ids() To get the topic voice ids array.
     2625 * @uses update_post_meta() To update our voice count/ids meta values.
     2626 * @uses apply_filters() To update our voice count/ids meta values.
     2627 *
     2628 * @return bool|int The topic voice count. False on failure.
     2629 */
     2630function bbp_maybe_bump_topic_voice_count( $topic_id = 0, $reply_id = 0, $action = '' ) {
     2631
     2632        // Valid our topic/reply ids.
     2633        $topic_id = bbp_get_topic_id( $topic_id );
     2634        $reply_id = bbp_get_reply_id( $reply_id );
     2635
     2636        // Bail early if we don't have valid ids.
     2637        if ( empty( $topic_id ) && empty( $reply_id ) ) {
     2638                return false;
     2639        }
     2640
     2641        // Bail if we don't have a valid action.
     2642        if ( ! in_array( $action, array( 'increase', 'decrease' ), true ) ) {
     2643                return false;
     2644        }
     2645
     2646        // Make sure we have a topic id.
     2647        if ( empty( $topic_id ) ) {
     2648                $topic_id = bbp_get_reply_topic_id( $reply_id );
     2649        }
     2650
     2651        // Set some defaults.
     2652        $published = $anonymous = false;
     2653        $author    = 0;
     2654
     2655        // Possibly update our author and published variables.
     2656        if ( ! empty( $reply_id ) ) {
     2657                $author    = bbp_get_reply_author_id( $reply_id );
     2658                $anonymous = empty( $author );
     2659
     2660                // Don't waste a potential call to the db, if it won't be counted anyway.
     2661                if ( ! $anonymous ) {
     2662                        $published = bbp_is_reply_published( $reply_id );
     2663                }
     2664        } else {
     2665                $author    = bbp_get_topic_author_id( $topic_id );
     2666                $published = bbp_is_topic_published( $topic_id );
     2667        }
     2668
     2669        // Get the topic voice ids.
     2670        $voice_ids = bbp_get_topic_voice_ids( $topic_id );
     2671
     2672        // If we have a valid author and we're published, update the voice ids array.
     2673        if ( $published && 'increase' === $action && ! $anonymous ) {
     2674                $voice_ids[] = $author;
     2675        } elseif ( ! $published && 'decrease' === $action && ! $anonymous ) {
     2676                unset( $voice_ids[ array_search( $author, $voice_ids, true ) ] );
     2677        }
     2678
     2679        // Count the unique voices.
     2680        $voice_count = count( array_unique( $voice_ids ) );
     2681
     2682        // Concentate our voice ids back to a string for the db.
     2683        $voice_ids = implode( ',', $voice_ids );
     2684
     2685        // Only update voice ids and count for a topic if it's not an anonymous reply.
     2686        if ( ! $anonymous ) {
     2687                update_post_meta( $topic_id, '_bbp_voice_ids', $voice_ids );
     2688                update_post_meta( $topic_id, '_bbp_voice_count', $voice_count );
     2689        }
     2690
     2691        /**
     2692         * Filters the return of a voice count bump for a topic.
     2693         *
     2694         * @since 2.6.0 bbPress (rXXXX)
     2695         *
     2696         * @param int    $voice_count The topic voice count.
     2697         * @param array  $voice_ids   Array of voice ids.
     2698         * @param int    $topic_id    The topic id.
     2699         * @param int    $reply_id    The reply id.
     2700         * @param string $action      The action being taken (`increase` or `decrease`).
     2701         */
     2702        return (int) apply_filters( 'bbp_maybe_bump_topic_voice_count', $voice_count, $voice_ids, $topic_id, $reply_id, $action );
     2703}
     2704
     2705/**
     2706 * Maybe increase the topic voice count.
     2707 *
     2708 * @since 2.6.0 bbPress (rXXXX)
     2709 *
     2710 * @param int $topic_id The topic id.
     2711 * @param int $reply_id The reply id.
     2712 *
     2713 * @uses bbp_is_reply() To determine if passed topic id is actually a reply.
     2714 * @uses bbp_get_reply_id() To validate the reply id.
     2715 * @uses bbp_get_reply_topic_id() To get the reply's topic id.
     2716 * @uses bbp_is_topic() To determine if passed topic id is actually a topic.
     2717 * @uses bbp_get_topic_id() To validate the topic id.
     2718 * @uses bbp_maybe_bump_topic_voice_count() To maybe increase the topic voice count.
     2719 *
     2720 * @return bool|int The topic voice count. False on failure.
     2721 */
     2722function bbp_maybe_increase_topic_voice_count( $topic_id = 0, $reply_id = 0 ) {
     2723
     2724        // If it's a reply, then get the topic id, and validate the reply id.
     2725        if ( bbp_is_reply( $topic_id ) ) {
     2726                $reply_id = bbp_get_reply_id( $topic_id );
     2727                $topic_id = bbp_get_reply_topic_id( $reply_id );
     2728
     2729        // If it's a topic, then validate the passed ids.
     2730        } elseif ( bbp_is_topic( $topic_id ) ) {
     2731                $reply_id = bbp_get_reply_id( $reply_id );
     2732                $topic_id = bbp_get_topic_id( $topic_id );
     2733
     2734        // Bail if the passed id isn't a topic or reply.
     2735        } else {
     2736                return false;
     2737        }
     2738
     2739        return bbp_maybe_bump_topic_voice_count( $topic_id, $reply_id, 'increase' );
     2740}
     2741
     2742/**
     2743 * Maybe decrease the topic voice count.
     2744 *
     2745 * @since 2.6.0 bbPress (rXXXX)
     2746 *
     2747 * @param int $topic_id The reply id.
     2748 * @param int $reply_id The topic id.
     2749 *
     2750 * @uses bbp_is_reply() To determine if passed topic id is actually a reply.
     2751 * @uses bbp_get_reply_id() To validate the reply id.
     2752 * @uses bbp_get_reply_topic_id() To get the reply's topic id.
     2753 * @uses bbp_is_topic() To determine if passed topic id is actually a topic.
     2754 * @uses bbp_get_topic_id() To validate the topic id.
     2755 * @uses bbp_maybe_bump_topic_voice_count() To maybe decrease the topic voice count.
     2756 *
     2757 * @return bool|int The topic voice count. False on failure.
     2758 */
     2759function bbp_maybe_decrease_topic_voice_count( $topic_id = 0, $reply_id = 0 ) {
     2760
     2761        // If it's a reply, then get the topic id, and validate the reply id.
     2762        if ( bbp_is_reply( $topic_id ) ) {
     2763                $reply_id = bbp_get_reply_id( $topic_id );
     2764                $topic_id = bbp_get_reply_topic_id( $reply_id );
     2765
     2766        // If it's a topic, then validate the passed ids.
     2767        } elseif ( bbp_is_topic( $topic_id ) ) {
     2768                $reply_id = bbp_get_reply_id( $reply_id );
     2769                $topic_id = bbp_get_topic_id( $topic_id );
     2770
     2771        // Bail if the passed id isn't a topic or reply.
     2772        } else {
     2773                return false;
     2774        }
     2775
     2776        return bbp_maybe_bump_topic_voice_count( $topic_id, $reply_id, 'decrease' );
     2777}
     2778
    26092779/** Topic Updaters ************************************************************/
    26102780
    26112781/**
    function bbp_update_topic_voice_count( $topic_id = 0 ) { 
    29133083
    29143084        // Query the DB to get voices in this topic
    29153085        $bbp_db = bbp_db();
    2916         $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() );
    2917         $voices = (int) $bbp_db->get_var( $query );
     3086        $query  = $bbp_db->prepare( "SELECT post_author FROM {$bbp_db->posts} WHERE ( post_parent = %d AND post_status = '%s' AND post_type = '%s' AND post_author != 0 ) 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() );
     3087        $voice_ids = $bbp_db->get_col( $query );
     3088        $voice_ids = array_map( 'absint', (array) $voice_ids );
     3089        $count     = count( array_unique( $voice_ids ) );
    29183090
    2919         // Update the voice count for this topic id
    2920         update_post_meta( $topic_id, '_bbp_voice_count', $voices );
     3091        // Update the voice ids and count for this topic id
     3092        update_post_meta( $topic_id, '_bbp_published_voice_ids', implode( ',', $voice_ids ) );
     3093        update_post_meta( $topic_id, '_bbp_voice_count', $count );
    29213094
    2922         return (int) apply_filters( 'bbp_update_topic_voice_count', $voices, $topic_id );
     3095        return (int) apply_filters( 'bbp_update_topic_voice_count', $count, $topic_id );
    29233096}
    29243097
    29253098/**
  • src/includes/topics/template.php

    diff --git src/includes/topics/template.php src/includes/topics/template.php
    index 18270c9..1c516ef 100644
    function bbp_topic_voice_count( $topic_id = 0, $integer = false ) { 
    23542354        }
    23552355
    23562356/**
     2357 * Return an array of topic voice ids.
     2358 *
     2359 * All `post_author` ids of public replies, along with public and closed topics
     2360 * are included in this array. Authors of anonymous topics are counted as one
     2361 * voice. Anonymous replies are not included. Voice ids are stored in the
     2362 * post_meta table as a comma-separated list, and converted to an array before
     2363 * being returned.
     2364 *
     2365 * @since 2.6.0 bbPress (rXXXX)
     2366 *
     2367 * @param int $topic_id The topic id. Required.
     2368 *
     2369 * @uses bbp_get_topic_id() To validate the topic id.
     2370 * @uses get_post_meta() To get the `_bbp_voice_ids` meta value.
     2371 * @uses bbp_db() To get the wpdb object.
     2372 * @uses bbp_get_public_status_id() To get the public status id.
     2373 * @uses bbp_get_reply_post_type() To get the reply post type.
     2374 * @uses bbp_get_topic_post_type() To get the topic post type.
     2375 *
     2376 * @return bool|array An array of voice ids. False on failure.
     2377 */
     2378function bbp_get_topic_voice_ids( $topic_id = 0 ) {
     2379
     2380        // Validate the topic id.
     2381        $topic_id = bbp_get_topic_id( $topic_id );
     2382
     2383        // Bail if the topic id isn't valid.
     2384        if ( empty( $topic_id ) ) {
     2385                return false;
     2386        }
     2387
     2388        // Check the post meta table first.
     2389        $voice_ids = get_post_meta( $topic_id, '_bbp_voice_ids', true );
     2390
     2391        // If no voice ids, get them from the db.
     2392        if ( false === $voice_ids ) {
     2393                // Query the DB to get voices in this topic.
     2394                $bbp_db = bbp_db();
     2395                $query  = $bbp_db->prepare( "SELECT post_author FROM {$bbp_db->posts} WHERE ( post_parent = %d AND post_status = '%s' AND post_type = '%s' AND post_author != 0 ) 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() );
     2396                $voice_ids = $bbp_db->get_col( $query );
     2397        }
     2398
     2399        // Make sure we have an array.
     2400        if ( ! is_array( $voice_ids ) ) {
     2401                $voice_ids = explode( ',', $voice_ids );
     2402        }
     2403
     2404        // Clean up the voice ids array. We don't want empty strings converted to an
     2405        // integer of 0, so we need to filter these out.
     2406        $voice_ids = array_filter( array_map( 'trim', $voice_ids ), 'strlen' );
     2407        $voice_ids = array_map( 'absint', $voice_ids );
     2408
     2409        /**
     2410         * Filters the voice ids array for a topic.
     2411         *
     2412         * @since 2.6.0 bbPress (rXXXX)
     2413         *
     2414         * @param array $voice_ids Array of voice ids.
     2415         * @param int   $topic_id  The topic id.
     2416         */
     2417        return (array) apply_filters( 'bbp_get_topic_voice_ids', $voice_ids, $topic_id );
     2418}
     2419
     2420/**
    23572421 * Output a the tags of a topic
    23582422 *
    23592423 * @since 2.0.0 bbPress (r2688)
  • tests/phpunit/testcases/topics/functions/counts.php

    diff --git tests/phpunit/testcases/topics/functions/counts.php tests/phpunit/testcases/topics/functions/counts.php
    index 3524ae7..0e610f7 100644
    class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 
    557557                $count = bbp_update_topic_voice_count( $t );
    558558                $this->assertSame( 2, $count );
    559559
     560                $voices = bbp_get_topic_voice_ids( $t );
     561                $this->assertEquals( array( bbp_get_current_user_id(), $u[0] ), $voices );
     562
    560563                $r = $this->factory->reply->create( array(
    561564                        'post_author' => $u[1],
    562565                        'post_parent' => $t,
    class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 
    565568                bbp_update_topic_voice_count( $t );
    566569                $count = bbp_get_topic_voice_count( $t );
    567570                $this->assertSame( '3', $count );
     571
     572                $voices = bbp_get_topic_voice_ids( $t );
     573                $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1] ), $voices );
    568574        }
    569575
    570576        /**
    class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 
    577583                        'This test has not been implemented yet.'
    578584                );
    579585        }
     586
     587        /**
     588         * @covers ::bbp_maybe_bump_topic_voice_count
     589         */
     590        public function test_bbp_maybe_bump_topic_voice_count() {
     591                $u = $this->factory->user->create_many( 2 );
     592                $t = $this->factory->topic->create();
     593
     594                $count = bbp_get_topic_voice_count( $t );
     595                $this->assertSame( '1', $count );
     596
     597                $voices = bbp_get_topic_voice_ids( $t );
     598                $this->assertEquals( array( bbp_get_current_user_id() ), $voices );
     599
     600                remove_action( 'bbp_insert_reply', 'bbp_maybe_increase_topic_voice_count' );
     601
     602                $r1 = $this->factory->reply->create( array(
     603                        'post_author' => $u[0],
     604                        'post_parent' => $t,
     605                ) );
     606
     607                bbp_maybe_bump_topic_voice_count( $t, $r1, 'increase' );
     608                $count = bbp_get_topic_voice_count( $t );
     609                $this->assertSame( '2', $count );
     610
     611                $voices = bbp_get_topic_voice_ids( $t );
     612                $this->assertEquals( array( bbp_get_current_user_id(), $u[0] ), $voices );
     613
     614                $r2 = $this->factory->reply->create( array(
     615                        'post_author' => $u[1],
     616                        'post_parent' => $t,
     617                ) );
     618
     619                bbp_maybe_bump_topic_voice_count( $t, $r2, 'increase' );
     620                $count = bbp_get_topic_voice_count( $t );
     621                $this->assertSame( '3', $count );
     622
     623                $voices = bbp_get_topic_voice_ids( $t );
     624                $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1] ), $voices );
     625
     626                $r3 = $this->factory->reply->create( array(
     627                        'post_author' => $u[1],
     628                        'post_parent' => $t,
     629                ) );
     630
     631                bbp_maybe_bump_topic_voice_count( $t, $r3, 'increase' );
     632                $count = bbp_get_topic_voice_count( $t );
     633                $this->assertSame( '3', $count );
     634
     635                $voices = bbp_get_topic_voice_ids( $t );
     636                $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1], $u[1] ), $voices );
     637
     638                remove_action( 'bbp_unapproved_reply', 'bbp_maybe_decrease_topic_voice_count' );
     639
     640                bbp_unapprove_reply( $r1 );
     641
     642                add_action( 'bbp_unapproved_reply', 'bbp_maybe_decrease_topic_voice_count' );
     643
     644                bbp_maybe_bump_topic_voice_count( $t, $r1, 'decrease' );
     645                $count = bbp_get_topic_voice_count( $t );
     646                $this->assertSame( '2', $count );
     647
     648                $voices = bbp_get_topic_voice_ids( $t );
     649                $this->assertEquals( array( bbp_get_current_user_id(), $u[1], $u[1] ), $voices );
     650
     651                add_action( 'bbp_insert_reply', 'bbp_maybe_increase_topic_voice_count' );
     652        }
     653
     654        /**
     655         * @covers ::bbp_maybe_increase_topic_voice_count
     656         */
     657        public function test_bbp_maybe_increase_topic_voice_count() {
     658                $u = $this->factory->user->create_many( 2 );
     659                $t = $this->factory->topic->create();
     660
     661                $count = bbp_get_topic_voice_count( $t );
     662                $this->assertSame( '1', $count );
     663
     664                $voices = bbp_get_topic_voice_ids( $t );
     665                $this->assertEquals( array( bbp_get_current_user_id() ), $voices );
     666
     667                remove_action( 'bbp_insert_reply', 'bbp_maybe_increase_topic_voice_count' );
     668
     669                $r = $this->factory->reply->create( array(
     670                        'post_author' => $u[0],
     671                        'post_parent' => $t,
     672                ) );
     673
     674                bbp_maybe_increase_topic_voice_count( $t, $r );
     675                $count = bbp_get_topic_voice_count( $t );
     676                $this->assertSame( '2', $count );
     677
     678                $voices = bbp_get_topic_voice_ids( $t );
     679                $this->assertEquals( array( bbp_get_current_user_id(), $u[0] ), $voices );
     680
     681                $r = $this->factory->reply->create( array(
     682                        'post_author' => $u[1],
     683                        'post_parent' => $t,
     684                ) );
     685
     686                bbp_maybe_increase_topic_voice_count( $r );
     687                $count = bbp_get_topic_voice_count( $t );
     688                $this->assertSame( '3', $count );
     689
     690                $voices = bbp_get_topic_voice_ids( $t );
     691                $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1] ), $voices );
     692
     693                add_action( 'bbp_insert_reply', 'bbp_maybe_increase_topic_voice_count' );
     694        }
     695
     696        /**
     697         * @covers ::bbp_maybe_increase_topic_voice_count
     698         */
     699        public function test_bbp_maybe_increase_topic_voice_count_for_bbp_new_topic_or_reply() {
     700
     701                // We don't want `bbp_insert_(topic|reply)` to jump the gun.
     702                remove_action( 'bbp_insert_topic', 'bbp_maybe_increase_topic_voice_count' );
     703                remove_action( 'bbp_insert_reply', 'bbp_maybe_increase_topic_voice_count' );
     704
     705                $u = $this->factory->user->create_many( 2 );
     706                $t = $this->factory->topic->create( array(
     707                        'post_author' => $u[0],
     708                ) );
     709
     710                do_action( 'bbp_new_topic', $t, 0, array(), bbp_get_current_user_id() );
     711
     712                $count = bbp_get_topic_voice_count( $t );
     713                $this->assertSame( '1', $count );
     714
     715                $voices = bbp_get_topic_voice_ids( $t );
     716                $this->assertEquals( array( $u[0] ), $voices );
     717
     718                $r1 = $this->factory->reply->create( array(
     719                        'post_author' => $u[0],
     720                        'post_parent' => $t,
     721                ) );
     722
     723                do_action( 'bbp_new_reply', $r1, $t, 0, array(), $u[0] );
     724
     725                $count = bbp_get_topic_voice_count( $t );
     726                $this->assertSame( '1', $count );
     727
     728                $voices = bbp_get_topic_voice_ids( $t );
     729                $this->assertEquals( array( $u[0], $u[0] ), $voices );
     730
     731                $r2 = $this->factory->reply->create( array(
     732                        'post_author' => $u[1],
     733                        'post_parent' => $t,
     734                ) );
     735
     736                do_action( 'bbp_new_reply', $r2, $t, 0, array(), $u[1] );
     737
     738                $count = bbp_get_topic_voice_count( $t );
     739                $this->assertSame( '2', $count );
     740
     741                $voices = bbp_get_topic_voice_ids( $t );
     742                $this->assertEquals( array( $u[0], $u[0], $u[1] ), $voices );
     743
     744                add_action( 'bbp_insert_topic', 'bbp_maybe_increase_topic_voice_count' );
     745                add_action( 'bbp_insert_reply', 'bbp_maybe_increase_topic_voice_count' );
     746        }
     747
     748        /**
     749         * @covers ::bbp_maybe_decrease_topic_voice_count
     750         */
     751        public function test_bbp_maybe_decrease_topic_voice_count() {
     752                $u = $this->factory->user->create_many( 2 );
     753                $t = $this->factory->topic->create();
     754
     755                $count = bbp_get_topic_voice_count( $t );
     756                $this->assertSame( '1', $count );
     757
     758                $voices = bbp_get_topic_voice_ids( $t );
     759                $this->assertEquals( array( bbp_get_current_user_id() ), $voices );
     760
     761                $r1 = $this->factory->reply->create( array(
     762                        'post_author' => $u[0],
     763                        'post_parent' => $t,
     764                ) );
     765                $r2 = $this->factory->reply->create( array(
     766                        'post_author' => $u[1],
     767                        'post_parent' => $t,
     768                ) );
     769                $r3 = $this->factory->reply->create( array(
     770                        'post_author' => $u[1],
     771                        'post_parent' => $t,
     772                ) );
     773
     774                remove_action( 'bbp_unapproved_reply', 'bbp_maybe_decrease_topic_voice_count' );
     775
     776                bbp_unapprove_reply( $r3 );
     777
     778                add_action( 'bbp_unapproved_reply', 'bbp_maybe_decrease_topic_voice_count' );
     779
     780                bbp_maybe_decrease_topic_voice_count( $t, $r3 );
     781                $count = bbp_get_topic_voice_count( $t );
     782                $this->assertSame( '3', $count );
     783
     784                $voices = bbp_get_topic_voice_ids( $t );
     785                $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1] ), $voices );
     786
     787                bbp_unapprove_reply( $r1 );
     788
     789                $count = bbp_get_topic_voice_count( $t );
     790                $this->assertSame( '2', $count );
     791
     792                $voices = bbp_get_topic_voice_ids( $t );
     793                $this->assertEquals( array( bbp_get_current_user_id(), $u[1] ), $voices );
     794        }
     795
     796        /**
     797         * @covers ::bbp_get_topic_voice_ids
     798         */
     799        public function test_bbp_get_topic_voice_ids() {
     800                $u = $this->factory->user->create_many( 2 );
     801                $t = $this->factory->topic->create();
     802
     803                $voices = bbp_get_topic_voice_ids( $t );
     804                $this->assertEquals( array( bbp_get_current_user_id() ), $voices );
     805
     806                $r1 = $this->factory->reply->create( array(
     807                        'post_author' => $u[0],
     808                        'post_parent' => $t,
     809                ) );
     810
     811                $voices = bbp_get_topic_voice_ids( $t );
     812                $this->assertEquals( array( bbp_get_current_user_id(), $u[0] ), $voices );
     813
     814                $r2 = $this->factory->reply->create_many( 2, array(
     815                        'post_author' => $u[1],
     816                        'post_parent' => $t,
     817                ) );
     818
     819                $voices = bbp_get_topic_voice_ids( $t );
     820                $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1], $u[1] ), $voices );
     821
     822                bbp_unapprove_reply( $r2[0] );
     823
     824                $voices = bbp_get_topic_voice_ids( $t );
     825                $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1] ), $voices );
     826        }
    580827}