Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/16/2014 01:05:35 PM (11 years ago)
Author:
boonebgorges
Message:

Introduce bp_activity_show_filters(), and use it for generating dynamic activity filter dropdowns

Activity filter dropdowns were previously hardcoded into templates, with
template-specific hooks for plugins to add their own filter values. This
resulted in an inconsistent and awkward process of maintaining filter lists
between components. The new method introduces new parameters to bp_activity_set_action():
$context (an array of the contexts in which the filter should appear) and
$label (the text that should be used when generating the <option> markup). The
new function bp_activity_show_filter() is then used in the templates to
generate a dynamic list of <option> markup, based on the values registered by
the activity types.

Fixes #5637

Props imath

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-template.php

    r8404 r8428  
    33723372}
    33733373add_action( 'bp_head', 'bp_activity_sitewide_feed' );
     3374
     3375/**
     3376 * Display available filters depending on the scope.
     3377 *
     3378 * @since BuddyPress (2.1.0)
     3379 *
     3380 * @param string $context The current context. 'activity', 'member',
     3381 *    'member_groups', 'group'.
     3382 * @uses bp_get_activity_show_filters()
     3383 */
     3384function bp_activity_show_filters( $context = '' ) {
     3385    echo bp_get_activity_show_filters( $context );
     3386}
     3387    /**
     3388     * Get available filters depending on the scope.
     3389     *
     3390     * @since BuddyPress (2.1.0)
     3391     *
     3392     * @param string $context The current context. 'activity', 'member',
     3393     *    'member_groups', 'group'
     3394     * @return string HTML for <option> values.
     3395     */
     3396    function bp_get_activity_show_filters( $context = '' ) {
     3397        // Set default context based on current page
     3398        if ( empty( $context ) ) {
     3399            if ( bp_is_user() ) {
     3400                switch ( bp_current_action() ) {
     3401                    case bp_get_groups_slug() :
     3402                        $context = 'member_groups';
     3403                        break;
     3404
     3405                    default :
     3406                        $context = 'member';
     3407                        break;
     3408                }
     3409            } else if ( bp_is_active( 'groups' ) && bp_is_group() ) {
     3410                $context = 'group';
     3411            } else {
     3412                $context = 'activity';
     3413            }
     3414        }
     3415
     3416        $filters = array();
     3417
     3418        // Walk through the registered actions, and prepare an the
     3419        // select box options.
     3420        foreach ( buddypress()->activity->actions as $actions ) {
     3421            foreach ( $actions as $action ) {
     3422                if ( ! in_array( $context, (array) $action['context'] ) ) {
     3423                    continue;
     3424                }
     3425
     3426                // Friends activity collapses two filters into one
     3427                if ( in_array( $action['key'], array( 'friendship_accepted', 'friendship_created' ) ) ) {
     3428                    $action['key'] = 'friendship_accepted,friendship_created';
     3429                }
     3430
     3431                $filters[ $action['key'] ] = $action['label'];
     3432            }
     3433        }
     3434
     3435        /**
     3436         * Modify the filter options available in the activity filter dropdown.
     3437         *
     3438         * @since BuddyPress (2.1.0)
     3439         *
     3440         * @param array $filters Array of filter options for the given
     3441         *        context, in the following format:
     3442         *            $option_value => $option_name
     3443         * @param string $context Context for the filter. 'activity'
     3444         *        'member', 'member_groups', 'group'.
     3445         */
     3446        $filters = apply_filters( 'bp_get_activity_show_filters', $filters, $context );
     3447
     3448        // Build the options output
     3449        $output = '';
     3450
     3451        if ( ! empty( $filters ) ) {
     3452            foreach ( $filters as $value => $filter ) {
     3453                $output .= '<option value="' . esc_attr( $value ) . '">' . esc_html( $filter ) . '</option>' . "\n";
     3454            }
     3455        }
     3456
     3457        return apply_filters( 'bp_get_activity_show_filters', $output, $filters, $context );
     3458    }
Note: See TracChangeset for help on using the changeset viewer.