Skip to:
Content

BuddyPress.org

Changeset 11928


Ignore:
Timestamp:
04/02/2018 04:32:31 AM (8 years ago)
Author:
r-a-y
Message:

XProfile: Conditionally load action and screen functions.

This commit conditionally loads action and screen function code for the
XProfile 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 extended profile page. Now, we only load
this code when needed.

See #7218.

Location:
trunk
Files:
6 added
2 edited
2 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-component.php

    r11360 r11928  
    6868                        'cssjs',
    6969                        'cache',
    70                         'actions',
    7170                        'activity',
    72                         'screens',
    7371                        'caps',
    7472                        'filters',
     
    8482
    8583                parent::includes( $includes );
     84        }
     85
     86        /**
     87         * Late includes method.
     88         *
     89         * Only load up certain code when on specific pages.
     90         *
     91         * @since 3.0.0
     92         */
     93        public function late_includes() {
     94                // Bail if PHPUnit is running.
     95                if ( defined( 'BP_TESTS_DIR' ) ) {
     96                        return;
     97                }
     98
     99                // Bail if not on a user page.
     100                if ( ! bp_is_user() ) {
     101                        return;
     102                }
     103
     104                // User nav.
     105                if ( bp_is_profile_component() ) {
     106                        require $this->path . 'bp-xprofile/screens/public.php';
     107
     108                        // Action - Delete avatar.
     109                        if ( is_user_logged_in()&& bp_is_user_change_avatar() && bp_is_action_variable( 'delete-avatar', 0 ) ) {
     110                                require $this->path . 'bp-xprofile/actions/delete-avatar.php';
     111                        }
     112
     113                        // Sub-nav items.
     114                        if ( is_user_logged_in() &&
     115                                in_array( bp_current_action(), array( 'edit', 'change-avatar', 'change-cover-image' ), true )
     116                        ) {
     117                                require $this->path . 'bp-xprofile/screens/' . bp_current_action() . '.php';
     118                        }
     119                }
     120
     121                // Settings.
     122                if ( is_user_logged_in() && bp_is_user_settings_profile() ) {
     123                        require $this->path . 'bp-xprofile/screens/settings-profile.php';
     124                }
    86125        }
    87126
  • trunk/src/bp-xprofile/screens/edit.php

    r11927 r11928  
    11<?php
    22/**
    3  * BuddyPress XProfile Screens.
    4  *
    5  * Screen functions are the controllers of BuddyPress. They will execute when
    6  * their specific URL is caught. They will first save or manipulate data using
    7  * business functions, then pass on the user to a template file.
     3 * XProfile: User's "Profile > Edit" screen handler
    84 *
    95 * @package BuddyPress
    106 * @subpackage XProfileScreens
    11  * @since 1.5.0
     7 * @since 3.0.0
    128 */
    13 
    14 // Exit if accessed directly.
    15 defined( 'ABSPATH' ) || exit;
    16 
    17 /**
    18  * Handles the display of the profile page by loading the correct template file.
    19  *
    20  * @since 1.0.0
    21  *
    22  */
    23 function xprofile_screen_display_profile() {
    24         $new = isset( $_GET['new'] ) ? $_GET['new'] : '';
    25 
    26         /**
    27          * Fires right before the loading of the XProfile screen template file.
    28          *
    29          * @since 1.0.0
    30          *
    31          * @param string $new $_GET parameter holding the "new" parameter.
    32          */
    33         do_action( 'xprofile_screen_display_profile', $new );
    34 
    35         /**
    36          * Filters the template to load for the XProfile screen.
    37          *
    38          * @since 1.0.0
    39          *
    40          * @param string $template Path to the XProfile template to load.
    41          */
    42         bp_core_load_template( apply_filters( 'xprofile_template_display_profile', 'members/single/home' ) );
    43 }
    449
    4510/**
     
    190155        bp_core_load_template( apply_filters( 'xprofile_template_edit_profile', 'members/single/home' ) );
    191156}
    192 
    193 /**
    194  * Handles the uploading and cropping of a user avatar. Displays the change avatar page.
    195  *
    196  * @since 1.0.0
    197  *
    198  */
    199 function xprofile_screen_change_avatar() {
    200 
    201         // Bail if not the correct screen.
    202         if ( ! bp_is_my_profile() && ! bp_current_user_can( 'bp_moderate' ) ) {
    203                 return false;
    204         }
    205 
    206         // Bail if there are action variables.
    207         if ( bp_action_variables() ) {
    208                 bp_do_404();
    209                 return;
    210         }
    211 
    212         $bp = buddypress();
    213 
    214         if ( ! isset( $bp->avatar_admin ) ) {
    215                 $bp->avatar_admin = new stdClass();
    216         }
    217 
    218         $bp->avatar_admin->step = 'upload-image';
    219 
    220         if ( !empty( $_FILES ) ) {
    221 
    222                 // Check the nonce.
    223                 check_admin_referer( 'bp_avatar_upload' );
    224 
    225                 // Pass the file to the avatar upload handler.
    226                 if ( bp_core_avatar_handle_upload( $_FILES, 'xprofile_avatar_upload_dir' ) ) {
    227                         $bp->avatar_admin->step = 'crop-image';
    228 
    229                         // Make sure we include the jQuery jCrop file for image cropping.
    230                         add_action( 'wp_print_scripts', 'bp_core_add_jquery_cropper' );
    231                 }
    232         }
    233 
    234         // If the image cropping is done, crop the image and save a full/thumb version.
    235         if ( isset( $_POST['avatar-crop-submit'] ) ) {
    236 
    237                 // Check the nonce.
    238                 check_admin_referer( 'bp_avatar_cropstore' );
    239 
    240                 $args = array(
    241                         'item_id'       => bp_displayed_user_id(),
    242                         'original_file' => $_POST['image_src'],
    243                         'crop_x'        => $_POST['x'],
    244                         'crop_y'        => $_POST['y'],
    245                         'crop_w'        => $_POST['w'],
    246                         'crop_h'        => $_POST['h']
    247                 );
    248 
    249                 if ( ! bp_core_avatar_handle_crop( $args ) ) {
    250                         bp_core_add_message( __( 'There was a problem cropping your profile photo.', 'buddypress' ), 'error' );
    251                 } else {
    252 
    253                         /**
    254                          * Fires right before the redirect, after processing a new avatar.
    255                          *
    256                          * @since 1.1.0
    257                          * @since 2.3.4 Add two new parameters to inform about the user id and
    258                          *              about the way the avatar was set (eg: 'crop' or 'camera').
    259                          *
    260                          * @param string $item_id Inform about the user id the avatar was set for.
    261                          * @param string $value   Inform about the way the avatar was set ('crop').
    262                          */
    263                         do_action( 'xprofile_avatar_uploaded', (int) $args['item_id'], 'crop' );
    264                         bp_core_add_message( __( 'Your new profile photo was uploaded successfully.', 'buddypress' ) );
    265                         bp_core_redirect( bp_displayed_user_domain() );
    266                 }
    267         }
    268 
    269         /**
    270          * Fires right before the loading of the XProfile change avatar screen template file.
    271          *
    272          * @since 1.0.0
    273          */
    274         do_action( 'xprofile_screen_change_avatar' );
    275 
    276         /**
    277          * Filters the template to load for the XProfile change avatar screen.
    278          *
    279          * @since 1.0.0
    280          *
    281          * @param string $template Path to the XProfile change avatar template to load.
    282          */
    283         bp_core_load_template( apply_filters( 'xprofile_template_change_avatar', 'members/single/home' ) );
    284 }
    285 
    286 /**
    287  * Displays the change cover image page.
    288  *
    289  * @since 2.4.0
    290  */
    291 function xprofile_screen_change_cover_image() {
    292 
    293         // Bail if not the correct screen.
    294         if ( ! bp_is_my_profile() && ! bp_current_user_can( 'bp_moderate' ) ) {
    295                 return false;
    296         }
    297 
    298         /**
    299          * Fires right before the loading of the XProfile change cover image screen template file.
    300          *
    301          * @since 2.4.0
    302          */
    303         do_action( 'xprofile_screen_change_cover_image' );
    304 
    305         /**
    306          * Filters the template to load for the XProfile cover image screen.
    307          *
    308          * @since 2.4.0
    309          *
    310          * @param string $template Path to the XProfile cover image template to load.
    311          */
    312         bp_core_load_template( apply_filters( 'xprofile_template_cover_image', 'members/single/home' ) );
    313 }
    314 
    315 /**
    316  * Show the xprofile settings template.
    317  *
    318  * @since 2.0.0
    319  */
    320 function bp_xprofile_screen_settings() {
    321 
    322         // Redirect if no privacy settings page is accessible.
    323         if ( bp_action_variables() || ! bp_is_active( 'xprofile' ) ) {
    324                 bp_do_404();
    325                 return;
    326         }
    327 
    328         /**
    329          * Filters the template to load for the XProfile settings screen.
    330          *
    331          * @since 2.0.0
    332          *
    333          * @param string $template Path to the XProfile change avatar template to load.
    334          */
    335         bp_core_load_template( apply_filters( 'bp_settings_screen_xprofile', '/members/single/settings/profile' ) );
    336 }
  • trunk/src/bp-xprofile/screens/settings-profile.php

    r11927 r11928  
    11<?php
    22/**
    3  * BuddyPress XProfile Actions.
    4  *
    5  * Action functions are exactly the same as screen functions, however they do not
    6  * have a template screen associated with them. Usually they will send the user
    7  * back to the default screen after execution.
     3 * XProfile: User's "Settings > Profile Visibility" screen handler
    84 *
    95 * @package BuddyPress
    10  * @subpackage XProfileActions
    11  * @since 1.5.0
     6 * @subpackage XProfileScreens
     7 * @since 3.0.0
    128 */
    139
    14 // Exit if accessed directly.
    15 defined( 'ABSPATH' ) || exit;
     10/**
     11 * Show the xprofile settings template.
     12 *
     13 * @since 2.0.0
     14 */
     15function bp_xprofile_screen_settings() {
    1616
    17 /**
    18  * This function runs when an action is set for a screen:
    19  * example.com/members/andy/profile/change-avatar/ [delete-avatar]
    20  *
    21  * The function will delete the active avatar for a user.
    22  *
    23  * @since 1.0.0
    24  *
    25  */
    26 function xprofile_action_delete_avatar() {
    27 
    28         if ( ! bp_is_user_change_avatar() || ! bp_is_action_variable( 'delete-avatar', 0 ) ) {
    29                 return false;
     17        // Redirect if no privacy settings page is accessible.
     18        if ( bp_action_variables() || ! bp_is_active( 'xprofile' ) ) {
     19                bp_do_404();
     20                return;
    3021        }
    3122
    32         // Check the nonce.
    33         check_admin_referer( 'bp_delete_avatar_link' );
    34 
    35         if ( ! bp_is_my_profile() && ! bp_current_user_can( 'bp_moderate' ) ) {
    36                 return false;
    37         }
    38 
    39         if ( bp_core_delete_existing_avatar( array( 'item_id' => bp_displayed_user_id() ) ) ) {
    40                 bp_core_add_message( __( 'Your profile photo was deleted successfully!', 'buddypress' ) );
    41         } else {
    42                 bp_core_add_message( __( 'There was a problem deleting your profile photo. Please try again.', 'buddypress' ), 'error' );
    43         }
    44 
    45         bp_core_redirect( wp_get_referer() );
     23        /**
     24         * Filters the template to load for the XProfile settings screen.
     25         *
     26         * @since 2.0.0
     27         *
     28         * @param string $template Path to the XProfile change avatar template to load.
     29         */
     30        bp_core_load_template( apply_filters( 'bp_settings_screen_xprofile', '/members/single/settings/profile' ) );
    4631}
    47 add_action( 'bp_actions', 'xprofile_action_delete_avatar' );
    4832
    4933/**
  • trunk/tests/phpunit/includes/loader.php

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