Skip to:
Content

bbPress.org


Ignore:
Timestamp:
07/02/2017 04:39:49 PM (7 years ago)
Author:
johnjamesjacoby
Message:

Queries: nopaging audit.

  • In r6506 the nopaging query argument was added to various queries to avoid paginating results when it wasn't necessary. This resulted in a few queries (widgets mainly) not obeying their specific settings.
  • In #3123, other inconsistencies in our query arguments were uncovered, triggering the need to audit our query usages and equalize them once again.

This change brings all queries back to par with one another, specifically in regards to posts_per_page => -1 style queries, and queries where filters can be suppressed and meta/term caches should not be primed.

It also groups together the get_user_object_ids functions. These are now unused in bbPress proper, though were previously useful before the engagements API was in place. These queries are considered too inefficient to rely upon in large-scale applications, but are included to provide filterable wrappers should someone need them, or should we need to bring them back later.

Props thebrandonallen. Fixes #3123.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/users/engagements.php

    r6573 r6607  
    228228    // Filter & return
    229229    return apply_filters( 'bbp_get_user_engagements', $engagements, $user_id );
    230 }
    231 
    232 /**
    233  * Get a user's engaged topic ids
    234  *
    235  * @since 2.6.0 bbPress (r6320)
    236  *
    237  * @param int $user_id Optional. User id
    238  *
    239  * @return array Topic ids if user has engaged, otherwise empty array
    240  */
    241 function bbp_get_user_engaged_topic_ids( $user_id = 0 ) {
    242     $user_id     = bbp_get_user_id( $user_id );
    243     $engagements = new WP_Query( array(
    244         'fields'        => 'ids',
    245         'post_type'     => bbp_get_topic_post_type(),
    246         'nopaging'      => true,
    247         'no_found_rows' => true,
    248         'meta_query'    => array( array(
    249             'key'     => '_bbp_engagement',
    250             'value'   => $user_id,
    251             'compare' => 'NUMERIC'
    252         ) )
    253     ) );
    254 
    255     // Filter & return
    256     return (array) apply_filters( 'bbp_get_user_engaged_topic_ids', $engagements->posts, $user_id );
    257230}
    258231
     
    928901
    929902/**
     903 * Get a user's object IDs
     904 *
     905 * For the most part, you should not need to use this function, and may even
     906 * want to come up with a more efficient way to get IDs on your own. Nevertheless,
     907 * it is available here for your convenience, using the most efficient query
     908 * parameters available inside of the various query APIs.
     909 *
     910 * @since 2.6.0 bbPress (r6606)
     911 *
     912 * @param int    $user_id   The user id
     913 * @param string $meta_key  The relationship key
     914 * @param string $meta_type The relationship type (usually 'post')
     915 * @param array  $args      The arguments to override defaults
     916 *
     917 * @return array|bool Results if user has objects, otherwise null
     918 */
     919function bbp_get_user_object_ids( $args = array() ) {
     920    $object_ids = $defaults = array();
     921
     922    // Parse arguments
     923    $r = bbp_parse_args( $args, array(
     924        'user_id'     => 0,
     925        'object_type' => bbp_get_topic_post_type(),
     926        'meta_key'    => '',
     927        'meta_type'   => 'post',
     928        'filter'      => 'user_object_ids',
     929        'args'        => array()
     930    ), 'get_user_object_ids' );
     931
     932    // Sanitize arguments
     933    $r['user_id']     = bbp_get_user_id( $r['user_id'] );
     934    $r['meta_key']    = sanitize_key( $r['meta_key'] );
     935    $r['meta_type']   = sanitize_key( $r['meta_type'] );
     936    $r['object_type'] = sanitize_key( $r['object_type'] );
     937    $r['filter']      = sanitize_key( $r['filter'] );
     938
     939    // Defaults
     940    if ( 'post' === $r['meta_type'] ) {
     941        $defaults = array(
     942            'fields'         => 'ids',
     943            'post_type'      => $r['object_type'],
     944            'posts_per_page' => -1,
     945            'meta_query'     => array( array(
     946                'key'     => $r['meta_key'],
     947                'value'   => $r['user_id'],
     948                'compare' => 'NUMERIC'
     949            ),
     950
     951            // Performance
     952            'nopaging'               => true,
     953            'suppress_filters'       => true,
     954            'update_post_term_cache' => false,
     955            'update_post_meta_cache' => false,
     956            'ignore_sticky_posts'    => true,
     957            'no_found_rows'          => true
     958        ) );
     959    }
     960
     961    // Parse arguments
     962    $r = bbp_parse_args( $r['args'], $defaults, "get_{$r['filter']}_args" );
     963
     964    // Queries
     965    if ( 'post' === $r['meta_type'] ) {
     966        $query      = new WP_Query( $r );
     967        $object_ids = $query->posts;
     968    }
     969
     970    // Filter & return
     971    return (array) apply_filters( "bbp_get_{$r['filter']}", $object_ids, $r, $args );
     972}
     973
     974/**
     975 * Get array of forum IDs that a user can moderate
     976 *
     977 * @since 2.6.0 bbPress (r5834)
     978 *
     979 * @param int $user_id User id.
     980 *
     981 * @return array Return array of forum ids, or empty array
     982 */
     983function bbp_get_moderator_forum_ids( $user_id = 0 ) {
     984    return bbp_get_user_object_ids( array(
     985        'user_id'   => $user_id,
     986        'meta_key'  => '_bbp_moderator_id',
     987        'post_type' => bbp_get_forum_post_type(),
     988        'filter'    => 'moderator_forum_ids'
     989    ) );
     990}
     991
     992/**
     993 * Get a user's engaged topic ids
     994 *
     995 * @since 2.6.0 bbPress (r6320)
     996 *
     997 * @param int $user_id Optional. User id
     998 *
     999 * @return array Return array of topic ids, or empty array
     1000 */
     1001function bbp_get_user_engaged_topic_ids( $user_id = 0 ) {
     1002    return bbp_get_user_object_ids( array(
     1003        'user_id'  => $user_id,
     1004        'meta_key' => '_bbp_engagement',
     1005        'filter'   => 'user_engaged_topic_ids'
     1006    ) );
     1007}
     1008
     1009/**
    9301010 * Get a user's favorite topic ids
    9311011 *
     
    9341014 * @param int $user_id Optional. User id
    9351015 *
    936  * @return array|bool Results if user has favorites, otherwise null
     1016 * @return array Return array of favorited ids, or empty array
    9371017 */
    9381018function bbp_get_user_favorites_topic_ids( $user_id = 0 ) {
    939     $user_id   = bbp_get_user_id( $user_id );
    940     $favorites = new WP_Query( array(
    941         'fields'        => 'ids',
    942         'post_type'     => bbp_get_topic_post_type(),
    943         'nopaging'      => true,
    944         'no_found_rows' => true,
    945         'meta_query'    => array( array(
    946             'key'     => '_bbp_favorite',
    947             'value'   => $user_id,
    948             'compare' => 'NUMERIC'
    949         ) )
     1019    return bbp_get_user_object_ids( array(
     1020        'user_id'  => $user_id,
     1021        'meta_key' => '_bbp_favorite',
     1022        'filter'   => 'user_favorites_topic_ids'
    9501023    ) );
    951 
    952     // Filter & return
    953     return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites->posts, $user_id );
    954 }
    955 
     1024}
    9561025
    9571026/**
     
    9621031 * @param int $user_id Optional. User id
    9631032 *
    964  * @return array|bool Results if user has subscriptions, otherwise null
     1033 * @return array Return array of subscribed ids, or empty array
    9651034 */
    9661035function bbp_get_user_subscribed_forum_ids( $user_id = 0 ) {
    967     $user_id       = bbp_get_user_id( $user_id );
    968     $subscriptions = new WP_Query( array(
    969         'fields'        => 'ids',
    970         'post_type'     => bbp_get_forum_post_type(),
    971         'nopaging'      => true,
    972         'no_found_rows' => true,
    973         'meta_query'    => array( array(
    974             'key'     => '_bbp_subscription',
    975             'value'   => $user_id,
    976             'compare' => 'NUMERIC'
    977         ) )
     1036    return bbp_get_user_object_ids( array(
     1037        'user_id'   => $user_id,
     1038        'meta_key'  => '_bbp_subscription',
     1039        'post_type' => bbp_get_forum_post_type(),
     1040        'filter'    => 'user_subscribed_forum_ids'
    9781041    ) );
    979 
    980     // Filter & return
    981     return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions->posts, $user_id );
    9821042}
    9831043
     
    9891049 * @param int $user_id Optional. User id
    9901050 *
    991  * @return array|bool Results if user has subscriptions, otherwise null
     1051 * @return array Return array of subscribed ids, or empty array
    9921052 */
    9931053function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) {
    994     $user_id       = bbp_get_user_id( $user_id );
    995     $subscriptions = new WP_Query( array(
    996         'fields'        => 'ids',
    997         'post_type'     => bbp_get_topic_post_type(),
    998         'nopaging'      => true,
    999         'no_found_rows' => true,
    1000         'meta_query' => array( array(
    1001             'key'     => '_bbp_subscription',
    1002             'value'   => $user_id,
    1003             'compare' => 'NUMERIC'
    1004         ) )
     1054    return bbp_get_user_object_ids( array(
     1055        'user_id'  => $user_id,
     1056        'meta_key' => '_bbp_subscription',
     1057        'filter'   => 'user_subscribed_topic_ids'
    10051058    ) );
    1006 
    1007     // Filter & return
    1008     return (array) apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions->posts, $user_id );
    10091059}
    10101060
Note: See TracChangeset for help on using the changeset viewer.