Skip to:
Content

bbPress.org

Ticket #1799: 1799.patch

File 1799.patch, 18.9 KB (added by johnjamesjacoby, 9 years ago)

Replace direct count queries with WP_Query() objects

  • includes/common/functions.php

     
    11841184 * @param int $parent_id Parent id
    11851185 * @param string $post_type Post type. Defaults to 'post'
    11861186 * @uses bbp_get_topic_post_type() To get the topic post type
    1187  * @uses wp_cache_get() To check if there is a cache of the last child id
    1188  * @uses wpdb::prepare() To prepare the query
    1189  * @uses wpdb::get_var() To get the result of the query in a variable
    1190  * @uses wp_cache_set() To set the cache for future use
     1187 * @uses WP_Query To get get the posts
    11911188 * @uses apply_filters() Calls 'bbp_get_public_child_last_id' with the child
    11921189 *                        id, parent id and post type
    11931190 * @return int The last active post_id
    11941191 */
    11951192function bbp_get_public_child_last_id( $parent_id = 0, $post_type = 'post' ) {
    1196         global $wpdb;
    11971193
    11981194        // Bail if nothing passed
    11991195        if ( empty( $parent_id ) )
    12001196                return false;
    12011197
    12021198        // The ID of the cached query
    1203         $cache_id    = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_last_id';
    12041199        $post_status = array( bbp_get_public_status_id() );
    12051200
    12061201        // Add closed status if topic post type
    12071202        if ( $post_type == bbp_get_topic_post_type() )
    12081203                $post_status[] = bbp_get_closed_status_id();
    12091204
    1210         // Join post statuses together
    1211         $post_status = "'" . join( "', '", $post_status ) . "'";
    1212 
    12131205        // Check for cache and set if needed
    1214         $child_id = wp_cache_get( $cache_id, 'bbpress' );
    1215         if ( empty( $child_id ) ) {
    1216                 $child_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", $parent_id, $post_type ) );
    1217                 wp_cache_set( $cache_id, $child_id, 'bbpress' );
    1218         }
     1206        $query = new WP_Query( array(
     1207                'fields'      => 'ids',
     1208                'post_parent' => $parent_id,
     1209                'post_status' => $post_status,
     1210                'post_type'   => $post_type,
     1211               
     1212                // Maybe change these later
     1213                'posts_per_page'         => 1,
     1214                'update_post_term_cache' => false,
     1215                'update_post_meta_cache' => false,
     1216                'ignore_sticky_posts'    => true,
     1217        ) );
     1218        $child_id = array_shift( $query->posts );
     1219        unset( $query );
    12191220
    12201221        // Filter and return
    12211222        return apply_filters( 'bbp_get_public_child_last_id', (int) $child_id, (int) $parent_id, $post_type );
     
    12271228 * @param int $parent_id Parent id
    12281229 * @param string $post_type Post type. Defaults to 'post'
    12291230 * @uses bbp_get_topic_post_type() To get the topic post type
    1230  * @uses wp_cache_get() To check if there is a cache of the children count
    1231  * @uses wpdb::prepare() To prepare the query
    1232  * @uses wpdb::get_var() To get the result of the query in a variable
    1233  * @uses wp_cache_set() To set the cache for future use
     1231 * @uses WP_Query To get get the posts
    12341232 * @uses apply_filters() Calls 'bbp_get_public_child_count' with the child
    12351233 *                        count, parent id and post type
    12361234 * @return int The number of children
    12371235 */
    12381236function bbp_get_public_child_count( $parent_id = 0, $post_type = 'post' ) {
    1239         global $wpdb;
    12401237
    12411238        // Bail if nothing passed
    12421239        if ( empty( $parent_id ) )
    12431240                return false;
    12441241
    12451242        // The ID of the cached query
    1246         $cache_id    = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_count';
    12471243        $post_status = array( bbp_get_public_status_id() );
    12481244
    12491245        // Add closed status if topic post type
    1250         if ( $post_type == bbp_get_topic_post_type() )
     1246        if ( bbp_get_topic_post_type() == $post_type )
    12511247                $post_status[] = bbp_get_closed_status_id();
    12521248
    1253         // Join post statuses together
    1254         $post_status = "'" . join( "', '", $post_status ) . "'";
    1255 
    12561249        // Check for cache and set if needed
    1257         $child_count = wp_cache_get( $cache_id, 'bbpress' );
    1258         if ( empty( $child_count ) ) {
    1259                 $child_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $parent_id, $post_type ) );
    1260                 wp_cache_set( $cache_id, $child_count, 'bbpress' );
    1261         }
     1250        $query = new WP_Query( array(
     1251                'fields'      => 'ids',
     1252                'post_parent' => $parent_id,
     1253                'post_status' => $post_status,
     1254                'post_type'   => $post_type,
     1255               
     1256                // Maybe change these later
     1257                'posts_per_page'         => -1,
     1258                'update_post_term_cache' => false,
     1259                'update_post_meta_cache' => false,
     1260                'ignore_sticky_posts'    => true,
     1261        ) );
     1262        $child_count = $query->post_count;
     1263        unset( $query );
    12621264
    12631265        // Filter and return
    12641266        return apply_filters( 'bbp_get_public_child_count', (int) $child_count, (int) $parent_id, $post_type );
     
    12701272 * @param int $parent_id Parent id
    12711273 * @param string $post_type Post type. Defaults to 'post'
    12721274 * @uses bbp_get_topic_post_type() To get the topic post type
    1273  * @uses wp_cache_get() To check if there is a cache of the children
    1274  * @uses wpdb::prepare() To prepare the query
    1275  * @uses wpdb::get_col() To get the result of the query in an array
    1276  * @uses wp_cache_set() To set the cache for future use
     1275 * @uses WP_Query To get get the posts
    12771276 * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids,
    12781277 *                        parent id and post type
    12791278 * @return array The array of children
    12801279 */
    12811280function bbp_get_public_child_ids( $parent_id = 0, $post_type = 'post' ) {
    1282         global $wpdb;
    12831281
    12841282        // Bail if nothing passed
    12851283        if ( empty( $parent_id ) )
    12861284                return false;
    12871285
    12881286        // The ID of the cached query
    1289         $cache_id    = 'bbp_parent_public_' . $parent_id . '_type_' . $post_type . '_child_ids';
    12901287        $post_status = array( bbp_get_public_status_id() );
    12911288
    12921289        // Add closed status if topic post type
    12931290        if ( $post_type == bbp_get_topic_post_type() )
    12941291                $post_status[] = bbp_get_closed_status_id();
    12951292
    1296         // Join post statuses together
    1297         $post_status = "'" . join( "', '", $post_status ) . "'";
    1298 
    12991293        // Check for cache and set if needed
    1300         $child_ids = wp_cache_get( $cache_id, 'bbpress' );
    1301         if ( empty( $child_ids ) ) {
    1302                 $child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) );
    1303                 wp_cache_set( $cache_id, $child_ids, 'bbpress' );
    1304         }
     1294        $query = new WP_Query( array(
     1295                'fields'      => 'ids',
     1296                'post_parent' => $parent_id,
     1297                'post_status' => $post_status,
     1298                'post_type'   => $post_type,
     1299               
     1300                // Maybe change these later
     1301                'posts_per_page'         => -1,
     1302                'update_post_term_cache' => false,
     1303                'update_post_meta_cache' => false,
     1304                'ignore_sticky_posts'    => true,
     1305        ) );
     1306        $child_ids = !empty( $query->posts ) ? $query->posts : array();
     1307        unset( $query );
    13051308
    13061309        // Filter and return
    13071310        return apply_filters( 'bbp_get_public_child_ids', $child_ids, (int) $parent_id, $post_type );
     
    13121315 * @param int $parent_id Parent id
    13131316 * @param string $post_type Post type. Defaults to 'post'
    13141317 * @uses bbp_get_topic_post_type() To get the topic post type
    1315  * @uses wp_cache_get() To check if there is a cache of the children
    1316  * @uses wpdb::prepare() To prepare the query
    1317  * @uses wpdb::get_col() To get the result of the query in an array
    1318  * @uses wp_cache_set() To set the cache for future use
     1318 * @uses WP_Query To get get the posts
    13191319 * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids,
    13201320 *                        parent id and post type
    13211321 * @return array The array of children
    13221322 */
    13231323function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) {
    1324         global $wpdb;
    13251324
    13261325        // Bail if nothing passed
    13271326        if ( empty( $parent_id ) )
    13281327                return false;
    13291328
    13301329        // The ID of the cached query
    1331         $cache_id    = 'bbp_parent_all_' . $parent_id . '_type_' . $post_type . '_child_ids';
    13321330        $post_status = array( bbp_get_public_status_id() );
    13331331
    13341332        // Extra post statuses based on post type
     
    13541352                        break;
    13551353        }
    13561354
    1357         // Join post statuses together
    1358         $post_status = "'" . join( "', '", $post_status ) . "'";
    1359 
    13601355        // Check for cache and set if needed
    1361         $child_ids = wp_cache_get( $cache_id, 'bbpress' );
    1362         if ( empty( $child_ids ) ) {
    1363                 $child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) );
    1364                 wp_cache_set( $cache_id, $child_ids, 'bbpress' );
    1365         }
     1356        $query = new WP_Query( array(
     1357                'fields'      => 'ids',
     1358                'post_parent' => $parent_id,
     1359                'post_status' => 'any',
     1360                'post_type'   => $post_type,
     1361               
     1362                // Maybe change these later
     1363                'posts_per_page'         => -1,
     1364                'update_post_term_cache' => false,
     1365                'update_post_meta_cache' => false,
     1366                'ignore_sticky_posts'    => true,
     1367        ) );
     1368        $child_ids = !empty( $query->posts ) ? $query->posts : array();
     1369        unset( $query );
    13661370
    13671371        // Filter and return
    13681372        return apply_filters( 'bbp_get_all_child_ids', $child_ids, (int) $parent_id, $post_type );
  • includes/core/cache.php

     
    145145
    146146        do_action( 'bbp_clean_post_cache', $_post->ID, $_post );
    147147
    148         // Child query types to clean
    149         $post_types = array(
    150                 bbp_get_topic_post_type(),
    151                 bbp_get_forum_post_type(),
    152                 bbp_get_reply_post_type()
    153         );
    154 
    155         // Loop through query types and clean caches
    156         foreach ( $post_types as $post_type ) {
    157                 wp_cache_delete( 'bbp_get_forum_'     . $_post->ID . '_reply_id',                              'bbpress' );
    158                 wp_cache_delete( 'bbp_parent_'        . $_post->ID . '_type_' . $post_type . '_child_last_id', 'bbpress' );
    159                 wp_cache_delete( 'bbp_parent_'        . $_post->ID . '_type_' . $post_type . '_child_count',   'bbpress' );
    160                 wp_cache_delete( 'bbp_parent_public_' . $_post->ID . '_type_' . $post_type . '_child_ids',     'bbpress' );
    161                 wp_cache_delete( 'bbp_parent_all_'    . $_post->ID . '_type_' . $post_type . '_child_ids',     'bbpress' );
    162         }
    163 
    164148        // Invalidate parent caches
    165149        if ( ! empty( $_post->post_parent ) ) {
    166150                bbp_clean_post_cache( $_post->post_parent );
  • includes/forums/functions.php

     
    13681368 * @return int Topic hidden topic count
    13691369 */
    13701370function bbp_update_forum_topic_count_hidden( $forum_id = 0, $topic_count = 0 ) {
    1371         global $wpdb;
    13721371
    13731372        // If topic_id was passed as $forum_id, then get its forum
    13741373        if ( bbp_is_topic( $forum_id ) ) {
     
    13841383        if ( !empty( $forum_id ) ) {
    13851384
    13861385                // Get topics of forum
    1387                 if ( empty( $topic_count ) )
    1388                         $topic_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( '" . join( '\',\'', array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ) ) . "') AND post_type = '%s';", $forum_id, bbp_get_topic_post_type() ) );
     1386                if ( empty( $topic_count ) ) {
     1387                        $query = new WP_Query( array(
     1388                                'fields'      => 'ids',
     1389                                'post_parent' => $forum_id,
     1390                                'post_status' => array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ),
     1391                                'post_type'   => bbp_get_topic_post_type(),
    13891392
     1393                                // Maybe change these later
     1394                                'posts_per_page'         => -1,
     1395                                'update_post_term_cache' => false,
     1396                                'update_post_meta_cache' => false,
     1397                                'ignore_sticky_posts'    => true,               
     1398                        ) );
     1399                        $topic_count = $query->post_count;
     1400                        unset( $query );
     1401                }
     1402
    13901403                // Update the count
    13911404                update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) $topic_count );
    13921405        }
     
    14161429 * @return int Forum reply count
    14171430 */
    14181431function bbp_update_forum_reply_count( $forum_id = 0 ) {
    1419         global $wpdb;
    1420 
    14211432        $forum_id = bbp_get_forum_id( $forum_id );
    14221433        $children_reply_count = 0;
    14231434
     
    14311442
    14321443        // Don't count replies if the forum is a category
    14331444        $topic_ids = bbp_forum_query_topic_ids( $forum_id );
    1434         if ( !empty( $topic_ids ) )
    1435                 $reply_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = '%s' AND post_type = '%s';", bbp_get_public_status_id(), bbp_get_reply_post_type() ) );
    1436         else
     1445        if ( !empty( $topic_ids ) ) {
     1446                $query = new WP_Query( array(
     1447                        'fields'          => 'ids',
     1448                        'post_parent__in' => $topic_ids,
     1449                        'post_status'     => bbp_get_public_status_id(),
     1450                        'post_type'       => bbp_get_reply_post_type(),
     1451
     1452                        // Maybe change these later
     1453                        'posts_per_page'         => -1,
     1454                        'update_post_term_cache' => false,
     1455                        'update_post_meta_cache' => false,
     1456                        'ignore_sticky_posts'    => true,               
     1457                ) );
     1458                $reply_count = ! empty( $query->posts ) ? count( $query->posts ) : 0;
     1459                unset( $query );
     1460        } else {
    14371461                $reply_count = 0;
     1462        }
    14381463
    14391464        // Calculate total replies in this forum
    14401465        $total_replies = (int) $reply_count + $children_reply_count;
     
    17491774 */
    17501775function bbp_forum_query_subforum_ids( $forum_id ) {
    17511776        $subforum_ids = bbp_get_public_child_ids( $forum_id, bbp_get_forum_post_type() );
    1752         //usort( $subforum_ids, '_bbp_forum_query_usort_subforum_ids' );
    17531777
    17541778        return apply_filters( 'bbp_get_forum_subforum_ids', $subforum_ids, $forum_id );
    17551779}
    17561780
    17571781/**
    1758  * Callback to sort forum ID's based on last active time
    1759  *
    1760  * @since bbPress (r3789)
    1761  * @param int $a First forum ID to compare
    1762  * @param int $b Second forum ID to compare
    1763  * @return Position change based on sort
    1764  */
    1765 function _bbp_forum_query_usort_subforum_ids( $a = 0, $b = 0 ) {
    1766         $ta = get_post_meta( $a, '_bbp_last_active_time', true );
    1767         $tb = get_post_meta( $b, '_bbp_last_active_time', true );
    1768         return ( $ta < $tb ) ? -1 : 1;
    1769 }
    1770 
    1771 /**
    17721782 * Returns the forum's last reply id
    17731783 *
    17741784 * @since bbPress (r2908)
    17751785 *
    17761786 * @param int $forum_id Forum id
    17771787 * @param int $topic_ids Optional. Topic ids
    1778  * @uses wp_cache_get() To check for cache and retrieve it
    17791788 * @uses bbp_forum_query_topic_ids() To get the forum's topic ids
    1780  * @uses wpdb::prepare() To prepare the query
    1781  * @uses wpdb::get_var() To execute the query and get the var back
    17821789 * @uses bbp_get_reply_post_type() To get the reply post type
    1783  * @uses wp_cache_set() To set the cache for future use
    17841790 * @uses apply_filters() Calls 'bbp_forum_query_last_reply_id' with the reply id
    17851791 *                        and forum id
    17861792 */
    1787 function bbp_forum_query_last_reply_id( $forum_id, $topic_ids = 0 ) {
    1788         global $wpdb;
     1793function bbp_forum_query_last_reply_id( $forum_id = 0, $topic_ids = 0 ) {
    17891794
    1790         $cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id';
    1791         $reply_id = (int) wp_cache_get( $cache_id, 'bbpress' );
     1795        // Validate forum
     1796        $forum_id = bbp_get_forum_id( $forum_id );
    17921797
    1793         if ( empty( $reply_id ) ) {
     1798        // Get topic ID's if none were passed
     1799        if ( empty( $topic_ids ) )
     1800                $topic_ids = bbp_forum_query_topic_ids( $forum_id );
    17941801
    1795                 if ( empty( $topic_ids ) ) {
    1796                         $topic_ids = bbp_forum_query_topic_ids( $forum_id );
    1797                 }
     1802        // Check for cache and set if needed
     1803        $query = new WP_Query( array(
     1804                'fields'          => 'ids',
     1805                'post_parent__in' => $topic_ids,
     1806                'post_status'     => bbp_get_public_status_id(),
     1807                'post_type'       => bbp_get_reply_post_type(),
     1808               
     1809                // Maybe change these later
     1810                'posts_per_page'         => 1,
     1811                'update_post_term_cache' => false,
     1812                'update_post_meta_cache' => false,
     1813                'ignore_sticky_posts'    => true,
     1814        ) );
     1815        $reply_id = array_shift( $query->posts );
     1816        unset( $query );
    17981817
    1799                 if ( !empty( $topic_ids ) ) {
    1800                         $reply_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = '%s' AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", bbp_get_public_status_id(), bbp_get_reply_post_type() ) );
    1801                         wp_cache_set( $cache_id, $reply_id, 'bbpress' ); // May be (int) 0
    1802                 } else {
    1803                         wp_cache_set( $cache_id, '0', 'bbpress' );
    1804                 }
    1805         }
    1806 
    18071818        return (int) apply_filters( 'bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id );
    18081819}
    18091820
  • includes/replies/template-tags.php

     
    8585                'post_status'    => $default_post_status,       // Of this status
    8686                'posts_per_page' => bbp_get_replies_per_page(), // This many
    8787                'paged'          => bbp_get_paged(),            // On this page
    88                 'orderby'        => 'date',                     // Sorted by date
     88                'orderby'        => 'ID',                       // Sorted by ID
    8989                'order'          => 'ASC',                      // Oldest to newest
    9090                's'              => $default_reply_search,      // Maybe search
    9191        );
  • includes/topics/functions.php

     
    22412241function bbp_update_topic_forum_id( $topic_id = 0, $forum_id = 0 ) {
    22422242
    22432243        // If it's a reply, then get the parent (topic id)
    2244         if ( bbp_is_reply( $topic_id ) )
     2244        if ( bbp_is_reply( $topic_id ) ) {
    22452245                $topic_id = bbp_get_reply_topic_id( $topic_id );
    2246         else
     2246        } else {
    22472247                $topic_id = bbp_get_topic_id( $topic_id );
     2248        }
    22482249
    22492250        if ( empty( $forum_id ) )
    22502251                $forum_id = get_post_field( 'post_parent', $topic_id );
     
    22932294function bbp_update_topic_reply_count( $topic_id = 0, $reply_count = 0 ) {
    22942295
    22952296        // If it's a reply, then get the parent (topic id)
    2296         if ( bbp_is_reply( $topic_id ) )
     2297        if ( bbp_is_reply( $topic_id ) ) {
    22972298                $topic_id = bbp_get_reply_topic_id( $topic_id );
    2298         else
     2299        } else {
    22992300                $topic_id = bbp_get_topic_id( $topic_id );
     2301        }
    23002302
    23012303        // Get replies of topic if not passed
    23022304        if ( empty( $reply_count ) )
     
    23292331        global $wpdb;
    23302332
    23312333        // If it's a reply, then get the parent (topic id)
    2332         if ( bbp_is_reply( $topic_id ) )
     2334        if ( bbp_is_reply( $topic_id ) ) {
    23332335                $topic_id = bbp_get_reply_topic_id( $topic_id );
    2334         else
     2336        } else {
    23352337                $topic_id = bbp_get_topic_id( $topic_id );
     2338        }
    23362339
    23372340        // Get replies of topic
    23382341        if ( empty( $reply_count ) )
     
    23642367function bbp_update_topic_last_active_id( $topic_id = 0, $active_id = 0 ) {
    23652368
    23662369        // If it's a reply, then get the parent (topic id)
    2367         if ( bbp_is_reply( $topic_id ) )
     2370        if ( bbp_is_reply( $topic_id ) ) {
    23682371                $topic_id = bbp_get_reply_topic_id( $topic_id );
    2369         else
     2372        } else {
    23702373                $topic_id = bbp_get_topic_id( $topic_id );
     2374        }
    23712375
    23722376        if ( empty( $active_id ) )
    23732377                $active_id = bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() );
     
    23992403function bbp_update_topic_last_active_time( $topic_id = 0, $new_time = '' ) {
    24002404
    24012405        // If it's a reply, then get the parent (topic id)
    2402         if ( bbp_is_reply( $topic_id ) )
     2406        if ( bbp_is_reply( $topic_id ) ) {
    24032407                $topic_id = bbp_get_reply_topic_id( $topic_id );
    2404         else
     2408        } else {
    24052409                $topic_id = bbp_get_topic_id( $topic_id );
     2410        }
    24062411
    24072412        // Check time and use current if empty
    2408         if ( empty( $new_time ) )
     2413        if ( empty( $new_time ) ) {
    24092414                $new_time = get_post_field( 'post_date', bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() ) );
     2415        }
    24102416
    24112417        // Update only if published
    2412         if ( !empty( $new_time ) )
     2418        if ( !empty( $new_time ) ) {
    24132419                update_post_meta( $topic_id, '_bbp_last_active_time', $new_time );
     2420        }
    24142421
    24152422        return apply_filters( 'bbp_update_topic_last_active_time', $new_time, $topic_id );
    24162423}
  • includes/users/functions.php

     
    10371037 * Get the total number of users on the forums
    10381038 *
    10391039 * @since bbPress (r2769)
    1040  * @uses wp_cache_get() Check if query is in cache
    1041  * @uses get_users() To execute our query and get the var back
    1042  * @uses wp_cache_set() Set the query in the cache
     1040 * @uses count_users() To execute our query and get the var back
    10431041 * @uses apply_filters() Calls 'bbp_get_total_users' with number of users
    10441042 * @return int Total number of users
    10451043 */