Skip to:
Content

bbPress.org

Changeset 5954


Ignore:
Timestamp:
11/18/2015 04:39:54 AM (9 years ago)
Author:
netweb
Message:

Performance: Replace direct SQL queries with WP_Query() objects in the following functions:

  • bbp_update_forum_topic_count()
  • bbp_update_forum_topic_count_hidden()
  • bbp_update_forum_reply_count()
  • bbp_get_public_child_last_id()
  • bbp_get_public_child_count()
  • bbp_get_public_child_ids()

Props johnjamesjacoby.
See #1799.

Location:
trunk
Files:
4 edited

Legend:

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

    r5951 r5954  
    15001500 * Query the DB and get the last public post_id that has parent_id as post_parent
    15011501 *
    1502  * @param int $parent_id Parent id
    1503  * @param string $post_type Post type. Defaults to 'post'
     1502 * @since 2.0.0 bbPress (r2868)
     1503 * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects
     1504 *
     1505 * @param int    $parent_id Parent id.
     1506 * @param string $post_type Post type. Defaults to 'post'.
     1507 * @uses bbp_get_public_status_id() To get the public status id
    15041508 * @uses bbp_get_topic_post_type() To get the topic post type
    1505  * @uses wp_cache_get() To check if there is a cache of the last child id
    1506  * @uses wpdb::prepare() To prepare the query
    1507  * @uses wpdb::get_var() To get the result of the query in a variable
    1508  * @uses wp_cache_set() To set the cache for future use
     1509 * @uses bbp_get_closed_status_id() To get the closed status id
     1510 * @uses WP_Query To get get the posts
    15091511 * @uses apply_filters() Calls 'bbp_get_public_child_last_id' with the child
    15101512 *                        id, parent id and post type
     
    15181520    }
    15191521
    1520     // The ID of the cached query
    1521     $cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_last_id';
    1522 
    1523     // Check for cache and set if needed
    1524     $child_id = wp_cache_get( $cache_id, 'bbpress_posts' );
    1525     if ( false === $child_id ) {
    1526         $post_status = array( bbp_get_public_status_id() );
    1527 
    1528         // Add closed status if topic post type
    1529         if ( $post_type === bbp_get_topic_post_type() ) {
    1530             $post_status[] = bbp_get_closed_status_id();
    1531         }
    1532 
    1533         // Join post statuses together
    1534         $post_status = "'" . implode( "', '", $post_status ) . "'";
    1535         $bbp_db      = bbp_db();
    1536         $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 LIMIT 1;", $parent_id, $post_type );
    1537         $child_id    = (int) $bbp_db->get_var( $query );
    1538 
    1539         wp_cache_set( $cache_id, $child_id, 'bbpress_posts' );
    1540     } else {
    1541         $child_id = (int) $child_id;
    1542     }
     1522    // Get the public posts status
     1523    $post_status = array( bbp_get_public_status_id() );
     1524
     1525    // Add closed status if topic post type
     1526    if ( bbp_get_topic_post_type() === $post_type ) {
     1527        $post_status[] = bbp_get_closed_status_id();
     1528    }
     1529
     1530    $query = new WP_Query( array(
     1531        'fields'      => 'ids',
     1532        'post_parent' => $parent_id,
     1533        'post_status' => $post_status,
     1534        'post_type'   => $post_type,
     1535
     1536        // Maybe change these later
     1537        'posts_per_page'         => 1,
     1538        'update_post_term_cache' => false,
     1539        'update_post_meta_cache' => false,
     1540        'ignore_sticky_posts'    => true,
     1541    ) );
     1542    $child_id = array_shift( $query->posts );
     1543    unset( $query );
    15431544
    15441545    // Filter and return
     
    15501551 *
    15511552 * @since 2.0.0 bbPress (r2868)
    1552  *
    1553  * @param int $parent_id Parent id
    1554  * @param string $post_type Post type. Defaults to 'post'
     1553 * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects
     1554 *
     1555 * @param int    $parent_id Parent id.
     1556 * @param string $post_type Post type. Defaults to 'post'.
     1557 * @uses bbp_get_public_status_id() To get the public status id
    15551558 * @uses bbp_get_topic_post_type() To get the topic post type
    1556  * @uses wp_cache_get() To check if there is a cache of the children count
    1557  * @uses wpdb::prepare() To prepare the query
    1558  * @uses wpdb::get_var() To get the result of the query in a variable
    1559  * @uses wp_cache_set() To set the cache for future use
     1559 * @uses bbp_get_closed_status_id() To get the closed status id
     1560 * @uses WP_Query To get get the posts
    15601561 * @uses apply_filters() Calls 'bbp_get_public_child_count' with the child
    15611562 *                        count, parent id and post type
     
    15691570    }
    15701571
    1571     // The ID of the cached query
    1572     $cache_id    = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_count';
    1573 
    1574     // Check for cache and set if needed
    1575     $child_count = wp_cache_get( $cache_id, 'bbpress_posts' );
    1576     if ( false === $child_count ) {
    1577         $post_status = array( bbp_get_public_status_id() );
    1578 
    1579         // Add closed status if topic post type
    1580         if ( $post_type === bbp_get_topic_post_type() ) {
    1581             $post_status[] = bbp_get_closed_status_id();
    1582         }
    1583 
    1584         // Join post statuses together
    1585         $post_status = "'" . implode( "', '", $post_status ) . "'";
    1586         $bbp_db      = bbp_db();
    1587         $query       = $bbp_db->prepare( "SELECT COUNT(ID) FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $parent_id, $post_type );
    1588         $child_count = (int) $bbp_db->get_var( $query );
    1589 
    1590         wp_cache_set( $cache_id, $child_count, 'bbpress_posts' );
    1591     } else {
    1592         $child_count = (int) $child_count;
    1593     }
     1572    // Check the public post status
     1573    $post_status = array( bbp_get_public_status_id() );
     1574
     1575    // Add closed status if topic post type
     1576    if ( bbp_get_topic_post_type() === $post_type ) {
     1577        $post_status[] = bbp_get_closed_status_id();
     1578    }
     1579
     1580    $query = new WP_Query( array(
     1581        'fields'      => 'ids',
     1582        'post_parent' => $parent_id,
     1583        'post_status' => $post_status,
     1584        'post_type'   => $post_type,
     1585
     1586        // Maybe change these later
     1587        'posts_per_page'         => -1,
     1588        'update_post_term_cache' => false,
     1589        'update_post_meta_cache' => false,
     1590        'ignore_sticky_posts'    => true,
     1591    ) );
     1592    $child_count = $query->post_count;
     1593    unset( $query );
    15941594
    15951595    // Filter and return
     
    15981598
    15991599/**
    1600  * Query the DB and get a the child id's of public children
     1600 * Query the DB and get the child id's of public children
    16011601 *
    16021602 * @since 2.0.0 bbPress (r2868)
    1603  *
    1604  * @param int $parent_id Parent id
    1605  * @param string $post_type Post type. Defaults to 'post'
     1603 * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects
     1604 *
     1605 * @param int    $parent_id Parent id.
     1606 * @param string $post_type Post type. Defaults to 'post'.
     1607 * @uses bbp_get_public_status_id() To get the public status id
    16061608 * @uses bbp_get_topic_post_type() To get the topic post type
    1607  * @uses wp_cache_get() To check if there is a cache of the children
    1608  * @uses bbp_get_public_status_id() To get the public status id
    16091609 * @uses bbp_get_closed_status_id() To get the closed status id
    1610  * @uses wpdb::prepare() To prepare the query
    1611  * @uses wpdb::get_col() To get the result of the query in an array
    1612  * @uses wp_cache_set() To set the cache for future use
     1610 * @uses WP_Query To get get the posts
    16131611 * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids,
    16141612 *                        parent id and post type
     
    16221620    }
    16231621
    1624     // The ID of the cached query
    1625     $cache_id  = 'bbp_parent_public_' . $parent_id . '_type_' . $post_type . '_child_ids';
    1626 
    1627     // Check for cache and set if needed
    1628     $child_ids = wp_cache_get( $cache_id, 'bbpress_posts' );
    1629     if ( false === $child_ids ) {
    1630         $post_status = array( bbp_get_public_status_id() );
    1631 
    1632         // Add closed status if topic post type
    1633         if ( $post_type === bbp_get_topic_post_type() ) {
    1634             $post_status[] = bbp_get_closed_status_id();
    1635         }
    1636 
    1637         // Join post statuses together
    1638         $post_status = "'" . implode( "', '", $post_status ) . "'";
    1639         $bbp_db      = bbp_db();
    1640         $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 );
    1641         $child_ids   = (array) $bbp_db->get_col( $query );
    1642 
    1643         wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' );
    1644     } else {
    1645         $child_ids = (array) $child_ids;
    1646     }
     1622    // Get the public post status
     1623    $post_status = array( bbp_get_public_status_id() );
     1624
     1625    // Add closed status if topic post type
     1626    if ( bbp_get_topic_post_type() === $post_type ) {
     1627        $post_status[] = bbp_get_closed_status_id();
     1628    }
     1629
     1630    $query = new WP_Query( array(
     1631        'fields'      => 'ids',
     1632        'post_parent' => $parent_id,
     1633        'post_status' => $post_status,
     1634        'post_type'   => $post_type,
     1635
     1636        // Maybe change these later
     1637        'posts_per_page'         => -1,
     1638        'update_post_term_cache' => false,
     1639        'update_post_meta_cache' => false,
     1640        'ignore_sticky_posts'    => true,
     1641    ) );
     1642    $child_ids = ! empty( $query->posts ) ? $query->posts : array();
     1643    unset( $query );
    16471644
    16481645    // Filter and return
  • trunk/src/includes/core/cache.php

    r5951 r5954  
    164164    // Loop through query types and clean caches
    165165    foreach ( $post_types as $post_type ) {
    166         wp_cache_delete( 'bbp_get_forum_'     . $_post->ID . '_reply_id',                              'bbpress_posts' );
    167         wp_cache_delete( 'bbp_parent_'        . $_post->ID . '_type_' . $post_type . '_child_last_id', 'bbpress_posts' );
    168         wp_cache_delete( 'bbp_parent_'        . $_post->ID . '_type_' . $post_type . '_child_count',   'bbpress_posts' );
    169         wp_cache_delete( 'bbp_parent_public_' . $_post->ID . '_type_' . $post_type . '_child_ids',     'bbpress_posts' );
    170166        wp_cache_delete( 'bbp_parent_all_'    . $_post->ID . '_type_' . $post_type . '_child_ids',     'bbpress_posts' );
    171167    }
  • trunk/src/includes/forums/functions.php

    r5951 r5954  
    15561556 *
    15571557 * @since 2.0.0 bbPress (r2888)
    1558  *
    1559  * @param int $forum_id Optional. Topic id to update
    1560  * @param int $topic_count Optional. Set the topic count manually
     1558 * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects
     1559 *
     1560 * @param int $forum_id Optional. Topic id to update.
     1561 * @param int $topic_count Optional. Set the topic count manually.
    15611562 * @uses bbp_is_topic() To check if the supplied id is a topic
    15621563 * @uses bbp_get_topic_id() To get the topic id
     
    15661567 * @uses bbp_get_spam_status_id() To get the spam status id
    15671568 * @uses bbp_get_pending_status_id() To get the pending status id
    1568  * @uses wpdb::prepare() To prepare our sql query
    1569  * @uses wpdb::get_var() To execute our query and get the var back
     1569 * @uses bbp_get_topic_post_type() To get the topic post type
    15701570 * @uses update_post_meta() To update the forum hidden topic count meta
    15711571 * @uses apply_filters() Calls 'bbp_update_forum_topic_count_hidden' with the
     
    15901590        // Get topics of forum
    15911591        if ( empty( $topic_count ) ) {
    1592             $statuses    = array( bbp_get_trash_status_id(), bbp_get_spam_status_id(), bbp_get_pending_status_id() );
    1593             $post_status = "'" . implode( "','", $statuses ) . "'";
    1594             $bbp_db      = bbp_db();
    1595             $topic_count = $bbp_db->get_var( $bbp_db->prepare( "SELECT COUNT(ID) FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $forum_id, bbp_get_topic_post_type() ) );
     1592            $query = new WP_Query( array(
     1593                'fields'      => 'ids',
     1594                'post_parent' => $forum_id,
     1595                'post_status' => array( bbp_get_trash_status_id(), bbp_get_spam_status_id(), bbp_get_pending_status_id() ),
     1596                'post_type'   => bbp_get_topic_post_type(),
     1597
     1598                // Maybe change these later
     1599                'posts_per_page'         => -1,
     1600                'update_post_term_cache' => false,
     1601                'update_post_meta_cache' => false,
     1602                'ignore_sticky_posts'    => true,
     1603            ) );
     1604            $topic_count = $query->post_count;
     1605            unset( $query );
    15961606        }
    15971607
     
    16091619 *
    16101620 * @since 2.0.0 bbPress (r2464)
    1611  *
    1612  * @param int $forum_id Optional. Forum id or topic id. It is checked whether it
     1621 * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects
     1622 *
     1623 * @param int  $forum_id Optional. Forum id or topic id. It is checked whether it
    16131624 *                       is a topic or a forum. If it's a topic, its parent,
    16141625 *                       i.e. the forum is automatically retrieved.
    1615  * @param bool $total_count Optional. To return the total count or normal
    1616  *                           count?
    16171626 * @uses bbp_get_forum_id() To get the forum id
    16181627 * @uses bbp_forum_query_subforum_ids() To get the subforum ids
    16191628 * @uses bbp_update_forum_reply_count() To update the forum reply count
    16201629 * @uses bbp_forum_query_topic_ids() To get the forum topic ids
    1621  * @uses wpdb::prepare() To prepare the sql statement
    1622  * @uses wpdb::get_var() To execute the query and get the var back
     1630 * @uses bbp_get_public_status_id() To get the public status id
     1631 * @uses bbp_get_reply_post_type() To get the reply post type
    16231632 * @uses update_post_meta() To update the forum's reply count meta
    1624  * @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the reply
     1633 * @uses apply_filters() Calls 'bbp_update_forum_reply_count' with the reply
    16251634 *                        count and forum id
    16261635 * @return int Forum reply count
     
    16431652    $topic_ids   = bbp_forum_query_topic_ids( $forum_id );
    16441653    if ( ! empty( $topic_ids ) ) {
    1645         $bbp_db      = bbp_db();
    1646         $topic_ids   = implode( ',', wp_parse_id_list( $topic_ids ) );
    1647         $reply_count = (int) $bbp_db->get_var( $bbp_db->prepare( "SELECT COUNT(ID) FROM {$bbp_db->posts} WHERE post_parent IN ( {$topic_ids} ) AND post_status = '%s' AND post_type = '%s';", bbp_get_public_status_id(), bbp_get_reply_post_type() ) );
     1654        $query = new WP_Query( array(
     1655            'fields'          => 'ids',
     1656            'post_parent__in' => $topic_ids,
     1657            'post_status'     => bbp_get_public_status_id(),
     1658            'post_type'       => bbp_get_reply_post_type(),
     1659
     1660            // Maybe change these later
     1661            'posts_per_page'         => -1,
     1662            'update_post_term_cache' => false,
     1663            'update_post_meta_cache' => false,
     1664            'ignore_sticky_posts'    => true,
     1665        ) );
     1666        $reply_count = ! empty( $query->posts ) ? count( $query->posts ) : 0;
     1667        unset( $query );
    16481668    }
    16491669
     
    20692089function bbp_forum_query_subforum_ids( $forum_id ) {
    20702090    $subforum_ids = bbp_get_all_child_ids( $forum_id, bbp_get_forum_post_type() );
    2071     //usort( $subforum_ids, '_bbp_forum_query_usort_subforum_ids' );
    20722091
    20732092    return (array) apply_filters( 'bbp_forum_query_subforum_ids', $subforum_ids, $forum_id );
     
    20752094
    20762095/**
    2077  * Callback to sort forum ID's based on last active time
    2078  *
    2079  * @since 2.1.0 bbPress (r3789)
    2080  *
    2081  * @param int $a First forum ID to compare
    2082  * @param int $b Second forum ID to compare
    2083  * @return Position change based on sort
    2084  */
    2085 function _bbp_forum_query_usort_subforum_ids( $a = 0, $b = 0 ) {
    2086     $ta = get_post_meta( $a, '_bbp_last_active_time', true );
    2087     $tb = get_post_meta( $b, '_bbp_last_active_time', true );
    2088     return ( $ta < $tb ) ? -1 : 1;
    2089 }
    2090 
    2091 /**
    20922096 * Returns the forum's last reply id
    20932097 *
    20942098 * @since 2.0.0 bbPress (r2908)
    2095  *
    2096  * @param int $forum_id Forum id
    2097  * @param int $topic_ids Optional. Topic ids
    2098  * @uses wp_cache_get() To check for cache and retrieve it
     2099 * @since 2.6.0 bbPress (r5954) Replace direct queries with WP_Query() objects
     2100 *
     2101 * @param int $forum_id Forum id.
     2102 * @param int $topic_ids Optional. Topic ids.
     2103 * @uses bbp_get_forum_id() To validate the forum id
    20992104 * @uses bbp_forum_query_topic_ids() To get the forum's topic ids
    2100  * @uses wpdb::prepare() To prepare the query
    2101  * @uses wpdb::get_var() To execute the query and get the var back
     2105 * @uses bbp_get_public_status_id() To get the public status id
    21022106 * @uses bbp_get_reply_post_type() To get the reply post type
    2103  * @uses wp_cache_set() To set the cache for future use
    21042107 * @uses apply_filters() Calls 'bbp_forum_query_last_reply_id' with the reply id
    21052108 *                        and forum id
    21062109 */
    2107 function bbp_forum_query_last_reply_id( $forum_id, $topic_ids = 0 ) {
    2108 
    2109     $cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id';
    2110     $reply_id = wp_cache_get( $cache_id, 'bbpress_posts' );
    2111 
    2112     if ( false === $reply_id ) {
    2113 
    2114         if ( empty( $topic_ids ) ) {
    2115             $topic_ids = bbp_forum_query_topic_ids( $forum_id );
    2116         }
    2117 
    2118         if ( ! empty( $topic_ids ) ) {
    2119             $bbp_db    = bbp_db();
    2120             $topic_ids = implode( ',', wp_parse_id_list( $topic_ids ) );
    2121             $reply_id  = (int) $bbp_db->get_var( $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_parent IN ( {$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() ) );
    2122         } else {
    2123             $reply_id = 0;
    2124         }
    2125 
    2126         wp_cache_set( $cache_id, $reply_id, 'bbpress_posts' );
    2127     } else {
    2128         $reply_id = (int) $reply_id;
    2129     }
     2110function bbp_forum_query_last_reply_id( $forum_id = 0, $topic_ids = 0 ) {
     2111
     2112    // Validate forum
     2113    $forum_id = bbp_get_forum_id( $forum_id );
     2114
     2115    // Get topic ID's if none were passed
     2116    if ( empty( $topic_ids ) ) {
     2117        $topic_ids = bbp_forum_query_topic_ids( $forum_id );
     2118    }
     2119
     2120    $query = new WP_Query( array(
     2121        'fields'          => 'ids',
     2122        'post_parent__in' => $topic_ids,
     2123        'post_status'     => bbp_get_public_status_id(),
     2124        'post_type'       => bbp_get_reply_post_type(),
     2125
     2126        // Maybe change these later
     2127        'posts_per_page'         => 1,
     2128        'update_post_term_cache' => false,
     2129        'update_post_meta_cache' => false,
     2130        'ignore_sticky_posts'    => true,
     2131    ) );
     2132    $reply_id = array_shift( $query->posts );
     2133    unset( $query );
    21302134
    21312135    return (int) apply_filters( 'bbp_forum_query_last_reply_id', $reply_id, $forum_id );
  • trunk/tests/phpunit/testcases/common/query.php

    r5953 r5954  
    172172        ) );
    173173
    174         bbp_clean_post_cache( $f );
    175174        $count = count( bbp_get_public_child_ids( $f, bbp_get_forum_post_type() ) );
    176175        $this->assertSame( 3, $count );
Note: See TracChangeset for help on using the changeset viewer.