Skip to:
Content

BuddyPress.org

Ticket #265: bp-groups.patch

File bp-groups.patch, 156.2 KB (added by williamsba1, 18 years ago)

bp-groups.php patch file

  • bp-groups.php

     
    1 <?php
    2 require_once( 'bp-core.php' );
    3 
    4 define ( 'BP_GROUPS_IS_INSTALLED', 1 );
    5 define ( 'BP_GROUPS_VERSION', '1.0b1' );
    6 
    7 include_once( 'bp-groups/bp-groups-classes.php' );
    8 include_once( 'bp-groups/bp-groups-ajax.php' );
    9 include_once( 'bp-groups/bp-groups-cssjs.php' );
    10 include_once( 'bp-groups/bp-groups-templatetags.php' );
    11 include_once( 'bp-groups/bp-groups-widgets.php' );
    12 include_once( 'bp-groups/bp-groups-notifications.php' );
    13 include_once( 'bp-groups/bp-groups-filters.php' );
    14 include_once( 'bp-groups/bp-groups-admin.php' );
    15 include_once( 'bp-groups/directories/bp-groups-directory-groups.php' );
    16 
    17 /**************************************************************************
    18  groups_install()
    19  
    20  Sets up the database tables ready for use on a site installation.
    21  **************************************************************************/
    22 
    23 function groups_install() {
    24         global $wpdb, $bp;
    25        
    26         if ( !empty($wpdb->charset) )
    27                 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
    28        
    29         $sql[] = "CREATE TABLE ". $bp['groups']['table_name'] ." (
    30                         id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    31                         creator_id int(11) NOT NULL,
    32                         name varchar(100) NOT NULL,
    33                         slug varchar(100) NOT NULL,
    34                         description longtext NOT NULL,
    35                         news longtext NOT NULL,
    36                         status varchar(10) NOT NULL DEFAULT 'open',
    37                         is_invitation_only tinyint(1) NOT NULL DEFAULT '0',
    38                         enable_wire tinyint(1) NOT NULL DEFAULT '1',
    39                         enable_forum tinyint(1) NOT NULL DEFAULT '1',
    40                         enable_photos tinyint(1) NOT NULL DEFAULT '1',
    41                         photos_admin_only tinyint(1) NOT NULL DEFAULT '0',
    42                         date_created datetime NOT NULL,
    43                         avatar_thumb varchar(150) NOT NULL,
    44                         avatar_full varchar(150) NOT NULL,
    45                     KEY creator_id (creator_id),
    46                     KEY status (status),
    47                     KEY is_invitation_only (is_invitation_only)
    48                    ) {$charset_collate};";
    49        
    50         $sql[] = "CREATE TABLE ". $bp['groups']['table_name_members'] ." (
    51                         id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    52                         group_id int(11) NOT NULL,
    53                         user_id int(11) NOT NULL,
    54                         inviter_id int(11) NOT NULL,
    55                         is_admin tinyint(1) NOT NULL DEFAULT '0',
    56                         is_mod tinyint(1) NOT NULL DEFAULT '0',
    57                         user_title varchar(100) NOT NULL,
    58                         date_modified datetime NOT NULL,
    59                         comments longtext NOT NULL,
    60                         is_confirmed tinyint(1) NOT NULL DEFAULT '0',
    61                         is_banned tinyint(1) NOT NULL DEFAULT '0',
    62                         invite_sent tinyint(1) NOT NULL DEFAULT '0',
    63                         KEY group_id (group_id),
    64                         KEY is_admin (is_admin),
    65                         KEY is_mod (is_mod),
    66                         KEY user_id (user_id),
    67                         KEY inviter_id (inviter_id),
    68                         KEY is_confirmed (is_confirmed)
    69                    ) {$charset_collate};";
    70 
    71         $sql[] = "CREATE TABLE ". $bp['groups']['table_name_groupmeta'] ." (
    72                         id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    73                         group_id int(11) NOT NULL,
    74                         meta_key varchar(255) DEFAULT NULL,
    75                         meta_value longtext DEFAULT NULL,
    76                         KEY group_id (group_id),
    77                         KEY meta_key (meta_key)
    78                    ) {$charset_collate};";
    79        
    80         if ( function_exists('bp_wire_install') ) {
    81                 $sql[] = "CREATE TABLE ". $bp['groups']['table_name_wire'] ." (
    82                                 id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    83                                 item_id int(11) NOT NULL,
    84                                 user_id int(11) NOT NULL,
    85                                 content longtext NOT NULL,
    86                                 date_posted datetime NOT NULL,
    87                                 KEY item_id (item_id),
    88                                 KEY user_id (user_id)
    89                            ) {$charset_collate};";             
    90         }
    91        
    92         require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
    93         dbDelta($sql);
    94        
    95         // dbDelta won't change character sets, so we need to do this seperately.
    96         // This will only be in here pre v1.0
    97         $wpdb->query( $wpdb->prepare( "ALTER TABLE " . $bp['groups']['table_name'] . " DEFAULT CHARACTER SET %s", $wpdb->charset ) );
    98         $wpdb->query( $wpdb->prepare( "ALTER TABLE " . $bp['groups']['table_name_members'] . " DEFAULT CHARACTER SET %s", $wpdb->charset ) );
    99         $wpdb->query( $wpdb->prepare( "ALTER TABLE " . $bp['groups']['table_name_groupmeta'] . " DEFAULT CHARACTER SET %s", $wpdb->charset ) );
    100        
    101         if ( function_exists('bp_wire_install') )
    102                 $wpdb->query( $wpdb->prepare( "ALTER TABLE " . $bp['groups']['table_name_wire'] . " DEFAULT CHARACTER SET %s", $wpdb->charset ) );             
    103        
    104         add_site_option( 'bp-groups-version', BP_GROUPS_VERSION );
    105 }
    106 
    107 
    108 /**************************************************************************
    109  groups_setup_globals()
    110  
    111  Set up and add all global variables for this component, and add them to
    112  the $bp global variable array.
    113  **************************************************************************/
    114 
    115 function groups_setup_globals( $no_global = false ) {
    116         global $wpdb;
    117        
    118         if ( !$no_global )
    119                 global $bp;
    120        
    121         $bp['groups'] = array(
    122                 'table_name' => $wpdb->base_prefix . 'bp_groups',
    123                 'table_name_members' => $wpdb->base_prefix . 'bp_groups_members',
    124                 'table_name_groupmeta' => $wpdb->base_prefix . 'bp_groups_groupmeta',
    125                 'image_base' => site_url( MUPLUGINDIR . '/bp-groups/images' ),
    126                 'format_activity_function' => 'groups_format_activity',
    127                 'slug'           => 'groups'
    128         );
    129        
    130         if ( function_exists('bp_wire_install') )
    131                 $bp['groups']['table_name_wire'] = $wpdb->base_prefix . 'bp_groups_wire';
    132        
    133         $bp['groups']['forbidden_names'] = array( 'my-groups', 'group-finder', 'create', 'invites', 'delete', 'add', 'admin', 'request-membership' );
    134 
    135         return $bp;
    136 }
    137 add_action( 'wp', 'groups_setup_globals', 1, false );   
    138 add_action( 'admin_menu', 'groups_setup_globals', 1, false );
    139 
    140 
    141 function groups_check_installed() {     
    142         global $wpdb, $bp;
    143        
    144         if ( is_site_admin() ) {
    145                 /* Need to check db tables exist, activate hook no-worky in mu-plugins folder. */
    146                 if ( ( $wpdb->get_var("show tables like '%" . $bp['groups']['table_name'] . "%'") == false ) || ( get_site_option('bp-groups-version') < BP_GROUPS_VERSION )  )
    147                         groups_install();
    148         }
    149 }
    150 add_action( 'admin_menu', 'groups_check_installed' );
    151 
    152 
    153 function groups_add_admin_menu() {
    154         global $wpdb, $bp;
    155        
    156         if ( is_site_admin() ) {
    157                 /* Add the administration tab under the "Site Admin" tab for site administrators */
    158                 add_submenu_page( 'wpmu-admin.php', __("Groups", 'buddypress'), __("Groups", 'buddypress'), 1, "groups_admin_settings", "groups_admin_settings" );
    159         }
    160 }
    161 add_action( 'admin_menu', 'groups_add_admin_menu' );
    162 
    163 /**************************************************************************
    164  groups_setup_nav()
    165  
    166  Set up front end navigation.
    167  **************************************************************************/
    168 
    169 function groups_setup_nav() {
    170         global $bp, $current_blog;
    171         global $group_obj, $is_single_group;
    172        
    173         if ( $group_id = BP_Groups_Group::group_exists($bp['current_action']) ) {
    174                 /* This is a single group page. */
    175                 $is_single_group = true;
    176                 $group_obj = new BP_Groups_Group( $group_id );
    177        
    178                 /* Using "item" not "group" for generic support in other components. */
    179                 if ( is_site_admin() )
    180                         $bp['is_item_admin'] = 1;
    181                 else
    182                         $bp['is_item_admin'] = groups_is_user_admin( $bp['loggedin_userid'], $group_obj->id );
    183                
    184                 /* If the user is not an admin, check if they are a moderator */
    185                 if ( !$bp['is_item_admin'] )
    186                         $bp['is_item_mod'] = groups_is_user_mod( $bp['loggedin_userid'], $group_obj->id );
    187                
    188                 /* Is the logged in user a member of the group? */
    189                 $is_member = ( groups_is_user_member( $bp['loggedin_userid'], $group_obj->id ) ) ? true : false;
    190        
    191                 /* Should this group be visible to the logged in user? */
    192                 $is_visible = ( $group_obj->status == 'public' || $is_member ) ? true : false;
    193         }
    194 
    195         /* Add 'Groups' to the main navigation */
    196         bp_core_add_nav_item( __('Groups', 'buddypress'), $bp['groups']['slug'] );
    197        
    198         if ( $bp['current_userid'] )
    199                 bp_core_add_nav_default( $bp['groups']['slug'], 'groups_screen_my_groups', 'my-groups' );
    200                
    201         $groups_link = $bp['loggedin_domain'] . $bp['groups']['slug'] . '/';
    202        
    203         /* Add the subnav items to the groups nav item */
    204         bp_core_add_subnav_item( $bp['groups']['slug'], 'my-groups', __('My Groups', 'buddypress'), $groups_link, 'groups_screen_my_groups', 'my-groups-list' );
    205         bp_core_add_subnav_item( $bp['groups']['slug'], 'create', __('Create a Group', 'buddypress'), $groups_link, 'groups_screen_create_group', false, bp_is_home() );
    206         bp_core_add_subnav_item( $bp['groups']['slug'], 'invites', __('Invites', 'buddypress'), $groups_link, 'groups_screen_group_invites', false, bp_is_home() );
    207        
    208         if ( $bp['current_component'] == $bp['groups']['slug'] ) {
    209                
    210                 if ( bp_is_home() && !$is_single_group ) {
    211                        
    212                         $bp['bp_options_title'] = __('My Groups', 'buddypress');
    213                        
    214                 } else if ( !bp_is_home() && !$is_single_group ) {
    215 
    216                         $bp['bp_options_avatar'] = bp_core_get_avatar( $bp['current_userid'], 1 );
    217                         $bp['bp_options_title'] = $bp['current_fullname'];
    218                        
    219                 } else if ( $is_single_group ) {
    220                         // We are viewing a single group, so set up the
    221                         // group navigation menu using the $group_obj global.
    222                        
    223                         /* When in a single group, the first action is bumped down one because of the
    224                            group name, so we need to adjust this and set the group name to current_item. */
    225                         $bp['current_item'] = $bp['current_action'];
    226                         $bp['current_action'] = $bp['action_variables'][0];
    227                         array_shift($bp['action_variables']);
    228                                                                        
    229                         $bp['bp_options_title'] = bp_create_excerpt( $group_obj->name, 1 );
    230                         $bp['bp_options_avatar'] = '<img src="' . $group_obj->avatar_thumb . '" alt="Group Avatar Thumbnail" />';
    231                        
    232                         $group_link = $bp['root_domain'] . '/' . $bp['groups']['slug'] . '/' . $group_obj->slug . '/';
    233                        
    234                         // If this is a private or hidden group, does the user have access?
    235                         if ( $group_obj->status == 'private' || $group_obj->status == 'hidden' ) {
    236                                 if ( groups_is_user_member( $bp['loggedin_userid'], $group_obj->id ) && is_user_logged_in() )
    237                                         $has_access = true;
    238                                 else
    239                                         $has_access = false;
    240                         } else {
    241                                 $has_access = true;
    242                         }
    243 
    244                         // Reset the existing subnav items
    245                         bp_core_reset_subnav_items($bp['groups']['slug']);
    246                        
    247                         bp_core_add_nav_default( $bp['groups']['slug'], 'groups_screen_group_home', 'home' );
    248                         bp_core_add_subnav_item( $bp['groups']['slug'], 'home', __('Home', 'buddypress'), $group_link, 'groups_screen_group_home', 'group-home' );
    249                        
    250                         // If the user is a group mod or more, then show the group admin nav item */
    251                         if ( $bp['is_item_mod'] || $bp['is_item_admin'] )
    252                                 bp_core_add_subnav_item( $bp['groups']['slug'], 'admin', __('Admin', 'buddypress'), $group_link , 'groups_screen_group_admin', 'group-admin', ( $bp['is_item_admin'] + (int)$bp['is_item_mod'] ) );
    253                        
    254                         // If this is a private group, and the user is not a member, show a "Request Membership" nav item.
    255                         if ( !$has_access && !groups_check_for_membership_request( $bp['loggedin_userid'], $group_obj->id ) )
    256                                 bp_core_add_subnav_item( $bp['groups']['slug'], 'request-membership', __('Request Membership', 'buddypress'), $group_link , 'groups_screen_group_request_membership', 'request-membership' );
    257                        
    258                         if ( $has_access && $group_obj->enable_forum && function_exists('bp_forums_setup') )
    259                                 bp_core_add_subnav_item( $bp['groups']['slug'], 'forum', __('Forum', 'buddypress'), $group_link , 'groups_screen_group_forum', 'group-forum', $is_visible);
    260 
    261                         if ( $has_access && $group_obj->enable_wire && function_exists('bp_wire_install') )
    262                                 bp_core_add_subnav_item( $bp['groups']['slug'], 'wire', __('Wire', 'buddypress'), $group_link, 'groups_screen_group_wire', 'group-wire', $is_visible );
    263 
    264                         if ( $has_access && $group_obj->enable_photos && function_exists('bp_gallery_install') )
    265                                 bp_core_add_subnav_item( $bp['groups']['slug'], 'photos', __('Photos', 'buddypress'), $group_link, 'groups_screen_group_photos', 'group-photos', $is_visible );
    266 
    267                         if ( $has_access )
    268                                 bp_core_add_subnav_item( $bp['groups']['slug'], 'members', __('Members', 'buddypress'), $group_link, 'groups_screen_group_members', 'group-members', $is_visible );
    269                        
    270                         if ( is_user_logged_in() && groups_is_user_member( $bp['loggedin_userid'], $group_obj->id ) ) {
    271                                 if ( function_exists('friends_install') )
    272                                         bp_core_add_subnav_item( $bp['groups']['slug'], 'send-invites', __('Send Invites', 'buddypress'), $group_link, 'groups_screen_group_invite', 'group-invite', $is_member );
    273                                
    274                                 bp_core_add_subnav_item( $bp['groups']['slug'], 'leave-group', __('Leave Group', 'buddypress'), $group_link, 'groups_screen_group_leave', 'group-leave', $is_member );
    275                         }
    276                 }
    277         }
    278 }
    279 add_action( 'wp', 'groups_setup_nav', 2 );
    280 add_action( 'admin_menu', 'groups_setup_nav', 2 );
    281 
    282 
    283 /***** Screens **********/
    284 
    285 function groups_screen_my_groups() {
    286         global $bp;
    287                
    288         do_action( 'groups_screen_my_groups' );
    289        
    290         bp_catch_uri( 'groups/index' );
    291 }
    292 
    293 function groups_screen_group_invites() {
    294         global $bp;
    295        
    296         $group_id = $bp['action_variables'][1];
    297        
    298         if ( isset($bp['action_variables']) && in_array( 'accept', $bp['action_variables'] ) && is_numeric($group_id) ) {
    299                
    300                 if ( !groups_accept_invite( $bp['loggedin_userid'], $group_id ) ) {
    301                         bp_core_add_message( __('Group invite could not be accepted', 'buddypress'), 'error' );                         
    302                 } else {
    303                         bp_core_add_message( __('Group invite accepted', 'buddypress') );
    304                        
    305                         /* Record this in activity streams */
    306                         groups_record_activity( array( 'item_id' => $group_id, 'component_name' => 'groups', 'component_action' => 'joined_group', 'is_private' => 0 ) );
    307                 }
    308 
    309                 bp_core_redirect( $bp['loggedin_domain'] . $bp['current_component'] . '/' . $bp['current_action'] );
    310                
    311         } else if ( isset($bp['action_variables']) && in_array( 'reject', $bp['action_variables'] ) && is_numeric($group_id) ) {
    312                
    313                 if ( !groups_reject_invite( $group_id, $bp['loggedin_userid'] ) ) {
    314                         bp_core_add_message( __('Group invite could not be rejected', 'buddypress'), 'error' );                                                 
    315                 } else {                       
    316                         bp_core_add_message( __('Group invite rejected', 'buddypress') );                       
    317                 }
    318 
    319                 bp_core_redirect( $bp['loggedin_domain'] . $bp['current_component'] . '/' . $bp['current_action'] );
    320         }
    321        
    322         // Remove notifications
    323         bp_core_delete_notifications_for_user_by_type( $bp['loggedin_userid'], 'groups', 'group_invite' );
    324 
    325         do_action( 'groups_screen_group_invites', $group_id );
    326        
    327         bp_catch_uri( 'groups/list-invites' ); 
    328 }
    329 
    330 function groups_screen_create_group() {
    331         global $bp;
    332         global $create_group_step, $group_obj, $completed_to_step;
    333        
    334         $no_instantiate = false;
    335         $reset_steps = false;
    336        
    337         if ( !$create_group_step = $bp['action_variables'][1] ) {
    338                 $create_group_step = '1';
    339                 $completed_to_step = 0;
    340                
    341                 unset($_SESSION['group_obj_id']);
    342                 unset($_SESSION['completed_to_step']);
    343                
    344                 $no_instantiate = true;
    345                 $reset_steps = true;
    346         }
    347        
    348         if ( isset($_SESSION['completed_to_step']) && !$reset_steps ) {
    349                 $completed_to_step = $_SESSION['completed_to_step'];
    350         }
    351        
    352         if ( isset( $_POST['save'] ) || isset( $_POST['skip'] ) ) {
    353                 $group_obj = new BP_Groups_Group( $_SESSION['group_obj_id'] );
    354 
    355                 // If the user skipped the avatar step, move onto the next step and don't save anything.
    356                 if ( isset( $_POST['skip'] ) && $create_group_step == "3" ) {
    357                         $create_group_step++;
    358                         $completed_to_step++;
    359                         $_SESSION['completed_to_step'] = $completed_to_step;
    360                 } else {
    361                        
    362                         // We're done.
    363                         if ( $create_group_step == 4 )
    364                                 bp_core_redirect( bp_group_permalink( $group_obj, false ) );
    365                        
    366                         if ( !$group_id = groups_manage_group( $create_group_step, $_SESSION['group_obj_id'] ) ) {
    367                                 bp_core_add_message( __('There was an error saving group details. Please try again.', 'buddypress'), 'error' );
    368                                 bp_core_redirect( $bp['loggedin_domain'] . $bp['groups']['slug'] . '/create/step/' . $create_group_step );
    369                         } else {
    370                                 $create_group_step++;
    371                                 $completed_to_step++;
    372                                 $_SESSION['completed_to_step'] = $completed_to_step;
    373                                 $_SESSION['group_obj_id'] = $group_id;
    374                         }
    375                 }
    376         }
    377 
    378         if ( isset($_SESSION['group_obj_id']) && !$group_obj && !$no_instantiate )
    379                 $group_obj = new BP_Groups_Group( $_SESSION['group_obj_id'] );
    380        
    381         bp_catch_uri( 'groups/create' );
    382 }
    383 
    384 function groups_screen_group_home() {
    385         global $is_single_group, $bp;
    386        
    387         if ( $is_single_group ) {
    388                
    389                 if ( isset($_GET['new']) ) {
    390                         // Delete group request notifications for the user
    391                         bp_core_delete_notifications_for_user_by_type( $bp['loggedin_userid'], 'groups', 'membership_request_accepted' );
    392                         bp_core_delete_notifications_for_user_by_type( $bp['loggedin_userid'], 'groups', 'membership_request_rejected' );
    393                         bp_core_delete_notifications_for_user_by_type( $bp['loggedin_userid'], 'groups', 'member_promoted_to_mod' );
    394                         bp_core_delete_notifications_for_user_by_type( $bp['loggedin_userid'], 'groups', 'member_promoted_to_admin' );
    395                 }       
    396 
    397                 do_action( 'groups_screen_group_home' );       
    398                
    399                 bp_catch_uri( 'groups/group-home' );           
    400         }
    401 }
    402 
    403 function groups_screen_group_forum() {
    404         global $is_single_group, $bp, $group_obj;
    405        
    406         if ( $is_single_group ) {
    407                 $topic_id = $bp['action_variables'][1];
    408                 $forum_id = groups_get_groupmeta( $group_obj->id, 'forum_id' );
    409                
    410                 if ( $topic_id ) {
    411                        
    412                         /* Posting a reply */
    413                         if ( isset( $_POST['submit_reply'] ) && function_exists( 'bp_forums_new_post') ) {
    414                                 groups_new_group_forum_post( $_POST['reply_text'], $topic_id );
    415                                 bp_core_redirect( bp_group_permalink( $group_obj, false ) . '/forum/topic/' . $topic_id );
    416                         }
    417                        
    418                         do_action( 'groups_screen_group_forum_topic' );
    419                        
    420                         // If we are viewing a topic, load it.
    421                         bp_catch_uri( 'groups/forum/topic' );
    422                 } else {
    423 
    424                         /* Posting a topic */
    425                         if ( isset( $_POST['submit_topic'] ) && function_exists( 'bp_forums_new_topic') ) {
    426                                 groups_new_group_forum_topic( $_POST['topic_title'], $_POST['topic_text'], $_POST['topic_tags'], $forum_id );
    427                                 bp_core_redirect( bp_group_permalink( $group_obj, false ) . '/forum/' );
    428                         }
    429                        
    430                         do_action( 'groups_screen_group_forum', $topic_id, $forum_id );
    431                        
    432                         // Load the forum home.
    433                         bp_catch_uri( 'groups/forum/index' );                           
    434                 }
    435         }
    436 }
    437 
    438 function groups_screen_group_wire() {
    439         global $bp;
    440         global $is_single_group, $group_obj;
    441        
    442         $wire_action = $bp['action_variables'][0];
    443                
    444         if ( $is_single_group ) {
    445                 if ( $wire_action == 'post' && BP_Groups_Member::check_is_member( $bp['loggedin_userid'], $group_obj->id ) ) {
    446 
    447                         if ( !groups_new_wire_post( $group_obj->id, $_POST['wire-post-textarea'] ) ) {
    448                                 bp_core_add_message( __('Wire message could not be posted.', 'buddypress'), 'error' );
    449                         } else {
    450                                 bp_core_add_message( __('Wire message successfully posted.', 'buddypress') );
    451                         }
    452 
    453                         if ( !strpos( $_SERVER['HTTP_REFERER'], $bp['wire']['slug'] ) ) {
    454                                 bp_core_redirect( bp_group_permalink( $group_obj, false ) );
    455                         } else {
    456                                 bp_core_redirect( bp_group_permalink( $group_obj, false ) . '/' . $bp['wire']['slug'] );
    457                         }
    458        
    459                 } else if ( $wire_action == 'delete' && BP_Groups_Member::check_is_member( $bp['loggedin_userid'], $group_obj->id ) ) {
    460                         $wire_message_id = $bp['action_variables'][1];
    461 
    462                         if ( !groups_delete_wire_post( $wire_message_id, $bp['groups']['table_name_wire'] ) ) {
    463                                 bp_core_add_message( __('There was an error deleting the wire message.', 'buddypress'), 'error' );
    464                         } else {
    465                                 bp_core_add_message( __('Wire message successfully deleted.', 'buddypress') );
    466                         }
    467                        
    468                         if ( !strpos( $_SERVER['HTTP_REFERER'], $bp['wire']['slug'] ) ) {
    469                                 bp_core_redirect( bp_group_permalink( $group_obj, false ) );
    470                         } else {
    471                                 bp_core_redirect( bp_group_permalink( $group_obj, false ) . '/' . $bp['wire']['slug'] );
    472                         }
    473                
    474                 } else if ( ( !$wire_action || $bp['action_variables'][1] == 'latest' ) ) {
    475                         bp_catch_uri( 'groups/wire' );
    476                 } else {
    477                         bp_catch_uri( 'groups/group-home' );
    478                 }
    479         }
    480 }
    481 
    482 function groups_screen_group_members() {
    483         global $bp;
    484         global $is_single_group, $group_obj;
    485        
    486         if ( $is_single_group ) {
    487                 do_action( 'groups_screen_group_members', $group_obj->id );
    488                
    489                 bp_catch_uri( 'groups/list-members' );
    490         }
    491 }
    492 
    493 function groups_screen_group_photos() {
    494         global $bp;
    495         global $is_single_group, $group_obj;
    496        
    497         if ( $is_single_group ) {
    498                 // Not implemented yet.
    499                 bp_catch_uri( 'groups/group-home' );
    500         }
    501 }
    502 
    503 function groups_screen_group_invite() {
    504         global $bp;
    505         global $is_single_group, $group_obj;
    506        
    507         if ( $is_single_group ) {
    508                 if ( isset($bp['action_variables']) && $bp['action_variables'][0] == 'send' ) {
    509                         // Send the invites.
    510                         groups_send_invites($group_obj);
    511                        
    512                         bp_core_add_message( __('Group invites sent.', 'buddypress') );
    513                         bp_core_redirect( bp_group_permalink( $group_obj, false ) );
    514                 } else {
    515                         do_action( 'groups_screen_group_invite', $group_obj->id );
    516                        
    517                         // Show send invite page
    518                         bp_catch_uri( 'groups/send-invite' );   
    519                 }
    520         }
    521 }
    522 
    523 function groups_screen_group_leave() {
    524         global $bp;
    525         global $is_single_group, $group_obj;
    526        
    527         if ( $is_single_group ) {
    528                 if ( isset($bp['action_variables']) && $bp['action_variables'][0] == 'yes' ) {
    529                         // remove the user from the group.
    530                         if ( !groups_leave_group( $group_obj->id ) ) {
    531                                 bp_core_add_message(  __('There was an error leaving the group. Please try again.', 'buddypress'), 'error' );
    532                                 bp_core_redirect( bp_group_permalink( $group_obj, false) );
    533                         } else {
    534                                 bp_core_add_message( __('You left the group successfully.', 'buddypress') );
    535                                 bp_core_redirect( $bp['loggedin_domain'] . $bp['groups']['slug'] );
    536                         }
    537                 } else if ( isset($bp['action_variables']) && $bp['action_variables'][0] == 'no' ) {
    538                         bp_core_redirect( bp_group_permalink( $group_obj, false) );
    539                 } else {
    540                         do_action( 'groups_screen_group_leave', $group_obj->id );
    541                        
    542                         // Show leave group page
    543                         bp_catch_uri( 'groups/leave-group-confirm' );
    544                 }
    545         }
    546 }
    547 
    548 function groups_screen_group_request_membership() {
    549         global $bp, $group_obj;
    550        
    551         if ( !is_user_logged_in() )
    552                 return false;
    553        
    554         if ( $group_obj->status == 'private' ) {
    555                 // If the user has submitted a request, send it.
    556                 if ( isset( $_POST['group-request-send']) ) {
    557                         if ( !groups_send_membership_request( $bp['loggedin_userid'], $group_obj->id ) ) {
    558                                 bp_core_add_message( __( 'There was an error sending your group membership request, please try again.', 'buddypress' ), 'error' );
    559                         } else {
    560                                 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' ) );
    561                         }
    562                         bp_core_redirect( bp_group_permalink( $group_obj, false ) );
    563                 }
    564                
    565                 do_action( 'groups_screen_group_request_membership', $group_obj->id );
    566                
    567                 bp_catch_uri( 'groups/request-membership' );
    568         }
    569 }
    570 
    571 function groups_screen_group_admin() {
    572         global $bp, $group_obj;
    573        
    574         if ( $bp['current_component'] == $bp['groups']['slug'] && !$bp['action_variables'] ) {
    575                
    576                 do_action( 'groups_screen_group_admin', $group_obj->id );
    577                
    578                 bp_catch_uri( 'groups/admin/edit-details' );           
    579         }
    580 
    581 }
    582 
    583 function groups_screen_group_admin_edit_details() {
    584         global $bp, $group_obj;
    585        
    586         if ( $bp['current_component'] == $bp['groups']['slug'] && $bp['action_variables'][0] == 'edit-details' ) {
    587        
    588                 if ( $bp['is_item_admin'] || $bp['is_item_mod']  ) {
    589                
    590                         // If the edit form has been submitted, save the edited details
    591                         if ( isset( $_POST['save'] ) ) {
    592                                 if ( !groups_edit_base_group_details( $_POST['group-id'], $_POST['group-name'], $_POST['group-desc'], $_POST['group-news'], (int)$_POST['group-notify-members'] ) ) {
    593                                         bp_core_add_message( __( 'There was an error updating group details, please try again.', 'buddypress' ), 'error' );
    594                                 } else {
    595                                         bp_core_add_message( __( 'Group details were successfully updated.', 'buddypress' ) );
    596                                 }
    597                                 bp_core_redirect( site_url() . '/' . $bp['current_component'] . '/' . $bp['current_item'] . '/admin/edit-details' );
    598                         }
    599 
    600                         do_action( 'groups_screen_group_admin_edit_details', $group_obj->id );
    601 
    602                         bp_catch_uri( 'groups/admin/edit-details' );
    603                        
    604                 }
    605         }
    606 }
    607 add_action( 'wp', 'groups_screen_group_admin_edit_details', 4 );
    608 
    609 
    610 function groups_screen_group_admin_settings() {
    611         global $bp, $group_obj;
    612        
    613         if ( $bp['current_component'] == $bp['groups']['slug'] && $bp['action_variables'][0] == 'group-settings' ) {
    614                
    615                 if ( !$bp['is_item_admin'] )
    616                         return false;
    617                
    618                 // If the edit form has been submitted, save the edited details
    619                 if ( isset( $_POST['save'] ) ) {
    620                         $enable_wire = ( isset($_POST['group-show-wire'] ) ) ? 1 : 0;
    621                         $enable_forum = ( isset($_POST['group-show-forum'] ) ) ? 1 : 0;
    622                         $enable_photos = ( isset($_POST['group-show-photos'] ) ) ? 1 : 0;
    623                         $photos_admin_only = ( $_POST['group-photos-status'] != 'all' ) ? 1 : 0;
    624                         $status = $_POST['group-status'];
    625                        
    626                         if ( !groups_edit_group_settings( $_POST['group-id'], $enable_wire, $enable_forum, $enable_photos, $photos_admin_only, $status ) ) {
    627                                 bp_core_add_message( __( 'There was an error updating group settings, please try again.', 'buddypress' ), 'error' );
    628                         } else {
    629                                 bp_core_add_message( __( 'Group settings were successfully updated.', 'buddypress' ) );
    630                         }
    631                        
    632                         bp_core_redirect( site_url() . '/' . $bp['current_component'] . '/' . $bp['current_item'] . '/admin/group-settings' );
    633                 }
    634 
    635                 do_action( 'groups_screen_group_admin_settings', $group_obj->id );
    636                
    637                 bp_catch_uri( 'groups/admin/group-settings' );
    638         }
    639 }
    640 add_action( 'wp', 'groups_screen_group_admin_settings', 4 );
    641 
    642 function groups_screen_group_admin_manage_members() {
    643         global $bp, $group_obj;
    644 
    645         if ( $bp['current_component'] == $bp['groups']['slug'] && $bp['action_variables'][0] == 'manage-members' ) {
    646                
    647                 if ( !$bp['is_item_admin'] )
    648                         return false;
    649                
    650                 if ( $bp['action_variables'][1] == 'promote' && is_numeric( $bp['action_variables'][2] ) ) {
    651                         $user_id = $bp['action_variables'][2];
    652                        
    653                         // Promote a user.
    654                         if ( !groups_promote_member( $user_id, $group_obj->id ) ) {
    655                                 bp_core_add_message( __( 'There was an error when promoting that user, please try again', 'buddypress' ), 'error' );
    656                         } else {
    657                                 bp_core_add_message( __( 'User promoted successfully', 'buddypress' ) );
    658                         }
    659                        
    660                         do_action( 'bp_groups_promoted_member', $user_id, $group_obj->id );
    661                        
    662                         bp_core_redirect( site_url() . '/' . $bp['current_component'] . '/' . $bp['current_item'] . '/admin/manage-members' );
    663                 }
    664                
    665                 if ( $bp['action_variables'][1] == 'demote' && is_numeric( $bp['action_variables'][2] ) ) {
    666                         $user_id = $bp['action_variables'][2];
    667                        
    668                         // Demote a user.
    669                         if ( !groups_demote_member( $user_id, $group_obj->id ) ) {
    670                                 bp_core_add_message( __( 'There was an error when demoting that user, please try again', 'buddypress' ), 'error' );
    671                         } else {
    672                                 bp_core_add_message( __( 'User demoted successfully', 'buddypress' ) );
    673                         }
    674 
    675                         do_action( 'bp_groups_demoted_member', $user_id, $group_obj->id );
    676                        
    677                         bp_core_redirect( site_url() . '/' . $bp['current_component'] . '/' . $bp['current_item'] . '/admin/manage-members' );
    678                 }
    679                
    680                 if ( $bp['action_variables'][1] == 'ban' && is_numeric( $bp['action_variables'][2] ) ) {
    681                         $user_id = $bp['action_variables'][2];
    682                        
    683                         // Ban a user.
    684                         if ( !groups_ban_member( $user_id, $group_obj->id ) ) {
    685                                 bp_core_add_message( __( 'There was an error when banning that user, please try again', 'buddypress' ), 'error' );
    686                         } else {
    687                                 bp_core_add_message( __( 'User banned successfully', 'buddypress' ) );
    688                         }
    689 
    690                         do_action( 'bp_groups_banned_member', $user_id, $group_obj->id );
    691                        
    692                         bp_core_redirect( site_url() . '/' . $bp['current_component'] . '/' . $bp['current_item'] . '/admin/manage-members' );
    693                 }
    694                
    695                 if ( $bp['action_variables'][1] == 'unban' && is_numeric( $bp['action_variables'][2] ) ) {
    696                         $user_id = $bp['action_variables'][2];
    697                        
    698                         // Remove a ban for user.
    699                         if ( !groups_unban_member( $user_id, $group_obj->id ) ) {
    700                                 bp_core_add_message( __( 'There was an error when unbanning that user, please try again', 'buddypress' ), 'error' );
    701                         } else {
    702                                 bp_core_add_message( __( 'User ban removed successfully', 'buddypress' ) );
    703                         }
    704 
    705                         do_action( 'bp_groups_unbanned_member', $user_id, $group_obj->id );
    706                        
    707                         bp_core_redirect( site_url() . '/' . $bp['current_component'] . '/' . $bp['current_item'] . '/admin/manage-members' );
    708                 }
    709 
    710                 do_action( 'groups_screen_group_admin_manage_members', $group_obj->id );
    711                
    712                 bp_catch_uri( 'groups/admin/manage-members' );
    713         }
    714 }
    715 add_action( 'wp', 'groups_screen_group_admin_manage_members', 4 );
    716 
    717 
    718 function groups_screen_group_admin_requests() {
    719         global $bp, $group_obj;
    720        
    721         if ( $bp['current_component'] == $bp['groups']['slug'] && $bp['action_variables'][0] == 'membership-requests' ) {
    722                
    723                 if ( !$bp['is_item_admin'] || $group_obj->status == 'public' )
    724                         return false;
    725                
    726                 // Remove any screen notifications
    727                 bp_core_delete_notifications_for_user_by_type( $bp['loggedin_userid'], 'groups', 'new_membership_request' );
    728                
    729                 $request_action = $bp['action_variables'][1];
    730                 $membership_id = $bp['action_variables'][2];
    731 
    732                 if ( isset($request_action) && isset($membership_id) ) {
    733                         if ( $request_action == 'accept' && is_numeric($membership_id) ) {
    734                                 // Accept the membership request
    735                                 if ( !groups_accept_membership_request( $membership_id ) ) {
    736                                         bp_core_add_message( __( 'There was an error accepting the membership request, please try again.', 'buddypress' ), 'error' );
    737                                 } else {
    738                                         bp_core_add_message( __( 'Group membership request accepted', 'buddypress' ) );
    739                                 }
    740                         } else if ( $request_action == 'reject' && is_numeric($membership_id) ) {
    741                                 // Reject the membership request
    742                                 if ( !groups_reject_membership_request( $membership_id ) ) {
    743                                         bp_core_add_message( __( 'There was an error rejecting the membership request, please try again.', 'buddypress' ), 'error' );
    744                                 } else {
    745                                         bp_core_add_message( __( 'Group membership request rejected', 'buddypress' ) );
    746                                 }       
    747                         }
    748                         bp_core_redirect( site_url() . '/' . $bp['current_component'] . '/' . $bp['current_item'] . '/admin/membership-requests' );
    749                 }
    750 
    751                 do_action( 'groups_screen_group_admin_requests', $group_obj->id );
    752                
    753                 bp_catch_uri( 'groups/admin/membership-requests' );
    754         }
    755 }
    756 add_action( 'wp', 'groups_screen_group_admin_requests', 4 );
    757 
    758 function groups_screen_group_admin_delete_group() {
    759         global $bp, $group_obj;
    760        
    761         if ( $bp['current_component'] == $bp['groups']['slug'] && $bp['action_variables'][0] == 'delete-group' ) {
    762                
    763                 if ( !$bp['is_item_admin'] )
    764                         return false;
    765                
    766                 if ( isset( $_POST['delete-group-button'] ) && isset( $_POST['delete-group-understand'] ) ) {
    767                         // Group admin has deleted the group, now do it.
    768                         if ( !groups_delete_group( $_POST['group-id']) ) {
    769                                 bp_core_add_message( __( 'There was an error deleting the group, please try again.', 'buddypress' ), 'error' );
    770                         } else {
    771                                 bp_core_add_message( __( 'The group was deleted successfully', 'buddypress' ) );
    772                                 bp_core_redirect( site_url() . '/' . $bp['groups']['slug'] . '/' );
    773                         }
    774                         bp_core_redirect( $bp['loggedin_domain'] . $bp['current_component'] );
    775                 } else {
    776                         do_action( 'groups_screen_group_admin_delete_group', $group_obj->id );
    777                        
    778                         bp_catch_uri( 'groups/admin/delete-group' );
    779                 }
    780         }
    781 }
    782 add_action( 'wp', 'groups_screen_group_admin_delete_group', 4 );
    783 
    784 function groups_screen_notification_settings() {
    785         global $current_user; ?>
    786         <table class="notification-settings" id="groups-notification-settings">
    787                 <tr>
    788                         <th class="icon"></th>
    789                         <th class="title"><?php _e( 'Groups', 'buddypress' ) ?></th>
    790                         <th class="yes"><?php _e( 'Yes', 'buddypress' ) ?></th>
    791                         <th class="no"><?php _e( 'No', 'buddypress' )?></th>
    792                 </tr>
    793                 <tr>
    794                         <td></td>
    795                         <td><?php _e( 'A member invites you to join a group', 'buddypress' ) ?></td>
    796                         <td class="yes"><input type="radio" name="notifications[notification_groups_invite]" value="yes" <?php if ( !get_usermeta( $current_user->id, 'notification_groups_invite') || get_usermeta( $current_user->id, 'notification_groups_invite') == 'yes' ) { ?>checked="checked" <?php } ?>/></td>
    797                         <td class="no"><input type="radio" name="notifications[notification_groups_invite]" value="no" <?php if ( get_usermeta( $current_user->id, 'notification_groups_invite') == 'no' ) { ?>checked="checked" <?php } ?>/></td>
    798                 </tr>
    799                 <tr>
    800                         <td></td>
    801                         <td><?php _e( 'Group information is updated', 'buddypress' ) ?></td>
    802                         <td class="yes"><input type="radio" name="notifications[notification_groups_group_updated]" value="yes" <?php if ( !get_usermeta( $current_user->id, 'notification_groups_group_updated') || get_usermeta( $current_user->id, 'notification_groups_group_updated') == 'yes' ) { ?>checked="checked" <?php } ?>/></td>
    803                         <td class="no"><input type="radio" name="notifications[notification_groups_group_updated]" value="no" <?php if ( get_usermeta( $current_user->id, 'notification_groups_group_updated') == 'no' ) { ?>checked="checked" <?php } ?>/></td>
    804                 </tr>
    805                 <?php if ( function_exists('bp_wire_install') ) { ?>
    806                 <tr>
    807                         <td></td>
    808                         <td><?php _e( 'A member posts on the wire of a group you belong to', 'buddypress' ) ?></td>
    809                         <td class="yes"><input type="radio" name="notifications[notification_groups_wire_post]" value="yes" <?php if ( !get_usermeta( $current_user->id, 'notification_groups_wire_post') || get_usermeta( $current_user->id, 'notification_groups_wire_post') == 'yes' ) { ?>checked="checked" <?php } ?>/></td>
    810                         <td class="no"><input type="radio" name="notifications[notification_groups_wire_post]" value="no" <?php if ( get_usermeta( $current_user->id, 'notification_groups_wire_post') == 'no' ) { ?>checked="checked" <?php } ?>/></td>
    811                 </tr>
    812                 <?php } ?>
    813                 <tr>
    814                         <td></td>
    815                         <td><?php _e( 'You are promoted to a group administrator or moderator', 'buddypress' ) ?></td>
    816                         <td class="yes"><input type="radio" name="notifications[notification_groups_admin_promotion]" value="yes" <?php if ( !get_usermeta( $current_user->id, 'notification_groups_admin_promotion') || get_usermeta( $current_user->id, 'notification_groups_admin_promotion') == 'yes' ) { ?>checked="checked" <?php } ?>/></td>
    817                         <td class="no"><input type="radio" name="notifications[notification_groups_admin_promotion]" value="no" <?php if ( get_usermeta( $current_user->id, 'notification_groups_admin_promotion') == 'no' ) { ?>checked="checked" <?php } ?>/></td>
    818                 </tr>
    819                 <tr>
    820                         <td></td>
    821                         <td><?php _e( 'A member requests to join a private group for which you are an admin', 'buddypress' ) ?></td>
    822                         <td class="yes"><input type="radio" name="notifications[notification_groups_membership_request]" value="yes" <?php if ( !get_usermeta( $current_user->id, 'notification_groups_membership_request') || get_usermeta( $current_user->id, 'notification_groups_membership_request') == 'yes' ) { ?>checked="checked" <?php } ?>/></td>
    823                         <td class="no"><input type="radio" name="notifications[notification_groups_membership_request]" value="no" <?php if ( get_usermeta( $current_user->id, 'notification_groups_membership_request') == 'no' ) { ?>checked="checked" <?php } ?>/></td>
    824                 </tr>
    825                
    826                 <?php do_action( 'groups_screen_notification_settings' ); ?>
    827         </table>
    828 <?php   
    829 }
    830 add_action( 'bp_notification_settings', 'groups_screen_notification_settings' );
    831 
    832 
    833 /***** Actions **********/
    834 
    835 function groups_action_join_group() {
    836         global $bp;
    837         global $is_single_group, $group_obj;   
    838 
    839         if ( !$is_single_group || $bp['current_component'] != $bp['groups']['slug'] || $bp['current_action'] != 'join' )
    840                 return false;
    841                
    842         // user wants to join a group
    843         if ( !groups_is_user_member( $bp['loggedin_userid'], $group_obj->id ) && !groups_is_user_banned( $bp['loggedin_userid'], $group_obj->id ) ) {
    844                 if ( !groups_join_group($group_obj->id) ) {
    845                         bp_core_add_message( __('There was an error joining the group.', 'buddypress'), 'error' );
    846                 } else {
    847                         bp_core_add_message( __('You joined the group!', 'buddypress') );
    848                 }
    849                 bp_core_redirect( bp_group_permalink( $group_obj, false ) );
    850         }
    851 
    852         bp_catch_uri( 'groups/group-home' );
    853 }
    854 add_action( 'wp', 'groups_action_join_group', 3 );
    855 
    856 
    857 /**************************************************************************
    858  groups_record_activity()
    859  
    860  Records activity for the logged in user within the friends component so that
    861  it will show in the users activity stream (if installed)
    862  **************************************************************************/
    863 
    864 function groups_record_activity( $args = true ) {
    865         global $group_obj;
    866        
    867         if ( function_exists('bp_activity_record') ) {
    868                 extract($args);
    869                
    870                 if ( $group_obj->status == 'public' )
    871                         bp_activity_record( $item_id, $component_name, $component_action, $is_private, $secondary_item_id, $user_id, $secondary_user_id );
    872         }
    873 }
    874 
    875 function groups_delete_activity( $args = true ) {
    876         if ( function_exists('bp_activity_delete') ) {
    877                 extract($args);
    878                 bp_activity_delete( $item_id, $component_name, $component_action, $user_id, $secondary_item_id );
    879         }
    880 }
    881 
    882 
    883 /**************************************************************************
    884  groups_format_activity()
    885  
    886  Selects and formats recorded groups component activity.
    887  Example: Selects the groups details for a joined group, then
    888           formats it to read "Andy Peatling joined the group 'A Cool Group'"
    889  **************************************************************************/
    890 
    891 function groups_format_activity( $item_id, $user_id, $action, $secondary_item_id = false, $for_secondary_user = false  ) {
    892         global $bp;
    893        
    894         switch( $action ) {
    895                 case 'joined_group':
    896                         $group = new BP_Groups_Group( $item_id );
    897                        
    898                         if ( !$group )
    899                                 return false;
    900                                
    901                         $user_link = bp_core_get_userlink($user_id);
    902                         $group_link = bp_group_permalink( $group, false );
    903                        
    904                         return array(
    905                                 'primary_link' => bp_group_permalink( $group, false ),
    906                                 'content' => apply_filters( 'bp_groups_joined_group_activity', sprintf( __('%s joined the group %s', 'buddypress'), $user_link,  '<a href="' . $group_link . '">' . $group->name . '</a>' ) . ' <span class="time-since">%s</span>', $user_link, $group_link, $group->name )
    907                         );                             
    908                 break;
    909                 case 'created_group':
    910                         $group = new BP_Groups_Group( $item_id );
    911                        
    912                         if ( !$group )
    913                                 return false;
    914                        
    915                         $user_link = bp_core_get_userlink($user_id);
    916                         $group_link = bp_group_permalink( $group, false );
    917                        
    918                         return array(
    919                                 'primary_link' => bp_group_permalink( $group, false ),
    920                                 'content' => apply_filters( 'bp_groups_created_group_activity', sprintf( __('%s created the group %s', 'buddypress'), $user_link, '<a href="' . $group_link . '">' . $group->name . '</a>') . ' <span class="time-since">%s</span>', $user_link, $group_link, $group->name )
    921                         );
    922                 break;
    923                 case 'new_wire_post':
    924                         $wire_post = new BP_Wire_Post( $bp['groups']['table_name_wire'], $item_id );
    925                         $group = new BP_Groups_Group( $wire_post->item_id );
    926                        
    927                         if ( !$group || !$wire_post || !$wire_post->content )
    928                                 return false;           
    929 
    930                         $user_link = bp_core_get_userlink($user_id);
    931                         $group_link = bp_group_permalink( $group, false );
    932                         $post_excerpt = bp_create_excerpt($wire_post->content);
    933                                        
    934                         $content = sprintf ( __('%s wrote on the wire of the group %s', 'buddypress'), $user_link, '<a href="' . $group_link . '">' . $group->name . '</a>' ) . ' <span class="time-since">%s</span>';                 
    935                         $content .= '<blockquote>' . $post_excerpt . '</blockquote>';
    936                        
    937                         $content = apply_filters( 'bp_groups_new_wire_post_activity', $content, $user_link, $group_link, $group->name, $post_excerpt );
    938                        
    939                         return array(
    940                                 'primary_link' => bp_group_permalink( $group, false ),
    941                                 'content' => $content
    942                         );
    943                 break;
    944                 case 'new_forum_post':
    945                         if ( function_exists('bp_forums_setup') ) {
    946                                 $group = new BP_Groups_Group( $item_id );
    947                                 $forum_post = bp_forums_get_post( $secondary_item_id );
    948                                 $forum_topic = bp_forums_get_topic_details( $forum_post['topic_id'] );
    949 
    950                                 if ( !$group || !$forum_post || !$forum_topic )
    951                                         return false;           
    952 
    953                                 $user_link = bp_core_get_userlink($user_id);
    954                                 $group_link = bp_group_permalink( $group, false );
    955                                
    956                                 $post_content = apply_filters( 'bp_the_topic_post_content', stripslashes( $forum_post['post_text'] ) );
    957                        
    958                                 $content = sprintf ( __('%s posted on the forum topic %s in the group %s:', 'buddypress'), $user_link, '<a href="' . $group_link . '/forum/topic/' . $forum_topic['topic_id'] . '">' . $forum_topic['topic_title'] . '</a>', '<a href="' . $group_link . '">' . $group->name . '</a>' ) . ' <span class="time-since">%s</span>';                       
    959                                 $content .= '<blockquote>' . $post_content . '</blockquote>';
    960                                
    961                                 $content = apply_filters( 'bp_groups_new_forum_post_activity', $content, $user_link, $group_link, $forum_topic['topic_id'], $forum_topic['topic_title'], $group_link, $group->name, $post_content );
    962 
    963                                 return array(
    964                                         'primary_link' => bp_group_permalink( $group, false ),
    965                                         'content' => $content
    966                                 );
    967                         }
    968                 break;
    969                 case 'new_forum_topic':
    970                         if ( function_exists('bp_forums_setup') ) {
    971                                 $group = new BP_Groups_Group( $item_id );
    972                                 $forum_topic = bp_forums_get_topic_details( $secondary_item_id );
    973                                 $forum_post = bp_forums_get_post( $forum_topic['topic_last_post_id'] );
    974 
    975                                 if ( !$group || !$forum_post || !$forum_topic )
    976                                         return false;
    977                                        
    978                                 $user_link = bp_core_get_userlink($user_id);
    979                                 $group_link = bp_group_permalink( $group, false );
    980                                
    981                                 $post_content = apply_filters( 'bp_the_topic_post_content', stripslashes( $forum_post['post_text'] ) );
    982                                
    983                                 $content = sprintf ( __('%s created the forum topic %s in the group %s:', 'buddypress'), $uesr_link, '<a href="' . $group_link . '/forum/topic/' . $forum_topic['topic_id'] . '">' . $forum_topic['topic_title'] . '</a>', '<a href="' . $group_link . '">' . $group->name . '</a>' ) . ' <span class="time-since">%s</span>';                 
    984                                 $content .= '<blockquote>' . $post_content . '</blockquote>';
    985                                
    986                                 $content = apply_filters( 'bp_groups_new_forum_topic_activity', $content, $user_link, $group_link, $forum_topic['topic_id'], $forum_topic['topic_title'], $group_link, $group->name, $post_content );
    987 
    988                                 return array(
    989                                         'primary_link' => bp_group_permalink( $group, false ),
    990                                         'content' => $content
    991                                 );
    992                         }
    993                 break;         
    994         }
    995        
    996         do_action( 'groups_format_activity', $action, $item_id, $user_id, $action, $secondary_item_id, $for_secondary_user );
    997        
    998         return false;
    999 }
    1000 
    1001 function groups_format_notifications( $action, $item_id, $secondary_item_id, $total_items ) {
    1002         global $bp;
    1003        
    1004         switch ( $action ) {
    1005                 case 'new_membership_request':
    1006                         $group_id = $secondary_item_id;
    1007                         $requesting_user_id = $item_id;
    1008                        
    1009                         $group = new BP_Groups_Group( $group_id, false, false );
    1010                        
    1011                         $group_link = bp_group_permalink( $group, false );
    1012                        
    1013                         if ( (int)$total_items > 1 ) {
    1014                                 return apply_filters( 'bp_groups_multiple_new_membership_requests_notification', '<a href="' . $group_link . '/admin/membership-requests/" title="' . __( 'Group Membership Requests', 'buddypress' ) . '">' . sprintf( __('%d new membership requests for the group "%s"'), (int)$total_items, $group->name ) . '</a>', $group_link, $total_items, $group->name );             
    1015                         } else {
    1016                                 $user_fullname = bp_core_global_user_fullname( $requesting_user_id );
    1017                                 return apply_filters( 'bp_groups_single_new_membership_request_notification', '<a href="' . $group_link . '/admin/membership-requests/" title="' . $user_fullname .' requests group membership">' . sprintf( __('%s requests membership for the group "%s"'), $user_fullname, $group->name ) . '</a>', $group_link, $user_fullname, $group->name );
    1018                         }       
    1019                 break;
    1020                
    1021                 case 'membership_request_accepted':
    1022                         $group_id = $item_id;
    1023                        
    1024                         $group = new BP_Groups_Group( $group_id, false, false );
    1025                         $group_link = bp_group_permalink( $group, false )  . '/?new';
    1026                        
    1027                         if ( (int)$total_items > 1 ) {
    1028                                 return apply_filters( 'bp_groups_multiple_membership_request_accepted_notification', '<a href="' . $bp['loggedin_domain'] . $bp['groups']['slug'] . '" title="' . __( 'Groups', 'buddypress' ) . '">' . sprintf( __('%d accepted group membership requests'), (int)$total_items, $group->name ) . '</a>', $total_items, $group_name );         
    1029                         } else {
    1030                                 return apply_filters( 'bp_groups_single_membership_request_accepted_notification', '<a href="' . $group_link . '">' . sprintf( __('Membership for group "%s" accepted'), $group->name ) . '</a>', $group_link, $group->name );
    1031                         }       
    1032                 break;
    1033                
    1034                 case 'membership_request_rejected':
    1035                         $group_id = $item_id;
    1036                        
    1037                         $group = new BP_Groups_Group( $group_id, false, false );
    1038                         $group_link = bp_group_permalink( $group, false )  . '/?new';
    1039                        
    1040                         if ( (int)$total_items > 1 ) {
    1041                                 return apply_filters( 'bp_groups_multiple_membership_request_rejected_notification', '<a href="' . site_url() . '/' . MEMBERS_SLUG . '/' . $bp['groups']['slug'] . '" title="' . __( 'Groups', 'buddypress' ) . '">' . sprintf( __('%d rejected group membership requests'), (int)$total_items, $group->name ) . '</a>', $total_items, $group->name );         
    1042                         } else {
    1043                                 return apply_filters( 'bp_groups_single_membership_request_rejected_notification', '<a href="' . $group_link . '">' . sprintf( __('Membership for group "%s" rejected'), $group->name ) . '</a>', $group_link, $group->name );
    1044                         }       
    1045                
    1046                 break;
    1047                
    1048                 case 'member_promoted_to_admin':
    1049                         $group_id = $item_id;
    1050                
    1051                         $group = new BP_Groups_Group( $group_id, false, false );
    1052                         $group_link = bp_group_permalink( $group, false )  . '/?new';
    1053                        
    1054                         if ( (int)$total_items > 1 ) {
    1055                                 return apply_filters( 'bp_groups_multiple_member_promoted_to_admin_notification', '<a href="' . $bp['loggedin_domain'] . $bp['groups']['slug'] . '" title="' . __( 'Groups', 'buddypress' ) . '">' . sprintf( __('You were promoted to an admin in %d groups'), (int)$total_items ) . '</a>', $total_items );           
    1056                         } else {
    1057                                 return apply_filters( 'bp_groups_single_member_promoted_to_admin_notification', '<a href="' . $group_link . '">' . sprintf( __('You were promoted to an admin in the group %s'), $group->name ) . '</a>', $group_link, $group->name );
    1058                         }       
    1059                 break;
    1060                
    1061                 case 'member_promoted_to_mod':
    1062                         $group_id = $item_id;
    1063        
    1064                         $group = new BP_Groups_Group( $group_id, false, false );
    1065                         $group_link = bp_group_permalink( $group, false )  . '/?new';
    1066                        
    1067                         if ( (int)$total_items > 1 ) {
    1068                                 return apply_filters( 'bp_groups_multiple_member_promoted_to_mod_notification', '<a href="' . $bp['loggedin_domain'] . $bp['groups']['slug'] . '" title="' . __( 'Groups', 'buddypress' ) . '">' . sprintf( __('You were promoted to a mod in %d groups'), (int)$total_items ) . '</a>', $total_items );               
    1069                         } else {
    1070                                 return apply_filters( 'bp_groups_single_member_promoted_to_mod_notification', '<a href="' . $group_link . '">' . sprintf( __('You were promoted to a mod in the group %s'), $group->name ) . '</a>', $group_link, $group->name );
    1071                         }       
    1072                 break;
    1073                
    1074                 case 'group_invite':
    1075                         $group_id = $item_id;
    1076 
    1077                         $group = new BP_Groups_Group( $group_id, false, false );
    1078                         $user_url = bp_core_get_userurl( $user_id );
    1079                        
    1080                         if ( (int)$total_items > 1 ) {
    1081                                 return apply_filters( 'bp_groups_multiple_group_invite_notification', '<a href="' . $bp['loggedin_domain'] . $bp['groups']['slug'] . '/invites" title="' . __( 'Group Invites', 'buddypress' ) . '">' . sprintf( __('You have %d new group invitations'), (int)$total_items ) . '</a>', $total_items );         
    1082                         } else {
    1083                                 return apply_filters( 'bp_groups_single_group_invite_notification', '<a href="' . $bp['loggedin_domain'] . $bp['groups']['slug'] . '/invites" title="' . __( 'Group Invites', 'buddypress' ) . '">' . sprintf( __('You have an invitation to the group: %s'), $group->name ) . '</a>', $group->name );
    1084                         }       
    1085                 break;
    1086         }
    1087 
    1088         do_action( 'groups_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
    1089        
    1090         return false;
    1091 }
    1092 
    1093 
    1094 /**************************************************************************
    1095  groups_update_last_activity()
    1096  
    1097  Sets groupmeta for the group with the last activity date for the group based
    1098  on specific group activities.
    1099  **************************************************************************/
    1100 
    1101 function groups_update_last_activity( $group_id ) {
    1102         groups_update_groupmeta( $group_id, 'last_activity', time() );
    1103 }
    1104 add_action( 'groups_deleted_wire_post', 'groups_update_last_activity' );
    1105 add_action( 'groups_new_wire_post', 'groups_update_last_activity' );
    1106 add_action( 'groups_joined_group', 'groups_update_last_activity' );
    1107 add_action( 'groups_leave_group', 'groups_update_last_activity' );
    1108 add_action( 'groups_created_group', 'groups_update_last_activity' );
    1109 add_action( 'bp_groups_new_forum_topic_post', 'groups_update_last_activity' );
    1110 add_action( 'bp_groups_new_forum_topic_post', 'groups_update_last_activity' );
    1111 
    1112 
    1113 
    1114 /**************************************************************************
    1115  groups_get_user_groups()
    1116  
    1117  Fetch the groups the current user is a member of.
    1118  **************************************************************************/
    1119 
    1120 function groups_get_user_groups( $pag_num, $pag_page ) {
    1121         global $bp;
    1122        
    1123         $groups = BP_Groups_Member::get_group_ids( $bp['current_userid'], $pag_num, $pag_page );
    1124 
    1125         return array( 'groups' => $groups['ids'], 'total' => $groups['total'] );
    1126 }
    1127 
    1128 function groups_get_recently_joined_for_user( $user_id, $pag_num = false, $pag_page = false ) {
    1129         return BP_Groups_Member::get_recently_joined( $user_id, $pag_num, $pag_page );
    1130 }
    1131 
    1132 function groups_get_most_popular_for_user( $user_id, $pag_num = false, $pag_page = false ) {
    1133         return BP_Groups_Member::get_most_popular( $user_id, $pag_num, $pag_page );     
    1134 }
    1135 
    1136 function groups_get_recently_active_for_user( $user_id, $pag_num = false, $pag_page = false ) {
    1137         return BP_Groups_Member::get_recently_active( $user_id, $pag_num, $pag_page );
    1138 }
    1139 
    1140 function groups_get_alphabetically_for_user( $user_id, $pag_num = false, $pag_page = false ) {
    1141         return BP_Groups_Member::get_alphabetically( $user_id, $pag_num, $pag_page );   
    1142 }
    1143 
    1144 function groups_get_user_is_admin_of( $user_id, $pag_num = false, $pag_page = false ) {
    1145         return BP_Groups_Member::get_is_admin_of( $user_id, $pag_num, $pag_page );     
    1146 }
    1147 
    1148 function groups_get_user_is_mod_of( $user_id, $pag_num = false, $pag_page = false ) {
    1149         return BP_Groups_Member::get_is_mod_of( $user_id, $pag_num, $pag_page );       
    1150 }
    1151 
    1152 function groups_total_groups_for_user( $user_id ) {
    1153         return BP_Groups_Member::total_group_count( $user_id );
    1154 }
    1155 
    1156 /**************************************************************************
    1157  groups_avatar_upload()
    1158  
    1159  Handle uploading of a group avatar
    1160 **************************************************************************/
    1161 
    1162 function groups_avatar_upload( $file ) {
    1163         // validate the group avatar upload if there is one.
    1164         $avatar_error = false;
    1165 
    1166         // Set friendly error feedback.
    1167         $uploadErrors = array(
    1168                 0 => __("There is no error, the file uploaded with success", 'buddypress'),
    1169                 1 => __("The uploaded file exceeds the upload_max_filesize directive in php.ini", 'buddypress'),
    1170                 2 => __("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", 'buddypress'),
    1171                 3 => __("The uploaded file was only partially uploaded", 'buddypress'),
    1172                 4 => __("No file was uploaded", 'buddypress'),
    1173                 6 => __("Missing a temporary folder", 'buddypress')
    1174         );
    1175 
    1176         if ( !bp_core_check_avatar_upload($file) ) {
    1177                 $avatar_error = true;
    1178                 $avatar_error_msg = __('Your group avatar upload failed, please try again. Error was: ' . $uploadErrors[$file['file']['error']] , 'buddypress');
    1179         }
    1180 
    1181         else if ( !bp_core_check_avatar_size($file) ) {
    1182                 $avatar_error = true;
    1183                 $avatar_size = size_format(1024 * CORE_MAX_FILE_SIZE);
    1184                 $avatar_error_msg = __('The file you uploaded is too big. Please upload a file under', 'buddypress') . size_format(CORE_MAX_FILE_SIZE);
    1185         }
    1186        
    1187         else if ( !bp_core_check_avatar_type($file) ) {
    1188                 $avatar_error = true;
    1189                 $avatar_error_msg = __('Please upload only JPG, GIF or PNG photos.', 'buddypress');             
    1190         }
    1191 
    1192         // "Handle" upload into temporary location
    1193         else if ( !$original = bp_core_handle_avatar_upload($file) ) {
    1194                 $avatar_error = true;
    1195                 $avatar_error_msg = __('Upload Failed! Please check the permissions on the group avatar upload directory.', 'buddypress');                                             
    1196         }
    1197        
    1198         if ( !$canvas = bp_core_resize_avatar($original) )
    1199                 $canvas = $original;
    1200        
    1201         if ( $avatar_error ) { ?>
    1202                 <div id="message" class="error">
    1203                         <p><?php echo $avatar_error_msg ?></p>
    1204                 </div>
    1205                 <?php
    1206                 bp_core_render_avatar_upload_form( '', true );
    1207         } else {
    1208                 bp_core_render_avatar_cropper( $original, $canvas, null, null, false, $bp['loggedin_domain'] );
    1209         }
    1210 }
    1211 
    1212 
    1213 /**************************************************************************
    1214  groups_save_avatar()
    1215  
    1216  Save the avatar location urls into the DB for the group.
    1217 **************************************************************************/
    1218 
    1219 function groups_get_avatar_hrefs( $avatars ) {
    1220         global $bp;
    1221        
    1222         $src = $bp['root_domain'] . '/';
    1223 
    1224         $thumb_href = str_replace( ABSPATH, $src, $avatars['v1_out'] );
    1225         $full_href = str_replace( ABSPATH, $src, $avatars['v2_out'] );
    1226        
    1227         return array( 'thumb_href' => $thumb_href, 'full_href' => $full_href );
    1228 }
    1229 
    1230 
    1231 function groups_search_groups( $search_terms, $pag_num_per_page = 5, $pag_page = 1, $sort_by = false, $order = false ) {
    1232         return BP_Groups_Group::search_groups( $search_terms, $pag_num_per_page, $pag_page, $sort_by, $order );
    1233 }
    1234 
    1235 function groups_filter_user_groups( $filter, $pag_num_per_page = 5, $pag_page = 1 ) {
    1236         return BP_Groups_Group::filter_user_groups( $filter, $pag_num_per_page, $pag_page );
    1237 }
    1238 
    1239 /**************************************************************************
    1240  groups_manage_group()
    1241  
    1242  Manage the creation of a group via the step by step wizard.
    1243 **************************************************************************/
    1244 
    1245 function groups_manage_group( $step, $group_id ) {
    1246         global $bp, $create_group_step, $group_obj, $bbpress_live;
    1247 
    1248         if ( is_numeric( $step ) && ( $step == '1' || $step == '2' || $step == '3' || $step == '4' ) ) {
    1249                
    1250                 if ( !$group_obj )
    1251                         $group_obj = new BP_Groups_Group( $group_id );         
    1252                        
    1253                 switch ( $step ) {
    1254                         case '1':
    1255                                 if ( $_POST['group-name'] != '' && $_POST['group-desc'] != '' && $_POST['group-news'] != '' ) {
    1256                                         $group_obj->creator_id = $bp['loggedin_userid'];
    1257                                         $group_obj->name = stripslashes($_POST['group-name']);
    1258                                         $group_obj->description = stripslashes($_POST['group-desc']);
    1259                                         $group_obj->news = stripslashes($_POST['group-news']);
    1260                                        
    1261                                         $slug = groups_check_slug( sanitize_title($_POST['group-name']) );
    1262 
    1263                                         $group_obj->slug = $slug;
    1264                                         $group_obj->status = 'public';
    1265                                         $group_obj->is_invitation_only = 0;
    1266                                         $group_obj->enable_wire = 1;
    1267                                         $group_obj->enable_forum = 1;
    1268                                         $group_obj->enable_photos = 1;
    1269                                         $group_obj->photos_admin_only = 0;
    1270                                         $group_obj->date_created = time();
    1271                                        
    1272                                         if ( !$group_obj->save() )
    1273                                                 return false;
    1274 
    1275                                         // Save the creator as the group administrator
    1276                                         $admin = new BP_Groups_Member( $bp['loggedin_userid'], $group_obj->id );
    1277                                         $admin->is_admin = 1;
    1278                                         $admin->user_title = __('Group Admin', 'buddypress');
    1279                                         $admin->date_modified = time();
    1280                                         $admin->inviter_id = 0;
    1281                                         $admin->is_confirmed = 1;
    1282                                        
    1283                                         if ( !$admin->save() )
    1284                                                 return false;
    1285                                        
    1286                                         do_action( 'bp_groups_step1_save' );
    1287                                        
    1288                                         /* Set groupmeta */
    1289                                         groups_update_groupmeta( $group_obj->id, 'total_member_count', 1 );
    1290                                         groups_update_groupmeta( $group_obj->id, 'last_activity', time() );
    1291                                         groups_update_groupmeta( $group_obj->id, 'theme', 'buddypress' );
    1292                                         groups_update_groupmeta( $group_obj->id, 'stylesheet', 'buddypress' );
    1293                                
    1294                                         return $group_obj->id;
    1295                                 }
    1296                                
    1297                                 return false;
    1298                         break;
    1299                        
    1300                         case '2':
    1301                                 $group_obj->status = 'public';
    1302                                 $group_obj->is_invitation_only = 0;
    1303                                 $group_obj->enable_wire = 1;
    1304                                 $group_obj->enable_forum = 1;
    1305                                 $group_obj->enable_photos = 1;
    1306                                 $group_obj->photos_admin_only = 0;
    1307                                
    1308                                 if ( !isset($_POST['group-show-wire']) )
    1309                                         $group_obj->enable_wire = 0;
    1310                                
    1311                                 if ( !isset($_POST['group-show-forum']) ) {
    1312                                         $group_obj->enable_forum = 0;
    1313                                 } else {
    1314                                         /* Create the forum if enable_forum = 1 */
    1315                                         if ( function_exists( 'bp_forums_setup' ) && groups_get_groupmeta( $group_obj->id, 'forum_id' ) == '' ) {
    1316                                                 groups_new_group_forum();
    1317                                         }
    1318                                 }
    1319                                
    1320                                 if ( !isset($_POST['group-show-photos']) )
    1321                                         $group_obj->enable_photos = 0;                         
    1322                                
    1323                                 if ( $_POST['group-photos-status'] != 'all' )
    1324                                         $group_obj->photos_admin_only = 1;
    1325                                
    1326                                 if ( $_POST['group-status'] == 'private' ) {
    1327                                         $group_obj->status = 'private';
    1328                                 } else if ( $_POST['group-status'] == 'hidden' ) {
    1329                                         $group_obj->status = 'hidden';
    1330                                 }
    1331                                
    1332                                 if ( !$group_obj->save() )
    1333                                         return false;
    1334                                        
    1335                                 do_action( 'bp_groups_step2_save' );
    1336                                        
    1337                                 return $group_obj->id;
    1338                         break;
    1339                        
    1340                         case '3':
    1341                                                                
    1342                                 // Image already cropped and uploaded, lets store a reference in the DB.
    1343                                 if ( !wp_verify_nonce($_POST['nonce'], 'slick_avatars') || !$result = bp_core_avatar_cropstore( $_POST['orig'], $_POST['canvas'], $_POST['v1_x1'], $_POST['v1_y1'], $_POST['v1_w'], $_POST['v1_h'], $_POST['v2_x1'], $_POST['v2_y1'], $_POST['v2_w'], $_POST['v2_h'], false, 'groupavatar', $group_obj->id ) )
    1344                                         return false;
    1345 
    1346                                 // Success on group avatar cropping, now save the results.
    1347                                 $avatar_hrefs = groups_get_avatar_hrefs($result);
    1348                                
    1349                                 $group_obj->avatar_thumb = $avatar_hrefs['thumb_href'];
    1350                                 $group_obj->avatar_full = $avatar_hrefs['full_href'];
    1351                                
    1352                                 if ( !$group_obj->save() )
    1353                                         return false;
    1354                                
    1355                                 do_action( 'bp_groups_step3_save' );
    1356                                
    1357                                 return $group_obj->id;
    1358                         break;
    1359                        
    1360                         case '4':
    1361                                 $send_invites = groups_send_invites($group_obj);
    1362 
    1363                                 /* Record in activity streams */
    1364                                 groups_record_activity( array( 'item_id' => $group_obj->id, 'component_name' => 'groups', 'component_action' => 'created_group', 'is_private' => 0 ) );
    1365                                
    1366                                 do_action( 'bp_groups_created_group', $group_obj->id );
    1367                                
    1368                                 return $group_obj->id;
    1369                         break;
    1370                 }
    1371         }
    1372        
    1373         return false;
    1374 }
    1375 
    1376 function groups_check_slug( $slug ) {
    1377         global $bp;
    1378        
    1379         if ( in_array( $slug, $bp['groups']['forbidden_names'] ) ) {
    1380                 $slug = $slug . '-' . rand();
    1381         }
    1382        
    1383         if ( BP_Groups_Group::check_slug( $slug ) ) {
    1384                 do {
    1385                         $slug = $slug . '-' . rand();
    1386                 }
    1387                 while ( BP_Groups_Group::check_slug( $slug ) );
    1388         }
    1389        
    1390         return $slug;
    1391 }
    1392 
    1393 function groups_get_slug( $group_id ) {
    1394         $group = new BP_Groups_Group( $group_id, false, false );
    1395         return $group->slug;
    1396 }
    1397 
    1398 function groups_is_user_admin( $user_id, $group_id ) {
    1399         return BP_Groups_Member::check_is_admin( $user_id, $group_id );
    1400 }
    1401 
    1402 function groups_is_user_mod( $user_id, $group_id ) {
    1403         return BP_Groups_Member::check_is_mod( $user_id, $group_id );
    1404 }
    1405 
    1406 function groups_is_user_member( $user_id, $group_id ) {
    1407         return BP_Groups_Member::check_is_member( $user_id, $group_id );
    1408 }
    1409 
    1410 function groups_is_user_banned( $user_id, $group_id ) {
    1411         return BP_Groups_Member::check_is_banned( $user_id, $group_id );
    1412 }
    1413 
    1414 function groups_new_group_forum() {
    1415         global $group_obj;
    1416        
    1417         $forum = bp_forums_new_forum( $group_obj->name . ' - Forum ', $group_obj->description );
    1418         groups_update_groupmeta( $group_obj->id, 'forum_id', $forum['forum_id'] );
    1419        
    1420         do_action( 'groups_new_group_forum', $forum, $group_obj->id );
    1421 }
    1422 
    1423 function groups_new_group_forum_post( $post_text, $topic_id ) {
    1424         global $group_obj;
    1425        
    1426         if ( $forum_post = bp_forums_new_post( $post_text, $topic_id ) ) {
    1427                 bp_core_add_message( __( 'Reply posted successfully!', 'buddypress') );
    1428 
    1429                 /* Record in activity streams */
    1430                 groups_record_activity( array( 'item_id' => $group_obj->id, 'component_name' => 'groups', 'component_action' => 'new_forum_post', 'is_private' => 0, 'secondary_item_id' => $forum_post['post_id'] ) );
    1431                
    1432                 do_action( 'bp_groups_new_forum_topic_post', $group_obj->id, $forum_post );
    1433                
    1434                 return $forum_post;
    1435         }
    1436        
    1437         bp_core_add_message( __( 'There was an error posting that reply.', 'buddypress'), 'error' );                                   
    1438         return false;
    1439 }
    1440 
    1441 function groups_new_group_forum_topic( $topic_title, $topic_text, $topic_tags, $forum_id ) {
    1442         global $group_obj;
    1443        
    1444         if ( $topic = bp_forums_new_topic( $topic_title, $topic_text, $topic_tags, $forum_id ) ) {
    1445                 bp_core_add_message( __( 'Topic posted successfully!', 'buddypress') );
    1446 
    1447                 /* Record in activity streams */
    1448                 groups_record_activity( array( 'item_id' => $group_obj->id, 'component_name' => 'groups', 'component_action' => 'new_forum_topic', 'is_private' => 0, 'secondary_item_id' => $topic['topic_id'] ) );
    1449                
    1450                 do_action( 'bp_groups_new_forum_topic', $group_obj->id, $topic );
    1451                
    1452                 return $topic;
    1453         }
    1454        
    1455         bp_core_add_message( __( 'There was an error posting that topic.', 'buddypress'), 'error' );                                   
    1456         return false;
    1457 }
    1458 
    1459 function groups_invite_user( $user_id, $group_id ) {
    1460         global $bp;
    1461        
    1462         if ( groups_is_user_member( $user_id, $group_id ) )
    1463                 return false;
    1464        
    1465         $invite = new BP_Groups_Member;
    1466         $invite->group_id = $group_id;
    1467         $invite->user_id = $user_id;
    1468         $invite->date_modified = time();
    1469         $invite->inviter_id = $bp['loggedin_userid'];
    1470         $invite->is_confirmed = 0;
    1471        
    1472         if ( !$invite->save() )
    1473                 return false;
    1474        
    1475         do_action( 'groups_invite_user', $group_id, $user_id );
    1476                
    1477         return true;
    1478 }
    1479 
    1480 function groups_uninvite_user( $user_id, $group_id ) {
    1481         global $bp;
    1482 
    1483         if ( !BP_Groups_Member::delete( $user_id, $group_id ) )
    1484                 return false;
    1485 
    1486         do_action( 'groups_uninvite_user', $group_id, $user_id );
    1487 
    1488         return true;
    1489 }
    1490 
    1491 function groups_accept_invite( $user_id, $group_id ) {
    1492         global $group_obj;
    1493        
    1494         if ( groups_is_user_member( $user_id, $group_id ) )
    1495                 return false;
    1496        
    1497         $group_obj = new BP_Groups_Group( $group_id );
    1498        
    1499         $member = new BP_Groups_Member( $user_id, $group_id );
    1500         $member->accept_invite();
    1501 
    1502         if ( !$member->save() )
    1503                 return false;
    1504        
    1505         do_action( 'groups_accept_invite', $user_id, $group_id );
    1506         return true;
    1507 }
    1508 
    1509 function groups_reject_invite( $user_id, $group_id ) {
    1510         if ( !BP_Groups_Member::delete( $user_id, $group_id ) )
    1511                 return false;
    1512        
    1513         do_action( 'groups_reject_invite', $user_id, $group_id );
    1514         return true;
    1515 }
    1516 
    1517 
    1518 function groups_get_invites_for_group( $user_id, $group_id ) {
    1519         return BP_Groups_Group::get_invites( $user_id, $group_id );
    1520 }
    1521 
    1522 function groups_check_user_has_invite( $user_id, $group_id ) {
    1523         return BP_Groups_Member::check_has_invite( $user_id, $group_id );
    1524 }
    1525 
    1526 function groups_get_invites_for_user( $user_id = false ) {
    1527         global $bp;
    1528        
    1529         if ( !$user_id )
    1530                 $user_id = $bp['loggedin_userid'];
    1531        
    1532         return BP_Groups_Member::get_invites( $user_id );
    1533 }
    1534 
    1535 function groups_send_invites( $group_obj ) {
    1536         global $bp;
    1537        
    1538         // Send friend invites.
    1539         $invited_users = groups_get_invites_for_group( $bp['loggedin_userid'], $group_obj->id );
    1540 
    1541         for ( $i = 0; $i < count( $invited_users ); $i++ ) {
    1542                 $member = new BP_Groups_Member( $invited_users[$i], $group_obj->id );
    1543                 $member->invite_sent = 1;
    1544                 $member->save();
    1545         }
    1546        
    1547         groups_notification_group_invites( $group_obj->id, $invited_users, $bp['loggedin_userid'] );
    1548        
    1549         do_action( 'groups_send_invites', $group_obj->id, $invited_users );
    1550 }
    1551 
    1552 function groups_check_group_exists( $group_id ) {
    1553         return BP_Groups_Group::group_exists( $group_id );
    1554 }
    1555 
    1556 function groups_leave_group( $group_id, $user_id = false ) {
    1557         global $bp;
    1558        
    1559         if ( !$user_id )
    1560                 $user_id = $bp['loggedin_userid'];
    1561                
    1562         // This is exactly the same as deleting and invite, just is_confirmed = 1 NOT 0.
    1563         if ( !groups_uninvite_user( $user_id, $group_id ) )
    1564                 return false;
    1565 
    1566         do_action( 'groups_leave_group', $group_id, $bp['loggedin_userid'] );
    1567 
    1568         /* Modify group member count */
    1569         groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') - 1 );
    1570        
    1571         return true;
    1572 }
    1573 
    1574 function groups_join_group( $group_id, $user_id = false ) {
    1575         global $bp;
    1576        
    1577         if ( !$user_id )
    1578                 $user_id = $bp['loggedin_userid'];
    1579        
    1580         $new_member = new BP_Groups_Member;
    1581         $new_member->group_id = $group_id;
    1582         $new_member->user_id = $user_id;
    1583         $new_member->inviter_id = 0;
    1584         $new_member->is_admin = 0;
    1585         $new_member->user_title = '';
    1586         $new_member->date_modified = time();
    1587         $new_member->is_confirmed = 1;
    1588        
    1589         if ( !$new_member->save() )
    1590                 return false;
    1591 
    1592         /* Record this in activity streams */
    1593         groups_record_activity( array( 'item_id' => $new_member->group_id, 'component_name' => 'groups', 'component_action' => 'joined_group', 'is_private' => 0 ) );
    1594        
    1595         /* Modify group member count */
    1596         groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') + 1 );
    1597 
    1598         do_action( 'groups_joined_group', $group_id, $bp['loggedin_userid'] );
    1599 
    1600         return true;
    1601 }
    1602 
    1603 function groups_get_group_admins( $group_id ) {
    1604         return BP_Groups_Member::get_group_administrator_ids( $group_id );
    1605 }
    1606 
    1607 function groups_get_group_mods( $group_id ) {
    1608         return BP_Groups_Member::get_group_moderator_ids( $group_id );
    1609 }
    1610 
    1611 function groups_get_group_users( $group_id, $limit = false, $page = false ) {
    1612         return BP_Groups_Member::get_all_for_group( $group_id, $limit, $page );
    1613 }
    1614 
    1615 function groups_is_group_admin( $user_id, $group_id ) {
    1616         return BP_Groups_Member::check_is_admin( $user_id, $group_id );
    1617 }
    1618 
    1619 function groups_is_group_mod( $user_id, $group_id ) {
    1620         return BP_Groups_Member::check_is_mod( $user_id, $group_id );
    1621 }
    1622 
    1623 function groups_new_wire_post( $group_id, $content ) {
    1624         if ( $wire_post_id = bp_wire_new_post( $group_id, $content ) ) {
    1625                 /* Record in activity streams */
    1626                 groups_record_activity( array( 'item_id' => $wire_post_id, 'group_id' => $group_id, 'component_name' => 'groups', 'component_action' => 'new_wire_post', 'is_private' => 0 ) );
    1627 
    1628                 do_action( 'groups_new_wire_post', $group_id, $wire_post_id );
    1629                
    1630                 return true;
    1631         }
    1632        
    1633         return false;
    1634 }
    1635 
    1636 function groups_delete_wire_post( $wire_post_id, $table_name ) {
    1637         global $bp;
    1638        
    1639         if ( bp_wire_delete_post( $wire_post_id, $table_name ) ) {
    1640                
    1641                 // Remove this activity stream item
    1642                 groups_delete_activity( array( 'item_id' => $wire_post_id, 'component_name' => 'groups', 'component_action' => 'new_wire_post', 'user_id' => $bp['current_userid'], 'group_id' => $group_id ) );
    1643                
    1644                 do_action( 'groups_deleted_wire_post', $wire_post_id );
    1645                 return true;
    1646         }
    1647        
    1648         return false;
    1649 }
    1650 
    1651 function groups_edit_base_group_details( $group_id, $group_name, $group_desc, $group_news ) {
    1652         global $bp;
    1653        
    1654         if ( $group_name == '' || $group_desc == '' || $group_news == '' )
    1655                 return false;
    1656                
    1657         $group = new BP_Groups_Group( $group_id, false, false );
    1658         $group->name = $group_name;
    1659         $group->description = $group_desc;
    1660         $group->news = $group_news;
    1661 
    1662         if ( !$group->save() )
    1663                 return false;
    1664 
    1665         if ( $notify_members )
    1666                 groups_notification_group_updated( $group->id );
    1667 
    1668         do_action( 'bp_groups_details_updated', $group->id );
    1669        
    1670         return true;
    1671 }
    1672 
    1673 function groups_edit_group_settings( $group_id, $enable_wire, $enable_forum, $enable_photos, $photos_admin_only, $status ) {
    1674         global $bp;
    1675        
    1676         $group = new BP_Groups_Group( $group_id, false, false );
    1677         $group->enable_wire = $enable_wire;
    1678         $group->enable_forum = $enable_forum;
    1679         $group->enable_photos = $enable_photos;
    1680         $group->photos_admin_only = $photos_admin_only;
    1681         $group->status = $status;
    1682        
    1683         if ( !$group->save() )
    1684                 return false;
    1685        
    1686         /* If forums have been enabled, and a forum does not yet exist, we need to create one. */
    1687         if ( $group->enable_forum ) {
    1688                 if ( function_exists( 'bp_forums_setup' ) && groups_get_groupmeta( $group->id, 'forum_id' ) == '' ) {
    1689                         $forum = bp_forums_new_forum( $group->name . ' - Forum ', $group->description );
    1690                         groups_update_groupmeta( $group->id, 'forum_id', $forum['forum_id'] );
    1691                 }
    1692         }
    1693        
    1694         do_action( 'bp_groups_settings_updated', $group->id );
    1695        
    1696         return true;
    1697 }
    1698 
    1699 function groups_promote_member( $user_id, $group_id ) {
    1700         global $bp;
    1701        
    1702         if ( !$bp['is_item_admin'] )
    1703                 return false;
    1704                
    1705         $member = new BP_Groups_Member( $user_id, $group_id );
    1706 
    1707         add_action( 'groups_premote_member', $user_id, $group_id );
    1708        
    1709         return $member->promote();
    1710 }
    1711 
    1712 function groups_demote_member( $user_id, $group_id ) {
    1713         global $bp;
    1714        
    1715         if ( !$bp['is_item_admin'] )
    1716                 return false;
    1717                
    1718         $member = new BP_Groups_Member( $user_id, $group_id );
    1719        
    1720         add_action( 'groups_demote_member', $user_id, $group_id );
    1721 
    1722         return $member->demote();
    1723 }
    1724 
    1725 function groups_ban_member( $user_id, $group_id ) {
    1726         global $bp;
    1727        
    1728         if ( !$bp['is_item_admin'] )
    1729                 return false;
    1730                
    1731         $member = new BP_Groups_Member( $user_id, $group_id );
    1732 
    1733         add_action( 'groups_ban_member', $user_id, $group_id );
    1734        
    1735         return $member->ban();
    1736 }
    1737 
    1738 function groups_unban_member( $user_id, $group_id ) {
    1739         global $bp;
    1740        
    1741         if ( !$bp['is_item_admin'] )
    1742                 return false;
    1743                
    1744         $member = new BP_Groups_Member( $user_id, $group_id );
    1745        
    1746         add_action( 'bp_groups_unban_member', $user_id, $group_id );
    1747        
    1748         return $member->unban();
    1749 }
    1750 
    1751 function groups_send_membership_request( $requesting_user_id, $group_id ) {
    1752         global $bp;
    1753 
    1754         $requesting_user = new BP_Groups_Member;
    1755         $requesting_user->group_id = $group_id;
    1756         $requesting_user->user_id = $requesting_user_id;
    1757         $requesting_user->inviter_id = 0;
    1758         $requesting_user->is_admin = 0;
    1759         $requesting_user->user_title = '';
    1760         $requesting_user->date_modified = time();
    1761         $requesting_user->is_confirmed = 0;
    1762         $requesting_user->comments = $_POST['group-request-membership-comments'];
    1763        
    1764         if ( $requesting_user->save() ) {
    1765                 $admins = groups_get_group_admins( $group_id );
    1766                
    1767                 for ( $i = 0; $i < count( $admins ); $i++ ) {
    1768                         // Saved okay, now send the email notification
    1769                         groups_notification_new_membership_request( $requesting_user_id, $admins[$i]->user_id, $group_id, $requesting_user->id );
    1770                 }
    1771                
    1772                 do_action( 'bp_groups_group_membership_requested', $requesting_user_id, $admins, $group_id, $requesting_user->id );
    1773        
    1774                 return true;
    1775         }
    1776        
    1777         return false;
    1778 }
    1779 
    1780 function groups_accept_membership_request( $membership_id ) {
    1781         $membership = new BP_Groups_Member( false, false, $membership_id );
    1782         $membership->accept_request();
    1783        
    1784         if ( !$membership->save() )
    1785                 return false;
    1786                
    1787         /* Modify group member count */
    1788         groups_update_groupmeta( $membership->group_id, 'total_member_count', (int) groups_get_groupmeta( $membership->group_id, 'total_member_count') + 1 );
    1789        
    1790         /* Record this in activity streams */
    1791         groups_record_activity( array( 'item_id' => $membership->group_id, 'component_name' => 'groups', 'component_action' => 'joined_group', 'is_private' => 0 ) );
    1792 
    1793         /* Send a notification to the user. */
    1794         groups_notification_membership_request_completed( $membership->user_id, $membership->group_id, true );
    1795        
    1796         do_action( 'bp_groups_membership_accepted', $membership->user_id, $membership->group_id );
    1797        
    1798         return true;
    1799 }
    1800 
    1801 function groups_reject_membership_request( $membership_id ) {
    1802         $membership = new BP_Groups_Member( false, false, $membership_id );
    1803        
    1804         if ( !BP_Groups_Member::delete( $membership->user_id, $membership->group_id ) )
    1805                 return false;
    1806        
    1807         // Send a notification to the user.
    1808         groups_notification_membership_request_completed( $membership->user_id, $membership->group_id, false );
    1809        
    1810         do_action( 'bp_groups_membership_rejected', $membership->user_id, $membership->group_id );
    1811        
    1812         return true;
    1813 }
    1814 
    1815 function groups_redirect_to_random_group() {
    1816         global $bp, $wpdb;
    1817        
    1818         if ( $bp['current_component'] == $bp['groups']['slug'] && isset( $_GET['random'] ) ) {
    1819                 $group = groups_get_random_group();
    1820 
    1821                 bp_core_redirect( $bp['root_domain'] . '/' . $bp['groups']['slug'] . '/' . $group['groups'][0]->slug );
    1822         }
    1823 }
    1824 add_action( 'wp', 'groups_redirect_to_random_group', 6 );
    1825 
    1826 function groups_delete_group( $group_id ) {
    1827         global $bp;
    1828        
    1829         // Check the user is the group admin.
    1830         if ( !$bp['is_item_admin'] )
    1831                 return false;
    1832        
    1833         // Get the group object
    1834         $group = new BP_Groups_Group( $group_id );
    1835        
    1836         if ( !$group->delete() )
    1837                 return false;
    1838        
    1839         // Remove the activity stream item
    1840         groups_delete_activity( array( 'item_id' => $group_id, 'component_name' => 'groups', 'component_action' => 'created_group', 'user_id' => $bp['loggedin_userid'] ) );
    1841        
    1842         add_action( 'bp_groups_delete_group', $group_id );
    1843        
    1844         return true;
    1845 }
    1846 
    1847 function groups_check_for_membership_request( $user_id, $group_id ) {
    1848         return BP_Groups_Member::check_for_membership_request( $user_id, $group_id );
    1849 }
    1850 
    1851 function groups_get_newest( $limit = null, $page = null ) {
    1852         return BP_Groups_Group::get_newest( $limit, $page );
    1853 }
    1854 
    1855 function groups_get_active( $limit = null, $page = null ) {
    1856         return BP_Groups_Group::get_active( $limit, $page );
    1857 }
    1858 
    1859 function groups_get_popular( $limit = null, $page = null ) {
    1860         return BP_Groups_Group::get_popular( $limit, $page );
    1861 }
    1862 
    1863 function groups_get_all( $limit = null, $page = null ) {
    1864         return BP_Groups_Group::get_all( $limit, $page );
    1865 }
    1866 
    1867 function groups_get_random_group() {
    1868         return BP_Groups_Group::get_random();
    1869 }
    1870 
    1871 //
    1872 // Group meta functions
    1873 //
    1874 
    1875 function groups_delete_groupmeta( $group_id, $meta_key = false, $meta_value = false ) {
    1876         global $wpdb, $bp;
    1877        
    1878         if ( !is_numeric( $group_id ) )
    1879                 return false;
    1880                
    1881         $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
    1882 
    1883         if ( is_array($meta_value) || is_object($meta_value) )
    1884                 $meta_value = serialize($meta_value);
    1885                
    1886         $meta_value = trim( $meta_value );
    1887 
    1888         if ( !$meta_key ) {
    1889                 $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['groups']['table_name_groupmeta'] . " WHERE group_id = %d", $group_id ) );           
    1890         } else if ( !$meta_value ) {
    1891                 $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 ) );
    1892         } else {
    1893                 $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['groups']['table_name_groupmeta'] . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key ) );
    1894         }
    1895        
    1896         // TODO need to look into using this.
    1897         // wp_cache_delete($group_id, 'groups');
    1898 
    1899         return true;
    1900 }
    1901 
    1902 function groups_get_groupmeta( $group_id, $meta_key = '') {
    1903         global $wpdb, $bp;
    1904        
    1905         $group_id = (int) $group_id;
    1906 
    1907         if ( !$group_id )
    1908                 return false;
    1909 
    1910         if ( !empty($meta_key) ) {
    1911                 $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
    1912                
    1913                 // TODO need to look into using this.
    1914                 //$user = wp_cache_get($user_id, 'users');
    1915                
    1916                 // Check the cached user object
    1917                 //if ( false !== $user && isset($user->$meta_key) )
    1918                 //      $metas = array($user->$meta_key);
    1919                 //else
    1920                 $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) );
    1921         } else {
    1922                 $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp['groups']['table_name_groupmeta'] . " WHERE group_id = %d", $group_id) );
    1923         }
    1924 
    1925         if ( empty($metas) ) {
    1926                 if ( empty($meta_key) )
    1927                         return array();
    1928                 else
    1929                         return '';
    1930         }
    1931 
    1932         $metas = array_map('maybe_unserialize', $metas);
    1933 
    1934         if ( count($metas) == 1 )
    1935                 return $metas[0];
    1936         else
    1937                 return $metas;
    1938 }
    1939 
    1940 function groups_update_groupmeta( $group_id, $meta_key, $meta_value ) {
    1941         global $wpdb, $bp;
    1942        
    1943         if ( !is_numeric( $group_id ) )
    1944                 return false;
    1945        
    1946         $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    1947         $meta_value = (string)$meta_value;
    1948 
    1949         if ( is_string($meta_value) )
    1950                 $meta_value = stripslashes($wpdb->escape($meta_value));
    1951        
    1952         $meta_value = maybe_serialize($meta_value);
    1953        
    1954         if (empty($meta_value)) {
    1955                 return groups_delete_groupmeta( $group_id, $meta_key );
    1956         }
    1957 
    1958         $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 ) );
    1959 
    1960         if ( !$cur ) {
    1961                 $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 ) );
    1962         } else if ( $cur->meta_value != $meta_value ) {
    1963                 $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 ) );
    1964         } else {
    1965                 return false;
    1966         }
    1967 
    1968         // TODO need to look into using this.
    1969         // wp_cache_delete($user_id, 'users');
    1970 
    1971         return true;
    1972 }
    1973 
    1974 // The following two functions will force the active member theme for
    1975 // groups pages, even though they are technically under the root "home" blog
    1976 // from a WordPress point of view.
    1977 
    1978 function groups_force_buddypress_theme( $template ) {
    1979         global $current_component, $current_action;
    1980         global $is_member_page;
    1981        
    1982         $groups_bp = groups_setup_globals(true);
    1983        
    1984         if ( $current_component != $groups_bp['groups']['slug'] )
    1985                 return $template;
    1986                
    1987         $member_theme = get_site_option('active-member-theme');
    1988        
    1989         if ( $member_theme == '' )
    1990                 $member_theme = 'buddypress-member';
    1991        
    1992         // The theme filter does not recognize any globals, where as the stylesheet filter does.
    1993         // We have to set up the globals to use manually.
    1994         bp_core_set_uri_globals();
    1995 
    1996         if ( $current_component == $groups_bp['groups']['slug'] )
    1997                 $is_single_group = BP_Groups_Group::group_exists( $current_action, $groups_bp['groups']['table_name'] );
    1998        
    1999         if ( $is_single_group ) {
    2000                 add_filter( 'theme_root', 'bp_core_set_member_theme_root' );
    2001                 add_filter( 'theme_root_uri', 'bp_core_set_member_theme_root_uri' );
    2002 
    2003                 return $member_theme;
    2004         } else {
    2005                 return $template;
    2006         }
    2007        
    2008         return $theme;
    2009 }
    2010 add_filter( 'template', 'groups_force_buddypress_theme', 1, 1 );
    2011 
    2012 function groups_force_buddypress_stylesheet( $stylesheet ) {
    2013         global $bp, $is_single_group, $is_member_page, $current_component;
    2014 
    2015         if ( $current_component != $bp['groups']['slug'] )
    2016                 return $stylesheet;
    2017 
    2018         $member_theme = get_site_option('active-member-theme');
    2019        
    2020         if ( $member_theme == '' )
    2021                 $member_theme = 'buddypress-member';
    2022        
    2023         if ( $is_single_group ) {
    2024                 add_filter( 'theme_root', 'bp_core_set_member_theme_root' );
    2025                 add_filter( 'theme_root_uri', 'bp_core_set_member_theme_root_uri' );
    2026                
    2027                 return $member_theme;
    2028         } else {
    2029                 return $stylesheet;     
    2030         }
    2031 }
    2032 add_filter( 'stylesheet', 'groups_force_buddypress_stylesheet', 1, 1 );
    2033 
    2034 function groups_remove_data( $user_id ) {
    2035         BP_Groups_Member::delete_all_for_user($user_id);
    2036        
    2037         do_action( 'groups_remove_data', $user_id );
    2038 }
    2039 add_action( 'wpmu_delete_user', 'groups_remove_data', 1 );
    2040 add_action( 'delete_user', 'groups_remove_data', 1 );
    2041 
    2042 
     1<?php
     2require_once( 'bp-core.php' );
     3
     4define ( 'BP_GROUPS_IS_INSTALLED', 1 );
     5define ( 'BP_GROUPS_VERSION', '1.0b1' );
     6
     7include_once( 'bp-groups/bp-groups-classes.php' );
     8include_once( 'bp-groups/bp-groups-ajax.php' );
     9include_once( 'bp-groups/bp-groups-cssjs.php' );
     10include_once( 'bp-groups/bp-groups-templatetags.php' );
     11include_once( 'bp-groups/bp-groups-widgets.php' );
     12include_once( 'bp-groups/bp-groups-notifications.php' );
     13include_once( 'bp-groups/bp-groups-filters.php' );
     14include_once( 'bp-groups/bp-groups-admin.php' );
     15include_once( 'bp-groups/directories/bp-groups-directory-groups.php' );
     16
     17/**************************************************************************
     18 groups_install()
     19 
     20 Sets up the database tables ready for use on a site installation.
     21 **************************************************************************/
     22
     23function groups_install() {
     24        global $wpdb, $bp;
     25       
     26        if ( !empty($wpdb->charset) )
     27                $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
     28       
     29        $sql[] = "CREATE TABLE ". $bp['groups']['table_name'] ." (
     30                        id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
     31                        creator_id int(11) NOT NULL,
     32                        name varchar(100) NOT NULL,
     33                        slug varchar(100) NOT NULL,
     34                        description longtext NOT NULL,
     35                        news longtext NOT NULL,
     36                        status varchar(10) NOT NULL DEFAULT 'open',
     37                        is_invitation_only tinyint(1) NOT NULL DEFAULT '0',
     38                        enable_wire tinyint(1) NOT NULL DEFAULT '1',
     39                        enable_forum tinyint(1) NOT NULL DEFAULT '1',
     40                        enable_photos tinyint(1) NOT NULL DEFAULT '1',
     41                        photos_admin_only tinyint(1) NOT NULL DEFAULT '0',
     42                        date_created datetime NOT NULL,
     43                        avatar_thumb varchar(150) NOT NULL,
     44                        avatar_full varchar(150) NOT NULL,
     45                    KEY creator_id (creator_id),
     46                    KEY status (status),
     47                    KEY is_invitation_only (is_invitation_only)
     48                   ) {$charset_collate};";
     49       
     50        $sql[] = "CREATE TABLE ". $bp['groups']['table_name_members'] ." (
     51                        id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
     52                        group_id int(11) NOT NULL,
     53                        user_id int(11) NOT NULL,
     54                        inviter_id int(11) NOT NULL,
     55                        is_admin tinyint(1) NOT NULL DEFAULT '0',
     56                        is_mod tinyint(1) NOT NULL DEFAULT '0',
     57                        user_title varchar(100) NOT NULL,
     58                        date_modified datetime NOT NULL,
     59                        comments longtext NOT NULL,
     60                        is_confirmed tinyint(1) NOT NULL DEFAULT '0',
     61                        is_banned tinyint(1) NOT NULL DEFAULT '0',
     62                        invite_sent tinyint(1) NOT NULL DEFAULT '0',
     63                        KEY group_id (group_id),
     64                        KEY is_admin (is_admin),
     65                        KEY is_mod (is_mod),
     66                        KEY user_id (user_id),
     67                        KEY inviter_id (inviter_id),
     68                        KEY is_confirmed (is_confirmed)
     69                   ) {$charset_collate};";
     70
     71        $sql[] = "CREATE TABLE ". $bp['groups']['table_name_groupmeta'] ." (
     72                        id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
     73                        group_id int(11) NOT NULL,
     74                        meta_key varchar(255) DEFAULT NULL,
     75                        meta_value longtext DEFAULT NULL,
     76                        KEY group_id (group_id),
     77                        KEY meta_key (meta_key)
     78                   ) {$charset_collate};";
     79       
     80        if ( function_exists('bp_wire_install') ) {
     81                $sql[] = "CREATE TABLE ". $bp['groups']['table_name_wire'] ." (
     82                                id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
     83                                item_id int(11) NOT NULL,
     84                                user_id int(11) NOT NULL,
     85                                content longtext NOT NULL,
     86                                date_posted datetime NOT NULL,
     87                                KEY item_id (item_id),
     88                                KEY user_id (user_id)
     89                           ) {$charset_collate};";             
     90        }
     91       
     92        require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
     93        dbDelta($sql);
     94       
     95        // dbDelta won't change character sets, so we need to do this seperately.
     96        // This will only be in here pre v1.0
     97        $wpdb->query( $wpdb->prepare( "ALTER TABLE " . $bp['groups']['table_name'] . " DEFAULT CHARACTER SET %s", $wpdb->charset ) );
     98        $wpdb->query( $wpdb->prepare( "ALTER TABLE " . $bp['groups']['table_name_members'] . " DEFAULT CHARACTER SET %s", $wpdb->charset ) );
     99        $wpdb->query( $wpdb->prepare( "ALTER TABLE " . $bp['groups']['table_name_groupmeta'] . " DEFAULT CHARACTER SET %s", $wpdb->charset ) );
     100       
     101        if ( function_exists('bp_wire_install') )
     102                $wpdb->query( $wpdb->prepare( "ALTER TABLE " . $bp['groups']['table_name_wire'] . " DEFAULT CHARACTER SET %s", $wpdb->charset ) );             
     103       
     104        add_site_option( 'bp-groups-version', BP_GROUPS_VERSION );
     105}
     106
     107
     108/**************************************************************************
     109 groups_setup_globals()
     110 
     111 Set up and add all global variables for this component, and add them to
     112 the $bp global variable array.
     113 **************************************************************************/
     114
     115function groups_setup_globals( $no_global = false ) {
     116        global $wpdb;
     117       
     118        if ( !$no_global )
     119                global $bp;
     120       
     121        $bp['groups'] = array(
     122                'table_name' => $wpdb->base_prefix . 'bp_groups',
     123                'table_name_members' => $wpdb->base_prefix . 'bp_groups_members',
     124                'table_name_groupmeta' => $wpdb->base_prefix . 'bp_groups_groupmeta',
     125                'image_base' => site_url( MUPLUGINDIR . '/bp-groups/images' ),
     126                'format_activity_function' => 'groups_format_activity',
     127                'slug'           => 'groups'
     128        );
     129       
     130        if ( function_exists('bp_wire_install') )
     131                $bp['groups']['table_name_wire'] = $wpdb->base_prefix . 'bp_groups_wire';
     132       
     133        $bp['groups']['forbidden_names'] = array( 'my-groups', 'group-finder', 'create', 'invites', 'delete', 'add', 'admin', 'request-membership' );
     134
     135        return $bp;
     136}
     137add_action( 'wp', 'groups_setup_globals', 1, false );   
     138add_action( 'admin_menu', 'groups_setup_globals', 1, false );
     139
     140
     141function groups_check_installed() {     
     142        global $wpdb, $bp;
     143       
     144        if ( is_site_admin() ) {
     145                /* Need to check db tables exist, activate hook no-worky in mu-plugins folder. */
     146                if ( ( $wpdb->get_var("show tables like '%" . $bp['groups']['table_name'] . "%'") == false ) || ( get_site_option('bp-groups-version') < BP_GROUPS_VERSION )  )
     147                        groups_install();
     148        }
     149}
     150add_action( 'admin_menu', 'groups_check_installed' );
     151
     152
     153function groups_add_admin_menu() {
     154        global $wpdb, $bp;
     155       
     156        if ( is_site_admin() ) {
     157                /* Add the administration tab under the "Site Admin" tab for site administrators */
     158                add_submenu_page( 'wpmu-admin.php', __("Groups", 'buddypress'), __("Groups", 'buddypress'), 1, "groups_admin_settings", "groups_admin_settings" );
     159        }
     160}
     161add_action( 'admin_menu', 'groups_add_admin_menu' );
     162
     163/**************************************************************************
     164 groups_setup_nav()
     165 
     166 Set up front end navigation.
     167 **************************************************************************/
     168
     169function groups_setup_nav() {
     170        global $bp, $current_blog;
     171        global $group_obj, $is_single_group;
     172       
     173        if ( $group_id = BP_Groups_Group::group_exists($bp['current_action']) ) {
     174                /* This is a single group page. */
     175                $is_single_group = true;
     176                $group_obj = new BP_Groups_Group( $group_id );
     177       
     178                /* Using "item" not "group" for generic support in other components. */
     179                if ( is_site_admin() )
     180                        $bp['is_item_admin'] = 1;
     181                else
     182                        $bp['is_item_admin'] = groups_is_user_admin( $bp['loggedin_userid'], $group_obj->id );
     183               
     184                /* If the user is not an admin, check if they are a moderator */
     185                if ( !$bp['is_item_admin'] )
     186                        $bp['is_item_mod'] = groups_is_user_mod( $bp['loggedin_userid'], $group_obj->id );
     187               
     188                /* Is the logged in user a member of the group? */
     189                $is_member = ( groups_is_user_member( $bp['loggedin_userid'], $group_obj->id ) ) ? true : false;
     190       
     191                /* Should this group be visible to the logged in user? */
     192                $is_visible = ( $group_obj->status == 'public' || $is_member ) ? true : false;
     193        }
     194
     195        /* Add 'Groups' to the main navigation */
     196        bp_core_add_nav_item( __('Groups', 'buddypress'), $bp['groups']['slug'] );
     197       
     198        if ( $bp['current_userid'] )
     199                bp_core_add_nav_default( $bp['groups']['slug'], 'groups_screen_my_groups', 'my-groups' );
     200               
     201        $groups_link = $bp['loggedin_domain'] . $bp['groups']['slug'] . '/';
     202       
     203        /* Add the subnav items to the groups nav item */
     204        bp_core_add_subnav_item( $bp['groups']['slug'], 'my-groups', __('My Groups', 'buddypress'), $groups_link, 'groups_screen_my_groups', 'my-groups-list' );
     205        bp_core_add_subnav_item( $bp['groups']['slug'], 'create', __('Create a Group', 'buddypress'), $groups_link, 'groups_screen_create_group', false, bp_is_home() );
     206        bp_core_add_subnav_item( $bp['groups']['slug'], 'invites', __('Invites', 'buddypress'), $groups_link, 'groups_screen_group_invites', false, bp_is_home() );
     207       
     208        if ( $bp['current_component'] == $bp['groups']['slug'] ) {
     209               
     210                if ( bp_is_home() && !$is_single_group ) {
     211                       
     212                        $bp['bp_options_title'] = __('My Groups', 'buddypress');
     213                       
     214                } else if ( !bp_is_home() && !$is_single_group ) {
     215
     216                        $bp['bp_options_avatar'] = bp_core_get_avatar( $bp['current_userid'], 1 );
     217                        $bp['bp_options_title'] = $bp['current_fullname'];
     218                       
     219                } else if ( $is_single_group ) {
     220                        // We are viewing a single group, so set up the
     221                        // group navigation menu using the $group_obj global.
     222                       
     223                        /* When in a single group, the first action is bumped down one because of the
     224                           group name, so we need to adjust this and set the group name to current_item. */
     225                        $bp['current_item'] = $bp['current_action'];
     226                        $bp['current_action'] = $bp['action_variables'][0];
     227                        array_shift($bp['action_variables']);
     228                                                                       
     229                        $bp['bp_options_title'] = bp_create_excerpt( $group_obj->name, 1 );
     230                        $bp['bp_options_avatar'] = '<img src="' . $group_obj->avatar_thumb . '" alt="Group Avatar Thumbnail" />';
     231                       
     232                        $group_link = $bp['root_domain'] . '/' . $bp['groups']['slug'] . '/' . $group_obj->slug . '/';
     233                       
     234                        // If this is a private or hidden group, does the user have access?
     235                        if ( $group_obj->status == 'private' || $group_obj->status == 'hidden' ) {
     236                                if ( groups_is_user_member( $bp['loggedin_userid'], $group_obj->id ) && is_user_logged_in() )
     237                                        $has_access = true;
     238                                else
     239                                        $has_access = false;
     240                        } else {
     241                                $has_access = true;
     242                        }
     243
     244                        // Reset the existing subnav items
     245                        bp_core_reset_subnav_items($bp['groups']['slug']);
     246                       
     247                        bp_core_add_nav_default( $bp['groups']['slug'], 'groups_screen_group_home', 'home' );
     248                        bp_core_add_subnav_item( $bp['groups']['slug'], 'home', __('Home', 'buddypress'), $group_link, 'groups_screen_group_home', 'group-home' );
     249                       
     250                        // If the user is a group mod or more, then show the group admin nav item */
     251                        if ( $bp['is_item_mod'] || $bp['is_item_admin'] )
     252                                bp_core_add_subnav_item( $bp['groups']['slug'], 'admin', __('Admin', 'buddypress'), $group_link , 'groups_screen_group_admin', 'group-admin', ( $bp['is_item_admin'] + (int)$bp['is_item_mod'] ) );
     253                       
     254                        // If this is a private group, and the user is not a member, show a "Request Membership" nav item.
     255                        if ( !$has_access && !groups_check_for_membership_request( $bp['loggedin_userid'], $group_obj->id ) )
     256                                bp_core_add_subnav_item( $bp['groups']['slug'], 'request-membership', __('Request Membership', 'buddypress'), $group_link , 'groups_screen_group_request_membership', 'request-membership' );
     257                       
     258                        if ( $has_access && $group_obj->enable_forum && function_exists('bp_forums_setup') )
     259                                bp_core_add_subnav_item( $bp['groups']['slug'], 'forum', __('Forum', 'buddypress'), $group_link , 'groups_screen_group_forum', 'group-forum', $is_visible);
     260
     261                        if ( $has_access && $group_obj->enable_wire && function_exists('bp_wire_install') )
     262                                bp_core_add_subnav_item( $bp['groups']['slug'], 'wire', __('Wire', 'buddypress'), $group_link, 'groups_screen_group_wire', 'group-wire', $is_visible );
     263
     264                        if ( $has_access && $group_obj->enable_photos && function_exists('bp_gallery_install') )
     265                                bp_core_add_subnav_item( $bp['groups']['slug'], 'photos', __('Photos', 'buddypress'), $group_link, 'groups_screen_group_photos', 'group-photos', $is_visible );
     266
     267                        if ( $has_access )
     268                                bp_core_add_subnav_item( $bp['groups']['slug'], 'members', __('Members', 'buddypress'), $group_link, 'groups_screen_group_members', 'group-members', $is_visible );
     269                       
     270                        if ( is_user_logged_in() && groups_is_user_member( $bp['loggedin_userid'], $group_obj->id ) ) {
     271                                if ( function_exists('friends_install') )
     272                                        bp_core_add_subnav_item( $bp['groups']['slug'], 'send-invites', __('Send Invites', 'buddypress'), $group_link, 'groups_screen_group_invite', 'group-invite', $is_member );
     273                               
     274                                bp_core_add_subnav_item( $bp['groups']['slug'], 'leave-group', __('Leave Group', 'buddypress'), $group_link, 'groups_screen_group_leave', 'group-leave', $is_member );
     275                        }
     276                }
     277        }
     278}
     279add_action( 'wp', 'groups_setup_nav', 2 );
     280add_action( 'admin_menu', 'groups_setup_nav', 2 );
     281
     282
     283/***** Screens **********/
     284
     285function groups_screen_my_groups() {
     286        global $bp;
     287               
     288        do_action( 'groups_screen_my_groups' );
     289       
     290        bp_catch_uri( 'groups/index' );
     291}
     292
     293function groups_screen_group_invites() {
     294        global $bp;
     295       
     296        $group_id = $bp['action_variables'][1];
     297       
     298        if ( isset($bp['action_variables']) && in_array( 'accept', $bp['action_variables'] ) && is_numeric($group_id) ) {
     299               
     300                if ( !groups_accept_invite( $bp['loggedin_userid'], $group_id ) ) {
     301                        bp_core_add_message( __('Group invite could not be accepted', 'buddypress'), 'error' );                         
     302                } else {
     303                        bp_core_add_message( __('Group invite accepted', 'buddypress') );
     304                       
     305                        /* Record this in activity streams */
     306                        groups_record_activity( array( 'item_id' => $group_id, 'component_name' => 'groups', 'component_action' => 'joined_group', 'is_private' => 0 ) );
     307                }
     308
     309                bp_core_redirect( $bp['loggedin_domain'] . $bp['current_component'] . '/' . $bp['current_action'] );
     310               
     311        } else if ( isset($bp['action_variables']) && in_array( 'reject', $bp['action_variables'] ) && is_numeric($group_id) ) {
     312               
     313                if ( !groups_reject_invite( $group_id, $bp['loggedin_userid'] ) ) {
     314                        bp_core_add_message( __('Group invite could not be rejected', 'buddypress'), 'error' );                                                 
     315                } else {                       
     316                        bp_core_add_message( __('Group invite rejected', 'buddypress') );                       
     317                }
     318
     319                bp_core_redirect( $bp['loggedin_domain'] . $bp['current_component'] . '/' . $bp['current_action'] );
     320        }
     321       
     322        // Remove notifications
     323        bp_core_delete_notifications_for_user_by_type( $bp['loggedin_userid'], 'groups', 'group_invite' );
     324
     325        do_action( 'groups_screen_group_invites', $group_id );
     326       
     327        bp_catch_uri( 'groups/list-invites' ); 
     328}
     329
     330function groups_screen_create_group() {
     331        global $bp;
     332        global $create_group_step, $group_obj, $completed_to_step;
     333       
     334        $no_instantiate = false;
     335        $reset_steps = false;
     336       
     337        if ( !$create_group_step = $bp['action_variables'][1] ) {
     338                $create_group_step = '1';
     339                $completed_to_step = 0;
     340               
     341                unset($_SESSION['group_obj_id']);
     342                unset($_SESSION['completed_to_step']);
     343               
     344                $no_instantiate = true;
     345                $reset_steps = true;
     346        }
     347       
     348        if ( isset($_SESSION['completed_to_step']) && !$reset_steps ) {
     349                $completed_to_step = $_SESSION['completed_to_step'];
     350        }
     351       
     352        if ( isset( $_POST['save'] ) || isset( $_POST['skip'] ) ) {
     353                $group_obj = new BP_Groups_Group( $_SESSION['group_obj_id'] );
     354
     355                // If the user skipped the avatar step, move onto the next step and don't save anything.
     356                if ( isset( $_POST['skip'] ) && $create_group_step == "3" ) {
     357                        $create_group_step++;
     358                        $completed_to_step++;
     359                        $_SESSION['completed_to_step'] = $completed_to_step;
     360                } else {
     361                       
     362                        // We're done.
     363                        if ( $create_group_step == 4 )
     364                                bp_core_redirect( bp_group_permalink( $group_obj, false ) );
     365                       
     366                        if ( !$group_id = groups_manage_group( $create_group_step, $_SESSION['group_obj_id'] ) ) {
     367                                bp_core_add_message( __('There was an error saving group details. Please try again.', 'buddypress'), 'error' );
     368                                bp_core_redirect( $bp['loggedin_domain'] . $bp['groups']['slug'] . '/create/step/' . $create_group_step );
     369                        } else {
     370                                $create_group_step++;
     371                                $completed_to_step++;
     372                                $_SESSION['completed_to_step'] = $completed_to_step;
     373                                $_SESSION['group_obj_id'] = $group_id;
     374                        }
     375                }
     376        }
     377
     378        if ( isset($_SESSION['group_obj_id']) && !$group_obj && !$no_instantiate )
     379                $group_obj = new BP_Groups_Group( $_SESSION['group_obj_id'] );
     380       
     381        bp_catch_uri( 'groups/create' );
     382}
     383
     384function groups_screen_group_home() {
     385        global $is_single_group, $bp;
     386       
     387        if ( $is_single_group ) {
     388               
     389                if ( isset($_GET['new']) ) {
     390                        // Delete group request notifications for the user
     391                        bp_core_delete_notifications_for_user_by_type( $bp['loggedin_userid'], 'groups', 'membership_request_accepted' );
     392                        bp_core_delete_notifications_for_user_by_type( $bp['loggedin_userid'], 'groups', 'membership_request_rejected' );
     393                        bp_core_delete_notifications_for_user_by_type( $bp['loggedin_userid'], 'groups', 'member_promoted_to_mod' );
     394                        bp_core_delete_notifications_for_user_by_type( $bp['loggedin_userid'], 'groups', 'member_promoted_to_admin' );
     395                }       
     396
     397                do_action( 'groups_screen_group_home' );       
     398               
     399                bp_catch_uri( 'groups/group-home' );           
     400        }
     401}
     402
     403function groups_screen_group_forum() {
     404        global $is_single_group, $bp, $group_obj;
     405       
     406        if ( $is_single_group ) {
     407                $topic_id = $bp['action_variables'][1];
     408                $forum_id = groups_get_groupmeta( $group_obj->id, 'forum_id' );
     409               
     410                if ( $topic_id ) {
     411                       
     412                        /* Posting a reply */
     413                        if ( isset( $_POST['submit_reply'] ) && function_exists( 'bp_forums_new_post') ) {
     414                                groups_new_group_forum_post( $_POST['reply_text'], $topic_id );
     415                                bp_core_redirect( bp_group_permalink( $group_obj, false ) . '/forum/topic/' . $topic_id );
     416                        }
     417                       
     418                        do_action( 'groups_screen_group_forum_topic' );
     419                       
     420                        // If we are viewing a topic, load it.
     421                        bp_catch_uri( 'groups/forum/topic' );
     422                } else {
     423
     424                        /* Posting a topic */
     425                        if ( isset( $_POST['submit_topic'] ) && function_exists( 'bp_forums_new_topic') ) {
     426                                groups_new_group_forum_topic( $_POST['topic_title'], $_POST['topic_text'], $_POST['topic_tags'], $forum_id );
     427                                bp_core_redirect( bp_group_permalink( $group_obj, false ) . '/forum/' );
     428                        }
     429                       
     430                        do_action( 'groups_screen_group_forum', $topic_id, $forum_id );
     431                       
     432                        // Load the forum home.
     433                        bp_catch_uri( 'groups/forum/index' );                           
     434                }
     435        }
     436}
     437
     438function groups_screen_group_wire() {
     439        global $bp;
     440        global $is_single_group, $group_obj;
     441       
     442        $wire_action = $bp['action_variables'][0];
     443               
     444        if ( $is_single_group ) {
     445                if ( $wire_action == 'post' && BP_Groups_Member::check_is_member( $bp['loggedin_userid'], $group_obj->id ) ) {
     446
     447                        if ( !groups_new_wire_post( $group_obj->id, $_POST['wire-post-textarea'] ) ) {
     448                                bp_core_add_message( __('Wire message could not be posted.', 'buddypress'), 'error' );
     449                        } else {
     450                                bp_core_add_message( __('Wire message successfully posted.', 'buddypress') );
     451                        }
     452
     453                        if ( !strpos( $_SERVER['HTTP_REFERER'], $bp['wire']['slug'] ) ) {
     454                                bp_core_redirect( bp_group_permalink( $group_obj, false ) );
     455                        } else {
     456                                bp_core_redirect( bp_group_permalink( $group_obj, false ) . '/' . $bp['wire']['slug'] );
     457                        }
     458       
     459                } else if ( $wire_action == 'delete' && BP_Groups_Member::check_is_member( $bp['loggedin_userid'], $group_obj->id ) ) {
     460                        $wire_message_id = $bp['action_variables'][1];
     461
     462                        if ( !groups_delete_wire_post( $wire_message_id, $bp['groups']['table_name_wire'] ) ) {
     463                                bp_core_add_message( __('There was an error deleting the wire message.', 'buddypress'), 'error' );
     464                        } else {
     465                                bp_core_add_message( __('Wire message successfully deleted.', 'buddypress') );
     466                        }
     467                       
     468                        if ( !strpos( $_SERVER['HTTP_REFERER'], $bp['wire']['slug'] ) ) {
     469                                bp_core_redirect( bp_group_permalink( $group_obj, false ) );
     470                        } else {
     471                                bp_core_redirect( bp_group_permalink( $group_obj, false ) . '/' . $bp['wire']['slug'] );
     472                        }
     473               
     474                } else if ( ( !$wire_action || $bp['action_variables'][1] == 'latest' ) ) {
     475                        bp_catch_uri( 'groups/wire' );
     476                } else {
     477                        bp_catch_uri( 'groups/group-home' );
     478                }
     479        }
     480}
     481
     482function groups_screen_group_members() {
     483        global $bp;
     484        global $is_single_group, $group_obj;
     485       
     486        if ( $is_single_group ) {
     487                do_action( 'groups_screen_group_members', $group_obj->id );
     488               
     489                bp_catch_uri( 'groups/list-members' );
     490        }
     491}
     492
     493function groups_screen_group_photos() {
     494        global $bp;
     495        global $is_single_group, $group_obj;
     496       
     497        if ( $is_single_group ) {
     498                // Not implemented yet.
     499                bp_catch_uri( 'groups/group-home' );
     500        }
     501}
     502
     503function groups_screen_group_invite() {
     504        global $bp;
     505        global $is_single_group, $group_obj;
     506       
     507        if ( $is_single_group ) {
     508                if ( isset($bp['action_variables']) && $bp['action_variables'][0] == 'send' ) {
     509                        // Send the invites.
     510                        groups_send_invites($group_obj);
     511                       
     512                        bp_core_add_message( __('Group invites sent.', 'buddypress') );
     513                        bp_core_redirect( bp_group_permalink( $group_obj, false ) );
     514                } else {
     515                        do_action( 'groups_screen_group_invite', $group_obj->id );
     516                       
     517                        // Show send invite page
     518                        bp_catch_uri( 'groups/send-invite' );   
     519                }
     520        }
     521}
     522
     523function groups_screen_group_leave() {
     524        global $bp;
     525        global $is_single_group, $group_obj;
     526       
     527        if ( $is_single_group ) {
     528                if ( isset($bp['action_variables']) && $bp['action_variables'][0] == 'yes' ) {
     529                        // remove the user from the group.
     530                        if ( !groups_leave_group( $group_obj->id ) ) {
     531                                bp_core_add_message(  __('There was an error leaving the group. Please try again.', 'buddypress'), 'error' );
     532                                bp_core_redirect( bp_group_permalink( $group_obj, false) );
     533                        } else {
     534                                bp_core_add_message( __('You left the group successfully.', 'buddypress') );
     535                                bp_core_redirect( $bp['loggedin_domain'] . $bp['groups']['slug'] );
     536                        }
     537                } else if ( isset($bp['action_variables']) && $bp['action_variables'][0] == 'no' ) {
     538                        bp_core_redirect( bp_group_permalink( $group_obj, false) );
     539                } else {
     540                        do_action( 'groups_screen_group_leave', $group_obj->id );
     541                       
     542                        // Show leave group page
     543                        bp_catch_uri( 'groups/leave-group-confirm' );
     544                }
     545        }
     546}
     547
     548function groups_screen_group_request_membership() {
     549        global $bp, $group_obj;
     550       
     551        if ( !is_user_logged_in() )
     552                return false;
     553       
     554        if ( $group_obj->status == 'private' ) {
     555                // If the user has submitted a request, send it.
     556                if ( isset( $_POST['group-request-send']) ) {
     557                        if ( !groups_send_membership_request( $bp['loggedin_userid'], $group_obj->id ) ) {
     558                                bp_core_add_message( __( 'There was an error sending your group membership request, please try again.', 'buddypress' ), 'error' );
     559                        } else {
     560                                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' ) );
     561                        }
     562                        bp_core_redirect( bp_group_permalink( $group_obj, false ) );
     563                }
     564               
     565                do_action( 'groups_screen_group_request_membership', $group_obj->id );
     566               
     567                bp_catch_uri( 'groups/request-membership' );
     568        }
     569}
     570
     571function groups_screen_group_admin() {
     572        global $bp, $group_obj;
     573       
     574        if ( $bp['current_component'] == $bp['groups']['slug'] && !$bp['action_variables'] ) {
     575               
     576                do_action( 'groups_screen_group_admin', $group_obj->id );
     577               
     578                bp_catch_uri( 'groups/admin/edit-details' );           
     579        }
     580
     581}
     582
     583function groups_screen_group_admin_edit_details() {
     584        global $bp, $group_obj;
     585       
     586        if ( $bp['current_component'] == $bp['groups']['slug'] && $bp['action_variables'][0] == 'edit-details' ) {
     587       
     588                if ( $bp['is_item_admin'] || $bp['is_item_mod']  ) {
     589               
     590                        // If the edit form has been submitted, save the edited details
     591                        if ( isset( $_POST['save'] ) ) {
     592                                if ( !groups_edit_base_group_details( $_POST['group-id'], $_POST['group-name'], $_POST['group-desc'], $_POST['group-news'], (int)$_POST['group-notify-members'] ) ) {
     593                                        bp_core_add_message( __( 'There was an error updating group details, please try again.', 'buddypress' ), 'error' );
     594                                } else {
     595                                        bp_core_add_message( __( 'Group details were successfully updated.', 'buddypress' ) );
     596                                }
     597                                bp_core_redirect( site_url() . '/' . $bp['current_component'] . '/' . $bp['current_item'] . '/admin/edit-details' );
     598                        }
     599
     600                        do_action( 'groups_screen_group_admin_edit_details', $group_obj->id );
     601
     602                        bp_catch_uri( 'groups/admin/edit-details' );
     603                       
     604                }
     605        }
     606}
     607add_action( 'wp', 'groups_screen_group_admin_edit_details', 4 );
     608
     609
     610function groups_screen_group_admin_settings() {
     611        global $bp, $group_obj;
     612       
     613        if ( $bp['current_component'] == $bp['groups']['slug'] && $bp['action_variables'][0] == 'group-settings' ) {
     614               
     615                if ( !$bp['is_item_admin'] )
     616                        return false;
     617               
     618                // If the edit form has been submitted, save the edited details
     619                if ( isset( $_POST['save'] ) ) {
     620                        $enable_wire = ( isset($_POST['group-show-wire'] ) ) ? 1 : 0;
     621                        $enable_forum = ( isset($_POST['group-show-forum'] ) ) ? 1 : 0;
     622                        $enable_photos = ( isset($_POST['group-show-photos'] ) ) ? 1 : 0;
     623                        $photos_admin_only = ( $_POST['group-photos-status'] != 'all' ) ? 1 : 0;
     624                        $status = $_POST['group-status'];
     625                       
     626                        if ( !groups_edit_group_settings( $_POST['group-id'], $enable_wire, $enable_forum, $enable_photos, $photos_admin_only, $status ) ) {
     627                                bp_core_add_message( __( 'There was an error updating group settings, please try again.', 'buddypress' ), 'error' );
     628                        } else {
     629                                bp_core_add_message( __( 'Group settings were successfully updated.', 'buddypress' ) );
     630                        }
     631                       
     632                        bp_core_redirect( site_url() . '/' . $bp['current_component'] . '/' . $bp['current_item'] . '/admin/group-settings' );
     633                }
     634
     635                do_action( 'groups_screen_group_admin_settings', $group_obj->id );
     636               
     637                bp_catch_uri( 'groups/admin/group-settings' );
     638        }
     639}
     640add_action( 'wp', 'groups_screen_group_admin_settings', 4 );
     641
     642function groups_screen_group_admin_manage_members() {
     643        global $bp, $group_obj;
     644
     645        if ( $bp['current_component'] == $bp['groups']['slug'] && $bp['action_variables'][0] == 'manage-members' ) {
     646               
     647                if ( !$bp['is_item_admin'] )
     648                        return false;
     649               
     650                if ( $bp['action_variables'][1] == 'promote' && is_numeric( $bp['action_variables'][2] ) ) {
     651                        $user_id = $bp['action_variables'][2];
     652                       
     653                        // Promote a user.
     654                        if ( !groups_promote_member( $user_id, $group_obj->id ) ) {
     655                                bp_core_add_message( __( 'There was an error when promoting that user, please try again', 'buddypress' ), 'error' );
     656                        } else {
     657                                bp_core_add_message( __( 'User promoted successfully', 'buddypress' ) );
     658                        }
     659                       
     660                        do_action( 'bp_groups_promoted_member', $user_id, $group_obj->id );
     661                       
     662                        bp_core_redirect( site_url() . '/' . $bp['current_component'] . '/' . $bp['current_item'] . '/admin/manage-members' );
     663                }
     664               
     665                if ( $bp['action_variables'][1] == 'demote' && is_numeric( $bp['action_variables'][2] ) ) {
     666                        $user_id = $bp['action_variables'][2];
     667                       
     668                        // Demote a user.
     669                        if ( !groups_demote_member( $user_id, $group_obj->id ) ) {
     670                                bp_core_add_message( __( 'There was an error when demoting that user, please try again', 'buddypress' ), 'error' );
     671                        } else {
     672                                bp_core_add_message( __( 'User demoted successfully', 'buddypress' ) );
     673                        }
     674
     675                        do_action( 'bp_groups_demoted_member', $user_id, $group_obj->id );
     676                       
     677                        bp_core_redirect( site_url() . '/' . $bp['current_component'] . '/' . $bp['current_item'] . '/admin/manage-members' );
     678                }
     679               
     680                if ( $bp['action_variables'][1] == 'ban' && is_numeric( $bp['action_variables'][2] ) ) {
     681                        $user_id = $bp['action_variables'][2];
     682                       
     683                        // Ban a user.
     684                        if ( !groups_ban_member( $user_id, $group_obj->id ) ) {
     685                                bp_core_add_message( __( 'There was an error when banning that user, please try again', 'buddypress' ), 'error' );
     686                        } else {
     687                                bp_core_add_message( __( 'User banned successfully', 'buddypress' ) );
     688                        }
     689
     690                        do_action( 'bp_groups_banned_member', $user_id, $group_obj->id );
     691                       
     692                        bp_core_redirect( site_url() . '/' . $bp['current_component'] . '/' . $bp['current_item'] . '/admin/manage-members' );
     693                }
     694               
     695                if ( $bp['action_variables'][1] == 'unban' && is_numeric( $bp['action_variables'][2] ) ) {
     696                        $user_id = $bp['action_variables'][2];
     697                       
     698                        // Remove a ban for user.
     699                        if ( !groups_unban_member( $user_id, $group_obj->id ) ) {
     700                                bp_core_add_message( __( 'There was an error when unbanning that user, please try again', 'buddypress' ), 'error' );
     701                        } else {
     702                                bp_core_add_message( __( 'User ban removed successfully', 'buddypress' ) );
     703                        }
     704
     705                        do_action( 'bp_groups_unbanned_member', $user_id, $group_obj->id );
     706                       
     707                        bp_core_redirect( site_url() . '/' . $bp['current_component'] . '/' . $bp['current_item'] . '/admin/manage-members' );
     708                }
     709
     710                do_action( 'groups_screen_group_admin_manage_members', $group_obj->id );
     711               
     712                bp_catch_uri( 'groups/admin/manage-members' );
     713        }
     714}
     715add_action( 'wp', 'groups_screen_group_admin_manage_members', 4 );
     716
     717
     718function groups_screen_group_admin_requests() {
     719        global $bp, $group_obj;
     720       
     721        if ( $bp['current_component'] == $bp['groups']['slug'] && $bp['action_variables'][0] == 'membership-requests' ) {
     722               
     723                if ( !$bp['is_item_admin'] || $group_obj->status == 'public' )
     724                        return false;
     725               
     726                // Remove any screen notifications
     727                bp_core_delete_notifications_for_user_by_type( $bp['loggedin_userid'], 'groups', 'new_membership_request' );
     728               
     729                $request_action = $bp['action_variables'][1];
     730                $membership_id = $bp['action_variables'][2];
     731
     732                if ( isset($request_action) && isset($membership_id) ) {
     733                        if ( $request_action == 'accept' && is_numeric($membership_id) ) {
     734                                // Accept the membership request
     735                                if ( !groups_accept_membership_request( $membership_id ) ) {
     736                                        bp_core_add_message( __( 'There was an error accepting the membership request, please try again.', 'buddypress' ), 'error' );
     737                                } else {
     738                                        bp_core_add_message( __( 'Group membership request accepted', 'buddypress' ) );
     739                                }
     740                        } else if ( $request_action == 'reject' && is_numeric($membership_id) ) {
     741                                // Reject the membership request
     742                                if ( !groups_reject_membership_request( $membership_id ) ) {
     743                                        bp_core_add_message( __( 'There was an error rejecting the membership request, please try again.', 'buddypress' ), 'error' );
     744                                } else {
     745                                        bp_core_add_message( __( 'Group membership request rejected', 'buddypress' ) );
     746                                }       
     747                        }
     748                        bp_core_redirect( site_url() . '/' . $bp['current_component'] . '/' . $bp['current_item'] . '/admin/membership-requests' );
     749                }
     750
     751                do_action( 'groups_screen_group_admin_requests', $group_obj->id );
     752               
     753                bp_catch_uri( 'groups/admin/membership-requests' );
     754        }
     755}
     756add_action( 'wp', 'groups_screen_group_admin_requests', 4 );
     757
     758function groups_screen_group_admin_delete_group() {
     759        global $bp, $group_obj;
     760       
     761        if ( $bp['current_component'] == $bp['groups']['slug'] && $bp['action_variables'][0] == 'delete-group' ) {
     762               
     763                if ( !$bp['is_item_admin'] )
     764                        return false;
     765               
     766                if ( isset( $_POST['delete-group-button'] ) && isset( $_POST['delete-group-understand'] ) ) {
     767                        // Group admin has deleted the group, now do it.
     768                        if ( !groups_delete_group( $_POST['group-id']) ) {
     769                                bp_core_add_message( __( 'There was an error deleting the group, please try again.', 'buddypress' ), 'error' );
     770                        } else {
     771                                bp_core_add_message( __( 'The group was deleted successfully', 'buddypress' ) );
     772                                bp_core_redirect( site_url() . '/' . $bp['groups']['slug'] . '/' );
     773                        }
     774                        bp_core_redirect( $bp['loggedin_domain'] . $bp['current_component'] );
     775                } else {
     776                        do_action( 'groups_screen_group_admin_delete_group', $group_obj->id );
     777                       
     778                        bp_catch_uri( 'groups/admin/delete-group' );
     779                }
     780        }
     781}
     782add_action( 'wp', 'groups_screen_group_admin_delete_group', 4 );
     783
     784function groups_screen_notification_settings() {
     785        global $current_user; ?>
     786        <table class="notification-settings" id="groups-notification-settings">
     787                <tr>
     788                        <th class="icon"></th>
     789                        <th class="title"><?php _e( 'Groups', 'buddypress' ) ?></th>
     790                        <th class="yes"><?php _e( 'Yes', 'buddypress' ) ?></th>
     791                        <th class="no"><?php _e( 'No', 'buddypress' )?></th>
     792                </tr>
     793                <tr>
     794                        <td></td>
     795                        <td><?php _e( 'A member invites you to join a group', 'buddypress' ) ?></td>
     796                        <td class="yes"><input type="radio" name="notifications[notification_groups_invite]" value="yes" <?php if ( !get_usermeta( $current_user->id, 'notification_groups_invite') || get_usermeta( $current_user->id, 'notification_groups_invite') == 'yes' ) { ?>checked="checked" <?php } ?>/></td>
     797                        <td class="no"><input type="radio" name="notifications[notification_groups_invite]" value="no" <?php if ( get_usermeta( $current_user->id, 'notification_groups_invite') == 'no' ) { ?>checked="checked" <?php } ?>/></td>
     798                </tr>
     799                <tr>
     800                        <td></td>
     801                        <td><?php _e( 'Group information is updated', 'buddypress' ) ?></td>
     802                        <td class="yes"><input type="radio" name="notifications[notification_groups_group_updated]" value="yes" <?php if ( !get_usermeta( $current_user->id, 'notification_groups_group_updated') || get_usermeta( $current_user->id, 'notification_groups_group_updated') == 'yes' ) { ?>checked="checked" <?php } ?>/></td>
     803                        <td class="no"><input type="radio" name="notifications[notification_groups_group_updated]" value="no" <?php if ( get_usermeta( $current_user->id, 'notification_groups_group_updated') == 'no' ) { ?>checked="checked" <?php } ?>/></td>
     804                </tr>
     805                <?php if ( function_exists('bp_wire_install') ) { ?>
     806                <tr>
     807                        <td></td>
     808                        <td><?php _e( 'A member posts on the wire of a group you belong to', 'buddypress' ) ?></td>
     809                        <td class="yes"><input type="radio" name="notifications[notification_groups_wire_post]" value="yes" <?php if ( !get_usermeta( $current_user->id, 'notification_groups_wire_post') || get_usermeta( $current_user->id, 'notification_groups_wire_post') == 'yes' ) { ?>checked="checked" <?php } ?>/></td>
     810                        <td class="no"><input type="radio" name="notifications[notification_groups_wire_post]" value="no" <?php if ( get_usermeta( $current_user->id, 'notification_groups_wire_post') == 'no' ) { ?>checked="checked" <?php } ?>/></td>
     811                </tr>
     812                <?php } ?>
     813                <tr>
     814                        <td></td>
     815                        <td><?php _e( 'You are promoted to a group administrator or moderator', 'buddypress' ) ?></td>
     816                        <td class="yes"><input type="radio" name="notifications[notification_groups_admin_promotion]" value="yes" <?php if ( !get_usermeta( $current_user->id, 'notification_groups_admin_promotion') || get_usermeta( $current_user->id, 'notification_groups_admin_promotion') == 'yes' ) { ?>checked="checked" <?php } ?>/></td>
     817                        <td class="no"><input type="radio" name="notifications[notification_groups_admin_promotion]" value="no" <?php if ( get_usermeta( $current_user->id, 'notification_groups_admin_promotion') == 'no' ) { ?>checked="checked" <?php } ?>/></td>
     818                </tr>
     819                <tr>
     820                        <td></td>
     821                        <td><?php _e( 'A member requests to join a private group for which you are an admin', 'buddypress' ) ?></td>
     822                        <td class="yes"><input type="radio" name="notifications[notification_groups_membership_request]" value="yes" <?php if ( !get_usermeta( $current_user->id, 'notification_groups_membership_request') || get_usermeta( $current_user->id, 'notification_groups_membership_request') == 'yes' ) { ?>checked="checked" <?php } ?>/></td>
     823                        <td class="no"><input type="radio" name="notifications[notification_groups_membership_request]" value="no" <?php if ( get_usermeta( $current_user->id, 'notification_groups_membership_request') == 'no' ) { ?>checked="checked" <?php } ?>/></td>
     824                </tr>
     825               
     826                <?php do_action( 'groups_screen_notification_settings' ); ?>
     827        </table>
     828<?php   
     829}
     830add_action( 'bp_notification_settings', 'groups_screen_notification_settings' );
     831
     832
     833/***** Actions **********/
     834
     835function groups_action_join_group() {
     836        global $bp;
     837        global $is_single_group, $group_obj;   
     838
     839        if ( !$is_single_group || $bp['current_component'] != $bp['groups']['slug'] || $bp['current_action'] != 'join' )
     840                return false;
     841               
     842        // user wants to join a group
     843        if ( !groups_is_user_member( $bp['loggedin_userid'], $group_obj->id ) && !groups_is_user_banned( $bp['loggedin_userid'], $group_obj->id ) ) {
     844                if ( !groups_join_group($group_obj->id) ) {
     845                        bp_core_add_message( __('There was an error joining the group.', 'buddypress'), 'error' );
     846                } else {
     847                        bp_core_add_message( __('You joined the group!', 'buddypress') );
     848                }
     849                bp_core_redirect( bp_group_permalink( $group_obj, false ) );
     850        }
     851
     852        bp_catch_uri( 'groups/group-home' );
     853}
     854add_action( 'wp', 'groups_action_join_group', 3 );
     855
     856
     857/**************************************************************************
     858 groups_record_activity()
     859 
     860 Records activity for the logged in user within the friends component so that
     861 it will show in the users activity stream (if installed)
     862 **************************************************************************/
     863
     864function groups_record_activity( $args = true ) {
     865        global $group_obj;
     866       
     867        if ( function_exists('bp_activity_record') ) {
     868                extract($args);
     869               
     870                if ( $group_obj->status == 'public' )
     871                        bp_activity_record( $item_id, $component_name, $component_action, $is_private, $secondary_item_id, $user_id, $secondary_user_id );
     872        }
     873}
     874
     875function groups_delete_activity( $args = true ) {
     876        if ( function_exists('bp_activity_delete') ) {
     877                extract($args);
     878                bp_activity_delete( $item_id, $component_name, $component_action, $user_id, $secondary_item_id );
     879        }
     880}
     881
     882
     883/**************************************************************************
     884 groups_format_activity()
     885 
     886 Selects and formats recorded groups component activity.
     887 Example: Selects the groups details for a joined group, then
     888          formats it to read "Andy Peatling joined the group 'A Cool Group'"
     889 **************************************************************************/
     890
     891function groups_format_activity( $item_id, $user_id, $action, $secondary_item_id = false, $for_secondary_user = false  ) {
     892        global $bp;
     893       
     894        switch( $action ) {
     895                case 'joined_group':
     896                        $group = new BP_Groups_Group( $item_id );
     897                       
     898                        if ( !$group )
     899                                return false;
     900                               
     901                        $user_link = bp_core_get_userlink($user_id);
     902                        $group_link = bp_group_permalink( $group, false );
     903                       
     904                        return array(
     905                                'primary_link' => bp_group_permalink( $group, false ),
     906                                'content' => apply_filters( 'bp_groups_joined_group_activity', sprintf( __('%s joined the group %s', 'buddypress'), $user_link,  '<a href="' . $group_link . '">' . $group->name . '</a>' ) . ' <span class="time-since">%s</span>', $user_link, $group_link, $group->name )
     907                        );                             
     908                break;
     909                case 'created_group':
     910                        $group = new BP_Groups_Group( $item_id );
     911                       
     912                        if ( !$group )
     913                                return false;
     914                       
     915                        $user_link = bp_core_get_userlink($user_id);
     916                        $group_link = bp_group_permalink( $group, false );
     917                       
     918                        return array(
     919                                'primary_link' => bp_group_permalink( $group, false ),
     920                                'content' => apply_filters( 'bp_groups_created_group_activity', sprintf( __('%s created the group %s', 'buddypress'), $user_link, '<a href="' . $group_link . '">' . $group->name . '</a>') . ' <span class="time-since">%s</span>', $user_link, $group_link, $group->name )
     921                        );
     922                break;
     923                case 'new_wire_post':
     924                        $wire_post = new BP_Wire_Post( $bp['groups']['table_name_wire'], $item_id );
     925                        $group = new BP_Groups_Group( $wire_post->item_id );
     926                       
     927                        if ( !$group || !$wire_post || !$wire_post->content )
     928                                return false;           
     929
     930                        $user_link = bp_core_get_userlink($user_id);
     931                        $group_link = bp_group_permalink( $group, false );
     932                        $post_excerpt = bp_create_excerpt($wire_post->content);
     933                                       
     934                        $content = sprintf ( __('%s wrote on the wire of the group %s', 'buddypress'), $user_link, '<a href="' . $group_link . '">' . $group->name . '</a>' ) . ' <span class="time-since">%s</span>';                 
     935                        $content .= '<blockquote>' . $post_excerpt . '</blockquote>';
     936                       
     937                        $content = apply_filters( 'bp_groups_new_wire_post_activity', $content, $user_link, $group_link, $group->name, $post_excerpt );
     938                       
     939                        return array(
     940                                'primary_link' => bp_group_permalink( $group, false ),
     941                                'content' => $content
     942                        );
     943                break;
     944                case 'new_forum_post':
     945                        if ( function_exists('bp_forums_setup') ) {
     946                                $group = new BP_Groups_Group( $item_id );
     947                                $forum_post = bp_forums_get_post( $secondary_item_id );
     948                                $forum_topic = bp_forums_get_topic_details( $forum_post['topic_id'] );
     949
     950                                if ( !$group || !$forum_post || !$forum_topic )
     951                                        return false;           
     952
     953                                $user_link = bp_core_get_userlink($user_id);
     954                                $group_link = bp_group_permalink( $group, false );
     955                               
     956                                $post_content = apply_filters( 'bp_the_topic_post_content', stripslashes( $forum_post['post_text'] ) );
     957                       
     958                                $content = sprintf ( __('%s posted on the forum topic %s in the group %s:', 'buddypress'), $user_link, '<a href="' . $group_link . '/forum/topic/' . $forum_topic['topic_id'] . '">' . $forum_topic['topic_title'] . '</a>', '<a href="' . $group_link . '">' . $group->name . '</a>' ) . ' <span class="time-since">%s</span>';                       
     959                                $content .= '<blockquote>' . $post_content . '</blockquote>';
     960                               
     961                                $content = apply_filters( 'bp_groups_new_forum_post_activity', $content, $user_link, $group_link, $forum_topic['topic_id'], $forum_topic['topic_title'], $group_link, $group->name, $post_content );
     962
     963                                return array(
     964                                        'primary_link' => bp_group_permalink( $group, false ),
     965                                        'content' => $content
     966                                );
     967                        }
     968                break;
     969                case 'new_forum_topic':
     970                        if ( function_exists('bp_forums_setup') ) {
     971                                $group = new BP_Groups_Group( $item_id );
     972                                $forum_topic = bp_forums_get_topic_details( $secondary_item_id );
     973                                $forum_post = bp_forums_get_post( $forum_topic['topic_last_post_id'] );
     974
     975                                if ( !$group || !$forum_post || !$forum_topic )
     976                                        return false;
     977                                       
     978                                $user_link = bp_core_get_userlink($user_id);
     979                                $group_link = bp_group_permalink( $group, false );
     980                               
     981                                $post_content = apply_filters( 'bp_the_topic_post_content', stripslashes( $forum_post['post_text'] ) );
     982                               
     983                                $content = sprintf ( __('%s created the forum topic %s in the group %s:', 'buddypress'), $uesr_link, '<a href="' . $group_link . '/forum/topic/' . $forum_topic['topic_id'] . '">' . $forum_topic['topic_title'] . '</a>', '<a href="' . $group_link . '">' . $group->name . '</a>' ) . ' <span class="time-since">%s</span>';                 
     984                                $content .= '<blockquote>' . $post_content . '</blockquote>';
     985                               
     986                                $content = apply_filters( 'bp_groups_new_forum_topic_activity', $content, $user_link, $group_link, $forum_topic['topic_id'], $forum_topic['topic_title'], $group_link, $group->name, $post_content );
     987
     988                                return array(
     989                                        'primary_link' => bp_group_permalink( $group, false ),
     990                                        'content' => $content
     991                                );
     992                        }
     993                break;         
     994        }
     995       
     996        do_action( 'groups_format_activity', $action, $item_id, $user_id, $action, $secondary_item_id, $for_secondary_user );
     997       
     998        return false;
     999}
     1000
     1001function groups_format_notifications( $action, $item_id, $secondary_item_id, $total_items ) {
     1002        global $bp;
     1003       
     1004        switch ( $action ) {
     1005                case 'new_membership_request':
     1006                        $group_id = $secondary_item_id;
     1007                        $requesting_user_id = $item_id;
     1008                       
     1009                        $group = new BP_Groups_Group( $group_id, false, false );
     1010                       
     1011                        $group_link = bp_group_permalink( $group, false );
     1012                       
     1013                        if ( (int)$total_items > 1 ) {
     1014                                return apply_filters( 'bp_groups_multiple_new_membership_requests_notification', '<a href="' . $group_link . '/admin/membership-requests/" title="' . __( 'Group Membership Requests', 'buddypress' ) . '">' . sprintf( __('%d new membership requests for the group "%s"'), (int)$total_items, $group->name ) . '</a>', $group_link, $total_items, $group->name );             
     1015                        } else {
     1016                                $user_fullname = bp_core_global_user_fullname( $requesting_user_id );
     1017                                return apply_filters( 'bp_groups_single_new_membership_request_notification', '<a href="' . $group_link . '/admin/membership-requests/" title="' . $user_fullname .' requests group membership">' . sprintf( __('%s requests membership for the group "%s"'), $user_fullname, $group->name ) . '</a>', $group_link, $user_fullname, $group->name );
     1018                        }       
     1019                break;
     1020               
     1021                case 'membership_request_accepted':
     1022                        $group_id = $item_id;
     1023                       
     1024                        $group = new BP_Groups_Group( $group_id, false, false );
     1025                        $group_link = bp_group_permalink( $group, false )  . '/?new';
     1026                       
     1027                        if ( (int)$total_items > 1 ) {
     1028                                return apply_filters( 'bp_groups_multiple_membership_request_accepted_notification', '<a href="' . $bp['loggedin_domain'] . $bp['groups']['slug'] . '" title="' . __( 'Groups', 'buddypress' ) . '">' . sprintf( __('%d accepted group membership requests'), (int)$total_items, $group->name ) . '</a>', $total_items, $group_name );         
     1029                        } else {
     1030                                return apply_filters( 'bp_groups_single_membership_request_accepted_notification', '<a href="' . $group_link . '">' . sprintf( __('Membership for group "%s" accepted'), $group->name ) . '</a>', $group_link, $group->name );
     1031                        }       
     1032                break;
     1033               
     1034                case 'membership_request_rejected':
     1035                        $group_id = $item_id;
     1036                       
     1037                        $group = new BP_Groups_Group( $group_id, false, false );
     1038                        $group_link = bp_group_permalink( $group, false )  . '/?new';
     1039                       
     1040                        if ( (int)$total_items > 1 ) {
     1041                                return apply_filters( 'bp_groups_multiple_membership_request_rejected_notification', '<a href="' . site_url() . '/' . MEMBERS_SLUG . '/' . $bp['groups']['slug'] . '" title="' . __( 'Groups', 'buddypress' ) . '">' . sprintf( __('%d rejected group membership requests'), (int)$total_items, $group->name ) . '</a>', $total_items, $group->name );         
     1042                        } else {
     1043                                return apply_filters( 'bp_groups_single_membership_request_rejected_notification', '<a href="' . $group_link . '">' . sprintf( __('Membership for group "%s" rejected'), $group->name ) . '</a>', $group_link, $group->name );
     1044                        }       
     1045               
     1046                break;
     1047               
     1048                case 'member_promoted_to_admin':
     1049                        $group_id = $item_id;
     1050               
     1051                        $group = new BP_Groups_Group( $group_id, false, false );
     1052                        $group_link = bp_group_permalink( $group, false )  . '/?new';
     1053                       
     1054                        if ( (int)$total_items > 1 ) {
     1055                                return apply_filters( 'bp_groups_multiple_member_promoted_to_admin_notification', '<a href="' . $bp['loggedin_domain'] . $bp['groups']['slug'] . '" title="' . __( 'Groups', 'buddypress' ) . '">' . sprintf( __('You were promoted to an admin in %d groups'), (int)$total_items ) . '</a>', $total_items );           
     1056                        } else {
     1057                                return apply_filters( 'bp_groups_single_member_promoted_to_admin_notification', '<a href="' . $group_link . '">' . sprintf( __('You were promoted to an admin in the group %s'), $group->name ) . '</a>', $group_link, $group->name );
     1058                        }       
     1059                break;
     1060               
     1061                case 'member_promoted_to_mod':
     1062                        $group_id = $item_id;
     1063       
     1064                        $group = new BP_Groups_Group( $group_id, false, false );
     1065                        $group_link = bp_group_permalink( $group, false )  . '/?new';
     1066                       
     1067                        if ( (int)$total_items > 1 ) {
     1068                                return apply_filters( 'bp_groups_multiple_member_promoted_to_mod_notification', '<a href="' . $bp['loggedin_domain'] . $bp['groups']['slug'] . '" title="' . __( 'Groups', 'buddypress' ) . '">' . sprintf( __('You were promoted to a mod in %d groups'), (int)$total_items ) . '</a>', $total_items );               
     1069                        } else {
     1070                                return apply_filters( 'bp_groups_single_member_promoted_to_mod_notification', '<a href="' . $group_link . '">' . sprintf( __('You were promoted to a mod in the group %s'), $group->name ) . '</a>', $group_link, $group->name );
     1071                        }       
     1072                break;
     1073               
     1074                case 'group_invite':
     1075                        $group_id = $item_id;
     1076
     1077                        $group = new BP_Groups_Group( $group_id, false, false );
     1078                        $user_url = bp_core_get_userurl( $user_id );
     1079                       
     1080                        if ( (int)$total_items > 1 ) {
     1081                                return apply_filters( 'bp_groups_multiple_group_invite_notification', '<a href="' . $bp['loggedin_domain'] . $bp['groups']['slug'] . '/invites" title="' . __( 'Group Invites', 'buddypress' ) . '">' . sprintf( __('You have %d new group invitations'), (int)$total_items ) . '</a>', $total_items );         
     1082                        } else {
     1083                                return apply_filters( 'bp_groups_single_group_invite_notification', '<a href="' . $bp['loggedin_domain'] . $bp['groups']['slug'] . '/invites" title="' . __( 'Group Invites', 'buddypress' ) . '">' . sprintf( __('You have an invitation to the group: %s'), $group->name ) . '</a>', $group->name );
     1084                        }       
     1085                break;
     1086        }
     1087
     1088        do_action( 'groups_format_notifications', $action, $item_id, $secondary_item_id, $total_items );
     1089       
     1090        return false;
     1091}
     1092
     1093
     1094/**************************************************************************
     1095 groups_update_last_activity()
     1096 
     1097 Sets groupmeta for the group with the last activity date for the group based
     1098 on specific group activities.
     1099 **************************************************************************/
     1100
     1101function groups_update_last_activity( $group_id ) {
     1102        groups_update_groupmeta( $group_id, 'last_activity', time() );
     1103}
     1104add_action( 'groups_deleted_wire_post', 'groups_update_last_activity' );
     1105add_action( 'groups_new_wire_post', 'groups_update_last_activity' );
     1106add_action( 'groups_joined_group', 'groups_update_last_activity' );
     1107add_action( 'groups_leave_group', 'groups_update_last_activity' );
     1108add_action( 'groups_created_group', 'groups_update_last_activity' );
     1109add_action( 'bp_groups_new_forum_topic_post', 'groups_update_last_activity' );
     1110add_action( 'bp_groups_new_forum_topic_post', 'groups_update_last_activity' );
     1111
     1112
     1113
     1114/**************************************************************************
     1115 groups_get_user_groups()
     1116 
     1117 Fetch the groups the current user is a member of.
     1118 **************************************************************************/
     1119
     1120function groups_get_user_groups( $pag_num, $pag_page ) {
     1121        global $bp;
     1122       
     1123        $groups = BP_Groups_Member::get_group_ids( $bp['current_userid'], $pag_num, $pag_page );
     1124
     1125        return array( 'groups' => $groups['ids'], 'total' => $groups['total'] );
     1126}
     1127
     1128function groups_get_recently_joined_for_user( $user_id, $pag_num = false, $pag_page = false ) {
     1129        return BP_Groups_Member::get_recently_joined( $user_id, $pag_num, $pag_page );
     1130}
     1131
     1132function groups_get_most_popular_for_user( $user_id, $pag_num = false, $pag_page = false ) {
     1133        return BP_Groups_Member::get_most_popular( $user_id, $pag_num, $pag_page );     
     1134}
     1135
     1136function groups_get_recently_active_for_user( $user_id, $pag_num = false, $pag_page = false ) {
     1137        return BP_Groups_Member::get_recently_active( $user_id, $pag_num, $pag_page );
     1138}
     1139
     1140function groups_get_alphabetically_for_user( $user_id, $pag_num = false, $pag_page = false ) {
     1141        return BP_Groups_Member::get_alphabetically( $user_id, $pag_num, $pag_page );   
     1142}
     1143
     1144function groups_get_user_is_admin_of( $user_id, $pag_num = false, $pag_page = false ) {
     1145        return BP_Groups_Member::get_is_admin_of( $user_id, $pag_num, $pag_page );     
     1146}
     1147
     1148function groups_get_user_is_mod_of( $user_id, $pag_num = false, $pag_page = false ) {
     1149        return BP_Groups_Member::get_is_mod_of( $user_id, $pag_num, $pag_page );       
     1150}
     1151
     1152function groups_total_groups_for_user( $user_id ) {
     1153        return BP_Groups_Member::total_group_count( $user_id );
     1154}
     1155
     1156/**************************************************************************
     1157 groups_avatar_upload()
     1158 
     1159 Handle uploading of a group avatar
     1160**************************************************************************/
     1161
     1162function groups_avatar_upload( $file ) {
     1163        // validate the group avatar upload if there is one.
     1164        $avatar_error = false;
     1165
     1166        // Set friendly error feedback.
     1167        $uploadErrors = array(
     1168                0 => __("There is no error, the file uploaded with success", 'buddypress'),
     1169                1 => __("The uploaded file exceeds the upload_max_filesize directive in php.ini", 'buddypress'),
     1170                2 => __("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", 'buddypress'),
     1171                3 => __("The uploaded file was only partially uploaded", 'buddypress'),
     1172                4 => __("No file was uploaded", 'buddypress'),
     1173                6 => __("Missing a temporary folder", 'buddypress')
     1174        );
     1175
     1176        if ( !bp_core_check_avatar_upload($file) ) {
     1177                $avatar_error = true;
     1178                $avatar_error_msg = __('Your group avatar upload failed, please try again. Error was: ' . $uploadErrors[$file['file']['error']] , 'buddypress');
     1179        }
     1180
     1181        else if ( !bp_core_check_avatar_size($file) ) {
     1182                $avatar_error = true;
     1183                $avatar_size = size_format(1024 * CORE_MAX_FILE_SIZE);
     1184                $avatar_error_msg = __('The file you uploaded is too big. Please upload a file under', 'buddypress') . size_format(CORE_MAX_FILE_SIZE);
     1185        }
     1186       
     1187        else if ( !bp_core_check_avatar_type($file) ) {
     1188                $avatar_error = true;
     1189                $avatar_error_msg = __('Please upload only JPG, GIF or PNG photos.', 'buddypress');             
     1190        }
     1191
     1192        // "Handle" upload into temporary location
     1193        else if ( !$original = bp_core_handle_avatar_upload($file) ) {
     1194                $avatar_error = true;
     1195                $avatar_error_msg = __('Upload Failed! Please check the permissions on the group avatar upload directory.', 'buddypress');                                             
     1196        }
     1197       
     1198        if ( !$canvas = bp_core_resize_avatar($original) )
     1199                $canvas = $original;
     1200       
     1201        if ( $avatar_error ) { ?>
     1202                <div id="message" class="error">
     1203                        <p><?php echo $avatar_error_msg ?></p>
     1204                </div>
     1205                <?php
     1206                bp_core_render_avatar_upload_form( '', true );
     1207        } else {
     1208                bp_core_render_avatar_cropper( $original, $canvas, null, null, false, $bp['loggedin_domain'] );
     1209        }
     1210}
     1211
     1212
     1213/**************************************************************************
     1214 groups_save_avatar()
     1215 
     1216 Save the avatar location urls into the DB for the group.
     1217**************************************************************************/
     1218
     1219function groups_get_avatar_hrefs( $avatars ) {
     1220        global $bp;
     1221       
     1222        $src = $bp['root_domain'] . '/';
     1223
     1224        $thumb_href = str_replace( ABSPATH, $src, stripslashes($avatars['v1_out']) );
     1225        $full_href = str_replace( ABSPATH, $src, stripslashes($avatars['v2_out']) );
     1226       
     1227        return array( 'thumb_href' => $thumb_href, 'full_href' => $full_href );
     1228}
     1229
     1230
     1231function groups_search_groups( $search_terms, $pag_num_per_page = 5, $pag_page = 1, $sort_by = false, $order = false ) {
     1232        return BP_Groups_Group::search_groups( $search_terms, $pag_num_per_page, $pag_page, $sort_by, $order );
     1233}
     1234
     1235function groups_filter_user_groups( $filter, $pag_num_per_page = 5, $pag_page = 1 ) {
     1236        return BP_Groups_Group::filter_user_groups( $filter, $pag_num_per_page, $pag_page );
     1237}
     1238
     1239/**************************************************************************
     1240 groups_manage_group()
     1241 
     1242 Manage the creation of a group via the step by step wizard.
     1243**************************************************************************/
     1244
     1245function groups_manage_group( $step, $group_id ) {
     1246        global $bp, $create_group_step, $group_obj, $bbpress_live;
     1247
     1248        if ( is_numeric( $step ) && ( $step == '1' || $step == '2' || $step == '3' || $step == '4' ) ) {
     1249               
     1250                if ( !$group_obj )
     1251                        $group_obj = new BP_Groups_Group( $group_id );         
     1252                       
     1253                switch ( $step ) {
     1254                        case '1':
     1255                                if ( $_POST['group-name'] != '' && $_POST['group-desc'] != '' && $_POST['group-news'] != '' ) {
     1256                                        $group_obj->creator_id = $bp['loggedin_userid'];
     1257                                        $group_obj->name = stripslashes($_POST['group-name']);
     1258                                        $group_obj->description = stripslashes($_POST['group-desc']);
     1259                                        $group_obj->news = stripslashes($_POST['group-news']);
     1260                                       
     1261                                        $slug = groups_check_slug( sanitize_title($_POST['group-name']) );
     1262
     1263                                        $group_obj->slug = $slug;
     1264                                        $group_obj->status = 'public';
     1265                                        $group_obj->is_invitation_only = 0;
     1266                                        $group_obj->enable_wire = 1;
     1267                                        $group_obj->enable_forum = 1;
     1268                                        $group_obj->enable_photos = 1;
     1269                                        $group_obj->photos_admin_only = 0;
     1270                                        $group_obj->date_created = time();
     1271                                       
     1272                                        if ( !$group_obj->save() )
     1273                                                return false;
     1274
     1275                                        // Save the creator as the group administrator
     1276                                        $admin = new BP_Groups_Member( $bp['loggedin_userid'], $group_obj->id );
     1277                                        $admin->is_admin = 1;
     1278                                        $admin->user_title = __('Group Admin', 'buddypress');
     1279                                        $admin->date_modified = time();
     1280                                        $admin->inviter_id = 0;
     1281                                        $admin->is_confirmed = 1;
     1282                                       
     1283                                        if ( !$admin->save() )
     1284                                                return false;
     1285                                       
     1286                                        do_action( 'bp_groups_step1_save' );
     1287                                       
     1288                                        /* Set groupmeta */
     1289                                        groups_update_groupmeta( $group_obj->id, 'total_member_count', 1 );
     1290                                        groups_update_groupmeta( $group_obj->id, 'last_activity', time() );
     1291                                        groups_update_groupmeta( $group_obj->id, 'theme', 'buddypress' );
     1292                                        groups_update_groupmeta( $group_obj->id, 'stylesheet', 'buddypress' );
     1293                               
     1294                                        return $group_obj->id;
     1295                                }
     1296                               
     1297                                return false;
     1298                        break;
     1299                       
     1300                        case '2':
     1301                                $group_obj->status = 'public';
     1302                                $group_obj->is_invitation_only = 0;
     1303                                $group_obj->enable_wire = 1;
     1304                                $group_obj->enable_forum = 1;
     1305                                $group_obj->enable_photos = 1;
     1306                                $group_obj->photos_admin_only = 0;
     1307                               
     1308                                if ( !isset($_POST['group-show-wire']) )
     1309                                        $group_obj->enable_wire = 0;
     1310                               
     1311                                if ( !isset($_POST['group-show-forum']) ) {
     1312                                        $group_obj->enable_forum = 0;
     1313                                } else {
     1314                                        /* Create the forum if enable_forum = 1 */
     1315                                        if ( function_exists( 'bp_forums_setup' ) && groups_get_groupmeta( $group_obj->id, 'forum_id' ) == '' ) {
     1316                                                groups_new_group_forum();
     1317                                        }
     1318                                }
     1319                               
     1320                                if ( !isset($_POST['group-show-photos']) )
     1321                                        $group_obj->enable_photos = 0;                         
     1322                               
     1323                                if ( $_POST['group-photos-status'] != 'all' )
     1324                                        $group_obj->photos_admin_only = 1;
     1325                               
     1326                                if ( $_POST['group-status'] == 'private' ) {
     1327                                        $group_obj->status = 'private';
     1328                                } else if ( $_POST['group-status'] == 'hidden' ) {
     1329                                        $group_obj->status = 'hidden';
     1330                                }
     1331                               
     1332                                if ( !$group_obj->save() )
     1333                                        return false;
     1334                                       
     1335                                do_action( 'bp_groups_step2_save' );
     1336                                       
     1337                                return $group_obj->id;
     1338                        break;
     1339                       
     1340                        case '3':
     1341                                                               
     1342                                // Image already cropped and uploaded, lets store a reference in the DB.
     1343                                if ( !wp_verify_nonce($_POST['nonce'], 'slick_avatars') || !$result = bp_core_avatar_cropstore( $_POST['orig'], $_POST['canvas'], $_POST['v1_x1'], $_POST['v1_y1'], $_POST['v1_w'], $_POST['v1_h'], $_POST['v2_x1'], $_POST['v2_y1'], $_POST['v2_w'], $_POST['v2_h'], false, 'groupavatar', $group_obj->id ) )
     1344                                        return false;
     1345
     1346                                // Success on group avatar cropping, now save the results.
     1347                                $avatar_hrefs = groups_get_avatar_hrefs($result);
     1348                               
     1349                                $group_obj->avatar_thumb = stripslashes($avatar_hrefs['thumb_href']);
     1350                                $group_obj->avatar_full = stripslashes($avatar_hrefs['full_href']);
     1351                               
     1352                                if ( !$group_obj->save() )
     1353                                        return false;
     1354                               
     1355                                do_action( 'bp_groups_step3_save' );
     1356                               
     1357                                return $group_obj->id;
     1358                        break;
     1359                       
     1360                        case '4':
     1361                                $send_invites = groups_send_invites($group_obj);
     1362
     1363                                /* Record in activity streams */
     1364                                groups_record_activity( array( 'item_id' => $group_obj->id, 'component_name' => 'groups', 'component_action' => 'created_group', 'is_private' => 0 ) );
     1365                               
     1366                                do_action( 'bp_groups_created_group', $group_obj->id );
     1367                               
     1368                                return $group_obj->id;
     1369                        break;
     1370                }
     1371        }
     1372       
     1373        return false;
     1374}
     1375
     1376function groups_check_slug( $slug ) {
     1377        global $bp;
     1378       
     1379        if ( in_array( $slug, $bp['groups']['forbidden_names'] ) ) {
     1380                $slug = $slug . '-' . rand();
     1381        }
     1382       
     1383        if ( BP_Groups_Group::check_slug( $slug ) ) {
     1384                do {
     1385                        $slug = $slug . '-' . rand();
     1386                }
     1387                while ( BP_Groups_Group::check_slug( $slug ) );
     1388        }
     1389       
     1390        return $slug;
     1391}
     1392
     1393function groups_get_slug( $group_id ) {
     1394        $group = new BP_Groups_Group( $group_id, false, false );
     1395        return $group->slug;
     1396}
     1397
     1398function groups_is_user_admin( $user_id, $group_id ) {
     1399        return BP_Groups_Member::check_is_admin( $user_id, $group_id );
     1400}
     1401
     1402function groups_is_user_mod( $user_id, $group_id ) {
     1403        return BP_Groups_Member::check_is_mod( $user_id, $group_id );
     1404}
     1405
     1406function groups_is_user_member( $user_id, $group_id ) {
     1407        return BP_Groups_Member::check_is_member( $user_id, $group_id );
     1408}
     1409
     1410function groups_is_user_banned( $user_id, $group_id ) {
     1411        return BP_Groups_Member::check_is_banned( $user_id, $group_id );
     1412}
     1413
     1414function groups_new_group_forum() {
     1415        global $group_obj;
     1416       
     1417        $forum = bp_forums_new_forum( $group_obj->name . ' - Forum ', $group_obj->description );
     1418        groups_update_groupmeta( $group_obj->id, 'forum_id', $forum['forum_id'] );
     1419       
     1420        do_action( 'groups_new_group_forum', $forum, $group_obj->id );
     1421}
     1422
     1423function groups_new_group_forum_post( $post_text, $topic_id ) {
     1424        global $group_obj;
     1425       
     1426        if ( $forum_post = bp_forums_new_post( $post_text, $topic_id ) ) {
     1427                bp_core_add_message( __( 'Reply posted successfully!', 'buddypress') );
     1428
     1429                /* Record in activity streams */
     1430                groups_record_activity( array( 'item_id' => $group_obj->id, 'component_name' => 'groups', 'component_action' => 'new_forum_post', 'is_private' => 0, 'secondary_item_id' => $forum_post['post_id'] ) );
     1431               
     1432                do_action( 'bp_groups_new_forum_topic_post', $group_obj->id, $forum_post );
     1433               
     1434                return $forum_post;
     1435        }
     1436       
     1437        bp_core_add_message( __( 'There was an error posting that reply.', 'buddypress'), 'error' );                                   
     1438        return false;
     1439}
     1440
     1441function groups_new_group_forum_topic( $topic_title, $topic_text, $topic_tags, $forum_id ) {
     1442        global $group_obj;
     1443       
     1444        if ( $topic = bp_forums_new_topic( $topic_title, $topic_text, $topic_tags, $forum_id ) ) {
     1445                bp_core_add_message( __( 'Topic posted successfully!', 'buddypress') );
     1446
     1447                /* Record in activity streams */
     1448                groups_record_activity( array( 'item_id' => $group_obj->id, 'component_name' => 'groups', 'component_action' => 'new_forum_topic', 'is_private' => 0, 'secondary_item_id' => $topic['topic_id'] ) );
     1449               
     1450                do_action( 'bp_groups_new_forum_topic', $group_obj->id, $topic );
     1451               
     1452                return $topic;
     1453        }
     1454       
     1455        bp_core_add_message( __( 'There was an error posting that topic.', 'buddypress'), 'error' );                                   
     1456        return false;
     1457}
     1458
     1459function groups_invite_user( $user_id, $group_id ) {
     1460        global $bp;
     1461       
     1462        if ( groups_is_user_member( $user_id, $group_id ) )
     1463                return false;
     1464       
     1465        $invite = new BP_Groups_Member;
     1466        $invite->group_id = $group_id;
     1467        $invite->user_id = $user_id;
     1468        $invite->date_modified = time();
     1469        $invite->inviter_id = $bp['loggedin_userid'];
     1470        $invite->is_confirmed = 0;
     1471       
     1472        if ( !$invite->save() )
     1473                return false;
     1474       
     1475        do_action( 'groups_invite_user', $group_id, $user_id );
     1476               
     1477        return true;
     1478}
     1479
     1480function groups_uninvite_user( $user_id, $group_id ) {
     1481        global $bp;
     1482
     1483        if ( !BP_Groups_Member::delete( $user_id, $group_id ) )
     1484                return false;
     1485
     1486        do_action( 'groups_uninvite_user', $group_id, $user_id );
     1487
     1488        return true;
     1489}
     1490
     1491function groups_accept_invite( $user_id, $group_id ) {
     1492        global $group_obj;
     1493       
     1494        if ( groups_is_user_member( $user_id, $group_id ) )
     1495                return false;
     1496       
     1497        $group_obj = new BP_Groups_Group( $group_id );
     1498       
     1499        $member = new BP_Groups_Member( $user_id, $group_id );
     1500        $member->accept_invite();
     1501
     1502        if ( !$member->save() )
     1503                return false;
     1504       
     1505        do_action( 'groups_accept_invite', $user_id, $group_id );
     1506        return true;
     1507}
     1508
     1509function groups_reject_invite( $user_id, $group_id ) {
     1510        if ( !BP_Groups_Member::delete( $user_id, $group_id ) )
     1511                return false;
     1512       
     1513        do_action( 'groups_reject_invite', $user_id, $group_id );
     1514        return true;
     1515}
     1516
     1517
     1518function groups_get_invites_for_group( $user_id, $group_id ) {
     1519        return BP_Groups_Group::get_invites( $user_id, $group_id );
     1520}
     1521
     1522function groups_check_user_has_invite( $user_id, $group_id ) {
     1523        return BP_Groups_Member::check_has_invite( $user_id, $group_id );
     1524}
     1525
     1526function groups_get_invites_for_user( $user_id = false ) {
     1527        global $bp;
     1528       
     1529        if ( !$user_id )
     1530                $user_id = $bp['loggedin_userid'];
     1531       
     1532        return BP_Groups_Member::get_invites( $user_id );
     1533}
     1534
     1535function groups_send_invites( $group_obj ) {
     1536        global $bp;
     1537       
     1538        // Send friend invites.
     1539        $invited_users = groups_get_invites_for_group( $bp['loggedin_userid'], $group_obj->id );
     1540
     1541        for ( $i = 0; $i < count( $invited_users ); $i++ ) {
     1542                $member = new BP_Groups_Member( $invited_users[$i], $group_obj->id );
     1543                $member->invite_sent = 1;
     1544                $member->save();
     1545        }
     1546       
     1547        groups_notification_group_invites( $group_obj->id, $invited_users, $bp['loggedin_userid'] );
     1548       
     1549        do_action( 'groups_send_invites', $group_obj->id, $invited_users );
     1550}
     1551
     1552function groups_check_group_exists( $group_id ) {
     1553        return BP_Groups_Group::group_exists( $group_id );
     1554}
     1555
     1556function groups_leave_group( $group_id, $user_id = false ) {
     1557        global $bp;
     1558       
     1559        if ( !$user_id )
     1560                $user_id = $bp['loggedin_userid'];
     1561               
     1562        // This is exactly the same as deleting and invite, just is_confirmed = 1 NOT 0.
     1563        if ( !groups_uninvite_user( $user_id, $group_id ) )
     1564                return false;
     1565
     1566        do_action( 'groups_leave_group', $group_id, $bp['loggedin_userid'] );
     1567
     1568        /* Modify group member count */
     1569        groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') - 1 );
     1570       
     1571        return true;
     1572}
     1573
     1574function groups_join_group( $group_id, $user_id = false ) {
     1575        global $bp;
     1576       
     1577        if ( !$user_id )
     1578                $user_id = $bp['loggedin_userid'];
     1579       
     1580        $new_member = new BP_Groups_Member;
     1581        $new_member->group_id = $group_id;
     1582        $new_member->user_id = $user_id;
     1583        $new_member->inviter_id = 0;
     1584        $new_member->is_admin = 0;
     1585        $new_member->user_title = '';
     1586        $new_member->date_modified = time();
     1587        $new_member->is_confirmed = 1;
     1588       
     1589        if ( !$new_member->save() )
     1590                return false;
     1591
     1592        /* Record this in activity streams */
     1593        groups_record_activity( array( 'item_id' => $new_member->group_id, 'component_name' => 'groups', 'component_action' => 'joined_group', 'is_private' => 0 ) );
     1594       
     1595        /* Modify group member count */
     1596        groups_update_groupmeta( $group_id, 'total_member_count', (int) groups_get_groupmeta( $group_id, 'total_member_count') + 1 );
     1597
     1598        do_action( 'groups_joined_group', $group_id, $bp['loggedin_userid'] );
     1599
     1600        return true;
     1601}
     1602
     1603function groups_get_group_admins( $group_id ) {
     1604        return BP_Groups_Member::get_group_administrator_ids( $group_id );
     1605}
     1606
     1607function groups_get_group_mods( $group_id ) {
     1608        return BP_Groups_Member::get_group_moderator_ids( $group_id );
     1609}
     1610
     1611function groups_get_group_users( $group_id, $limit = false, $page = false ) {
     1612        return BP_Groups_Member::get_all_for_group( $group_id, $limit, $page );
     1613}
     1614
     1615function groups_is_group_admin( $user_id, $group_id ) {
     1616        return BP_Groups_Member::check_is_admin( $user_id, $group_id );
     1617}
     1618
     1619function groups_is_group_mod( $user_id, $group_id ) {
     1620        return BP_Groups_Member::check_is_mod( $user_id, $group_id );
     1621}
     1622
     1623function groups_new_wire_post( $group_id, $content ) {
     1624        if ( $wire_post_id = bp_wire_new_post( $group_id, $content ) ) {
     1625                /* Record in activity streams */
     1626                groups_record_activity( array( 'item_id' => $wire_post_id, 'group_id' => $group_id, 'component_name' => 'groups', 'component_action' => 'new_wire_post', 'is_private' => 0 ) );
     1627
     1628                do_action( 'groups_new_wire_post', $group_id, $wire_post_id );
     1629               
     1630                return true;
     1631        }
     1632       
     1633        return false;
     1634}
     1635
     1636function groups_delete_wire_post( $wire_post_id, $table_name ) {
     1637        global $bp;
     1638       
     1639        if ( bp_wire_delete_post( $wire_post_id, $table_name ) ) {
     1640               
     1641                // Remove this activity stream item
     1642                groups_delete_activity( array( 'item_id' => $wire_post_id, 'component_name' => 'groups', 'component_action' => 'new_wire_post', 'user_id' => $bp['current_userid'], 'group_id' => $group_id ) );
     1643               
     1644                do_action( 'groups_deleted_wire_post', $wire_post_id );
     1645                return true;
     1646        }
     1647       
     1648        return false;
     1649}
     1650
     1651function groups_edit_base_group_details( $group_id, $group_name, $group_desc, $group_news ) {
     1652        global $bp;
     1653       
     1654        if ( $group_name == '' || $group_desc == '' || $group_news == '' )
     1655                return false;
     1656               
     1657        $group = new BP_Groups_Group( $group_id, false, false );
     1658        $group->name = $group_name;
     1659        $group->description = $group_desc;
     1660        $group->news = $group_news;
     1661
     1662        if ( !$group->save() )
     1663                return false;
     1664
     1665        if ( $notify_members )
     1666                groups_notification_group_updated( $group->id );
     1667
     1668        do_action( 'bp_groups_details_updated', $group->id );
     1669       
     1670        return true;
     1671}
     1672
     1673function groups_edit_group_settings( $group_id, $enable_wire, $enable_forum, $enable_photos, $photos_admin_only, $status ) {
     1674        global $bp;
     1675       
     1676        $group = new BP_Groups_Group( $group_id, false, false );
     1677        $group->enable_wire = $enable_wire;
     1678        $group->enable_forum = $enable_forum;
     1679        $group->enable_photos = $enable_photos;
     1680        $group->photos_admin_only = $photos_admin_only;
     1681        $group->status = $status;
     1682       
     1683        if ( !$group->save() )
     1684                return false;
     1685       
     1686        /* If forums have been enabled, and a forum does not yet exist, we need to create one. */
     1687        if ( $group->enable_forum ) {
     1688                if ( function_exists( 'bp_forums_setup' ) && groups_get_groupmeta( $group->id, 'forum_id' ) == '' ) {
     1689                        $forum = bp_forums_new_forum( $group->name . ' - Forum ', $group->description );
     1690                        groups_update_groupmeta( $group->id, 'forum_id', $forum['forum_id'] );
     1691                }
     1692        }
     1693       
     1694        do_action( 'bp_groups_settings_updated', $group->id );
     1695       
     1696        return true;
     1697}
     1698
     1699function groups_promote_member( $user_id, $group_id ) {
     1700        global $bp;
     1701       
     1702        if ( !$bp['is_item_admin'] )
     1703                return false;
     1704               
     1705        $member = new BP_Groups_Member( $user_id, $group_id );
     1706
     1707        add_action( 'groups_premote_member', $user_id, $group_id );
     1708       
     1709        return $member->promote();
     1710}
     1711
     1712function groups_demote_member( $user_id, $group_id ) {
     1713        global $bp;
     1714       
     1715        if ( !$bp['is_item_admin'] )
     1716                return false;
     1717               
     1718        $member = new BP_Groups_Member( $user_id, $group_id );
     1719       
     1720        add_action( 'groups_demote_member', $user_id, $group_id );
     1721
     1722        return $member->demote();
     1723}
     1724
     1725function groups_ban_member( $user_id, $group_id ) {
     1726        global $bp;
     1727       
     1728        if ( !$bp['is_item_admin'] )
     1729                return false;
     1730               
     1731        $member = new BP_Groups_Member( $user_id, $group_id );
     1732
     1733        add_action( 'groups_ban_member', $user_id, $group_id );
     1734       
     1735        return $member->ban();
     1736}
     1737
     1738function groups_unban_member( $user_id, $group_id ) {
     1739        global $bp;
     1740       
     1741        if ( !$bp['is_item_admin'] )
     1742                return false;
     1743               
     1744        $member = new BP_Groups_Member( $user_id, $group_id );
     1745       
     1746        add_action( 'bp_groups_unban_member', $user_id, $group_id );
     1747       
     1748        return $member->unban();
     1749}
     1750
     1751function groups_send_membership_request( $requesting_user_id, $group_id ) {
     1752        global $bp;
     1753
     1754        $requesting_user = new BP_Groups_Member;
     1755        $requesting_user->group_id = $group_id;
     1756        $requesting_user->user_id = $requesting_user_id;
     1757        $requesting_user->inviter_id = 0;
     1758        $requesting_user->is_admin = 0;
     1759        $requesting_user->user_title = '';
     1760        $requesting_user->date_modified = time();
     1761        $requesting_user->is_confirmed = 0;
     1762        $requesting_user->comments = $_POST['group-request-membership-comments'];
     1763       
     1764        if ( $requesting_user->save() ) {
     1765                $admins = groups_get_group_admins( $group_id );
     1766               
     1767                for ( $i = 0; $i < count( $admins ); $i++ ) {
     1768                        // Saved okay, now send the email notification
     1769                        groups_notification_new_membership_request( $requesting_user_id, $admins[$i]->user_id, $group_id, $requesting_user->id );
     1770                }
     1771               
     1772                do_action( 'bp_groups_group_membership_requested', $requesting_user_id, $admins, $group_id, $requesting_user->id );
     1773       
     1774                return true;
     1775        }
     1776       
     1777        return false;
     1778}
     1779
     1780function groups_accept_membership_request( $membership_id ) {
     1781        $membership = new BP_Groups_Member( false, false, $membership_id );
     1782        $membership->accept_request();
     1783       
     1784        if ( !$membership->save() )
     1785                return false;
     1786               
     1787        /* Modify group member count */
     1788        groups_update_groupmeta( $membership->group_id, 'total_member_count', (int) groups_get_groupmeta( $membership->group_id, 'total_member_count') + 1 );
     1789       
     1790        /* Record this in activity streams */
     1791        groups_record_activity( array( 'item_id' => $membership->group_id, 'component_name' => 'groups', 'component_action' => 'joined_group', 'is_private' => 0 ) );
     1792
     1793        /* Send a notification to the user. */
     1794        groups_notification_membership_request_completed( $membership->user_id, $membership->group_id, true );
     1795       
     1796        do_action( 'bp_groups_membership_accepted', $membership->user_id, $membership->group_id );
     1797       
     1798        return true;
     1799}
     1800
     1801function groups_reject_membership_request( $membership_id ) {
     1802        $membership = new BP_Groups_Member( false, false, $membership_id );
     1803       
     1804        if ( !BP_Groups_Member::delete( $membership->user_id, $membership->group_id ) )
     1805                return false;
     1806       
     1807        // Send a notification to the user.
     1808        groups_notification_membership_request_completed( $membership->user_id, $membership->group_id, false );
     1809       
     1810        do_action( 'bp_groups_membership_rejected', $membership->user_id, $membership->group_id );
     1811       
     1812        return true;
     1813}
     1814
     1815function groups_redirect_to_random_group() {
     1816        global $bp, $wpdb;
     1817       
     1818        if ( $bp['current_component'] == $bp['groups']['slug'] && isset( $_GET['random'] ) ) {
     1819                $group = groups_get_random_group();
     1820
     1821                bp_core_redirect( $bp['root_domain'] . '/' . $bp['groups']['slug'] . '/' . $group['groups'][0]->slug );
     1822        }
     1823}
     1824add_action( 'wp', 'groups_redirect_to_random_group', 6 );
     1825
     1826function groups_delete_group( $group_id ) {
     1827        global $bp;
     1828       
     1829        // Check the user is the group admin.
     1830        if ( !$bp['is_item_admin'] )
     1831                return false;
     1832       
     1833        // Get the group object
     1834        $group = new BP_Groups_Group( $group_id );
     1835       
     1836        if ( !$group->delete() )
     1837                return false;
     1838       
     1839        // Remove the activity stream item
     1840        groups_delete_activity( array( 'item_id' => $group_id, 'component_name' => 'groups', 'component_action' => 'created_group', 'user_id' => $bp['loggedin_userid'] ) );
     1841       
     1842        add_action( 'bp_groups_delete_group', $group_id );
     1843       
     1844        return true;
     1845}
     1846
     1847function groups_check_for_membership_request( $user_id, $group_id ) {
     1848        return BP_Groups_Member::check_for_membership_request( $user_id, $group_id );
     1849}
     1850
     1851function groups_get_newest( $limit = null, $page = null ) {
     1852        return BP_Groups_Group::get_newest( $limit, $page );
     1853}
     1854
     1855function groups_get_active( $limit = null, $page = null ) {
     1856        return BP_Groups_Group::get_active( $limit, $page );
     1857}
     1858
     1859function groups_get_popular( $limit = null, $page = null ) {
     1860        return BP_Groups_Group::get_popular( $limit, $page );
     1861}
     1862
     1863function groups_get_all( $limit = null, $page = null ) {
     1864        return BP_Groups_Group::get_all( $limit, $page );
     1865}
     1866
     1867function groups_get_random_group() {
     1868        return BP_Groups_Group::get_random();
     1869}
     1870
     1871//
     1872// Group meta functions
     1873//
     1874
     1875function groups_delete_groupmeta( $group_id, $meta_key = false, $meta_value = false ) {
     1876        global $wpdb, $bp;
     1877       
     1878        if ( !is_numeric( $group_id ) )
     1879                return false;
     1880               
     1881        $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
     1882
     1883        if ( is_array($meta_value) || is_object($meta_value) )
     1884                $meta_value = serialize($meta_value);
     1885               
     1886        $meta_value = trim( $meta_value );
     1887
     1888        if ( !$meta_key ) {
     1889                $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['groups']['table_name_groupmeta'] . " WHERE group_id = %d", $group_id ) );           
     1890        } else if ( !$meta_value ) {
     1891                $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 ) );
     1892        } else {
     1893                $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['groups']['table_name_groupmeta'] . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key ) );
     1894        }
     1895       
     1896        // TODO need to look into using this.
     1897        // wp_cache_delete($group_id, 'groups');
     1898
     1899        return true;
     1900}
     1901
     1902function groups_get_groupmeta( $group_id, $meta_key = '') {
     1903        global $wpdb, $bp;
     1904       
     1905        $group_id = (int) $group_id;
     1906
     1907        if ( !$group_id )
     1908                return false;
     1909
     1910        if ( !empty($meta_key) ) {
     1911                $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
     1912               
     1913                // TODO need to look into using this.
     1914                //$user = wp_cache_get($user_id, 'users');
     1915               
     1916                // Check the cached user object
     1917                //if ( false !== $user && isset($user->$meta_key) )
     1918                //      $metas = array($user->$meta_key);
     1919                //else
     1920                $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) );
     1921        } else {
     1922                $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp['groups']['table_name_groupmeta'] . " WHERE group_id = %d", $group_id) );
     1923        }
     1924
     1925        if ( empty($metas) ) {
     1926                if ( empty($meta_key) )
     1927                        return array();
     1928                else
     1929                        return '';
     1930        }
     1931
     1932        $metas = array_map('maybe_unserialize', $metas);
     1933
     1934        if ( count($metas) == 1 )
     1935                return $metas[0];
     1936        else
     1937                return $metas;
     1938}
     1939
     1940function groups_update_groupmeta( $group_id, $meta_key, $meta_value ) {
     1941        global $wpdb, $bp;
     1942       
     1943        if ( !is_numeric( $group_id ) )
     1944                return false;
     1945       
     1946        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
     1947        $meta_value = (string)$meta_value;
     1948
     1949        if ( is_string($meta_value) )
     1950                $meta_value = stripslashes($wpdb->escape($meta_value));
     1951       
     1952        $meta_value = maybe_serialize($meta_value);
     1953       
     1954        if (empty($meta_value)) {
     1955                return groups_delete_groupmeta( $group_id, $meta_key );
     1956        }
     1957
     1958        $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 ) );
     1959
     1960        if ( !$cur ) {
     1961                $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 ) );
     1962        } else if ( $cur->meta_value != $meta_value ) {
     1963                $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 ) );
     1964        } else {
     1965                return false;
     1966        }
     1967
     1968        // TODO need to look into using this.
     1969        // wp_cache_delete($user_id, 'users');
     1970
     1971        return true;
     1972}
     1973
     1974// The following two functions will force the active member theme for
     1975// groups pages, even though they are technically under the root "home" blog
     1976// from a WordPress point of view.
     1977
     1978function groups_force_buddypress_theme( $template ) {
     1979        global $current_component, $current_action;
     1980        global $is_member_page;
     1981       
     1982        $groups_bp = groups_setup_globals(true);
     1983       
     1984        if ( $current_component != $groups_bp['groups']['slug'] )
     1985                return $template;
     1986               
     1987        $member_theme = get_site_option('active-member-theme');
     1988       
     1989        if ( $member_theme == '' )
     1990                $member_theme = 'buddypress-member';
     1991       
     1992        // The theme filter does not recognize any globals, where as the stylesheet filter does.
     1993        // We have to set up the globals to use manually.
     1994        bp_core_set_uri_globals();
     1995
     1996        if ( $current_component == $groups_bp['groups']['slug'] )
     1997                $is_single_group = BP_Groups_Group::group_exists( $current_action, $groups_bp['groups']['table_name'] );
     1998       
     1999        if ( $is_single_group ) {
     2000                add_filter( 'theme_root', 'bp_core_set_member_theme_root' );
     2001                add_filter( 'theme_root_uri', 'bp_core_set_member_theme_root_uri' );
     2002
     2003                return $member_theme;
     2004        } else {
     2005                return $template;
     2006        }
     2007       
     2008        return $theme;
     2009}
     2010add_filter( 'template', 'groups_force_buddypress_theme', 1, 1 );
     2011
     2012function groups_force_buddypress_stylesheet( $stylesheet ) {
     2013        global $bp, $is_single_group, $is_member_page, $current_component;
     2014
     2015        if ( $current_component != $bp['groups']['slug'] )
     2016                return $stylesheet;
     2017
     2018        $member_theme = get_site_option('active-member-theme');
     2019       
     2020        if ( $member_theme == '' )
     2021                $member_theme = 'buddypress-member';
     2022       
     2023        if ( $is_single_group ) {
     2024                add_filter( 'theme_root', 'bp_core_set_member_theme_root' );
     2025                add_filter( 'theme_root_uri', 'bp_core_set_member_theme_root_uri' );
     2026               
     2027                return $member_theme;
     2028        } else {
     2029                return $stylesheet;     
     2030        }
     2031}
     2032add_filter( 'stylesheet', 'groups_force_buddypress_stylesheet', 1, 1 );
     2033
     2034function groups_remove_data( $user_id ) {
     2035        BP_Groups_Member::delete_all_for_user($user_id);
     2036       
     2037        do_action( 'groups_remove_data', $user_id );
     2038}
     2039add_action( 'wpmu_delete_user', 'groups_remove_data', 1 );
     2040add_action( 'delete_user', 'groups_remove_data', 1 );
     2041
     2042
    20432043?>
     2044 No newline at end of file