Skip to:
Content

bbPress.org


Ignore:
Timestamp:
07/09/2017 04:53:10 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Users: Use bbp_parse_args() in user content loops.

This commit changes the function signatures of several wrappers for bbp_has_ functions away from simply accepting a $user_id to accepting an array of arguments, in a fully backwards compatible way. It also updates the surrounding documentation to more accurately describe what is returned and why.

This allows the arguments used within these functions to be explicitly filtered, passively or aggressively overridden, or bypassed entirely.

File:
1 edited

Legend:

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

    r6612 r6619  
    484484 *
    485485 * @since 2.0.0 bbPress (r2660)
    486  *
    487  * @param int $user_id Optional. User id
    488  *
    489  * @return array|bool Results if the user has created topics, otherwise false
    490  */
    491 function bbp_get_user_topics_started( $user_id = 0 ) {
    492 
    493     // Validate user
    494     $user_id = bbp_get_user_id( $user_id );
    495     if ( empty( $user_id ) ) {
    496         return false;
    497     }
    498 
    499     // Try to get the topics
    500     $query = bbp_has_topics( array(
    501         'author' => $user_id
    502     ) );
     486 * @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments
     487 *
     488 * @param array $args    Optional. Arguments to pass into bbp_has_topics()
     489 *
     490 * @return bool True if user has started topics, otherwise false
     491 */
     492function bbp_get_user_topics_started( $args = array() ) {
     493
     494    // Backwards compat for pre-2.6.0
     495    if ( is_numeric( $args ) ) {
     496        $args = array(
     497            'author' => bbp_get_user_id( $args, false, false )
     498        );
     499    }
     500
     501    // Default arguments
     502    $defaults = array(
     503        'author' => bbp_get_displayed_user_id()
     504    );
     505
     506    // Parse arguments
     507    $r = bbp_parse_args( $args, $defaults, 'get_user_topics_started' );
     508
     509    // Get the topics
     510    $query   = bbp_has_topics( $r );
     511    $user_id = $r['author'];
    503512
    504513    // Filter & return
    505     return apply_filters( 'bbp_get_user_topics_started', $query, $user_id );
     514    return apply_filters( 'bbp_get_user_topics_started', $query, $user_id, $r, $args );
    506515}
    507516
     
    510519 *
    511520 * @since 2.2.0 bbPress (r4225)
    512  *
    513  * @param int $user_id Optional. User id
    514  *
    515  * @return array|bool Results if the user has created topics, otherwise false
    516  */
    517 function bbp_get_user_replies_created( $user_id = 0 ) {
    518 
    519     // Validate user
    520     $user_id = bbp_get_user_id( $user_id );
    521     if ( empty( $user_id ) ) {
    522         return false;
    523     }
    524 
    525     // Try to get the topics
    526     $query = bbp_has_replies( array(
     521 * @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments
     522 *
     523 * @param array $args Optional. Arguments to pass into bbp_has_replies()
     524 *
     525 * @return bool True if user has created replies, otherwise false
     526 */
     527function bbp_get_user_replies_created( $args = array() ) {
     528
     529    // Backwards compat for pre-2.6.0
     530    if ( is_numeric( $args ) ) {
     531        $args = array(
     532            'author' => bbp_get_user_id( $args, false, false ),
     533            'post_type' => bbp_get_reply_post_type(),
     534            'order'     => 'DESC'
     535        );
     536    }
     537
     538    // Default arguments
     539    $defaults = array(
     540        'author'    => bbp_get_displayed_user_id(),
    527541        'post_type' => bbp_get_reply_post_type(),
    528         'order'     => 'DESC',
    529         'author'    => $user_id
    530     ) );
     542        'order'     => 'DESC'
     543    );
     544
     545    // Parse arguments
     546    $r = bbp_parse_args( $args, $defaults, 'get_user_replies_created' );
     547
     548    // Get the replies
     549    $query   = bbp_has_replies( $r );
     550    $user_id = $r['author'];
    531551
    532552    // Filter & return
    533     return apply_filters( 'bbp_get_user_replies_created', $query, $user_id );
     553    return apply_filters( 'bbp_get_user_replies_created', $query, $user_id, $r, $args );
    534554}
    535555
Note: See TracChangeset for help on using the changeset viewer.