Skip to:
Content

BuddyPress.org

Changeset 11923


Ignore:
Timestamp:
04/01/2018 11:30:48 PM (20 months ago)
Author:
r-a-y
Message:

Groups: Conditionally load action and screen functions.

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

See #7218.

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

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/actions/create.php

    r11922 r11923  
    11<?php
    22/**
    3  * BuddyPress Groups Actions.
    4  *
    5  * Action functions are exactly the same as screen functions, however they do
    6  * not have a template screen associated with them. Usually they will send the
    7  * user back to the default screen after execution.
     3 * Groups: Create action
    84 *
    95 * @package BuddyPress
    10  * @subpackage GroupsActions
    11  * @since 1.5.0
     6 * @subpackage GroupActions
     7 * @since 3.0.0
    128 */
    13 
    14 // Exit if accessed directly.
    15 defined( 'ABSPATH' ) || exit;
    16 
    17 /**
    18  * Protect access to single groups.
    19  *
    20  * @since 2.1.0
    21  */
    22 function bp_groups_group_access_protection() {
    23     if ( ! bp_is_group() ) {
    24         return;
    25     }
    26 
    27     $current_group   = groups_get_current_group();
    28     $user_has_access = $current_group->user_has_access;
    29     $is_visible      = $current_group->is_visible;
    30     $no_access_args  = array();
    31 
    32     // The user can know about the group but doesn't have full access.
    33     if ( ! $user_has_access && $is_visible ) {
    34         // Always allow access to home and request-membership.
    35         if ( bp_is_current_action( 'home' ) || bp_is_current_action( 'request-membership' ) ) {
    36             $user_has_access = true;
    37 
    38         // User doesn't have access, so set up redirect args.
    39         } elseif ( is_user_logged_in() ) {
    40             $no_access_args = array(
    41                 'message'  => __( 'You do not have access to this group.', 'buddypress' ),
    42                 'root'     => bp_get_group_permalink( $current_group ) . 'home/',
    43                 'redirect' => false
    44             );
    45         }
    46     }
    47 
    48     // Protect the admin tab from non-admins.
    49     if ( bp_is_current_action( 'admin' ) && ! bp_is_item_admin() ) {
    50         $user_has_access = false;
    51         $no_access_args  = array(
    52             'message'  => __( 'You are not an admin of this group.', 'buddypress' ),
    53             'root'     => bp_get_group_permalink( $current_group ),
    54             'redirect' => false
    55         );
    56     }
    57 
    58     /**
    59      * Allow plugins to filter whether the current user has access to this group content.
    60      *
    61      * Note that if a plugin sets $user_has_access to false, it may also
    62      * want to change the $no_access_args, to avoid problems such as
    63      * logged-in users being redirected to wp-login.php.
    64      *
    65      * @since 2.1.0
    66      *
    67      * @param bool  $user_has_access True if the user has access to the
    68      *                               content, otherwise false.
    69      * @param array $no_access_args  Arguments to be passed to bp_core_no_access() in case
    70      *                               of no access. Note that this value is passed by reference,
    71      *                               so it can be modified by the filter callback.
    72      */
    73     $user_has_access = apply_filters_ref_array( 'bp_group_user_has_access', array( $user_has_access, &$no_access_args ) );
    74 
    75     // If user has access, we return rather than redirect.
    76     if ( $user_has_access ) {
    77         return;
    78     }
    79 
    80     // Groups that the user cannot know about should return a 404 for non-members.
    81     // Unset the current group so that you're not redirected
    82     // to the default group tab.
    83     if ( ! $is_visible ) {
    84         buddypress()->groups->current_group = 0;
    85         buddypress()->is_single_item        = false;
    86         bp_do_404();
    87         return;
    88     } else {
    89         bp_core_no_access( $no_access_args );
    90     }
    91 
    92 }
    93 add_action( 'bp_actions', 'bp_groups_group_access_protection' );
    949
    9510/**
     
    385300
    386301/**
    387  * Catch and process "Join Group" button clicks.
    388  *
    389  * @since 1.0.0
    390  *
    391  * @return bool
    392  */
    393 function groups_action_join_group() {
    394 
    395     if ( !bp_is_single_item() || !bp_is_groups_component() || !bp_is_current_action( 'join' ) )
    396         return false;
    397 
    398     // Nonce check.
    399     if ( !check_admin_referer( 'groups_join_group' ) )
    400         return false;
    401 
    402     $bp = buddypress();
    403 
    404     // Skip if banned or already a member.
    405     if ( !groups_is_user_member( bp_loggedin_user_id(), $bp->groups->current_group->id ) && !groups_is_user_banned( bp_loggedin_user_id(), $bp->groups->current_group->id ) ) {
    406 
    407         // User wants to join a group that is not public.
    408         if ( bp_current_user_can( 'groups_join_group', array( 'group_id' => $bp->groups->current_group->id ) ) ) {
    409             if ( !groups_check_user_has_invite( bp_loggedin_user_id(), $bp->groups->current_group->id ) ) {
    410                 bp_core_add_message( __( 'There was an error joining the group.', 'buddypress' ), 'error' );
    411                 bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) );
    412             }
    413         }
    414 
    415         // User wants to join any group.
    416         if ( !groups_join_group( $bp->groups->current_group->id ) )
    417             bp_core_add_message( __( 'There was an error joining the group.', 'buddypress' ), 'error' );
    418         else
    419             bp_core_add_message( __( 'You joined the group!', 'buddypress' ) );
    420 
    421         bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) );
    422     }
    423 
    424     /**
    425      * Filters the template to load for the single group screen.
    426      *
    427      * @since 1.0.0
    428      *
    429      * @param string $value Path to the single group template to load.
    430      */
    431     bp_core_load_template( apply_filters( 'groups_template_group_home', 'groups/single/home' ) );
    432 }
    433 add_action( 'bp_actions', 'groups_action_join_group' );
    434 
    435 /**
    436  * Catch and process "Leave Group" button clicks.
    437  *
    438  * When a group member clicks on the "Leave Group" button from a group's page,
    439  * this function is run.
    440  *
    441  * Note: When leaving a group from the group directory, AJAX is used and
    442  * another function handles this. See {@link bp_legacy_theme_ajax_joinleave_group()}.
    443  *
    444  * @since 1.2.4
    445  *
    446  * @return bool
    447  */
    448 function groups_action_leave_group() {
    449     if ( ! bp_is_single_item() || ! bp_is_groups_component() || ! bp_is_current_action( 'leave-group' ) ) {
    450         return false;
    451     }
    452 
    453     // Nonce check.
    454     if ( ! check_admin_referer( 'groups_leave_group' ) ) {
    455         return false;
    456     }
    457 
    458     // User wants to leave any group.
    459     if ( groups_is_user_member( bp_loggedin_user_id(), bp_get_current_group_id() ) ) {
    460         $bp = buddypress();
    461 
    462         // Stop sole admins from abandoning their group.
    463         $group_admins = groups_get_group_admins( bp_get_current_group_id() );
    464 
    465         if ( 1 == count( $group_admins ) && $group_admins[0]->user_id == bp_loggedin_user_id() ) {
    466             bp_core_add_message( __( 'This group must have at least one admin', 'buddypress' ), 'error' );
    467         } elseif ( ! groups_leave_group( $bp->groups->current_group->id ) ) {
    468             bp_core_add_message( __( 'There was an error leaving the group.', 'buddypress' ), 'error' );
    469         } else {
    470             bp_core_add_message( __( 'You successfully left the group.', 'buddypress' ) );
    471         }
    472 
    473         $group = groups_get_current_group();
    474         $redirect = bp_get_group_permalink( $group );
    475 
    476         if ( ! $group->is_visible ) {
    477             $redirect = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() );
    478         }
    479 
    480         bp_core_redirect( $redirect );
    481     }
    482 
    483     /** This filter is documented in bp-groups/bp-groups-actions.php */
    484     bp_core_load_template( apply_filters( 'groups_template_group_home', 'groups/single/home' ) );
    485 }
    486 add_action( 'bp_actions', 'groups_action_leave_group' );
    487 
    488 /**
    489302 * Sort the group creation steps.
    490303 *
     
    524337    do_action( 'groups_action_sort_creation_steps' );
    525338}
    526 
    527 /**
    528  * Catch requests for a random group page (example.com/groups/?random-group) and redirect.
    529  *
    530  * @since 1.2.0
    531  */
    532 function groups_action_redirect_to_random_group() {
    533 
    534     if ( bp_is_groups_component() && isset( $_GET['random-group'] ) ) {
    535         $group = BP_Groups_Group::get_random( 1, 1 );
    536 
    537         bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . $group['groups'][0]->slug ) );
    538     }
    539 }
    540 add_action( 'bp_actions', 'groups_action_redirect_to_random_group' );
    541 
    542 /**
    543  * Load the activity feed for the current group.
    544  *
    545  * @since 1.2.0
    546  *
    547  * @return false|null False on failure.
    548  */
    549 function groups_action_group_feed() {
    550 
    551     // Get current group.
    552     $group = groups_get_current_group();
    553 
    554     if ( ! bp_is_active( 'activity' ) || ! bp_is_groups_component() || ! $group || ! bp_is_current_action( 'feed' ) )
    555         return false;
    556 
    557     // If group isn't public or if logged-in user is not a member of the group, do
    558     // not output the group activity feed.
    559     if ( ! bp_group_is_visible( $group ) ) {
    560         return false;
    561     }
    562 
    563     // Set up the feed.
    564     buddypress()->activity->feed = new BP_Activity_Feed( array(
    565         'id'            => 'group',
    566 
    567         /* translators: Group activity RSS title - "[Site Name] | [Group Name] | Activity" */
    568         'title'         => sprintf( __( '%1$s | %2$s | Activity', 'buddypress' ), bp_get_site_name(), bp_get_current_group_name() ),
    569 
    570         'link'          => bp_get_group_permalink( $group ),
    571         'description'   => sprintf( __( "Activity feed for the group, %s.", 'buddypress' ), bp_get_current_group_name() ),
    572         'activity_args' => array(
    573             'object'           => buddypress()->groups->id,
    574             'primary_id'       => bp_get_current_group_id(),
    575             'display_comments' => 'threaded'
    576         )
    577     ) );
    578 }
    579 add_action( 'bp_actions', 'groups_action_group_feed' );
  • trunk/src/bp-groups/classes/class-bp-groups-component.php

    r11783 r11923  
    124124        $includes = array(
    125125            'cache',
    126             'actions',
    127126            'filters',
    128             'screens',
    129127            'widgets',
    130128            'activity',
     
    140138
    141139        parent::includes( $includes );
     140    }
     141
     142    /**
     143     * Late includes method.
     144     *
     145     * Only load up certain code when on specific pages.
     146     *
     147     * @since 3.0.0
     148     */
     149    public function late_includes() {
     150        // Bail if PHPUnit is running.
     151        if ( defined( 'BP_TESTS_DIR' ) ) {
     152            return;
     153        }
     154
     155        if ( bp_is_groups_component() ) {
     156            // Authenticated actions.
     157            if ( is_user_logged_in() &&
     158                in_array( bp_current_action(), array( 'create', 'join', 'leave-group' ), true )
     159            ) {
     160                require $this->path . 'bp-groups/actions/' . bp_current_action() . '.php';
     161            }
     162
     163            // Actions - RSS feed handler.
     164            if ( bp_is_active( 'activity' ) && bp_is_current_action( 'feed' ) ) {
     165                require $this->path . 'bp-groups/actions/feed.php';
     166            }
     167
     168            // Actions - Random group handler.
     169            if ( isset( $_GET['random-group'] ) ) {
     170                require $this->path . 'bp-groups/actions/random.php';
     171            }
     172
     173            // Screens - Directory.
     174            if ( bp_is_groups_directory() ) {
     175                require $this->path . 'bp-groups/screens/directory.php';
     176            }
     177
     178            // Screens - User profile integration.
     179            if ( bp_is_user() ) {
     180                require $this->path . 'bp-groups/screens/user/my-groups.php';
     181
     182                if ( bp_is_current_action( 'invites' ) ) {
     183                    require $this->path . 'bp-groups/screens/user/invites.php';
     184                }
     185            }
     186
     187            // Single group.
     188            if ( bp_is_group() ) {
     189                // Actions - Access protection.
     190                require $this->path . 'bp-groups/actions/access.php';
     191
     192                // Public nav items.
     193                if ( in_array( bp_current_action(), array( 'home', 'request-membership', 'activity', 'members', 'send-invites' ), true ) ) {
     194                    require $this->path . 'bp-groups/screens/single/' . bp_current_action() . '.php';
     195                }
     196
     197                // Admin nav items.
     198                if ( bp_is_item_admin() && is_user_logged_in() ) {
     199                    require $this->path . 'bp-groups/screens/single/admin.php';
     200
     201                    if ( in_array( bp_get_group_current_admin_tab(), array( 'edit-details', 'group-settings', 'group-avatar', 'group-cover-image', 'manage-members', 'membership-requests', 'delete-group' ), true ) ) {
     202                        require $this->path . 'bp-groups/screens/single/admin/' . bp_get_group_current_admin_tab() . '.php';
     203                    }
     204                }
     205            }
     206
     207            // Theme compatibility.
     208            new BP_Groups_Theme_Compat();
     209        }
     210
     211        // Groups notifications HTML table.
     212        if ( bp_is_user_settings_notifications() ) {
     213            require $this->path . 'bp-groups/screens/user/settings-email.php';
     214        }
    142215    }
    143216
  • trunk/tests/phpunit/includes/loader.php

    r11885 r11923  
    1919
    2020/**
    21  * Load up activity action and screen code.
     21 * Load up component action and screen code.
    2222 *
    2323 * In BuddyPress, this is loaded conditionally, but PHPUnit needs all files
    2424 * loaded at the same time to prevent weird load order issues.
    2525 */
    26 add_action( 'bp_activity_includes', function() {
    27     $dirs = array(
    28         buddypress()->plugin_dir . 'bp-activity/actions/',
    29         buddypress()->plugin_dir . 'bp-activity/screens/',
    30     );
     26$components = array( 'activity', 'groups' );
     27foreach ( $components as $component ) {
     28    add_action( "bp_{$component}_includes", function() use ( $component ) {
     29        $dirs = array(
     30            buddypress()->plugin_dir . 'bp-' . $component . '/actions/',
     31            buddypress()->plugin_dir . 'bp-' . $component . '/screens/',
     32        );
    3133
    32     foreach ( $dirs as $dir ) {
    33         foreach ( glob( $dir . "*.php" ) as $file ) {
    34             require $file;
     34        foreach ( $dirs as $dir ) {
     35            foreach ( glob( $dir . "*.php" ) as $file ) {
     36                require $file;
     37            }
    3538        }
    36     }
    37 } );
     39    } );
     40}
Note: See TracChangeset for help on using the changeset viewer.