Skip to:
Content

BuddyPress.org

Changeset 3818


Ignore:
Timestamp:
01/22/2011 01:53:24 PM (16 years ago)
Author:
johnjamesjacoby
Message:

Refactor Groups component to use BP_Component class. Also fix bp_nav conflict when viewing a single group. Split code out of bp-groups-loader into smaller files.

Location:
trunk/bp-groups
Files:
6 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-groups/bp-groups-loader.php

    r3806 r3818  
    11<?php
    22
    3 // Required Files
    4 require ( BP_PLUGIN_DIR . '/bp-groups/bp-groups-classes.php'  );
    5 require ( BP_PLUGIN_DIR . '/bp-groups/bp-groups-widgets.php'  );
    6 require ( BP_PLUGIN_DIR . '/bp-groups/bp-groups-filters.php'  );
    7 require ( BP_PLUGIN_DIR . '/bp-groups/bp-groups-template.php' );
    83
    94/**
    10  * Puts important groups component data into the $bp global for later use.
     5 * BuddyPress Groups Loader
    116 *
    12  * @package BuddyPress Groups
    13  * @global $bp The global BuddyPress settings variable created in bp_core_setup_globals()
     7 * A groups component, for users to group themselves together. Includes a
     8 * robust sub-component API that allows Groups to be extended.
     9 * Comes preconfigured with an activity stream, discussion forums, and settings.
     10 *
     11 * @package BuddyPress
     12 * @subpackage Groups Core
    1413 */
    15 function groups_setup_globals() {
    16         global $bp;
    17 
    18         // Define a slug, if necessary
    19         if ( !defined( 'BP_GROUPS_SLUG' ) )
    20                 define ( 'BP_GROUPS_SLUG', bp_core_component_slug_from_root_slug( $bp->pages->groups->slug ) );
    21 
    22         // For internal identification
    23         $bp->groups->id        = 'groups';
    24         $bp->groups->name      = $bp->pages->groups->name;
    25 
    26         // Slugs
    27         $bp->groups->slug      = BP_GROUPS_SLUG;
    28         $bp->groups->root_slug = $bp->pages->groups->slug;
    29 
    30         // Tables
    31         $bp->groups->table_name           = $bp->table_prefix . 'bp_groups';
    32         $bp->groups->table_name_members   = $bp->table_prefix . 'bp_groups_members';
    33         $bp->groups->table_name_groupmeta = $bp->table_prefix . 'bp_groups_groupmeta';
    34 
    35         // Notifications
    36         $bp->groups->notification_callback = 'groups_format_notifications';
    37 
    38         // Register this in the active components array
    39         $bp->active_components[$bp->groups->slug] = $bp->groups->id;
    40 
    41         $bp->groups->forbidden_names = apply_filters( 'groups_forbidden_names', array( 'my-groups', 'create', 'invites', 'send-invites', 'forum', 'delete', 'add', 'admin', 'request-membership', 'members', 'settings', 'avatar', BP_GROUPS_SLUG ) );
    42 
    43         $bp->groups->group_creation_steps = apply_filters( 'groups_create_group_steps', array(
    44                 'group-details'  => array( 'name' => __( 'Details', 'buddypress' ), 'position' => 0 ),
    45                 'group-settings' => array( 'name' => __( 'Settings', 'buddypress' ), 'position' => 10 ),
    46                 'group-avatar'   => array( 'name' => __( 'Avatar', 'buddypress' ), 'position' => 20 ),
    47         ) );
    48 
    49         if ( bp_is_active( 'friends' ) )
    50                 $bp->groups->group_creation_steps['group-invites'] = array( 'name' => __( 'Invites', 'buddypress' ), 'position' => 30 );
    51 
    52         $bp->groups->valid_status = apply_filters( 'groups_valid_status', array( 'public', 'private', 'hidden' ) );
    53 
    54         // Auto join group when non group member performs group activity
    55         $bp->groups->auto_join = defined( 'BP_DISABLE_AUTO_GROUP_JOIN' ) ? false : true;
    56 
    57         // The default text for the groups directory search box
    58         $bp->default_search_strings[$bp->groups->slug] = __( 'Search Groups...', 'buddypress' );
    59 
    60         do_action( 'groups_setup_globals' );
    61 }
    62 add_action( 'bp_setup_globals', 'groups_setup_globals' );
    63 
    64 function groups_setup_nav() {
    65         global $bp;
    66 
    67         if ( bp_is_current_component( $bp->groups->slug ) && $group_id = BP_Groups_Group::group_exists($bp->current_action) ) {
    68 
    69                 // This is a single group page.
    70                 $bp->is_single_item = true;
    71                 $bp->groups->current_group = new BP_Groups_Group( $group_id );
    72 
    73                 // Using "item" not "group" for generic support in other components.
    74                 if ( is_super_admin() )
    75                         $bp->is_item_admin = 1;
    76                 else
    77                         $bp->is_item_admin = groups_is_user_admin( $bp->loggedin_user->id, $bp->groups->current_group->id );
    78 
    79                 // If the user is not an admin, check if they are a moderator
    80                 if ( !$bp->is_item_admin )
    81                         $bp->is_item_mod = groups_is_user_mod( $bp->loggedin_user->id, $bp->groups->current_group->id );
    82 
    83                 // Is the logged in user a member of the group?
    84                 $bp->groups->current_group->is_user_member = ( is_user_logged_in() && groups_is_user_member( $bp->loggedin_user->id, $bp->groups->current_group->id ) ) ? true : false;
    85 
    86                 // Should this group be visible to the logged in user?
    87                 $bp->groups->current_group->is_group_visible_to_member = ( 'public' == $bp->groups->current_group->status || $bp->groups->current_group->is_user_member ) ? true : false;
     14
     15class BP_Groups_Component extends BP_Component {
     16
     17        /**
     18         * Start the groups component creation process
     19         *
     20         * @since BuddyPress {unknown}
     21         */
     22        function BP_Groups_Component() {
     23                parent::start( 'groups', __( 'User Groups', 'buddypress' ) );
    8824        }
    8925
    90         // Add 'Groups' to the main navigation
    91         bp_core_new_nav_item( array( 'name' => sprintf( __( 'Groups <span>(%d)</span>', 'buddypress' ), groups_total_groups_for_user() ), 'slug' => $bp->groups->slug, 'position' => 70, 'screen_function' => 'groups_screen_my_groups', 'default_subnav_slug' => 'my-groups', 'item_css_id' => $bp->groups->id ) );
    92 
    93         $groups_link = $bp->loggedin_user->domain . $bp->groups->slug . '/';
    94 
    95         // Add the subnav items to the groups nav item
    96         bp_core_new_subnav_item( array( 'name' => __( 'My Groups', 'buddypress' ), 'slug' => 'my-groups', 'parent_url' => $groups_link, 'parent_slug' => $bp->groups->slug, 'screen_function' => 'groups_screen_my_groups',     'position' => 10, 'item_css_id'     => 'groups-my-groups' ) );
    97         bp_core_new_subnav_item( array( 'name' => __( 'Invites',   'buddypress' ), 'slug' => 'invites',   'parent_url' => $groups_link, 'parent_slug' => $bp->groups->slug, 'screen_function' => 'groups_screen_group_invites', 'position' => 30, 'user_has_access' => bp_is_my_profile() ) );
    98 
    99         if ( bp_is_current_component( $bp->groups->slug ) ) {
    100 
    101                 if ( bp_is_my_profile() && !$bp->is_single_item ) {
    102 
    103                         $bp->bp_options_title = __( 'My Groups', 'buddypress' );
    104 
    105                 } else if ( !bp_is_my_profile() && !$bp->is_single_item ) {
    106 
    107                         $bp->bp_options_avatar = bp_core_fetch_avatar( array( 'item_id' => $bp->displayed_user->id, 'type' => 'thumb' ) );
    108                         $bp->bp_options_title = $bp->displayed_user->fullname;
    109 
    110                 } else if ( $bp->is_single_item ) {
     26        /**
     27         * Setup globals
     28         *
     29         * The BP_GROUPS_SLUG constant is deprecated, and only used here for
     30         * backwards compatibility.
     31         *
     32         * @since BuddyPress {unknown}
     33         * @global obj $bp
     34         */
     35        function _setup_globals() {
     36                global $bp;
     37
     38                // Define a slug, if necessary
     39                if ( !defined( 'BP_GROUPS_SLUG' ) )
     40                        define( 'BP_GROUPS_SLUG', $this->id );
     41
     42                // Do some slug checks
     43                $this->slug      = BP_GROUPS_SLUG;
     44                $this->root_slug = isset( $bp->pages->groups->slug ) ? $bp->pages->groups->slug : $this->slug;
     45
     46                // Tables
     47                $this->table_name            = $bp->table_prefix . 'bp_groups';
     48                $this->table_name_members    = $bp->table_prefix . 'bp_groups_members';
     49                $this->table_name_groupmeta  = $bp->table_prefix . 'bp_groups_groupmeta';
     50
     51                // Notifications
     52                $this->notification_callback = 'groups_format_notifications';
     53
     54                // Illegal group names/slugs
     55                $this->forbidden_names       = apply_filters( 'groups_forbidden_names', array(
     56                        'my-groups',
     57                        'create',
     58                        'invites',
     59                        'send-invites',
     60                        'forum',
     61                        'delete',
     62                        'add',
     63                        'admin',
     64                        'request-membership',
     65                        'members',
     66                        'settings',
     67                        'avatar',
     68                        $this->slug,
     69                        $this->root_slug,
     70                ) );
     71
     72                // The default text for the blogs directory search box
     73                $bp->default_search_strings[$this->id] = __( 'Search Groups...', 'buddypress' );
     74
     75                // Preconfigured group creation steps
     76                $this->group_creation_steps = apply_filters( 'groups_create_group_steps', array(
     77                        'group-details'  => array(
     78                                'name'       => __( 'Details',  'buddypress' ),
     79                                'position'   => 0
     80                        ),
     81                        'group-settings' => array(
     82                                'name'       => __( 'Settings', 'buddypress' ),
     83                                'position'   => 10
     84                        ),
     85                        'group-avatar'   => array(
     86                                'name'       => __( 'Avatar',   'buddypress' ),
     87                                'position'   => 20 ),
     88                ) );
     89
     90                // If friends component is active, add invitations
     91                if ( bp_is_active( 'friends' ) ) {
     92                        $this->group_creation_steps['group-invites'] = array(
     93                                'name'     => __( 'Invites', 'buddypress' ),
     94                                'position' => 30
     95                        );
     96                }
     97
     98                // Groups statuses
     99                $this->valid_status = apply_filters( 'groups_valid_status', array(
     100                        'public',
     101                        'private',
     102                        'hidden'
     103                ) );
     104
     105                // Auto join group when non group member performs group activity
     106                $this->auto_join = defined( 'BP_DISABLE_AUTO_GROUP_JOIN' ) ? false : true;
     107
     108                // Register this in the active components array
     109                $bp->active_components[$this->slug] = $this->id;
     110
     111                // The default text for the groups directory search box
     112                $bp->default_search_strings[$this->slug] = __( 'Search Groups...', 'buddypress' );
     113        }
     114
     115        /**
     116         * Include files
     117         */
     118        function _includes() {
     119                require_once( BP_PLUGIN_DIR . '/bp-groups/bp-groups-cache.php'     );
     120                require_once( BP_PLUGIN_DIR . '/bp-groups/bp-groups-forums.php'    );
     121                require_once( BP_PLUGIN_DIR . '/bp-groups/bp-groups-actions.php'   );
     122                require_once( BP_PLUGIN_DIR . '/bp-groups/bp-groups-filters.php'   );
     123                require_once( BP_PLUGIN_DIR . '/bp-groups/bp-groups-screens.php'   );
     124                require_once( BP_PLUGIN_DIR . '/bp-groups/bp-groups-classes.php'   );
     125                require_once( BP_PLUGIN_DIR . '/bp-groups/bp-groups-widgets.php'   );
     126                require_once( BP_PLUGIN_DIR . '/bp-groups/bp-groups-activity.php'  );
     127                require_once( BP_PLUGIN_DIR . '/bp-groups/bp-groups-template.php'  );
     128                require_once( BP_PLUGIN_DIR . '/bp-groups/bp-groups-functions.php' );
     129        }
     130
     131        /**
     132         * Setup BuddyBar navigation
     133         *
     134         * @global obj $bp
     135         */
     136        function _setup_nav() {
     137                global $bp;
     138
     139                // Are we viewing a single group?
     140                if ( bp_is_current_component( 'groups' ) && $group_id = BP_Groups_Group::group_exists( bp_current_action() ) ) {
     141
     142                        $bp->is_single_item  = true;
     143                        $this->current_group = new BP_Groups_Group( $group_id );
     144
     145                        // Using "item" not "group" for generic support in other components.
     146                        if ( is_super_admin() )
     147                                $bp->is_item_admin = 1;
     148                        else
     149                                $bp->is_item_admin = groups_is_user_admin( $bp->loggedin_user->id, $this->current_group->id );
     150
     151                        // If the user is not an admin, check if they are a moderator
     152                        if ( empty( $bp->is_item_admin ) )
     153                                $bp->is_item_mod = groups_is_user_mod( $bp->loggedin_user->id, $this->current_group->id );
     154
     155                        // Is the logged in user a member of the group?
     156                        if ( ( is_user_logged_in() && groups_is_user_member( $bp->loggedin_user->id, $this->current_group->id ) ) )
     157                                $this->current_group->is_user_member = true;
     158                        else
     159                                $this->current_group->is_user_member = false;
     160
     161                        // Should this group be visible to the logged in user?
     162                        if ( 'public' == $this->current_group->status || $this->current_group->is_user_member )
     163                                $this->current_group->is_visible = true;
     164                        else
     165                                $this->current_group->is_visible = false;
     166                }
     167
     168                // Add 'Groups' to the main navigation
     169                bp_core_new_nav_item( array(
     170                        'name'                => sprintf( __( 'Groups <span>(%d)</span>', 'buddypress' ), groups_total_groups_for_user() ),
     171                        'slug'                => $this->slug,
     172                        'position'            => 70,
     173                        'screen_function'     => 'groups_screen_my_groups',
     174                        'default_subnav_slug' => 'my-groups',
     175                        'item_css_id'         => $this->id
     176                ) );
     177
     178                $groups_link = trailingslashit( $bp->loggedin_user->domain . $this->slug );
     179
     180                // Add the My Groups nav item
     181                bp_core_new_subnav_item( array(
     182                        'name'            => __( 'My Groups', 'buddypress' ),
     183                        'slug'            => 'my-groups',
     184                        'parent_url'      => $groups_link,
     185                        'parent_slug'     => $this->slug,
     186                        'screen_function' => 'groups_screen_my_groups',
     187                        'position'        => 10,
     188                        'item_css_id'     => 'groups-my-groups'
     189                ) );
     190
     191                // Add the Group Invites nav item
     192                bp_core_new_subnav_item( array(
     193                        'name'            => __( 'Invitations',   'buddypress' ),
     194                        'slug'            => 'invites',
     195                        'parent_url'      => $groups_link,
     196                        'parent_slug'     => $this->slug,
     197                        'screen_function' => 'groups_screen_group_invites',
     198                        'position'        => 30,
     199                        'user_has_access' => bp_is_my_profile()
     200                ) );
     201
     202
     203                if ( bp_is_current_component( 'groups' ) ) {
     204
     205                        if ( bp_is_my_profile() && !bp_is_single_item() ) {
     206
     207                                $bp->bp_options_title = __( 'My Groups', 'buddypress' );
     208
     209                        } else if ( !bp_is_my_profile() && !$bp->is_single_item ) {
     210
     211                                $bp->bp_options_avatar = bp_core_fetch_avatar( array(
     212                                        'item_id' => $bp->displayed_user->id,
     213                                        'type'    => 'thumb'
     214                                ) );
     215                                $bp->bp_options_title  = $bp->displayed_user->fullname;
     216
    111217                        // We are viewing a single group, so set up the
    112                         // group navigation menu using the $bp->groups->current_group global.
    113 
    114                         // When in a single group, the first action is bumped down one because of the
    115                         // group name, so we need to adjust this and set the group name to current_item.
    116                         $bp->current_item   = isset( $bp->current_action )      ? $bp->current_action      : false;
    117                         $bp->current_action = isset( $bp->action_variables[0] ) ? $bp->action_variables[0] : false;
    118                         array_shift( $bp->action_variables );
    119 
    120                         $bp->bp_options_title = $bp->groups->current_group->name;
    121 
    122                         if ( !$bp->bp_options_avatar = bp_core_fetch_avatar( array( 'item_id' => $bp->groups->current_group->id, 'object' => 'group', 'type' => 'thumb', 'avatar_dir' => 'group-avatars', 'alt' => __( 'Group Avatar', 'buddypress' ) ) ) )
    123                                 $bp->bp_options_avatar = '<img src="' . esc_attr( $group->avatar_full ) . '" class="avatar" alt="' . esc_attr( $group->name ) . '" />';
    124 
    125                         $group_link = $bp->root_domain . '/' . $bp->groups->root_slug . '/' . $bp->groups->current_group->slug . '/';
    126 
    127                         // If this is a private or hidden group, does the user have access?
    128                         if ( 'private' == $bp->groups->current_group->status || 'hidden' == $bp->groups->current_group->status ) {
    129                                 if ( $bp->groups->current_group->is_user_member && is_user_logged_in() || is_super_admin() )
    130                                         $bp->groups->current_group->user_has_access = true;
    131                                 else
    132                                         $bp->groups->current_group->user_has_access = false;
    133                         } else {
    134                                 $bp->groups->current_group->user_has_access = true;
    135                         }
    136 
    137                         // Reset the existing subnav items
    138                         bp_core_reset_subnav_items( $bp->groups->root_slug );
    139 
    140                         // Add a new default subnav item for when the groups nav is selected.
    141                         bp_core_new_nav_default( array( 'parent_slug' => $bp->groups->slug, 'screen_function' => 'groups_screen_group_home', 'subnav_slug' => 'home' ) );
    142 
    143                         // Add the "Home" subnav item, as this will always be present
    144                         bp_core_new_subnav_item( array( 'name' => __( 'Home', 'buddypress' ), 'slug' => 'home', 'parent_url' => $group_link, 'parent_slug' => $bp->groups->root_slug, 'screen_function' => 'groups_screen_group_home', 'position' => 10, 'item_css_id' => 'home' ) );
    145 
    146                         // If the user is a group mod or more, then show the group admin nav item
    147                         if ( $bp->is_item_mod || $bp->is_item_admin )
    148                                 bp_core_new_subnav_item( array( 'name' => __( 'Admin', 'buddypress' ), 'slug' => 'admin', 'parent_url' => $group_link, 'parent_slug' => $bp->groups->root_slug, 'screen_function' => 'groups_screen_group_admin', 'position' => 20, 'user_has_access' => ( $bp->is_item_admin + (int)$bp->is_item_mod ), 'item_css_id' => 'admin' ) );
    149 
    150                         // If this is a private group, and the user is not a member, show a "Request Membership" nav item.
    151                         if ( !is_super_admin() && is_user_logged_in() && !$bp->groups->current_group->is_user_member && !groups_check_for_membership_request( $bp->loggedin_user->id, $bp->groups->current_group->id ) && $bp->groups->current_group->status == 'private' )
    152                                 bp_core_new_subnav_item( array( 'name' => __( 'Request Membership', 'buddypress' ), 'slug' => 'request-membership', 'parent_url' => $group_link, 'parent_slug' => $bp->groups->root_slug, 'screen_function' => 'groups_screen_group_request_membership', 'position' => 30 ) );
    153 
    154                         // Forums are enabled and turned on
    155                         if ( $bp->groups->current_group->enable_forum && function_exists( 'bp_forums_setup' ) )
    156                                 bp_core_new_subnav_item( array( 'name' => __( 'Forum', 'buddypress' ), 'slug' => 'forum', 'parent_url' => $group_link, 'parent_slug' => $bp->groups->root_slug, 'screen_function' => 'groups_screen_group_forum', 'position' => 40, 'user_has_access' => $bp->groups->current_group->user_has_access, 'item_css_id' => 'forums' ) );
    157 
    158                         bp_core_new_subnav_item( array( 'name' => sprintf( __( 'Members (%s)', 'buddypress' ), number_format( $bp->groups->current_group->total_member_count ) ), 'slug' => 'members', 'parent_url' => $group_link, 'parent_slug' => $bp->groups->root_slug, 'screen_function' => 'groups_screen_group_members', 'position' => 60, 'user_has_access' => $bp->groups->current_group->user_has_access, 'item_css_id' => 'members'  ) );
    159 
    160                         if ( is_user_logged_in() && groups_is_user_member( $bp->loggedin_user->id, $bp->groups->current_group->id ) ) {
    161                                 if ( bp_is_active('friends') )
    162                                         bp_core_new_subnav_item( array( 'name' => __( 'Send Invites', 'buddypress' ), 'slug' => 'send-invites', 'parent_url' => $group_link, 'parent_slug' => $bp->groups->root_slug, 'screen_function' => 'groups_screen_group_invite', 'item_css_id' => 'invite', 'position' => 70, 'user_has_access' => $bp->groups->current_group->user_has_access ) );
     218                        // group navigation menu using the $this->current_group global.
     219                        } else if ( bp_is_single_item() ) {
     220                                // When in a single group, the first action is bumped down one because of the
     221                                // group name, so we need to adjust this and set the group name to current_item.
     222                                $bp->current_item   = isset( $bp->current_action )      ? $bp->current_action      : false;
     223                                $bp->current_action = isset( $bp->action_variables[0] ) ? $bp->action_variables[0] : false;
     224                                array_shift( $bp->action_variables );
     225
     226                                $bp->bp_options_title  = $this->current_group->name;
     227                                $bp->bp_options_avatar = bp_core_fetch_avatar( array(
     228                                        'item_id'    => $this->current_group->id,
     229                                        'object'     => 'group',
     230                                        'type'       => 'thumb',
     231                                        'avatar_dir' => 'group-avatars',
     232                                        'alt'        => __( 'Group Avatar', 'buddypress' )
     233                                ) );
     234                                if ( empty( $bp->bp_options_avatar ) )
     235                                        $bp->bp_options_avatar = '<img src="' . esc_attr( $group->avatar_full ) . '" class="avatar" alt="' . esc_attr( $group->name ) . '" />';
     236
     237                                // Add 'Groups' to the main navigation
     238                                bp_core_new_nav_item( array(
     239                                        'name'                => __( 'Groups', 'buddypress' ),
     240                                        'slug'                => $this->root_slug,
     241                                        'position'            => -1, // Do not show in BuddyBar
     242                                        'screen_function'     => 'groups_screen_group_home',
     243                                        'default_subnav_slug' => 'home',
     244                                        'item_css_id'         => $this->id
     245                                ) );
     246
     247                                $group_link = trailingslashit( $bp->root_domain . '/' . $this->root_slug . '/' . $this->current_group->slug );
     248
     249                                // If this is a private or hidden group, does the user have access?
     250                                if ( 'private' == $this->current_group->status || 'hidden' == $this->current_group->status ) {
     251                                        if ( $this->current_group->is_user_member && is_user_logged_in() || is_super_admin() )
     252                                                $this->current_group->user_has_access = true;
     253                                        else
     254                                                $this->current_group->user_has_access = false;
     255                                } else {
     256                                        $this->current_group->user_has_access = true;
     257                                }
     258
     259                                // Add the "Home" subnav item, as this will always be present
     260                                bp_core_new_subnav_item( array(
     261                                        'name'            => __( 'Home', 'buddypress' ),
     262                                        'slug'            => 'home',
     263                                        'parent_url'      => $group_link,
     264                                        'parent_slug'     => $this->root_slug,
     265                                        'screen_function' => 'groups_screen_group_home',
     266                                        'position'        => 10,
     267                                        'item_css_id'     => 'home'
     268                                ) );
     269
     270                                // If the user is a group mod or more, then show the group admin nav item
     271                                if ( bp_is_item_admin() || bp_is_item_mod() ) {
     272                                        bp_core_new_subnav_item( array(
     273                                                'name'            => __( 'Admin', 'buddypress' ),
     274                                                'slug'            => 'admin',
     275                                                'parent_url'      => $group_link,
     276                                                'parent_slug'     => $this->root_slug,
     277                                                'screen_function' => 'groups_screen_group_admin',
     278                                                'position'        => 20,
     279                                                'user_has_access' => ( $bp->is_item_admin + (int)$bp->is_item_mod ),
     280                                                'item_css_id'     => 'admin'
     281                                        ) );
     282                                }
     283
     284                                // If this is a private group, and the user is not a member, show a "Request Membership" nav item.
     285                                if ( is_user_logged_in() &&
     286                                         !is_super_admin() &&
     287                                         !$this->current_group->is_user_member &&
     288                                         !groups_check_for_membership_request( $bp->loggedin_user->id, $this->current_group->id ) &&
     289                                         $this->current_group->status == 'private'
     290                                        ) {
     291                                        bp_core_new_subnav_item( array(
     292                                                'name'               => __( 'Request Membership', 'buddypress' ),
     293                                                'slug'               => 'request-membership',
     294                                                'parent_url'         => $group_link,
     295                                                'parent_slug'        => $this->root_slug,
     296                                                'screen_function'    => 'groups_screen_group_request_membership',
     297                                                'position'           => 30
     298                                        ) );
     299                                }
     300
     301                                // Forums are enabled and turned on
     302                                if ( $this->current_group->enable_forum && bp_is_active( 'forums' ) ) {
     303                                        bp_core_new_subnav_item( array(
     304                                                'name'            => __( 'Forum', 'buddypress' ),
     305                                                'slug'            => 'forum',
     306                                                'parent_url'      => $group_link,
     307                                                'parent_slug'     => $this->root_slug,
     308                                                'screen_function' => 'groups_screen_group_forum',
     309                                                'position'        => 40,
     310                                                'user_has_access' => $this->current_group->user_has_access,
     311                                                'item_css_id'     => 'forums'
     312                                        ) );
     313                                }
     314
     315                                bp_core_new_subnav_item( array(
     316                                        'name'            => sprintf( __( 'Members (%s)', 'buddypress' ), number_format( $this->current_group->total_member_count ) ),
     317                                        'slug'            => 'members',
     318                                        'parent_url'      => $group_link,
     319                                        'parent_slug'     => $this->root_slug,
     320                                        'screen_function' => 'groups_screen_group_members',
     321                                        'position'        => 60,
     322                                        'user_has_access' => $this->current_group->user_has_access,
     323                                        'item_css_id'     => 'members'
     324                                ) );
     325
     326                                if ( is_user_logged_in() && groups_is_user_member( $bp->loggedin_user->id, $this->current_group->id ) ) {
     327                                        if ( bp_is_active( 'friends' ) ) {
     328                                                bp_core_new_subnav_item( array(
     329                                                        'name'            => __( 'Send Invites', 'buddypress' ),
     330                                                        'slug'            => 'send-invites',
     331                                                        'parent_url'      => $group_link,
     332                                                        'parent_slug'     => $this->root_slug,
     333                                                        'screen_function' => 'groups_screen_group_invite',
     334                                                        'item_css_id'     => 'invite',
     335                                                        'position'        => 70,
     336                                                        'user_has_access' => $this->current_group->user_has_access
     337                                                ) );
     338                                        }
     339                                }
    163340                        }
    164341                }
    165         }
    166 
    167         if ( isset( $bp->groups->current_group->user_has_access ) )
    168                 do_action( 'groups_setup_nav', $bp->groups->current_group->user_has_access );
    169         else
    170                 do_action( 'groups_setup_nav');
    171 }
    172 add_action( 'bp_setup_nav', 'groups_setup_nav' );
    173 
    174 function groups_directory_groups_setup() {
    175         global $bp;
    176 
    177         if ( bp_is_current_component( 'groups' ) && empty( $bp->current_action ) && empty( $bp->current_item ) ) {
    178                 $bp->is_directory = true;
    179 
    180                 do_action( 'groups_directory_groups_setup' );
    181                 bp_core_load_template( apply_filters( 'groups_template_directory_groups', 'groups/index' ) );
     342
     343                if ( isset( $this->current_group->user_has_access ) )
     344                        do_action( 'groups_setup_nav', $this->current_group->user_has_access );
     345                else
     346                        do_action( 'groups_setup_nav');
    182347        }
    183348}
    184 add_action( 'wp', 'groups_directory_groups_setup', 2 );
    185 
    186 function groups_setup_adminbar_menu() {
    187         global $bp;
    188 
    189         if ( empty( $bp->groups->current_group ) )
    190                 return false;
    191 
    192         // Don't show this menu to non site admins or if you're viewing your own profile
    193         if ( !is_super_admin() )
    194                 return false; ?>
    195 
    196         <li id="bp-adminbar-adminoptions-menu">
    197                 <a href=""><?php _e( 'Admin Options', 'buddypress' ) ?></a>
    198 
    199                 <ul>
    200                         <li><a class="confirm" href="<?php echo wp_nonce_url( bp_get_group_permalink( $bp->groups->current_group ) . 'admin/delete-group/', 'groups_delete_group' ) ?>&amp;delete-group-button=1&amp;delete-group-understand=1"><?php _e( "Delete Group", 'buddypress' ) ?></a></li>
    201 
    202                         <?php do_action( 'groups_adminbar_menu_items' ) ?>
    203 
    204                 </ul>
    205         </li>
    206 
    207         <?php
    208 }
    209 add_action( 'bp_adminbar_menus', 'groups_setup_adminbar_menu', 20 );
    210 
    211 /********************************************************************************
    212  * Screen Functions
    213  *
    214  * Screen functions are the controllers of BuddyPress. They will execute when their
    215  * specific URL is caught. They will first save or manipulate data using business
    216  * functions, then pass on the user to a template file.
    217  */
    218 
    219 function groups_screen_my_groups() {
    220         global $bp;
    221 
    222         // Delete group request notifications for the user
    223         if ( isset( $_GET['n'] ) ) {
    224                 bp_users_delete_notifications_by_type( $bp->loggedin_user->id, $bp->groups->id, 'membership_request_accepted' );
    225                 bp_users_delete_notifications_by_type( $bp->loggedin_user->id, $bp->groups->id, 'membership_request_rejected' );
    226                 bp_users_delete_notifications_by_type( $bp->loggedin_user->id, $bp->groups->id, 'member_promoted_to_mod'      );
    227                 bp_users_delete_notifications_by_type( $bp->loggedin_user->id, $bp->groups->id, 'member_promoted_to_admin'    );
    228         }
    229 
    230         do_action( 'groups_screen_my_groups' );
    231 
    232         bp_core_load_template( apply_filters( 'groups_template_my_groups', 'members/single/home' ) );
    233 }
    234 
    235 function groups_screen_group_invites() {
    236         global $bp;
    237 
    238         $group_id = 0;
    239         if ( isset( $bp->action_variables[1] ) )
    240                 $group_id = $bp->action_variables[1];
    241 
    242         if ( isset( $bp->action_variables ) && in_array( 'accept', (array)$bp->action_variables ) && is_numeric( $group_id ) ) {
    243                 // Check the nonce
    244                 if ( !check_admin_referer( 'groups_accept_invite' ) )
    245                         return false;
    246 
    247                 if ( !groups_accept_invite( $bp->loggedin_user->id, $group_id ) ) {
    248                         bp_core_add_message( __('Group invite could not be accepted', 'buddypress'), 'error' );
    249                 } else {
    250                         bp_core_add_message( __('Group invite accepted', 'buddypress') );
    251 
    252                         // Record this in activity streams
    253                         $group = new BP_Groups_Group( $group_id );
    254 
    255                         groups_record_activity( array(
    256                                 'action'  => apply_filters( 'groups_activity_accepted_invite_action', sprintf( __( '%1$s joined the group %2$s', 'buddypress'), bp_core_get_userlink( $bp->loggedin_user->id ), '<a href="' . bp_get_group_permalink( $group ) . '">' . esc_attr( $group->name ) . '</a>' ), $bp->loggedin_user->id, &$group ),
    257                                 'type'    => 'joined_group',
    258                                 'item_id' => $group->id
    259                         ) );
    260                 }
    261 
    262                 bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/' . $bp->current_action );
    263 
    264         } elseif ( isset( $bp->action_variables ) && in_array( 'reject', (array)$bp->action_variables ) && is_numeric( $group_id ) ) {
    265                 // Check the nonce
    266                 if ( !check_admin_referer( 'groups_reject_invite' ) )
    267                         return false;
    268 
    269                 if ( !groups_reject_invite( $bp->loggedin_user->id, $group_id ) )
    270                         bp_core_add_message( __('Group invite could not be rejected', 'buddypress'), 'error' );
    271                 else
    272                         bp_core_add_message( __('Group invite rejected', 'buddypress') );
    273 
    274                 bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/' . $bp->current_action );
    275         }
    276 
    277         // Remove notifications
    278         bp_users_delete_notifications_by_type( $bp->loggedin_user->id, $bp->groups->id, 'group_invite' );
    279 
    280         do_action( 'groups_screen_group_invites', $group_id );
    281 
    282         bp_core_load_template( apply_filters( 'groups_template_group_invites', 'members/single/home' ) );
    283 }
    284 
    285 function groups_screen_group_home() {
    286         global $bp;
    287 
    288         if ( $bp->is_single_item ) {
    289                 if ( isset( $_GET['n'] ) ) {
    290                         bp_users_delete_notifications_by_type( $bp->loggedin_user->id, $bp->groups->id, 'membership_request_accepted' );
    291                         bp_users_delete_notifications_by_type( $bp->loggedin_user->id, $bp->groups->id, 'membership_request_rejected' );
    292                         bp_users_delete_notifications_by_type( $bp->loggedin_user->id, $bp->groups->id, 'member_promoted_to_mod'      );
    293                         bp_users_delete_notifications_by_type( $bp->loggedin_user->id, $bp->groups->id, 'member_promoted_to_admin'    );
    294                 }
    295 
    296                 do_action( 'groups_screen_group_home' );
    297 
    298                 bp_core_load_template( apply_filters( 'groups_template_group_home', 'groups/single/home' ) );
    299         }
    300 }
    301 
    302 function groups_screen_group_forum() {
    303         global $bp;
    304 
    305         if ( $bp->is_single_item && $bp->groups->current_group->user_has_access ) {
    306 
    307                 // Fetch the details we need
    308                 $topic_slug     = isset( $bp->action_variables[1] ) ? $bp->action_variables[1] : false;
    309                 $topic_id       = bp_forums_get_topic_id_from_slug( $topic_slug );
    310                 $forum_id       = groups_get_groupmeta( $bp->groups->current_group->id, 'forum_id' );
    311                 $user_is_banned = false;
    312 
    313                 if ( !is_super_admin() && groups_is_user_banned( $bp->loggedin_user->id, $bp->groups->current_group->id ) )
    314                         $user_is_banned = true;
    315 
    316                 if ( $topic_slug && $topic_id ) {
    317 
    318                         // Posting a reply
    319                         if ( !$user_is_banned && !isset( $bp->action_variables[2] ) && isset( $_POST['submit_reply'] ) ) {
    320                                 // Check the nonce
    321                                 check_admin_referer( 'bp_forums_new_reply' );
    322 
    323                                 // Auto join this user if they are not yet a member of this group
    324                                 if ( $bp->groups->auto_join && !is_super_admin() && 'public' == $bp->groups->current_group->status && !groups_is_user_member( $bp->loggedin_user->id, $bp->groups->current_group->id ) )
    325                                         groups_join_group( $bp->groups->current_group->id, $bp->loggedin_user->id );
    326 
    327                                 $topic_page = isset( $_GET['topic_page'] ) ? $_GET['topic_page'] : false;
    328 
    329                                 if ( !$post_id = groups_new_group_forum_post( $_POST['reply_text'], $topic_id, $topic_page ) )
    330                                         bp_core_add_message( __( 'There was an error when replying to that topic', 'buddypress'), 'error' );
    331                                 else
    332                                         bp_core_add_message( __( 'Your reply was posted successfully', 'buddypress') );
    333 
    334                                 if ( isset( $_SERVER['QUERY_STRING'] ) )
    335                                         $query_vars = '?' . $_SERVER['QUERY_STRING'];
    336 
    337                                 bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic_slug . '/' . $query_vars . '#post-' . $post_id );
    338                         }
    339 
    340                         // Sticky a topic
    341                         else if ( isset( $bp->action_variables[2] ) && 'stick' == $bp->action_variables[2] && ( isset( $bp->is_item_admin ) || isset( $bp->is_item_mod ) ) ) {
    342                                 // Check the nonce
    343                                 check_admin_referer( 'bp_forums_stick_topic' );
    344 
    345                                 if ( !bp_forums_sticky_topic( array( 'topic_id' => $topic_id ) ) )
    346                                         bp_core_add_message( __( 'There was an error when making that topic a sticky', 'buddypress' ), 'error' );
    347                                 else
    348                                         bp_core_add_message( __( 'The topic was made sticky successfully', 'buddypress' ) );
    349 
    350                                 do_action( 'groups_stick_forum_topic', $topic_id );
    351                                 bp_core_redirect( wp_get_referer() );
    352                         }
    353 
    354                         // Un-Sticky a topic
    355                         else if ( isset( $bp->action_variables[2] ) && 'unstick' == $bp->action_variables[2] && ( isset( $bp->is_item_admin ) || isset( $bp->is_item_mod ) ) ) {
    356                                 // Check the nonce
    357                                 check_admin_referer( 'bp_forums_unstick_topic' );
    358 
    359                                 if ( !bp_forums_sticky_topic( array( 'topic_id' => $topic_id, 'mode' => 'unstick' ) ) )
    360                                         bp_core_add_message( __( 'There was an error when unsticking that topic', 'buddypress'), 'error' );
    361                                 else
    362                                         bp_core_add_message( __( 'The topic was unstuck successfully', 'buddypress') );
    363 
    364                                 do_action( 'groups_unstick_forum_topic', $topic_id );
    365                                 bp_core_redirect( wp_get_referer() );
    366                         }
    367 
    368                         // Close a topic
    369                         else if ( isset( $bp->action_variables[2] ) && 'close' == $bp->action_variables[2] && ( isset( $bp->is_item_admin ) || isset( $bp->is_item_mod ) ) ) {
    370                                 // Check the nonce
    371                                 check_admin_referer( 'bp_forums_close_topic' );
    372 
    373                                 if ( !bp_forums_openclose_topic( array( 'topic_id' => $topic_id ) ) )
    374                                         bp_core_add_message( __( 'There was an error when closing that topic', 'buddypress'), 'error' );
    375                                 else
    376                                         bp_core_add_message( __( 'The topic was closed successfully', 'buddypress') );
    377 
    378                                 do_action( 'groups_close_forum_topic', $topic_id );
    379                                 bp_core_redirect( wp_get_referer() );
    380                         }
    381 
    382                         // Open a topic
    383                         else if ( isset( $bp->action_variables[2] ) && 'open' == $bp->action_variables[2] && ( isset( $bp->is_item_admin ) || isset( $bp->is_item_mod ) ) ) {
    384                                 // Check the nonce
    385                                 check_admin_referer( 'bp_forums_open_topic' );
    386 
    387                                 if ( !bp_forums_openclose_topic( array( 'topic_id' => $topic_id, 'mode' => 'open' ) ) )
    388                                         bp_core_add_message( __( 'There was an error when opening that topic', 'buddypress'), 'error' );
    389                                 else
    390                                         bp_core_add_message( __( 'The topic was opened successfully', 'buddypress') );
    391 
    392                                 do_action( 'groups_open_forum_topic', $topic_id );
    393                                 bp_core_redirect( wp_get_referer() );
    394                         }
    395 
    396                         // Delete a topic
    397                         else if ( !$user_is_banned && isset( $bp->action_variables[2] ) && 'delete' == $bp->action_variables[2] && empty( $bp->action_variables[3] ) ) {
    398                                 // Fetch the topic
    399                                 $topic = bp_forums_get_topic_details( $topic_id );
    400 
    401                                 /* Check the logged in user can delete this topic */
    402                                 if ( !$bp->is_item_admin && !$bp->is_item_mod && (int)$bp->loggedin_user->id != (int)$topic->topic_poster )
    403                                         bp_core_redirect( wp_get_referer() );
    404 
    405                                 // Check the nonce
    406                                 check_admin_referer( 'bp_forums_delete_topic' );
    407 
    408                                 do_action( 'groups_before_delete_forum_topic', $topic_id );
    409 
    410                                 if ( !groups_delete_group_forum_topic( $topic_id ) )
    411                                         bp_core_add_message( __( 'There was an error deleting the topic', 'buddypress'), 'error' );
    412                                 else
    413 
    414                                 do_action( 'groups_delete_forum_topic', $topic_id );
    415                                 bp_core_redirect( wp_get_referer() );
    416                         }
    417 
    418                         // Editing a topic
    419                         else if ( !$user_is_banned && isset( $bp->action_variables[2] ) && 'edit' == $bp->action_variables[2] && empty( $bp->action_variables[3] ) ) {
    420                                 // Fetch the topic
    421                                 $topic = bp_forums_get_topic_details( $topic_id );
    422 
    423                                 // Check the logged in user can edit this topic
    424                                 if ( !$bp->is_item_admin && !$bp->is_item_mod && (int)$bp->loggedin_user->id != (int)$topic->topic_poster )
    425                                         bp_core_redirect( wp_get_referer() );
    426 
    427                                 if ( isset( $_POST['save_changes'] ) ) {
    428                                         // Check the nonce
    429                                         check_admin_referer( 'bp_forums_edit_topic' );
    430 
    431                                         if ( !groups_update_group_forum_topic( $topic_id, $_POST['topic_title'], $_POST['topic_text'] ) )
    432                                                 bp_core_add_message( __( 'There was an error when editing that topic', 'buddypress'), 'error' );
    433                                         else
    434                                                 bp_core_add_message( __( 'The topic was edited successfully', 'buddypress') );
    435 
    436                                         do_action( 'groups_edit_forum_topic', $topic_id );
    437                                         bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic_slug . '/' );
    438                                 }
    439 
    440                                 bp_core_load_template( apply_filters( 'groups_template_group_forum_topic_edit', 'groups/single/home' ) );
    441                         }
    442 
    443                         // Delete a post
    444                         else if ( !$user_is_banned && isset( $bp->action_variables[2] ) && 'delete' == $bp->action_variables[2] && isset( $bp->action_variables[4] ) && $post_id = $bp->action_variables[4] ) {
    445                                 // Fetch the post
    446                                 $post = bp_forums_get_post( $post_id );
    447 
    448                                 // Check the logged in user can edit this topic
    449                                 if ( !$bp->is_item_admin && !$bp->is_item_mod && (int)$bp->loggedin_user->id != (int)$post->poster_id )
    450                                         bp_core_redirect( wp_get_referer() );
    451 
    452                                 // Check the nonce
    453                                 check_admin_referer( 'bp_forums_delete_post' );
    454 
    455                                 do_action( 'groups_before_delete_forum_post', $post_id );
    456 
    457                                 if ( !groups_delete_group_forum_post( $bp->action_variables[4], $topic_id ) )
    458                                         bp_core_add_message( __( 'There was an error deleting that post', 'buddypress'), 'error' );
    459                                 else
    460                                         bp_core_add_message( __( 'The post was deleted successfully', 'buddypress') );
    461 
    462                                 do_action( 'groups_delete_forum_post', $post_id );
    463                                 bp_core_redirect( wp_get_referer() );
    464                         }
    465 
    466                         // Editing a post
    467                         else if ( !$user_is_banned && isset( $bp->action_variables[2] ) && 'edit' == $bp->action_variables[2] && isset( $bp->action_variables[4] ) && $post_id = $bp->action_variables[4] ) {
    468                                 // Fetch the post
    469                                 $post = bp_forums_get_post( $bp->action_variables[4] );
    470 
    471                                 // Check the logged in user can edit this topic
    472                                 if ( !$bp->is_item_admin && !$bp->is_item_mod && (int)$bp->loggedin_user->id != (int)$post->poster_id )
    473                                         bp_core_redirect( wp_get_referer() );
    474 
    475                                 if ( isset( $_POST['save_changes'] ) ) {
    476                                         // Check the nonce
    477                                         check_admin_referer( 'bp_forums_edit_post' );
    478 
    479                                         if ( !$post_id = groups_update_group_forum_post( $post_id, $_POST['post_text'], $topic_id, $_GET['topic_page'] ) )
    480                                                 bp_core_add_message( __( 'There was an error when editing that post', 'buddypress'), 'error' );
    481                                         else
    482                                                 bp_core_add_message( __( 'The post was edited successfully', 'buddypress') );
    483 
    484                                         if ( $_SERVER['QUERY_STRING'] )
    485                                                 $query_vars = '?' . $_SERVER['QUERY_STRING'];
    486 
    487                                         do_action( 'groups_edit_forum_post', $post_id );
    488                                         bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic_slug . '/' . $query_vars . '#post-' . $post_id );
    489                                 }
    490 
    491                                 bp_core_load_template( apply_filters( 'groups_template_group_forum_topic_edit', 'groups/single/home' ) );
    492                         }
    493 
    494                         // Standard topic display
    495                         else {
    496                                 if ( $user_is_banned )
    497                                         bp_core_add_message( __( "You have been banned from this group.", 'buddypress' ) );
    498 
    499                                 bp_core_load_template( apply_filters( 'groups_template_group_forum_topic', 'groups/single/home' ) );
    500                         }
    501 
    502                 } else {
    503                         // Posting a topic
    504                         if ( isset( $_POST['submit_topic'] ) && function_exists( 'bp_forums_new_topic') ) {
    505                                 // Check the nonce
    506                                 check_admin_referer( 'bp_forums_new_topic' );
    507 
    508                                 if ( $user_is_banned ) {
    509                                         $error_message = __( "You have been banned from this group.", 'buddypress' );
    510 
    511                                 } elseif ( $bp->groups->auto_join && !is_super_admin() && 'public' == $bp->groups->current_group->status && !groups_is_user_member( $bp->loggedin_user->id, $bp->groups->current_group->id ) ) {
    512                                         // Auto join this user if they are not yet a member of this group
    513                                         groups_join_group( $bp->groups->current_group->id, $bp->loggedin_user->id );
    514                                 }
    515 
    516                                 if ( empty( $_POST['topic_title'] ) )
    517                                         $error_message = __( 'Please provide a title for your forum topic.', 'buddypress' );
    518                                 else if ( empty( $_POST['topic_text'] ) )
    519                                         $error_message = __( 'Forum posts cannot be empty. Please enter some text.', 'buddypress' );
    520 
    521                                 if ( isset( $error_message ) ) {
    522                                         bp_core_add_message( $error_message, 'error' );
    523                                         $redirect = bp_get_group_permalink( $bp->groups->current_group ) . 'forum';
    524                                 } else {
    525                                         if ( !$topic = groups_new_group_forum_topic( $_POST['topic_title'], $_POST['topic_text'], $_POST['topic_tags'], $forum_id ) ) {
    526                                                 bp_core_add_message( __( 'There was an error when creating the topic', 'buddypress'), 'error' );
    527                                                 $redirect = bp_get_group_permalink( $bp->groups->current_group ) . 'forum';
    528                                         } else {
    529                                                 bp_core_add_message( __( 'The topic was created successfully', 'buddypress') );
    530                                                 $redirect = bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug . '/';
    531                                         }
    532                                 }
    533 
    534                                 bp_core_redirect( $redirect );
    535                         }
    536 
    537                         do_action( 'groups_screen_group_forum', $topic_id, $forum_id );
    538 
    539                         bp_core_load_template( apply_filters( 'groups_template_group_forum', 'groups/single/home' ) );
    540                 }
    541         }
    542 }
    543 
    544 function groups_screen_group_members() {
    545         global $bp;
    546 
    547         if ( $bp->is_single_item ) {
    548                 // Refresh the group member count meta
    549                 groups_update_groupmeta( $bp->groups->current_group->id, 'total_member_count', groups_get_total_member_count( $bp->groups->current_group->id ) );
    550 
    551                 do_action( 'groups_screen_group_members', $bp->groups->current_group->id );
    552                 bp_core_load_template( apply_filters( 'groups_template_group_members', 'groups/single/home' ) );
    553         }
    554 }
    555 
    556 function groups_screen_group_invite() {
    557         global $bp;
    558 
    559         if ( $bp->is_single_item ) {
    560                 if ( !empty( $bp->action_variables[0] ) && 'send' == $bp->action_variables[0] ) {
    561 
    562                         if ( !check_admin_referer( 'groups_send_invites', '_wpnonce_send_invites' ) )
    563                                 return false;
    564 
    565                         // Send the invites.
    566                         groups_send_invites( $bp->loggedin_user->id, $bp->groups->current_group->id );
    567 
    568                         bp_core_add_message( __('Group invites sent.', 'buddypress') );
    569 
    570                         do_action( 'groups_screen_group_invite', $bp->groups->current_group->id );
    571 
    572                         bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) );
    573                 } else {
    574                         // Show send invite page
    575                         bp_core_load_template( apply_filters( 'groups_template_group_invite', 'groups/single/home' ) );
    576                 }
    577         }
    578 }
    579 
    580 function groups_screen_group_request_membership() {
    581         global $bp;
    582 
    583         if ( !is_user_logged_in() )
    584                 return false;
    585 
    586         if ( 'private' == $bp->groups->current_group->status ) {
    587                 // If the user has submitted a request, send it.
    588                 if ( isset( $_POST['group-request-send']) ) {
    589                         // Check the nonce
    590                         if ( !check_admin_referer( 'groups_request_membership' ) )
    591                                 return false;
    592 
    593                         if ( !groups_send_membership_request( $bp->loggedin_user->id, $bp->groups->current_group->id ) ) {
    594                                 bp_core_add_message( __( 'There was an error sending your group membership request, please try again.', 'buddypress' ), 'error' );
    595                         } else {
    596                                 bp_core_add_message( __( 'Your membership request was sent to the group administrator successfully. You will be notified when the group administrator responds to your request.', 'buddypress' ) );
    597                         }
    598                         bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) );
    599                 }
    600 
    601                 do_action( 'groups_screen_group_request_membership', $bp->groups->current_group->id );
    602 
    603                 bp_core_load_template( apply_filters( 'groups_template_group_request_membership', 'groups/single/home' ) );
    604         }
    605 }
    606 
    607 function groups_screen_group_activity_permalink() {
    608         global $bp;
    609 
    610         if ( !bp_is_current_component( $bp->groups->slug ) || $bp->current_action != $bp->activity->slug || empty( $bp->action_variables[0] ) )
    611                 return false;
    612 
    613         $bp->is_single_item = true;
    614 
    615         bp_core_load_template( apply_filters( 'groups_template_group_home', 'groups/single/home' ) );
    616 }
    617 add_action( 'wp', 'groups_screen_group_activity_permalink', 3 );
    618 
    619 function groups_screen_group_admin() {
    620         global $bp;
    621 
    622         if ( !bp_is_current_component( BP_GROUPS_SLUG ) || 'admin' != $bp->current_action )
    623                 return false;
    624 
    625         if ( !empty( $bp->action_variables[0] ) )
    626                 return false;
    627 
    628         bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . 'admin/edit-details/' );
    629 }
    630 
    631 function groups_screen_group_admin_edit_details() {
    632         global $bp;
    633 
    634         if ( bp_is_current_component( 'groups' ) && isset( $bp->action_variables[0] ) && 'edit-details' == $bp->action_variables[0] ) {
    635 
    636                 if ( $bp->is_item_admin || $bp->is_item_mod  ) {
    637 
    638                         // If the edit form has been submitted, save the edited details
    639                         if ( isset( $_POST['save'] ) ) {
    640                                 // Check the nonce
    641                                 if ( !check_admin_referer( 'groups_edit_group_details' ) )
    642                                         return false;
    643 
    644                                 if ( !groups_edit_base_group_details( $_POST['group-id'], $_POST['group-name'], $_POST['group-desc'], (int)$_POST['group-notify-members'] ) ) {
    645                                         bp_core_add_message( __( 'There was an error updating group details, please try again.', 'buddypress' ), 'error' );
    646                                 } else {
    647                                         bp_core_add_message( __( 'Group details were successfully updated.', 'buddypress' ) );
    648                                 }
    649 
    650                                 do_action( 'groups_group_details_edited', $bp->groups->current_group->id );
    651 
    652                                 bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . 'admin/edit-details/' );
    653                         }
    654 
    655                         do_action( 'groups_screen_group_admin_edit_details', $bp->groups->current_group->id );
    656 
    657                         bp_core_load_template( apply_filters( 'groups_template_group_admin', 'groups/single/home' ) );
    658                 }
    659         }
    660 }
    661 add_action( 'wp', 'groups_screen_group_admin_edit_details', 4 );
    662 
    663 function groups_screen_group_admin_settings() {
    664         global $bp;
    665 
    666         if ( bp_is_current_component( 'groups' ) && isset( $bp->action_variables[0] ) && 'group-settings' == $bp->action_variables[0] ) {
    667 
    668                 if ( !$bp->is_item_admin )
    669                         return false;
    670 
    671                 // If the edit form has been submitted, save the edited details
    672                 if ( isset( $_POST['save'] ) ) {
    673                         $enable_forum = ( isset($_POST['group-show-forum'] ) ) ? 1 : 0;
    674 
    675                         $allowed_status = apply_filters( 'groups_allowed_status', array( 'public', 'private', 'hidden' ) );
    676                         $status = ( in_array( $_POST['group-status'], (array)$allowed_status ) ) ? $_POST['group-status'] : 'public';
    677 
    678                         // Check the nonce
    679                         if ( !check_admin_referer( 'groups_edit_group_settings' ) )
    680                                 return false;
    681 
    682                         if ( !groups_edit_group_settings( $_POST['group-id'], $enable_forum, $status ) ) {
    683                                 bp_core_add_message( __( 'There was an error updating group settings, please try again.', 'buddypress' ), 'error' );
    684                         } else {
    685                                 bp_core_add_message( __( 'Group settings were successfully updated.', 'buddypress' ) );
    686                         }
    687 
    688                         do_action( 'groups_group_settings_edited', $bp->groups->current_group->id );
    689 
    690                         bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . 'admin/group-settings/' );
    691                 }
    692 
    693                 do_action( 'groups_screen_group_admin_settings', $bp->groups->current_group->id );
    694 
    695                 bp_core_load_template( apply_filters( 'groups_template_group_admin_settings', 'groups/single/home' ) );
    696         }
    697 }
    698 add_action( 'wp', 'groups_screen_group_admin_settings', 4 );
    699 
    700 function groups_screen_group_admin_avatar() {
    701         global $bp;
    702 
    703         if ( bp_is_current_component( 'groups' ) && isset( $bp->action_variables[0] ) && 'group-avatar' == $bp->action_variables[0] ) {
    704 
    705                 if ( !$bp->is_item_admin )
    706                         return false;
    707 
    708                 // If the group admin has deleted the admin avatar
    709                 if ( 'delete' == $bp->action_variables[1] ) {
    710 
    711                         // Check the nonce
    712                         check_admin_referer( 'bp_group_avatar_delete' );
    713 
    714                         if ( bp_core_delete_existing_avatar( array( 'item_id' => $bp->groups->current_group->id, 'object' => 'group' ) ) )
    715                                 bp_core_add_message( __( 'Your avatar was deleted successfully!', 'buddypress' ) );
    716                         else
    717                                 bp_core_add_message( __( 'There was a problem deleting that avatar, please try again.', 'buddypress' ), 'error' );
    718 
    719                 }
    720 
    721                 $bp->avatar_admin->step = 'upload-image';
    722 
    723                 if ( !empty( $_FILES ) ) {
    724 
    725                         // Check the nonce
    726                         check_admin_referer( 'bp_avatar_upload' );
    727 
    728                         // Pass the file to the avatar upload handler
    729                         if ( bp_core_avatar_handle_upload( $_FILES, 'groups_avatar_upload_dir' ) ) {
    730                                 $bp->avatar_admin->step = 'crop-image';
    731 
    732                                 // Make sure we include the jQuery jCrop file for image cropping
    733                                 add_action( 'wp_print_scripts', 'bp_core_add_jquery_cropper' );
    734                         }
    735 
    736                 }
    737 
    738                 // If the image cropping is done, crop the image and save a full/thumb version
    739                 if ( isset( $_POST['avatar-crop-submit'] ) ) {
    740 
    741                         // Check the nonce
    742                         check_admin_referer( 'bp_avatar_cropstore' );
    743 
    744                         if ( !bp_core_avatar_handle_crop( array( 'object' => 'group', 'avatar_dir' => 'group-avatars', 'item_id' => $bp->groups->current_group->id, 'original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h'] ) ) )
    745                                 bp_core_add_message( __( 'There was a problem cropping the avatar, please try uploading it again', 'buddypress' ) );
    746                         else
    747                                 bp_core_add_message( __( 'The new group avatar was uploaded successfully!', 'buddypress' ) );
    748 
    749                 }
    750 
    751                 do_action( 'groups_screen_group_admin_avatar', $bp->groups->current_group->id );
    752 
    753                 bp_core_load_template( apply_filters( 'groups_template_group_admin_avatar', 'groups/single/home' ) );
    754         }
    755 }
    756 add_action( 'wp', 'groups_screen_group_admin_avatar', 4 );
    757 
    758 function groups_screen_group_admin_manage_members() {
    759         global $bp;
    760 
    761         if ( bp_is_current_component( 'groups' ) && isset( $bp->action_variables[0] ) && 'manage-members' == $bp->action_variables[0] ) {
    762 
    763                 if ( !$bp->is_item_admin )
    764                         return false;
    765 
    766                 if ( 'promote' == $bp->action_variables[1] && ( 'mod' == $bp->action_variables[2] || 'admin' == $bp->action_variables[2] ) && is_numeric( $bp->action_variables[3] ) ) {
    767                         $user_id = $bp->action_variables[3];
    768                         $status = $bp->action_variables[2];
    769 
    770                         // Check the nonce first.
    771                         if ( !check_admin_referer( 'groups_promote_member' ) )
    772                                 return false;
    773 
    774                         // Promote a user.
    775                         if ( !groups_promote_member( $user_id, $bp->groups->current_group->id, $status ) )
    776                                 bp_core_add_message( __( 'There was an error when promoting that user, please try again', 'buddypress' ), 'error' );
    777                         else
    778                                 bp_core_add_message( __( 'User promoted successfully', 'buddypress' ) );
    779 
    780                         do_action( 'groups_promoted_member', $user_id, $bp->groups->current_group->id );
    781 
    782                         bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . 'admin/manage-members/' );
    783                 }
    784 
    785                 if ( 'demote' == $bp->action_variables[1] && is_numeric( $bp->action_variables[2] ) ) {
    786                         $user_id = $bp->action_variables[2];
    787 
    788                         // Check the nonce first.
    789                         if ( !check_admin_referer( 'groups_demote_member' ) )
    790                                 return false;
    791 
    792                         // Demote a user.
    793                         if ( !groups_demote_member( $user_id, $bp->groups->current_group->id ) )
    794                                 bp_core_add_message( __( 'There was an error when demoting that user, please try again', 'buddypress' ), 'error' );
    795                         else
    796                                 bp_core_add_message( __( 'User demoted successfully', 'buddypress' ) );
    797 
    798                         do_action( 'groups_demoted_member', $user_id, $bp->groups->current_group->id );
    799 
    800                         bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . 'admin/manage-members/' );
    801                 }
    802 
    803                 if ( 'ban' == $bp->action_variables[1] && is_numeric( $bp->action_variables[2] ) ) {
    804                         $user_id = $bp->action_variables[2];
    805 
    806                         // Check the nonce first.
    807                         if ( !check_admin_referer( 'groups_ban_member' ) )
    808                                 return false;
    809 
    810                         // Ban a user.
    811                         if ( !groups_ban_member( $user_id, $bp->groups->current_group->id ) )
    812                                 bp_core_add_message( __( 'There was an error when banning that user, please try again', 'buddypress' ), 'error' );
    813                         else
    814                                 bp_core_add_message( __( 'User banned successfully', 'buddypress' ) );
    815 
    816                         do_action( 'groups_banned_member', $user_id, $bp->groups->current_group->id );
    817 
    818                         bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . 'admin/manage-members/' );
    819                 }
    820 
    821                 if ( 'unban' == $bp->action_variables[1] && is_numeric( $bp->action_variables[2] ) ) {
    822                         $user_id = $bp->action_variables[2];
    823 
    824                         // Check the nonce first.
    825                         if ( !check_admin_referer( 'groups_unban_member' ) )
    826                                 return false;
    827 
    828                         // Remove a ban for user.
    829                         if ( !groups_unban_member( $user_id, $bp->groups->current_group->id ) )
    830                                 bp_core_add_message( __( 'There was an error when unbanning that user, please try again', 'buddypress' ), 'error' );
    831                         else
    832                                 bp_core_add_message( __( 'User ban removed successfully', 'buddypress' ) );
    833 
    834                         do_action( 'groups_unbanned_member', $user_id, $bp->groups->current_group->id );
    835 
    836                         bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . 'admin/manage-members/' );
    837                 }
    838 
    839                 if ( 'remove' == $bp->action_variables[1] && is_numeric( $bp->action_variables[2] ) ) {
    840                         $user_id = $bp->action_variables[2];
    841 
    842                         // Check the nonce first.
    843                         if ( !check_admin_referer( 'groups_remove_member' ) )
    844                                 return false;
    845 
    846                         // Remove a user.
    847                         if ( !groups_remove_member( $user_id, $bp->groups->current_group->id ) )
    848                                 bp_core_add_message( __( 'There was an error removing that user from the group, please try again', 'buddypress' ), 'error' );
    849                         else
    850                                 bp_core_add_message( __( 'User removed successfully', 'buddypress' ) );
    851 
    852                         do_action( 'groups_removed_member', $user_id, $bp->groups->current_group->id );
    853 
    854                         bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . 'admin/manage-members/' );
    855                 }
    856 
    857                 do_action( 'groups_screen_group_admin_manage_members', $bp->groups->current_group->id );
    858 
    859                 bp_core_load_template( apply_filters( 'groups_template_group_admin_manage_members', 'groups/single/home' ) );
    860         }
    861 }
    862 add_action( 'wp', 'groups_screen_group_admin_manage_members', 4 );
    863 
    864 function groups_screen_group_admin_requests() {
    865         global $bp;
    866 
    867         if ( bp_is_current_component( 'groups' ) && isset( $bp->action_variables[0] ) && 'membership-requests' == $bp->action_variables[0] ) {
    868 
    869                 // Ask for a login if the user is coming here via an email notification
    870                 if ( !is_user_logged_in() )
    871                         bp_core_redirect( site_url( 'wp-login.php?redirect_to=' . $bp->root_domain . '/' . $bp->current_component . '/' . $bp->current_item . '/admin/membership-requests/' ) );
    872 
    873                 if ( !$bp->is_item_admin || 'public' == $bp->groups->current_group->status )
    874                         return false;
    875 
    876                 // Remove any screen notifications
    877                 bp_users_delete_notifications_by_type( $bp->loggedin_user->id, $bp->groups->id, 'new_membership_request' );
    878 
    879                 $request_action = $bp->action_variables[1];
    880                 $membership_id = $bp->action_variables[2];
    881 
    882                 if ( isset($request_action) && isset($membership_id) ) {
    883                         if ( 'accept' == $request_action && is_numeric($membership_id) ) {
    884 
    885                                 // Check the nonce first.
    886                                 if ( !check_admin_referer( 'groups_accept_membership_request' ) )
    887                                         return false;
    888 
    889                                 // Accept the membership request
    890                                 if ( !groups_accept_membership_request( $membership_id ) ) {
    891                                         bp_core_add_message( __( 'There was an error accepting the membership request, please try again.', 'buddypress' ), 'error' );
    892                                 } else {
    893                                         bp_core_add_message( __( 'Group membership request accepted', 'buddypress' ) );
    894                                 }
    895 
    896                         } else if ( 'reject' == $request_action && is_numeric($membership_id) ) {
    897                                 /* Check the nonce first. */
    898                                 if ( !check_admin_referer( 'groups_reject_membership_request' ) )
    899                                         return false;
    900 
    901                                 // Reject the membership request
    902                                 if ( !groups_reject_membership_request( $membership_id ) ) {
    903                                         bp_core_add_message( __( 'There was an error rejecting the membership request, please try again.', 'buddypress' ), 'error' );
    904                                 } else {
    905                                         bp_core_add_message( __( 'Group membership request rejected', 'buddypress' ) );
    906                                 }
    907 
    908                         }
    909 
    910                         do_action( 'groups_group_request_managed', $bp->groups->current_group->id, $request_action, $membership_id );
    911 
    912                         bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . 'admin/membership-requests/' );
    913                 }
    914 
    915                 do_action( 'groups_screen_group_admin_requests', $bp->groups->current_group->id );
    916 
    917                 bp_core_load_template( apply_filters( 'groups_template_group_admin_requests', 'groups/single/home' ) );
    918         }
    919 }
    920 add_action( 'wp', 'groups_screen_group_admin_requests', 4 );
    921 
    922 function groups_screen_group_admin_delete_group() {
    923         global $bp;
    924 
    925         if ( bp_is_current_component( 'groups' ) && isset( $bp->action_variables[0] ) && 'delete-group' == $bp->action_variables[0] ) {
    926 
    927                 if ( !$bp->is_item_admin && !is_super_admin() )
    928                         return false;
    929 
    930                 if ( isset( $_REQUEST['delete-group-button'] ) && isset( $_REQUEST['delete-group-understand'] ) ) {
    931                         // Check the nonce first.
    932                         if ( !check_admin_referer( 'groups_delete_group' ) )
    933                                 return false;
    934 
    935                         do_action( 'groups_before_group_deleted', $bp->groups->current_group->id );
    936 
    937                         // Group admin has deleted the group, now do it.
    938                         if ( !groups_delete_group( $bp->groups->current_group->id ) ) {
    939                                 bp_core_add_message( __( 'There was an error deleting the group, please try again.', 'buddypress' ), 'error' );
    940                         } else {
    941                                 bp_core_add_message( __( 'The group was deleted successfully', 'buddypress' ) );
    942 
    943                                 do_action( 'groups_group_deleted', $bp->groups->current_group->id );
    944 
    945                                 bp_core_redirect( $bp->loggedin_user->domain . $bp->groups->slug . '/' );
    946                         }
    947 
    948                         bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component );
    949                 }
    950 
    951                 do_action( 'groups_screen_group_admin_delete_group', $bp->groups->current_group->id );
    952 
    953                 bp_core_load_template( apply_filters( 'groups_template_group_admin_delete_group', 'groups/single/home' ) );
    954         }
    955 }
    956 add_action( 'wp', 'groups_screen_group_admin_delete_group', 4 );
    957 
    958 function groups_screen_notification_settings() {
    959         global $bp;
    960 
    961         if ( !$group_invite = get_user_meta( $bp->displayed_user->id, 'notification_groups_invite', true ) )
    962                 $group_invite  = 'yes';
    963 
    964         if ( !$group_update = get_user_meta( $bp->displayed_user->id, 'notification_groups_group_updated', true ) )
    965                 $group_update  = 'yes';
    966 
    967         if ( !$group_promo = get_user_meta( $bp->displayed_user->id, 'notification_groups_admin_promotion', true ) )
    968                 $group_promo   = 'yes';
    969 
    970         if ( !$group_request = get_user_meta( $bp->displayed_user->id, 'notification_groups_membership_request', true ) )
    971                 $group_request = 'yes';
     349// Create the groups component
     350$bp->groups = new BP_Groups_Component();
     351
    972352?>
    973 
    974         <table class="notification-settings zebra" id="groups-notification-settings">
    975                 <thead>
    976                         <tr>
    977                                 <th class="icon"></th>
    978                                 <th class="title"><?php _e( 'Groups', 'buddypress' ) ?></th>
    979                                 <th class="yes"><?php _e( 'Yes', 'buddypress' ) ?></th>
    980                                 <th class="no"><?php _e( 'No', 'buddypress' )?></th>
    981                         </tr>
    982                 </thead>
    983 
    984                 <tbody>
    985                         <tr id="groups-notification-settings-invitation">
    986                                 <td></td>
    987                                 <td><?php _e( 'A member invites you to join a group', 'buddypress' ) ?></td>
    988                                 <td class="yes"><input type="radio" name="notifications[notification_groups_invite]" value="yes" <?php checked( $group_invite, 'yes', true ) ?>/></td>
    989                                 <td class="no"><input type="radio" name="notifications[notification_groups_invite]" value="no" <?php checked( $group_invite, 'no', true ) ?>/></td>
    990                         </tr>
    991                         <tr id="groups-notification-settings-info-updated">
    992                                 <td></td>
    993                                 <td><?php _e( 'Group information is updated', 'buddypress' ) ?></td>
    994                                 <td class="yes"><input type="radio" name="notifications[notification_groups_group_updated]" value="yes" <?php checked( $group_update, 'yes', true ) ?>/></td>
    995                                 <td class="no"><input type="radio" name="notifications[notification_groups_group_updated]" value="no" <?php checked( $group_update, 'no', true ) ?>/></td>
    996                         </tr>
    997                         <tr id="groups-notification-settings-promoted">
    998                                 <td></td>
    999                                 <td><?php _e( 'You are promoted to a group administrator or moderator', 'buddypress' ) ?></td>
    1000                                 <td class="yes"><input type="radio" name="notifications[notification_groups_admin_promotion]" value="yes" <?php checked( $group_promo, 'yes', true ) ?>/></td>
    1001                                 <td class="no"><input type="radio" name="notifications[notification_groups_admin_promotion]" value="no" <?php checked( $group_promo, 'no', true ) ?>/></td>
    1002                         </tr>
    1003                         <tr id="groups-notification-settings-request">
    1004                                 <td></td>
    1005                                 <td><?php _e( 'A member requests to join a private group for which you are an admin', 'buddypress' ) ?></td>
    1006                                 <td class="yes"><input type="radio" name="notifications[notification_groups_membership_request]" value="yes" <?php checked( $group_request, 'yes', true ) ?>/></td>
    1007                                 <td class="no"><input type="radio" name="notifications[notification_groups_membership_request]" value="no" <?php checked( $group_request, 'no', true ) ?>/></td>
    1008                         </tr>
    1009 
    1010                         <?php do_action( 'groups_screen_notification_settings' ); ?>
    1011 
    1012                 </tbody>
    1013         </table>
    1014 
    1015 <?php
    1016 }
    1017 add_action( 'bp_notification_settings', 'groups_screen_notification_settings' );
    1018 
    1019 
    1020 /********************************************************************************
    1021  * Action Functions
    1022  *
    1023  * Action functions are exactly the same as screen functions, however they do not
    1024  * have a template screen associated with them. Usually they will send the user
    1025  * back to the default screen after execution.
    1026  */
    1027 
    1028 function groups_action_create_group() {
    1029         global $bp;
    1030 
    1031         // If we're not at domain.org/groups/create/ then return false
    1032         if ( !bp_is_current_component( $bp->groups->slug ) || 'create' != $bp->current_action )
    1033                 return false;
    1034 
    1035         if ( !is_user_logged_in() )
    1036                 return false;
    1037 
    1038         // Make sure creation steps are in the right order
    1039         groups_action_sort_creation_steps();
    1040 
    1041         // If no current step is set, reset everything so we can start a fresh group creation
    1042         if ( !isset( $bp->action_variables[1] ) || !$bp->groups->current_create_step = $bp->action_variables[1] ) {
    1043 
    1044                 unset( $bp->groups->current_create_step );
    1045                 unset( $bp->groups->completed_create_steps );
    1046 
    1047                 setcookie( 'bp_new_group_id', false, time() - 1000, COOKIEPATH );
    1048                 setcookie( 'bp_completed_create_steps', false, time() - 1000, COOKIEPATH );
    1049 
    1050                 $reset_steps = true;
    1051                 bp_core_redirect( $bp->root_domain . '/' . $bp->groups->root_slug . '/create/step/' . array_shift( array_keys( $bp->groups->group_creation_steps ) ) . '/' );
    1052         }
    1053 
    1054         // If this is a creation step that is not recognized, just redirect them back to the first screen
    1055         if ( $bp->action_variables[1] && !$bp->groups->group_creation_steps[$bp->action_variables[1]] ) {
    1056                 bp_core_add_message( __('There was an error saving group details. Please try again.', 'buddypress'), 'error' );
    1057                 bp_core_redirect( $bp->root_domain . '/' . $bp->groups->root_slug . '/create/' );
    1058         }
    1059 
    1060         // Fetch the currently completed steps variable
    1061         if ( isset( $_COOKIE['bp_completed_create_steps'] ) && !isset( $reset_steps ) )
    1062                 $bp->groups->completed_create_steps = unserialize( stripslashes( $_COOKIE['bp_completed_create_steps'] ) );
    1063 
    1064         // Set the ID of the new group, if it has already been created in a previous step
    1065         if ( isset( $_COOKIE['bp_new_group_id'] ) ) {
    1066                 $bp->groups->new_group_id = $_COOKIE['bp_new_group_id'];
    1067                 $bp->groups->current_group = new BP_Groups_Group( $bp->groups->new_group_id );
    1068         }
    1069 
    1070         // If the save, upload or skip button is hit, lets calculate what we need to save
    1071         if ( isset( $_POST['save'] ) ) {
    1072 
    1073                 // Check the nonce
    1074                 check_admin_referer( 'groups_create_save_' . $bp->groups->current_create_step );
    1075 
    1076                 if ( 'group-details' == $bp->groups->current_create_step ) {
    1077                         if ( empty( $_POST['group-name'] ) || empty( $_POST['group-desc'] ) || !strlen( trim( $_POST['group-name'] ) ) || !strlen( trim( $_POST['group-desc'] ) ) ) {
    1078                                 bp_core_add_message( __( 'Please fill in all of the required fields', 'buddypress' ), 'error' );
    1079                                 bp_core_redirect( $bp->root_domain . '/' . $bp->groups->root_slug . '/create/step/' . $bp->groups->current_create_step . '/' );
    1080                         }
    1081 
    1082                         $new_group_id = isset( $bp->groups->new_group_id ) ? $bp->groups->new_group_id : 0;
    1083 
    1084                         if ( !$bp->groups->new_group_id = groups_create_group( array( 'group_id' => $new_group_id, 'name' => $_POST['group-name'], 'description' => $_POST['group-desc'], 'slug' => groups_check_slug( sanitize_title( esc_attr( $_POST['group-name'] ) ) ), 'date_created' => bp_core_current_time(), 'status' => 'public' ) ) ) {
    1085                                 bp_core_add_message( __( 'There was an error saving group details, please try again.', 'buddypress' ), 'error' );
    1086                                 bp_core_redirect( $bp->root_domain . '/' . $bp->groups->root_slug . '/create/step/' . $bp->groups->current_create_step . '/' );
    1087                         }
    1088 
    1089                         groups_update_groupmeta( $bp->groups->new_group_id, 'total_member_count', 1 );
    1090                         groups_update_groupmeta( $bp->groups->new_group_id, 'last_activity', bp_core_current_time() );
    1091                 }
    1092 
    1093                 if ( 'group-settings' == $bp->groups->current_create_step ) {
    1094                         $group_status = 'public';
    1095                         $group_enable_forum = 1;
    1096 
    1097                         if ( !isset($_POST['group-show-forum']) ) {
    1098                                 $group_enable_forum = 0;
    1099                         } else {
    1100                                 // Create the forum if enable_forum = 1
    1101                                 if ( function_exists( 'bp_forums_setup' ) && '' == groups_get_groupmeta( $bp->groups->new_group_id, 'forum_id' ) ) {
    1102                                         groups_new_group_forum();
    1103                                 }
    1104                         }
    1105 
    1106                         if ( 'private' == $_POST['group-status'] )
    1107                                 $group_status = 'private';
    1108                         else if ( 'hidden' == $_POST['group-status'] )
    1109                                 $group_status = 'hidden';
    1110 
    1111                         if ( !$bp->groups->new_group_id = groups_create_group( array( 'group_id' => $bp->groups->new_group_id, 'status' => $group_status, 'enable_forum' => $group_enable_forum ) ) ) {
    1112                                 bp_core_add_message( __( 'There was an error saving group details, please try again.', 'buddypress' ), 'error' );
    1113                                 bp_core_redirect( $bp->root_domain . '/' . $bp->groups->root_slug . '/create/step/' . $bp->groups->current_create_step . '/' );
    1114                         }
    1115                 }
    1116 
    1117                 if ( 'group-invites' == $bp->groups->current_create_step )
    1118                         groups_send_invites( $bp->loggedin_user->id, $bp->groups->new_group_id );
    1119 
    1120                 do_action( 'groups_create_group_step_save_' . $bp->groups->current_create_step );
    1121                 do_action( 'groups_create_group_step_complete' ); // Mostly for clearing cache on a generic action name
    1122 
    1123                 /**
    1124                  * Once we have successfully saved the details for this step of the creation process
    1125                  * we need to add the current step to the array of completed steps, then update the cookies
    1126                  * holding the information
    1127                  */
    1128                 $completed_create_steps = isset( $bp->groups->completed_create_steps ) ? $bp->groups->completed_create_steps : array();
    1129                 if ( !in_array( $bp->groups->current_create_step, $completed_create_steps ) )
    1130                         $bp->groups->completed_create_steps[] = $bp->groups->current_create_step;
    1131 
    1132                 // Reset cookie info
    1133                 setcookie( 'bp_new_group_id', $bp->groups->new_group_id, time()+60*60*24, COOKIEPATH );
    1134                 setcookie( 'bp_completed_create_steps', serialize( $bp->groups->completed_create_steps ), time()+60*60*24, COOKIEPATH );
    1135 
    1136                 // If we have completed all steps and hit done on the final step we
    1137                 // can redirect to the completed group
    1138                 if ( count( $bp->groups->completed_create_steps ) == count( $bp->groups->group_creation_steps ) && $bp->groups->current_create_step == array_pop( array_keys( $bp->groups->group_creation_steps ) ) ) {
    1139                         unset( $bp->groups->current_create_step );
    1140                         unset( $bp->groups->completed_create_steps );
    1141 
    1142                         // Once we compelete all steps, record the group creation in the activity stream.
    1143                         groups_record_activity( array(
    1144                                 'action' => apply_filters( 'groups_activity_created_group_action', sprintf( __( '%1$s created the group %2$s', 'buddypress'), bp_core_get_userlink( $bp->loggedin_user->id ), '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' ) ),
    1145                                 'type' => 'created_group',
    1146                                 'item_id' => $bp->groups->new_group_id
    1147                         ) );
    1148 
    1149                         do_action( 'groups_group_create_complete', $bp->groups->new_group_id );
    1150 
    1151                         bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) );
    1152                 } else {
    1153                         /**
    1154                          * Since we don't know what the next step is going to be (any plugin can insert steps)
    1155                          * we need to loop the step array and fetch the next step that way.
    1156                          */
    1157                         foreach ( (array)$bp->groups->group_creation_steps as $key => $value ) {
    1158                                 if ( $key == $bp->groups->current_create_step ) {
    1159                                         $next = 1;
    1160                                         continue;
    1161                                 }
    1162 
    1163                                 if ( isset( $next ) ) {
    1164                                         $next_step = $key;
    1165                                         break;
    1166                                 }
    1167                         }
    1168 
    1169                         bp_core_redirect( $bp->root_domain . '/' . $bp->groups->root_slug . '/create/step/' . $next_step . '/' );
    1170                 }
    1171         }
    1172 
    1173         // Group avatar is handled separately
    1174         if ( 'group-avatar' == $bp->groups->current_create_step && isset( $_POST['upload'] ) ) {
    1175                 if ( !empty( $_FILES ) && isset( $_POST['upload'] ) ) {
    1176                         // Normally we would check a nonce here, but the group save nonce is used instead
    1177 
    1178                         // Pass the file to the avatar upload handler
    1179                         if ( bp_core_avatar_handle_upload( $_FILES, 'groups_avatar_upload_dir' ) ) {
    1180                                 $bp->avatar_admin->step = 'crop-image';
    1181 
    1182                                 // Make sure we include the jQuery jCrop file for image cropping
    1183                                 add_action( 'wp_print_scripts', 'bp_core_add_jquery_cropper' );
    1184                         }
    1185                 }
    1186 
    1187                 // If the image cropping is done, crop the image and save a full/thumb version
    1188                 if ( isset( $_POST['avatar-crop-submit'] ) && isset( $_POST['upload'] ) ) {
    1189                         // Normally we would check a nonce here, but the group save nonce is used instead
    1190 
    1191                         if ( !bp_core_avatar_handle_crop( array( 'object' => 'group', 'avatar_dir' => 'group-avatars', 'item_id' => $bp->groups->current_group->id, 'original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h'] ) ) )
    1192                                 bp_core_add_message( __( 'There was an error saving the group avatar, please try uploading again.', 'buddypress' ), 'error' );
    1193                         else
    1194                                 bp_core_add_message( __( 'The group avatar was uploaded successfully!', 'buddypress' ) );
    1195                 }
    1196         }
    1197 
    1198         bp_core_load_template( apply_filters( 'groups_template_create_group', 'groups/create' ) );
    1199 }
    1200 add_action( 'wp', 'groups_action_create_group', 3 );
    1201 
    1202 function groups_action_join_group() {
    1203         global $bp;
    1204 
    1205         if ( !$bp->is_single_item || !bp_is_current_component( $bp->groups->slug ) || $bp->current_action != 'join' )
    1206                 return false;
    1207 
    1208         // Nonce check
    1209         if ( !check_admin_referer( 'groups_join_group' ) )
    1210                 return false;
    1211 
    1212         // Skip if banned or already a member
    1213         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 ) ) {
    1214 
    1215                 // User wants to join a group that is not public
    1216                 if ( $bp->groups->current_group->status != 'public' ) {
    1217                         if ( !groups_check_user_has_invite( $bp->loggedin_user->id, $bp->groups->current_group->id ) ) {
    1218                                 bp_core_add_message( __( 'There was an error joining the group.', 'buddypress' ), 'error' );
    1219                                 bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) );
    1220                         }
    1221                 }
    1222 
    1223                 // User wants to join any group
    1224                 if ( !groups_join_group( $bp->groups->current_group->id ) )
    1225                         bp_core_add_message( __( 'There was an error joining the group.', 'buddypress' ), 'error' );
    1226                 else
    1227                         bp_core_add_message( __( 'You joined the group!', 'buddypress' ) );
    1228 
    1229                 bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) );
    1230         }
    1231 
    1232         bp_core_load_template( apply_filters( 'groups_template_group_home', 'groups/single/home' ) );
    1233 }
    1234 add_action( 'wp', 'groups_action_join_group', 3 );
    1235 
    1236 
    1237 function groups_action_leave_group() {
    1238         global $bp;
    1239 
    1240         if ( !$bp->is_single_item || !bp_is_current_component( $bp->groups->slug ) || $bp->current_action != 'leave-group' )
    1241                 return false;
    1242 
    1243         // Nonce check
    1244         if ( !check_admin_referer( 'groups_leave_group' ) )
    1245                 return false;
    1246 
    1247         // User wants to leave any group
    1248         if ( groups_is_user_member( $bp->loggedin_user->id, $bp->groups->current_group->id ) ) {
    1249                 if ( !groups_leave_group( $bp->groups->current_group->id ) ) {
    1250                         bp_core_add_message( __( 'There was an error leaving the group.', 'buddypress' ), 'error' );
    1251                 } else {
    1252                         bp_core_add_message( __( 'You successfully left the group.', 'buddypress' ) );
    1253                 }
    1254                 bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) );
    1255         }
    1256 
    1257         bp_core_load_template( apply_filters( 'groups_template_group_home', 'groups/single/home' ) );
    1258 }
    1259 add_action( 'wp', 'groups_action_leave_group', 3 );
    1260 
    1261 
    1262 function groups_action_sort_creation_steps() {
    1263         global $bp;
    1264 
    1265         if ( !bp_is_current_component( BP_GROUPS_SLUG ) && $bp->current_action != 'create' )
    1266                 return false;
    1267 
    1268         if ( !is_array( $bp->groups->group_creation_steps ) )
    1269                 return false;
    1270 
    1271         foreach ( (array)$bp->groups->group_creation_steps as $slug => $step ) {
    1272                 while ( !empty( $temp[$step['position']] ) )
    1273                         $step['position']++;
    1274 
    1275                 $temp[$step['position']] = array( 'name' => $step['name'], 'slug' => $slug );
    1276         }
    1277 
    1278         // Sort the steps by their position key
    1279         ksort($temp);
    1280         unset($bp->groups->group_creation_steps);
    1281 
    1282         foreach( (array)$temp as $position => $step )
    1283                 $bp->groups->group_creation_steps[$step['slug']] = array( 'name' => $step['name'], 'position' => $position );
    1284 }
    1285 
    1286 function groups_action_redirect_to_random_group() {
    1287         global $bp, $wpdb;
    1288 
    1289         if ( bp_is_current_component( $bp->groups->slug ) && isset( $_GET['random-group'] ) ) {
    1290                 $group = groups_get_groups( array( 'type' => 'random', 'per_page' => 1 ) );
    1291 
    1292                 bp_core_redirect( $bp->root_domain . '/' . $bp->groups->slug . '/' . $group['groups'][0]->slug . '/' );
    1293         }
    1294 }
    1295 add_action( 'wp', 'groups_action_redirect_to_random_group', 6 );
    1296 
    1297 function groups_action_group_feed() {
    1298         global $bp, $wp_query;
    1299 
    1300         if ( !bp_is_active( 'activity' ) || !bp_is_current_component( $bp->groups->slug ) || !isset( $bp->groups->current_group ) || $bp->current_action != 'feed' )
    1301                 return false;
    1302 
    1303         $wp_query->is_404 = false;
    1304         status_header( 200 );
    1305 
    1306         if ( 'public' != $bp->groups->current_group->status ) {
    1307                 if ( !groups_is_user_member( $bp->loggedin_user->id, $bp->groups->current_group->id ) )
    1308                         return false;
    1309         }
    1310 
    1311         include_once( 'bp-activity/feeds/bp-activity-group-feed.php' );
    1312         die;
    1313 }
    1314 add_action( 'wp', 'groups_action_group_feed', 3 );
    1315 
    1316 
    1317 /********************************************************************************
    1318  * Activity & Notification Functions
    1319  *
    1320  * These functions handle the recording, deleting and formatting of activity and
    1321  * notifications for the user and for this specific component.
    1322  */
    1323 
    1324 function groups_register_activity_actions() {
    1325         global $bp;
    1326 
    1327         if ( !function_exists( 'bp_activity_set_action' ) )
    1328                 return false;
    1329 
    1330         bp_activity_set_action( $bp->groups->id, 'created_group',   __( 'Created a group',       'buddypress' ) );
    1331         bp_activity_set_action( $bp->groups->id, 'joined_group',    __( 'Joined a group',        'buddypress' ) );
    1332         bp_activity_set_action( $bp->groups->id, 'new_forum_topic', __( 'New group forum topic', 'buddypress' ) );
    1333         bp_activity_set_action( $bp->groups->id, 'new_forum_post',  __( 'New group forum post',  'buddypress' ) );
    1334 
    1335         do_action( 'groups_register_activity_actions' );
    1336 }
    1337 add_action( 'bp_register_activity_actions', 'groups_register_activity_actions' );
    1338 
    1339 function groups_record_activity( $args = '' ) {
    1340         global $bp;
    1341 
    1342         if ( !function_exists( 'bp_activity_add' ) )
    1343                 return false;
    1344 
    1345         // If the group is not public, hide the activity sitewide.
    1346         if ( 'public' == $bp->groups->current_group->status )
    1347                 $hide_sitewide = false;
    1348         else
    1349                 $hide_sitewide = true;
    1350 
    1351         $defaults = array (
    1352                 'id'                => false,
    1353                 'user_id'           => $bp->loggedin_user->id,
    1354                 'action'            => '',
    1355                 'content'           => '',
    1356                 'primary_link'      => '',
    1357                 'component'         => $bp->groups->id,
    1358                 'type'              => false,
    1359                 'item_id'           => false,
    1360                 'secondary_item_id' => false,
    1361                 'recorded_time'     => bp_core_current_time(),
    1362                 'hide_sitewide'     => $hide_sitewide
    1363         );
    1364 
    1365         $r = wp_parse_args( $args, $defaults );
    1366         extract( $r );
    1367 
    1368         return bp_activity_add( array( 'id' => $id, 'user_id' => $user_id, 'action' => $action, 'content' => $content, 'primary_link' => $primary_link, 'component' => $component, 'type' => $type, 'item_id' => $item_id, 'secondary_item_id' => $secondary_item_id, 'recorded_time' => $recorded_time, 'hide_sitewide' => $hide_sitewide ) );
    1369 }
    1370 
    1371 function groups_update_last_activity( $group_id = 0 ) {
    1372         global $bp;
    1373 
    1374         if ( !$group_id )
    1375                 $group_id = $bp->groups->current_group->id;
    1376 
    1377         if ( !$group_id )
    1378                 return false;
    1379 
    1380         groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
    1381 }
    1382 add_action( 'groups_leave_group', 'groups_update_last_activity' );
    1383 add_action( 'groups_created_group', 'groups_update_last_activity' );
    1384 add_action( 'groups_new_forum_topic', 'groups_update_last_activity' );
    1385 add_action( 'groups_new_forum_topic_post', 'groups_update_last_activity' );
    1386 
    1387 function groups_format_notifications( $action, $item_id, $secondary_item_id, $total_items ) {
    1388         global $bp;
    1389 
    1390         switch ( $action ) {
    1391                 case 'new_membership_request':
    1392                         $group_id = $secondary_item_id;
    1393                         $requesting_user_id = $item_id;
    1394 
    1395                         $group = new BP_Groups_Group( $group_id );
    1396 
    1397                         $group_link = bp_get_group_permalink( $group );
    1398 
    1399                         if ( (int)$total_items > 1 ) {
    1400                                 return apply_filters( 'bp_groups_multiple_new_membership_requests_notification', '<a href="' . $group_link . '/admin/membership-requests/?n=1" title="' . __( 'Group Membership Requests', 'buddypress' ) . '">' . sprintf( __( '%1$d new membership requests for the group "%2$s"', 'buddypress' ), (int)$total_items, $group->name ) . '</a>', $group_link, $total_items, $group->name );
    1401                         } else {
    1402                                 $user_fullname = bp_core_get_user_displayname( $requesting_user_id );
    1403                                 return apply_filters( 'bp_groups_single_new_membership_request_notification', '<a href="' . $group_link . 'admin/membership-requests/?n=1" title="' . sprintf( __( '%s requests group membership', 'buddypress' ), $user_fullname ) . '">' . sprintf( __( '%1$s requests membership for the group "%2$s"', 'buddypress' ), $user_fullname, $group->name ) . '</a>', $group_link, $user_fullname, $group->name );
    1404                         }
    1405                         break;
    1406 
    1407                 case 'membership_request_accepted':
    1408                         $group_id = $item_id;
    1409 
    1410                         $group = new BP_Groups_Group( $group_id );
    1411                         $group_link = bp_get_group_permalink( $group );
    1412 
    1413                         if ( (int)$total_items > 1 )
    1414                                 return apply_filters( 'bp_groups_multiple_membership_request_accepted_notification', '<a href="' . $bp->loggedin_user->domain . $bp->groups->slug . '/?n=1" title="' . __( 'Groups', 'buddypress' ) . '">' . sprintf( __( '%d accepted group membership requests', 'buddypress' ), (int)$total_items, $group->name ) . '</a>', $total_items, $group_name );
    1415                         else
    1416                                 return apply_filters( 'bp_groups_single_membership_request_accepted_notification', '<a href="' . $group_link . '?n=1">' . sprintf( __( 'Membership for group "%s" accepted', 'buddypress' ), $group->name ) . '</a>', $group_link, $group->name );
    1417 
    1418                         break;
    1419 
    1420                 case 'membership_request_rejected':
    1421                         $group_id = $item_id;
    1422 
    1423                         $group = new BP_Groups_Group( $group_id );
    1424                         $group_link = bp_get_group_permalink( $group );
    1425 
    1426                         if ( (int)$total_items > 1 )
    1427                                 return apply_filters( 'bp_groups_multiple_membership_request_rejected_notification', '<a href="' . site_url() . '/' . $bp->members->slug . '/' . $bp->groups->slug . '/?n=1" title="' . __( 'Groups', 'buddypress' ) . '">' . sprintf( __( '%d rejected group membership requests', 'buddypress' ), (int)$total_items, $group->name ) . '</a>', $total_items, $group->name );
    1428                         else
    1429                                 return apply_filters( 'bp_groups_single_membership_request_rejected_notification', '<a href="' . $group_link . '?n=1">' . sprintf( __( 'Membership for group "%s" rejected', 'buddypress' ), $group->name ) . '</a>', $group_link, $group->name );
    1430 
    1431                         break;
    1432 
    1433                 case 'member_promoted_to_admin':
    1434                         $group_id = $item_id;
    1435 
    1436                         $group = new BP_Groups_Group( $group_id );
    1437                         $group_link = bp_get_group_permalink( $group );
    1438 
    1439                         if ( (int)$total_items > 1 )
    1440                                 return apply_filters( 'bp_groups_multiple_member_promoted_to_admin_notification', '<a href="' . $bp->loggedin_user->domain . $bp->groups->slug . '/?n=1" title="' . __( 'Groups', 'buddypress' ) . '">' . sprintf( __( 'You were promoted to an admin in %d groups', 'buddypress' ), (int)$total_items ) . '</a>', $total_items );
    1441                         else
    1442                                 return apply_filters( 'bp_groups_single_member_promoted_to_admin_notification', '<a href="' . $group_link . '?n=1">' . sprintf( __( 'You were promoted to an admin in the group %s', 'buddypress' ), $group->name ) . '</a>', $group_link, $group->name );
    1443 
    1444                         break;
    1445 
    1446                 case 'member_promoted_to_mod':
    1447                         $group_id = $item_id;
    1448 
    1449                         $group = new BP_Groups_Group( $group_id );
    1450                         $group_link = bp_get_group_permalink( $group );
    1451 
    1452                         if ( (int)$total_items > 1 )
    1453                                 return apply_filters( 'bp_groups_multiple_member_promoted_to_mod_notification', '<a href="' . $bp->loggedin_user->domain . $bp->groups->slug . '/?n=1" title="' . __( 'Groups', 'buddypress' ) . '">' . sprintf( __( 'You were promoted to a mod in %d groups', 'buddypress' ), (int)$total_items ) . '</a>', $total_items );
    1454                         else
    1455                                 return apply_filters( 'bp_groups_single_member_promoted_to_mod_notification', '<a href="' . $group_link . '?n=1">' . sprintf( __( 'You were promoted to a mod in the group %s', 'buddypress' ), $group->name ) . '</a>', $group_link, $group->name );
    1456 
    1457                         break;
    1458 
    1459                 case 'group_invite':
    1460                         $group_id = $item_id;
    1461 
    1462                         $group = new BP_Groups_Group( $group_id );
    1463                         $user_url = bp_core_get_user_domain( $bp->loggedin_user->id );
    1464 
    1465                         if ( (int)$total_items > 1 )
    1466                                 return apply_filters( 'bp_groups_multiple_group_invite_notification', '<a href="' . $bp->loggedin_user->domain . $bp->groups->slug . '/invites/?n=1" title="' . __( 'Group Invites', 'buddypress' ) . '">' . sprintf( __( 'You have %d new group invitations', 'buddypress' ), (int)$total_items ) . '</a>', $total_items );
    1467                         else
    1468                                 return apply_filters( 'bp_groups_single_group_invite_notification', '<a href="' . $bp->loggedin_user->domain . $bp->groups->slug . '/invites/?n=1" title="' . __( 'Group Invites', 'buddypress' ) . '">' . sprintf( __( 'You have an invitation to the group: %s', 'buddypress' ), $group->name ) . '</a>', $group->name );
    1469 
    1470                         break;
    1471         }
    1472 
    1473         do_action( 'groups_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
    1474 
    1475         return false;
    1476 }
    1477 
    1478 
    1479 /********************************************************************************
    1480  * Business Functions
    1481  *
    1482  * Business functions are where all the magic happens in BuddyPress. They will
    1483  * handle the actual saving or manipulation of information. Usually they will
    1484  * hand off to a database class for data access, then return
    1485  * true or false on success or failure.
    1486  */
    1487 
    1488 function groups_get_group( $args = '' ) {
    1489         $defaults = array(
    1490                 'group_id'   => false,
    1491                 'load_users' => false
    1492         );
    1493 
    1494         $args = wp_parse_args( $args, $defaults );
    1495         extract( $args, EXTR_SKIP );
    1496 
    1497         return apply_filters( 'groups_get_group', new BP_Groups_Group( $group_id, true, $load_users ) );
    1498 }
    1499 
    1500 /*** Group Creation, Editing & Deletion *****************************************/
    1501 
    1502 function groups_create_group( $args = '' ) {
    1503         global $bp;
    1504 
    1505         extract( $args );
    1506 
    1507         /**
    1508          * Possible parameters (pass as assoc array):
    1509          *      'group_id'
    1510          *      'creator_id'
    1511          *      'name'
    1512          *      'description'
    1513          *      'slug'
    1514          *      'status'
    1515          *      'enable_forum'
    1516          *      'date_created'
    1517          */
    1518 
    1519         if ( isset( $group_id ) && $group_id )
    1520                 $group = new BP_Groups_Group( $group_id );
    1521         else
    1522                 $group = new BP_Groups_Group;
    1523 
    1524         if ( isset( $creator_id ) && $creator_id )
    1525                 $group->creator_id = $creator_id;
    1526         else
    1527                 $group->creator_id = $bp->loggedin_user->id;
    1528 
    1529         if ( isset( $name ) )
    1530                 $group->name = $name;
    1531 
    1532         if ( isset( $description ) )
    1533                 $group->description = $description;
    1534 
    1535         if ( isset( $slug ) && groups_check_slug( $slug ) )
    1536                 $group->slug = $slug;
    1537 
    1538         if ( isset( $status ) ) {
    1539                 if ( groups_is_valid_status( $status ) )
    1540                         $group->status = $status;
    1541         }
    1542 
    1543         if ( isset( $enable_forum ) )
    1544                 $group->enable_forum = $enable_forum;
    1545         else if ( !$group_id && !isset( $enable_forum ) )
    1546                 $group->enable_forum = 1;
    1547 
    1548         if ( isset( $date_created ) )
    1549                 $group->date_created = $date_created;
    1550 
    1551         if ( !$group->save() )
    1552                 return false;
    1553 
    1554         // If this is a new group, set up the creator as the first member and admin
    1555         if ( !$group_id ) {
    1556                 $member                = new BP_Groups_Member;
    1557                 $member->group_id      = $group->id;
    1558                 $member->user_id       = $group->creator_id;
    1559                 $member->is_admin      = 1;
    1560                 $member->user_title    = __( 'Group Admin', 'buddypress' );
    1561                 $member->is_confirmed  = 1;
    1562                 $member->date_modified = bp_core_current_time();
    1563 
    1564                 $member->save();
    1565                 do_action( 'groups_create_group', $group->id, $member, $group );
    1566 
    1567         } else {
    1568                 do_action( 'groups_update_group', $group->id, $group );
    1569         }
    1570 
    1571         do_action( 'groups_created_group', $group->id );
    1572 
    1573         return $group->id;
    1574 }
    1575 
    1576 function groups_edit_base_group_details( $group_id, $group_name, $group_desc, $notify_members ) {
    1577         global $bp;
    1578 
    1579         if ( empty( $group_name ) || empty( $group_desc ) )
    1580                 return false;
    1581 
    1582         $group              = new BP_Groups_Group( $group_id );
    1583         $group->name        = $group_name;
    1584         $group->description = $group_desc;
    1585 
    1586         if ( !$group->save() )
    1587                 return false;
    1588 
    1589         if ( $notify_members ) {
    1590                 require_once ( BP_PLUGIN_DIR . '/bp-groups/bp-groups-notifications.php' );
    1591                 groups_notification_group_updated( $group->id );
    1592         }
    1593 
    1594         do_action( 'groups_details_updated', $group->id );
    1595 
    1596         return true;
    1597 }
    1598 
    1599 function groups_edit_group_settings( $group_id, $enable_forum, $status ) {
    1600         global $bp;
    1601 
    1602         $group = new BP_Groups_Group( $group_id );
    1603         $group->enable_forum = $enable_forum;
    1604 
    1605         /***
    1606          * Before we potentially switch the group status, if it has been changed to public
    1607          * from private and there are outstanding membership requests, auto-accept those requests.
    1608          */
    1609         if ( 'private' == $group->status && 'public' == $status )
    1610                 groups_accept_all_pending_membership_requests( $group->id );
    1611 
    1612         // Now update the status
    1613         $group->status = $status;
    1614 
    1615         if ( !$group->save() )
    1616                 return false;
    1617 
    1618         // If forums have been enabled, and a forum does not yet exist, we need to create one.
    1619         if ( $group->enable_forum ) {
    1620                 if ( function_exists( 'bp_forums_setup' ) && '' == groups_get_groupmeta( $group->id, 'forum_id' ) ) {
    1621                         groups_new_group_forum( $group->id, $group->name, $group->description );
    1622                 }
    1623         }
    1624 
    1625         groups_update_groupmeta( $group->id, 'last_activity', bp_core_current_time() );
    1626         do_action( 'groups_settings_updated', $group->id );
    1627 
    1628         return true;
    1629 }
    1630 
    1631 /**
    1632  * Delete a group and all of its associated meta
    1633  *
    1634  * @global object $bp BuddyPress global settings
    1635  * @param int $group_id
    1636  * @since 1.0
    1637  */
    1638 function groups_delete_group( $group_id ) {
    1639         global $bp;
    1640 
    1641         // Check the user is the group admin.
    1642         if ( !$bp->is_item_admin )
    1643                 return false;
    1644 
    1645         // Get the group object
    1646         $group = new BP_Groups_Group( $group_id );
    1647         if ( !$group->delete() )
    1648                 return false;
    1649 
    1650         do_action( 'groups_before_delete_group', $group_id );
    1651 
    1652         // Delete all group activity from activity streams
    1653         if ( bp_is_active( 'activity' ) )
    1654                 bp_activity_delete_by_item_id( array( 'item_id' => $group_id, 'component' => $bp->groups->id ) );
    1655 
    1656         // Remove all outstanding invites for this group
    1657         groups_delete_all_group_invites( $group_id );
    1658 
    1659         // Remove all notifications for any user belonging to this group
    1660         bp_core_delete_all_notifications_by_type( $group_id, $bp->groups->slug );
    1661 
    1662         // Remove forum if component is active and current group has one
    1663         if ( bp_is_active( 'forums' ) && $forum_id = groups_get_groupmeta( $group_id, 'forum_id' ) ) {
    1664                 do_action( 'bbpress_init' );
    1665                 bb_delete_forum( $forum_id );
    1666         }
    1667 
    1668         do_action( 'groups_delete_group', $group_id);
    1669 
    1670         return true;
    1671 }
    1672 
    1673 function groups_is_valid_status( $status ) {
    1674         global $bp;
    1675 
    1676         return in_array( $status, (array)$bp->groups->valid_status );
    1677 }
    1678 
    1679 function groups_check_slug( $slug ) {
    1680         global $bp;
    1681 
    1682         if ( 'wp' == substr( $slug, 0, 2 ) )
    1683                 $slug = substr( $slug, 2, strlen( $slug ) - 2 );
    1684 
    1685         if ( in_array( $slug, (array)$bp->groups->forbidden_names ) )
    1686                 $slug = $slug . '-' . rand();
    1687 
    1688         if ( BP_Groups_Group::check_slug( $slug ) ) {
    1689                 do {
    1690                         $slug = $slug . '-' . rand();
    1691                 }
    1692                 while ( BP_Groups_Group::check_slug( $slug ) );
    1693         }
    1694 
    1695         return $slug;
    1696 }
    1697 
    1698 function groups_get_slug( $group_id ) {
    1699         $group = new BP_Groups_Group( $group_id );
    1700         return $group->slug;
    1701 }
    1702 
    1703 /*** User Actions ***************************************************************/
    1704 
    1705 function groups_leave_group( $group_id, $user_id = 0 ) {
    1706         global $bp;
    1707 
    1708         if ( !$user_id )
    1709                 $user_id = $bp->loggedin_user->id;
    1710 
    1711         // Don't let single admins leave the group.
    1712         if ( count( groups_get_group_admins( $group_id ) ) < 2 ) {
    1713                 if ( groups_is_user_admin( $user_id, $group_id ) ) {
    1714                         bp_core_add_message( __( 'As the only Admin, you cannot leave the group.', 'buddypress' ), 'error' );
    1715                         return false;
    1716                 }
    1717         }
    1718 
    1719         $membership = new BP_Groups_Member( $user_id, $group_id );
    1720 
    1721         // This is exactly the same as deleting an invite, just is_confirmed = 1 NOT 0.
    1722         if ( !groups_uninvite_user( $user_id, $group_id ) )
    1723                 return false;
    1724 
    1725         // Modify group member count
    1726         groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') - 1 );
    1727 
    1728         // Modify user's group memberhip count
    1729         update_user_meta( $user_id, 'total_group_count', (int) get_user_meta( $user_id, 'total_group_count', true ) - 1 );
    1730 
    1731         /**
    1732          * If the user joined this group less than five minutes ago, remove the
    1733          * joined_group activity so users cannot flood the activity stream by
    1734          * joining/leaving the group in quick succession.
    1735          */
    1736         if ( function_exists( 'bp_activity_delete' ) && gmmktime() <= strtotime( '+5 minutes', (int)strtotime( $membership->date_modified ) ) )
    1737                 bp_activity_delete( array( 'component' => $bp->groups->id, 'type' => 'joined_group', 'user_id' => $user_id, 'item_id' => $group_id ) );
    1738 
    1739         bp_core_add_message( __( 'You successfully left the group.', 'buddypress' ) );
    1740 
    1741         do_action( 'groups_leave_group', $group_id, $user_id );
    1742 
    1743         return true;
    1744 }
    1745 
    1746 function groups_join_group( $group_id, $user_id = 0 ) {
    1747         global $bp;
    1748 
    1749         if ( !$user_id )
    1750                 $user_id = $bp->loggedin_user->id;
    1751 
    1752         // Check if the user has an outstanding invite, is so delete it.
    1753         if ( groups_check_user_has_invite( $user_id, $group_id ) )
    1754                 groups_delete_invite( $user_id, $group_id );
    1755 
    1756         // Check if the user has an outstanding request, is so delete it.
    1757         if ( groups_check_for_membership_request( $user_id, $group_id ) )
    1758                 groups_delete_membership_request( $user_id, $group_id );
    1759 
    1760         // User is already a member, just return true
    1761         if ( groups_is_user_member( $user_id, $group_id ) )
    1762                 return true;
    1763 
    1764         $new_member                = new BP_Groups_Member;
    1765         $new_member->group_id      = $group_id;
    1766         $new_member->user_id       = $user_id;
    1767         $new_member->inviter_id    = 0;
    1768         $new_member->is_admin      = 0;
    1769         $new_member->user_title    = '';
    1770         $new_member->date_modified = bp_core_current_time();
    1771         $new_member->is_confirmed  = 1;
    1772 
    1773         if ( !$new_member->save() )
    1774                 return false;
    1775 
    1776         if ( !isset( $bp->groups->current_group ) || !$bp->groups->current_group || $group_id != $bp->groups->current_group->id )
    1777                 $group = new BP_Groups_Group( $group_id );
    1778         else
    1779                 $group = $bp->groups->current_group;
    1780 
    1781         // Record this in activity streams
    1782         groups_record_activity( array(
    1783                 'action'  => apply_filters( 'groups_activity_joined_group', sprintf( __( '%1$s joined the group %2$s', 'buddypress'), bp_core_get_userlink( $user_id ), '<a href="' . bp_get_group_permalink( $group ) . '">' . esc_attr( bp_get_group_name( $group ) ) . '</a>' ) ),
    1784                 'type'    => 'joined_group',
    1785                 'item_id' => $group_id,
    1786                 'user_id' => $user_id
    1787         ) );
    1788 
    1789         // Modify group meta
    1790         groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') + 1 );
    1791         groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
    1792 
    1793         do_action( 'groups_join_group', $group_id, $user_id );
    1794 
    1795         return true;
    1796 }
    1797 
    1798 /*** General Group Functions ****************************************************/
    1799 
    1800 function groups_check_group_exists( $group_id ) {
    1801         return BP_Groups_Group::group_exists( $group_id );
    1802 }
    1803 
    1804 function groups_get_group_admins( $group_id ) {
    1805         return BP_Groups_Member::get_group_administrator_ids( $group_id );
    1806 }
    1807 
    1808 function groups_get_group_mods( $group_id ) {
    1809         return BP_Groups_Member::get_group_moderator_ids( $group_id );
    1810 }
    1811 
    1812 function groups_get_group_members( $group_id, $limit = false, $page = false ) {
    1813         return BP_Groups_Member::get_all_for_group( $group_id, $limit, $page );
    1814 }
    1815 
    1816 function groups_get_total_member_count( $group_id ) {
    1817         return BP_Groups_Group::get_total_member_count( $group_id );
    1818 }
    1819 
    1820 /*** Group Fetching, Filtering & Searching  *************************************/
    1821 
    1822 function groups_get_groups( $args = '' ) {
    1823         global $bp;
    1824 
    1825         $defaults = array(
    1826                 'type'            => 'active', // active, newest, alphabetical, random, popular, most-forum-topics or most-forum-posts
    1827                 'user_id'         => false,    // Pass a user_id to limit to only groups that this user is a member of
    1828                 'include'         => false,    // Only include these specific groups (group_ids)
    1829                 'exclude'         => false,    // Do not include these specific groups (group_ids)
    1830                 'search_terms'    => false,    // Limit to groups that match these search terms
    1831                 'show_hidden'     => false,    // Show hidden groups to non-admins
    1832 
    1833                 'per_page'        => 20,       // The number of results to return per page
    1834                 'page'            => 1,        // The page to return if limiting per page
    1835                 'populate_extras' => true,     // Fetch meta such as is_banned and is_member
    1836         );
    1837 
    1838         $params = wp_parse_args( $args, $defaults );
    1839         extract( $params, EXTR_SKIP );
    1840 
    1841         $groups = BP_Groups_Group::get( $type, $per_page, $page, $user_id, $search_terms, $include, $populate_extras, $exclude, $show_hidden );
    1842 
    1843         return apply_filters( 'groups_get_groups', $groups, &$params );
    1844 }
    1845 
    1846 function groups_get_total_group_count() {
    1847         if ( !$count = wp_cache_get( 'bp_total_group_count', 'bp' ) ) {
    1848                 $count = BP_Groups_Group::get_total_group_count();
    1849                 wp_cache_set( 'bp_total_group_count', $count, 'bp' );
    1850         }
    1851 
    1852         return $count;
    1853 }
    1854 
    1855 function groups_get_user_groups( $user_id = 0, $pag_num = 0, $pag_page = 0 ) {
    1856         global $bp;
    1857 
    1858         if ( !$user_id )
    1859                 $user_id = $bp->displayed_user->id;
    1860 
    1861         return BP_Groups_Member::get_group_ids( $user_id, $pag_num, $pag_page );
    1862 }
    1863 
    1864 function groups_total_groups_for_user( $user_id = 0 ) {
    1865         global $bp;
    1866 
    1867         if ( !$user_id )
    1868                 $user_id = ( $bp->displayed_user->id ) ? $bp->displayed_user->id : $bp->loggedin_user->id;
    1869 
    1870         if ( !$count = wp_cache_get( 'bp_total_groups_for_user_' . $user_id, 'bp' ) ) {
    1871                 $count = BP_Groups_Member::total_group_count( $user_id );
    1872                 wp_cache_set( 'bp_total_groups_for_user_' . $user_id, $count, 'bp' );
    1873         }
    1874 
    1875         return $count;
    1876 }
    1877 
    1878 /*** Group Avatars *************************************************************/
    1879 
    1880 function groups_avatar_upload_dir( $group_id = 0 ) {
    1881         global $bp;
    1882 
    1883         if ( !$group_id )
    1884                 $group_id = $bp->groups->current_group->id;
    1885 
    1886         $path    = BP_AVATAR_UPLOAD_PATH . '/group-avatars/' . $group_id;
    1887         $newbdir = $path;
    1888 
    1889         if ( !file_exists( $path ) )
    1890                 @wp_mkdir_p( $path );
    1891 
    1892         $newurl    = BP_AVATAR_URL . '/group-avatars/' . $group_id;
    1893         $newburl   = $newurl;
    1894         $newsubdir = '/group-avatars/' . $group_id;
    1895 
    1896         return apply_filters( 'groups_avatar_upload_dir', array( 'path' => $path, 'url' => $newurl, 'subdir' => $newsubdir, 'basedir' => $newbdir, 'baseurl' => $newburl, 'error' => false ) );
    1897 }
    1898 
    1899 /*** Group Member Status Checks ************************************************/
    1900 
    1901 function groups_is_user_admin( $user_id, $group_id ) {
    1902         return BP_Groups_Member::check_is_admin( $user_id, $group_id );
    1903 }
    1904 
    1905 function groups_is_user_mod( $user_id, $group_id ) {
    1906         return BP_Groups_Member::check_is_mod( $user_id, $group_id );
    1907 }
    1908 
    1909 function groups_is_user_member( $user_id, $group_id ) {
    1910         return BP_Groups_Member::check_is_member( $user_id, $group_id );
    1911 }
    1912 
    1913 function groups_is_user_banned( $user_id, $group_id ) {
    1914         return BP_Groups_Member::check_is_banned( $user_id, $group_id );
    1915 }
    1916 
    1917 /**
    1918  * Is the specified user the creator of the group?
    1919  *
    1920  * @param int $user_id
    1921  * @param int $group_id
    1922  * @since 1.2.6
    1923  * @uses BP_Groups_Member
    1924  */
    1925 function groups_is_user_creator( $user_id, $group_id ) {
    1926         return BP_Groups_Member::check_is_creator( $user_id, $group_id );
    1927 }
    1928 
    1929 /*** Group Activity Posting **************************************************/
    1930 
    1931 function groups_post_update( $args = '' ) {
    1932         global $bp;
    1933 
    1934         $defaults = array(
    1935                 'content'  => false,
    1936                 'user_id'  => $bp->loggedin_user->id,
    1937                 'group_id' => $bp->groups->current_group->id
    1938         );
    1939 
    1940         $r = wp_parse_args( $args, $defaults );
    1941         extract( $r, EXTR_SKIP );
    1942 
    1943         if ( empty( $content ) || !strlen( trim( $content ) ) || empty( $user_id ) || empty( $group_id ) )
    1944                 return false;
    1945 
    1946         $bp->groups->current_group = new BP_Groups_Group( $group_id );
    1947 
    1948         // Be sure the user is a member of the group before posting.
    1949         if ( !is_super_admin() && !groups_is_user_member( $user_id, $group_id ) )
    1950                 return false;
    1951 
    1952         // Record this in activity streams
    1953         $activity_action  = sprintf( __( '%1$s posted an update in the group %2$s:', 'buddypress'), bp_core_get_userlink( $user_id ), '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );
    1954         $activity_content = $content;
    1955 
    1956         $activity_id = groups_record_activity( array(
    1957                 'user_id' => $user_id,
    1958                 'action'  => apply_filters( 'groups_activity_new_update_action',  $activity_action  ),
    1959                 'content' => apply_filters( 'groups_activity_new_update_content', $activity_content ),
    1960                 'type'    => 'activity_update',
    1961                 'item_id' => $group_id
    1962         ) );
    1963 
    1964         // Require the notifications code so email notifications can be set on
    1965         // the 'bp_activity_posted_update' action.
    1966         require_once( BP_PLUGIN_DIR . '/bp-groups/bp-groups-notifications.php' );
    1967 
    1968         groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
    1969         do_action( 'bp_groups_posted_update', $content, $user_id, $group_id, $activity_id );
    1970 
    1971         return $activity_id;
    1972 }
    1973 
    1974 /*** Group Forums **************************************************************/
    1975 
    1976 function groups_new_group_forum( $group_id = 0, $group_name = '', $group_desc = '' ) {
    1977         global $bp;
    1978 
    1979         if ( !$group_id )
    1980                 $group_id = $bp->groups->current_group->id;
    1981 
    1982         if ( !$group_name )
    1983                 $group_name = $bp->groups->current_group->name;
    1984 
    1985         if ( !$group_desc )
    1986                 $group_desc = $bp->groups->current_group->description;
    1987 
    1988         $forum_id = bp_forums_new_forum( array( 'forum_name' => $group_name, 'forum_desc' => $group_desc ) );
    1989 
    1990         groups_update_groupmeta( $group_id, 'forum_id', $forum_id );
    1991 
    1992         do_action( 'groups_new_group_forum', $forum_id, $group_id );
    1993 }
    1994 
    1995 function groups_update_group_forum( $group_id ) {
    1996 
    1997         $group = new BP_Groups_Group( $group_id );
    1998 
    1999         if ( !$group->enable_forum || !function_exists( 'bp_forums_setup' ) )
    2000                 return false;
    2001 
    2002         $args = array(
    2003                 'forum_id'      => groups_get_groupmeta( $group_id, 'forum_id' ),
    2004                 'forum_name'    => $group->name,
    2005                 'forum_desc'    => $group->desc,
    2006                 'forum_slug'    => $group->slug
    2007         );
    2008 
    2009         bp_forums_update_forum( apply_filters( 'groups_update_group_forum', $args ) );
    2010 }
    2011 add_action( 'groups_details_updated', 'groups_update_group_forum' );
    2012 
    2013 function groups_new_group_forum_post( $post_text, $topic_id, $page = false ) {
    2014         global $bp;
    2015 
    2016         if ( empty( $post_text ) )
    2017                 return false;
    2018 
    2019         $post_text = apply_filters( 'group_forum_post_text_before_save', $post_text );
    2020         $topic_id  = apply_filters( 'group_forum_post_topic_id_before_save', $topic_id );
    2021 
    2022         if ( $post_id = bp_forums_insert_post( array( 'post_text' => $post_text, 'topic_id' => $topic_id ) ) ) {
    2023                 $topic = bp_forums_get_topic_details( $topic_id );
    2024 
    2025                 $activity_action = sprintf( __( '%1$s posted on the forum topic %2$s in the group %3$s:', 'buddypress'), bp_core_get_userlink( $bp->loggedin_user->id ), '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug .'/">' . esc_attr( $topic->topic_title ) . '</a>', '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );
    2026                 $activity_content = bp_create_excerpt( $post_text );
    2027                 $primary_link = bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug . '/';
    2028 
    2029                 if ( $page )
    2030                         $primary_link .= "?topic_page=" . $page;
    2031 
    2032                 // Record this in activity streams
    2033                 groups_record_activity( array(
    2034                         'action'            => apply_filters( 'groups_activity_new_forum_post_action', $activity_action, $post_id, $post_text, &$topic ),
    2035                         'content'           => apply_filters( 'groups_activity_new_forum_post_content', $activity_content, $post_id, $post_text, &$topic ),
    2036                         'primary_link'      => apply_filters( 'groups_activity_new_forum_post_primary_link', "{$primary_link}#post-{$post_id}" ),
    2037                         'type'              => 'new_forum_post',
    2038                         'item_id'           => $bp->groups->current_group->id,
    2039                         'secondary_item_id' => $post_id
    2040                 ) );
    2041 
    2042                 do_action( 'groups_new_forum_topic_post', $bp->groups->current_group->id, $post_id );
    2043 
    2044                 return $post_id;
    2045         }
    2046 
    2047         return false;
    2048 }
    2049 
    2050 function groups_new_group_forum_topic( $topic_title, $topic_text, $topic_tags, $forum_id ) {
    2051         global $bp;
    2052 
    2053         if ( empty( $topic_title ) || empty( $topic_text ) )
    2054                 return false;
    2055 
    2056         $topic_title = apply_filters( 'group_forum_topic_title_before_save', $topic_title );
    2057         $topic_text  = apply_filters( 'group_forum_topic_text_before_save', $topic_text );
    2058         $topic_tags  = apply_filters( 'group_forum_topic_tags_before_save', $topic_tags );
    2059         $forum_id    = apply_filters( 'group_forum_topic_forum_id_before_save', $forum_id );
    2060 
    2061         if ( $topic_id = bp_forums_new_topic( array( 'topic_title' => $topic_title, 'topic_text' => $topic_text, 'topic_tags' => $topic_tags, 'forum_id' => $forum_id ) ) ) {
    2062                 $topic = bp_forums_get_topic_details( $topic_id );
    2063 
    2064                 $activity_action = sprintf( __( '%1$s started the forum topic %2$s in the group %3$s:', 'buddypress'), bp_core_get_userlink( $bp->loggedin_user->id ), '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug .'/">' . esc_attr( $topic->topic_title ) . '</a>', '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );
    2065                 $activity_content = bp_create_excerpt( $topic_text );
    2066 
    2067                 // Record this in activity streams
    2068                 groups_record_activity( array(
    2069                         'action'            => apply_filters( 'groups_activity_new_forum_topic_action', $activity_action, $topic_text, &$topic ),
    2070                         'content'           => apply_filters( 'groups_activity_new_forum_topic_content', $activity_content, $topic_text, &$topic ),
    2071                         'primary_link'      => apply_filters( 'groups_activity_new_forum_topic_primary_link', bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug . '/' ),
    2072                         'type'              => 'new_forum_topic',
    2073                         'item_id'           => $bp->groups->current_group->id,
    2074                         'secondary_item_id' => $topic->topic_id
    2075                 ) );
    2076 
    2077                 do_action( 'groups_new_forum_topic', $bp->groups->current_group->id, &$topic );
    2078 
    2079                 return $topic;
    2080         }
    2081 
    2082         return false;
    2083 }
    2084 
    2085 function groups_update_group_forum_topic( $topic_id, $topic_title, $topic_text ) {
    2086         global $bp;
    2087 
    2088         $topic_title = apply_filters( 'group_forum_topic_title_before_save', $topic_title );
    2089         $topic_text  = apply_filters( 'group_forum_topic_text_before_save',  $topic_text  );
    2090 
    2091         if ( $topic = bp_forums_update_topic( array( 'topic_title' => $topic_title, 'topic_text' => $topic_text, 'topic_id' => $topic_id ) ) ) {
    2092                 // Update the activity stream item
    2093                 if ( function_exists( 'bp_activity_delete_by_item_id' ) )
    2094                         bp_activity_delete_by_item_id( array( 'item_id' => $bp->groups->current_group->id, 'secondary_item_id' => $topic_id, 'component' => $bp->groups->id, 'type' => 'new_forum_topic' ) );
    2095 
    2096                 $activity_action = sprintf( __( '%1$s started the forum topic %2$s in the group %3$s:', 'buddypress'), bp_core_get_userlink( $topic->topic_poster ), '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug .'/">' . esc_attr( $topic->topic_title ) . '</a>', '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );
    2097                 $activity_content = bp_create_excerpt( $topic_text );
    2098 
    2099                 // Record this in activity streams
    2100                 groups_record_activity( array(
    2101                         'action'            => apply_filters( 'groups_activity_new_forum_topic_action', $activity_action, $topic_text, &$topic ),
    2102                         'content'           => apply_filters( 'groups_activity_new_forum_topic_content', $activity_content, $topic_text, &$topic ),
    2103                         'primary_link'      => apply_filters( 'groups_activity_new_forum_topic_primary_link', bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug . '/' ),
    2104                         'type'              => 'new_forum_topic',
    2105                         'item_id'           => (int)$bp->groups->current_group->id,
    2106                         'user_id'           => (int)$topic->topic_poster,
    2107                         'secondary_item_id' => $topic->topic_id,
    2108                         'recorded_time '    => $topic->topic_time
    2109                 ) );
    2110 
    2111                 do_action( 'groups_update_group_forum_topic', &$topic );
    2112 
    2113                 return $topic;
    2114         }
    2115 
    2116         return false;
    2117 }
    2118 
    2119 function groups_update_group_forum_post( $post_id, $post_text, $topic_id, $page = false ) {
    2120         global $bp;
    2121 
    2122         $post_text = apply_filters( 'group_forum_post_text_before_save', $post_text );
    2123         $topic_id  = apply_filters( 'group_forum_post_topic_id_before_save', $topic_id );
    2124         $post      = bp_forums_get_post( $post_id );
    2125 
    2126         if ( $post_id = bp_forums_insert_post( array( 'post_id' => $post_id, 'post_text' => $post_text, 'post_time' => $post->post_time, 'topic_id' => $topic_id, 'poster_id' => $post->poster_id ) ) ) {
    2127                 $topic = bp_forums_get_topic_details( $topic_id );
    2128 
    2129                 $activity_action = sprintf( __( '%1$s posted on the forum topic %2$s in the group %3$s:', 'buddypress'), bp_core_get_userlink( $post->poster_id ), '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug .'">' . esc_attr( $topic->topic_title ) . '</a>', '<a href="' . bp_get_group_permalink( $bp->groups->current_group ) . '">' . esc_attr( $bp->groups->current_group->name ) . '</a>' );
    2130                 $activity_content = bp_create_excerpt( $post_text );
    2131                 $primary_link = bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug . '/';
    2132 
    2133                 if ( $page )
    2134                         $primary_link .= "?topic_page=" . $page;
    2135 
    2136                 // Fetch an existing entry and update if one exists.
    2137                 if ( function_exists( 'bp_activity_get_activity_id' ) )
    2138                         $id = bp_activity_get_activity_id( array( 'user_id' => $post->poster_id, 'component' => $bp->groups->id, 'type' => 'new_forum_post', 'item_id' => $bp->groups->current_group->id, 'secondary_item_id' => $post_id ) );
    2139 
    2140                 // Update the entry in activity streams
    2141                 groups_record_activity( array(
    2142                         'id'                => $id,
    2143                         'action'            => apply_filters( 'groups_activity_new_forum_post_action', $activity_action, $post_text, &$topic, &$forum_post ),
    2144                         'content'           => apply_filters( 'groups_activity_new_forum_post_content', $activity_content, $post_text, &$topic, &$forum_post ),
    2145                         'primary_link'      => apply_filters( 'groups_activity_new_forum_post_primary_link', $primary_link . "#post-" . $post_id ),
    2146                         'type'              => 'new_forum_post',
    2147                         'item_id'           => (int)$bp->groups->current_group->id,
    2148                         'user_id'           => (int)$post->poster_id,
    2149                         'secondary_item_id' => $post_id,
    2150                         'recorded_time'     => $post->post_time
    2151                 ) );
    2152 
    2153                 do_action( 'groups_update_group_forum_post', &$post, &$topic );
    2154 
    2155                 return $post_id;
    2156         }
    2157 
    2158         return false;
    2159 }
    2160 
    2161 function groups_delete_group_forum_topic( $topic_id ) {
    2162         global $bp;
    2163 
    2164         if ( bp_forums_delete_topic( array( 'topic_id' => $topic_id ) ) ) {
    2165                 do_action( 'groups_before_delete_group_forum_topic', $topic_id );
    2166 
    2167                 // Delete the activity stream item
    2168                 if ( function_exists( 'bp_activity_delete' ) )
    2169                         bp_activity_delete( array( 'item_id' => $bp->groups->current_group->id, 'secondary_item_id' => $topic_id, 'component' => $bp->groups->id, 'type' => 'new_forum_topic' ) );
    2170 
    2171                 do_action( 'groups_delete_group_forum_topic', $topic_id );
    2172 
    2173                 return true;
    2174         }
    2175 
    2176         return false;
    2177 }
    2178 
    2179 function groups_delete_group_forum_post( $post_id, $topic_id ) {
    2180         global $bp;
    2181 
    2182         if ( bp_forums_delete_post( array( 'post_id' => $post_id ) ) ) {
    2183                 do_action( 'groups_before_delete_group_forum_post', $post_id, $topic_id );
    2184 
    2185                 // Delete the activity stream item
    2186                 if ( function_exists( 'bp_activity_delete' ) )
    2187                         bp_activity_delete( array( 'item_id' => $bp->groups->current_group->id, 'secondary_item_id' => $post_id, 'component' => $bp->groups->id, 'type' => 'new_forum_post' ) );
    2188 
    2189                 do_action( 'groups_delete_group_forum_post', $post_id, $topic_id );
    2190 
    2191                 return true;
    2192         }
    2193 
    2194         return false;
    2195 }
    2196 
    2197 
    2198 function groups_total_public_forum_topic_count( $type = 'newest' ) {
    2199         return apply_filters( 'groups_total_public_forum_topic_count', BP_Groups_Group::get_global_forum_topic_count( $type ) );
    2200 }
    2201 
    2202 /*** Group Invitations *********************************************************/
    2203 
    2204 function groups_get_invites_for_user( $user_id = 0, $limit = false, $page = false, $exclude = false ) {
    2205         global $bp;
    2206 
    2207         if ( !$user_id )
    2208                 $user_id = $bp->loggedin_user->id;
    2209 
    2210         return BP_Groups_Member::get_invites( $user_id, $limit, $page, $exclude );
    2211 }
    2212 
    2213 function groups_invite_user( $args = '' ) {
    2214         global $bp;
    2215 
    2216         $defaults = array(
    2217                 'user_id'       => false,
    2218                 'group_id'      => false,
    2219                 'inviter_id'    => $bp->loggedin_user->id,
    2220                 'date_modified' => bp_core_current_time(),
    2221                 'is_confirmed'  => 0
    2222         );
    2223 
    2224         $args = wp_parse_args( $args, $defaults );
    2225         extract( $args, EXTR_SKIP );
    2226 
    2227         if ( !$user_id || !$group_id )
    2228                 return false;
    2229 
    2230         if ( !groups_is_user_member( $user_id, $group_id ) && !groups_check_user_has_invite( $user_id, $group_id ) ) {
    2231                 $invite                = new BP_Groups_Member;
    2232                 $invite->group_id      = $group_id;
    2233                 $invite->user_id       = $user_id;
    2234                 $invite->date_modified = $date_modified;
    2235                 $invite->inviter_id    = $inviter_id;
    2236                 $invite->is_confirmed  = $is_confirmed;
    2237 
    2238                 if ( !$invite->save() )
    2239                         return false;
    2240 
    2241                 do_action( 'groups_invite_user', $args );
    2242         }
    2243 
    2244         return true;
    2245 }
    2246 
    2247 function groups_uninvite_user( $user_id, $group_id ) {
    2248         global $bp;
    2249 
    2250         if ( !BP_Groups_Member::delete( $user_id, $group_id ) )
    2251                 return false;
    2252 
    2253         do_action( 'groups_uninvite_user', $group_id, $user_id );
    2254 
    2255         return true;
    2256 }
    2257 
    2258 function groups_accept_invite( $user_id, $group_id ) {
    2259         global $bp;
    2260 
    2261         if ( groups_is_user_member( $user_id, $group_id ) )
    2262                 return false;
    2263 
    2264         $member = new BP_Groups_Member( $user_id, $group_id );
    2265         $member->accept_invite();
    2266 
    2267         if ( !$member->save() )
    2268                 return false;
    2269 
    2270         // Remove request to join
    2271         if ( $member->check_for_membership_request( $user_id, $group_id ) )
    2272                 $member->delete_request( $user_id, $group_id );
    2273 
    2274         // Modify group meta
    2275         groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') + 1 );
    2276         groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
    2277 
    2278         bp_users_delete_notifications_by_item_id( $user_id, $group_id, $bp->groups->id, 'group_invite' );
    2279 
    2280         do_action( 'groups_accept_invite', $user_id, $group_id );
    2281         return true;
    2282 }
    2283 
    2284 function groups_reject_invite( $user_id, $group_id ) {
    2285         if ( !BP_Groups_Member::delete( $user_id, $group_id ) )
    2286                 return false;
    2287 
    2288         do_action( 'groups_reject_invite', $user_id, $group_id );
    2289 
    2290         return true;
    2291 }
    2292 
    2293 function groups_delete_invite( $user_id, $group_id ) {
    2294         global $bp;
    2295 
    2296         $delete = BP_Groups_Member::delete_invite( $user_id, $group_id );
    2297 
    2298         if ( $delete )
    2299                 bp_users_delete_notifications_by_item_id( $user_id, $group_id, $bp->groups->id, 'group_invite' );
    2300 
    2301         return $delete;
    2302 }
    2303 
    2304 function groups_send_invites( $user_id, $group_id ) {
    2305         global $bp;
    2306 
    2307         require_once ( BP_PLUGIN_DIR . '/bp-groups/bp-groups-notifications.php' );
    2308 
    2309         if ( !$user_id )
    2310                 $user_id = $bp->loggedin_user->id;
    2311 
    2312         // Send friend invites.
    2313         $invited_users = groups_get_invites_for_group( $user_id, $group_id );
    2314         $group = new BP_Groups_Group( $group_id );
    2315 
    2316         for ( $i = 0; $i < count( $invited_users ); $i++ ) {
    2317                 $member = new BP_Groups_Member( $invited_users[$i], $group_id );
    2318 
    2319                 // Send the actual invite
    2320                 groups_notification_group_invites( $group, $member, $user_id );
    2321 
    2322                 $member->invite_sent = 1;
    2323                 $member->save();
    2324         }
    2325 
    2326         do_action( 'groups_send_invites', $bp->groups->current_group->id, $invited_users );
    2327 }
    2328 
    2329 function groups_get_invites_for_group( $user_id, $group_id ) {
    2330         return BP_Groups_Group::get_invites( $user_id, $group_id );
    2331 }
    2332 
    2333 function groups_check_user_has_invite( $user_id, $group_id ) {
    2334         return BP_Groups_Member::check_has_invite( $user_id, $group_id );
    2335 }
    2336 
    2337 function groups_delete_all_group_invites( $group_id ) {
    2338         return BP_Groups_Group::delete_all_invites( $group_id );
    2339 }
    2340 
    2341 /*** Group Promotion & Banning *************************************************/
    2342 
    2343 function groups_promote_member( $user_id, $group_id, $status ) {
    2344         global $bp;
    2345 
    2346         if ( !$bp->is_item_admin )
    2347                 return false;
    2348 
    2349         $member = new BP_Groups_Member( $user_id, $group_id );
    2350 
    2351         do_action( 'groups_premote_member', $group_id, $user_id, $status );
    2352 
    2353         return $member->promote( $status );
    2354 }
    2355 
    2356 function groups_demote_member( $user_id, $group_id ) {
    2357         global $bp;
    2358 
    2359         $member = new BP_Groups_Member( $user_id, $group_id );
    2360 
    2361         do_action( 'groups_demote_member', $group_id, $user_id );
    2362 
    2363         return $member->demote();
    2364 }
    2365 
    2366 function groups_ban_member( $user_id, $group_id ) {
    2367         global $bp;
    2368 
    2369         if ( !$bp->is_item_admin )
    2370                 return false;
    2371 
    2372         $member = new BP_Groups_Member( $user_id, $group_id );
    2373 
    2374         do_action( 'groups_ban_member', $group_id, $user_id );
    2375 
    2376         return $member->ban();
    2377 }
    2378 
    2379 function groups_unban_member( $user_id, $group_id ) {
    2380         global $bp;
    2381 
    2382         if ( !$bp->is_item_admin )
    2383                 return false;
    2384 
    2385         $member = new BP_Groups_Member( $user_id, $group_id );
    2386 
    2387         do_action( 'groups_unban_member', $group_id, $user_id );
    2388 
    2389         return $member->unban();
    2390 }
    2391 
    2392 /*** Group Removal *******************************************************/
    2393 
    2394 function groups_remove_member( $user_id, $group_id ) {
    2395         global $bp;
    2396 
    2397         if ( !$bp->is_item_admin )
    2398                 return false;
    2399 
    2400         $member = new BP_Groups_Member( $user_id, $group_id );
    2401 
    2402         do_action( 'groups_remove_member', $group_id, $user_id );
    2403 
    2404         return $member->remove();
    2405 }
    2406 
    2407 /*** Group Membership ****************************************************/
    2408 
    2409 function groups_send_membership_request( $requesting_user_id, $group_id ) {
    2410         global $bp;
    2411 
    2412         // Prevent duplicate requests
    2413         if ( groups_check_for_membership_request( $requesting_user_id, $group_id ) )
    2414                 return false;
    2415 
    2416         // Check if the user is already a member or is banned
    2417         if ( groups_is_user_member( $requesting_user_id, $group_id ) || groups_is_user_banned( $requesting_user_id, $group_id ) )
    2418                 return false;
    2419 
    2420         $requesting_user                = new BP_Groups_Member;
    2421         $requesting_user->group_id      = $group_id;
    2422         $requesting_user->user_id       = $requesting_user_id;
    2423         $requesting_user->inviter_id    = 0;
    2424         $requesting_user->is_admin      = 0;
    2425         $requesting_user->user_title    = '';
    2426         $requesting_user->date_modified = bp_core_current_time();
    2427         $requesting_user->is_confirmed  = 0;
    2428         $requesting_user->comments      = $_POST['group-request-membership-comments'];
    2429 
    2430         if ( $requesting_user->save() ) {
    2431                 $admins = groups_get_group_admins( $group_id );
    2432 
    2433                 require_once ( BP_PLUGIN_DIR . '/bp-groups/bp-groups-notifications.php' );
    2434 
    2435                 // Saved okay, now send the email notification
    2436                 for ( $i = 0; $i < count( $admins ); $i++ )
    2437                         groups_notification_new_membership_request( $requesting_user_id, $admins[$i]->user_id, $group_id, $requesting_user->id );
    2438 
    2439                 do_action( 'groups_membership_requested', $requesting_user_id, $admins, $group_id, $requesting_user->id );
    2440 
    2441                 return true;
    2442         }
    2443 
    2444         return false;
    2445 }
    2446 
    2447 function groups_accept_membership_request( $membership_id, $user_id = 0, $group_id = false ) {
    2448         global $bp;
    2449 
    2450         if ( $user_id && $group_id )
    2451                 $membership = new BP_Groups_Member( $user_id, $group_id );
    2452         else
    2453                 $membership = new BP_Groups_Member( false, false, $membership_id );
    2454 
    2455         $membership->accept_request();
    2456 
    2457         if ( !$membership->save() )
    2458                 return false;
    2459 
    2460         // Check if the user has an outstanding invite, if so delete it.
    2461         if ( groups_check_user_has_invite( $membership->user_id, $membership->group_id ) )
    2462                 groups_delete_invite( $membership->user_id, $membership->group_id );
    2463 
    2464         // Modify group member count
    2465         groups_update_groupmeta( $membership->group_id, 'total_member_count', (int) groups_get_groupmeta( $membership->group_id, 'total_member_count') + 1 );
    2466 
    2467         // Record this in activity streams
    2468         $group = new BP_Groups_Group( $membership->group_id );
    2469 
    2470         groups_record_activity( array(
    2471                 'action'  => apply_filters( 'groups_activity_membership_accepted_action', sprintf( __( '%1$s joined the group %2$s', 'buddypress'), bp_core_get_userlink( $membership->user_id ), '<a href="' . bp_get_group_permalink( $group ) . '">' . esc_attr( $group->name ) . '</a>' ), $membership->user_id, &$group ),
    2472                 'type'    => 'joined_group',
    2473                 'item_id' => $membership->group_id,
    2474                 'user_id' => $membership->user_id
    2475         ) );
    2476 
    2477         // Send a notification to the user.
    2478         require_once ( BP_PLUGIN_DIR . '/bp-groups/bp-groups-notifications.php' );
    2479         groups_notification_membership_request_completed( $membership->user_id, $membership->group_id, true );
    2480 
    2481         do_action( 'groups_membership_accepted', $membership->user_id, $membership->group_id );
    2482 
    2483         return true;
    2484 }
    2485 
    2486 function groups_reject_membership_request( $membership_id, $user_id = 0, $group_id = false ) {
    2487         if ( !$membership = groups_delete_membership_request( $membership_id, $user_id, $group_id ) )
    2488                 return false;
    2489 
    2490         // Send a notification to the user.
    2491         require_once ( BP_PLUGIN_DIR . '/bp-groups/bp-groups-notifications.php' );
    2492         groups_notification_membership_request_completed( $membership->user_id, $membership->group_id, false );
    2493 
    2494         do_action( 'groups_membership_rejected', $membership->user_id, $membership->group_id );
    2495 
    2496         return true;
    2497 }
    2498 
    2499 function groups_delete_membership_request( $membership_id, $user_id = 0, $group_id = false ) {
    2500         if ( $user_id && $group_id )
    2501                 $membership = new BP_Groups_Member( $user_id, $group_id );
    2502         else
    2503                 $membership = new BP_Groups_Member( false, false, $membership_id );
    2504 
    2505         if ( !BP_Groups_Member::delete( $membership->user_id, $membership->group_id ) )
    2506                 return false;
    2507 
    2508         return $membership;
    2509 }
    2510 
    2511 function groups_check_for_membership_request( $user_id, $group_id ) {
    2512         return BP_Groups_Member::check_for_membership_request( $user_id, $group_id );
    2513 }
    2514 
    2515 function groups_accept_all_pending_membership_requests( $group_id ) {
    2516         $user_ids = BP_Groups_Member::get_all_membership_request_user_ids( $group_id );
    2517 
    2518         if ( !$user_ids )
    2519                 return false;
    2520 
    2521         foreach ( (array) $user_ids as $user_id )
    2522                 groups_accept_membership_request( false, $user_id, $group_id );
    2523 
    2524         do_action( 'groups_accept_all_pending_membership_requests', $group_id );
    2525 
    2526         return true;
    2527 }
    2528 
    2529 /*** Group Meta ****************************************************/
    2530 
    2531 function groups_delete_groupmeta( $group_id, $meta_key = false, $meta_value = false ) {
    2532         global $wpdb, $bp;
    2533 
    2534         if ( !is_numeric( $group_id ) )
    2535                 return false;
    2536 
    2537         $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    2538 
    2539         if ( is_array( $meta_value ) || is_object( $meta_value ) )
    2540                 $meta_value = serialize($meta_value);
    2541 
    2542         $meta_value = trim( $meta_value );
    2543 
    2544         if ( !$meta_key )
    2545                 $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d", $group_id ) );
    2546         else if ( $meta_value )
    2547                 $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d AND meta_key = %s AND meta_value = %s", $group_id, $meta_key, $meta_value ) );
    2548         else
    2549                 $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key ) );
    2550 
    2551         // Delete the cached object
    2552         wp_cache_delete( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, 'bp' );
    2553 
    2554         return true;
    2555 }
    2556 
    2557 function groups_get_groupmeta( $group_id, $meta_key = '') {
    2558         global $wpdb, $bp;
    2559 
    2560         $group_id = (int) $group_id;
    2561 
    2562         if ( !$group_id )
    2563                 return false;
    2564 
    2565         if ( !empty($meta_key) ) {
    2566                 $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    2567 
    2568                 if ( !$metas = wp_cache_get( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, 'bp' ) ) {
    2569                         $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key) );
    2570                         wp_cache_set( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, $metas, 'bp' );
    2571                 }
    2572         } else {
    2573                 $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d", $group_id) );
    2574         }
    2575 
    2576         if ( empty( $metas ) ) {
    2577                 if ( empty( $meta_key ) )
    2578                         return array();
    2579                 else
    2580                         return '';
    2581         }
    2582 
    2583         $metas = array_map( 'maybe_unserialize', (array)$metas );
    2584 
    2585         if ( 1 == count( $metas ) )
    2586                 return $metas[0];
    2587         else
    2588                 return $metas;
    2589 }
    2590 
    2591 function groups_update_groupmeta( $group_id, $meta_key, $meta_value ) {
    2592         global $wpdb, $bp;
    2593 
    2594         if ( !is_numeric( $group_id ) )
    2595                 return false;
    2596 
    2597         $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    2598 
    2599         if ( is_string( $meta_value ) )
    2600                 $meta_value = stripslashes( $wpdb->escape( $meta_value ) );
    2601 
    2602         $meta_value = maybe_serialize( $meta_value );
    2603 
    2604         if ( empty( $meta_value ) )
    2605                 return groups_delete_groupmeta( $group_id, $meta_key );
    2606 
    2607         $cur = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key ) );
    2608 
    2609         if ( !$cur )
    2610                 $wpdb->query( $wpdb->prepare( "INSERT INTO " . $bp->groups->table_name_groupmeta . " ( group_id, meta_key, meta_value ) VALUES ( %d, %s, %s )", $group_id, $meta_key, $meta_value ) );
    2611         else if ( $cur->meta_value != $meta_value )
    2612                 $wpdb->query( $wpdb->prepare( "UPDATE " . $bp->groups->table_name_groupmeta . " SET meta_value = %s WHERE group_id = %d AND meta_key = %s", $meta_value, $group_id, $meta_key ) );
    2613         else
    2614                 return false;
    2615 
    2616         // Update the cached object and recache
    2617         wp_cache_set( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, $meta_value, 'bp' );
    2618 
    2619         return true;
    2620 }
    2621 
    2622 /*** Group Cleanup Functions ****************************************************/
    2623 
    2624 function groups_remove_data_for_user( $user_id ) {
    2625         BP_Groups_Member::delete_all_for_user( $user_id );
    2626 
    2627         do_action( 'groups_remove_data_for_user', $user_id );
    2628 }
    2629 add_action( 'wpmu_delete_user',  'groups_remove_data_for_user' );
    2630 add_action( 'delete_user',       'groups_remove_data_for_user' );
    2631 add_action( 'bp_make_spam_user', 'groups_remove_data_for_user' );
    2632 
    2633 
    2634 /********************************************************************************
    2635  * Caching
    2636  *
    2637  * Caching functions handle the clearing of cached objects and pages on specific
    2638  * actions throughout BuddyPress.
    2639  */
    2640 
    2641 function groups_clear_group_object_cache( $group_id ) {
    2642         wp_cache_delete( 'bp_total_group_count', 'bp' );
    2643 }
    2644 add_action( 'groups_group_deleted',              'groups_clear_group_object_cache' );
    2645 add_action( 'groups_settings_updated',           'groups_clear_group_object_cache' );
    2646 add_action( 'groups_details_updated',            'groups_clear_group_object_cache' );
    2647 add_action( 'groups_group_avatar_updated',       'groups_clear_group_object_cache' );
    2648 add_action( 'groups_create_group_step_complete', 'groups_clear_group_object_cache' );
    2649 
    2650 function groups_clear_group_user_object_cache( $group_id, $user_id ) {
    2651         wp_cache_delete( 'bp_total_groups_for_user_' . $user_id );
    2652 }
    2653 add_action( 'groups_join_group',   'groups_clear_group_user_object_cache', 10, 2 );
    2654 add_action( 'groups_leave_group',  'groups_clear_group_user_object_cache', 10, 2 );
    2655 add_action( 'groups_ban_member',   'groups_clear_group_user_object_cache', 10, 2 );
    2656 add_action( 'groups_unban_member', 'groups_clear_group_user_object_cache', 10, 2 );
    2657 
    2658 /* List actions to clear super cached pages on, if super cache is installed */
    2659 add_action( 'groups_join_group',                 'bp_core_clear_cache' );
    2660 add_action( 'groups_leave_group',                'bp_core_clear_cache' );
    2661 add_action( 'groups_accept_invite',              'bp_core_clear_cache' );
    2662 add_action( 'groups_reject_invite',              'bp_core_clear_cache' );
    2663 add_action( 'groups_invite_user',                'bp_core_clear_cache' );
    2664 add_action( 'groups_uninvite_user',              'bp_core_clear_cache' );
    2665 add_action( 'groups_details_updated',            'bp_core_clear_cache' );
    2666 add_action( 'groups_settings_updated',           'bp_core_clear_cache' );
    2667 add_action( 'groups_unban_member',               'bp_core_clear_cache' );
    2668 add_action( 'groups_ban_member',                 'bp_core_clear_cache' );
    2669 add_action( 'groups_demote_member',              'bp_core_clear_cache' );
    2670 add_action( 'groups_premote_member',             'bp_core_clear_cache' );
    2671 add_action( 'groups_membership_rejected',        'bp_core_clear_cache' );
    2672 add_action( 'groups_membership_accepted',        'bp_core_clear_cache' );
    2673 add_action( 'groups_membership_requested',       'bp_core_clear_cache' );
    2674 add_action( 'groups_create_group_step_complete', 'bp_core_clear_cache' );
    2675 add_action( 'groups_created_group',              'bp_core_clear_cache' );
    2676 add_action( 'groups_group_avatar_updated',       'bp_core_clear_cache' );
    2677 
    2678 ?>
  • trunk/bp-groups/bp-groups-template.php

    r3796 r3818  
    8787                        $this->groups    = array( $group );
    8888                } else {
    89                         $this->groups = groups_get_groups( array( 'type' => $type, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'user_id' => $user_id, 'search_terms' => $search_terms, 'include' => $include, 'exclude' => $exclude, 'populate_extras' => $populate_extras, 'show_hidden' => $show_hidden ) );
     89                        $this->groups = groups_get_groups( array(
     90                                'type'            => $type,
     91                                'per_page'        => $this->pag_num,
     92                                'page'            => $this->pag_page,
     93                                'user_id'         => $user_id,
     94                                'search_terms'    => $search_terms,
     95                                'include'         => $include,
     96                                'exclude'         => $exclude,
     97                                'populate_extras' => $populate_extras,
     98                                'show_hidden'     => $show_hidden
     99                        ) );
    90100                }
    91101
    … …  
    597607        global $bp;
    598608
    599         if ( !$bp->is_single_item || !bp_is_current_component( $bp->groups->slug ) || $bp->current_action != $bp->activity->slug )
     609        if ( !bp_is_single_item() || !bp_is_current_component( 'groups' ) || !bp_is_current_action( $bp->activity->slug ) )
    600610                return false;
    601611
    … …  
    17001710
    17011711        /* Make sure we are in the groups component */
    1702         if ( !bp_is_current_component( $bp->groups->slug ) || 'create' != $bp->current_action )
     1712        if ( !bp_is_current_component( 'groups' ) ||  !bp_is_current_action( 'create' ) )
    17031713                return false;
    17041714
    … …  
    19791989        global $bp;
    19801990
    1981         if ( !bp_is_current_component( $bp->groups->slug ) || 'admin' != $bp->current_action )
     1991        if ( !bp_is_current_component( 'groups' ) || !bp_is_current_action( 'admin' ) )
    19821992                return false;
    19831993
Note: See TracChangeset for help on using the changeset viewer.