/bp-groups/bp-groups-actions.php

  1. <?php 
  2. /** 
  3. * BuddyPress Groups Actions. 
  4. * 
  5. * Action functions are exactly the same as screen functions, however they do 
  6. * not have a template screen associated with them. Usually they will send the 
  7. * user back to the default screen after execution. 
  8. * 
  9. * @package BuddyPress 
  10. * @subpackage GroupsActions 
  11. * @since 1.5.0 
  12. */ 
  13.  
  14. // Exit if accessed directly. 
  15. defined( 'ABSPATH' ) || exit; 
  16.  
  17. /** 
  18. * Fire the 'bp_groups_register_group_types' action. 
  19. * 
  20. * @since 2.6.0 
  21. */ 
  22. function bp_groups_register_group_types() { 
  23. /** 
  24. * Fires when it's appropriate to register group types. 
  25. * 
  26. * @since 2.6.0 
  27. */ 
  28. do_action( 'bp_groups_register_group_types' ); 
  29. add_action( 'bp_register_taxonomies', 'bp_groups_register_group_types' ); 
  30.  
  31. /** 
  32. * Protect access to single groups. 
  33. * 
  34. * @since 2.1.0 
  35. */ 
  36. function bp_groups_group_access_protection() { 
  37. if ( ! bp_is_group() ) { 
  38. return; 
  39.  
  40. $current_group = groups_get_current_group(); 
  41. $user_has_access = $current_group->user_has_access; 
  42. $no_access_args = array(); 
  43.  
  44. if ( ! $user_has_access && 'hidden' !== $current_group->status ) { 
  45. // Always allow access to home and request-membership. 
  46. if ( bp_is_current_action( 'home' ) || bp_is_current_action( 'request-membership' ) ) { 
  47. $user_has_access = true; 
  48.  
  49. // User doesn't have access, so set up redirect args. 
  50. } elseif ( is_user_logged_in() ) { 
  51. $no_access_args = array( 
  52. 'message' => __( 'You do not have access to this group.', 'buddypress' ),  
  53. 'root' => bp_get_group_permalink( $current_group ) . 'home/',  
  54. 'redirect' => false 
  55. ); 
  56.  
  57. // Protect the admin tab from non-admins. 
  58. if ( bp_is_current_action( 'admin' ) && ! bp_is_item_admin() ) { 
  59. $user_has_access = false; 
  60. $no_access_args = array( 
  61. 'message' => __( 'You are not an admin of this group.', 'buddypress' ),  
  62. 'root' => bp_get_group_permalink( $current_group ),  
  63. 'redirect' => false 
  64. ); 
  65.  
  66. /** 
  67. * Allow plugins to filter whether the current user has access to this group content. 
  68. * 
  69. * Note that if a plugin sets $user_has_access to false, it may also 
  70. * want to change the $no_access_args, to avoid problems such as 
  71. * logged-in users being redirected to wp-login.php. 
  72. * 
  73. * @since 2.1.0 
  74. * 
  75. * @param bool $user_has_access True if the user has access to the 
  76. * content, otherwise false. 
  77. * @param array $no_access_args Arguments to be passed to bp_core_no_access() in case 
  78. * of no access. Note that this value is passed by reference,  
  79. * so it can be modified by the filter callback. 
  80. */ 
  81. $user_has_access = apply_filters_ref_array( 'bp_group_user_has_access', array( $user_has_access, &$no_access_args ) ); 
  82.  
  83. // If user has access, we return rather than redirect. 
  84. if ( $user_has_access ) { 
  85. return; 
  86.  
  87. // Hidden groups should return a 404 for non-members. 
  88. // Unset the current group so that you're not redirected 
  89. // to the default group tab. 
  90. if ( 'hidden' == $current_group->status ) { 
  91. buddypress()->groups->current_group = 0; 
  92. buddypress()->is_single_item = false; 
  93. bp_do_404(); 
  94. return; 
  95. } else { 
  96. bp_core_no_access( $no_access_args ); 
  97.  
  98. add_action( 'bp_actions', 'bp_groups_group_access_protection' ); 
  99.  
  100. /** 
  101. * Catch and process group creation form submissions. 
  102. * 
  103. * @since 1.2.0 
  104. * 
  105. * @return bool 
  106. */ 
  107. function groups_action_create_group() { 
  108.  
  109. // If we're not at domain.org/groups/create/ then return false. 
  110. if ( !bp_is_groups_component() || !bp_is_current_action( 'create' ) ) 
  111. return false; 
  112.  
  113. if ( !is_user_logged_in() ) 
  114. return false; 
  115.  
  116. if ( !bp_user_can_create_groups() ) { 
  117. bp_core_add_message( __( 'Sorry, you are not allowed to create groups.', 'buddypress' ), 'error' ); 
  118. bp_core_redirect( bp_get_groups_directory_permalink() ); 
  119.  
  120. $bp = buddypress(); 
  121.  
  122. // Make sure creation steps are in the right order. 
  123. groups_action_sort_creation_steps(); 
  124.  
  125. // If no current step is set, reset everything so we can start a fresh group creation. 
  126. $bp->groups->current_create_step = bp_action_variable( 1 ); 
  127. if ( !bp_get_groups_current_create_step() ) { 
  128. unset( $bp->groups->current_create_step ); 
  129. unset( $bp->groups->completed_create_steps ); 
  130.  
  131. setcookie( 'bp_new_group_id', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); 
  132. setcookie( 'bp_completed_create_steps', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); 
  133.  
  134. $reset_steps = true; 
  135. $keys = array_keys( $bp->groups->group_creation_steps ); 
  136. bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create/step/' . array_shift( $keys ) ) ); 
  137.  
  138. // If this is a creation step that is not recognized, just redirect them back to the first screen. 
  139. if ( bp_get_groups_current_create_step() && empty( $bp->groups->group_creation_steps[bp_get_groups_current_create_step()] ) ) { 
  140. bp_core_add_message( __('There was an error saving group details. Please try again.', 'buddypress'), 'error' ); 
  141. bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create' ) ); 
  142.  
  143. // Fetch the currently completed steps variable. 
  144. if ( isset( $_COOKIE['bp_completed_create_steps'] ) && !isset( $reset_steps ) ) 
  145. $bp->groups->completed_create_steps = json_decode( base64_decode( stripslashes( $_COOKIE['bp_completed_create_steps'] ) ) ); 
  146.  
  147. // Set the ID of the new group, if it has already been created in a previous step. 
  148. if ( bp_get_new_group_id() ) { 
  149. $bp->groups->current_group = groups_get_group( $bp->groups->new_group_id ); 
  150.  
  151. // Only allow the group creator to continue to edit the new group. 
  152. if ( ! bp_is_group_creator( $bp->groups->current_group, bp_loggedin_user_id() ) ) { 
  153. bp_core_add_message( __( 'Only the group creator may continue editing this group.', 'buddypress' ), 'error' ); 
  154. bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create' ) ); 
  155.  
  156. // If the save, upload or skip button is hit, lets calculate what we need to save. 
  157. if ( isset( $_POST['save'] ) ) { 
  158.  
  159. // Check the nonce. 
  160. check_admin_referer( 'groups_create_save_' . bp_get_groups_current_create_step() ); 
  161.  
  162. if ( 'group-details' == bp_get_groups_current_create_step() ) { 
  163. if ( empty( $_POST['group-name'] ) || empty( $_POST['group-desc'] ) || !strlen( trim( $_POST['group-name'] ) ) || !strlen( trim( $_POST['group-desc'] ) ) ) { 
  164. bp_core_add_message( __( 'Please fill in all of the required fields', 'buddypress' ), 'error' ); 
  165. bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create/step/' . bp_get_groups_current_create_step() ) ); 
  166.  
  167. $new_group_id = isset( $bp->groups->new_group_id ) ? $bp->groups->new_group_id : 0; 
  168.  
  169. if ( !$bp->groups->new_group_id = groups_create_group( array( 'group_id' => $new_group_id, 'name' => $_POST['group-name'], 'description' => $_POST['group-desc'], 'slug' => groups_check_slug( sanitize_title( esc_attr( $_POST['group-name'] ) ) ), 'date_created' => bp_core_current_time(), 'status' => 'public' ) ) ) { 
  170. bp_core_add_message( __( 'There was an error saving group details. Please try again.', 'buddypress' ), 'error' ); 
  171. bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create/step/' . bp_get_groups_current_create_step() ) ); 
  172.  
  173. if ( 'group-settings' == bp_get_groups_current_create_step() ) { 
  174. $group_status = 'public'; 
  175. $group_enable_forum = 1; 
  176.  
  177. if ( !isset($_POST['group-show-forum']) ) { 
  178. $group_enable_forum = 0; 
  179. } else { 
  180. // Create the forum if enable_forum = 1. 
  181. if ( bp_is_active( 'forums' ) && !groups_get_groupmeta( $bp->groups->new_group_id, 'forum_id' ) ) { 
  182. groups_new_group_forum(); 
  183.  
  184. if ( 'private' == $_POST['group-status'] ) 
  185. $group_status = 'private'; 
  186. elseif ( 'hidden' == $_POST['group-status'] ) 
  187. $group_status = 'hidden'; 
  188.  
  189. if ( !$bp->groups->new_group_id = groups_create_group( array( 'group_id' => $bp->groups->new_group_id, 'status' => $group_status, 'enable_forum' => $group_enable_forum ) ) ) { 
  190. bp_core_add_message( __( 'There was an error saving group details. Please try again.', 'buddypress' ), 'error' ); 
  191. bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create/step/' . bp_get_groups_current_create_step() ) ); 
  192.  
  193. // Save group types. 
  194. if ( ! empty( $_POST['group-types'] ) ) { 
  195. bp_groups_set_group_type( $bp->groups->new_group_id, $_POST['group-types'] ); 
  196.  
  197. /** 
  198. * Filters the allowed invite statuses. 
  199. * 
  200. * @since 1.5.0 
  201. * 
  202. * @param array $value Array of statuses allowed. 
  203. * Possible values are 'members,  
  204. * 'mods', and 'admins'. 
  205. */ 
  206. $allowed_invite_status = apply_filters( 'groups_allowed_invite_status', array( 'members', 'mods', 'admins' ) ); 
  207. $invite_status = !empty( $_POST['group-invite-status'] ) && in_array( $_POST['group-invite-status'], (array) $allowed_invite_status ) ? $_POST['group-invite-status'] : 'members'; 
  208.  
  209. groups_update_groupmeta( $bp->groups->new_group_id, 'invite_status', $invite_status ); 
  210.  
  211. if ( 'group-invites' === bp_get_groups_current_create_step() ) { 
  212. if ( ! empty( $_POST['friends'] ) ) { 
  213. foreach ( (array) $_POST['friends'] as $friend ) { 
  214. groups_invite_user( array( 
  215. 'user_id' => (int) $friend,  
  216. 'group_id' => $bp->groups->new_group_id,  
  217. ) ); 
  218.  
  219. groups_send_invites( bp_loggedin_user_id(), $bp->groups->new_group_id ); 
  220.  
  221. /** 
  222. * Fires before finalization of group creation and cookies are set. 
  223. * 
  224. * This hook is a variable hook dependent on the current step 
  225. * in the creation process. 
  226. * 
  227. * @since 1.1.0 
  228. */ 
  229. do_action( 'groups_create_group_step_save_' . bp_get_groups_current_create_step() ); 
  230.  
  231. /** 
  232. * Fires after the group creation step is completed. 
  233. * 
  234. * Mostly for clearing cache on a generic action name. 
  235. * 
  236. * @since 1.1.0 
  237. */ 
  238. do_action( 'groups_create_group_step_complete' ); 
  239.  
  240. /** 
  241. * Once we have successfully saved the details for this step of the creation process 
  242. * we need to add the current step to the array of completed steps, then update the cookies 
  243. * holding the information 
  244. */ 
  245. $completed_create_steps = isset( $bp->groups->completed_create_steps ) ? $bp->groups->completed_create_steps : array(); 
  246. if ( !in_array( bp_get_groups_current_create_step(), $completed_create_steps ) ) 
  247. $bp->groups->completed_create_steps[] = bp_get_groups_current_create_step(); 
  248.  
  249. // Reset cookie info. 
  250. setcookie( 'bp_new_group_id', $bp->groups->new_group_id, time()+60*60*24, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); 
  251. setcookie( 'bp_completed_create_steps', base64_encode( json_encode( $bp->groups->completed_create_steps ) ), time()+60*60*24, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); 
  252.  
  253. // If we have completed all steps and hit done on the final step we 
  254. // can redirect to the completed group. 
  255. $keys = array_keys( $bp->groups->group_creation_steps ); 
  256. if ( count( $bp->groups->completed_create_steps ) == count( $keys ) && bp_get_groups_current_create_step() == array_pop( $keys ) ) { 
  257. unset( $bp->groups->current_create_step ); 
  258. unset( $bp->groups->completed_create_steps ); 
  259.  
  260. setcookie( 'bp_new_group_id', false, time() - 3600, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); 
  261. setcookie( 'bp_completed_create_steps', false, time() - 3600, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); 
  262.  
  263. // Once we completed all steps, record the group creation in the activity stream. 
  264. groups_record_activity( array( 
  265. 'type' => 'created_group',  
  266. 'item_id' => $bp->groups->new_group_id 
  267. ) ); 
  268.  
  269. /** 
  270. * Fires after the group has been successfully created. 
  271. * 
  272. * @since 1.1.0 
  273. * 
  274. * @param int $new_group_id ID of the newly created group. 
  275. */ 
  276. do_action( 'groups_group_create_complete', $bp->groups->new_group_id ); 
  277.  
  278. bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) ); 
  279. } else { 
  280. /** 
  281. * Since we don't know what the next step is going to be (any plugin can insert steps) 
  282. * we need to loop the step array and fetch the next step that way. 
  283. */ 
  284. foreach ( $keys as $key ) { 
  285. if ( $key == bp_get_groups_current_create_step() ) { 
  286. $next = 1; 
  287. continue; 
  288.  
  289. if ( isset( $next ) ) { 
  290. $next_step = $key; 
  291. break; 
  292.  
  293. bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create/step/' . $next_step ) ); 
  294.  
  295. // Remove invitations. 
  296. if ( 'group-invites' === bp_get_groups_current_create_step() && ! empty( $_REQUEST['user_id'] ) && is_numeric( $_REQUEST['user_id'] ) ) { 
  297. if ( ! check_admin_referer( 'groups_invite_uninvite_user' ) ) { 
  298. return false; 
  299.  
  300. $message = __( 'Invite successfully removed', 'buddypress' ); 
  301. $error = false; 
  302.  
  303. if( ! groups_uninvite_user( (int) $_REQUEST['user_id'], $bp->groups->new_group_id ) ) { 
  304. $message = __( 'There was an error removing the invite', 'buddypress' ); 
  305. $error = 'error'; 
  306.  
  307. bp_core_add_message( $message, $error ); 
  308. bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create/step/group-invites' ) ); 
  309.  
  310. // Group avatar is handled separately. 
  311. if ( 'group-avatar' == bp_get_groups_current_create_step() && isset( $_POST['upload'] ) ) { 
  312. if ( ! isset( $bp->avatar_admin ) ) { 
  313. $bp->avatar_admin = new stdClass(); 
  314.  
  315. if ( !empty( $_FILES ) && isset( $_POST['upload'] ) ) { 
  316. // Normally we would check a nonce here, but the group save nonce is used instead. 
  317. // Pass the file to the avatar upload handler. 
  318. if ( bp_core_avatar_handle_upload( $_FILES, 'groups_avatar_upload_dir' ) ) { 
  319. $bp->avatar_admin->step = 'crop-image'; 
  320.  
  321. // Make sure we include the jQuery jCrop file for image cropping. 
  322. add_action( 'wp_print_scripts', 'bp_core_add_jquery_cropper' ); 
  323.  
  324. // If the image cropping is done, crop the image and save a full/thumb version. 
  325. if ( isset( $_POST['avatar-crop-submit'] ) && isset( $_POST['upload'] ) ) { 
  326.  
  327. // Normally we would check a nonce here, but the group save nonce is used instead. 
  328. $args = array( 
  329. 'object' => 'group',  
  330. 'avatar_dir' => 'group-avatars',  
  331. 'item_id' => $bp->groups->current_group->id,  
  332. 'original_file' => $_POST['image_src'],  
  333. 'crop_x' => $_POST['x'],  
  334. 'crop_y' => $_POST['y'],  
  335. 'crop_w' => $_POST['w'],  
  336. 'crop_h' => $_POST['h'] 
  337. ); 
  338.  
  339. if ( ! bp_core_avatar_handle_crop( $args ) ) { 
  340. bp_core_add_message( __( 'There was an error saving the group profile photo, please try uploading again.', 'buddypress' ), 'error' ); 
  341. } else { 
  342. /** 
  343. * Fires after a group avatar is uploaded. 
  344. * 
  345. * @since 2.8.0 
  346. * 
  347. * @param int $group_id ID of the group. 
  348. * @param string $type Avatar type. 'crop' or 'full'. 
  349. * @param array $args Array of parameters passed to the avatar handler. 
  350. */ 
  351. do_action( 'groups_avatar_uploaded', bp_get_current_group_id(), 'crop', $args ); 
  352.  
  353. bp_core_add_message( __( 'The group profile photo was uploaded successfully.', 'buddypress' ) ); 
  354.  
  355. /** 
  356. * Filters the template to load for the group creation screen. 
  357. * 
  358. * @since 1.0.0 
  359. * 
  360. * @param string $value Path to the group creation template to load. 
  361. */ 
  362. bp_core_load_template( apply_filters( 'groups_template_create_group', 'groups/create' ) ); 
  363. add_action( 'bp_actions', 'groups_action_create_group' ); 
  364.  
  365. /** 
  366. * Catch and process "Join Group" button clicks. 
  367. * 
  368. * @since 1.0.0 
  369. * 
  370. * @return bool 
  371. */ 
  372. function groups_action_join_group() { 
  373.  
  374. if ( !bp_is_single_item() || !bp_is_groups_component() || !bp_is_current_action( 'join' ) ) 
  375. return false; 
  376.  
  377. // Nonce check. 
  378. if ( !check_admin_referer( 'groups_join_group' ) ) 
  379. return false; 
  380.  
  381. $bp = buddypress(); 
  382.  
  383. // Skip if banned or already a member. 
  384. if ( !groups_is_user_member( bp_loggedin_user_id(), $bp->groups->current_group->id ) && !groups_is_user_banned( bp_loggedin_user_id(), $bp->groups->current_group->id ) ) { 
  385.  
  386. // User wants to join a group that is not public. 
  387. if ( $bp->groups->current_group->status != 'public' ) { 
  388. if ( !groups_check_user_has_invite( bp_loggedin_user_id(), $bp->groups->current_group->id ) ) { 
  389. bp_core_add_message( __( 'There was an error joining the group.', 'buddypress' ), 'error' ); 
  390. bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) ); 
  391.  
  392. // User wants to join any group. 
  393. if ( !groups_join_group( $bp->groups->current_group->id ) ) 
  394. bp_core_add_message( __( 'There was an error joining the group.', 'buddypress' ), 'error' ); 
  395. else 
  396. bp_core_add_message( __( 'You joined the group!', 'buddypress' ) ); 
  397.  
  398. bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) ); 
  399.  
  400. /** 
  401. * Filters the template to load for the single group screen. 
  402. * 
  403. * @since 1.0.0 
  404. * 
  405. * @param string $value Path to the single group template to load. 
  406. */ 
  407. bp_core_load_template( apply_filters( 'groups_template_group_home', 'groups/single/home' ) ); 
  408. add_action( 'bp_actions', 'groups_action_join_group' ); 
  409.  
  410. /** 
  411. * Catch and process "Leave Group" button clicks. 
  412. * 
  413. * When a group member clicks on the "Leave Group" button from a group's page,  
  414. * this function is run. 
  415. * 
  416. * Note: When leaving a group from the group directory, AJAX is used and 
  417. * another function handles this. See {@link bp_legacy_theme_ajax_joinleave_group()}. 
  418. * 
  419. * @since 1.2.4 
  420. * 
  421. * @return bool 
  422. */ 
  423. function groups_action_leave_group() { 
  424. if ( ! bp_is_single_item() || ! bp_is_groups_component() || ! bp_is_current_action( 'leave-group' ) ) { 
  425. return false; 
  426.  
  427. // Nonce check. 
  428. if ( ! check_admin_referer( 'groups_leave_group' ) ) { 
  429. return false; 
  430.  
  431. // User wants to leave any group. 
  432. if ( groups_is_user_member( bp_loggedin_user_id(), bp_get_current_group_id() ) ) { 
  433. $bp = buddypress(); 
  434.  
  435. // Stop sole admins from abandoning their group. 
  436. $group_admins = groups_get_group_admins( bp_get_current_group_id() ); 
  437.  
  438. if ( 1 == count( $group_admins ) && $group_admins[0]->user_id == bp_loggedin_user_id() ) { 
  439. bp_core_add_message( __( 'This group must have at least one admin', 'buddypress' ), 'error' ); 
  440. } elseif ( ! groups_leave_group( $bp->groups->current_group->id ) ) { 
  441. bp_core_add_message( __( 'There was an error leaving the group.', 'buddypress' ), 'error' ); 
  442. } else { 
  443. bp_core_add_message( __( 'You successfully left the group.', 'buddypress' ) ); 
  444.  
  445. $redirect = bp_get_group_permalink( groups_get_current_group() ); 
  446.  
  447. if( 'hidden' == $bp->groups->current_group->status ) { 
  448. $redirect = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() ); 
  449.  
  450. bp_core_redirect( $redirect ); 
  451.  
  452. /** This filter is documented in bp-groups/bp-groups-actions.php */ 
  453. bp_core_load_template( apply_filters( 'groups_template_group_home', 'groups/single/home' ) ); 
  454. add_action( 'bp_actions', 'groups_action_leave_group' ); 
  455.  
  456. /** 
  457. * Sort the group creation steps. 
  458. * 
  459. * @since 1.1.0 
  460. * 
  461. * @return false|null False on failure. 
  462. */ 
  463. function groups_action_sort_creation_steps() { 
  464.  
  465. if ( !bp_is_groups_component() || !bp_is_current_action( 'create' ) ) 
  466. return false; 
  467.  
  468. $bp = buddypress(); 
  469.  
  470. if ( !is_array( $bp->groups->group_creation_steps ) ) 
  471. return false; 
  472.  
  473. foreach ( (array) $bp->groups->group_creation_steps as $slug => $step ) { 
  474. while ( !empty( $temp[$step['position']] ) ) 
  475. $step['position']++; 
  476.  
  477. $temp[$step['position']] = array( 'name' => $step['name'], 'slug' => $slug ); 
  478.  
  479. // Sort the steps by their position key. 
  480. ksort($temp); 
  481. unset($bp->groups->group_creation_steps); 
  482.  
  483. foreach( (array) $temp as $position => $step ) 
  484. $bp->groups->group_creation_steps[$step['slug']] = array( 'name' => $step['name'], 'position' => $position ); 
  485.  
  486. /** 
  487. * Fires after group creation sets have been sorted. 
  488. * 
  489. * @since 2.3.0 
  490. */ 
  491. do_action( 'groups_action_sort_creation_steps' ); 
  492.  
  493. /** 
  494. * Catch requests for a random group page (example.com/groups/?random-group) and redirect. 
  495. * 
  496. * @since 1.2.0 
  497. */ 
  498. function groups_action_redirect_to_random_group() { 
  499.  
  500. if ( bp_is_groups_component() && isset( $_GET['random-group'] ) ) { 
  501. $group = BP_Groups_Group::get_random( 1, 1 ); 
  502.  
  503. bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . $group['groups'][0]->slug ) ); 
  504. add_action( 'bp_actions', 'groups_action_redirect_to_random_group' ); 
  505.  
  506. /** 
  507. * Load the activity feed for the current group. 
  508. * 
  509. * @since 1.2.0 
  510. * 
  511. * @return false|null False on failure. 
  512. */ 
  513. function groups_action_group_feed() { 
  514.  
  515. // Get current group. 
  516. $group = groups_get_current_group(); 
  517.  
  518. if ( ! bp_is_active( 'activity' ) || ! bp_is_groups_component() || ! $group || ! bp_is_current_action( 'feed' ) ) 
  519. return false; 
  520.  
  521. // If group isn't public or if logged-in user is not a member of the group, do 
  522. // not output the group activity feed. 
  523. if ( ! bp_group_is_visible( $group ) ) { 
  524. return false; 
  525.  
  526. // Set up the feed. 
  527. buddypress()->activity->feed = new BP_Activity_Feed( array( 
  528. 'id' => 'group',  
  529.  
  530. /** translators: Group activity RSS title - "[Site Name] | [Group Name] | Activity" */ 
  531. 'title' => sprintf( __( '%1$s | %2$s | Activity', 'buddypress' ), bp_get_site_name(), bp_get_current_group_name() ),  
  532.  
  533. 'link' => bp_get_group_permalink( $group ),  
  534. 'description' => sprintf( __( "Activity feed for the group, %s.", 'buddypress' ), bp_get_current_group_name() ),  
  535. 'activity_args' => array( 
  536. 'object' => buddypress()->groups->id,  
  537. 'primary_id' => bp_get_current_group_id(),  
  538. 'display_comments' => 'threaded' 
  539. ) ); 
  540. add_action( 'bp_actions', 'groups_action_group_feed' ); 
  541.  
  542. /** 
  543. * Update orphaned child groups when the parent is deleted. 
  544. * 
  545. * @since 2.7.0 
  546. * 
  547. * @param BP_Groups_Group $group Instance of the group item being deleted. 
  548. */ 
  549. function bp_groups_update_orphaned_groups_on_group_delete( $group ) { 
  550. // Get child groups and set the parent to the deleted parent's parent. 
  551. $grandparent_group_id = $group->parent_id; 
  552. $child_args = array( 
  553. 'parent_id' => $group->id,  
  554. 'show_hidden' => true,  
  555. 'per_page' => false,  
  556. 'update_meta_cache' => false,  
  557. ); 
  558. $children = groups_get_groups( $child_args ); 
  559. $children = $children['groups']; 
  560.  
  561. foreach ( $children as $cgroup ) { 
  562. $cgroup->parent_id = $grandparent_group_id; 
  563. $cgroup->save(); 
  564. add_action( 'bp_groups_delete_group', 'bp_groups_update_orphaned_groups_on_group_delete', 10, 2 ); 
.