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