Skip to:
Content

bbPress.org

Changeset 6036


Ignore:
Timestamp:
05/30/2016 10:28:36 AM (5 years ago)
Author:
netweb
Message:

General Performance: Introduce increase/decrease helper count functions

Previously when a new topic or reply was created, a bunch of queries to recalculate the topic and reply counts for topics and forums were ran. Now these have been replaced with more efficient increase/decrease helper functions to get the current value and just "bump" the count based on the action (new topic-reply/split-topic/move-topic/spam-trash-topic/etc...)

Props thebrandonallen, tharsheblows, netweb
See #1799

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/core/actions.php

    r6034 r6036  
    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' );
     
    267308add_action( 'bbp_spam_topic',    'bbp_decrease_user_topic_count' );
    268309add_action( 'bbp_spam_reply',    'bbp_decrease_user_reply_count' );
     310
     311// Insert 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 );
    269314
    270315// Topic status transition helpers for replies
  • trunk/src/includes/forums/functions.php

    r6032 r6036  
    7373        'forum_id' => $forum_id,
    7474    ) );
     75
     76    /**
     77     * Fires after forum has been inserted via `bbp_insert_forum`.
     78     *
     79     * @since 2.6.0 bbPress (r6036)
     80     *
     81     * @param int $forum_id The forum id.
     82     */
     83    do_action( 'bbp_insert_forum', (int) $forum_id );
    7584
    7685    // Return new forum ID
     
    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 );
     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                }
     1159
     1160                // Update the total topic count.
    11461161                $parent_total_topic_count = bbp_get_forum_topic_count( $parent_forum_id, true,  true );
    1147 
    1148                 // Update counts
    1149                 update_post_meta( $parent_forum_id, '_bbp_topic_count',       (int) ( $parent_topic_count       + $difference ) );
    11501162                update_post_meta( $parent_forum_id, '_bbp_total_topic_count', (int) ( $parent_total_topic_count + $difference ) );
    11511163            }
     
    11561168
    11571169    return (int) apply_filters( 'bbp_bump_forum_topic_count', $forum_topic_count, $forum_id, $difference, $update_ancestors );
     1170}
     1171
     1172/**
     1173 * Increase the total topic count of a forum by one.
     1174 *
     1175 * @since 2.6.0 bbPress (r6036)
     1176 *
     1177 * @param int $forum_id The forum id.
     1178 *
     1179 * @uses bbp_is_topic() To get the topic id
     1180 * @uses bbp_get_topic_forum_id() To get the topics forum id
     1181 * @uses bbp_is_topic_published() To get the topics published status
     1182 * @uses bbp_is_topic_closed() To get the topics closed status
     1183 * @uses bbp_increase_forum_topic_count_hidden() To increase the forums hidden
     1184 *                                                topic count by 1
     1185 * @uses bbp_bump_forum_topic_count() To bump the forum topic count
     1186 *
     1187 * @return void
     1188 */
     1189function bbp_increase_forum_topic_count( $forum_id = 0 ) {
     1190
     1191    // Bail early if no id is passed.
     1192    if ( empty( $forum_id ) ) {
     1193        return;
     1194    }
     1195
     1196    // If it's a topic, get the forum id.
     1197    if ( bbp_is_topic( $forum_id ) ) {
     1198        $topic_id = $forum_id;
     1199        $forum_id = bbp_get_topic_forum_id( $topic_id );
     1200
     1201        // If this is a new, unpublished, topic, increase hidden count and bail.
     1202        if ( 'bbp_new_topic' === current_filter() && ( ! bbp_is_topic_published( $topic_id ) && ! bbp_is_topic_closed( $topic_id ) ) ) {
     1203            bbp_increase_forum_topic_count_hidden( $forum_id );
     1204            return;
     1205        }
     1206    }
     1207
     1208    bbp_bump_forum_topic_count( $forum_id );
     1209}
     1210
     1211/**
     1212 * Decrease the total topic count of a forum by one.
     1213 *
     1214 * @since 2.6.0 bbPress (r6036)
     1215 *
     1216 * @param int $forum_id The forum id.
     1217 *
     1218 * @uses bbp_is_topic() To get the topic id
     1219 * @uses bbp_get_topic_forum_id() To get the topics forum id
     1220 * @uses bbp_bump_forum_topic_count() To bump the forum topic count
     1221 *
     1222 * @return void
     1223 */
     1224function bbp_decrease_forum_topic_count( $forum_id = 0 ) {
     1225
     1226    // Bail early if no id is passed.
     1227    if ( empty( $forum_id ) ) {
     1228        return;
     1229    }
     1230
     1231    // If it's a topic, get the forum id.
     1232    if ( bbp_is_topic( $forum_id ) ) {
     1233        $forum_id = bbp_get_topic_forum_id( $forum_id );
     1234    }
     1235
     1236    bbp_bump_forum_topic_count( $forum_id, -1 );
    11581237}
    11591238
     
    11921271
    11931272/**
     1273 * Increase the total hidden topic count of a forum by one.
     1274 *
     1275 * @since 2.6.0 bbPress (r6036)
     1276 *
     1277 * @param int $forum_id The forum id.
     1278 *
     1279 * @uses bbp_is_topic() To get the topic id
     1280 * @uses bbp_get_topic_forum_id() To get the topics forum id
     1281 * @uses bbp_bump_forum_topic_count_hidden() To bump the forum hidden topic count
     1282 *
     1283 * @return void
     1284 */
     1285function bbp_increase_forum_topic_count_hidden( $forum_id = 0 ) {
     1286
     1287    // Bail early if no id is passed.
     1288    if ( empty( $forum_id ) ) {
     1289        return;
     1290    }
     1291
     1292    // If it's a topic, get the forum id.
     1293    if ( bbp_is_topic( $forum_id ) ) {
     1294        $forum_id = bbp_get_topic_forum_id( $forum_id );
     1295    }
     1296
     1297    bbp_bump_forum_topic_count_hidden( $forum_id );
     1298}
     1299
     1300/**
     1301 * Decrease the total hidden topic count of a forum by one.
     1302 *
     1303 * @since 2.6.0 bbPress (r6036)
     1304 *
     1305 * @param int $forum_id The forum id.
     1306 *
     1307 * @uses bbp_is_topic() To get the topic id
     1308 * @uses bbp_get_topic_forum_id() To get the topics forum id
     1309 * @uses bbp_bump_forum_topic_count_hidden() To bump the forums hidden topic
     1310 *                                            count by -1
     1311 *
     1312 * @return void
     1313 */
     1314function bbp_decrease_forum_topic_count_hidden( $forum_id = 0 ) {
     1315
     1316    // Bail early if no id is passed.
     1317    if ( empty( $forum_id ) ) {
     1318        return;
     1319    }
     1320
     1321    // If it's a topic, get the forum id.
     1322    if ( bbp_is_topic( $forum_id ) ) {
     1323        $forum_id = bbp_get_topic_forum_id( $forum_id );
     1324    }
     1325
     1326    bbp_bump_forum_topic_count_hidden( $forum_id, -1 );
     1327}
     1328
     1329/**
    11941330 * Bump the total topic count of a forum
    11951331 *
     
    12331369            foreach ( (array) $ancestors as $parent_forum_id ) {
    12341370
    1235                 // Get forum counts
    1236                 $parent_topic_count       = bbp_get_forum_reply_count( $parent_forum_id, false, true );
     1371                // Only update reply count when an ancestor is not a category.
     1372                if ( ! bbp_is_forum_category( $parent_forum_id ) ) {
     1373
     1374                    $parent_reply_count = bbp_get_forum_reply_count( $parent_forum_id, false, true );
     1375                    update_post_meta( $parent_forum_id, '_bbp_reply_count', (int) ( $parent_reply_count + $difference ) );
     1376                }
     1377
     1378                // Update the total reply count.
    12371379                $parent_total_reply_count = bbp_get_forum_reply_count( $parent_forum_id, true,  true );
    1238 
    1239                 // Update counts
    1240                 update_post_meta( $parent_forum_id, '_bbp_reply_count',       (int) ( $parent_topic_count       + $difference ) );
    12411380                update_post_meta( $parent_forum_id, '_bbp_total_reply_count', (int) ( $parent_total_reply_count + $difference ) );
    12421381            }
     
    12471386
    12481387    return (int) apply_filters( 'bbp_bump_forum_reply_count', $forum_reply_count, $forum_id, $difference, $update_ancestors );
     1388}
     1389
     1390/**
     1391 * Increase the total reply count of a forum by one.
     1392 *
     1393 * @since 2.6.0 bbPress (r6036)
     1394 *
     1395 * @param int $forum_id The forum id.
     1396 *
     1397 * @uses bbp_is_reply() To get the reply id
     1398 * @uses bbp_get_reply_forum_id() To get the replies forum id
     1399 * @uses bbp_is_reply_published() To get the replies published status
     1400 * @uses bbp_bump_forum_reply_count() To bump the forum reply count
     1401 *
     1402 * @return void
     1403 */
     1404function bbp_increase_forum_reply_count( $forum_id = 0 ) {
     1405
     1406    // Bail early if no id is passed.
     1407    if ( empty( $forum_id ) ) {
     1408        return;
     1409    }
     1410
     1411    // If it's a reply, get the forum id.
     1412    if ( bbp_is_reply( $forum_id ) ) {
     1413        $reply_id = $forum_id;
     1414        $forum_id = bbp_get_reply_forum_id( $reply_id );
     1415
     1416        // Don't update if this is a new, unpublished, reply.
     1417        if ( 'bbp_new_reply' === current_filter() && ! bbp_is_reply_published( $reply_id ) ) {
     1418            return;
     1419        }
     1420    }
     1421
     1422    bbp_bump_forum_reply_count( $forum_id );
     1423}
     1424
     1425/**
     1426 * Decrease the total reply count of a forum by one.
     1427 *
     1428 * @since 2.6.0 bbPress (r6036)
     1429 *
     1430 * @param int $forum_id The forum id.
     1431 *
     1432 * @uses bbp_is_reply() To get the reply id
     1433 * @uses bbp_get_reply_forum_id() To get the replies forum id
     1434 * @uses bbp_bump_forum_reply_count() To bump the forum reply count
     1435 *
     1436 * @return void
     1437 */
     1438function bbp_decrease_forum_reply_count( $forum_id = 0 ) {
     1439
     1440    // Bail early if no id is passed.
     1441    if ( empty( $forum_id ) ) {
     1442        return;
     1443    }
     1444
     1445    // If it's a reply, get the forum id.
     1446    if ( bbp_is_reply( $forum_id ) ) {
     1447        $forum_id = bbp_get_reply_forum_id( $forum_id );
     1448    }
     1449
     1450    bbp_bump_forum_reply_count( $forum_id, -1 );
     1451}
     1452
     1453/**
     1454 * Update forum reply counts when a topic is approved or unapproved.
     1455 *
     1456 * @since 2.6.0 bbPress (r6036)
     1457 *
     1458 * @param int $topic_id The topic id.
     1459 *
     1460 * @uses bbp_get_public_child_ids() To get the topic's public child ids
     1461 * @uses bbp_get_reply_post_type() To get the reply post type
     1462 * @uses bbp_bump_forum_reply_count() To bump the forum reply count
     1463 * @uses bbp_get_topic_forum_id() To get the topics forum id
     1464 *
     1465 * @return void
     1466 */
     1467function bbp_approved_unapproved_topic_update_forum_reply_count( $topic_id = 0 ) {
     1468
     1469    // Bail early if we don't have a topic id.
     1470    if ( empty( $topic_id ) ) {
     1471        return;
     1472    }
     1473
     1474    // Get the topic's replies.
     1475    $replies = bbp_get_public_child_ids( $topic_id, bbp_get_reply_post_type() );
     1476    $count   = count( $replies );
     1477
     1478    // If we're unapproving, set count to negative.
     1479    if ( 'bbp_unapproved_topic' === current_filter() ) {
     1480        $count = -$count;
     1481    }
     1482
     1483    // Update counts.
     1484    bbp_bump_forum_reply_count( bbp_get_topic_forum_id( $topic_id ), $count );
    12491485}
    12501486
     
    17331969
    17341970    // 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'] );
     1971    bbp_update_forum_subforum_count( $r['forum_id'] );
     1972
     1973    // Only update topic count if we're deleting a topic, or in the dashboard.
     1974    if ( in_array( current_filter(), array( 'bbp_deleted_topic', 'save_post' ), true ) ) {
     1975        bbp_update_forum_reply_count(        $r['forum_id'] );
     1976        bbp_update_forum_topic_count(        $r['forum_id'] );
     1977        bbp_update_forum_topic_count_hidden( $r['forum_id'] );
     1978    }
    17391979
    17401980    // Update the parent forum if one was passed
     
    18492089 */
    18502090function bbp_get_hidden_forum_ids() {
    1851     $forum_ids = get_option( '_bbp_hidden_forums', array() );
     2091    $forum_ids = get_option( '_bbp_hidden_forums', array() );
    18522092
    18532093    return apply_filters( 'bbp_get_hidden_forum_ids', (array) $forum_ids );
     
    18662106 */
    18672107function bbp_get_private_forum_ids() {
    1868     $forum_ids = get_option( '_bbp_private_forums', array() );
     2108    $forum_ids = get_option( '_bbp_private_forums', array() );
    18692109
    18702110    return apply_filters( 'bbp_get_private_forum_ids', (array) $forum_ids );
     
    20752315 */
    20762316function bbp_forum_query_topic_ids( $forum_id ) {
    2077     $topic_ids = bbp_get_public_child_ids( $forum_id, bbp_get_topic_post_type() );
     2317    $topic_ids = bbp_get_public_child_ids( $forum_id, bbp_get_topic_post_type() );
    20782318
    20792319    return (array) apply_filters( 'bbp_forum_query_topic_ids', $topic_ids, $forum_id );
  • trunk/src/includes/replies/functions.php

    r6032 r6036  
    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 (r6036)
     72     *
     73     * @param int $reply_id               The reply id.
     74     * @param int $reply_meta['topic_id'] The reply topic meta.
     75     * @param int $reply_meta['forum_id'] The reply forum meta.
     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;
     81}
     82
     83/**
     84 * Update counts after a reply is inserted via `bbp_insert_reply`.
     85 *
     86 * @since 2.6.0 bbPress (r6036)
     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 topics reply count by 1
     95 * @uses bbp_increase_forum_reply_count() To bump the forums reply count by 1
     96 * @uses bbp_increase_topic_reply_count_hidden() To bump the topics hidden reply
     97 *                                               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    }
    70112}
    71113
     
    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
     
    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        }
  • trunk/src/includes/topics/functions.php

    r6032 r6036  
    7070    // Update the topic and hierarchy
    7171    bbp_update_topic( $topic_id, $topic_meta['forum_id'], array(), $topic_data['post_author'], false );
     72
     73    /**
     74     * Fires after topic has been inserted via `bbp_insert_topic`.
     75     *
     76     * @since 2.6.0 bbPress (r6036)
     77     *
     78     * @param int $topic_id               The topic id.
     79     * @param int $topic_meta['forum_id'] The topic forum meta.
     80     */
     81    do_action( 'bbp_insert_topic', (int) $topic_id, (int) $topic_meta['forum_id'] );
    7282
    7383    // Return new topic ID
     
    10221032 * up the old and new branches and updating the counts.
    10231033 *
    1024  * @param int $topic_id Topic id
    1025  * @param int $old_forum_id Old forum id
    1026  * @param int $new_forum_id New forum id
     1034 * @since 2.0.0 bbPress (r2907)
     1035 *
     1036 * @param int $topic_id     The topic id.
     1037 * @param int $old_forum_id Old forum id.
     1038 * @param int $new_forum_id New forum id.
    10271039 * @uses bbp_get_topic_id() To get the topic id
    10281040 * @uses bbp_get_forum_id() To get the forum id
     1041 * @uses bbp_clean_post_cache() To clean the old and new forum post caches
     1042 * @uses bbp_update_topic_forum_id() To update the topic forum id
    10291043 * @uses wp_update_post() To update the topic post parent`
    10301044 * @uses bbp_get_stickies() To get the old forums sticky topics
     
    10331047 * @uses bbp_stick_topic() To stick the topic in the new forum
    10341048 * @uses bbp_get_reply_post_type() To get the reply post type
    1035  * @uses bbp_get_all_child_ids() To get the public child ids
     1049 * @uses bbp_get_all_child_ids() To get all the child ids
    10361050 * @uses bbp_update_reply_forum_id() To update the reply forum id
    1037  * @uses bbp_update_topic_forum_id() To update the topic forum id
    10381051 * @uses get_post_ancestors() To get the topic's ancestors
     1052 * @uses bbp_get_public_child_ids() To get all the public child ids
     1053 * @uses get_post_field() To get the topic's post status
     1054 * @uses bbp_get_public_status_id() To get the public status id
     1055 * @uses bbp_decrease_forum_topic_count() To bump the forum topic count by -1
     1056 * @uses bbp_bump_forum_reply_count() To bump the forum reply count
     1057 * @uses bbp_increase_forum_topic_count() To bump the forum topic count by 1
     1058 * @uses bbp_decrease_forum_topic_count_hidden() To bump the forum topic hidden count by -1
     1059 * @uses bbp_increase_forum_topic_count_hidden() To bump the forum topic hidden count by 1
    10391060 * @uses bbp_is_forum() To check if the ancestor is a forum
    10401061 * @uses bbp_update_forum() To update the forum
     
    11101131    // Get topic ancestors
    11111132    $old_forum_ancestors = array_values( array_unique( array_merge( array( $old_forum_id ), (array) get_post_ancestors( $old_forum_id ) ) ) );
     1133
     1134    // Get reply count.
     1135    $public_reply_count = count( bbp_get_public_child_ids( $topic_id, bbp_get_reply_post_type() ) );
     1136
     1137    // Topic status.
     1138    $topic_status = get_post_field( 'post_status', $topic_id );
     1139
     1140    // Update old/new forum counts.
     1141    if ( $topic_status === bbp_get_public_status_id() ) {
     1142
     1143        // Update old forum counts.
     1144        bbp_decrease_forum_topic_count( $old_forum_id );
     1145        bbp_bump_forum_reply_count( $old_forum_id, -$public_reply_count );
     1146
     1147        // Update new forum counts.
     1148        bbp_increase_forum_topic_count( $new_forum_id );
     1149        bbp_bump_forum_reply_count( $new_forum_id, $public_reply_count );
     1150    } else {
     1151
     1152        // Update old forum counts.
     1153        bbp_decrease_forum_topic_count_hidden( $old_forum_id );
     1154
     1155        // Update new forum counts.
     1156        bbp_increase_forum_topic_count_hidden( $new_forum_id );
     1157    }
    11121158
    11131159    // Loop through ancestors and update them
     
    23792425
    23802426/**
     2427 * Increase the total reply count of a topic by one.
     2428 *
     2429 * @since 2.6.0 bbPress (r6036)
     2430 *
     2431 * @param int $topic_id The topic id.
     2432 *
     2433 * @uses bbp_is_reply() To check if the passed topic id is a reply
     2434 * @uses bbp_get_reply_topic_id() To get the replies topic id
     2435 * @uses bbp_is_reply_published() To check if the reply is published
     2436 * @uses bbp_increase_topic_reply_count_hidden() To increase the topics reply
     2437 *                                                hidden count by 1
     2438 * @uses bbp_bump_topic_reply_count() To bump the topic reply count
     2439 *
     2440 * @return void
     2441 */
     2442function bbp_increase_topic_reply_count( $topic_id = 0 ) {
     2443
     2444    // Bail early if no id is passed.
     2445    if ( empty( $topic_id ) ) {
     2446        return;
     2447    }
     2448
     2449    // If it's a reply, get the topic id.
     2450    if ( bbp_is_reply( $topic_id ) ) {
     2451        $reply_id = $topic_id;
     2452        $topic_id = bbp_get_reply_topic_id( $reply_id );
     2453
     2454        // If this is a new, unpublished, reply, update hidden count and bail.
     2455        if ( 'bbp_new_reply' === current_filter() && ! bbp_is_reply_published( $reply_id ) ) {
     2456            bbp_increase_topic_reply_count_hidden( $topic_id );
     2457            return;
     2458        }
     2459    }
     2460
     2461    bbp_bump_topic_reply_count( $topic_id );
     2462}
     2463
     2464/**
     2465 * Decrease the total reply count of a topic by one.
     2466 *
     2467 * @since 2.6.0 bbPress (r6036)
     2468 *
     2469 * @param int $topic_id The topic id.
     2470 *
     2471 * @uses bbp_is_reply() To check if the passed topic id is a reply
     2472 * @uses bbp_get_reply_topic_id() To get the replies topic id
     2473 * @uses bbp_bump_topic_reply_count() To bump the topic reply count
     2474 *
     2475 * @return void
     2476 */
     2477function bbp_decrease_topic_reply_count( $topic_id = 0 ) {
     2478
     2479    // Bail early if no id is passed.
     2480    if ( empty( $topic_id ) ) {
     2481        return;
     2482    }
     2483
     2484    // If it's a reply, get the topic id.
     2485    if ( bbp_is_reply( $topic_id ) ) {
     2486        $topic_id = bbp_get_reply_topic_id( $topic_id );
     2487    }
     2488
     2489    bbp_bump_topic_reply_count( $topic_id, -1 );
     2490}
     2491
     2492/**
    23812493 * Bump the total hidden reply count of a topic
    23822494 *
     
    24092521
    24102522    return (int) apply_filters( 'bbp_bump_topic_reply_count_hidden', $new_count, $topic_id, $difference );
     2523}
     2524
     2525/**
     2526 * Increase the total hidden reply count of a topic by one.
     2527 *
     2528 * @since 2.6.0 bbPress (r6036)
     2529 *
     2530 * @param int $topic_id The topic id.
     2531 *
     2532 * @uses bbp_is_reply() To check if the passed topic id is a reply
     2533 * @uses bbp_get_reply_topic_id() To get the topic id
     2534 * @uses bbp_bump_topic_reply_count_hidden() To bump topic hidden reply count
     2535 *
     2536 * @return void
     2537 */
     2538function bbp_increase_topic_reply_count_hidden( $topic_id = 0 ) {
     2539
     2540    // Bail early if no id is passed.
     2541    if ( empty( $topic_id ) ) {
     2542        return;
     2543    }
     2544
     2545    // If it's a reply, get the topic id.
     2546    if ( bbp_is_reply( $topic_id ) ) {
     2547        $topic_id = bbp_get_reply_topic_id( $topic_id );
     2548    }
     2549
     2550    bbp_bump_topic_reply_count_hidden( $topic_id );
     2551}
     2552
     2553/**
     2554 * Decrease the total hidden reply count of a topic by one.
     2555 *
     2556 * @since 2.6.0 bbPress (r6036)
     2557 *
     2558 * @param int $topic_id The topic id.
     2559 *
     2560 * @uses bbp_is_reply() To check if the passed topic id is a reply
     2561 * @uses bbp_get_reply_topic_id() To get the topic id
     2562 * @uses bbp_bump_topic_reply_count_hidden() To bump topic hidden reply count
     2563 *
     2564 * @return void
     2565 */
     2566function bbp_decrease_topic_reply_count_hidden( $topic_id = 0 ) {
     2567
     2568    // Bail early if no id is passed.
     2569    if ( empty( $topic_id ) ) {
     2570        return;
     2571    }
     2572
     2573    // If it's a reply, get the topic id.
     2574    if ( bbp_is_reply( $topic_id ) ) {
     2575        $topic_id = bbp_get_reply_topic_id( $topic_id );
     2576    }
     2577
     2578    bbp_bump_topic_reply_count_hidden( $topic_id, -1 );
     2579}
     2580
     2581/**
     2582 * Update counts after a topic is inserted via `bbp_insert_topic`.
     2583 *
     2584 * @since 2.6.0 bbPress (r6036)
     2585 *
     2586 * @param int $reply_id The reply id.
     2587 * @param int $topic_id The topic id.
     2588 *
     2589 * @uses bbp_get_topic_status() To get the post status
     2590 * @uses bbp_get_public_status_id() To get the public status id
     2591 * @uses bbp_increase_forum_topic_count() To bump the topic's forum topic count by 1
     2592 * @uses bbp_increase_forum_topic_count_hidden() To bump the topic's forum topic
     2593 *                                               hidden count by 1
     2594 *
     2595 * @return void
     2596 */
     2597function bbp_insert_topic_update_counts( $topic_id = 0, $forum_id = 0 ) {
     2598
     2599    // If the topic is public, update the forum topic counts.
     2600    if ( bbp_get_topic_status( $topic_id ) === bbp_get_public_status_id() ) {
     2601        bbp_increase_forum_topic_count( $forum_id );
     2602
     2603    // If the topic isn't public only update the forum topic hidden count.
     2604    } else {
     2605        bbp_increase_forum_topic_count_hidden( $forum_id );
     2606    }
    24112607}
    24122608
  • trunk/tests/phpunit/testcases/forums/functions/counts.php

    r6011 r6036  
    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(
     
    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 );
     
    4645            ),
    4746        ) );
    48 
    49         // Cheating here, but we need $_SERVER['SERVER_NAME'] to be set.
    50         $this->setUp_wp_mail( false );
    5147
    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 
    58         $count = bbp_get_forum_topic_count( $f, true, true );
    59         $this->assertSame( 2, $count );
    60 
    61         $count = bbp_get_forum_topic_count_hidden( $f, true, true );
    62         $this->assertSame( 0, $count );
     51        $count = bbp_get_forum_topic_count( $f, true, true );
     52        $this->assertSame( 2, $count );
     53
     54        $count = bbp_get_forum_topic_count_hidden( $f, true, true );
     55        $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
     
    255252
    256253    /**
     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( 2, 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( '2', $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( '1', $count );
     292    }
     293
     294    /**
    257295     * @covers ::bbp_bump_forum_topic_count_hidden
    258296     */
     
    270308
    271309    /**
     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( 2, 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( '2', $count );
     346
     347        bbp_decrease_forum_topic_count_hidden( $f );
     348
     349        $count = bbp_get_forum_topic_count_hidden( $f );
     350        $this->assertSame( '1', $count );
     351    }
     352
     353    /**
    272354     * @covers ::bbp_bump_forum_reply_count
    273355     */
     
    279361
    280362        bbp_bump_forum_reply_count( $f );
     363
     364        $count = bbp_get_forum_reply_count( $f );
     365        $this->assertSame( '1', $count );
     366    }
     367
     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( 2, 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( '2', $count );
     404
     405        bbp_decrease_forum_reply_count( $f );
    281406
    282407        $count = bbp_get_forum_reply_count( $f );
  • trunk/tests/phpunit/testcases/topics/functions/counts.php

    r5922 r6036  
    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(
     
    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_topic_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 );
     
    5857            ),
    5958        ) );
    60 
    61         // Cheating here, but we need $_SERVER['SERVER_NAME'] to be set.
    62         $this->setUp_wp_mail( false );
    6359
    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 
    70         $count = bbp_get_topic_reply_count( $t, true );
    71         $this->assertSame( 2, $count );
    72 
    73         $count = bbp_get_topic_reply_count_hidden( $t, true );
    74         $this->assertSame( 0, $count );
    75 
    76         $count = bbp_get_topic_voice_count( $t, true );
    77         $this->assertSame( 2, $count );
     63        $count = bbp_get_topic_reply_count( $t, true );
     64        $this->assertSame( 2, $count );
     65
     66        $count = bbp_get_topic_reply_count_hidden( $t, true );
     67        $this->assertSame( 0, $count );
     68
     69        $count = bbp_get_topic_voice_count( $t, true );
     70        $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_topic_subscribers',   11, 5 );
     75
    7876    }
    7977
     
    342340
    343341    /**
     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 2
     366        bbp_update_topic_reply_count( $t, 2 );
     367
     368        $count = bbp_get_topic_reply_count( $t );
     369        $this->assertSame( '2', $count );
     370
     371        bbp_decrease_topic_reply_count( $t );
     372
     373        $count = bbp_get_topic_reply_count( $t );
     374        $this->assertSame( '1', $count );
     375    }
     376
     377    /**
    344378     * @covers ::bbp_bump_topic_reply_count_hidden
    345379     */
     
    357391        $count = bbp_get_topic_reply_count_hidden( $t );
    358392        $this->assertSame( '4', $count );
     393    }
     394
     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 2
     420        bbp_update_topic_reply_count_hidden( $t, 2 );
     421
     422        $count = bbp_get_topic_reply_count_hidden( $t );
     423        $this->assertSame( '2', $count );
     424
     425        bbp_decrease_topic_reply_count_hidden( $t );
     426
     427        $count = bbp_get_topic_reply_count_hidden( $t );
     428        $this->assertSame( '1', $count );
    359429    }
    360430
  • trunk/tests/phpunit/testcases/topics/functions/topic.php

    r5947 r6036  
    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 );
Note: See TracChangeset for help on using the changeset viewer.