[ 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      if ( !bp_get_signup_allowed() ) {
  46          $bp->signup->step = 'registration-disabled';
  47  
  48          // If the signup page is submitted, validate and save.
  49      } elseif ( isset( $_POST['signup_submit'] ) && bp_verify_nonce_request( 'bp_new_signup' ) ) {
  50  
  51          /**
  52           * Fires before the validation of a new signup.
  53           *
  54           * @since 2.0.0
  55           */
  56          do_action( 'bp_signup_pre_validate' );
  57  
  58          // Check the base account details for problems.
  59          $account_details = bp_core_validate_user_signup( $_POST['signup_username'], $_POST['signup_email'] );
  60  
  61          // If there are errors with account details, set them for display.
  62          if ( ! empty( $account_details['errors']->errors['user_name'] ) ) {
  63              $bp->signup->errors['signup_username'] = $account_details['errors']->errors['user_name'][0];
  64          }
  65  
  66          if ( ! empty( $account_details['errors']->errors['user_email'] ) ) {
  67              $bp->signup->errors['signup_email'] = $account_details['errors']->errors['user_email'][0];
  68          }
  69  
  70          $signup_pass = '';
  71          if ( isset( $_POST['signup_password'] ) ) {
  72              $signup_pass = wp_unslash( $_POST['signup_password'] );
  73          }
  74  
  75          $signup_pass_confirm = '';
  76          if ( isset( $_POST['signup_password_confirm'] ) ) {
  77              $signup_pass_confirm = wp_unslash( $_POST['signup_password_confirm'] );
  78          }
  79  
  80          // Check the account password for problems.
  81          $account_password = bp_members_validate_user_password( $signup_pass, $signup_pass_confirm );
  82          $password_error   = $account_password->get_error_message();
  83  
  84          if ( $password_error ) {
  85              $bp->signup->errors['signup_password'] = $password_error;
  86          }
  87  
  88          if ( bp_signup_requires_privacy_policy_acceptance() && ! empty( $_POST['signup-privacy-policy-check'] ) && empty( $_POST['signup-privacy-policy-accept'] ) ) {
  89              $bp->signup->errors['signup_privacy_policy'] = __( 'You must indicate that you have read and agreed to the Privacy Policy.', 'buddypress' );
  90          }
  91  
  92          $bp->signup->username = $_POST['signup_username'];
  93          $bp->signup->email = $_POST['signup_email'];
  94  
  95          // Now we've checked account details, we can check profile information.
  96          if ( bp_is_active( 'xprofile' ) ) {
  97  
  98              // Make sure hidden field is passed and populated.
  99              if ( isset( $_POST['signup_profile_field_ids'] ) && !empty( $_POST['signup_profile_field_ids'] ) ) {
 100  
 101                  // Let's compact any profile field info into an array.
 102                  $profile_field_ids = explode( ',', $_POST['signup_profile_field_ids'] );
 103  
 104                  // Loop through the posted fields formatting any datebox values then validate the field.
 105                  foreach ( (array) $profile_field_ids as $field_id ) {
 106                      bp_xprofile_maybe_format_datebox_post_data( $field_id );
 107  
 108                      // Trim post fields.
 109                      if ( isset( $_POST[ 'field_' . $field_id ] ) ) {
 110                          if ( is_array( $_POST[ 'field_' . $field_id ] ) ) {
 111                              $_POST[ 'field_' . $field_id ] = array_map( 'trim', $_POST[ 'field_' . $field_id ] );
 112                          } else {
 113                              $_POST[ 'field_' . $field_id ] = trim( $_POST[ 'field_' . $field_id ] );
 114                          }
 115                      }
 116  
 117                      // Create errors for required fields without values.
 118                      if ( xprofile_check_is_required_field( $field_id ) && empty( $_POST[ 'field_' . $field_id ] ) && ! bp_current_user_can( 'bp_moderate' ) )
 119                          $bp->signup->errors['field_' . $field_id] = __( 'This is a required field', 'buddypress' );
 120                  }
 121  
 122                  // This situation doesn't naturally occur so bounce to website root.
 123              } else {
 124                  bp_core_redirect( bp_get_root_domain() );
 125              }
 126          }
 127  
 128          // Finally, let's check the blog details, if the user wants a blog and blog creation is enabled.
 129          if ( isset( $_POST['signup_with_blog'] ) ) {
 130              $active_signup = bp_core_get_root_option( 'registration' );
 131  
 132              if ( 'blog' == $active_signup || 'all' == $active_signup ) {
 133                  $blog_details = bp_core_validate_blog_signup( $_POST['signup_blog_url'], $_POST['signup_blog_title'] );
 134  
 135                  // If there are errors with blog details, set them for display.
 136                  if ( !empty( $blog_details['errors']->errors['blogname'] ) )
 137                      $bp->signup->errors['signup_blog_url'] = $blog_details['errors']->errors['blogname'][0];
 138  
 139                  if ( !empty( $blog_details['errors']->errors['blog_title'] ) )
 140                      $bp->signup->errors['signup_blog_title'] = $blog_details['errors']->errors['blog_title'][0];
 141              }
 142          }
 143  
 144          /**
 145           * Fires after the validation of a new signup.
 146           *
 147           * @since 1.1.0
 148           */
 149          do_action( 'bp_signup_validate' );
 150  
 151          // Add any errors to the action for the field in the template for display.
 152          if ( !empty( $bp->signup->errors ) ) {
 153              foreach ( (array) $bp->signup->errors as $fieldname => $error_message ) {
 154                  /**
 155                   * Filters the error message in the loop.
 156                   *
 157                   * @since 1.5.0
 158                   *
 159                   * @param string $value Error message wrapped in html.
 160                   */
 161                  add_action( 'bp_' . $fieldname . '_errors', function() use ( $error_message ) {
 162                      echo apply_filters( 'bp_members_signup_error_message', "<div class=\"error\">" . $error_message . "</div>" );
 163                  } );
 164              }
 165          } else {
 166              $bp->signup->step = 'save-details';
 167  
 168              // No errors! Let's register those deets.
 169              $active_signup = bp_core_get_root_option( 'registration' );
 170  
 171              if ( 'none' != $active_signup ) {
 172  
 173                  // Make sure the extended profiles module is enabled.
 174                  if ( bp_is_active( 'xprofile' ) ) {
 175                      // Let's compact any profile field info into usermeta.
 176                      $profile_field_ids = explode( ',', $_POST['signup_profile_field_ids'] );
 177  
 178                      /*
 179                       * Loop through the posted fields, formatting any
 180                       * datebox values, then add to usermeta.
 181                       */
 182                      foreach ( (array) $profile_field_ids as $field_id ) {
 183                          bp_xprofile_maybe_format_datebox_post_data( $field_id );
 184  
 185                          if ( !empty( $_POST['field_' . $field_id] ) )
 186                              $usermeta['field_' . $field_id] = $_POST['field_' . $field_id];
 187  
 188                          if ( !empty( $_POST['field_' . $field_id . '_visibility'] ) )
 189                              $usermeta['field_' . $field_id . '_visibility'] = $_POST['field_' . $field_id . '_visibility'];
 190                      }
 191  
 192                      // Store the profile field ID's in usermeta.
 193                      $usermeta['profile_field_ids'] = $_POST['signup_profile_field_ids'];
 194                  }
 195  
 196                  // Hash and store the password.
 197                  $usermeta['password'] = wp_hash_password( $_POST['signup_password'] );
 198  
 199                  // If the user decided to create a blog, save those details to usermeta.
 200                  if ( 'blog' == $active_signup || 'all' == $active_signup )
 201                      $usermeta['public'] = ( isset( $_POST['signup_blog_privacy'] ) && 'public' == $_POST['signup_blog_privacy'] ) ? true : false;
 202  
 203                  /**
 204                   * Filters the user meta used for signup.
 205                   *
 206                   * @since 1.1.0
 207                   *
 208                   * @param array $usermeta Array of user meta to add to signup.
 209                   */
 210                  $usermeta = apply_filters( 'bp_signup_usermeta', $usermeta );
 211  
 212                  // Finally, sign up the user and/or blog.
 213                  if ( isset( $_POST['signup_with_blog'] ) && is_multisite() )
 214                      $wp_user_id = bp_core_signup_blog( $blog_details['domain'], $blog_details['path'], $blog_details['blog_title'], $_POST['signup_username'], $_POST['signup_email'], $usermeta );
 215                  else
 216                      $wp_user_id = bp_core_signup_user( $_POST['signup_username'], $_POST['signup_password'], $_POST['signup_email'], $usermeta );
 217  
 218                  if ( is_wp_error( $wp_user_id ) ) {
 219                      $bp->signup->step = 'request-details';
 220                      bp_core_add_message( $wp_user_id->get_error_message(), 'error' );
 221                  } else {
 222                      $bp->signup->step = 'completed-confirmation';
 223                  }
 224              }
 225  
 226              /**
 227               * Fires after the completion of a new signup.
 228               *
 229               * @since 1.1.0
 230               */
 231              do_action( 'bp_complete_signup' );
 232          }
 233  
 234      }
 235  
 236      /**
 237       * Fires right before the loading of the Member registration screen template file.
 238       *
 239       * @since 1.5.0
 240       */
 241      do_action( 'bp_core_screen_signup' );
 242  
 243      /**
 244       * Filters the template to load for the Member registration page screen.
 245       *
 246       * @since 1.5.0
 247       *
 248       * @param string $value Path to the Member registration template to load.
 249       */
 250      bp_core_load_template( apply_filters( 'bp_core_template_register', array( 'register', 'registration/register' ) ) );
 251  }
 252  add_action( 'bp_screens', 'bp_core_screen_signup' );


Generated: Wed Nov 25 01:01:39 2020 Cross-referenced by PHPXref 0.7.1