Skip to:
Content

bbPress.org

Ticket #1799: 1799.2.patch

File 1799.2.patch, 15.4 KB (added by johnjamesjacoby, 9 years ago)

Refresh

  • includes/common/functions.php

     
    12021202 * @param int $parent_id Parent id
    12031203 * @param string $post_type Post type. Defaults to 'post'
    12041204 * @uses bbp_get_topic_post_type() To get the topic post type
    1205  * @uses wp_cache_get() To check if there is a cache of the last child id
    1206  * @uses wpdb::prepare() To prepare the query
    1207  * @uses wpdb::get_var() To get the result of the query in a variable
    1208  * @uses wp_cache_set() To set the cache for future use
     1205 * @uses WP_Query To get get the posts
    12091206 * @uses apply_filters() Calls 'bbp_get_public_child_last_id' with the child
    12101207 *                        id, parent id and post type
    12111208 * @return int The last active post_id
    12121209 */
    12131210function bbp_get_public_child_last_id( $parent_id = 0, $post_type = 'post' ) {
    1214         global $wpdb;
    12151211
    12161212        // Bail if nothing passed
    12171213        if ( empty( $parent_id ) )
    12181214                return false;
    12191215
    12201216        // The ID of the cached query
    1221         $cache_id    = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_last_id';
    12221217        $post_status = array( bbp_get_public_status_id() );
    12231218
    12241219        // Add closed status if topic post type
    12251220        if ( $post_type == bbp_get_topic_post_type() )
    12261221                $post_status[] = bbp_get_closed_status_id();
    12271222
    1228         // Join post statuses together
    1229         $post_status = "'" . join( "', '", $post_status ) . "'";
    1230 
    12311223        // Check for cache and set if needed
    1232         $child_id = wp_cache_get( $cache_id, 'bbpress' );
    1233         if ( empty( $child_id ) ) {
    1234                 $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 ) );
    1235                 wp_cache_set( $cache_id, $child_id, 'bbpress' );
    1236         }
     1224        $query = new WP_Query( array(
     1225                'fields'      => 'ids',
     1226                'post_parent' => $parent_id,
     1227                'post_status' => $post_status,
     1228                'post_type'   => $post_type,
     1229               
     1230                // Maybe change these later
     1231                'posts_per_page'         => 1,
     1232                'update_post_term_cache' => false,
     1233                'update_post_meta_cache' => false,
     1234                'ignore_sticky_posts'    => true,
     1235        ) );
     1236        $child_id = array_shift( $query->posts );
     1237        unset( $query );
    12371238
    12381239        // Filter and return
    12391240        return apply_filters( 'bbp_get_public_child_last_id', (int) $child_id, (int) $parent_id, $post_type );
     
    12451246 * @param int $parent_id Parent id
    12461247 * @param string $post_type Post type. Defaults to 'post'
    12471248 * @uses bbp_get_topic_post_type() To get the topic post type
    1248  * @uses wp_cache_get() To check if there is a cache of the children count
    1249  * @uses wpdb::prepare() To prepare the query
    1250  * @uses wpdb::get_var() To get the result of the query in a variable
    1251  * @uses wp_cache_set() To set the cache for future use
     1249 * @uses WP_Query To get get the posts
    12521250 * @uses apply_filters() Calls 'bbp_get_public_child_count' with the child
    12531251 *                        count, parent id and post type
    12541252 * @return int The number of children
    12551253 */
    12561254function bbp_get_public_child_count( $parent_id = 0, $post_type = 'post' ) {
    1257         global $wpdb;
    12581255
    12591256        // Bail if nothing passed
    12601257        if ( empty( $parent_id ) )
    12611258                return false;
    12621259
    12631260        // The ID of the cached query
    1264         $cache_id    = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_count';
    12651261        $post_status = array( bbp_get_public_status_id() );
    12661262
    12671263        // Add closed status if topic post type
    1268         if ( $post_type == bbp_get_topic_post_type() )
     1264        if ( bbp_get_topic_post_type() == $post_type )
    12691265                $post_status[] = bbp_get_closed_status_id();
    12701266
    1271         // Join post statuses together
    1272         $post_status = "'" . join( "', '", $post_status ) . "'";
    1273 
    12741267        // Check for cache and set if needed
    1275         $child_count = wp_cache_get( $cache_id, 'bbpress' );
    1276         if ( empty( $child_count ) ) {
    1277                 $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 ) );
    1278                 wp_cache_set( $cache_id, $child_count, 'bbpress' );
    1279         }
     1268        $query = new WP_Query( array(
     1269                'fields'      => 'ids',
     1270                'post_parent' => $parent_id,
     1271                'post_status' => $post_status,
     1272                'post_type'   => $post_type,
     1273               
     1274                // Maybe change these later
     1275                'posts_per_page'         => -1,
     1276                'update_post_term_cache' => false,
     1277                'update_post_meta_cache' => false,
     1278                'ignore_sticky_posts'    => true,
     1279        ) );
     1280        $child_count = $query->post_count;
     1281        unset( $query );
    12801282
    12811283        // Filter and return
    12821284        return apply_filters( 'bbp_get_public_child_count', (int) $child_count, (int) $parent_id, $post_type );
     
    12881290 * @param int $parent_id Parent id
    12891291 * @param string $post_type Post type. Defaults to 'post'
    12901292 * @uses bbp_get_topic_post_type() To get the topic post type
    1291  * @uses wp_cache_get() To check if there is a cache of the children
    1292  * @uses wpdb::prepare() To prepare the query
    1293  * @uses wpdb::get_col() To get the result of the query in an array
    1294  * @uses wp_cache_set() To set the cache for future use
     1293 * @uses WP_Query To get get the posts
    12951294 * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids,
    12961295 *                        parent id and post type
    12971296 * @return array The array of children
    12981297 */
    12991298function bbp_get_public_child_ids( $parent_id = 0, $post_type = 'post' ) {
    1300         global $wpdb;
    13011299
    13021300        // Bail if nothing passed
    13031301        if ( empty( $parent_id ) )
    13041302                return false;
    13051303
    13061304        // The ID of the cached query
    1307         $cache_id    = 'bbp_parent_public_' . $parent_id . '_type_' . $post_type . '_child_ids';
    13081305        $post_status = array( bbp_get_public_status_id() );
    13091306
    13101307        // Add closed status if topic post type
    13111308        if ( $post_type == bbp_get_topic_post_type() )
    13121309                $post_status[] = bbp_get_closed_status_id();
    13131310
    1314         // Join post statuses together
    1315         $post_status = "'" . join( "', '", $post_status ) . "'";
    1316 
    13171311        // Check for cache and set if needed
    1318         $child_ids = wp_cache_get( $cache_id, 'bbpress' );
    1319         if ( empty( $child_ids ) ) {
    1320                 $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 ) );
    1321                 wp_cache_set( $cache_id, $child_ids, 'bbpress' );
    1322         }
     1312        $query = new WP_Query( array(
     1313                'fields'      => 'ids',
     1314                'post_parent' => $parent_id,
     1315                'post_status' => $post_status,
     1316                'post_type'   => $post_type,
     1317               
     1318                // Maybe change these later
     1319                'posts_per_page'         => -1,
     1320                'update_post_term_cache' => false,
     1321                'update_post_meta_cache' => false,
     1322                'ignore_sticky_posts'    => true,
     1323        ) );
     1324        $child_ids = !empty( $query->posts ) ? $query->posts : array();
     1325        unset( $query );
    13231326
    13241327        // Filter and return
    13251328        return apply_filters( 'bbp_get_public_child_ids', $child_ids, (int) $parent_id, $post_type );
    13261329}
     1330
    13271331/**
    13281332 * Query the DB and get a the child id's of all children
    13291333 *
    13301334 * @param int $parent_id Parent id
    13311335 * @param string $post_type Post type. Defaults to 'post'
    13321336 * @uses bbp_get_topic_post_type() To get the topic post type
    1333  * @uses wp_cache_get() To check if there is a cache of the children
    1334  * @uses wpdb::prepare() To prepare the query
    1335  * @uses wpdb::get_col() To get the result of the query in an array
    1336  * @uses wp_cache_set() To set the cache for future use
     1337 * @uses WP_Query To get get the posts
    13371338 * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids,
    13381339 *                        parent id and post type
    13391340 * @return array The array of children
    13401341 */
    13411342function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) {
    1342         global $wpdb;
    13431343
    13441344        // Bail if nothing passed
    13451345        if ( empty( $parent_id ) )
    13461346                return false;
    13471347
    13481348        // The ID of the cached query
    1349         $cache_id    = 'bbp_parent_all_' . $parent_id . '_type_' . $post_type . '_child_ids';
    13501349        $post_status = array( bbp_get_public_status_id() );
    13511350
    13521351        // Extra post statuses based on post type
     
    13721371                        break;
    13731372        }
    13741373
    1375         // Join post statuses together
    1376         $post_status = "'" . join( "', '", $post_status ) . "'";
    1377 
    13781374        // Check for cache and set if needed
    1379         $child_ids = wp_cache_get( $cache_id, 'bbpress' );
    1380         if ( empty( $child_ids ) ) {
    1381                 $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 ) );
    1382                 wp_cache_set( $cache_id, $child_ids, 'bbpress' );
    1383         }
     1375        $query = new WP_Query( array(
     1376                'fields'      => 'ids',
     1377                'post_parent' => $parent_id,
     1378                'post_status' => 'any',
     1379                'post_type'   => $post_type,
     1380               
     1381                // Maybe change these later
     1382                'posts_per_page'         => -1,
     1383                'update_post_term_cache' => false,
     1384                'update_post_meta_cache' => false,
     1385                'ignore_sticky_posts'    => true,
     1386        ) );
     1387        $child_ids = !empty( $query->posts ) ? $query->posts : array();
     1388        unset( $query );
    13841389
    13851390        // Filter and return
    13861391        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

     
    13431343 * @return int Topic hidden topic count
    13441344 */
    13451345function bbp_update_forum_topic_count_hidden( $forum_id = 0, $topic_count = 0 ) {
    1346         global $wpdb;
    13471346
    13481347        // If topic_id was passed as $forum_id, then get its forum
    13491348        if ( bbp_is_topic( $forum_id ) ) {
     
    13591358        if ( !empty( $forum_id ) ) {
    13601359
    13611360                // Get topics of forum
    1362                 if ( empty( $topic_count ) )
    1363                         $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() ) );
     1361                if ( empty( $topic_count ) ) {
     1362                        $query = new WP_Query( array(
     1363                                'fields'      => 'ids',
     1364                                'post_parent' => $forum_id,
     1365                                'post_status' => array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ),
     1366                                'post_type'   => bbp_get_topic_post_type(),
    13641367
     1368                                // Maybe change these later
     1369                                'posts_per_page'         => -1,
     1370                                'update_post_term_cache' => false,
     1371                                'update_post_meta_cache' => false,
     1372                                'ignore_sticky_posts'    => true,
     1373                        ) );
     1374                        $topic_count = $query->post_count;
     1375                        unset( $query );
     1376                }
     1377
    13651378                // Update the count
    13661379                update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) $topic_count );
    13671380        }
     
    13911404 * @return int Forum reply count
    13921405 */
    13931406function bbp_update_forum_reply_count( $forum_id = 0 ) {
    1394         global $wpdb;
    13951407
    13961408        $forum_id = bbp_get_forum_id( $forum_id );
    13971409        $children_reply_count = 0;
     
    14071419        // Don't count replies if the forum is a category
    14081420        $topic_ids = bbp_forum_query_topic_ids( $forum_id );
    14091421        if ( !empty( $topic_ids ) ) {
    1410                 $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() ) );
     1422                $query = new WP_Query( array(
     1423                        'fields'          => 'ids',
     1424                        'post_parent__in' => $topic_ids,
     1425                        'post_status'     => bbp_get_public_status_id(),
     1426                        'post_type'       => bbp_get_reply_post_type(),
     1427
     1428                        // Maybe change these later
     1429                        'posts_per_page'         => -1,
     1430                        'update_post_term_cache' => false,
     1431                        'update_post_meta_cache' => false,
     1432                        'ignore_sticky_posts'    => true,
     1433                ) );
     1434                $reply_count = ! empty( $query->posts ) ? count( $query->posts ) : 0;
     1435                unset( $query );
    14111436        } else {
    1412                 $reply_count = 0;
     1437                $reply_count = 0;
    14131438        }
    14141439
    14151440        // Calculate total replies in this forum
     
    17261751 */
    17271752function bbp_forum_query_subforum_ids( $forum_id ) {
    17281753        $subforum_ids = bbp_get_public_child_ids( $forum_id, bbp_get_forum_post_type() );
    1729         //usort( $subforum_ids, '_bbp_forum_query_usort_subforum_ids' );
    17301754
    17311755        return apply_filters( 'bbp_get_forum_subforum_ids', $subforum_ids, $forum_id );
    17321756}
    17331757
    17341758/**
    1735  * Callback to sort forum ID's based on last active time
    1736  *
    1737  * @since bbPress (r3789)
    1738  * @param int $a First forum ID to compare
    1739  * @param int $b Second forum ID to compare
    1740  * @return Position change based on sort
    1741  */
    1742 function _bbp_forum_query_usort_subforum_ids( $a = 0, $b = 0 ) {
    1743         $ta = get_post_meta( $a, '_bbp_last_active_time', true );
    1744         $tb = get_post_meta( $b, '_bbp_last_active_time', true );
    1745         return ( $ta < $tb ) ? -1 : 1;
    1746 }
    1747 
    1748 /**
    17491759 * Returns the forum's last reply id
    17501760 *
    17511761 * @since bbPress (r2908)
    17521762 *
    17531763 * @param int $forum_id Forum id
    17541764 * @param int $topic_ids Optional. Topic ids
    1755  * @uses wp_cache_get() To check for cache and retrieve it
    17561765 * @uses bbp_forum_query_topic_ids() To get the forum's topic ids
    1757  * @uses wpdb::prepare() To prepare the query
    1758  * @uses wpdb::get_var() To execute the query and get the var back
    17591766 * @uses bbp_get_reply_post_type() To get the reply post type
    1760  * @uses wp_cache_set() To set the cache for future use
    17611767 * @uses apply_filters() Calls 'bbp_forum_query_last_reply_id' with the reply id
    17621768 *                        and forum id
    17631769 */
    1764 function bbp_forum_query_last_reply_id( $forum_id, $topic_ids = 0 ) {
    1765         global $wpdb;
     1770function bbp_forum_query_last_reply_id( $forum_id = 0, $topic_ids = 0 ) {
    17661771
    1767         $cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id';
    1768         $reply_id = (int) wp_cache_get( $cache_id, 'bbpress' );
     1772        // Validate forum
     1773        $forum_id = bbp_get_forum_id( $forum_id );
    17691774
    1770         if ( empty( $reply_id ) ) {
     1775        // Get topic ID's if none were passed
     1776        if ( empty( $topic_ids ) )
     1777                $topic_ids = bbp_forum_query_topic_ids( $forum_id );
    17711778
    1772                 if ( empty( $topic_ids ) ) {
    1773                         $topic_ids = bbp_forum_query_topic_ids( $forum_id );
    1774                 }
     1779        // Check for cache and set if needed
     1780        $query = new WP_Query( array(
     1781                'fields'          => 'ids',
     1782                'post_parent__in' => $topic_ids,
     1783                'post_status'     => bbp_get_public_status_id(),
     1784                'post_type'       => bbp_get_reply_post_type(),
    17751785
    1776                 if ( !empty( $topic_ids ) ) {
    1777                         $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() ) );
    1778                         wp_cache_set( $cache_id, $reply_id, 'bbpress' ); // May be (int) 0
    1779                 } else {
    1780                         wp_cache_set( $cache_id, '0', 'bbpress' );
    1781                 }
    1782         }
     1786                // Maybe change these later
     1787                'posts_per_page'         => 1,
     1788                'update_post_term_cache' => false,
     1789                'update_post_meta_cache' => false,
     1790                'ignore_sticky_posts'    => true,
     1791        ) );
     1792        $reply_id = array_shift( $query->posts );
     1793        unset( $query );
    17831794
    1784         return (int) apply_filters( 'bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id );
    1785 }
     1795        return (int) apply_filters( 'bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id );
     1796 }
    17861797
    17871798/** Listeners *****************************************************************/
    17881799