[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-groups/actions/ -> create.php (source)

   1  <?php
   2  /**
   3   * Groups: Create action
   4   *
   5   * @package BuddyPress
   6   * @subpackage GroupActions
   7   * @since 3.0.0
   8   */
   9  
  10  /**
  11   * Catch and process group creation form submissions.
  12   *
  13   * @since 1.2.0
  14   *
  15   * @return bool
  16   */
  17  function groups_action_create_group() {
  18  
  19      // If we're not at domain.org/groups/create/ then return false.
  20      if ( !bp_is_groups_component() || !bp_is_current_action( 'create' ) )
  21          return false;
  22  
  23      if ( !is_user_logged_in() )
  24          return false;
  25  
  26      if ( !bp_user_can_create_groups() ) {
  27          bp_core_add_message( __( 'Sorry, you are not allowed to create groups.', 'buddypress' ), 'error' );
  28          bp_core_redirect( bp_get_groups_directory_permalink() );
  29      }
  30  
  31      $bp = buddypress();
  32  
  33      // Make sure creation steps are in the right order.
  34      groups_action_sort_creation_steps();
  35  
  36      // If no current step is set, reset everything so we can start a fresh group creation.
  37      $bp->groups->current_create_step = bp_action_variable( 1 );
  38      if ( !bp_get_groups_current_create_step() ) {
  39          unset( $bp->groups->current_create_step );
  40          unset( $bp->groups->completed_create_steps );
  41  
  42          setcookie( 'bp_new_group_id', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
  43          setcookie( 'bp_completed_create_steps', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
  44  
  45          $reset_steps = true;
  46          $keys        = array_keys( $bp->groups->group_creation_steps );
  47          bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create/step/' . array_shift( $keys ) ) );
  48      }
  49  
  50      // If this is a creation step that is not recognized, just redirect them back to the first screen.
  51      if ( bp_get_groups_current_create_step() && empty( $bp->groups->group_creation_steps[bp_get_groups_current_create_step()] ) ) {
  52          bp_core_add_message( __('There was an error saving group details. Please try again.', 'buddypress'), 'error' );
  53          bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create' ) );
  54      }
  55  
  56      // Fetch the currently completed steps variable.
  57      if ( isset( $_COOKIE['bp_completed_create_steps'] ) && !isset( $reset_steps ) )
  58          $bp->groups->completed_create_steps = json_decode( base64_decode( stripslashes( $_COOKIE['bp_completed_create_steps'] ) ) );
  59  
  60      // Set the ID of the new group, if it has already been created in a previous step.
  61      if ( bp_get_new_group_id() ) {
  62          $bp->groups->current_group = groups_get_group( $bp->groups->new_group_id );
  63  
  64          // Only allow the group creator to continue to edit the new group.
  65          if ( ! bp_is_group_creator( $bp->groups->current_group, bp_loggedin_user_id() ) ) {
  66              bp_core_add_message( __( 'Only the group creator may continue editing this group.', 'buddypress' ), 'error' );
  67              bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create' ) );
  68          }
  69      }
  70  
  71      // If the save, upload or skip button is hit, lets calculate what we need to save.
  72      if ( isset( $_POST['save'] ) ) {
  73  
  74          // Check the nonce.
  75          check_admin_referer( 'groups_create_save_' . bp_get_groups_current_create_step() );
  76  
  77          if ( 'group-details' == bp_get_groups_current_create_step() ) {
  78              if ( empty( $_POST['group-name'] ) || empty( $_POST['group-desc'] ) || !strlen( trim( $_POST['group-name'] ) ) || !strlen( trim( $_POST['group-desc'] ) ) ) {
  79                  bp_core_add_message( __( 'Please fill in all of the required fields', 'buddypress' ), 'error' );
  80                  bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create/step/' . bp_get_groups_current_create_step() ) );
  81              }
  82  
  83              $new_group_id = isset( $bp->groups->new_group_id ) ? $bp->groups->new_group_id : 0;
  84  
  85              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' ) ) ) {
  86                  bp_core_add_message( __( 'There was an error saving group details. Please try again.', 'buddypress' ), 'error' );
  87                  bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create/step/' . bp_get_groups_current_create_step() ) );
  88              }
  89          }
  90  
  91          if ( 'group-settings' == bp_get_groups_current_create_step() ) {
  92              $group_status = 'public';
  93              $group_enable_forum = 1;
  94  
  95              if ( !isset($_POST['group-show-forum']) ) {
  96                  $group_enable_forum = 0;
  97              }
  98  
  99              if ( 'private' == $_POST['group-status'] )
 100                  $group_status = 'private';
 101              elseif ( 'hidden' == $_POST['group-status'] )
 102                  $group_status = 'hidden';
 103  
 104              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 ) ) ) {
 105                  bp_core_add_message( __( 'There was an error saving group details. Please try again.', 'buddypress' ), 'error' );
 106                  bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create/step/' . bp_get_groups_current_create_step() ) );
 107              }
 108  
 109              // Save group types.
 110              if ( ! empty( $_POST['group-types'] ) ) {
 111                  bp_groups_set_group_type( $bp->groups->new_group_id, $_POST['group-types'] );
 112              }
 113  
 114              /**
 115               * Filters the allowed invite statuses.
 116               *
 117               * @since 1.5.0
 118               *
 119               * @param array $value Array of statuses allowed.
 120               *                     Possible values are 'members,
 121               *                     'mods', and 'admins'.
 122               */
 123              $allowed_invite_status = apply_filters( 'groups_allowed_invite_status', array( 'members', 'mods', 'admins' ) );
 124              $invite_status           = !empty( $_POST['group-invite-status'] ) && in_array( $_POST['group-invite-status'], (array) $allowed_invite_status ) ? $_POST['group-invite-status'] : 'members';
 125  
 126              groups_update_groupmeta( $bp->groups->new_group_id, 'invite_status', $invite_status );
 127          }
 128  
 129          if ( 'group-invites' === bp_get_groups_current_create_step() ) {
 130              if ( ! empty( $_POST['friends'] ) ) {
 131                  foreach ( (array) $_POST['friends'] as $friend ) {
 132                      groups_invite_user( array(
 133                          'user_id'  => (int) $friend,
 134                          'group_id' => $bp->groups->new_group_id,
 135                      ) );
 136                  }
 137              }
 138  
 139              groups_send_invites( array(    'group_id' => $bp->groups->new_group_id ) );
 140          }
 141  
 142          /**
 143           * Fires before finalization of group creation and cookies are set.
 144           *
 145           * This hook is a variable hook dependent on the current step
 146           * in the creation process.
 147           *
 148           * @since 1.1.0
 149           */
 150          do_action( 'groups_create_group_step_save_' . bp_get_groups_current_create_step() );
 151  
 152          /**
 153           * Fires after the group creation step is completed.
 154           *
 155           * Mostly for clearing cache on a generic action name.
 156           *
 157           * @since 1.1.0
 158           */
 159          do_action( 'groups_create_group_step_complete' );
 160  
 161          /**
 162           * Once we have successfully saved the details for this step of the creation process
 163           * we need to add the current step to the array of completed steps, then update the cookies
 164           * holding the information
 165           */
 166          $completed_create_steps = isset( $bp->groups->completed_create_steps ) ? $bp->groups->completed_create_steps : array();
 167          if ( !in_array( bp_get_groups_current_create_step(), $completed_create_steps ) )
 168              $bp->groups->completed_create_steps[] = bp_get_groups_current_create_step();
 169  
 170          // Reset cookie info.
 171          setcookie( 'bp_new_group_id', $bp->groups->new_group_id, time()+60*60*24, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
 172          setcookie( 'bp_completed_create_steps', base64_encode( json_encode( $bp->groups->completed_create_steps ) ), time()+60*60*24, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
 173  
 174          // If we have completed all steps and hit done on the final step we
 175          // can redirect to the completed group.
 176          $keys = array_keys( $bp->groups->group_creation_steps );
 177          if ( count( $bp->groups->completed_create_steps ) == count( $keys ) && bp_get_groups_current_create_step() == array_pop( $keys ) ) {
 178              unset( $bp->groups->current_create_step );
 179              unset( $bp->groups->completed_create_steps );
 180  
 181              setcookie( 'bp_new_group_id', false, time() - 3600, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
 182              setcookie( 'bp_completed_create_steps', false, time() - 3600, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
 183  
 184              // Once we completed all steps, record the group creation in the activity stream.
 185              if ( bp_is_active( 'activity' ) ) {
 186                  groups_record_activity( array(
 187                      'type' => 'created_group',
 188                      'item_id' => $bp->groups->new_group_id
 189                  ) );
 190              }
 191  
 192              /**
 193               * Fires after the group has been successfully created.
 194               *
 195               * @since 1.1.0
 196               *
 197               * @param int $new_group_id ID of the newly created group.
 198               */
 199              do_action( 'groups_group_create_complete', $bp->groups->new_group_id );
 200  
 201              bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) );
 202          } else {
 203              /**
 204               * Since we don't know what the next step is going to be (any plugin can insert steps)
 205               * we need to loop the step array and fetch the next step that way.
 206               */
 207              foreach ( $keys as $key ) {
 208                  if ( $key == bp_get_groups_current_create_step() ) {
 209                      $next = 1;
 210                      continue;
 211                  }
 212  
 213                  if ( isset( $next ) ) {
 214                      $next_step = $key;
 215                      break;
 216                  }
 217              }
 218  
 219              bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create/step/' . $next_step ) );
 220          }
 221      }
 222  
 223      // Remove invitations.
 224      if ( 'group-invites' === bp_get_groups_current_create_step() && ! empty( $_REQUEST['user_id'] ) && is_numeric( $_REQUEST['user_id'] ) ) {
 225          if ( ! check_admin_referer( 'groups_invite_uninvite_user' ) ) {
 226              return false;
 227          }
 228  
 229          $message = __( 'Invite successfully removed', 'buddypress' );
 230          $error   = false;
 231  
 232          if( ! groups_uninvite_user( (int) $_REQUEST['user_id'], $bp->groups->new_group_id ) ) {
 233              $message = __( 'There was an error removing the invite', 'buddypress' );
 234              $error   = 'error';
 235          }
 236  
 237          bp_core_add_message( $message, $error );
 238          bp_core_redirect( trailingslashit( bp_get_groups_directory_permalink() . 'create/step/group-invites' ) );
 239      }
 240  
 241      // Group avatar is handled separately.
 242      if ( 'group-avatar' == bp_get_groups_current_create_step() && isset( $_POST['upload'] ) ) {
 243          if ( ! isset( $bp->avatar_admin ) ) {
 244              $bp->avatar_admin = new stdClass();
 245          }
 246  
 247          if ( !empty( $_FILES ) && isset( $_POST['upload'] ) ) {
 248              // Normally we would check a nonce here, but the group save nonce is used instead.
 249              // Pass the file to the avatar upload handler.
 250              if ( bp_core_avatar_handle_upload( $_FILES, 'groups_avatar_upload_dir' ) ) {
 251                  $bp->avatar_admin->step = 'crop-image';
 252  
 253                  // Make sure we include the jQuery jCrop file for image cropping.
 254                  add_action( 'wp_print_scripts', 'bp_core_add_jquery_cropper' );
 255              }
 256          }
 257  
 258          // If the image cropping is done, crop the image and save a full/thumb version.
 259          if ( isset( $_POST['avatar-crop-submit'] ) && isset( $_POST['upload'] ) ) {
 260  
 261              // Normally we would check a nonce here, but the group save nonce is used instead.
 262              $args = array(
 263                  'object'        => 'group',
 264                  'avatar_dir'    => 'group-avatars',
 265                  'item_id'       => $bp->groups->current_group->id,
 266                  'original_file' => $_POST['image_src'],
 267                  'crop_x'        => $_POST['x'],
 268                  'crop_y'        => $_POST['y'],
 269                  'crop_w'        => $_POST['w'],
 270                  'crop_h'        => $_POST['h']
 271              );
 272  
 273              $cropped_avatar = bp_core_avatar_handle_crop( $args, 'array' );
 274  
 275              if ( ! $cropped_avatar ) {
 276                  bp_core_add_message( __( 'There was an error saving the group profile photo, please try uploading again.', 'buddypress' ), 'error' );
 277              } else {
 278                  /**
 279                   * Fires after a group avatar is uploaded.
 280                   *
 281                   * @since 2.8.0
 282                   * @since 10.0.0 Adds a new param: an array containing the full, thumb avatar and the timestamp.
 283                   *
 284                   * @param int    $group_id       ID of the group.
 285                   * @param string $type           Avatar type. 'crop' or 'camera'.
 286                   * @param array  $args           Array of parameters passed to the crop handler.
 287                   * @param array  $cropped_avatar Array containing the full, thumb avatar and the timestamp.
 288                   */
 289                  do_action( 'groups_avatar_uploaded', bp_get_current_group_id(), 'crop', $args, $cropped_avatar );
 290  
 291                  bp_core_add_message( __( 'The group profile photo was uploaded successfully.', 'buddypress' ) );
 292              }
 293          }
 294      }
 295  
 296      /**
 297       * Filters the template to load for the group creation screen.
 298       *
 299       * @since 1.0.0
 300       *
 301       * @param string $value Path to the group creation template to load.
 302       */
 303      bp_core_load_template( apply_filters( 'groups_template_create_group', 'groups/create' ) );
 304  }
 305  add_action( 'bp_actions', 'groups_action_create_group' );
 306  
 307  /**
 308   * Sort the group creation steps.
 309   *
 310   * @since 1.1.0
 311   *
 312   * @return false|null False on failure.
 313   */
 314  function groups_action_sort_creation_steps() {
 315  
 316      if ( !bp_is_groups_component() || !bp_is_current_action( 'create' ) )
 317          return false;
 318  
 319      $bp = buddypress();
 320  
 321      if ( !is_array( $bp->groups->group_creation_steps ) )
 322          return false;
 323  
 324      foreach ( (array) $bp->groups->group_creation_steps as $slug => $step ) {
 325          while ( !empty( $temp[$step['position']] ) )
 326              $step['position']++;
 327  
 328          $temp[$step['position']] = array( 'name' => $step['name'], 'slug' => $slug );
 329      }
 330  
 331      // Sort the steps by their position key.
 332      ksort($temp);
 333      unset($bp->groups->group_creation_steps);
 334  
 335      foreach( (array) $temp as $position => $step )
 336          $bp->groups->group_creation_steps[$step['slug']] = array( 'name' => $step['name'], 'position' => $position );
 337  
 338      /**
 339       * Fires after group creation sets have been sorted.
 340       *
 341       * @since 2.3.0
 342       */
 343      do_action( 'groups_action_sort_creation_steps' );
 344  }


Generated: Sun Oct 13 01:00:54 2024 Cross-referenced by PHPXref 0.7.1