/** * Process input from the Group Type bulk change select. * * @since 2.7.0 * * @param string $doaction Current $_GET action being performed in admin screen. */ function bp_groups_admin_process_group_type_bulk_changes($doaction) { // Bail if no groups are specified or if this isn't a relevant action. if (empty($_REQUEST['gid']) || empty($_REQUEST['bp_change_type']) && empty($_REQUEST['bp_change_type2']) || empty($_REQUEST['bp_change_group_type'])) { return; } // Bail if nonce check fails. check_admin_referer('bp-bulk-groups-change-type-' . bp_loggedin_user_id(), 'bp-bulk-groups-change-type-nonce'); if (!bp_current_user_can('bp_moderate')) { return; } $new_type = ''; if (!empty($_REQUEST['bp_change_type2'])) { $new_type = sanitize_text_field($_REQUEST['bp_change_type2']); } elseif (!empty($_REQUEST['bp_change_type'])) { $new_type = sanitize_text_field($_REQUEST['bp_change_type']); } // Check that the selected type actually exists. if ('remove_group_type' !== $new_type && null === bp_groups_get_group_type_object($new_type)) { $error = true; } else { // Run through group ids. $error = false; foreach ((array) $_REQUEST['gid'] as $group_id) { $group_id = (int) $group_id; // Get the old group type to check against. $group_type = bp_groups_get_group_type($group_id); if ('remove_group_type' === $new_type) { // Remove the current group type, if there's one to remove. if ($group_type) { $removed = bp_groups_remove_group_type($group_id, $group_type); if (false === $removed || is_wp_error($removed)) { $error = true; } } } else { // Set the new group type. if ($new_type !== $group_type) { $set = bp_groups_set_group_type($group_id, $new_type); if (false === $set || is_wp_error($set)) { $error = true; } } } } } // If there were any errors, show the error message. if ($error) { $redirect = add_query_arg(array('updated' => 'group-type-change-error'), wp_get_referer()); } else { $redirect = add_query_arg(array('updated' => 'group-type-change-success'), wp_get_referer()); } wp_redirect($redirect); exit; }
/** * @group group_types */ public function test_group_type__not_in_takes_precedence_over_group_type() { $g1 = $this->factory->group->create(); $g2 = $this->factory->group->create(); $g3 = $this->factory->group->create(); bp_groups_register_group_type('foo'); bp_groups_set_group_type($g1, 'foo'); bp_groups_set_group_type($g2, 'foo'); bp_groups_set_group_type($g3, 'foo'); $groups = BP_Groups_Group::get(array('group_type' => 'foo', 'group_type__not_in' => 'foo')); $this->assertEmpty($groups['groups']); }
/** * Delete a group's type when the group is deleted. * * @since 2.6.0 * * @param int $group_id ID of the group. * @return array $value See {@see bp_groups_set_group_type()}. */ function bp_remove_group_type_on_group_delete($group_id = 0) { bp_groups_set_group_type($group_id, ''); }
public function test_bp_groups_set_group_type_should_set_multiple_types_when_passing_array_of_types() { $g = $this->factory->group->create(array('creator_id' => self::$u1)); bp_groups_register_group_type('foo'); bp_groups_register_group_type('bar'); // Set multiple group types. $types = array('foo', 'bar'); bp_groups_set_group_type($g, $types); // Assert! $this->assertEqualSets($types, bp_groups_get_group_type($g, false)); }
/** * Catch and process group creation form submissions. * * @since 1.2.0 * * @return bool */ function groups_action_create_group() { // If we're not at domain.org/groups/create/ then return false. if (!bp_is_groups_component() || !bp_is_current_action('create')) { return false; } if (!is_user_logged_in()) { return false; } if (!bp_user_can_create_groups()) { bp_core_add_message(__('Sorry, you are not allowed to create groups.', 'buddypress'), 'error'); bp_core_redirect(bp_get_groups_directory_permalink()); } $bp = buddypress(); // Make sure creation steps are in the right order. groups_action_sort_creation_steps(); // If no current step is set, reset everything so we can start a fresh group creation. $bp->groups->current_create_step = bp_action_variable(1); if (!bp_get_groups_current_create_step()) { unset($bp->groups->current_create_step); unset($bp->groups->completed_create_steps); setcookie('bp_new_group_id', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl()); setcookie('bp_completed_create_steps', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl()); $reset_steps = true; $keys = array_keys($bp->groups->group_creation_steps); bp_core_redirect(trailingslashit(bp_get_groups_directory_permalink() . 'create/step/' . array_shift($keys))); } // If this is a creation step that is not recognized, just redirect them back to the first screen. if (bp_get_groups_current_create_step() && empty($bp->groups->group_creation_steps[bp_get_groups_current_create_step()])) { bp_core_add_message(__('There was an error saving group details. Please try again.', 'buddypress'), 'error'); bp_core_redirect(trailingslashit(bp_get_groups_directory_permalink() . 'create')); } // Fetch the currently completed steps variable. if (isset($_COOKIE['bp_completed_create_steps']) && !isset($reset_steps)) { $bp->groups->completed_create_steps = json_decode(base64_decode(stripslashes($_COOKIE['bp_completed_create_steps']))); } // Set the ID of the new group, if it has already been created in a previous step. if (isset($_COOKIE['bp_new_group_id'])) { $bp->groups->new_group_id = (int) $_COOKIE['bp_new_group_id']; $bp->groups->current_group = groups_get_group($bp->groups->new_group_id); // Only allow the group creator to continue to edit the new group. if (!bp_is_group_creator($bp->groups->current_group, bp_loggedin_user_id())) { bp_core_add_message(__('Only the group creator may continue editing this group.', 'buddypress'), 'error'); bp_core_redirect(trailingslashit(bp_get_groups_directory_permalink() . 'create')); } } // If the save, upload or skip button is hit, lets calculate what we need to save. if (isset($_POST['save'])) { // Check the nonce. check_admin_referer('groups_create_save_' . bp_get_groups_current_create_step()); if ('group-details' == bp_get_groups_current_create_step()) { if (empty($_POST['group-name']) || empty($_POST['group-desc']) || !strlen(trim($_POST['group-name'])) || !strlen(trim($_POST['group-desc']))) { bp_core_add_message(__('Please fill in all of the required fields', 'buddypress'), 'error'); bp_core_redirect(trailingslashit(bp_get_groups_directory_permalink() . 'create/step/' . bp_get_groups_current_create_step())); } $new_group_id = isset($bp->groups->new_group_id) ? $bp->groups->new_group_id : 0; 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')))) { bp_core_add_message(__('There was an error saving group details. Please try again.', 'buddypress'), 'error'); bp_core_redirect(trailingslashit(bp_get_groups_directory_permalink() . 'create/step/' . bp_get_groups_current_create_step())); } } if ('group-settings' == bp_get_groups_current_create_step()) { $group_status = 'public'; $group_enable_forum = 1; if (!isset($_POST['group-show-forum'])) { $group_enable_forum = 0; } else { // Create the forum if enable_forum = 1. if (bp_is_active('forums') && !groups_get_groupmeta($bp->groups->new_group_id, 'forum_id')) { groups_new_group_forum(); } } if ('private' == $_POST['group-status']) { $group_status = 'private'; } elseif ('hidden' == $_POST['group-status']) { $group_status = 'hidden'; } 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)))) { bp_core_add_message(__('There was an error saving group details. Please try again.', 'buddypress'), 'error'); bp_core_redirect(trailingslashit(bp_get_groups_directory_permalink() . 'create/step/' . bp_get_groups_current_create_step())); } // Save group types. if (!empty($_POST['group-types'])) { bp_groups_set_group_type($bp->groups->new_group_id, $_POST['group-types']); } /** * Filters the allowed invite statuses. * * @since 1.5.0 * * @param array $value Array of statuses allowed. * Possible values are 'members, * 'mods', and 'admins'. */ $allowed_invite_status = apply_filters('groups_allowed_invite_status', array('members', 'mods', 'admins')); $invite_status = !empty($_POST['group-invite-status']) && in_array($_POST['group-invite-status'], (array) $allowed_invite_status) ? $_POST['group-invite-status'] : 'members'; groups_update_groupmeta($bp->groups->new_group_id, 'invite_status', $invite_status); } if ('group-invites' === bp_get_groups_current_create_step()) { if (!empty($_POST['friends'])) { foreach ((array) $_POST['friends'] as $friend) { groups_invite_user(array('user_id' => (int) $friend, 'group_id' => $bp->groups->new_group_id)); } } groups_send_invites(bp_loggedin_user_id(), $bp->groups->new_group_id); } /** * Fires before finalization of group creation and cookies are set. * * This hook is a variable hook dependent on the current step * in the creation process. * * @since 1.1.0 */ do_action('groups_create_group_step_save_' . bp_get_groups_current_create_step()); /** * Fires after the group creation step is completed. * * Mostly for clearing cache on a generic action name. * * @since 1.1.0 */ do_action('groups_create_group_step_complete'); /** * Once we have successfully saved the details for this step of the creation process * we need to add the current step to the array of completed steps, then update the cookies * holding the information */ $completed_create_steps = isset($bp->groups->completed_create_steps) ? $bp->groups->completed_create_steps : array(); if (!in_array(bp_get_groups_current_create_step(), $completed_create_steps)) { $bp->groups->completed_create_steps[] = bp_get_groups_current_create_step(); } // Reset cookie info. setcookie('bp_new_group_id', $bp->groups->new_group_id, time() + 60 * 60 * 24, COOKIEPATH, COOKIE_DOMAIN, is_ssl()); setcookie('bp_completed_create_steps', base64_encode(json_encode($bp->groups->completed_create_steps)), time() + 60 * 60 * 24, COOKIEPATH, COOKIE_DOMAIN, is_ssl()); // If we have completed all steps and hit done on the final step we // can redirect to the completed group. $keys = array_keys($bp->groups->group_creation_steps); if (count($bp->groups->completed_create_steps) == count($keys) && bp_get_groups_current_create_step() == array_pop($keys)) { unset($bp->groups->current_create_step); unset($bp->groups->completed_create_steps); setcookie('bp_new_group_id', false, time() - 3600, COOKIEPATH, COOKIE_DOMAIN, is_ssl()); setcookie('bp_completed_create_steps', false, time() - 3600, COOKIEPATH, COOKIE_DOMAIN, is_ssl()); // Once we completed all steps, record the group creation in the activity stream. groups_record_activity(array('type' => 'created_group', 'item_id' => $bp->groups->new_group_id)); /** * Fires after the group has been successfully created. * * @since 1.1.0 * * @param int $new_group_id ID of the newly created group. */ do_action('groups_group_create_complete', $bp->groups->new_group_id); bp_core_redirect(bp_get_group_permalink($bp->groups->current_group)); } else { /** * Since we don't know what the next step is going to be (any plugin can insert steps) * we need to loop the step array and fetch the next step that way. */ foreach ($keys as $key) { if ($key == bp_get_groups_current_create_step()) { $next = 1; continue; } if (isset($next)) { $next_step = $key; break; } } bp_core_redirect(trailingslashit(bp_get_groups_directory_permalink() . 'create/step/' . $next_step)); } } // Remove invitations. if ('group-invites' === bp_get_groups_current_create_step() && !empty($_REQUEST['user_id']) && is_numeric($_REQUEST['user_id'])) { if (!check_admin_referer('groups_invite_uninvite_user')) { return false; } $message = __('Invite successfully removed', 'buddypress'); $error = false; if (!groups_uninvite_user((int) $_REQUEST['user_id'], $bp->groups->new_group_id)) { $message = __('There was an error removing the invite', 'buddypress'); $error = 'error'; } bp_core_add_message($message, $error); bp_core_redirect(trailingslashit(bp_get_groups_directory_permalink() . 'create/step/group-invites')); } // Group avatar is handled separately. if ('group-avatar' == bp_get_groups_current_create_step() && isset($_POST['upload'])) { if (!isset($bp->avatar_admin)) { $bp->avatar_admin = new stdClass(); } if (!empty($_FILES) && isset($_POST['upload'])) { // Normally we would check a nonce here, but the group save nonce is used instead. // Pass the file to the avatar upload handler. if (bp_core_avatar_handle_upload($_FILES, 'groups_avatar_upload_dir')) { $bp->avatar_admin->step = 'crop-image'; // Make sure we include the jQuery jCrop file for image cropping. add_action('wp_print_scripts', 'bp_core_add_jquery_cropper'); } } // If the image cropping is done, crop the image and save a full/thumb version. if (isset($_POST['avatar-crop-submit']) && isset($_POST['upload'])) { // Normally we would check a nonce here, but the group save nonce is used instead. if (!bp_core_avatar_handle_crop(array('object' => 'group', 'avatar_dir' => 'group-avatars', 'item_id' => $bp->groups->current_group->id, 'original_file' => $_POST['image_src'], 'crop_x' => $_POST['x'], 'crop_y' => $_POST['y'], 'crop_w' => $_POST['w'], 'crop_h' => $_POST['h']))) { bp_core_add_message(__('There was an error saving the group profile photo, please try uploading again.', 'buddypress'), 'error'); } else { bp_core_add_message(__('The group profile photo was uploaded successfully.', 'buddypress')); } } } /** * Filters the template to load for the group creation screen. * * @since 1.0.0 * * @param string $value Path to the group creation template to load. */ bp_core_load_template(apply_filters('groups_template_create_group', 'groups/create')); }
/** * Handle the display of a group's admin/group-settings page. * * @since 1.0.0 */ function groups_screen_group_admin_settings() { if ('group-settings' != bp_get_group_current_admin_tab()) { return false; } if (!bp_is_item_admin()) { return false; } $bp = buddypress(); // If the edit form has been submitted, save the edited details. if (isset($_POST['save'])) { $enable_forum = isset($_POST['group-show-forum']) ? 1 : 0; // Checked against a whitelist for security. /** This filter is documented in bp-groups/bp-groups-admin.php */ $allowed_status = apply_filters('groups_allowed_status', array('public', 'private', 'hidden')); $status = in_array($_POST['group-status'], (array) $allowed_status) ? $_POST['group-status'] : 'public'; // Checked against a whitelist for security. /** This filter is documented in bp-groups/bp-groups-admin.php */ $allowed_invite_status = apply_filters('groups_allowed_invite_status', array('members', 'mods', 'admins')); $invite_status = isset($_POST['group-invite-status']) && in_array($_POST['group-invite-status'], (array) $allowed_invite_status) ? $_POST['group-invite-status'] : 'members'; // Check the nonce. if (!check_admin_referer('groups_edit_group_settings')) { return false; } /* * Save group types. * * Ensure we keep types that have 'show_in_create_screen' set to false. */ $current_types = bp_groups_get_group_type(bp_get_current_group_id(), false); $current_types = array_intersect(bp_groups_get_group_types(array('show_in_create_screen' => false)), (array) $current_types); if (isset($_POST['group-types'])) { $current_types = array_merge($current_types, $_POST['group-types']); // Set group types. bp_groups_set_group_type(bp_get_current_group_id(), $current_types); // No group types checked, so this means we want to wipe out all group types. } else { /* * Passing a blank string will wipe out all types for the group. * * Ensure we keep types that have 'show_in_create_screen' set to false. */ $current_types = empty($current_types) ? '' : $current_types; // Set group types. bp_groups_set_group_type(bp_get_current_group_id(), $current_types); } if (!groups_edit_group_settings($_POST['group-id'], $enable_forum, $status, $invite_status)) { bp_core_add_message(__('There was an error updating group settings. Please try again.', 'buddypress'), 'error'); } else { bp_core_add_message(__('Group settings were successfully updated.', 'buddypress')); } /** * Fires before the redirect if a group settings has been edited and saved. * * @since 1.0.0 * * @param int $id ID of the group that was edited. */ do_action('groups_group_settings_edited', $bp->groups->current_group->id); bp_core_redirect(bp_get_group_permalink(groups_get_current_group()) . 'admin/group-settings/'); } /** * Fires before the loading of the group admin/group-settings page template. * * @since 1.0.0 * * @param int $id ID of the group that is being displayed. */ do_action('groups_screen_group_admin_settings', $bp->groups->current_group->id); /** * Filters the template to load for a group's admin/group-settings page. * * @since 1.0.0 * * @param string $value Path to a group's admin/group-settings template. */ bp_core_load_template(apply_filters('groups_template_group_admin_settings', 'groups/single/home')); }