Ticket #1799: 1799.7.patch
File 1799.7.patch, 22.6 KB (added by , 9 years ago) |
---|
-
src/includes/admin/tools.php
diff --git src/includes/admin/tools.php src/includes/admin/tools.php index 5190542..e089275 100644
function bbp_admin_repair_topic_voice_count() { 1023 1023 $statement = __( 'Counting the number of voices in each topic… %s', 'bbpress' ); 1024 1024 $result = __( 'Failed!', 'bbpress' ); 1025 1025 1026 $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_voice_count';"; 1026 // Delete our old counts and ids. 1027 $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` IN ( '_bbp_voice_count', '_bbp_voice_ids' );"; 1027 1028 if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) { 1028 1029 return array( 1, sprintf( $statement, $result ) ); 1029 1030 } … … function bbp_admin_repair_topic_voice_count() { 1034 1035 $pps = bbp_get_public_status_id(); 1035 1036 $cps = bbp_get_closed_status_id(); 1036 1037 1037 $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) ( 1038 SELECT `postmeta`.`meta_value`, '_bbp_voice_count', COUNT(DISTINCT `post_author`) as `meta_value` 1039 FROM `{$bbp_db->posts}` AS `posts` 1040 LEFT JOIN `{$bbp_db->postmeta}` AS `postmeta` 1041 ON `posts`.`ID` = `postmeta`.`post_id` 1042 AND `postmeta`.`meta_key` = '_bbp_topic_id' 1043 WHERE `posts`.`post_type` IN ( '{$tpt}', '{$rpt}' ) 1044 AND `posts`.`post_status` IN ( '{$pps}', '{$cps}' ) 1045 AND `posts`.`post_author` != '0' 1046 GROUP BY `postmeta`.`meta_value`);"; 1038 $sql = "SELECT `postmeta`.`meta_value` AS `id`, COUNT(DISTINCT `post_author`) AS `count`, GROUP_CONCAT( `post_author` ) AS `voices` 1039 FROM `{$bbp_db->posts}` AS `posts` 1040 LEFT JOIN `{$bbp_db->postmeta}` AS `postmeta` 1041 ON `posts`.`ID` = `postmeta`.`post_id` 1042 AND `postmeta`.`meta_key` = '_bbp_topic_id' 1043 WHERE ( `posts`.`post_type` = '{$tpt}' AND `posts`.`post_status` IN ( '{$pps}', '{$cps}' ) ) 1044 OR ( `posts`.`post_type` = '{$rpt}' AND `posts`.`post_status` = '{$pps}' AND `posts`.`post_author` != '0' ) 1045 GROUP BY `postmeta`.`meta_value`;"; 1047 1046 1048 if ( is_wp_error( $bbp_db->query( $sql ) ) ) { 1047 // Get our counts and voice ids. 1048 $results = $bbp_db->get_results( $sql ); 1049 1050 if ( is_wp_error( $results ) ) { 1049 1051 return array( 2, sprintf( $statement, $result ) ); 1050 1052 } 1051 1053 1054 // Set up our MySQL VALUES groups. 1055 $values = array(); 1056 foreach ( $results as $key => $result ) { 1057 1058 // If we don't have a valid post id, bail. 1059 if ( empty( $result->id ) ) { 1060 // Unset results item to manage memory. 1061 unset( $results[ $key ] ); 1062 continue; 1063 } 1064 1065 $values[] = "( {$result->id}, '_bbp_voice_count', '{$result->count}' )"; 1066 $values[] = "( {$result->id}, '_bbp_voice_ids', '{$result->voices}' )"; 1067 1068 // Unset results item to manage memory. 1069 unset( $results[ $key ] ); 1070 } 1071 1072 // Insert our meta roles. 1073 $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) VALUES " . implode( ',', $values ) . ";"; 1074 1075 // Unset values array to manage memory. Probably not needed, but can't hurt. 1076 unset( $values ); 1077 1078 if ( is_wp_error( $bbp_db->query( $sql ) ) ) { 1079 return array( 3, sprintf( $statement, $result ) ); 1080 } 1081 1052 1082 return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) ); 1053 1083 } 1054 1084 -
src/includes/core/actions.php
diff --git src/includes/core/actions.php src/includes/core/actions.php index 263e9f7..2605a13 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 4f5bb5c..473caae 100644
function bbp_update_reply_walker( $reply_id, $last_active_time = '', $forum_id = 1021 1021 // See https://bbpress.trac.wordpress.org/ticket/2838 1022 1022 bbp_update_topic_last_active_time( $ancestor, $topic_last_active_time ); 1023 1023 1024 // Counts1025 bbp_update_topic_voice_count( $ancestor );1026 1027 1024 // Only update reply count if we're deleting a reply, or in the dashboard. 1028 1025 if ( in_array( current_filter(), array( 'bbp_deleted_reply', 'save_post' ) ) ) { 1029 1026 bbp_update_topic_reply_count( $ancestor ); 1030 1027 bbp_update_topic_reply_count_hidden( $ancestor ); 1028 bbp_update_topic_voice_count( $ancestor ); 1031 1029 } 1032 1030 1033 1031 // 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 678fb8b..12e7b33 100644
function bbp_insert_topic_update_counts( $topic_id = 0, $forum_id = 0 ) { 2588 2588 } 2589 2589 } 2590 2590 2591 /** 2592 * Maybe bump the topic voice count. 2593 * 2594 * @since 2.6.0 bbPress (rXXXX) 2595 * 2596 * @param int $topic_id The reply id. 2597 * @param int $reply_id The topic id. 2598 * @param string $action Expected `increase` or `decrease`. 2599 * 2600 * @uses bbp_get_topic_id() To validate the topic id. 2601 * @uses bbp_get_reply_id() To validate the reply id. 2602 * @uses bbp_get_reply_topic_id() To get the reply's topic id. 2603 * @uses get_post_field() To get `the post_author` field. 2604 * @uses bbp_is_reply_published() To determine if the reply is published. 2605 * @uses bbp_is_topic_published() To determine if the topic is published. 2606 * @uses bbp_get_topic_voice_ids() To get the topic voice ids array. 2607 * @uses update_post_meta() To update our voice count/ids meta values. 2608 * @uses apply_filters() To update our voice count/ids meta values. 2609 * 2610 * @return bool|int The topic voice count. False on failure. 2611 */ 2612 function bbp_maybe_bump_topic_voice_count( $topic_id = 0, $reply_id = 0, $action = '' ) { 2613 2614 // Valid our topic/reply ids. 2615 $topic_id = bbp_get_topic_id( $topic_id ); 2616 $reply_id = bbp_get_reply_id( $reply_id ); 2617 2618 // Bail early if we don't have valid ids. 2619 if ( empty( $topic_id ) && empty( $reply_id ) ) { 2620 return false; 2621 } 2622 2623 // Bail if we don't have a valid action. 2624 if ( ! in_array( $action, array( 'increase', 'decrease' ) ) ) { 2625 return false; 2626 } 2627 2628 // Make sure we have a topic id. 2629 if ( empty( $topic_id ) ) { 2630 $topic_id = bbp_get_reply_topic_id( $reply_id ); 2631 } 2632 2633 // Set some defaults. 2634 $published = $anonymous = false; 2635 $author = 0; 2636 2637 // Possibly update our author and published variables. 2638 if ( ! empty( $reply_id ) ) { 2639 $author = (int) get_post_field( 'post_author', $reply_id ); 2640 $anonymous = empty( $author ); 2641 2642 // Don't waste a potential call to the db, if it won't be counted anyway. 2643 if ( ! $anonymous ) { 2644 $published = bbp_is_reply_published( $reply_id ); 2645 } 2646 } else { 2647 $author = (int) get_post_field( 'post_author', $topic_id ); 2648 $published = bbp_is_topic_published( $topic_id ); 2649 } 2650 2651 // Get the topic voice ids. 2652 $voice_ids = bbp_get_topic_voice_ids( $topic_id ); 2653 2654 // If we have a valid author and we're published, update the voice ids array. 2655 if ( $published && 'increase' === $action && ! $anonymous ) { 2656 $voice_ids[] = $author; 2657 } elseif ( ! $published && 'decrease' === $action && ! $anonymous ) { 2658 unset( $voice_ids[ array_search( $author, $voice_ids ) ] ); 2659 } 2660 2661 // Count the unique voices. 2662 $voice_count = count( array_unique( $voice_ids ) ); 2663 2664 // Concentate our voice ids back to a string for the db. 2665 $voice_ids = implode( ',', $voice_ids ); 2666 2667 // Only update voice ids and count for a topic if it's not an anonymous reply. 2668 if ( ! $anonymous ) { 2669 update_post_meta( $topic_id, '_bbp_voice_ids', $voice_ids ); 2670 update_post_meta( $topic_id, '_bbp_voice_count', $voice_count ); 2671 } 2672 2673 /** 2674 * Filters the return of a voice count bump for a topic. 2675 * 2676 * @since 2.6.0 bbPress (rXXXX) 2677 * 2678 * @param int $voice_count The topic voice count. 2679 * @param array $voice_ids Array of voice ids. 2680 * @param int $topic_id The topic id. 2681 * @param int $reply_id The reply id. 2682 * @param string $action The action being taken (`increase` or `decrease`). 2683 */ 2684 return (int) apply_filters( 'bbp_maybe_bump_topic_voice_count', $voice_count, $voice_ids, $topic_id, $reply_id, $action ); 2685 } 2686 2687 /** 2688 * Maybe increase the topic voice count. 2689 * 2690 * @since 2.6.0 bbPress (rXXXX) 2691 * 2692 * @param int $topic_id The reply id. 2693 * @param int $reply_id The topic id. 2694 * 2695 * @uses bbp_is_reply() To determine if passed topic id is actually a reply. 2696 * @uses bbp_get_reply_id() To validate the reply id. 2697 * @uses bbp_get_reply_topic_id() To get the reply's topic id. 2698 * @uses bbp_is_topic() To determine if passed topic id is actually a topic. 2699 * @uses bbp_get_topic_id() To validate the topic id. 2700 * @uses bbp_maybe_bump_topic_voice_count() To maybe increase the topic voice count. 2701 * 2702 * @return bool|int The topic voice count. False on failure. 2703 */ 2704 function bbp_maybe_increase_topic_voice_count( $topic_id = 0, $reply_id = 0 ) { 2705 2706 // If it's a reply, then get the topic id, and validate the reply id. 2707 if ( bbp_is_reply( $topic_id ) ) { 2708 $reply_id = bbp_get_reply_id( $topic_id ); 2709 $topic_id = bbp_get_reply_topic_id( $reply_id ); 2710 2711 // If it's a topic, then validate the passed ids. 2712 } elseif ( bbp_is_topic( $topic_id ) ) { 2713 $reply_id = bbp_get_reply_id( $reply_id ); 2714 $topic_id = bbp_get_topic_id( $topic_id ); 2715 2716 // Bail if the passed id isn't a topic or reply. 2717 } else { 2718 return false; 2719 } 2720 2721 return bbp_maybe_bump_topic_voice_count( $topic_id, $reply_id, 'increase' ); 2722 } 2723 2724 /** 2725 * Maybe decrease the topic voice count. 2726 * 2727 * @since 2.6.0 bbPress (rXXXX) 2728 * 2729 * @param int $topic_id The reply id. 2730 * @param int $reply_id The topic id. 2731 * 2732 * @uses bbp_is_reply() To determine if passed topic id is actually a reply. 2733 * @uses bbp_get_reply_id() To validate the reply id. 2734 * @uses bbp_get_reply_topic_id() To get the reply's topic id. 2735 * @uses bbp_is_topic() To determine if passed topic id is actually a topic. 2736 * @uses bbp_get_topic_id() To validate the topic id. 2737 * @uses bbp_maybe_bump_topic_voice_count() To maybe decrease the topic voice count. 2738 * 2739 * @return bool|int The topic voice count. False on failure. 2740 */ 2741 function bbp_maybe_decrease_topic_voice_count( $topic_id = 0, $reply_id = 0 ) { 2742 2743 // If it's a reply, then get the topic id, and validate the reply id. 2744 if ( bbp_is_reply( $topic_id ) ) { 2745 $reply_id = bbp_get_reply_id( $topic_id ); 2746 $topic_id = bbp_get_reply_topic_id( $reply_id ); 2747 2748 // If it's a topic, then validate the passed ids. 2749 } elseif ( bbp_is_topic( $topic_id ) ) { 2750 $reply_id = bbp_get_reply_id( $reply_id ); 2751 $topic_id = bbp_get_topic_id( $topic_id ); 2752 2753 // Bail if the passed id isn't a topic or reply. 2754 } else { 2755 return false; 2756 } 2757 2758 return bbp_maybe_bump_topic_voice_count( $topic_id, $reply_id, 'decrease' ); 2759 } 2760 2591 2761 /** Topic Updaters ************************************************************/ 2592 2762 2593 2763 /** … … function bbp_update_topic_voice_count( $topic_id = 0 ) { 2895 3065 2896 3066 // Query the DB to get voices in this topic 2897 3067 $bbp_db = bbp_db(); 2898 $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() ); 2899 $voices = (int) $bbp_db->get_var( $query ); 3068 $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() ); 3069 $voice_ids = $bbp_db->get_col( $query ); 3070 $voice_ids = array_map( 'intval', (array) $voice_ids ); 3071 $count = count( array_unique( $voice_ids ) ); 2900 3072 2901 // Update the voice count for this topic id 2902 update_post_meta( $topic_id, '_bbp_voice_count', $voices ); 3073 // Update the voice ids and count for this topic id 3074 update_post_meta( $topic_id, '_bbp_published_voice_ids', implode( ',', $voice_ids ) ); 3075 update_post_meta( $topic_id, '_bbp_voice_count', $count ); 2903 3076 2904 return (int) apply_filters( 'bbp_update_topic_voice_count', $ voices, $topic_id );3077 return (int) apply_filters( 'bbp_update_topic_voice_count', $count, $topic_id ); 2905 3078 } 2906 3079 2907 3080 /** -
src/includes/topics/template.php
diff --git src/includes/topics/template.php src/includes/topics/template.php index 8973f66..defdbf6 100644
function bbp_topic_voice_count( $topic_id = 0, $integer = false ) { 2351 2351 } 2352 2352 2353 2353 /** 2354 * Return an array of topic voice ids. 2355 * 2356 * All `post_author` ids of public replies, along with public and closed topics 2357 * are included in this array. Authors of anonymous topics are counted as one 2358 * voice. Anonymous replies are not included. Voice ids are stored in the 2359 * post_meta table as a comma-separated list, and converted to an array before 2360 * being returned. 2361 * 2362 * @since 2.6.0 bbPress (rXXXX) 2363 * 2364 * @param int $topic_id The topic id. Required. 2365 * 2366 * @uses bbp_get_topic_id() To validate the topic id. 2367 * @uses get_post_meta() To get the `_bbp_voice_ids` meta value. 2368 * @uses bbp_db() To get the wpdb object. 2369 * @uses bbp_get_public_status_id() To get the public status id. 2370 * @uses bbp_get_reply_post_type() To get the reply post type. 2371 * @uses bbp_get_topic_post_type() To get the topic post type. 2372 * 2373 * @return bool|array An array of voice ids. False on failure. 2374 */ 2375 function bbp_get_topic_voice_ids( $topic_id = 0 ) { 2376 2377 // Validate the topic id. 2378 $topic_id = bbp_get_topic_id( $topic_id ); 2379 2380 // Bail if the topic id isn't valid. 2381 if ( empty( $topic_id ) ) { 2382 return false; 2383 } 2384 2385 // Check the post meta table first. 2386 $voice_ids = get_post_meta( $topic_id, '_bbp_voice_ids', true ); 2387 2388 // If no voice ids, get them from the db. 2389 if ( false === $voice_ids ) { 2390 // Query the DB to get voices in this topic. 2391 $bbp_db = bbp_db(); 2392 $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() ); 2393 $voice_ids = $bbp_db->get_col( $query ); 2394 } 2395 2396 // Make sure we have an array. 2397 if ( ! is_array( $voice_ids ) ) { 2398 $voice_ids = explode( ',', $voice_ids ); 2399 } 2400 2401 // Clean up the voice ids array. We don't want empty strings converted to an 2402 // integer of 0, so we need to filter these out. 2403 $voice_ids = array_filter( array_map( 'trim', $voice_ids ), 'strlen' ); 2404 $voice_ids = array_map( 'absint', $voice_ids ); 2405 2406 /** 2407 * Filters the voice ids array for a topic. 2408 * 2409 * @since 2.6.0 bbPress (rXXXX) 2410 * 2411 * @param array $voice_ids Array of voice ids. 2412 * @param int $topic_id The topic id. 2413 */ 2414 return (array) apply_filters( 'bbp_get_topic_voice_ids', $voice_ids, $topic_id ); 2415 } 2416 2417 /** 2354 2418 * Output a the tags of a topic 2355 2419 * 2356 2420 * @since 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 ffd1bc7..0bc1e52 100644
class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 563 563 $count = bbp_update_topic_voice_count( $t ); 564 564 $this->assertSame( 2, $count ); 565 565 566 $voices = bbp_get_topic_voice_ids( $t ); 567 $this->assertEquals( array( bbp_get_current_user_id(), $u[0] ), $voices ); 568 566 569 $r = $this->factory->reply->create( array( 567 570 'post_author' => $u[1], 568 571 'post_parent' => $t, … … class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 571 574 bbp_update_topic_voice_count( $t ); 572 575 $count = bbp_get_topic_voice_count( $t ); 573 576 $this->assertSame( '3', $count ); 577 578 $voices = bbp_get_topic_voice_ids( $t ); 579 $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1] ), $voices ); 574 580 } 575 581 576 582 /** … … class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 583 589 'This test has not been implemented yet.' 584 590 ); 585 591 } 592 593 /** 594 * @covers ::bbp_maybe_bump_topic_voice_count 595 */ 596 public function test_bbp_maybe_bump_topic_voice_count() { 597 $u = $this->factory->user->create_many( 2 ); 598 $t = $this->factory->topic->create(); 599 600 $count = bbp_get_topic_voice_count( $t ); 601 $this->assertSame( '1', $count ); 602 603 $voices = bbp_get_topic_voice_ids( $t ); 604 $this->assertEquals( array( bbp_get_current_user_id() ), $voices ); 605 606 remove_action( 'bbp_insert_reply', 'bbp_maybe_increase_topic_voice_count' ); 607 608 $r1 = $this->factory->reply->create( array( 609 'post_author' => $u[0], 610 'post_parent' => $t, 611 ) ); 612 613 bbp_maybe_bump_topic_voice_count( $t, $r1, 'increase' ); 614 $count = bbp_get_topic_voice_count( $t ); 615 $this->assertSame( '2', $count ); 616 617 $voices = bbp_get_topic_voice_ids( $t ); 618 $this->assertEquals( array( bbp_get_current_user_id(), $u[0] ), $voices ); 619 620 $r2 = $this->factory->reply->create( array( 621 'post_author' => $u[1], 622 'post_parent' => $t, 623 ) ); 624 625 bbp_maybe_bump_topic_voice_count( $t, $r2, 'increase' ); 626 $count = bbp_get_topic_voice_count( $t ); 627 $this->assertSame( '3', $count ); 628 629 $voices = bbp_get_topic_voice_ids( $t ); 630 $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1] ), $voices ); 631 632 $r3 = $this->factory->reply->create( array( 633 'post_author' => $u[1], 634 'post_parent' => $t, 635 ) ); 636 637 bbp_maybe_bump_topic_voice_count( $t, $r3, 'increase' ); 638 $count = bbp_get_topic_voice_count( $t ); 639 $this->assertSame( '3', $count ); 640 641 $voices = bbp_get_topic_voice_ids( $t ); 642 $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1], $u[1] ), $voices ); 643 644 remove_action( 'bbp_unapproved_reply', 'bbp_maybe_decrease_topic_voice_count' ); 645 646 bbp_unapprove_reply( $r1 ); 647 648 add_action( 'bbp_unapproved_reply', 'bbp_maybe_decrease_topic_voice_count' ); 649 650 bbp_maybe_bump_topic_voice_count( $t, $r1, 'decrease' ); 651 $count = bbp_get_topic_voice_count( $t ); 652 $this->assertSame( '2', $count ); 653 654 $voices = bbp_get_topic_voice_ids( $t ); 655 $this->assertEquals( array( bbp_get_current_user_id(), $u[1], $u[1] ), $voices ); 656 657 add_action( 'bbp_insert_reply', 'bbp_maybe_increase_topic_voice_count' ); 658 } 659 660 /** 661 * @covers ::bbp_maybe_increase_topic_voice_count 662 */ 663 public function test_bbp_maybe_increase_topic_voice_count() { 664 $u = $this->factory->user->create_many( 2 ); 665 $t = $this->factory->topic->create(); 666 667 $count = bbp_get_topic_voice_count( $t ); 668 $this->assertSame( '1', $count ); 669 670 $voices = bbp_get_topic_voice_ids( $t ); 671 $this->assertEquals( array( bbp_get_current_user_id() ), $voices ); 672 673 remove_action( 'bbp_insert_reply', 'bbp_maybe_increase_topic_voice_count' ); 674 675 $r = $this->factory->reply->create( array( 676 'post_author' => $u[0], 677 'post_parent' => $t, 678 ) ); 679 680 bbp_maybe_increase_topic_voice_count( $t, $r ); 681 $count = bbp_get_topic_voice_count( $t ); 682 $this->assertSame( '2', $count ); 683 684 $voices = bbp_get_topic_voice_ids( $t ); 685 $this->assertEquals( array( bbp_get_current_user_id(), $u[0] ), $voices ); 686 687 $r = $this->factory->reply->create( array( 688 'post_author' => $u[1], 689 'post_parent' => $t, 690 ) ); 691 692 bbp_maybe_increase_topic_voice_count( $r ); 693 $count = bbp_get_topic_voice_count( $t ); 694 $this->assertSame( '3', $count ); 695 696 $voices = bbp_get_topic_voice_ids( $t ); 697 $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1] ), $voices ); 698 699 add_action( 'bbp_insert_reply', 'bbp_maybe_increase_topic_voice_count' ); 700 } 701 702 /** 703 * @covers ::bbp_maybe_decrease_topic_voice_count 704 */ 705 public function test_bbp_maybe_decrease_topic_voice_count() { 706 $u = $this->factory->user->create_many( 2 ); 707 $t = $this->factory->topic->create(); 708 709 $count = bbp_get_topic_voice_count( $t ); 710 $this->assertSame( '1', $count ); 711 712 $voices = bbp_get_topic_voice_ids( $t ); 713 $this->assertEquals( array( bbp_get_current_user_id() ), $voices ); 714 715 $r1 = $this->factory->reply->create( array( 716 'post_author' => $u[0], 717 'post_parent' => $t, 718 ) ); 719 $r2 = $this->factory->reply->create( array( 720 'post_author' => $u[1], 721 'post_parent' => $t, 722 ) ); 723 $r3 = $this->factory->reply->create( array( 724 'post_author' => $u[1], 725 'post_parent' => $t, 726 ) ); 727 728 remove_action( 'bbp_unapproved_reply', 'bbp_maybe_decrease_topic_voice_count' ); 729 730 bbp_unapprove_reply( $r3 ); 731 732 add_action( 'bbp_unapproved_reply', 'bbp_maybe_decrease_topic_voice_count' ); 733 734 bbp_maybe_decrease_topic_voice_count( $t, $r3 ); 735 $count = bbp_get_topic_voice_count( $t ); 736 $this->assertSame( '3', $count ); 737 738 $voices = bbp_get_topic_voice_ids( $t ); 739 $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1] ), $voices ); 740 741 bbp_unapprove_reply( $r1 ); 742 743 $count = bbp_get_topic_voice_count( $t ); 744 $this->assertSame( '2', $count ); 745 746 $voices = bbp_get_topic_voice_ids( $t ); 747 $this->assertEquals( array( bbp_get_current_user_id(), $u[1] ), $voices ); 748 } 749 750 /** 751 * @covers ::bbp_get_topic_voice_ids 752 */ 753 public function test_bbp_get_topic_voice_ids() { 754 $u = $this->factory->user->create_many( 2 ); 755 $t = $this->factory->topic->create(); 756 757 $voices = bbp_get_topic_voice_ids( $t ); 758 $this->assertEquals( array( bbp_get_current_user_id() ), $voices ); 759 760 $r1 = $this->factory->reply->create( array( 761 'post_author' => $u[0], 762 'post_parent' => $t, 763 ) ); 764 765 $voices = bbp_get_topic_voice_ids( $t ); 766 $this->assertEquals( array( bbp_get_current_user_id(), $u[0] ), $voices ); 767 768 $r2 = $this->factory->reply->create_many( 2, array( 769 'post_author' => $u[1], 770 'post_parent' => $t, 771 ) ); 772 773 $voices = bbp_get_topic_voice_ids( $t ); 774 $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1], $u[1] ), $voices ); 775 776 bbp_unapprove_reply( $r2[0] ); 777 778 $voices = bbp_get_topic_voice_ids( $t ); 779 $this->assertEquals( array( bbp_get_current_user_id(), $u[0], $u[1] ), $voices ); 780 } 586 781 }