Skip to:
Content

bbPress.org

Ticket #1799: 1799.8.patch

File 1799.8.patch, 35.5 KB (added by thebrandonallen, 9 years ago)

no voice counts

  • src/includes/core/actions.php

    diff --git src/includes/core/actions.php src/includes/core/actions.php
    index 18fa0bb..263e9f7 100644
    add_action( 'bbp_unspammed_reply', 'bbp_update_reply_walker' ); 
    255255add_action( 'bbp_approved_reply',   'bbp_update_reply_walker' );
    256256add_action( 'bbp_unapproved_reply', 'bbp_update_reply_walker' );
    257257
     258// Update forum topic/reply counts
     259add_action( 'bbp_new_reply',        'bbp_increase_forum_reply_count'        );
     260add_action( 'bbp_new_topic',        'bbp_increase_forum_topic_count'        );
     261add_action( 'bbp_trashed_reply',    'bbp_decrease_forum_reply_count'        );
     262add_action( 'bbp_trashed_topic',    'bbp_decrease_forum_topic_count'        );
     263add_action( 'bbp_trashed_topic',    'bbp_increase_forum_topic_count_hidden' );
     264add_action( 'bbp_untrashed_reply',  'bbp_increase_forum_reply_count'        );
     265add_action( 'bbp_untrashed_topic',  'bbp_increase_forum_topic_count'        );
     266add_action( 'bbp_untrashed_topic',  'bbp_decrease_forum_topic_count_hidden' );
     267add_action( 'bbp_spammed_reply',    'bbp_decrease_forum_reply_count'        );
     268add_action( 'bbp_spammed_topic',    'bbp_decrease_forum_topic_count'        );
     269add_action( 'bbp_spammed_topic',    'bbp_increase_forum_topic_count_hidden' );
     270add_action( 'bbp_unspammed_reply',  'bbp_increase_forum_reply_count'        );
     271add_action( 'bbp_unspammed_topic',  'bbp_increase_forum_topic_count'        );
     272add_action( 'bbp_unspammed_topic',  'bbp_decrease_forum_topic_count_hidden' );
     273add_action( 'bbp_approved_reply',   'bbp_increase_forum_reply_count'        );
     274add_action( 'bbp_approved_topic',   'bbp_increase_forum_topic_count'        );
     275add_action( 'bbp_approved_topic',   'bbp_decrease_forum_topic_count_hidden' );
     276add_action( 'bbp_unapproved_reply', 'bbp_decrease_forum_reply_count'        );
     277add_action( 'bbp_unapproved_topic', 'bbp_decrease_forum_topic_count'        );
     278add_action( 'bbp_unapproved_topic', 'bbp_increase_forum_topic_count_hidden' );
     279
     280// Update forum reply counts for approved/unapproved topics.
     281add_action( 'bbp_approved_topic',   'bbp_approved_unapproved_topic_update_forum_reply_count' );
     282add_action( 'bbp_unapproved_topic', 'bbp_approved_unapproved_topic_update_forum_reply_count' );
     283
     284// Update topic reply counts
     285add_action( 'bbp_new_reply',        'bbp_increase_topic_reply_count'        );
     286add_action( 'bbp_trashed_reply',    'bbp_decrease_topic_reply_count'        );
     287add_action( 'bbp_trashed_reply',    'bbp_increase_topic_reply_count_hidden' );
     288add_action( 'bbp_untrashed_reply',  'bbp_increase_topic_reply_count'        );
     289add_action( 'bbp_untrashed_reply',  'bbp_decrease_topic_reply_count_hidden' );
     290add_action( 'bbp_spammed_reply',    'bbp_decrease_topic_reply_count'        );
     291add_action( 'bbp_spammed_reply',    'bbp_increase_topic_reply_count_hidden' );
     292add_action( 'bbp_unspammed_reply',  'bbp_increase_topic_reply_count'        );
     293add_action( 'bbp_unspammed_reply',  'bbp_decrease_topic_reply_count_hidden' );
     294add_action( 'bbp_approved_reply',   'bbp_increase_topic_reply_count'        );
     295add_action( 'bbp_approved_reply',   'bbp_decrease_topic_reply_count_hidden' );
     296add_action( 'bbp_unapproved_reply', 'bbp_decrease_topic_reply_count'        );
     297add_action( 'bbp_unapproved_reply', 'bbp_increase_topic_reply_count_hidden' );
     298
    258299// Users topic & reply counts
    259300add_action( 'bbp_new_topic',     'bbp_increase_user_topic_count' );
    260301add_action( 'bbp_new_reply',     'bbp_increase_user_reply_count' );
    add_action( 'bbp_trash_reply', 'bbp_decrease_user_reply_count' ); 
    267308add_action( 'bbp_spam_topic',    'bbp_decrease_user_topic_count' );
    268309add_action( 'bbp_spam_reply',    'bbp_decrease_user_reply_count' );
    269310
     311// Insert forum/topic/reply counts
     312add_action( 'bbp_insert_topic', 'bbp_insert_topic_update_counts', 10, 2 );
     313add_action( 'bbp_insert_reply', 'bbp_insert_reply_update_counts', 10, 3 );
     314
    270315// Topic status transition helpers for replies
    271316add_action( 'bbp_trash_topic',   'bbp_trash_topic_replies'   );
    272317add_action( 'bbp_untrash_topic', 'bbp_untrash_topic_replies' );
  • src/includes/forums/functions.php

    diff --git src/includes/forums/functions.php src/includes/forums/functions.php
    index a821d79..615c143 100644
    function bbp_insert_forum( $forum_data = array(), $forum_meta = array() ) { 
    7373                'forum_id' => $forum_id,
    7474        ) );
    7575
     76        /**
     77         * Fires after forum has been inserted via `bbp_insert_forum`.
     78         *
     79         * @since 2.6.0 bbPress (rXXXX)
     80         *
     81         * @param int $forum_id The forum id.
     82         */
     83        do_action( 'bbp_insert_forum', (int) $forum_id );
     84
    7685        // Return new forum ID
    7786        return $forum_id;
    7887}
    function bbp_bump_forum_topic_count( $forum_id = 0, $difference = 1, $update_anc 
    11411150                if ( ! empty( $ancestors ) ) {
    11421151                        foreach ( (array) $ancestors as $parent_forum_id ) {
    11431152
    1144                                 // Get forum counts
    1145                                 $parent_topic_count       = bbp_get_forum_topic_count( $parent_forum_id, false, true );
    1146                                 $parent_total_topic_count = bbp_get_forum_topic_count( $parent_forum_id, true,  true );
     1153                                // Only update topic count when an ancestor is not a category.
     1154                                if ( ! bbp_is_forum_category( $parent_forum_id ) ) {
     1155
     1156                                        $parent_topic_count = bbp_get_forum_topic_count( $parent_forum_id, false, true );
     1157                                        update_post_meta( $parent_forum_id, '_bbp_topic_count', (int) ( $parent_topic_count + $difference ) );
     1158                                }
    11471159
    1148                                 // Update counts
    1149                                 update_post_meta( $parent_forum_id, '_bbp_topic_count',       (int) ( $parent_topic_count       + $difference ) );
     1160                                // Update the total topic count.
     1161                                $parent_total_topic_count = bbp_get_forum_topic_count( $parent_forum_id, true,  true );
    11501162                                update_post_meta( $parent_forum_id, '_bbp_total_topic_count', (int) ( $parent_total_topic_count + $difference ) );
    11511163                        }
    11521164                }
    function bbp_bump_forum_topic_count( $forum_id = 0, $difference = 1, $update_anc 
    11581170}
    11591171
    11601172/**
     1173 * Increase the total topic count of a forum by one.
     1174 *
     1175 * @since 2.6.0 bbPress (rXXXX)
     1176 *
     1177 * @param int $forum_id Optional. Forum id.
     1178 *
     1179 * @uses bbp_get_forum_id() To get the forum id.
     1180 * @uses bbp_bump_forum_topic_count() To bump forum topic count.
     1181 *
     1182 * @return void
     1183 */
     1184function bbp_increase_forum_topic_count( $forum_id = 0 ) {
     1185
     1186        // Bail early if no id is passed.
     1187        if ( empty( $forum_id ) ) {
     1188                return;
     1189        }
     1190
     1191        // If it's a topic, get the forum id.
     1192        if ( bbp_is_topic( $forum_id ) ) {
     1193                $topic_id = $forum_id;
     1194                $forum_id = bbp_get_topic_forum_id( $topic_id );
     1195
     1196                // If this is a new, unpublished, topic, increase hidden count and bail.
     1197                if ( 'bbp_new_topic' === current_filter() && ( ! bbp_is_topic_published( $topic_id ) && ! bbp_is_topic_closed( $topic_id ) ) ) {
     1198                        bbp_increase_forum_topic_count_hidden( $forum_id );
     1199                        return;
     1200                }
     1201        }
     1202
     1203        bbp_bump_forum_topic_count( $forum_id );
     1204}
     1205
     1206/**
     1207 * Decrease the total topic count of a forum by one.
     1208 *
     1209 * @since 2.6.0 bbPress (rXXXX)
     1210 *
     1211 * @param int $forum_id Optional. Forum id.
     1212 * @uses bbp_get_forum_id() To get the forum id.
     1213 * @uses bbp_bump_forum_topic_count() To bump forum topic count.
     1214 *
     1215 * @return void
     1216 */
     1217function bbp_decrease_forum_topic_count( $forum_id = 0 ) {
     1218
     1219        // Bail early if no id is passed.
     1220        if ( empty( $forum_id ) ) {
     1221                return;
     1222        }
     1223
     1224        // If it's a topic, get the forum id.
     1225        if ( bbp_is_topic( $forum_id ) ) {
     1226                $forum_id = bbp_get_topic_forum_id( $forum_id );
     1227        }
     1228
     1229        bbp_bump_forum_topic_count( $forum_id, -1 );
     1230}
     1231
     1232/**
    11611233 * Bump the total hidden topic count of a forum
    11621234 *
    11631235 * @since 2.1.0 bbPress (r3825)
    function bbp_bump_forum_topic_count_hidden( $forum_id = 0, $difference = 1 ) { 
    11911263}
    11921264
    11931265/**
     1266 * Increase the total hidden topic count of a forum by one.
     1267 *
     1268 * @since 2.6.0 bbPress (rXXXX)
     1269 *
     1270 * @param int $forum_id Optional. Forum id.
     1271 *
     1272 * @uses bbp_get_forum_id() To get the forum id.
     1273 * @uses bbp_bump_forum_topic_count_hidden() To bump forum hidden topic count.
     1274 *
     1275 * @return void
     1276 */
     1277function bbp_increase_forum_topic_count_hidden( $forum_id = 0 ) {
     1278
     1279        // Bail early if no id is passed.
     1280        if ( empty( $forum_id ) ) {
     1281                return;
     1282        }
     1283
     1284        // If it's a topic, get the forum id.
     1285        if ( bbp_is_topic( $forum_id ) ) {
     1286                $forum_id = bbp_get_topic_forum_id( $forum_id );
     1287        }
     1288
     1289        bbp_bump_forum_topic_count_hidden( $forum_id );
     1290}
     1291
     1292/**
     1293 * Decrease the total hidden topic count of a forum by one.
     1294 *
     1295 * @since 2.6.0 bbPress (rXXXX)
     1296 *
     1297 * @param int $forum_id Optional. Forum id.
     1298 *
     1299 * @uses bbp_get_forum_id() To get the forum id.
     1300 * @uses bbp_bump_forum_topic_count_hidden() To bump forum hidden topic count.
     1301 *
     1302 * @return void
     1303 */
     1304function bbp_decrease_forum_topic_count_hidden( $forum_id = 0 ) {
     1305
     1306        // Bail early if no id is passed.
     1307        if ( empty( $forum_id ) ) {
     1308                return;
     1309        }
     1310
     1311        // If it's a topic, get the forum id.
     1312        if ( bbp_is_topic( $forum_id ) ) {
     1313                $forum_id = bbp_get_topic_forum_id( $forum_id );
     1314        }
     1315
     1316        bbp_bump_forum_topic_count_hidden( $forum_id, -1 );
     1317}
     1318
     1319/**
    11941320 * Bump the total topic count of a forum
    11951321 *
    11961322 * @since 2.1.0 bbPress (r3825)
    function bbp_bump_forum_reply_count( $forum_id = 0, $difference = 1, $update_anc 
    12321358                if ( ! empty( $ancestors ) ) {
    12331359                        foreach ( (array) $ancestors as $parent_forum_id ) {
    12341360
    1235                                 // Get forum counts
    1236                                 $parent_topic_count       = bbp_get_forum_reply_count( $parent_forum_id, false, true );
    1237                                 $parent_total_reply_count = bbp_get_forum_reply_count( $parent_forum_id, true,  true );
     1361                                // Only update reply count when an ancestor is not a category.
     1362                                if ( ! bbp_is_forum_category( $parent_forum_id ) ) {
     1363
     1364                                        $parent_reply_count = bbp_get_forum_reply_count( $parent_forum_id, false, true );
     1365                                        update_post_meta( $parent_forum_id, '_bbp_reply_count', (int) ( $parent_reply_count + $difference ) );
     1366                                }
    12381367
    1239                                 // Update counts
    1240                                 update_post_meta( $parent_forum_id, '_bbp_reply_count',       (int) ( $parent_topic_count       + $difference ) );
     1368                                // Update the total reply count.
     1369                                $parent_total_reply_count = bbp_get_forum_reply_count( $parent_forum_id, true,  true );
    12411370                                update_post_meta( $parent_forum_id, '_bbp_total_reply_count', (int) ( $parent_total_reply_count + $difference ) );
    12421371                        }
    12431372                }
    function bbp_bump_forum_reply_count( $forum_id = 0, $difference = 1, $update_anc 
    12481377        return (int) apply_filters( 'bbp_bump_forum_reply_count', $forum_reply_count, $forum_id, $difference, $update_ancestors );
    12491378}
    12501379
     1380/**
     1381 * Increase the total reply count of a forum by one.
     1382 *
     1383 * @since 2.6.0 bbPress (rXXXX)
     1384 *
     1385 * @param int $forum_id Optional. Forum id.
     1386 *
     1387 * @uses bbp_get_forum_id() To get the forum id.
     1388 * @uses bbp_bump_forum_reply_count() To bump forum topic count.
     1389 *
     1390 * @return void
     1391 */
     1392function bbp_increase_forum_reply_count( $forum_id = 0 ) {
     1393
     1394        // Bail early if no id is passed.
     1395        if ( empty( $forum_id ) ) {
     1396                return;
     1397        }
     1398
     1399        // If it's a reply, get the forum id.
     1400        if ( bbp_is_reply( $forum_id ) ) {
     1401                $reply_id = $forum_id;
     1402                $forum_id = bbp_get_reply_forum_id( $reply_id );
     1403
     1404                // Don't update if this is a new, unpublished, reply.
     1405                if ( 'bbp_new_reply' === current_filter() && ! bbp_is_reply_published( $reply_id ) ) {
     1406                        return;
     1407                }
     1408        }
     1409
     1410        bbp_bump_forum_reply_count( $forum_id );
     1411}
     1412
     1413/**
     1414 * Decrease the total reply count of a forum by one.
     1415 *
     1416 * @since 2.6.0 bbPress (rXXXX)
     1417 *
     1418 * @param int $forum_id Optional. Forum id.
     1419 *
     1420 * @uses bbp_get_forum_id() To get the forum id.
     1421 * @uses bbp_bump_forum_reply_count() To bump forum topic count.
     1422 *
     1423 * @return void
     1424 */
     1425function bbp_decrease_forum_reply_count( $forum_id = 0 ) {
     1426
     1427        // Bail early if no id is passed.
     1428        if ( empty( $forum_id ) ) {
     1429                return;
     1430        }
     1431
     1432        // If it's a reply, get the forum id.
     1433        if ( bbp_is_reply( $forum_id ) ) {
     1434                $forum_id = bbp_get_reply_forum_id( $forum_id );
     1435        }
     1436
     1437        bbp_bump_forum_reply_count( $forum_id, -1 );
     1438}
     1439
     1440/**
     1441 * Update forum reply counts when a topic is approved or unapproved.
     1442 *
     1443 * @since 2.6.0 bbPress (rXXXX)
     1444 *
     1445 * @param int $topic_id
     1446 *
     1447 * @uses bbp_get_reply_post_type() To get the reply post type.
     1448 * @uses bbp_get_public_child_ids() To get the topic's public child ids.
     1449 * @uses bbp_get_topic_forum_id() To get the topic's forum id.
     1450 * @uses bbp_bump_forum_reply_count() To bump the forum reply count.
     1451 *
     1452 * @return void
     1453 */
     1454function bbp_approved_unapproved_topic_update_forum_reply_count( $topic_id = 0 ) {
     1455
     1456        // Bail early if we don't have a topic id.
     1457        if ( empty( $topic_id ) ) {
     1458                return;
     1459        }
     1460
     1461        // Get the topic's replies.
     1462        $replies = bbp_get_public_child_ids( $topic_id, bbp_get_reply_post_type() );
     1463        $count = count( $replies );
     1464
     1465        // If we're unapproving, set count to negative.
     1466        if ( 'bbp_unapproved_topic' === current_filter() ) {
     1467                $count = -$count;
     1468        }
     1469
     1470        // Update counts.
     1471        bbp_bump_forum_reply_count( bbp_get_topic_forum_id( $topic_id ), $count );
     1472}
     1473
    12511474/** Forum Updaters ************************************************************/
    12521475
    12531476/**
    function bbp_update_forum( $args = array() ) { 
    17321955        }
    17331956
    17341957        // Counts
    1735         bbp_update_forum_subforum_count    ( $r['forum_id'] );
    1736         bbp_update_forum_reply_count       ( $r['forum_id'] );
    1737         bbp_update_forum_topic_count       ( $r['forum_id'] );
    1738         bbp_update_forum_topic_count_hidden( $r['forum_id'] );
     1958        bbp_update_forum_subforum_count( $r['forum_id'] );
     1959
     1960        // Only update topic count if we're deleting a topic, or in the dashboard.
     1961        if ( in_array( current_filter(), array( 'bbp_deleted_topic', 'save_post' ) ) ) {
     1962                bbp_update_forum_reply_count(        $r['forum_id'] );
     1963                bbp_update_forum_topic_count(        $r['forum_id'] );
     1964                bbp_update_forum_topic_count_hidden( $r['forum_id'] );
     1965        }
    17391966
    17401967        // Update the parent forum if one was passed
    17411968        if ( ! empty( $r['post_parent'] ) && is_numeric( $r['post_parent'] ) ) {
  • src/includes/replies/functions.php

    diff --git src/includes/replies/functions.php src/includes/replies/functions.php
    index caabba0..6b1c7b1 100644
    function bbp_insert_reply( $reply_data = array(), $reply_meta = array() ) { 
    6565        // Update the reply and hierarchy
    6666        bbp_update_reply( $reply_id, $reply_meta['topic_id'], $reply_meta['forum_id'], array(), $reply_data['post_author'], false, $reply_meta['reply_to'] );
    6767
     68        /**
     69         * Fires after reply has been inserted via `bbp_insert_reply`.
     70         *
     71         * @since 2.6.0 bbPress (rXXXX)
     72         *
     73         * @param int $reply_id The reply id.
     74         * @param int $topic_id The topic id.
     75         * @param int $forum_id The forum id.
     76         */
     77        do_action( 'bbp_insert_reply', (int) $reply_id, (int) $reply_meta['topic_id'], (int) $reply_meta['forum_id'] );
     78
    6879        // Return new reply ID
    6980        return $reply_id;
    7081}
    7182
     83/**
     84 * Update counts after a reply is inserted via `bbp_insert_reply`.
     85 *
     86 * @since 2.6.0 bbPress (rXXXX)
     87 *
     88 * @param int $reply_id
     89 * @param int $topic_id
     90 * @param int $forum_id
     91 *
     92 * @uses get_post_field() To get the post status.
     93 * @uses bbp_get_public_status_id() To get the public status id.
     94 * @uses bbp_increase_topic_reply_count() To bump the reply's topic reply count by 1.
     95 * @uses bbp_increase_forum_reply_count() To bump the reply's forum reply count by 1.
     96 * @uses bbp_increase_topic_reply_count_hidden() To bump the reply's topic reply
     97 *                                               hidden count by 1.
     98 *
     99 * @return void
     100 */
     101function bbp_insert_reply_update_counts( $reply_id = 0, $topic_id = 0, $forum_id = 0 ) {
     102
     103        // Get the reply status.
     104        $status = get_post_field( 'post_status', $reply_id );
     105
     106        // If the reply is public, update the forum/topic reply counts.
     107        if ( $status === bbp_get_public_status_id() ) {
     108                bbp_increase_topic_reply_count( $topic_id );
     109                bbp_increase_forum_reply_count( $forum_id );
     110
     111        // If the reply isn't public only update the topic reply hidden count.
     112        } else {
     113                bbp_increase_topic_reply_count_hidden( $topic_id );
     114        }
     115}
     116
    72117/** Post Form Handlers ********************************************************/
    73118
    74119/**
    function bbp_update_reply_walker( $reply_id, $last_active_time = '', $forum_id = 
    9771022                                bbp_update_topic_last_active_time( $ancestor, $topic_last_active_time );
    9781023
    9791024                                // Counts
    980                                 bbp_update_topic_voice_count       ( $ancestor );
    981                                 bbp_update_topic_reply_count       ( $ancestor );
    982                                 bbp_update_topic_reply_count_hidden( $ancestor );
     1025                                bbp_update_topic_voice_count( $ancestor );
     1026
     1027                                // Only update reply count if we're deleting a reply, or in the dashboard.
     1028                                if ( in_array( current_filter(), array( 'bbp_deleted_reply', 'save_post' ) ) ) {
     1029                                        bbp_update_topic_reply_count(        $ancestor );
     1030                                        bbp_update_topic_reply_count_hidden( $ancestor );
     1031                                }
    9831032
    9841033                        // Forum meta relating to most recent topic
    9851034                        } elseif ( bbp_is_forum( $ancestor ) ) {
    function bbp_update_reply_walker( $reply_id, $last_active_time = '', $forum_id = 
    10031052                                }
    10041053
    10051054                                // Counts
    1006                                 bbp_update_forum_reply_count( $ancestor );
     1055                                // Only update reply count if we're deleting a reply, or in the dashboard.
     1056                                if ( in_array( current_filter(), array( 'bbp_deleted_reply', 'save_post' ) ) ) {
     1057                                        bbp_update_forum_reply_count( $ancestor );
     1058                                }
    10071059                        }
    10081060                }
    10091061        }
  • src/includes/topics/functions.php

    diff --git src/includes/topics/functions.php src/includes/topics/functions.php
    index 5a762e5..5e75cf4 100644
    function bbp_insert_topic( $topic_data = array(), $topic_meta = array() ) { 
    7070        // Update the topic and hierarchy
    7171        bbp_update_topic( $topic_id, $topic_meta['forum_id'], array(), $topic_data['post_author'], false );
    7272
     73        /**
     74         * Fires after topic has been inserted via `bbp_insert_topic`.
     75         *
     76         * @since 2.6.0 bbPress (rXXXX)
     77         *
     78         * @param int $topic_id The topic id.
     79         * @param int $forum_id The forum id.
     80         */
     81        do_action( 'bbp_insert_topic', (int) $topic_id, (int) $topic_meta['forum_id'] );
     82
    7383        // Return new topic ID
    7484        return $topic_id;
    7585}
    function bbp_update_topic_walker( $topic_id, $last_active_time = '', $forum_id = 
    10221032 * @uses update_post_meta() To update the old forum sticky meta
    10231033 * @uses bbp_stick_topic() To stick the topic in the new forum
    10241034 * @uses bbp_get_reply_post_type() To get the reply post type
    1025  * @uses bbp_get_all_child_ids() To get the public child ids
     1035 * @uses bbp_get_all_child_ids() To get the all child ids.
     1036 * @uses bbp_get_public_child_ids() To get the all child ids.
     1037 * @uses get_post_field() To get the topic's post status.
     1038 * @uses bbp_get_public_status_id() To get the public status id.
     1039 * @uses bbp_decrease_forum_topic_count() To bump the forum topic count by -1.
     1040 * @uses bbp_bump_forum_reply_count() To bump the forum reply count.
     1041 * @uses bbp_increase_forum_topic_count() To bump the forum topic count by 1.
     1042 * @uses bbp_decrease_forum_topic_count_hidden() To bump the forum topic hidden count by -1.
     1043 * @uses bbp_increase_forum_topic_count_hidden() To bump the forum topic hidden count by 1.
    10261044 * @uses bbp_update_reply_forum_id() To update the reply forum id
    10271045 * @uses bbp_update_topic_forum_id() To update the topic forum id
    10281046 * @uses get_post_ancestors() To get the topic's ancestors
    function bbp_move_topic_handler( $topic_id, $old_forum_id, $new_forum_id ) { 
    11001118        // Get topic ancestors
    11011119        $old_forum_ancestors = array_values( array_unique( array_merge( array( $old_forum_id ), (array) get_post_ancestors( $old_forum_id ) ) ) );
    11021120
     1121        // Get reply count.
     1122        $public_reply_count = count( bbp_get_public_child_ids( $topic_id, bbp_get_reply_post_type() ) );
     1123
     1124        // Topic status.
     1125        $topic_status = get_post_field( 'post_status', $topic_id );
     1126
     1127        // Update old/new forum counts.
     1128        if ( $topic_status === bbp_get_public_status_id() ) {
     1129
     1130                // Update old forum counts.
     1131                bbp_decrease_forum_topic_count( $old_forum_id );
     1132                bbp_bump_forum_reply_count( $old_forum_id, -$public_reply_count );
     1133
     1134                // Update new forum counts.
     1135                bbp_increase_forum_topic_count( $new_forum_id );
     1136                bbp_bump_forum_reply_count( $new_forum_id, $public_reply_count );
     1137        } else {
     1138
     1139                // Update old forum counts.
     1140                bbp_decrease_forum_topic_count_hidden( $old_forum_id );
     1141
     1142                // Update new forum counts.
     1143                bbp_increase_forum_topic_count_hidden( $new_forum_id );
     1144        }
     1145
    11031146        // Loop through ancestors and update them
    11041147        if ( ! empty( $old_forum_ancestors ) ) {
    11051148                foreach ( $old_forum_ancestors as $ancestor ) {
    function bbp_bump_topic_reply_count( $topic_id = 0, $difference = 1 ) { 
    23642407}
    23652408
    23662409/**
     2410 * Increase the total reply count of a topic by one.
     2411 *
     2412 * @since 2.6.0 bbPress (rXXXX)
     2413 *
     2414 * @param int $topic_id Optional. Forum id.
     2415 *
     2416 * @uses bbp_is_reply() To check if the passed topic id is a reply.
     2417 * @uses bbp_get_reply_topic_id() To get the topic id.
     2418 * @uses bbp_bump_topic_reply_count() To bump topic reply count.
     2419 *
     2420 * @return void
     2421 */
     2422function bbp_increase_topic_reply_count( $topic_id = 0 ) {
     2423
     2424        // Bail early if no id is passed.
     2425        if ( empty( $topic_id ) ) {
     2426                return;
     2427        }
     2428
     2429        // If it's a reply, get the topic id.
     2430        if ( bbp_is_reply( $topic_id ) ) {
     2431                $reply_id = $topic_id;
     2432                $topic_id = bbp_get_reply_topic_id( $reply_id );
     2433
     2434                // If this is a new, unpublished, reply, update hidden count and bail.
     2435                if ( 'bbp_new_reply' === current_filter() && ! bbp_is_reply_published( $reply_id ) ) {
     2436                        bbp_increase_topic_reply_count_hidden( $topic_id );
     2437                        return;
     2438                }
     2439        }
     2440
     2441        bbp_bump_topic_reply_count( $topic_id );
     2442}
     2443
     2444/**
     2445 * Decrease the total reply count of a topic by one.
     2446 *
     2447 * @since 2.6.0 bbPress (rXXXX)
     2448 *
     2449 * @param int $topic_id Optional. Topic id.
     2450 *
     2451 * @uses bbp_is_reply() To check if the passed topic id is a reply.
     2452 * @uses bbp_get_reply_topic_id() To get the topic id.
     2453 * @uses bbp_bump_topic_reply_count() To bump topic reply count.
     2454 *
     2455 * @return void
     2456 */
     2457function bbp_decrease_topic_reply_count( $topic_id = 0 ) {
     2458
     2459        // Bail early if no id is passed.
     2460        if ( empty( $topic_id ) ) {
     2461                return;
     2462        }
     2463
     2464        // If it's a reply, get the topic id.
     2465        if ( bbp_is_reply( $topic_id ) ) {
     2466                $topic_id = bbp_get_reply_topic_id( $topic_id );
     2467        }
     2468
     2469        bbp_bump_topic_reply_count( $topic_id, -1 );
     2470}
     2471
     2472/**
    23672473 * Bump the total hidden reply count of a topic
    23682474 *
    23692475 * @since 2.1.0 bbPress (r3825)
    function bbp_bump_topic_reply_count_hidden( $topic_id = 0, $difference = 1 ) { 
    23962502        return (int) apply_filters( 'bbp_bump_topic_reply_count_hidden', $new_count, $topic_id, $difference );
    23972503}
    23982504
     2505/**
     2506 * Increase the total hidden reply count of a topic by one.
     2507 *
     2508 * @since 2.6.0 bbPress (rXXXX)
     2509 *
     2510 * @param int $topic_id Optional. Topic id.
     2511 *
     2512 * @uses bbp_is_reply() To check if the passed topic id is a reply.
     2513 * @uses bbp_get_reply_topic_id() To get the topic id.
     2514 * @uses bbp_bump_topic_reply_count_hidden() To bump topic hidden reply count.
     2515 *
     2516 * @return void
     2517 */
     2518function bbp_increase_topic_reply_count_hidden( $topic_id = 0 ) {
     2519
     2520        // Bail early if no id is passed.
     2521        if ( empty( $topic_id ) ) {
     2522                return;
     2523        }
     2524
     2525        // If it's a reply, get the topic id.
     2526        if ( bbp_is_reply( $topic_id ) ) {
     2527                $topic_id = bbp_get_reply_topic_id( $topic_id );
     2528        }
     2529
     2530        bbp_bump_topic_reply_count_hidden( $topic_id );
     2531}
     2532
     2533/**
     2534 * Decrease the total hidden reply count of a topic by one.
     2535 *
     2536 * @since 2.6.0 bbPress (rXXXX)
     2537 *
     2538 * @param int $topic_id Optional. Topic id.
     2539 *
     2540 * @uses bbp_is_reply() To check if the passed topic id is a reply.
     2541 * @uses bbp_get_reply_topic_id() To get the topic id.
     2542 * @uses bbp_bump_topic_reply_count_hidden() To bump topic hidden reply count.
     2543 *
     2544 * @return void
     2545 */
     2546function bbp_decrease_topic_reply_count_hidden( $topic_id = 0 ) {
     2547
     2548        // Bail early if no id is passed.
     2549        if ( empty( $topic_id ) ) {
     2550                return;
     2551        }
     2552
     2553        // If it's a reply, get the topic id.
     2554        if ( bbp_is_reply( $topic_id ) ) {
     2555                $topic_id = bbp_get_reply_topic_id( $topic_id );
     2556        }
     2557
     2558        bbp_bump_topic_reply_count_hidden( $topic_id, -1 );
     2559}
     2560
     2561/**
     2562 * Update counts after a topic is inserted via `bbp_insert_topic`.
     2563 *
     2564 * @since 2.6.0 bbPress (rXXXX)
     2565 *
     2566 * @param int $reply_id
     2567 * @param int $topic_id
     2568 *
     2569 * @uses get_post_field() To get the post status.
     2570 * @uses bbp_get_public_status_id() To get the public status id.
     2571 * @uses bbp_increase_forum_topic_count() To bump the topic's forum topic count by 1.
     2572 * @uses bbp_increase_forum_topic_count_hidden() To bump the topic's forum topic
     2573 *                                               hidden count by 1.
     2574 *
     2575 * @return void
     2576 */
     2577function bbp_insert_topic_update_counts( $topic_id = 0, $forum_id = 0 ) {
     2578
     2579        // Get the topic status.
     2580        $status = get_post_field( 'post_status', $topic_id );
     2581
     2582        // If the topic is public, update the forum topic counts.
     2583        if ( $status === bbp_get_public_status_id() ) {
     2584                bbp_increase_forum_topic_count( $forum_id );
     2585
     2586        // If the topic isn't public only update the forum topic hidden count.
     2587        } else {
     2588                bbp_increase_forum_topic_count_hidden( $forum_id );
     2589        }
     2590}
     2591
    23992592/** Topic Updaters ************************************************************/
    24002593
    24012594/**
  • tests/phpunit/includes/testcase.php

    diff --git tests/phpunit/includes/testcase.php tests/phpunit/includes/testcase.php
    index 2cfe03a..9ac767a 100644
     
    33class BBP_UnitTestCase extends WP_UnitTestCase {
    44
    55        protected static $cached_SERVER_NAME = null;
     6        protected static $bbp_hooks_saved = array();
    67
    78        /**
    89         * Fake WP mail globals, to avoid errors
    class BBP_UnitTestCase extends WP_UnitTestCase { 
    2829                        }
    2930                }
    3031
     32                if ( ! self::$bbp_hooks_saved ) {
     33                        $this->_bbp_maybe_backup_hooks();
     34                }
     35
    3136                $this->factory = new BBP_UnitTest_Factory;
    3237
    3338                if ( class_exists( 'BP_UnitTest_Factory' ) ) {
    class BBP_UnitTestCase extends WP_UnitTestCase { 
    5661                                }
    5762                        }
    5863                }
     64
     65                $this->_bbp_maybe_restore_hooks();
     66        }
     67
     68        /**
     69         * Saves the action and filter-related globals so they can be restored later
     70         *
     71         * Stores $merged_filters, $wp_actions, $wp_current_filter, and $wp_filter
     72         * on a class variable so they can be restored on tearDown() using _restore_hooks()
     73         *
     74         * This is a backport from the WP_UnitTestCase in WP 4.0+, so that we can
     75         * get tests to pass in 3.9 and below. Some tests were failing for no other
     76         * reason than the below globals were a mess.
     77         *
     78         * @global array $merged_filters
     79         * @global array $wp_actions
     80         * @global array $wp_current_filter
     81         * @global array $wp_filter
     82         * @return void
     83         */
     84        protected function _bbp_maybe_backup_hooks() {
     85                if ( version_compare( $GLOBALS['wp_version'], '4.0', '>=' ) ) {
     86                        return;
     87                }
     88
     89                $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' );
     90                foreach ( $globals as $key ) {
     91                        self::$bbp_hooks_saved[ $key ] = $GLOBALS[ $key ];
     92                }
     93        }
     94
     95        /**
     96         * Restores the hook-related globals to their state at setUp()
     97         *
     98         * so that future tests aren't affected by hooks set during this last test
     99         *
     100         * This is a backport from the WP_UnitTestCase in WP 4.0+, so that we can
     101         * get tests to pass in 3.9 and below. Some tests were failing for no other
     102         * reason than the below globals were a mess.
     103         *
     104         * @global array $merged_filters
     105         * @global array $wp_actions
     106         * @global array $wp_current_filter
     107         * @global array $wp_filter
     108         * @return void
     109         */
     110        protected function _bbp_maybe_restore_hooks(){
     111                if ( version_compare( $GLOBALS['wp_version'], '4.0', '>=' ) ) {
     112                        return;
     113                }
     114
     115                $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' );
     116                foreach ( $globals as $key ) {
     117                        $GLOBALS[ $key ] = self::$bbp_hooks_saved[ $key ];
     118                }
    59119        }
    60120
    61121        function assertPreConditions() {
  • tests/phpunit/testcases/forums/functions/counts.php

    diff --git tests/phpunit/testcases/forums/functions/counts.php tests/phpunit/testcases/forums/functions/counts.php
    index dcc8bc3..633b05a 100644
    class BBP_Tests_Forums_Functions_Counts extends BBP_UnitTestCase { 
    1313         * Generic function to test the forum counts with a new topic
    1414         */
    1515        public function test_bbp_forum_new_topic_counts() {
     16                remove_action( 'bbp_insert_topic', 'bbp_insert_topic_update_counts', 10 );
     17
    1618                $f = $this->factory->forum->create();
    1719                $t1 = $this->factory->topic->create( array(
    1820                        'post_parent' => $f,
    class BBP_Tests_Forums_Functions_Counts extends BBP_UnitTestCase { 
    6062
    6163                $count = bbp_get_forum_topic_count_hidden( $f, true, true );
    6264                $this->assertSame( 0, $count );
     65
     66                remove_action( 'bbp_insert_topic', 'bbp_insert_topic_update_counts', 10, 2 );
    6367        }
    6468
    6569        /**
    class BBP_Tests_Forums_Functions_Counts extends BBP_UnitTestCase { 
    253257                $this->assertSame( '1', $count );
    254258        }
    255259
     260        /**
     261         * @covers ::bbp_increase_forum_topic_count
     262         */
     263        public function test_bbp_increase_forum_topic_count() {
     264                $f = $this->factory->forum->create();
     265
     266                $count = bbp_get_forum_topic_count( $f );
     267                $this->assertSame( '0', $count );
     268
     269                bbp_increase_forum_topic_count( $f );
     270
     271                $count = bbp_get_forum_topic_count( $f );
     272                $this->assertSame( '1', $count );
     273        }
     274
     275        /**
     276         * @covers ::bbp_decrease_forum_topic_count
     277         */
     278        public function test_bbp_decrease_forum_topic_count() {
     279                $f = $this->factory->forum->create();
     280
     281                $count = bbp_get_forum_topic_count( $f );
     282                $this->assertSame( '0', $count );
     283
     284                $t = $this->factory->topic->create_many( 9, array(
     285                        'post_parent' => $f,
     286                ) );
     287
     288                bbp_update_forum_topic_count( $f );
     289
     290                $count = bbp_get_forum_topic_count( $f );
     291                $this->assertSame( '9', $count );
     292
     293                bbp_update_forum_topic_count( $f );
     294
     295                bbp_decrease_forum_topic_count( $f );
     296
     297                $count = bbp_get_forum_topic_count( $f );
     298                $this->assertSame( '8', $count );
     299        }
     300
    256301        /**
    257302         * @covers ::bbp_bump_forum_topic_count_hidden
    258303         */
    class BBP_Tests_Forums_Functions_Counts extends BBP_UnitTestCase { 
    268313                $this->assertSame( '1', $count );
    269314        }
    270315
     316        /**
     317         * @covers ::bbp_increase_forum_topic_count_hidden
     318         */
     319        public function test_bbp_increase_forum_topic_count_hidden() {
     320                $f = $this->factory->forum->create();
     321
     322                $count = bbp_get_forum_topic_count_hidden( $f );
     323                $this->assertSame( '0', $count );
     324
     325                bbp_increase_forum_topic_count_hidden( $f );
     326
     327                $count = bbp_get_forum_topic_count_hidden( $f );
     328                $this->assertSame( '1', $count );
     329        }
     330
     331        /**
     332         * @covers ::bbp_decrease_forum_topic_count_hidden
     333         */
     334        public function test_bbp_decrease_forum_topic_count_hidden() {
     335                $f = $this->factory->forum->create();
     336
     337                $count = bbp_get_forum_topic_count_hidden( $f );
     338                $this->assertSame( '0', $count );
     339
     340                $t = $this->factory->topic->create_many( 9, array(
     341                        'post_parent' => $f,
     342                        'post_status' => bbp_get_spam_status_id(),
     343                        'topic_meta' => array(
     344                                'forum_id' => $f,
     345                                'spam_meta_status' => 'publish',
     346                        )
     347                ) );
     348
     349                bbp_update_forum_topic_count_hidden( $f );
     350
     351                $count = bbp_get_forum_topic_count_hidden( $f );
     352                $this->assertSame( '9', $count );
     353
     354                bbp_decrease_forum_topic_count_hidden( $f );
     355
     356                $count = bbp_get_forum_topic_count_hidden( $f );
     357                $this->assertSame( '8', $count );
     358        }
     359
    271360        /**
    272361         * @covers ::bbp_bump_forum_reply_count
    273362         */
    class BBP_Tests_Forums_Functions_Counts extends BBP_UnitTestCase { 
    283372                $this->assertSame( '1', $count );
    284373        }
    285374
     375        /**
     376         * @covers ::bbp_increase_forum_reply_count
     377         */
     378        public function test_bbp_increase_forum_reply_count() {
     379                $f = $this->factory->forum->create();
     380
     381                $count = bbp_get_forum_reply_count( $f );
     382                $this->assertSame( '0', $count );
     383
     384                bbp_increase_forum_reply_count( $f );
     385
     386                $count = bbp_get_forum_reply_count( $f );
     387                $this->assertSame( '1', $count );
     388        }
     389
     390        /**
     391         * @covers ::bbp_decrease_forum_reply_count
     392         */
     393        public function test_bbp_decrease_forum_reply_count() {
     394                $f = $this->factory->forum->create();
     395
     396                $count = bbp_get_forum_reply_count( $f );
     397                $this->assertSame( '0', $count );
     398
     399                $t = $this->factory->topic->create( array(
     400                        'post_parent' => $f,
     401                ) );
     402
     403                $r = $this->factory->reply->create_many( 9, array(
     404                        'post_parent' => $t,
     405                ) );
     406
     407                bbp_update_forum_reply_count( $f );
     408
     409                $count = bbp_get_forum_reply_count( $f );
     410                $this->assertSame( '9', $count );
     411
     412                bbp_decrease_forum_reply_count( $f );
     413
     414                $count = bbp_get_forum_reply_count( $f );
     415                $this->assertSame( '8', $count );
     416        }
     417
    286418        /**
    287419         * @covers ::bbp_update_forum_subforum_count
    288420         */
  • tests/phpunit/testcases/topics/functions/counts.php

    diff --git tests/phpunit/testcases/topics/functions/counts.php tests/phpunit/testcases/topics/functions/counts.php
    index c040874..ffd1bc7 100644
    class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 
    1313         * Generic function to test the topics counts with a new reply
    1414         */
    1515        public function test_bbp_topic_new_reply_counts() {
     16                remove_action( 'bbp_insert_reply', 'bbp_insert_reply_update_counts', 10 );
     17
    1618                $f = $this->factory->forum->create();
    1719                $t = $this->factory->topic->create( array(
    1820                        'post_parent' => $f,
    class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 
    7577
    7678                $count = bbp_get_topic_voice_count( $t, true );
    7779                $this->assertSame( 2, $count );
     80
     81                add_action( 'bbp_insert_reply', 'bbp_insert_reply_update_counts', 10, 2 );
    7882        }
    7983
    8084        /**
    class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 
    340344                $this->assertSame( '4', $count );
    341345        }
    342346
     347        /**
     348         * @covers ::bbp_increase_topic_reply_count
     349         */
     350        public function test_bbp_increase_topic_reply_count() {
     351                $t = $this->factory->topic->create();
     352
     353                $count = bbp_get_topic_reply_count( $t );
     354                $this->assertSame( '0', $count );
     355
     356                bbp_increase_topic_reply_count( $t );
     357
     358                $count = bbp_get_topic_reply_count( $t );
     359                $this->assertSame( '1', $count );
     360        }
     361
     362        /**
     363         * @covers ::bbp_decrease_topic_reply_count
     364         */
     365        public function test_bbp_decrease_topic_reply_count() {
     366                $t = $this->factory->topic->create();
     367
     368                $count = bbp_get_topic_reply_count( $t );
     369                $this->assertSame( '0', $count );
     370
     371                // Set the count manually to 9
     372                bbp_update_topic_reply_count( $t, 9 );
     373
     374                $count = bbp_get_topic_reply_count( $t );
     375                $this->assertSame( '9', $count );
     376
     377                bbp_decrease_topic_reply_count( $t );
     378
     379                $count = bbp_get_topic_reply_count( $t );
     380                $this->assertSame( '8', $count );
     381        }
     382
    343383        /**
    344384         * @covers ::bbp_bump_topic_reply_count_hidden
    345385         */
    class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 
    358398                $this->assertSame( '4', $count );
    359399        }
    360400
     401        /**
     402         * @covers ::bbp_increase_topic_reply_count_hidden
     403         */
     404        public function test_bbp_increase_topic_reply_count_hidden() {
     405                $t = $this->factory->topic->create();
     406
     407                $count = bbp_get_topic_reply_count_hidden( $t );
     408                $this->assertSame( '0', $count );
     409
     410                bbp_increase_topic_reply_count_hidden( $t );
     411
     412                $count = bbp_get_topic_reply_count_hidden( $t );
     413                $this->assertSame( '1', $count );
     414        }
     415
     416        /**
     417         * @covers ::bbp_decrease_topic_reply_count_hidden
     418         */
     419        public function test_bbp_decrease_topic_reply_count_hidden() {
     420                $t = $this->factory->topic->create();
     421
     422                $count = bbp_get_topic_reply_count_hidden( $t );
     423                $this->assertSame( '0', $count );
     424
     425                // Set the count manually to 9
     426                bbp_update_topic_reply_count_hidden( $t, 9 );
     427
     428                $count = bbp_get_topic_reply_count_hidden( $t );
     429                $this->assertSame( '9', $count );
     430
     431                bbp_decrease_topic_reply_count_hidden( $t );
     432
     433                $count = bbp_get_topic_reply_count_hidden( $t );
     434                $this->assertSame( '8', $count );
     435        }
     436
    361437        /**
    362438         * @covers ::bbp_update_topic_reply_count
    363439         */
  • tests/phpunit/testcases/topics/functions/topic.php

    diff --git tests/phpunit/testcases/topics/functions/topic.php tests/phpunit/testcases/topics/functions/topic.php
    index 5b191fb..f26840d 100644
    class BBP_Tests_Topics_Functions_Topic extends BBP_UnitTestCase { 
    181181                $this->assertSame( $new_forum_id, bbp_get_reply_forum_id( $reply_id ) );
    182182                $this->assertSame( $topic_id, bbp_get_reply_topic_id( $reply_id ) );
    183183
     184                // Old Topic/Reply Counts
     185                $this->assertSame( 0, bbp_get_forum_topic_count( $old_forum_id, true, true ) );
     186                $this->assertSame( 0, bbp_get_forum_reply_count( $old_forum_id, true, true ) );
     187
     188
    184189                // Retore the user
    185190                $this->set_current_user( $this->old_current_user );
    186191        }