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/engagements.php

    r6616 r6619  
    209209 *
    210210 * @since 2.6.0 bbPress (r6320)
    211  *
    212  * @param int $user_id Optional. User id
    213  *
    214  * @return array|bool Results if user has engaged, otherwise false
    215  */
    216 function bbp_get_user_engagements( $user_id = 0 ) {
    217     $user_id     = bbp_get_user_id( $user_id );
    218     $engagements = bbp_has_topics( array(
    219         'meta_query' => array(
    220             array(
     211 * @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments
     212 *
     213 * @param array $args Optional. Arguments to pass into bbp_has_replies()
     214 *
     215 * @return bool True if user has engaged, otherwise false
     216 */
     217function bbp_get_user_engagements( $args = array() ) {
     218
     219    // Backwards compat for pre-2.6.0
     220    if ( is_numeric( $args ) ) {
     221        $args = array(
     222            'meta_query' => array( array(
    221223                'key'     => '_bbp_engagement',
    222                 'value'   => $user_id,
     224                'value'   => bbp_get_user_id( $args, false, false ),
    223225                'compare' => 'NUMERIC'
    224             )
    225         )
    226     ) );
    227 
    228     // Filter & return
    229     return apply_filters( 'bbp_get_user_engagements', $engagements, $user_id );
     226            ) )
     227        );
     228    }
     229
     230    // Default arguments
     231    $defaults = array(
     232        'meta_query' => array( array(
     233            'key'     => '_bbp_engagement',
     234            'value'   => bbp_get_displayed_user_id(),
     235            'compare' => 'NUMERIC'
     236        ) )
     237    );
     238
     239    // Parse arguments
     240    $r = bbp_parse_args( $args, $defaults, 'get_user_engagements' );
     241
     242    // Get the topics
     243    $query   = bbp_has_topics( $r );
     244    $user_id = isset( $r['meta_query'][0]['value'] )
     245        ? $r['meta_query'][0]['value']
     246        : 0;
     247
     248    // Filter & return
     249    return apply_filters( 'bbp_get_user_engagements', $query, $user_id, $r, $args );
    230250}
    231251
     
    439459 *
    440460 * @since 2.0.0 bbPress (r2652)
    441  *
    442  * @param int $user_id Optional. User id
    443  *
    444  * @return array|bool Results if user has favorites, otherwise false
    445  */
    446 function bbp_get_user_favorites( $user_id = 0 ) {
    447     $user_id = bbp_get_user_id( $user_id );
    448     $query   = bbp_has_topics( array(
    449         'meta_query' => array(
    450             array(
     461 * @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments
     462 *
     463 * @param array $args Optional. Arguments to pass into bbp_has_replies()
     464 *
     465 * @return bool True if user has favorites, otherwise false
     466 */
     467function bbp_get_user_favorites( $args = array() ) {
     468
     469    // Backwards compat for pre-2.6.0
     470    if ( is_numeric( $args ) ) {
     471        $args = array(
     472            'meta_query' => array( array(
    451473                'key'     => '_bbp_favorite',
    452                 'value'   => $user_id,
     474                'value'   => bbp_get_user_id( $args, false, false ),
    453475                'compare' => 'NUMERIC'
    454             )
    455         )
    456     ) );
    457 
    458     // Filter & return
    459     return apply_filters( 'bbp_get_user_favorites', $query, $user_id );
     476            ) )
     477        );
     478    }
     479
     480    // Default arguments
     481    $defaults = array(
     482        'meta_query' => array( array(
     483            'key'     => '_bbp_favorite',
     484            'value'   => bbp_get_displayed_user_id(),
     485            'compare' => 'NUMERIC'
     486        ) )
     487    );
     488
     489    // Parse arguments
     490    $r = bbp_parse_args( $args, $defaults, 'get_user_favorites' );
     491
     492    // Get the topics
     493    $query   = bbp_has_topics( $r );
     494        $user_id = isset( $r['meta_query'][0]['value'] )
     495        ? $r['meta_query'][0]['value']
     496        : 0;
     497
     498    // Filter & return
     499    return apply_filters( 'bbp_get_user_favorites', $query, $user_id, $r, $args );
    460500}
    461501
     
    664704 *
    665705 * @since 2.0.0 bbPress (r2668)
    666  *
    667  * @param int $user_id Optional. User id
    668  *
    669  * @return array|bool Results if user has subscriptions, otherwise false
    670  */
    671 function bbp_get_user_topic_subscriptions( $user_id = 0 ) {
    672     $user_id = bbp_get_user_id( $user_id );
    673     $query   = bbp_has_topics( array(
    674         'meta_query' => array(
    675             array(
     706 * @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments
     707 *
     708 * @param array $args Optional. Arguments to pass into bbp_has_replies()
     709 *
     710 * @return bool True if user has forum subscriptions, otherwise false
     711 */
     712function bbp_get_user_topic_subscriptions( $args = array() ) {
     713
     714    // Backwards compat for pre-2.6.0
     715    if ( is_numeric( $args ) ) {
     716        $args = array(
     717            'meta_query' => array( array(
    676718                'key'     => '_bbp_subscription',
    677                 'value'   => $user_id,
     719                'value'   => bbp_get_user_id( $args, false, false ),
    678720                'compare' => 'NUMERIC'
    679             )
    680         )
    681     ) );
    682 
    683     // Filter & return
    684     return apply_filters( 'bbp_get_user_topic_subscriptions', $query, $user_id );
     721            ) )
     722        );
     723    }
     724
     725    // Default arguments
     726    $defaults = array(
     727        'meta_query' => array( array(
     728            'key'     => '_bbp_subscription',
     729            'value'   => bbp_get_displayed_user_id(),
     730            'compare' => 'NUMERIC'
     731        ) )
     732    );
     733
     734    // Parse arguments
     735    $r = bbp_parse_args( $args, $defaults, 'get_user_topic_subscriptions' );
     736
     737    // Get the topics
     738    $query   = bbp_has_topics( $r );
     739    $user_id = isset( $r['meta_query'][0]['value'] )
     740        ? $r['meta_query'][0]['value']
     741        : 0;
     742
     743    // Filter & return
     744    return apply_filters( 'bbp_get_user_topic_subscriptions', $query, $user_id, $r, $args );
    685745}
    686746
     
    689749 *
    690750 * @since 2.5.0 bbPress (r5156)
    691  *
    692  * @param int $user_id Optional. User id
    693  *
    694  * @return array|bool Results if user has subscriptions, otherwise false
    695  */
    696 function bbp_get_user_forum_subscriptions( $user_id = 0 ) {
    697     $user_id = bbp_get_user_id( $user_id );
    698     $query   = bbp_has_forums( array(
    699         'meta_query' => array(
    700             array(
     751 * @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments
     752 *
     753 * @param array $args Optional. Arguments to pass into bbp_has_replies()
     754 *
     755 * @return bool True if user has forum subscriptions, otherwise false
     756 */
     757function bbp_get_user_forum_subscriptions( $args = array() ) {
     758
     759    // Backwards compat for pre-2.6.0
     760    if ( is_numeric( $args ) ) {
     761        $args = array(
     762            'meta_query' => array( array(
    701763                'key'     => '_bbp_subscription',
    702                 'value'   => $user_id,
     764                'value'   => bbp_get_user_id( $args, false, false ),
    703765                'compare' => 'NUMERIC'
    704             )
    705         )
    706     ) );
    707 
    708     // Filter & return
    709     return apply_filters( 'bbp_get_user_forum_subscriptions', $query, $user_id );
     766            ) )
     767        );
     768    }
     769
     770    // Default arguments
     771    $defaults = array(
     772        'meta_query' => array( array(
     773            'key'     => '_bbp_subscription',
     774            'value'   => bbp_get_displayed_user_id(),
     775            'compare' => 'NUMERIC'
     776        ) )
     777    );
     778
     779    // Parse arguments
     780    $r = bbp_parse_args( $args, $defaults, 'get_user_forum_subscriptions' );
     781
     782    // Get the forums
     783    $query   = bbp_has_forums( $r );
     784    $user_id = isset( $r['meta_query'][0]['value'] )
     785        ? $r['meta_query'][0]['value']
     786        : 0;
     787
     788    // Filter & return
     789    return apply_filters( 'bbp_get_user_forum_subscriptions', $query, $user_id, $r, $args );
    710790}
    711791
Note: See TracChangeset for help on using the changeset viewer.