Skip to:
Content

BuddyPress.org

Changeset 11931


Ignore:
Timestamp:
04/02/2018 04:43:02 PM (7 years ago)
Author:
r-a-y
Message:

Friends: Conditionally load action and screen functions.

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

See #7218.

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

Legend:

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

    r11360 r11931  
    4848        $includes = array(
    4949            'cache',
    50             'actions',
    51             'screens',
    5250            'filters',
    5351            'activity',
     
    6361
    6462        parent::includes( $includes );
     63    }
     64
     65    /**
     66     * Late includes method.
     67     *
     68     * Only load up certain code when on specific pages.
     69     *
     70     * @since 3.0.0
     71     */
     72    public function late_includes() {
     73        // Bail if PHPUnit is running.
     74        if ( defined( 'BP_TESTS_DIR' ) ) {
     75            return;
     76        }
     77
     78        // Friends.
     79        if ( bp_is_user_friends() ) {
     80            // Authenticated actions.
     81            if ( is_user_logged_in() &&
     82                in_array( bp_current_action(), array( 'add-friend', 'remove-friend' ), true )
     83            ) {
     84                require $this->path . 'bp-friends/actions/' . bp_current_action() . '.php';
     85            }
     86
     87            // User nav.
     88            require $this->path . 'bp-friends/screens/my-friends.php';
     89            if ( is_user_logged_in() && bp_is_user_friend_requests() ) {
     90                require $this->path . 'bp-friends/screens/requests.php';
     91            }
     92        }
     93
     94        // Settings.
     95        if ( bp_is_user_settings_notifications() ) {
     96            require $this->path . 'bp-friends/screens/settings-email.php';
     97        }
    6598    }
    6699
  • trunk/src/bp-friends/screens/settings-email.php

    r11930 r11931  
    11<?php
    22/**
    3  * BuddyPress Friends Screen Functions.
    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 * Friends: Integration into user's "Settings > Email" screen
    84 *
    95 * @package BuddyPress
    106 * @subpackage FriendsScreens
    11  * @since 1.5.0
     7 * @since 3.0.0
    128 */
    13 
    14 // Exit if accessed directly.
    15 defined( 'ABSPATH' ) || exit;
    16 
    17 /**
    18  * Catch and process the My Friends page.
    19  *
    20  * @since 1.0.0
    21  */
    22 function friends_screen_my_friends() {
    23 
    24     /**
    25      * Fires before the loading of template for the My Friends page.
    26      *
    27      * @since 1.0.0
    28      */
    29     do_action( 'friends_screen_my_friends' );
    30 
    31     /**
    32      * Filters the template used to display the My Friends page.
    33      *
    34      * @since 1.0.0
    35      *
    36      * @param string $template Path to the my friends template to load.
    37      */
    38     bp_core_load_template( apply_filters( 'friends_template_my_friends', 'members/single/home' ) );
    39 }
    40 
    41 /**
    42  * Catch and process the Requests page.
    43  *
    44  * @since 1.0.0
    45  */
    46 function friends_screen_requests() {
    47     if ( bp_is_action_variable( 'accept', 0 ) && is_numeric( bp_action_variable( 1 ) ) ) {
    48         // Check the nonce.
    49         check_admin_referer( 'friends_accept_friendship' );
    50 
    51         if ( friends_accept_friendship( bp_action_variable( 1 ) ) )
    52             bp_core_add_message( __( 'Friendship accepted', 'buddypress' ) );
    53         else
    54             bp_core_add_message( __( 'Friendship could not be accepted', 'buddypress' ), 'error' );
    55 
    56         bp_core_redirect( trailingslashit( bp_loggedin_user_domain() . bp_current_component() . '/' . bp_current_action() ) );
    57 
    58     } elseif ( bp_is_action_variable( 'reject', 0 ) && is_numeric( bp_action_variable( 1 ) ) ) {
    59         // Check the nonce.
    60         check_admin_referer( 'friends_reject_friendship' );
    61 
    62         if ( friends_reject_friendship( bp_action_variable( 1 ) ) )
    63             bp_core_add_message( __( 'Friendship rejected', 'buddypress' ) );
    64         else
    65             bp_core_add_message( __( 'Friendship could not be rejected', 'buddypress' ), 'error' );
    66 
    67         bp_core_redirect( trailingslashit( bp_loggedin_user_domain() . bp_current_component() . '/' . bp_current_action() ) );
    68 
    69     } elseif ( bp_is_action_variable( 'cancel', 0 ) && is_numeric( bp_action_variable( 1 ) ) ) {
    70         // Check the nonce.
    71         check_admin_referer( 'friends_withdraw_friendship' );
    72 
    73         if ( friends_withdraw_friendship( bp_loggedin_user_id(), bp_action_variable( 1 ) ) )
    74             bp_core_add_message( __( 'Friendship request withdrawn', 'buddypress' ) );
    75         else
    76             bp_core_add_message( __( 'Friendship request could not be withdrawn', 'buddypress' ), 'error' );
    77 
    78         bp_core_redirect( trailingslashit( bp_loggedin_user_domain() . bp_current_component() . '/' . bp_current_action() ) );
    79     }
    80 
    81     /**
    82      * Fires before the loading of template for the friends requests page.
    83      *
    84      * @since 1.0.0
    85      */
    86     do_action( 'friends_screen_requests' );
    87 
    88     /**
    89      * Filters the template used to display the My Friends page.
    90      *
    91      * @since 1.0.0
    92      *
    93      * @param string $template Path to the friends request template to load.
    94      */
    95     bp_core_load_template( apply_filters( 'friends_template_requests', 'members/single/home' ) );
    96 }
    979
    9810/**
  • trunk/tests/phpunit/includes/loader.php

    r11928 r11931  
    2424 * loaded at the same time to prevent weird load order issues.
    2525 */
    26 $components = array( 'activity', 'groups', 'members', 'messages', 'settings', 'xprofile' );
     26$components = array( 'activity', 'friends', '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.