Skip to:
Content

bbPress.org

Ticket #1799: 1799.9.patch

File 1799.9.patch, 35.7 KB (added by thebrandonallen, 9 years ago)

Tests fixes, coding standards, no voice counts

  • src/includes/core/actions.php

    diff --git src/includes/core/actions.php src/includes/core/actions.php
    index 18fa0bb..e56287d 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 // Users topic & reply counts
     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
     299// Users topic & reply counts.
    259300add_action( 'bbp_new_topic',     'bbp_increase_user_topic_count' );
    260301add_action( 'bbp_new_reply',     'bbp_increase_user_reply_count' );
    261302add_action( 'bbp_untrash_topic', 'bbp_increase_user_topic_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..cbb1776 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 The 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 The forum id.
     1212 *
     1213 * @uses bbp_get_forum_id() To get the forum id.
     1214 * @uses bbp_bump_forum_topic_count() To bump forum topic count.
     1215 *
     1216 * @return void
     1217 */
     1218function bbp_decrease_forum_topic_count( $forum_id = 0 ) {
     1219
     1220        // Bail early if no id is passed.
     1221        if ( empty( $forum_id ) ) {
     1222                return;
     1223        }
     1224
     1225        // If it's a topic, get the forum id.
     1226        if ( bbp_is_topic( $forum_id ) ) {
     1227                $forum_id = bbp_get_topic_forum_id( $forum_id );
     1228        }
     1229
     1230        bbp_bump_forum_topic_count( $forum_id, -1 );
     1231}
     1232
     1233/**
    11611234 * Bump the total hidden topic count of a forum
    11621235 *
    11631236 * @since 2.1.0 bbPress (r3825)
    function bbp_bump_forum_topic_count_hidden( $forum_id = 0, $difference = 1 ) { 
    11911264}
    11921265
    11931266/**
     1267 * Increase the total hidden topic count of a forum by one.
     1268 *
     1269 * @since 2.6.0 bbPress (rXXXX)
     1270 *
     1271 * @param int $forum_id The forum id.
     1272 *
     1273 * @uses bbp_get_forum_id() To get the forum id.
     1274 * @uses bbp_bump_forum_topic_count_hidden() To bump forum hidden topic count.
     1275 *
     1276 * @return void
     1277 */
     1278function bbp_increase_forum_topic_count_hidden( $forum_id = 0 ) {
     1279
     1280        // Bail early if no id is passed.
     1281        if ( empty( $forum_id ) ) {
     1282                return;
     1283        }
     1284
     1285        // If it's a topic, get the forum id.
     1286        if ( bbp_is_topic( $forum_id ) ) {
     1287                $forum_id = bbp_get_topic_forum_id( $forum_id );
     1288        }
     1289
     1290        bbp_bump_forum_topic_count_hidden( $forum_id );
     1291}
     1292
     1293/**
     1294 * Decrease the total hidden topic count of a forum by one.
     1295 *
     1296 * @since 2.6.0 bbPress (rXXXX)
     1297 *
     1298 * @param int $forum_id The forum id.
     1299 *
     1300 * @uses bbp_get_forum_id() To get the forum id.
     1301 * @uses bbp_bump_forum_topic_count_hidden() To bump forum hidden topic count.
     1302 *
     1303 * @return void
     1304 */
     1305function bbp_decrease_forum_topic_count_hidden( $forum_id = 0 ) {
     1306
     1307        // Bail early if no id is passed.
     1308        if ( empty( $forum_id ) ) {
     1309                return;
     1310        }
     1311
     1312        // If it's a topic, get the forum id.
     1313        if ( bbp_is_topic( $forum_id ) ) {
     1314                $forum_id = bbp_get_topic_forum_id( $forum_id );
     1315        }
     1316
     1317        bbp_bump_forum_topic_count_hidden( $forum_id, -1 );
     1318}
     1319
     1320/**
    11941321 * Bump the total topic count of a forum
    11951322 *
    11961323 * @since 2.1.0 bbPress (r3825)
    function bbp_bump_forum_reply_count( $forum_id = 0, $difference = 1, $update_anc 
    12321359                if ( ! empty( $ancestors ) ) {
    12331360                        foreach ( (array) $ancestors as $parent_forum_id ) {
    12341361
    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 );
     1362                                // Only update reply count when an ancestor is not a category.
     1363                                if ( ! bbp_is_forum_category( $parent_forum_id ) ) {
     1364
     1365                                        $parent_reply_count = bbp_get_forum_reply_count( $parent_forum_id, false, true );
     1366                                        update_post_meta( $parent_forum_id, '_bbp_reply_count', (int) ( $parent_reply_count + $difference ) );
     1367                                }
    12381368
    1239                                 // Update counts
    1240                                 update_post_meta( $parent_forum_id, '_bbp_reply_count',       (int) ( $parent_topic_count       + $difference ) );
     1369                                // Update the total reply count.
     1370                                $parent_total_reply_count = bbp_get_forum_reply_count( $parent_forum_id, true,  true );
    12411371                                update_post_meta( $parent_forum_id, '_bbp_total_reply_count', (int) ( $parent_total_reply_count + $difference ) );
    12421372                        }
    12431373                }
    function bbp_bump_forum_reply_count( $forum_id = 0, $difference = 1, $update_anc 
    12481378        return (int) apply_filters( 'bbp_bump_forum_reply_count', $forum_reply_count, $forum_id, $difference, $update_ancestors );
    12491379}
    12501380
     1381/**
     1382 * Increase the total reply count of a forum by one.
     1383 *
     1384 * @since 2.6.0 bbPress (rXXXX)
     1385 *
     1386 * @param int $forum_id The forum id.
     1387 *
     1388 * @uses bbp_get_forum_id() To get the forum id.
     1389 * @uses bbp_bump_forum_reply_count() To bump forum topic count.
     1390 *
     1391 * @return void
     1392 */
     1393function bbp_increase_forum_reply_count( $forum_id = 0 ) {
     1394
     1395        // Bail early if no id is passed.
     1396        if ( empty( $forum_id ) ) {
     1397                return;
     1398        }
     1399
     1400        // If it's a reply, get the forum id.
     1401        if ( bbp_is_reply( $forum_id ) ) {
     1402                $reply_id = $forum_id;
     1403                $forum_id = bbp_get_reply_forum_id( $reply_id );
     1404
     1405                // Don't update if this is a new, unpublished, reply.
     1406                if ( 'bbp_new_reply' === current_filter() && ! bbp_is_reply_published( $reply_id ) ) {
     1407                        return;
     1408                }
     1409        }
     1410
     1411        bbp_bump_forum_reply_count( $forum_id );
     1412}
     1413
     1414/**
     1415 * Decrease the total reply count of a forum by one.
     1416 *
     1417 * @since 2.6.0 bbPress (rXXXX)
     1418 *
     1419 * @param int $forum_id The forum id.
     1420 *
     1421 * @uses bbp_get_forum_id() To get the forum id.
     1422 * @uses bbp_bump_forum_reply_count() To bump forum topic count.
     1423 *
     1424 * @return void
     1425 */
     1426function bbp_decrease_forum_reply_count( $forum_id = 0 ) {
     1427
     1428        // Bail early if no id is passed.
     1429        if ( empty( $forum_id ) ) {
     1430                return;
     1431        }
     1432
     1433        // If it's a reply, get the forum id.
     1434        if ( bbp_is_reply( $forum_id ) ) {
     1435                $forum_id = bbp_get_reply_forum_id( $forum_id );
     1436        }
     1437
     1438        bbp_bump_forum_reply_count( $forum_id, -1 );
     1439}
     1440
     1441/**
     1442 * Update forum reply counts when a topic is approved or unapproved.
     1443 *
     1444 * @since 2.6.0 bbPress (rXXXX)
     1445 *
     1446 * @param int $topic_id The topic id.
     1447 *
     1448 * @uses bbp_get_reply_post_type() To get the reply post type.
     1449 * @uses bbp_get_public_child_ids() To get the topic's public child ids.
     1450 * @uses bbp_get_topic_forum_id() To get the topic's forum id.
     1451 * @uses bbp_bump_forum_reply_count() To bump the forum reply count.
     1452 *
     1453 * @return void
     1454 */
     1455function bbp_approved_unapproved_topic_update_forum_reply_count( $topic_id = 0 ) {
     1456
     1457        // Bail early if we don't have a topic id.
     1458        if ( empty( $topic_id ) ) {
     1459                return;
     1460        }
     1461
     1462        // Get the topic's replies.
     1463        $replies = bbp_get_public_child_ids( $topic_id, bbp_get_reply_post_type() );
     1464        $count   = count( $replies );
     1465
     1466        // If we're unapproving, set count to negative.
     1467        if ( 'bbp_unapproved_topic' === current_filter() ) {
     1468                $count = -$count;
     1469        }
     1470
     1471        // Update counts.
     1472        bbp_bump_forum_reply_count( bbp_get_topic_forum_id( $topic_id ), $count );
     1473}
     1474
    12511475/** Forum Updaters ************************************************************/
    12521476
    12531477/**
    function bbp_update_forum( $args = array() ) { 
    17321956        }
    17331957
    17341958        // 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'] );
     1959        bbp_update_forum_subforum_count( $r['forum_id'] );
     1960
     1961        // Only update topic count if we're deleting a topic, or in the dashboard.
     1962        if ( in_array( current_filter(), array( 'bbp_deleted_topic', 'save_post' ), true ) ) {
     1963                bbp_update_forum_reply_count(        $r['forum_id'] );
     1964                bbp_update_forum_topic_count(        $r['forum_id'] );
     1965                bbp_update_forum_topic_count_hidden( $r['forum_id'] );
     1966        }
    17391967
    17401968        // Update the parent forum if one was passed
    17411969        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..36d9c2e 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 The reply id.
     89 * @param int $topic_id The topic id.
     90 * @param int $forum_id The forum id.
     91 *
     92 * @uses bbp_get_reply_status() To get the reply 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        // If the reply is public, update the forum/topic reply counts.
     104        if ( bbp_get_reply_status( $reply_id ) === bbp_get_public_status_id() ) {
     105                bbp_increase_topic_reply_count( $topic_id );
     106                bbp_increase_forum_reply_count( $forum_id );
     107
     108        // If the reply isn't public only update the topic reply hidden count.
     109        } else {
     110                bbp_increase_topic_reply_count_hidden( $topic_id );
     111        }
     112}
     113
    72114/** Post Form Handlers ********************************************************/
    73115
    74116/**
    function bbp_update_reply_walker( $reply_id, $last_active_time = '', $forum_id = 
    9771019                                bbp_update_topic_last_active_time( $ancestor, $topic_last_active_time );
    9781020
    9791021                                // Counts
    980                                 bbp_update_topic_voice_count       ( $ancestor );
    981                                 bbp_update_topic_reply_count       ( $ancestor );
    982                                 bbp_update_topic_reply_count_hidden( $ancestor );
     1022                                bbp_update_topic_voice_count( $ancestor );
     1023
     1024                                // Only update reply count if we're deleting a reply, or in the dashboard.
     1025                                if ( in_array( current_filter(), array( 'bbp_deleted_reply', 'save_post' ), true ) ) {
     1026                                        bbp_update_topic_reply_count(        $ancestor );
     1027                                        bbp_update_topic_reply_count_hidden( $ancestor );
     1028                                }
    9831029
    9841030                        // Forum meta relating to most recent topic
    9851031                        } elseif ( bbp_is_forum( $ancestor ) ) {
    function bbp_update_reply_walker( $reply_id, $last_active_time = '', $forum_id = 
    10031049                                }
    10041050
    10051051                                // Counts
    1006                                 bbp_update_forum_reply_count( $ancestor );
     1052                                // Only update reply count if we're deleting a reply, or in the dashboard.
     1053                                if ( in_array( current_filter(), array( 'bbp_deleted_reply', 'save_post' ), true ) ) {
     1054                                        bbp_update_forum_reply_count( $ancestor );
     1055                                }
    10071056                        }
    10081057                }
    10091058        }
  • src/includes/topics/functions.php

    diff --git src/includes/topics/functions.php src/includes/topics/functions.php
    index 5a762e5..1878eb4 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 The topic 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 The 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 The 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 The 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 The reply id.
     2567 * @param int $topic_id The topic id.
     2568 *
     2569 * @uses bbp_get_topic_status() 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        // If the topic is public, update the forum topic counts.
     2580        if ( bbp_get_topic_status( $topic_id ) === bbp_get_public_status_id() ) {
     2581                bbp_increase_forum_topic_count( $forum_id );
     2582
     2583        // If the topic isn't public only update the forum topic hidden count.
     2584        } else {
     2585                bbp_increase_forum_topic_count_hidden( $forum_id );
     2586        }
     2587}
     2588
    23992589/** Topic Updaters ************************************************************/
    24002590
    24012591/**
  • tests/phpunit/includes/testcase.php

    diff --git tests/phpunit/includes/testcase.php tests/phpunit/includes/testcase.php
    index 2cfe03a..db6abe7 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
  • 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..a9776df 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 { 
    2325                ) );
    2426                $u = $this->factory->user->create();
    2527
    26                 // Cheating here, but we need $_SERVER['SERVER_NAME'] to be set.
    27                 $this->setUp_wp_mail( false );
     28                // Don't attempt to send an email. This is for speed and PHP errors.
     29                remove_action( 'bbp_new_topic', 'bbp_notify_forum_subscribers', 11, 4 );
    2830
    2931                // Simulate the 'bbp_new_topic' action.
    3032                do_action( 'bbp_new_topic', $t1, $f, false, bbp_get_current_user_id(), $t1 );
    3133
    32                 // Reverse our changes.
    33                 $this->tearDown_wp_mail( false );
    34 
    3534                $count = bbp_get_forum_topic_count( $f, true, true );
    3635                $this->assertSame( 1, $count );
    3736
    class BBP_Tests_Forums_Functions_Counts extends BBP_UnitTestCase { 
    4645                        ),
    4746                ) );
    4847
    49                 // Cheating here, but we need $_SERVER['SERVER_NAME'] to be set.
    50                 $this->setUp_wp_mail( false );
    51 
    5248                // Simulate the 'bbp_new_topic' action.
    5349                do_action( 'bbp_new_topic', $t2, $f, false, $u , $t2 );
    5450
    55                 // Reverse our changes.
    56                 $this->tearDown_wp_mail( false );
    57 
    5851                $count = bbp_get_forum_topic_count( $f, true, true );
    5952                $this->assertSame( 2, $count );
    6053
    6154                $count = bbp_get_forum_topic_count_hidden( $f, true, true );
    6255                $this->assertSame( 0, $count );
     56
     57                // Re-add removed actions.
     58                add_action( 'bbp_insert_topic', 'bbp_insert_topic_update_counts', 10, 2 );
     59                add_action( 'bbp_new_topic',    'bbp_notify_forum_subscribers',   11, 4 );
    6360        }
    6461
    6562        /**
    class BBP_Tests_Forums_Functions_Counts extends BBP_UnitTestCase { 
    253250                $this->assertSame( '1', $count );
    254251        }
    255252
     253        /**
     254         * @covers ::bbp_increase_forum_topic_count
     255         */
     256        public function test_bbp_increase_forum_topic_count() {
     257                $f = $this->factory->forum->create();
     258
     259                $count = bbp_get_forum_topic_count( $f );
     260                $this->assertSame( '0', $count );
     261
     262                bbp_increase_forum_topic_count( $f );
     263
     264                $count = bbp_get_forum_topic_count( $f );
     265                $this->assertSame( '1', $count );
     266        }
     267
     268        /**
     269         * @covers ::bbp_decrease_forum_topic_count
     270         */
     271        public function test_bbp_decrease_forum_topic_count() {
     272                $f = $this->factory->forum->create();
     273
     274                $count = bbp_get_forum_topic_count( $f );
     275                $this->assertSame( '0', $count );
     276
     277                $t = $this->factory->topic->create_many( 9, array(
     278                        'post_parent' => $f,
     279                ) );
     280
     281                bbp_update_forum_topic_count( $f );
     282
     283                $count = bbp_get_forum_topic_count( $f );
     284                $this->assertSame( '9', $count );
     285
     286                bbp_update_forum_topic_count( $f );
     287
     288                bbp_decrease_forum_topic_count( $f );
     289
     290                $count = bbp_get_forum_topic_count( $f );
     291                $this->assertSame( '8', $count );
     292        }
     293
    256294        /**
    257295         * @covers ::bbp_bump_forum_topic_count_hidden
    258296         */
    class BBP_Tests_Forums_Functions_Counts extends BBP_UnitTestCase { 
    268306                $this->assertSame( '1', $count );
    269307        }
    270308
     309        /**
     310         * @covers ::bbp_increase_forum_topic_count_hidden
     311         */
     312        public function test_bbp_increase_forum_topic_count_hidden() {
     313                $f = $this->factory->forum->create();
     314
     315                $count = bbp_get_forum_topic_count_hidden( $f );
     316                $this->assertSame( '0', $count );
     317
     318                bbp_increase_forum_topic_count_hidden( $f );
     319
     320                $count = bbp_get_forum_topic_count_hidden( $f );
     321                $this->assertSame( '1', $count );
     322        }
     323
     324        /**
     325         * @covers ::bbp_decrease_forum_topic_count_hidden
     326         */
     327        public function test_bbp_decrease_forum_topic_count_hidden() {
     328                $f = $this->factory->forum->create();
     329
     330                $count = bbp_get_forum_topic_count_hidden( $f );
     331                $this->assertSame( '0', $count );
     332
     333                $t = $this->factory->topic->create_many( 9, array(
     334                        'post_parent' => $f,
     335                        'post_status' => bbp_get_spam_status_id(),
     336                        'topic_meta' => array(
     337                                'forum_id' => $f,
     338                                'spam_meta_status' => 'publish',
     339                        )
     340                ) );
     341
     342                bbp_update_forum_topic_count_hidden( $f );
     343
     344                $count = bbp_get_forum_topic_count_hidden( $f );
     345                $this->assertSame( '9', $count );
     346
     347                bbp_decrease_forum_topic_count_hidden( $f );
     348
     349                $count = bbp_get_forum_topic_count_hidden( $f );
     350                $this->assertSame( '8', $count );
     351        }
     352
    271353        /**
    272354         * @covers ::bbp_bump_forum_reply_count
    273355         */
    class BBP_Tests_Forums_Functions_Counts extends BBP_UnitTestCase { 
    283365                $this->assertSame( '1', $count );
    284366        }
    285367
     368        /**
     369         * @covers ::bbp_increase_forum_reply_count
     370         */
     371        public function test_bbp_increase_forum_reply_count() {
     372                $f = $this->factory->forum->create();
     373
     374                $count = bbp_get_forum_reply_count( $f );
     375                $this->assertSame( '0', $count );
     376
     377                bbp_increase_forum_reply_count( $f );
     378
     379                $count = bbp_get_forum_reply_count( $f );
     380                $this->assertSame( '1', $count );
     381        }
     382
     383        /**
     384         * @covers ::bbp_decrease_forum_reply_count
     385         */
     386        public function test_bbp_decrease_forum_reply_count() {
     387                $f = $this->factory->forum->create();
     388
     389                $count = bbp_get_forum_reply_count( $f );
     390                $this->assertSame( '0', $count );
     391
     392                $t = $this->factory->topic->create( array(
     393                        'post_parent' => $f,
     394                ) );
     395
     396                $r = $this->factory->reply->create_many( 9, array(
     397                        'post_parent' => $t,
     398                ) );
     399
     400                bbp_update_forum_reply_count( $f );
     401
     402                $count = bbp_get_forum_reply_count( $f );
     403                $this->assertSame( '9', $count );
     404
     405                bbp_decrease_forum_reply_count( $f );
     406
     407                $count = bbp_get_forum_reply_count( $f );
     408                $this->assertSame( '8', $count );
     409        }
     410
    286411        /**
    287412         * @covers ::bbp_update_forum_subforum_count
    288413         */
  • 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..c5ffc88 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 { 
    3133                ) );
    3234                $u = $this->factory->user->create();
    3335
    34                 // Cheating here, but we need $_SERVER['SERVER_NAME'] to be set.
    35                 $this->setUp_wp_mail( false );
     36                // Don't attempt to send an email. This is for speed and PHP errors.
     37                remove_action( 'bbp_new_reply', 'bbp_notify_subscribers', 11, 5 );
    3638
    3739                // Simulate the 'bbp_new_reply' action.
    3840                do_action( 'bbp_new_reply', $r1, $t, $f, false, bbp_get_current_user_id() );
    3941
    40                 // Reverse our changes.
    41                 $this->tearDown_wp_mail( false );
    42 
    4342                $count = bbp_get_topic_reply_count( $t, true );
    4443                $this->assertSame( 1, $count );
    4544
    class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 
    5857                        ),
    5958                ) );
    6059
    61                 // Cheating here, but we need $_SERVER['SERVER_NAME'] to be set.
    62                 $this->setUp_wp_mail( false );
    63 
    6460                // Simulate the 'bbp_new_topic' action.
    6561                do_action( 'bbp_new_reply', $r2, $t, $f, false, $u );
    6662
    67                 // Reverse our changes.
    68                 $this->tearDown_wp_mail( false );
    69 
    7063                $count = bbp_get_topic_reply_count( $t, true );
    7164                $this->assertSame( 2, $count );
    7265
    class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 
    7568
    7669                $count = bbp_get_topic_voice_count( $t, true );
    7770                $this->assertSame( 2, $count );
     71
     72                // Re-add removed actions.
     73                add_action( 'bbp_insert_reply', 'bbp_insert_reply_update_counts', 10, 2 );
     74                add_action( 'bbp_new_reply',    'bbp_notify_subscribers',         11, 5 );
     75
    7876        }
    7977
    8078        /**
    class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 
    340338                $this->assertSame( '4', $count );
    341339        }
    342340
     341        /**
     342         * @covers ::bbp_increase_topic_reply_count
     343         */
     344        public function test_bbp_increase_topic_reply_count() {
     345                $t = $this->factory->topic->create();
     346
     347                $count = bbp_get_topic_reply_count( $t );
     348                $this->assertSame( '0', $count );
     349
     350                bbp_increase_topic_reply_count( $t );
     351
     352                $count = bbp_get_topic_reply_count( $t );
     353                $this->assertSame( '1', $count );
     354        }
     355
     356        /**
     357         * @covers ::bbp_decrease_topic_reply_count
     358         */
     359        public function test_bbp_decrease_topic_reply_count() {
     360                $t = $this->factory->topic->create();
     361
     362                $count = bbp_get_topic_reply_count( $t );
     363                $this->assertSame( '0', $count );
     364
     365                // Set the count manually to 9
     366                bbp_update_topic_reply_count( $t, 9 );
     367
     368                $count = bbp_get_topic_reply_count( $t );
     369                $this->assertSame( '9', $count );
     370
     371                bbp_decrease_topic_reply_count( $t );
     372
     373                $count = bbp_get_topic_reply_count( $t );
     374                $this->assertSame( '8', $count );
     375        }
     376
    343377        /**
    344378         * @covers ::bbp_bump_topic_reply_count_hidden
    345379         */
    class BBP_Tests_Topics_Functions_Counts extends BBP_UnitTestCase { 
    358392                $this->assertSame( '4', $count );
    359393        }
    360394
     395        /**
     396         * @covers ::bbp_increase_topic_reply_count_hidden
     397         */
     398        public function test_bbp_increase_topic_reply_count_hidden() {
     399                $t = $this->factory->topic->create();
     400
     401                $count = bbp_get_topic_reply_count_hidden( $t );
     402                $this->assertSame( '0', $count );
     403
     404                bbp_increase_topic_reply_count_hidden( $t );
     405
     406                $count = bbp_get_topic_reply_count_hidden( $t );
     407                $this->assertSame( '1', $count );
     408        }
     409
     410        /**
     411         * @covers ::bbp_decrease_topic_reply_count_hidden
     412         */
     413        public function test_bbp_decrease_topic_reply_count_hidden() {
     414                $t = $this->factory->topic->create();
     415
     416                $count = bbp_get_topic_reply_count_hidden( $t );
     417                $this->assertSame( '0', $count );
     418
     419                // Set the count manually to 9
     420                bbp_update_topic_reply_count_hidden( $t, 9 );
     421
     422                $count = bbp_get_topic_reply_count_hidden( $t );
     423                $this->assertSame( '9', $count );
     424
     425                bbp_decrease_topic_reply_count_hidden( $t );
     426
     427                $count = bbp_get_topic_reply_count_hidden( $t );
     428                $this->assertSame( '8', $count );
     429        }
     430
    361431        /**
    362432         * @covers ::bbp_update_topic_reply_count
    363433         */
  • 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        }