Skip to:
Content

BuddyPress.org

Changeset 11926


Ignore:
Timestamp:
04/02/2018 02:58:28 AM (7 years ago)
Author:
r-a-y
Message:

Settings: Conditionally load action and screen functions.

This commit conditionally loads action and screen function code for the
Settings component, utilizing the 'bp_late_include' hook introduced in
r11884.

Previously, we loaded these functions at all times, which is unnecessary
when a user is not on a BuddyPress settings page. Now, we only load this
code when needed.

See #7218.

Location:
trunk
Files:
9 added
1 deleted
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-settings/actions/general.php

    r11925 r11926  
    11<?php
    22/**
    3  * BuddyPress Settings Actions
    4  *
    5  * @todo split actions into separate screen functions
     3 * Settings: Email address and password action handler
     4 *
    65 * @package BuddyPress
    76 * @subpackage SettingsActions
    8  * @since 1.5.0
     7 * @since 3.0.0
    98 */
    10 
    11 // Exit if accessed directly.
    12 defined( 'ABSPATH' ) || exit;
    139
    1410/**
     
    256252
    257253/**
    258  * Handles the changing and saving of user notification settings.
    259  *
    260  * @since 1.6.0
    261  */
    262 function bp_settings_action_notifications() {
    263     if ( ! bp_is_post_request() ) {
    264         return;
    265     }
    266 
    267     // Bail if no submit action.
    268     if ( ! isset( $_POST['submit'] ) ) {
    269         return;
    270     }
    271 
    272     // Bail if not in settings.
    273     if ( ! bp_is_settings_component() || ! bp_is_current_action( 'notifications' ) ) {
    274         return false;
    275     }
    276 
    277     // 404 if there are any additional action variables attached
    278     if ( bp_action_variables() ) {
    279         bp_do_404();
    280         return;
    281     }
    282 
    283     check_admin_referer( 'bp_settings_notifications' );
    284 
    285     bp_settings_update_notification_settings( bp_displayed_user_id(), (array) $_POST['notifications'] );
    286 
    287     // Switch feedback for super admins.
    288     if ( bp_is_my_profile() ) {
    289         bp_core_add_message( __( 'Your notification settings have been saved.',        'buddypress' ), 'success' );
    290     } else {
    291         bp_core_add_message( __( "This user's notification settings have been saved.", 'buddypress' ), 'success' );
    292     }
    293 
    294     /**
    295      * Fires after the notification settings have been saved, and before redirect.
    296      *
    297      * @since 1.5.0
    298      */
    299     do_action( 'bp_core_notification_settings_after_save' );
    300 
    301     bp_core_redirect( bp_displayed_user_domain() . bp_get_settings_slug() . '/notifications/' );
    302 }
    303 add_action( 'bp_actions', 'bp_settings_action_notifications' );
    304 
    305 /**
    306  * Handles the setting of user capabilities, spamming, hamming, role, etc...
    307  *
    308  * @since 1.6.0
    309  */
    310 function bp_settings_action_capabilities() {
    311     if ( ! bp_is_post_request() ) {
    312         return;
    313     }
    314 
    315     // Bail if no submit action.
    316     if ( ! isset( $_POST['capabilities-submit'] ) ) {
    317         return;
    318     }
    319 
    320     // Bail if not in settings.
    321     if ( ! bp_is_settings_component() || ! bp_is_current_action( 'capabilities' ) ) {
    322         return false;
    323     }
    324 
    325     // 404 if there are any additional action variables attached
    326     if ( bp_action_variables() ) {
    327         bp_do_404();
    328         return;
    329     }
    330 
    331     // Only super admins can currently spam users (but they can't spam
    332     // themselves).
    333     if ( ! is_super_admin() || bp_is_my_profile() ) {
    334         return;
    335     }
    336 
    337     // Nonce check.
    338     check_admin_referer( 'capabilities' );
    339 
    340     /**
    341      * Fires before the capabilities settings have been saved.
    342      *
    343      * @since 1.6.0
    344      */
    345     do_action( 'bp_settings_capabilities_before_save' );
    346 
    347     /* Spam **************************************************************/
    348 
    349     $is_spammer = !empty( $_POST['user-spammer'] ) ? true : false;
    350 
    351     if ( bp_is_user_spammer( bp_displayed_user_id() ) != $is_spammer ) {
    352         $status = ( true == $is_spammer ) ? 'spam' : 'ham';
    353         bp_core_process_spammer_status( bp_displayed_user_id(), $status );
    354 
    355         /**
    356          * Fires after processing a user as a spammer.
    357          *
    358          * @since 1.1.0
    359          *
    360          * @param int    $value  ID of the currently displayed user.
    361          * @param string $status Determined status of "spam" or "ham" for the displayed user.
    362          */
    363         do_action( 'bp_core_action_set_spammer_status', bp_displayed_user_id(), $status );
    364     }
    365 
    366     /* Other *************************************************************/
    367 
    368     /**
    369      * Fires after the capabilities settings have been saved and before redirect.
    370      *
    371      * @since 1.6.0
    372      */
    373     do_action( 'bp_settings_capabilities_after_save' );
    374 
    375     // Redirect to the root domain.
    376     bp_core_redirect( bp_displayed_user_domain() . bp_get_settings_slug() . '/capabilities/' );
    377 }
    378 add_action( 'bp_actions', 'bp_settings_action_capabilities' );
    379 
    380 /**
    381  * Handles the deleting of a user.
    382  *
    383  * @since 1.6.0
    384  */
    385 function bp_settings_action_delete_account() {
    386     if ( ! bp_is_post_request() ) {
    387         return;
    388     }
    389 
    390     // Bail if no submit action.
    391     if ( ! isset( $_POST['delete-account-understand'] ) ) {
    392         return;
    393     }
    394 
    395     // Bail if not in settings.
    396     if ( ! bp_is_settings_component() || ! bp_is_current_action( 'delete-account' ) ) {
    397         return false;
    398     }
    399 
    400     // 404 if there are any additional action variables attached
    401     if ( bp_action_variables() ) {
    402         bp_do_404();
    403         return;
    404     }
    405 
    406     // Bail if account deletion is disabled.
    407     if ( bp_disable_account_deletion() && ! bp_current_user_can( 'delete_users' ) ) {
    408         return false;
    409     }
    410 
    411     // Nonce check.
    412     check_admin_referer( 'delete-account' );
    413 
    414     // Get username now because it might be gone soon!
    415     $username = bp_get_displayed_user_fullname();
    416 
    417     // Delete the users account.
    418     if ( bp_core_delete_account( bp_displayed_user_id() ) ) {
    419 
    420         // Add feedback after deleting a user.
    421         bp_core_add_message( sprintf( __( '%s was successfully deleted.', 'buddypress' ), $username ), 'success' );
    422 
    423         // Redirect to the root domain.
    424         bp_core_redirect( bp_get_root_domain() );
    425     }
    426 }
    427 add_action( 'bp_actions', 'bp_settings_action_delete_account' );
    428 
    429 /**
    430254 * Process email change verification or cancel requests.
    431255 *
     
    486310}
    487311add_action( 'bp_actions', 'bp_settings_verify_email_change' );
    488 
    489 /**
    490  * Removes 'Email' sub nav, if no component has registered options there.
    491  *
    492  * @since 2.2.0
    493  */
    494 function bp_settings_remove_email_subnav() {
    495     if ( ! has_action( 'bp_notification_settings' ) ) {
    496         bp_core_remove_subnav_item( BP_SETTINGS_SLUG, 'notifications' );
    497     }
    498 }
    499 add_action( 'bp_actions', 'bp_settings_remove_email_subnav' );
  • trunk/src/bp-settings/classes/class-bp-settings-component.php

    r10555 r11926  
    4343    public function includes( $includes = array() ) {
    4444        parent::includes( array(
    45             'actions',
    46             'screens',
    4745            'template',
    4846            'functions',
    4947        ) );
     48    }
     49
     50    /**
     51     * Late includes method.
     52     *
     53     * Only load up certain code when on specific pages.
     54     *
     55     * @since 3.0.0
     56     */
     57    public function late_includes() {
     58        // Bail if PHPUnit is running.
     59        if ( defined( 'BP_TESTS_DIR' ) ) {
     60            return;
     61        }
     62
     63        // Bail if not on Settings component.
     64        if ( ! bp_is_settings_component() ) {
     65            return;
     66        }
     67
     68        $actions = array( 'notifications', 'capabilities', 'delete-account' );
     69
     70        // Authenticated actions.
     71        if ( is_user_logged_in() ) {
     72            if ( bp_is_current_action( 'general' ) ) {
     73                require $this->path . 'bp-settings/screens/general.php';
     74
     75            // Specific to post requests.
     76            } elseif ( bp_is_post_request() && in_array( bp_current_action(), $actions, true ) ) {
     77                require $this->path . 'bp-settings/actions/' . bp_current_action() . '.php';
     78            }
     79        }
     80
     81        // Screens - User profile integration.
     82        if ( bp_is_user() ) {
     83            require $this->path . 'bp-settings/screens/general.php';
     84
     85            // Sub-nav items.
     86            if ( in_array( bp_current_action(), $actions, true ) ) {
     87                require $this->path . 'bp-settings/screens/' . bp_current_action() . '.php';
     88            }
     89        }
    5090    }
    5191
  • trunk/tests/phpunit/includes/loader.php

    r11925 r11926  
    2424 * loaded at the same time to prevent weird load order issues.
    2525 */
    26 $components = array( 'activity', 'groups', 'messages' );
     26$components = array( 'activity', 'groups', 'messages', 'settings' );
    2727foreach ( $components as $component ) {
    2828    add_action( "bp_{$component}_includes", function() use ( $component ) {
Note: See TracChangeset for help on using the changeset viewer.