Skip to:
Content

Changeset 6848


Ignore:
Timestamp:
08/09/2018 07:53:28 PM (7 months ago)
Author:
johnjamesjacoby
Message:

Tests: get all post type/status counts using bbp_get_child_counts().

  • Fix a broken test, and improve a few others
  • Update several count() instances that were doing expensive _ids() counts
  • Strategically bust the bbpress_posts query cache group on updates

This change improves general performance and count reliability by ensuring the accuracy of forum/topic/reply relationship totals.

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/common/functions.php

    r6838 r6848  
    17291729
    17301730    // Bail if nothing passed
    1731     if ( empty( $parent_id ) || empty( $post_type ) ) {
     1731    if ( empty( $post_type ) ) {
    17321732        return false;
    17331733    }
  • trunk/src/includes/forums/functions.php

    r6814 r6848  
    7575    // Update the forum and hierarchy
    7676    bbp_update_forum( array(
    77         'forum_id' => $forum_id
     77        'forum_id'    => $forum_id,
     78        'post_parent' => $forum_data['post_parent']
    7879    ) );
    7980
     
    99100     */
    100101    do_action( 'bbp_insert_forum', (int) $forum_id );
     102
     103    // Bump the last changed cache
     104    wp_cache_set( 'last_changed', microtime(), 'bbpress_posts' );
    101105
    102106    // Return forum_id
     
    13671371
    13681372    // Get the topic's replies.
    1369     $replies = bbp_get_public_child_ids( $topic_id, bbp_get_reply_post_type() );
    1370     $count   = count( $replies );
     1373    $count = bbp_get_public_child_count( $topic_id, bbp_get_reply_post_type() );
    13711374
    13721375    // If we're unapproving, set count to negative.
     
    15931596    $forum_id = bbp_get_forum_id( $forum_id );
    15941597
     1598    // Maybe query for counts
    15951599    if ( empty( $subforums ) ) {
    1596         $subforums = count( bbp_forum_query_subforum_ids( $forum_id ) );
     1600        $subforums = bbp_get_public_child_count( $forum_id, bbp_get_forum_post_type() );
    15971601    }
    15981602
     
    16291633
    16301634    // Get total topics for this forum
    1631     $topics = (int) count( bbp_forum_query_topic_ids( $forum_id ) );
     1635    $topics = bbp_get_public_child_count( $forum_id, bbp_get_topic_post_type() );
    16321636
    16331637    // Calculate total topics in this forum
     
    17291733    $topic_ids   = bbp_forum_query_topic_ids( $forum_id );
    17301734    if ( ! empty( $topic_ids ) ) {
    1731         $query = new WP_Query( array(
    1732             'fields'          => 'ids',
    1733             'post_parent__in' => $topic_ids,
    1734             'post_status'     => bbp_get_public_status_id(),
    1735             'post_type'       => bbp_get_reply_post_type(),
    1736             'posts_per_page'  => -1,
    1737 
    1738             // Performance
    1739             'nopaging'               => true,
    1740             'suppress_filters'       => true,
    1741             'update_post_term_cache' => false,
    1742             'update_post_meta_cache' => false,
    1743             'ignore_sticky_posts'    => true,
    1744             'no_found_rows'          => true
    1745         ) );
    1746         $reply_count = ! empty( $query->posts ) ? count( $query->posts ) : 0;
    1747         unset( $query );
     1735        $reply_count = bbp_get_public_child_count( $forum_id, bbp_get_reply_post_type() );
    17481736    }
    17491737
     
    17881776    ), 'update_forum' );
    17891777
     1778    // Update the forum parent
     1779    bbp_update_forum_id( $r['forum_id'], $r['post_parent'] );
     1780
    17901781    // Last topic and reply ID's
    17911782    bbp_update_forum_last_topic_id( $r['forum_id'], $r['last_topic_id'] );
     
    18211812        ) );
    18221813    }
     1814
     1815    // Bump the custom query cache
     1816    wp_cache_set( 'last_changed', microtime(), 'bbpress_posts' );
    18231817}
    18241818
  • trunk/src/includes/replies/functions.php

    r6826 r6848  
    9999    if ( bbp_is_reply_published( $reply_id ) ) {
    100100        bbp_increase_topic_reply_count( $topic_id );
    101         bbp_increase_forum_reply_count( $reply_id );
     101        bbp_increase_forum_reply_count( $forum_id );
    102102
    103103    // If the reply isn't public only update the topic reply hidden count.
     
    856856        bbp_update_reply_walker( $reply_id, $last_active_time, $forum_id, $topic_id, false );
    857857    }
     858
     859    // Bump the custom query cache
     860    wp_cache_set( 'last_changed', microtime(), 'bbpress_posts' );
    858861}
    859862
     
    10021005
    10031006    // Update the forum ID
    1004     $forum_id = bbp_update_forum_id( $reply_id, $forum_id );
     1007    $retval = bbp_update_forum_id( $reply_id, $forum_id );
    10051008
    10061009    // Filter & return
    1007     return (int) apply_filters( 'bbp_update_reply_forum_id', $forum_id, $reply_id );
     1010    return (int) apply_filters( 'bbp_update_reply_forum_id', $retval, $reply_id, $forum_id );
    10081011}
    10091012
     
    10451048
    10461049    // Update the topic ID
    1047     $topic_id = bbp_update_topic_id( $reply_id, $topic_id );
     1050    $retval = bbp_update_topic_id( $reply_id, $topic_id );
    10481051
    10491052    // Filter & return
    1050     return (int) apply_filters( 'bbp_update_reply_topic_id', $topic_id, $reply_id );
     1053    return (int) apply_filters( 'bbp_update_reply_topic_id', $retval, $reply_id, $topic_id );
    10511054}
    10521055
  • trunk/src/includes/topics/functions.php

    r6827 r6848  
    833833        bbp_update_topic_walker( $topic_id, $last_active, $forum_id, 0, false );
    834834    }
     835
     836    // Bump the custom query cache
     837    wp_cache_set( 'last_changed', microtime(), 'bbpress_posts' );
    835838}
    836839
     
    888891            if ( bbp_is_forum( $ancestor ) ) {
    889892
     893                // Get the forum
     894                $forum = bbp_get_forum( $ancestor );
     895
    890896                // Update the forum
    891897                bbp_update_forum( array(
    892                     'forum_id'           => $ancestor,
     898                    'forum_id'           => $forum->ID,
     899                    'post_parent'        => $forum->post_parent,
    893900                    'last_topic_id'      => $topic_id,
    894901                    'last_reply_id'      => $reply_id,
     
    984991
    985992    // Get reply count.
    986     $public_reply_count = count( bbp_get_public_child_ids( $topic_id, bbp_get_reply_post_type() ) );
     993    $public_reply_count = bbp_get_public_child_count( $topic_id, bbp_get_reply_post_type() );
    987994
    988995    // Topic status.
  • trunk/tests/phpunit/testcases/admin/tools.php

    r6585 r6848  
    642642        ) );
    643643
    644         // Forums should NOT have a _bbp_forum_id meta key
    645         $this->assertEquals( array(), get_post_meta( $f, '_bbp_forum_id', false ) );
     644        // Forums should have an empty _bbp_forum_id meta key
     645        $this->assertEquals( array( 0 => '0' ), get_post_meta( $f, '_bbp_forum_id', false ) );
    646646
    647647        // Topics should have a _bbp_forum_id meta key
  • trunk/tests/phpunit/testcases/common/query.php

    r5954 r6848  
    5050
    5151    /**
    52      * @group  counts
     52     * @group  jjj
    5353     * @covers ::bbp_get_public_child_count
    5454     */
    5555    public function test_bbp_get_public_child_count() {
     56
     57        /* Empty Forum ********************************************************/
     58
    5659        $f = $this->factory->forum->create();
    5760
    58         // Test initial forum public child counts
     61        // Test initial zero forum public child counts
    5962        $count = bbp_get_public_child_count( $f, bbp_get_forum_post_type() );
    6063        $this->assertSame( 0, $count );
     
    6568        /* Sub-Forums *********************************************************/
    6669
     70        // 3 public sub-forums
    6771        $this->factory->forum->create_many( 3, array(
    6872            'post_parent' => $f,
    6973        ) );
    7074
     75        // 1 private sub-forum
    7176        $this->factory->forum->create( array(
    7277            'post_parent' => $f,
  • trunk/tests/phpunit/testcases/forums/functions/counts.php

    r6036 r6848  
    415415        $f1 = $this->factory->forum->create();
    416416
     417        $count = bbp_get_forum_subforum_count( $f1, true );
     418        $this->assertSame( 0, $count );
     419
    417420        $f2 = $this->factory->forum->create_many( 3, array(
    418421            'post_parent' => $f1,
     
    420423
    421424        $count = bbp_get_forum_subforum_count( $f1, true );
    422         $this->assertSame( 0, $count );
     425        $this->assertSame( 3, $count );
     426
     427        bbp_update_forum_subforum_count( $f1, 10 );
     428
     429        $count = bbp_get_forum_subforum_count( $f1, true );
     430        $this->assertSame( 10, $count );
    423431
    424432        bbp_update_forum_subforum_count( $f1 );
Note: See TracChangeset for help on using the changeset viewer.