Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/23/2008 12:56:37 PM (17 years ago)
Author:
apeatling
Message:

Removed home base requirement

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-xprofile/bp-xprofile-signup.php

    r391 r394  
    8080 **************************************************************************/
    8181
    82 function xprofile_validate_signup_fields() {
     82function xprofile_validate_signup_fields( $result ) {
    8383    global $bp_xprofile_callback, $avatar_error, $avatar_error_msg, $has_errors;
    8484    global $canvas, $original;
    85 
     85    global $current_site, $active_signup;
     86   
    8687    if ( isset( $_POST['validate_custom'] ) ) {
    8788        // form has been submitted, let's validate the form
    8889        // using the built in Wordpress functions and our own.
    8990
    90         $active_signup = 'all';
    91         $active_signup = apply_filters( 'wpmu_active_signup', $active_signup ); // return "all", "none", "blog" or "user"
    92 
    93         $newblogname = isset( $_GET['new'] ) ? strtolower(preg_replace('/^-|-$|[^-a-zA-Z0-9]/', '', $_GET['new'])) : null;
    94         if ( $_POST['blog_public'] != 1 )
    95             $_POST['blog_public'] = 0;
    96 
    97         if ( $active_signup == "none" ) {
    98             _e( "Registration has been disabled." , 'buddypress');
     91        extract($result);
     92       
     93        $counter = 0;
     94        $has_errors = false;
     95        $prev_field_id = -1;
     96       
     97        // Validate all sign up fields
     98        $fields = BP_XProfile_Field::get_signup_fields();
     99        foreach ( $fields as $field ) {
     100           
     101            $value = $_POST['field_' . $field->id];
     102           
     103            // Need to check if the previous field had
     104            // the same ID, as to not validate individual
     105            // day/month/year dropdowns individually.
     106            if ( $prev_field_id != $field->id ) {
     107                $field = new BP_XProfile_Field($field->id);
     108               
     109                    if ( $field->type == "datebox" ) {
     110                        if ( $_POST['field_' . $field->id . '_day'] != "" && $_POST['field_' . $field->id . '_month'] != "" && $_POST['field_' . $field->id . '_year'] != "") {
     111                            $value = strtotime( $_POST['field_' . $field->id . '_day'] . " " .
     112                                                $_POST['field_' . $field->id . '_month'] . " " .
     113                                                $_POST['field_' . $field->id . '_year']);                               
     114                        }
     115                }
     116               
     117                if ( is_array($value) ) {
     118                    $value = serialize( $value );
     119                }
     120               
     121                $bp_xprofile_callback[$counter] = array(
     122                    "field_id" => $field->id,
     123                    "type" => $field->type,
     124                    "value" => $value
     125                );
     126               
     127                if ( $field->is_required && $value == '' ) {
     128                    $bp_xprofile_callback[$counter]["error_msg"] = $field->name . ' cannot be left blank.';
     129                    $has_errors = true;
     130                }
     131               
     132                $counter++;
     133            }
     134           
     135            $prev_field_id = $field->id;
     136        }
     137
     138        // validate the avatar upload if there is one.
     139        $avatar_error = false;
     140       
     141        if ( bp_core_check_avatar_upload($_FILES) ) {
     142            if ( !bp_core_check_avatar_upload($_FILES) ) {
     143                $avatar_error = true;
     144                $avatar_error_msg = __('Your avatar upload failed, please try again.', 'buddypress');
     145            }
     146
     147            if ( !bp_core_check_avatar_size($_FILES) ) {
     148                $avatar_error = true;
     149                $avatar_size = size_format(CORE_MAX_FILE_SIZE);
     150                $avatar_error_msg = sprintf( __('The file you uploaded is too big. Please upload a file under %s', 'buddypress'), $avatar_size);
     151            }
     152
     153            if ( !bp_core_check_avatar_type($_FILES) ) {
     154                $avatar_error = true;
     155                $avatar_error_msg = __('Please upload only JPG, GIF or PNG photos.', 'buddypress');     
     156            }
     157
     158            // "Handle" upload into temporary location
     159            if ( !$original = bp_core_handle_avatar_upload($_FILES) ) {
     160                $avatar_error = true;
     161                $avatar_error_msg = __('Upload Failed! Your photo dimensions are likely too big.', 'buddypress');                       
     162            }
     163
     164            if ( !bp_core_check_avatar_dimensions($original) ) {
     165                $avatar_error = true;
     166                $avatar_error_msg = sprintf( __('The image you upload must have dimensions of %d x %d pixels or larger.', 'buddypress'), CORE_AVATAR_V2_W, CORE_AVATAR_V2_W );
     167            }
     168           
     169            if ( !$canvas = bp_core_resize_avatar($original) )
     170                $canvas = $original;
     171        }
     172       
     173        if ( !$has_errors && !$avatar_error ) {
     174            $public = (int) $_POST['blog_public'];
     175           
     176            // put the user profile meta in a session ready to store.
     177            for ( $i = 0; $i < count($bp_xprofile_callback); $i++ ) {
     178                $meta['field_' . $bp_xprofile_callback[$i]['field_id']] .= $bp_xprofile_callback[$i]['value'];
     179            }
     180
     181            $meta['xprofile_field_ids'] = $_POST['xprofile_ids'];
     182            $meta['avatar_image_resized'] = $canvas;
     183            $meta['avatar_image_original'] = $original;
     184            $meta['public'] = $public;
     185            $meta['lang_id'] = 1;
     186           
     187            $_SESSION['xprofile_meta'] = $meta;
    99188        } else {
    100             if ( $active_signup == 'all' || $active_signup == "blog" ) {
    101                 $_POST['blog_id'] = $_POST['user_name'];
    102                 $_POST['blog_title'] = $_POST['field_1'] . " " . $_POST['field_2']; // The core name fields.
    103 
    104                 $counter = 0;
    105                 $has_errors = false;
    106                 $prev_field_id = -1;
    107                
    108                 // Validate all sign up fields
    109                 $fields = BP_XProfile_Field::get_signup_fields();
    110                 foreach ( $fields as $field ) {
    111                    
    112                     $value = $_POST['field_' . $field->id];
    113                    
    114                     // Need to check if the previous field had
    115                     // the same ID, as to not validate individual
    116                     // day/month/year dropdowns individually.
    117                     if ( $prev_field_id != $field->id ) {
    118                         $field = new BP_XProfile_Field($field->id);
    119                        
    120                         if ( $field->type == "datebox" ) {
    121                             if ( $_POST['field_' . $field->id . '_day'] != "" && $_POST['field_' . $field->id . '_month'] != "" && $_POST['field_' . $field->id . '_year'] != "") {
    122                                 $value = strtotime( $_POST['field_' . $field->id . '_day'] . " " .
    123                                                     $_POST['field_' . $field->id . '_month'] . " " .
    124                                                     $_POST['field_' . $field->id . '_year']);                               
    125                             }
    126                         }
    127                        
    128                         if ( is_array($value) ) {
    129                             $value = serialize( $value );
    130                         }
    131                        
    132                         $bp_xprofile_callback[$counter] = array(
    133                             "field_id" => $field->id,
    134                             "type" => $field->type,
    135                             "value" => $value
    136                         );
    137                        
    138                         if ( $field->is_required && $value == '' ) {
    139                             $bp_xprofile_callback[$counter]["error_msg"] = $field->name . ' cannot be left blank.';
    140                             $has_errors = true;
    141                         }
    142                        
    143                         $counter++;
    144                     }
    145                    
    146                     $prev_field_id = $field->id;
    147                 }
    148 
    149                 // validate the avatar upload if there is one.
    150                 $avatar_error = false;
    151                
    152                 if ( bp_core_check_avatar_upload($_FILES) ) {
    153                     if ( !bp_core_check_avatar_upload($_FILES) ) {
    154                         $avatar_error = true;
    155                         $avatar_error_msg = __('Your avatar upload failed, please try again.', 'buddypress');
    156                     }
    157 
    158                     if ( !bp_core_check_avatar_size($_FILES) ) {
    159                         $avatar_error = true;
    160                         $avatar_size = size_format(1024 * CORE_MAX_FILE_SIZE);
    161                         $avatar_error_msg = sprintf( __('The file you uploaded is too big. Please upload a file under %d', 'buddypress'), $avatar_size);
    162                     }
    163 
    164                     if ( !bp_core_check_avatar_type($_FILES) ) {
    165                         $avatar_error = true;
    166                         $avatar_error_msg = __('Please upload only JPG, GIF or PNG photos.', 'buddypress');     
    167                     }
    168 
    169                     // "Handle" upload into temporary location
    170                     if ( !$original = bp_core_handle_avatar_upload($_FILES) ) {
    171                         $avatar_error = true;
    172                         $avatar_error_msg = __('Upload Failed! Your photo dimensions are likely too big.', 'buddypress');                       
    173                     }
    174 
    175                     if ( !bp_core_check_avatar_dimensions($original) ) {
    176                         $avatar_error = true;
    177                         $avatar_error_msg = sprintf( __('The image you upload must have dimensions of %d x %d pixels or larger.', 'buddypress'), CORE_AVATAR_V2_W, CORE_AVATAR_V2_W );
    178                     }
    179                    
    180                     if ( !$canvas = bp_core_resize_avatar($original) )
    181                         $canvas = $original;
    182                 }
    183                
    184                 if ( !is_user_logged_in() ) {
    185                     $result = wpmu_validate_user_signup( $_POST['user_name'], $_POST['user_email'] );
    186                     extract($result);
    187                
    188                     if ( $errors->get_error_code() || $has_errors || $avatar_error ) {
    189                         signup_user($user_name, $user_email, $errors);
    190                    
    191                         echo '</div>';
    192                         get_footer();
    193                         die;
    194                     }
    195                 }
    196                
    197                 if ( !$has_errors ) {
    198                     if ( !is_user_logged_in() ) {
    199                         // This is a new user signing up, not an existing user creating a home base.
    200                         $result = wpmu_validate_blog_signup( $_POST['blog_id'], $_POST['blog_title'] );
    201                         extract($result);
    202 
    203                         if ( $errors->get_error_code() ) {
    204                             signup_user( $user_name, $user_email, $errors );
    205                             return;
    206                         }
    207                        
    208                         $public = (int) $_POST['blog_public'];
    209                         $meta = array( 'lang_id' => 1, 'public' => $public );
    210 
    211                         for ( $i = 0; $i < count($bp_xprofile_callback); $i++ ) {
    212                             $meta['field_' . $bp_xprofile_callback[$i]['field_id']] .= $bp_xprofile_callback[$i]['value'];
    213                         }
    214 
    215                         $meta['xprofile_field_ids'] = $_POST['xprofile_ids'];
    216                         $meta['avatar_image_resized'] = $canvas;
    217                         $meta['avatar_image_original'] = $original;
    218 
    219                         $meta = apply_filters( "add_signup_meta", $meta );
    220 
    221                         wpmu_signup_blog( $domain, $path, $blog_title, $user_name, $user_email, $meta );
    222                         confirm_blog_signup( $domain, $path, $blog_title, $user_name, $user_email, $meta );
    223 
    224                         echo '</div></div>';
    225                         get_footer();
    226                         die;
    227                     } else {
    228                         bp_core_validate_homebase_form_secondary();
    229                     }
    230                 } else {
    231                     if ( !is_user_logged_in() ) {
    232                         signup_user( $user_name, $user_email, $errors );
    233                    
    234                         echo '</div>';
    235                         get_footer();
    236                         die;
    237                     } else {
    238                         bp_core_validate_homebase_form_secondary( $user_name, $user_email, $errors );
    239                     }
    240                 }
    241 
    242             } else {
    243                 _e( "Registration has been disabled." , 'buddypress');
    244             }
    245         }
    246     }   
    247 }
    248 
    249 add_action( 'preprocess_signup_form', 'xprofile_validate_signup_fields' );
    250 
     189            $errors->add( 'bp_xprofile_errors', '' );
     190        }
     191    }
     192   
     193    return array('user_name' => $user_name, 'user_email' => $user_email, 'errors' => $errors);
     194}
     195add_filter( 'wpmu_validate_user_signup', 'xprofile_validate_signup_fields', 10, 1 );
     196
     197
     198function xprofile_add_profile_meta( $meta ) {
     199    return $_SESSION['xprofile_meta'];
     200}
     201add_filter( 'add_signup_meta', 'xprofile_add_profile_meta' );
    251202
    252203/**************************************************************************
     
    258209
    259210function xprofile_hidden_signup_fields() {
    260     global $active_signup;
    261    
    262     // Override the stage variable so we can redirect the validation of the form
    263     // to our own custom validation function.
    264    
    265     if( !is_user_logged_in() ) {
    266     ?>
    267     <input type="hidden" name="stage" value="" />
    268     <?php } ?>
    269     <input type="hidden" name="validate_custom" value="1" />
    270     <?php
    271    
    272     if ( $active_signup != 'none' )
    273         $active_signup = 'blog';
     211    ?><input type="hidden" name="validate_custom" value="1" /><?php
    274212}
    275213add_action( 'signup_hidden_fields', 'xprofile_hidden_signup_fields' );
     
    277215
    278216/**************************************************************************
    279  xprofile_on_activate()
     217 xprofile_on_activate_user()
    280218 
    281219 When a user activates their account, move the extra field data to the
    282  correct tables, and then remove the WP options table entries.
     220 correct tables.
    283221 **************************************************************************/
    284222
    285 function xprofile_on_activate( $blog_id = null, $user_id = null ) {
    286     global $wpdb, $profile_picture_path;
    287    
    288     if ( WP_INSTALLING )
    289         return false;
    290    
    291     /* Only do this if this is a new user, and not a user creating a home base */
    292     if ( !is_user_logged_in() ) {
    293 
    294         // Extract signup meta fields to fill out profile
    295         $field_ids = get_blog_option( $blog_id, 'xprofile_field_ids' );
    296         $field_ids = explode( ",", $field_ids );
    297            
    298         // Get the new user ID.
    299         $sql = "SELECT u.ID from " . $wpdb->base_prefix . "users u,
    300                 " . $wpdb->base_prefix . "usermeta um
    301                 WHERE u.ID = um.user_id
    302                 AND um.meta_key = 'primary_blog'
    303                 AND um.meta_value = " . $blog_id;
    304 
    305         $user_id = $wpdb->get_var($sql);
    306 
    307         // Loop through each bit of profile data and save it to profile.
    308         for ( $i = 0; $i < count($field_ids); $i++ ) {
    309             $field_value = get_blog_option( $blog_id, 'field_' . $field_ids[$i] );
    310        
    311             $field               = new BP_XProfile_ProfileData();
    312             $field->user_id      = $user_id;
    313             $field->value        = $field_value;
    314             $field->field_id     = $field_ids[$i];
    315             $field->last_updated = time(); 
    316 
    317             $field->save();
    318             delete_blog_option( $blog_id, 'field_' . $field_ids[$i] );
    319         }
    320         delete_blog_option( $blog_id, 'xprofile_field_ids' );
    321        
    322         /* Make this blog the "home base" for the new user */
    323         update_usermeta( $user_id, 'home_base', $blog_id );
    324         update_usermeta( $user_id, 'last_activity', time() );
    325        
    326         /* Set the BuddyPress theme as the theme for this blog */
    327         $wpdb->set_blog_id($blog_id);       
    328         switch_theme( 'buddypress', 'buddypress' );
    329        
    330         // move and set the avatar if one has been provided.
    331         $resized = get_blog_option( $blog_id, 'avatar_image_resized' );
    332         $original = get_blog_option( $blog_id, 'avatar_image_original' );   
    333    
    334         if ( $resized && $original ) {
    335             $upload_dir = bp_upload_dir(NULL, $blog_id);
    336 
    337             if ( $upload_dir ) {
    338                 $resized_strip_path = explode( '/', $resized );
    339                 $original_strip_path = explode( '/', $original );
    340 
    341                 $resized_filename = $resized_strip_path[count($resized_strip_path) - 1];
    342                 $original_filename = $original_strip_path[count($original_strip_path) - 1];
    343 
    344                 $resized_new = $upload_dir['path'] . '/' . $resized_filename;
    345                 $original_new = $upload_dir['path'] . '/' . $original_filename;
    346 
    347                 @copy( $resized, $resized_new );
    348                 @copy( $original, $original_new );
    349 
    350                 @unlink($resized);
    351                 @unlink($original);
    352 
    353                 $resized = $resized_new;
    354                 $original = $original_new;
    355             }
    356        
    357             // Render the cropper UI
    358             $action = get_blog_option( $blog_id, 'siteurl' ) . '/wp-activate.php?key=' . $_GET['key'] . '&amp;cropped=true';
    359             bp_core_render_avatar_cropper($original, $resized, $action, $user_id);
    360         }
    361     }
    362    
    363 }
    364 add_action( 'wpmu_new_blog', 'xprofile_on_activate' );
    365 
     223function xprofile_on_activate_blog( $blog_id, $user_id, $password, $title, $meta ) {
     224    xprofile_extract_signup_meta( $user_id, $meta );
     225   
     226    // move and set the avatar if one has been provided.
     227    xprofile_handle_signup_avatar( $user_id, $meta );
     228}
     229add_action( 'wpmu_activate_blog', 'xprofile_on_activate_blog', 1, 5 );
     230
     231
     232function xprofile_on_activate_user( $user_id, $password, $meta ) {
     233    xprofile_extract_signup_meta( $user_id, $meta );
     234
     235    // move and set the avatar if one has been provided.
     236    xprofile_handle_signup_avatar( $user_id, $meta );
     237}
     238add_action( 'wpmu_activate_user', 'xprofile_on_activate_user', 1, 3 );
     239
     240
     241function xprofile_extract_signup_meta( $user_id, $meta ) {
     242    // Extract signup meta fields to fill out profile
     243    $field_ids = $meta['xprofile_field_ids'];
     244    $field_ids = explode( ',', $field_ids );
     245
     246    // Loop through each bit of profile data and save it to profile.
     247    for ( $i = 0; $i < count($field_ids); $i++ ) {
     248        if ( $field_ids[$i] == '' ) continue;
     249       
     250        $field_value = $meta["field_{$field_ids[$i]}"];
     251       
     252        $field               = new BP_XProfile_ProfileData();
     253        $field->user_id      = $user_id;
     254        $field->value        = $field_value;
     255        $field->field_id     = $field_ids[$i];
     256        $field->last_updated = time(); 
     257
     258        $field->save();
     259    }
     260
     261    update_usermeta( $user_id, 'last_activity', time() );
     262}
     263
     264function xprofile_handle_signup_avatar( $user_id, $meta ) {
     265    $resized = $meta['avatar_image_resized'];
     266    $original = $meta['avatar_image_original'];
     267   
     268    if ( !empty($resized) && !empty($original) ) {
     269        $upload_dir = bp_avatar_upload_dir( $user_id );
     270       
     271        if ( $upload_dir ) {
     272            $resized_strip_path = explode( '/', $resized );
     273            $original_strip_path = explode( '/', $original );
     274
     275            $resized_filename = $resized_strip_path[count($resized_strip_path) - 1];
     276            $original_filename = $original_strip_path[count($original_strip_path) - 1];
     277
     278            $resized_new = $upload_dir['path'] . '/' . $resized_filename;
     279            $original_new = $upload_dir['path'] . '/' . $original_filename;
     280
     281            @copy( $resized, $resized_new );
     282            @copy( $original, $original_new );
     283
     284            @unlink($resized);
     285            @unlink($original);
     286
     287            $resized = $resized_new;
     288            $original = $original_new;
     289        }
     290   
     291        // Render the cropper UI
     292        $action = site_url() . '/wp-activate.php?key=' . $_GET['key'] . '&amp;cropped=true';
     293        bp_core_render_avatar_cropper($original, $resized, $action, $user_id);
     294    }
     295}
    366296
    367297function xprofile_catch_activate_crop() {
     
    381311        }
    382312       
    383         $blog_id = get_usermeta( $user_id, 'home_base' );
    384         $url = get_blog_option( $blog_id, 'siteurl' );
     313        $ud = get_userdata($user_id);
     314        $url = site_url() . '/members/' . $ud->user_login;
    385315       
    386316        wp_redirect( $url );
     
    404334
    405335
    406 // function xprofile_replace_blog_references() {
    407 //  if ( strpos( $_SERVER['SCRIPT_NAME'], 'wp-signup.php' ) ) {
    408 //      add_action( 'wp_head', 'xprofile_start_blog_reference_replacement' );
    409 //  }   
    410 // }
    411 // add_action( 'wp', 'xprofile_replace_blog_references' );
    412 
    413 // function xprofile_start_blog_reference_replacement( $contents ) {   
    414 //  ob_start();
    415 //  add_action('wp_footer', 'xprofile_end_blog_reference_replacement');
    416 // }
    417 //
    418 // function xprofile_blog_reference_replacement( $contents ) {
    419 //  echo str_replace( 'blog', 'account', $contents );
    420 // }
    421 //
    422 // function xprofile_end_blog_reference_replacement() {
    423 //  $contents = ob_get_contents();
    424 //  ob_end_clean();
    425 //  xprofile_blog_reference_replacement($contents);
    426 // }
    427 
    428 
    429 
    430336?>
Note: See TracChangeset for help on using the changeset viewer.