Ticket #1799: 1799.12.voice_counts.patch
File 1799.12.voice_counts.patch, 22.7 KB (added by , 8 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() { 1026 1026 $statement = __( 'Counting the number of voices in each topic… %s', 'bbpress' ); 1027 1027 $result = __( 'Failed!', 'bbpress' ); 1028 1028 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' );"; 1030 1031 if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) { 1031 1032 return array( 1, sprintf( $statement, $result ) ); 1032 1033 } … … function bbp_admin_repair_topic_voice_count() { 1037 1038 $pps = bbp_get_public_status_id(); 1038 1039 $cps = bbp_get_closed_status_id(); 1039 1040 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`;"; 1050 1049 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 ) ) { 1052 1054 return array( 2, sprintf( $statement, $result ) ); 1053 1055 } 1054 1056 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 1055 1085 return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) ); 1056 1086 } 1057 1087 -
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' ); 319 319 add_action( 'bbp_spam_topic', 'bbp_spam_topic_replies' ); 320 320 add_action( 'bbp_unspam_topic', 'bbp_unspam_topic_replies' ); 321 321 322 // Voice counts. 323 add_action( 'bbp_new_reply', 'bbp_maybe_increase_topic_voice_count' ); 324 add_action( 'bbp_new_topic', 'bbp_maybe_increase_topic_voice_count' ); 325 add_action( 'bbp_approved_reply', 'bbp_maybe_increase_topic_voice_count' ); 326 add_action( 'bbp_unspammed_reply', 'bbp_maybe_increase_topic_voice_count' ); 327 add_action( 'bbp_untrashed_reply', 'bbp_maybe_increase_topic_voice_count' ); 328 add_action( 'bbp_unapproved_reply', 'bbp_maybe_decrease_topic_voice_count' ); 329 add_action( 'bbp_spammed_reply', 'bbp_maybe_decrease_topic_voice_count' ); 330 add_action( 'bbp_trashed_reply', 'bbp_maybe_decrease_topic_voice_count' ); 331 332 // Insert topic/reply voice counts. 333 add_action( 'bbp_insert_topic', 'bbp_maybe_increase_topic_voice_count' ); 334 add_action( 'bbp_insert_reply', 'bbp_maybe_increase_topic_voice_count', 10, 2 ); 335 322 336 // User status 323 337 // @todo make these sub-actions 324 338 add_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 = 1018 1018 // See https://bbpress.trac.wordpress.org/ticket/2838 1019 1019 bbp_update_topic_last_active_time( $ancestor, $topic_last_active_time ); 1020 1020 1021 // Counts1022 bbp_update_topic_voice_count( $ancestor );1023 1024 1021 // Only update reply count if we're deleting a reply, or in the dashboard. 1025 1022 if ( in_array( current_filter(), array( 'bbp_deleted_reply', 'save_post' ), true ) ) { 1026 1023 bbp_update_topic_reply_count( $ancestor ); 1027 1024 bbp_update_topic_reply_count_hidden( $ancestor ); 1025 bbp_update_topic_voice_count( $ancestor ); 1028 1026 } 1029 1027 1030 1028 // 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..b78be0f 100644
function bbp_insert_topic_update_counts( $topic_id = 0, $forum_id = 0 ) { 2606 2606 } 2607 2607 } 2608 2608 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 bbp_get_reply_author_id() To get reply author id. 2622 * @uses bbp_is_reply_published() To determine if the reply is published. 2623 * @uses bbp_get_topic_author_id() To get topic author id. 2624 * @uses bbp_is_topic_published() To determine if the topic is published. 2625 * @uses bbp_get_topic_voice_ids() To get the topic voice ids array. 2626 * @uses update_post_meta() To update our voice count/ids meta values. 2627 * @uses apply_filters() To update our voice count/ids meta values. 2628 * 2629 * @return bool|int The topic voice count. False on failure. 2630 */ 2631 function bbp_maybe_bump_topic_voice_count( $topic_id = 0, $reply_id = 0, $action = '' ) { 2632 2633 // Valid our topic/reply ids. 2634 $topic_id = bbp_get_topic_id( $topic_id ); 2635 $reply_id = bbp_get_reply_id( $reply_id ); 2636 2637 // Bail early if we don't have valid ids. 2638 if ( empty( $topic_id ) && empty( $reply_id ) ) { 2639 return false; 2640 } 2641 2642 // Bail if we don't have a valid action. 2643 if ( ! in_array( $action, array( 'increase', 'decrease' ), true ) ) { 2644 return false; 2645 } 2646 2647 // Make sure we have a topic id. 2648 if ( empty( $topic_id ) ) { 2649 $topic_id = bbp_get_reply_topic_id( $reply_id ); 2650 } 2651 2652 // Set some defaults. 2653 $published = $anonymous = false; 2654 $author = 0; 2655 2656 // Possibly update our author and published variables. 2657 if ( ! empty( $reply_id ) ) { 2658 $author = bbp_get_reply_author_id( $reply_id ); 2659 $anonymous = empty( $author ); 2660 2661 // Don't waste a potential call to the db, if it won't be counted anyway. 2662 if ( ! $anonymous ) { 2663 $published = bbp_is_reply_published( $reply_id ); 2664 } 2665 } else { 2666 $author = bbp_get_topic_author_id( $topic_id ); 2667 $published = bbp_is_topic_published( $topic_id ); 2668 } 2669 2670 // Get the topic voice ids. 2671 $voice_ids = bbp_get_topic_voice_ids( $topic_id ); 2672 2673 // If we have a valid author and we're published, update the voice ids array. 2674 if ( $published && 'increase' === $action && ! $anonymous ) { 2675 $voice_ids[] = $author; 2676 } elseif ( ! $published && 'decrease' === $action && ! $anonymous ) { 2677 unset( $voice_ids[ array_search( $author, $voice_ids, true ) ] ); 2678 } 2679 2680 // Count the unique voices. 2681 $voice_count = count( array_unique( $voice_ids ) ); 2682 2683 // Concentate our voice ids back to a string for the db. 2684 $voice_ids = implode( ',', $voice_ids ); 2685 2686 // Only update voice ids and count for a topic if it's not an anonymous reply. 2687 if ( ! $anonymous ) { 2688 update_post_meta( $topic_id, '_bbp_voice_ids', $voice_ids ); 2689 update_post_meta( $topic_id, '_bbp_voice_count', $voice_count ); 2690 } 2691 2692 /** 2693 * Filters the return of a voice count bump for a topic. 2694 * 2695 * @since 2.6.0 bbPress (rXXXX) 2696 * 2697 * @param int $voice_count The topic voice count. 2698 * @param array $voice_ids Array of voice ids. 2699 * @param int $topic_id The topic id. 2700 * @param int $reply_id The reply id. 2701 * @param string $action The action being taken (`increase` or `decrease`). 2702 */ 2703 return (int) apply_filters( 'bbp_maybe_bump_topic_voice_count', $voice_count, $voice_ids, $topic_id, $reply_id, $action ); 2704 } 2705 2706 /** 2707 * Maybe increase the topic voice count. 2708 * 2709 * @since 2.6.0 bbPress (rXXXX) 2710 * 2711 * @param int $topic_id The topic id. 2712 * @param int $reply_id The reply id. 2713 * 2714 * @uses bbp_is_reply() To determine if passed topic id is actually a reply. 2715 * @uses bbp_get_reply_id() To validate the reply id. 2716 * @uses bbp_get_reply_topic_id() To get the reply's topic id. 2717 * @uses bbp_is_topic() To determine if passed topic id is actually a topic. 2718 * @uses bbp_get_topic_id() To validate the topic id. 2719 * @uses bbp_maybe_bump_topic_voice_count() To maybe increase the topic voice count. 2720 * 2721 * @return bool|int The topic voice count. False on failure. 2722 */ 2723 function bbp_maybe_increase_topic_voice_count( $topic_id = 0, $reply_id = 0 ) { 2724 2725 // If it's a reply, then get the topic id, and validate the reply id. 2726 if ( bbp_is_reply( $topic_id ) ) { 2727 $reply_id = bbp_get_reply_id( $topic_id ); 2728 $topic_id = bbp_get_reply_topic_id( $reply_id ); 2729 2730 // If it's a topic, then validate the passed ids. 2731 } elseif ( bbp_is_topic( $topic_id ) ) { 2732 $reply_id = bbp_get_reply_id( $reply_id ); 2733 $topic_id = bbp_get_topic_id( $topic_id ); 2734 2735 // Bail if the passed id isn't a topic or reply. 2736 } else { 2737 return false; 2738 } 2739 2740 return bbp_maybe_bump_topic_voice_count( $topic_id, $reply_id, 'increase' ); 2741 } 2742 2743 /** 2744 * Maybe decrease the topic voice count. 2745 * 2746 * @since 2.6.0 bbPress (rXXXX) 2747 * 2748 * @param int $topic_id The reply id. 2749 * @param int $reply_id The topic id. 2750 * 2751 * @uses bbp_is_reply() To determine if passed topic id is actually a reply. 2752 * @uses bbp_get_reply_id() To validate the reply id. 2753 * @uses bbp_get_reply_topic_id() To get the reply's topic id. 2754 * @uses bbp_is_topic() To determine if passed topic id is actually a topic. 2755 * @uses bbp_get_topic_id() To validate the topic id. 2756 * @uses bbp_maybe_bump_topic_voice_count() To maybe decrease the topic voice count. 2757 * 2758 * @return bool|int The topic voice count. False on failure. 2759 */ 2760 function bbp_maybe_decrease_topic_voice_count( $topic_id = 0, $reply_id = 0 ) { 2761 2762 // If it's a reply, then get the topic id, and validate the reply id. 2763 if ( bbp_is_reply( $topic_id ) ) { 2764 $reply_id = bbp_get_reply_id( $topic_id ); 2765 $topic_id = bbp_get_reply_topic_id( $reply_id ); 2766 2767 // If it's a topic, then validate the passed ids. 2768 } elseif ( bbp_is_topic( $topic_id ) ) { 2769 $reply_id = bbp_get_reply_id( $reply_id ); 2770 $topic_id = bbp_get_topic_id( $topic_id ); 2771 2772 // Bail if the passed id isn't a topic or reply. 2773 } else { 2774 return false; 2775 } 2776 2777 return bbp_maybe_bump_topic_voice_count( $topic_id, $reply_id, 'decrease' ); 2778 } 2779 2609 2780 /** Topic Updaters ************************************************************/ 2610 2781 2611 2782 /** … … function bbp_update_topic_voice_count( $topic_id = 0 ) { 2913 3084 2914 3085 // Query the DB to get voices in this topic 2915 3086 $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 ); 3087 $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() ); 3088 $voice_ids = $bbp_db->get_col( $query ); 3089 $voice_ids = array_map( 'absint', (array) $voice_ids ); 3090 $count = count( array_unique( $voice_ids ) ); 2918 3091 2919 // Update the voice count for this topic id 2920 update_post_meta( $topic_id, '_bbp_voice_count', $voices ); 3092 // Update the voice ids and count for this topic id 3093 update_post_meta( $topic_id, '_bbp_published_voice_ids', implode( ',', $voice_ids ) ); 3094 update_post_meta( $topic_id, '_bbp_voice_count', $count ); 2921 3095 2922 return (int) apply_filters( 'bbp_update_topic_voice_count', $ voices, $topic_id );3096 return (int) apply_filters( 'bbp_update_topic_voice_count', $count, $topic_id ); 2923 3097 } 2924 3098 2925 3099 /** -
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 ) { 2354 2354 } 2355 2355 2356 2356 /** 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 */ 2378 function 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 /** 2357 2421 * Output a the tags of a topic 2358 2422 * 2359 2423 * @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..9bd25e9 100644
class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 557 557 $count = bbp_update_topic_voice_count( $t ); 558 558 $this->assertSame( 2, $count ); 559 559 560 $voices = bbp_get_topic_voice_ids( $t ); 561 $this->assertEquals( array( bbp_get_current_user_id(), $u[0] ), $voices ); 562 560 563 $r = $this->factory->reply->create( array( 561 564 'post_author' => $u[1], 562 565 'post_parent' => $t, … … class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 565 568 bbp_update_topic_voice_count( $t ); 566 569 $count = bbp_get_topic_voice_count( $t ); 567 570 $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 ); 568 574 } 569 575 570 576 /** … … class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 577 583 'This test has not been implemented yet.' 578 584 ); 579 585 } 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_decrease_topic_voice_count 698 */ 699 public function test_bbp_maybe_decrease_topic_voice_count() { 700 $u = $this->factory->user->create_many( 2 ); 701 $t = $this->factory->topic->create(); 702 703 $count = bbp_get_topic_voice_count( $t ); 704 $this->assertSame( '1', $count ); 705 706 $voices = bbp_get_topic_voice_ids( $t ); 707 $this->assertEquals( array( bbp_get_current_user_id() ), $voices ); 708 709 $r1 = $this->factory->reply->create( array( 710 'post_author' => $u[0], 711 'post_parent' => $t, 712 ) ); 713 $r2 = $this->factory->reply->create( array( 714 'post_author' => $u[1], 715 'post_parent' => $t, 716 ) ); 717 $r3 = $this->factory->reply->create( array( 718 'post_author' => $u[1], 719 'post_parent' => $t, 720 ) ); 721 722 remove_action( 'bbp_unapproved_reply', 'bbp_maybe_decrease_topic_voice_count' ); 723 724 bbp_unapprove_reply( $r3 ); 725 726 add_action( 'bbp_unapproved_reply', 'bbp_maybe_decrease_topic_voice_count' ); 727 728 bbp_maybe_decrease_topic_voice_count( $t, $r3 ); 729 $count = bbp_get_topic_voice_count( $t ); 730 $this->assertSame( '3', $count ); 731 732 $voices = bbp_get_topic_voice_ids( $t ); 733 $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1] ), $voices ); 734 735 bbp_unapprove_reply( $r1 ); 736 737 $count = bbp_get_topic_voice_count( $t ); 738 $this->assertSame( '2', $count ); 739 740 $voices = bbp_get_topic_voice_ids( $t ); 741 $this->assertEquals( array( bbp_get_current_user_id(), $u[1] ), $voices ); 742 } 743 744 /** 745 * @covers ::bbp_get_topic_voice_ids 746 */ 747 public function test_bbp_get_topic_voice_ids() { 748 $u = $this->factory->user->create_many( 2 ); 749 $t = $this->factory->topic->create(); 750 751 $voices = bbp_get_topic_voice_ids( $t ); 752 $this->assertEquals( array( bbp_get_current_user_id() ), $voices ); 753 754 $r1 = $this->factory->reply->create( array( 755 'post_author' => $u[0], 756 'post_parent' => $t, 757 ) ); 758 759 $voices = bbp_get_topic_voice_ids( $t ); 760 $this->assertEquals( array( bbp_get_current_user_id(), $u[0] ), $voices ); 761 762 $r2 = $this->factory->reply->create_many( 2, array( 763 'post_author' => $u[1], 764 'post_parent' => $t, 765 ) ); 766 767 $voices = bbp_get_topic_voice_ids( $t ); 768 $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1], $u[1] ), $voices ); 769 770 bbp_unapprove_reply( $r2[0] ); 771 772 $voices = bbp_get_topic_voice_ids( $t ); 773 $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1] ), $voices ); 774 } 580 775 }