[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-members/screens/ -> register.php (source)

   1  <?php
   2  /**
   3   * Members: Register screen handler
   4   *
   5   * @package BuddyPress
   6   * @subpackage MembersScreens
   7   * @since 3.0.0
   8   */
   9  
  10  /**
  11   * Handle the loading of the signup screen.
  12   *
  13   * @since 1.1.0
  14   */
  15  function bp_core_screen_signup() {
  16      $bp = buddypress();
  17  
  18      if ( ! bp_is_current_component( 'register' ) || bp_current_action() )
  19          return;
  20  
  21      // Not a directory.
  22      bp_update_is_directory( false, 'register' );
  23  
  24      // If the user is logged in, redirect away from here.
  25      if ( is_user_logged_in() ) {
  26  
  27          $redirect_to = bp_is_component_front_page( 'register' )
  28              ? bp_get_members_directory_permalink()
  29              : bp_get_root_domain();
  30  
  31          /**
  32           * Filters the URL to redirect logged in users to when visiting registration page.
  33           *
  34           * @since 1.5.1
  35           *
  36           * @param string $redirect_to URL to redirect user to.
  37           */
  38          bp_core_redirect( apply_filters( 'bp_loggedin_register_page_redirect_to', $redirect_to ) );
  39  
  40          return;
  41      }
  42  
  43      $bp->signup->step = 'request-details';
  44  
  45      // Could the user be accepting an invitation?
  46      $active_invite = false;
  47      if ( bp_get_members_invitations_allowed() ) {
  48          // Check to see if there's a valid invitation.
  49          $maybe_invite = bp_get_members_invitation_from_request();
  50          if ( $maybe_invite->id && $maybe_invite->invitee_email ) {
  51              // Check if this user is already a member.
  52              $args = array(
  53                  'invitee_email' => $maybe_invite->invitee_email,
  54                  'accepted'      => 'accepted',
  55                  'fields'        => 'ids',
  56              );
  57              $accepted_invites = bp_members_invitations_get_invites( $args );
  58              if ( ! $accepted_invites ) {
  59                  $active_invite = true;
  60              }
  61          }
  62      }
  63  
  64      if ( ! bp_get_signup_allowed() && ! $active_invite ) {
  65          $bp->signup->step = 'registration-disabled';
  66          // If the signup page is submitted, validate and save.
  67      } elseif ( isset( $_POST['signup_submit'] ) && bp_verify_nonce_request( 'bp_new_signup' ) ) {
  68  
  69          /**
  70           * Fires before the validation of a new signup.
  71           *
  72           * @since 2.0.0
  73           */
  74          do_action( 'bp_signup_pre_validate' );
  75  
  76          // Check the base account details for problems.
  77          $account_details = bp_core_validate_user_signup( $_POST['signup_username'], $_POST['signup_email'] );
  78  
  79          // If there are errors with account details, set them for display.
  80          if ( ! empty( $account_details['errors']->errors['user_name'] ) ) {
  81              $bp->signup->errors['signup_username'] = $account_details['errors']->errors['user_name'][0];
  82          }
  83  
  84          if ( ! empty( $account_details['errors']->errors['user_email'] ) ) {
  85              $bp->signup->errors['signup_email'] = $account_details['errors']->errors['user_email'][0];
  86          }
  87  
  88          $signup_pass = '';
  89          if ( isset( $_POST['signup_password'] ) ) {
  90              $signup_pass = wp_unslash( $_POST['signup_password'] );
  91          }
  92  
  93          $signup_pass_confirm = '';
  94          if ( isset( $_POST['signup_password_confirm'] ) ) {
  95              $signup_pass_confirm = wp_unslash( $_POST['signup_password_confirm'] );
  96          }
  97  
  98          // Check the account password for problems.
  99          $account_password = bp_members_validate_user_password( $signup_pass, $signup_pass_confirm );
 100          $password_error   = $account_password->get_error_message();
 101  
 102          if ( $password_error ) {
 103              $bp->signup->errors['signup_password'] = $password_error;
 104          }
 105  
 106          if ( bp_signup_requires_privacy_policy_acceptance() && ! empty( $_POST['signup-privacy-policy-check'] ) && empty( $_POST['signup-privacy-policy-accept'] ) ) {
 107              $bp->signup->errors['signup_privacy_policy'] = __( 'You must indicate that you have read and agreed to the Privacy Policy.', 'buddypress' );
 108          }
 109  
 110          $bp->signup->username = $_POST['signup_username'];
 111          $bp->signup->email = $_POST['signup_email'];
 112  
 113          // Now we've checked account details, we can check profile information.
 114          if ( bp_is_active( 'xprofile' ) ) {
 115  
 116              // Make sure hidden field is passed and populated.
 117              if ( isset( $_POST['signup_profile_field_ids'] ) && !empty( $_POST['signup_profile_field_ids'] ) ) {
 118  
 119                  // Let's compact any profile field info into an array.
 120                  $profile_field_ids = explode( ',', $_POST['signup_profile_field_ids'] );
 121  
 122                  // Loop through the posted fields formatting any datebox values then validate the field.
 123                  foreach ( (array) $profile_field_ids as $field_id ) {
 124                      bp_xprofile_maybe_format_datebox_post_data( $field_id );
 125  
 126                      // Trim post fields.
 127                      if ( isset( $_POST[ 'field_' . $field_id ] ) ) {
 128                          if ( is_array( $_POST[ 'field_' . $field_id ] ) ) {
 129                              $_POST[ 'field_' . $field_id ] = array_map( 'trim', $_POST[ 'field_' . $field_id ] );
 130                          } else {
 131                              $_POST[ 'field_' . $field_id ] = trim( $_POST[ 'field_' . $field_id ] );
 132                          }
 133                      }
 134  
 135                      // Create errors for required fields without values.
 136                      if ( xprofile_check_is_required_field( $field_id ) && empty( $_POST[ 'field_' . $field_id ] ) && ! bp_current_user_can( 'bp_moderate' ) )
 137                          $bp->signup->errors['field_' . $field_id] = __( 'This is a required field', 'buddypress' );
 138                  }
 139  
 140                  // This situation doesn't naturally occur so bounce to website root.
 141              } else {
 142                  bp_core_redirect( bp_get_root_domain() );
 143              }
 144          }
 145  
 146          // Finally, let's check the blog details, if the user wants a blog and blog creation is enabled.
 147          if ( isset( $_POST['signup_with_blog'] ) ) {
 148              $active_signup = bp_core_get_root_option( 'registration' );
 149  
 150              if ( 'blog' == $active_signup || 'all' == $active_signup ) {
 151                  $blog_details = bp_core_validate_blog_signup( $_POST['signup_blog_url'], $_POST['signup_blog_title'] );
 152  
 153                  // If there are errors with blog details, set them for display.
 154                  if ( !empty( $blog_details['errors']->errors['blogname'] ) )
 155                      $bp->signup->errors['signup_blog_url'] = $blog_details['errors']->errors['blogname'][0];
 156  
 157                  if ( !empty( $blog_details['errors']->errors['blog_title'] ) )
 158                      $bp->signup->errors['signup_blog_title'] = $blog_details['errors']->errors['blog_title'][0];
 159              }
 160          }
 161  
 162          /**
 163           * Fires after the validation of a new signup.
 164           *
 165           * @since 1.1.0
 166           */
 167          do_action( 'bp_signup_validate' );
 168  
 169          // Add any errors to the action for the field in the template for display.
 170          if ( !empty( $bp->signup->errors ) ) {
 171              foreach ( (array) $bp->signup->errors as $fieldname => $error_message ) {
 172                  /**
 173                   * Filters the error message in the loop.
 174                   *
 175                   * @since 1.5.0
 176                   * @since 8.0.0 Adds the `$fieldname` parameter to the anonymous function.
 177                   *
 178                   * @param string $value     Error message wrapped in html.
 179                   * @param string $fieldname The name of the signup field.
 180                   */
 181                  add_action( 'bp_' . $fieldname . '_errors', function() use ( $error_message, $fieldname ) {
 182                      /**
 183                       * Filter here to edit the error message about the invalid field value.
 184                       *
 185                       * @since 1.5.0
 186                       * @since 8.0.0 Adds the `$fieldname` parameter.
 187                       *
 188                       * @param string $value     Error message wrapped in html.
 189                       * @param string $fieldname The name of the signup field.
 190                       */
 191                      echo apply_filters( 'bp_members_signup_error_message', "<div class=\"error\">" . $error_message . "</div>", $fieldname );
 192                  } );
 193              }
 194          } else {
 195              $bp->signup->step = 'save-details';
 196  
 197              // No errors! Let's register those deets.
 198              $active_signup = bp_core_get_root_option( 'registration' );
 199  
 200              if ( 'none' != $active_signup ) {
 201  
 202                  // Make sure the extended profiles module is enabled.
 203                  if ( bp_is_active( 'xprofile' ) ) {
 204                      // Let's compact any profile field info into usermeta.
 205                      $profile_field_ids = explode( ',', $_POST['signup_profile_field_ids'] );
 206  
 207                      /*
 208                       * Loop through the posted fields, formatting any
 209                       * datebox values, then add to usermeta.
 210                       */
 211                      foreach ( (array) $profile_field_ids as $field_id ) {
 212                          bp_xprofile_maybe_format_datebox_post_data( $field_id );
 213  
 214                          if ( !empty( $_POST['field_' . $field_id] ) )
 215                              $usermeta['field_' . $field_id] = $_POST['field_' . $field_id];
 216  
 217                          if ( !empty( $_POST['field_' . $field_id . '_visibility'] ) )
 218                              $usermeta['field_' . $field_id . '_visibility'] = $_POST['field_' . $field_id . '_visibility'];
 219                      }
 220  
 221                      // Store the profile field ID's in usermeta.
 222                      $usermeta['profile_field_ids'] = $_POST['signup_profile_field_ids'];
 223                  }
 224  
 225                  // Hash and store the password.
 226                  $usermeta['password'] = wp_hash_password( $_POST['signup_password'] );
 227  
 228                  // If the user decided to create a blog, save those details to usermeta.
 229                  if ( 'blog' == $active_signup || 'all' == $active_signup )
 230                      $usermeta['public'] = ( isset( $_POST['signup_blog_privacy'] ) && 'public' == $_POST['signup_blog_privacy'] ) ? true : false;
 231  
 232                  /**
 233                   * Filters the user meta used for signup.
 234                   *
 235                   * @since 1.1.0
 236                   *
 237                   * @param array $usermeta Array of user meta to add to signup.
 238                   */
 239                  $usermeta = apply_filters( 'bp_signup_usermeta', $usermeta );
 240  
 241                  // Finally, sign up the user and/or blog.
 242                  if ( isset( $_POST['signup_with_blog'] ) && is_multisite() )
 243                      $wp_user_id = bp_core_signup_blog( $blog_details['domain'], $blog_details['path'], $blog_details['blog_title'], $_POST['signup_username'], $_POST['signup_email'], $usermeta );
 244                  else
 245                      $wp_user_id = bp_core_signup_user( $_POST['signup_username'], $_POST['signup_password'], $_POST['signup_email'], $usermeta );
 246  
 247                  if ( is_wp_error( $wp_user_id ) ) {
 248                      $bp->signup->step = 'request-details';
 249                      bp_core_add_message( $wp_user_id->get_error_message(), 'error' );
 250                  } else {
 251                      $bp->signup->step = 'completed-confirmation';
 252                  }
 253              }
 254  
 255              /**
 256               * Fires after the completion of a new signup.
 257               *
 258               * @since 1.1.0
 259               */
 260              do_action( 'bp_complete_signup' );
 261          }
 262  
 263      }
 264  
 265      /**
 266       * Fires right before the loading of the Member registration screen template file.
 267       *
 268       * @since 1.5.0
 269       */
 270      do_action( 'bp_core_screen_signup' );
 271  
 272      /**
 273       * Filters the template to load for the Member registration page screen.
 274       *
 275       * @since 1.5.0
 276       *
 277       * @param string $value Path to the Member registration template to load.
 278       */
 279      bp_core_load_template( apply_filters( 'bp_core_template_register', array( 'register', 'registration/register' ) ) );
 280  }
 281  add_action( 'bp_screens', 'bp_core_screen_signup' );


Generated: Wed Jul 28 01:01:38 2021 Cross-referenced by PHPXref 0.7.1