Skip to:
Content

bbPress.org

Ticket #2894: 2894.diff

File 2894.diff, 11.1 KB (added by netweb, 9 years ago)
  • src/includes/common/functions.php

     
    16471647}
    16481648
    16491649/**
    1650  * Query the DB and get a the child id's of all children
     1650 * Query the DB and get the child id's of all children
    16511651 *
    16521652 * @since 2.0.0 bbPress (r3325)
     1653 * @since 2.6.0 bbPress (rXXXX) Replace direct queries with WP_Query() objects
    16531654 *
    1654  * @param int $  parent_id  Parent id
    1655  * @param string $post_type Post type. Defaults to 'post'
    1656  * @uses wp_cache_get() To check if there is a cache of the children
     1655 * @param int    $parent_id  Parent id.
     1656 * @param string $post_type Post type. Defaults to 'post'.
    16571657 * @uses bbp_get_public_status_id() To get the public status id
    16581658 * @uses bbp_get_private_status_id() To get the private status id
    16591659 * @uses bbp_get_hidden_status_id() To get the hidden status id
     
    16641664 * @uses bbp_get_forum_post_type() To get the forum post type
    16651665 * @uses bbp_get_topic_post_type() To get the topic post type
    16661666 * @uses bbp_get_reply_post_type() To get the reply post type
    1667  * @uses wpdb::prepare() To prepare the query
    1668  * @uses wpdb::get_col() To get the result of the query in an array
    1669  * @uses wp_cache_set() To set the cache for future use
     1667 * @uses WP_Query To get get the posts
    16701668 * @uses apply_filters() Calls 'bbp_get_all_child_ids' with the child ids,
    16711669 *                        parent id and post type
    16721670 * @return array The array of children
     
    16731671 */
    16741672function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) {
    16751673
    1676         // Bail if nothing passed
     1674        // Bail if nothing passed.
    16771675        if ( empty( $parent_id ) ) {
    16781676                return false;
    16791677        }
    16801678
    1681         // The ID of the cached query
    1682         $cache_id  = 'bbp_parent_all_' . $parent_id . '_type_' . $post_type . '_child_ids';
     1679        // Get the public post status.
     1680        $post_status = array( bbp_get_public_status_id() );
    16831681
    1684         // Check for cache and set if needed
    1685         $child_ids = wp_cache_get( $cache_id, 'bbpress_posts' );
    1686         if ( false === $child_ids ) {
    1687                 $post_status = array( bbp_get_public_status_id() );
     1682        // Extra post statuses based on post type.
     1683        switch ( $post_type ) {
    16881684
    1689                 // Extra post statuses based on post type
    1690                 switch ( $post_type ) {
     1685                // Forum
     1686                case bbp_get_forum_post_type() :
     1687                        $post_status[] = bbp_get_private_status_id();
     1688                        $post_status[] = bbp_get_hidden_status_id();
     1689                        break;
    16911690
    1692                         // Forum
    1693                         case bbp_get_forum_post_type() :
    1694                                 $post_status[] = bbp_get_private_status_id();
    1695                                 $post_status[] = bbp_get_hidden_status_id();
    1696                                 break;
     1691                // Topic
     1692                case bbp_get_topic_post_type() :
     1693                        $post_status[] = bbp_get_pending_status_id();
     1694                        $post_status[] = bbp_get_closed_status_id();
     1695                        $post_status[] = bbp_get_trash_status_id();
     1696                        $post_status[] = bbp_get_spam_status_id();
     1697                        break;
    16971698
    1698                         // Topic
    1699                         case bbp_get_topic_post_type() :
    1700                                 $post_status[] = bbp_get_pending_status_id();
    1701                                 $post_status[] = bbp_get_closed_status_id();
    1702                                 $post_status[] = bbp_get_trash_status_id();
    1703                                 $post_status[] = bbp_get_spam_status_id();
    1704                                 break;
     1699                // Reply
     1700                case bbp_get_reply_post_type() :
     1701                        $post_status[] = bbp_get_pending_status_id();
     1702                        $post_status[] = bbp_get_trash_status_id();
     1703                        $post_status[] = bbp_get_spam_status_id();
     1704                        break;
     1705        }
    17051706
    1706                         // Reply
    1707                         case bbp_get_reply_post_type() :
    1708                                 $post_status[] = bbp_get_pending_status_id();
    1709                                 $post_status[] = bbp_get_trash_status_id();
    1710                                 $post_status[] = bbp_get_spam_status_id();
    1711                                 break;
    1712                 }
     1707        $query = new WP_Query( array(
     1708                'fields'      => 'ids',
     1709                'post_parent' => $parent_id,
     1710                'post_status' => 'any',
     1711                'post_type'   => $post_type,
    17131712
    1714                 // Join post statuses together
    1715                 $post_status = "'" . implode( "', '", $post_status ) . "'";
    1716                 $bbp_db      = bbp_db();
    1717                 $query       = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type );
    1718                 $child_ids   = (array) $bbp_db->get_col( $query );
     1713                // Maybe change these later
     1714                'posts_per_page'         => -1,
     1715                'update_post_term_cache' => false,
     1716                'update_post_meta_cache' => false,
     1717                'ignore_sticky_posts'    => true,
     1718        ) );
     1719        $child_ids = ! empty( $query->posts ) ? $query->posts : array();
     1720        unset( $query );
    17191721
    1720                 wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' );
    1721         } else {
    1722                 $child_ids = (array) $child_ids;
    1723         }
    1724 
    17251722        // Filter and return
    17261723        return (array) apply_filters( 'bbp_get_all_child_ids', $child_ids, $parent_id, $post_type );
    17271724}
  • src/includes/core/cache.php

     
    161161
    162162        do_action( 'bbp_clean_post_cache', $_post->ID, $_post );
    163163
    164         // Loop through query types and clean caches
    165         foreach ( $post_types as $post_type ) {
    166                 wp_cache_delete( 'bbp_parent_all_'    . $_post->ID . '_type_' . $post_type . '_child_ids',     'bbpress_posts' );
    167         }
    168 
    169164        // Invalidate parent caches
    170165        if ( ! empty( $_post->post_parent ) ) {
    171166                bbp_clean_post_cache( $_post->post_parent );
  • tests/phpunit/testcases/common/query.php

     
    251251        public function test_bbp_get_all_child_ids() {
    252252                $f = $this->factory->forum->create();
    253253
    254                 // Test initial forum public child counts
     254                // Test initial forum public child forum counts.
    255255                $count = count( bbp_get_all_child_ids( $f, bbp_get_forum_post_type() ) );
    256256                $this->assertSame( 0, $count );
    257257
     258                // Test initial forum public child topic counts.
    258259                $count = count( bbp_get_all_child_ids( $f, bbp_get_topic_post_type() ) );
    259260                $this->assertSame( 0, $count );
    260261
    261262                /* Sub-Forums *********************************************************/
    262263
    263                 $this->factory->forum->create_many( 3, array(
     264                // Create two child forums of the above parent forum.
     265                $this->factory->forum->create_many( 2, array(
    264266                        'post_parent' => $f,
    265267                ) );
    266268
     269                // Forums should now be 2.
     270                $count = count( bbp_get_all_child_ids( $f, bbp_get_forum_post_type() ) );
     271                $this->assertSame( 2, $count );
     272
     273                // Create a private forum.
    267274                $this->factory->forum->create( array(
    268275                        'post_parent' => $f,
    269276                        'post_status' => bbp_get_private_status_id(),
    270277                ) );
    271278
     279                // Forums should now be 3.
    272280                $count = count( bbp_get_all_child_ids( $f, bbp_get_forum_post_type() ) );
     281                $this->assertSame( 3, $count );
     282
     283                // Create a hidden forum.
     284                $this->factory->forum->create( array(
     285                        'post_parent' => $f,
     286                        'post_status' => bbp_get_hidden_status_id(),
     287                ) );
     288
     289                // Forums should now be 4.
     290                $count = count( bbp_get_all_child_ids( $f, bbp_get_forum_post_type() ) );
    273291                $this->assertSame( 4, $count );
    274292
     293                // Create two more child forums.
    275294                $this->factory->forum->create_many( 2, array(
    276295                        'post_parent' => $f,
    277296                ) );
     
    281300
    282301                /* Topics *************************************************************/
    283302
    284                 $t1 = $this->factory->topic->create_many( 3, array(
     303                // Create two topics of the above parent forum.
     304                $t = $this->factory->topic->create_many( 2, array(
    285305                        'post_parent' => $f,
    286306                        'topic_meta' => array(
    287307                                'forum_id' => $f,
     
    288308                        ),
    289309                ) );
    290310
     311                // Topics should now be 2.
     312                $count = count( bbp_get_all_child_ids( $f, bbp_get_topic_post_type() ) );
     313                $this->assertSame( 2, $count );
     314
     315                // Create a pending status topic.
    291316                $this->factory->topic->create( array(
    292317                        'post_parent' => $f,
    293                         'post_status' => bbp_get_spam_status_id(),
     318                        'post_status' => bbp_get_pending_status_id(),
    294319                        'topic_meta' => array(
    295320                                'forum_id' => $f,
    296321                        ),
    297322                ) );
    298323
     324                // Topics should now be 3.
    299325                $count = count( bbp_get_all_child_ids( $f, bbp_get_topic_post_type() ) );
    300                 $this->assertSame( 4, $count );
     326                $this->assertSame( 3, $count );
    301327
    302                 $this->factory->topic->create_many( 2, array(
     328                // Create a spam status topic.
     329                $this->factory->topic->create( array(
    303330                        'post_parent' => $f,
     331                        'post_status' => bbp_get_spam_status_id(),
    304332                        'topic_meta' => array(
    305333                                'forum_id' => $f,
    306334                        ),
    307335                ) );
    308336
     337                // Topics should now be 4.
    309338                $count = count( bbp_get_all_child_ids( $f, bbp_get_topic_post_type() ) );
    310                 $this->assertSame( 6, $count );
     339                $this->assertSame( 4, $count );
    311340
    312                 $this->factory->topic->create( array(
     341                // Create two more child topics.
     342                $this->factory->topic->create_many( 2, array(
    313343                        'post_parent' => $f,
    314                         'post_status' => bbp_get_pending_status_id(),
    315344                        'topic_meta' => array(
    316345                                'forum_id' => $f,
    317346                        ),
    318347                ) );
    319348
     349                // Topics should now be 6.
    320350                $count = count( bbp_get_all_child_ids( $f, bbp_get_topic_post_type() ) );
    321                 $this->assertSame( 7, $count );
     351                $this->assertSame( 6, $count );
    322352
    323353                /* Replies ************************************************************/
    324354
    325                 $this->factory->reply->create_many( 3, array(
    326                         'post_parent' => $t1[0],
     355                // Create two replies of the above parent topic.
     356                $this->factory->reply->create_many( 2, array(
     357                        'post_parent' => $t[0],
    327358                        'reply_meta' => array(
    328359                                'forum_id' => $f,
    329                                 'topic_id' => $t1[0],
     360                                'topic_id' => $t[0],
    330361                        ),
    331362                ) );
    332363
     364                // Replies should now be 2.
     365                $count = count( bbp_get_all_child_ids( $t[0], bbp_get_reply_post_type() ) );
     366                $this->assertSame( 2, $count );
     367
     368                // Create a pending status reply.
    333369                $this->factory->reply->create( array(
    334                         'post_parent' => $t1[0],
    335                         'post_status' => bbp_get_spam_status_id(),
     370                        'post_parent' => $t[0],
     371                        'post_status' => bbp_get_pending_status_id(),
    336372                        'reply_meta' => array(
    337373                                'forum_id' => $f,
    338                                 'topic_id' => $t1[0],
     374                                'topic_id' => $t[0],
    339375                        ),
    340376                ) );
    341377
    342                 $count = count( bbp_get_all_child_ids( $t1[0], bbp_get_reply_post_type() ) );
    343                 $this->assertSame( 4, $count );
     378                // Replies should now be 3.
     379                $count = count( bbp_get_all_child_ids( $t[0], bbp_get_reply_post_type() ) );
     380                $this->assertSame( 3, $count );
    344381
    345                 $this->factory->reply->create_many( 2, array(
    346                         'post_parent' => $t1[0],
     382                // Create a spam status reply.
     383                $this->factory->reply->create( array(
     384                        'post_parent' => $t[0],
     385                        'post_status' => bbp_get_spam_status_id(),
    347386                        'reply_meta' => array(
    348387                                'forum_id' => $f,
    349                                 'topic_id' => $t1[0],
     388                                'topic_id' => $t[0],
    350389                        ),
    351390                ) );
    352391
    353                 $count = count( bbp_get_all_child_ids( $t1[0], bbp_get_reply_post_type() ) );
    354                 $this->assertSame( 6, $count );
     392                // Replies should now be 4.
     393                $count = count( bbp_get_all_child_ids( $t[0], bbp_get_reply_post_type() ) );
     394                $this->assertSame( 4, $count );
    355395
    356                 $this->factory->reply->create( array(
    357                         'post_parent' => $t1[0],
    358                         'post_status' => bbp_get_pending_status_id(),
     396                // Create two more child replies.
     397                $this->factory->reply->create_many( 2, array(
     398                        'post_parent' => $t[0],
    359399                        'reply_meta' => array(
    360400                                'forum_id' => $f,
    361                                 'topic_id' => $t1[0],
     401                                'topic_id' => $t[0],
    362402                        ),
    363403                ) );
    364404
    365                 $count = count( bbp_get_all_child_ids( $t1[0], bbp_get_reply_post_type() ) );
    366                 $this->assertSame( 7, $count );
     405                // Replies should now be 6.
     406                $count = count( bbp_get_all_child_ids( $t[0], bbp_get_reply_post_type() ) );
     407                $this->assertSame( 6, $count );
    367408        }
    368409}