Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
12/18/2013 04:53:16 PM (12 years ago)
Author:
johnjamesjacoby
Message:

Introduce bp_parse_args() to bp-core-functions.php, ported from bbPress. See #5306.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-functions.php

    r7690 r7704  
    200200
    201201    return $new_args;
     202}
     203
     204/**
     205 * Merge user defined arguments into defaults array.
     206 *
     207 * This function is used throughout BuddyPress to allow for either a string or
     208 * array to be merged into another array. It is identical to wp_parse_args()
     209 * except it allows for arguments to be passively or aggressively filtered using
     210 * the optional $filter_key parameter. If no $filter_key is passed, no filters
     211 * are applied.
     212 *
     213 * @since BuddyPress (r7704)
     214 *
     215 * @param string|array $args Value to merge with $defaults
     216 * @param array $defaults Array that serves as the defaults.
     217 * @param string $filter_key String to key the filters from
     218 * @return array Merged user defined values with defaults.
     219 */
     220function bp_parse_args( $args, $defaults = array(), $filter_key = '' ) {
     221
     222    // Setup a temporary array from $args
     223    if ( is_object( $args ) ) {
     224        $r = get_object_vars( $args );
     225    } elseif ( is_array( $args ) ) {
     226        $r =& $args;
     227    } else {
     228        wp_parse_str( $args, $r );
     229    }
     230
     231    // Passively filter the args before the parse
     232    if ( !empty( $filter_key ) ) {
     233        $r = apply_filters( 'bp_before_' . $filter_key . '_parse_args', $r );
     234    }
     235
     236    // Parse
     237    if ( is_array( $defaults ) && !empty( $defaults ) ) {
     238        $r = array_merge( $defaults, $r );
     239    }
     240
     241    // Aggressively filter the args after the parse
     242    if ( !empty( $filter_key ) ) {
     243        $r = apply_filters( 'bp_after_' . $filter_key . '_parse_args', $r );
     244    }
     245
     246    // Return the parsed results
     247    return $r;
    202248}
    203249
Note: See TracChangeset for help on using the changeset viewer.