Skip to:
Content

bbPress.org


Ignore:
Timestamp:
05/30/2016 10:28:36 AM (7 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.