Skip to:
Content

BuddyPress.org

Ticket #265: bp-groups.php

File bp-groups.php, 72.7 KB (added by williamsba1, 18 years ago)

fix for ticket

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