Skip to:
Content

BuddyPress.org

Changeset 6842


Ignore:
Timestamp:
03/07/2013 02:52:46 PM (8 years ago)
Author:
boonebgorges
Message:

Improves the save routine for the Components Dashboard page

The subtabs of the Components Dashboard page (All, Active, Inactive, Must Use,
Retired) all work differently in terms of how data is returned from them. Some
of the subtabs - All and Active - are accurate reflections of the components
that the user currently wants to be active. Retired and Inactive, on the other
hand, don't contain complete data about which components are supposed to be
turned on. For that reason, we can't trust the checkbox data submitted through
the POST when setting the active components.

This changeset introduces bp_core_admin_get_active_components_from_submitted_settings(),
which handles the logic necessary to determine which components the user
intends to be active, based on the data submitted in the POST request. It's
been separated out into a standalone function for better unit testing.

Test: https://github.com/buddypress/BuddyPress-Unit-Tests/commit/49e72ea3fc1f103ab6336c79f5c5b66294f4aad6

See #4671

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/admin/bp-core-components.php

    r6771 r6842  
    6262    );
    6363
    64     // Required components
    65     $required_components = array(
    66         'core' => array(
    67             'title'       => __( 'BuddyPress Core', 'buddypress' ),
    68             'description' => __( 'It&#8216;s what makes <del>time travel</del> BuddyPress possible!', 'buddypress' )
    69         ),
    70         'members' => array(
    71             'title'       => __( 'Community Members', 'buddypress' ),
    72             'description' => __( 'Everything in a BuddyPress community revolves around its members.', 'buddypress' )
    73         ),
    74     );
    75 
    76     // Retired components
    77     $retired_components['forums'] = array(
    78         'title'       => __( 'Group Forums', 'buddypress' ),
    79         'description' => sprintf( __( 'BuddyPress Forums are retired. Use %s.', 'buddypress' ), '<a href="http://bbpress.org">bbPress</a>' )
    80     );
    81 
    82     // Optional core components
    83     $optional_components = array(
    84         'xprofile' => array(
    85             'title'       => __( 'Extended Profiles', 'buddypress' ),
    86             'description' => __( 'Customize your community with fully editable profile fields that allow your users to describe themselves.', 'buddypress' )
    87         ),
    88         'settings' => array(
    89             'title'       => __( 'Account Settings', 'buddypress' ),
    90             'description' => __( 'Allow your users to modify their account and notification settings directly from within their profiles.', 'buddypress' )
    91         ),
    92         'friends'  => array(
    93             'title'       => __( 'Friend Connections', 'buddypress' ),
    94             'description' => __( 'Let your users make connections so they can track the activity of others and focus on the people they care about the most.', 'buddypress' )
    95         ),
    96         'messages' => array(
    97             'title'       => __( 'Private Messaging', 'buddypress' ),
    98             'description' => __( 'Allow your users to talk to each other directly and in private. Not just limited to one-on-one discussions, messages can be sent between any number of members.', 'buddypress' )
    99         ),
    100         'activity' => array(
    101             'title'       => __( 'Activity Streams', 'buddypress' ),
    102             'description' => __( 'Global, personal, and group activity streams with threaded commenting, direct posting, favoriting and @mentions, all with full RSS feed and email notification support.', 'buddypress' )
    103         ),
    104         'groups'   => array(
    105             'title'       => __( 'User Groups', 'buddypress' ),
    106             'description' => __( 'Groups allow your users to organize themselves into specific public, private or hidden sections with separate activity streams and member listings.', 'buddypress' )
    107         ),
    108         'forums'   => array(
    109             'title'       => __( 'Group Forums (Legacy)', 'buddypress' ),
    110             'description' => __( 'Group forums allow for focused, bulletin-board style conversations.', 'buddypress' )
    111         ),
    112         'blogs'    => array(
    113             'title'       => __( 'Site Tracking', 'buddypress' ),
    114             'description' => __( 'Record activity for new posts and comments from your site.', 'buddypress' )
    115         )
    116     );
     64    $optional_components = bp_core_admin_get_components( 'optional' );
     65    $required_components = bp_core_admin_get_components( 'required' );
     66    $retired_components = bp_core_admin_get_components( 'retired' );
    11767
    11868    // Don't show Forums component in optional components if it's disabled
    11969    if ( ! bp_is_active( 'forums' ) ) {
    12070        unset( $optional_components['forums'] );
    121     }
    122 
    123     // Add blogs tracking if multisite
    124     if ( is_multisite() ) {
    125         $optional_components['blogs']['description'] = __( 'Record activity for new sites, posts, and comments across your network.', 'buddypress' );
    12671    }
    12772
     
    298243        require_once( BP_PLUGIN_DIR . '/bp-core/admin/bp-core-schema.php' );
    299244
    300         $bp->active_components = stripslashes_deep( $_POST['bp_components'] );
     245        $submitted = stripslashes_deep( $_POST['bp_components'] );
     246        $bp->active_components = bp_core_admin_get_active_components_from_submitted_settings( $submitted );
    301247
    302248        bp_core_install( $bp->active_components );
     
    312258}
    313259add_action( 'bp_admin_init', 'bp_core_admin_components_settings_handler' );
     260
     261/**
     262 * Calculates the components that should be active after save, based on submitted settings
     263 *
     264 * The way that active components must be set after saving your settings must
     265 * be calculated differently depending on which of the Components subtabs you
     266 * are coming from:
     267 * - When coming from All or Active, the submitted checkboxes accurately
     268 *   reflect the desired active components, so we simply pass them through
     269 * - When coming from Inactive, components can only be activated - already
     270 *   active components will not be passed in the $_POST global. Thus, we must
     271 *   parse the newly activated components with the already active components
     272 *   saved in the $bp global
     273 * - When activating a Retired component, the situation is similar to Inactive.
     274 * - When deactivating a Retired component, no value is passed in the $_POST
     275 *   global (because the component settings are checkboxes). So, in order to
     276 *   determine whether a retired component is being deactivated, we retrieve a
     277 *   list of retired components, and check each one to ensure that its checkbox
     278 *   is not present, before merging the submitted components with the active
     279 *   ones.
     280 *
     281 * @since (BuddyPress) 1.7
     282 *
     283 * @param array This is the array of component settings coming from the POST
     284 *   global. You should stripslashes_deep() before passing to this function
     285 * @return array The calculated list of component settings
     286 */
     287function bp_core_admin_get_active_components_from_submitted_settings( $submitted ) {
     288    $current_action = 'all';
     289
     290    if ( isset( $_GET['action'] ) && in_array( $_GET['action'], array( 'active', 'inactive', 'retired' ) ) ) {
     291        $current_action = $_GET['action'];
     292    }
     293
     294    $current_components = buddypress()->active_components;
     295
     296    switch ( $current_action ) {
     297        case 'retired' :
     298            $retired_components = bp_core_admin_get_components( 'retired' );
     299            foreach ( array_keys( $retired_components ) as $retired_component ) {
     300                if ( ! isset( $submitted[ $retired_component ] ) ) {
     301                    unset( $current_components[ $retired_component ] );
     302                }
     303            }
     304            // fall through
     305
     306        case 'inactive' :
     307            $components = array_merge( $submitted, $current_components );
     308            break;
     309
     310        case 'all' :
     311        case 'active' :
     312        default :
     313            $components = $submitted;
     314            break;
     315    }
     316
     317    return $components;
     318}
     319
     320/**
     321 * Return a list of component information, optionally filtered by type
     322 *
     323 * We use this information both to build the markup for the admin screens, as
     324 * well as to do some processing on settings data submitted from those screens.
     325 *
     326 * @since (BuddyPress) 1.7
     327 *
     328 * @param string $type 'all', 'optional', 'retired', 'required'
     329 * @return array An array of requested component data
     330 */
     331function bp_core_admin_get_components( $type = 'all' ) {
     332
     333    // Required components
     334    $required_components = array(
     335        'core' => array(
     336            'title'       => __( 'BuddyPress Core', 'buddypress' ),
     337            'description' => __( 'It&#8216;s what makes <del>time travel</del> BuddyPress possible!', 'buddypress' )
     338        ),
     339        'members' => array(
     340            'title'       => __( 'Community Members', 'buddypress' ),
     341            'description' => __( 'Everything in a BuddyPress community revolves around its members.', 'buddypress' )
     342        ),
     343    );
     344
     345    // Retired components
     346    $retired_components = array(
     347        'forums' => array(
     348            'title'       => __( 'Group Forums', 'buddypress' ),
     349            'description' => sprintf( __( 'BuddyPress Forums are retired. Use %s.', 'buddypress' ), '<a href="http://bbpress.org">bbPress</a>' )
     350        ),
     351    );
     352
     353    // Optional core components
     354    $optional_components = array(
     355        'xprofile' => array(
     356            'title'       => __( 'Extended Profiles', 'buddypress' ),
     357            'description' => __( 'Customize your community with fully editable profile fields that allow your users to describe themselves.', 'buddypress' )
     358        ),
     359        'settings' => array(
     360            'title'       => __( 'Account Settings', 'buddypress' ),
     361            'description' => __( 'Allow your users to modify their account and notification settings directly from within their profiles.', 'buddypress' )
     362        ),
     363        'friends'  => array(
     364            'title'       => __( 'Friend Connections', 'buddypress' ),
     365            'description' => __( 'Let your users make connections so they can track the activity of others and focus on the people they care about the most.', 'buddypress' )
     366        ),
     367        'messages' => array(
     368            'title'       => __( 'Private Messaging', 'buddypress' ),
     369            'description' => __( 'Allow your users to talk to each other directly and in private. Not just limited to one-on-one discussions, messages can be sent between any number of members.', 'buddypress' )
     370        ),
     371        'activity' => array(
     372            'title'       => __( 'Activity Streams', 'buddypress' ),
     373            'description' => __( 'Global, personal, and group activity streams with threaded commenting, direct posting, favoriting and @mentions, all with full RSS feed and email notification support.', 'buddypress' )
     374        ),
     375        'groups'   => array(
     376            'title'       => __( 'User Groups', 'buddypress' ),
     377            'description' => __( 'Groups allow your users to organize themselves into specific public, private or hidden sections with separate activity streams and member listings.', 'buddypress' )
     378        ),
     379        'forums'   => array(
     380            'title'       => __( 'Group Forums (Legacy)', 'buddypress' ),
     381            'description' => __( 'Group forums allow for focused, bulletin-board style conversations.', 'buddypress' )
     382        ),
     383        'blogs'    => array(
     384            'title'       => __( 'Site Tracking', 'buddypress' ),
     385            'description' => __( 'Record activity for new posts and comments from your site.', 'buddypress' )
     386        )
     387    );
     388
     389
     390    // Add blogs tracking if multisite
     391    if ( is_multisite() ) {
     392        $optional_components['blogs']['description'] = __( 'Record activity for new sites, posts, and comments across your network.', 'buddypress' );
     393    }
     394
     395    switch ( $type ) {
     396        case 'required' :
     397            $components = $required_components;
     398            break;
     399        case 'optional' :
     400            $components = $optional_components;
     401            break;
     402        case 'retired' :
     403            $components = $retired_components;
     404            break;
     405        case 'all' :
     406        default :
     407            $components = array_merge( $required_components, $optional_components, $retired_components );
     408            break;
     409
     410    }
     411
     412    return $components;
     413}
Note: See TracChangeset for help on using the changeset viewer.