Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.62 KB | None | 0 0
  1. /**
  2.  * registration for digital sheet
  3.  */
  4.  
  5. // registration form shortcode
  6. function itamar_ds_registration_form($atts) {
  7.    
  8.     $atts = shortcode_atts(
  9.         array(
  10.             'ccap' => '',
  11.             'link' => '',
  12.             'name' => '',
  13.         ), $atts, 'ds_form' );
  14.  
  15.    
  16.     // only show the registration form to non-logged-in members
  17.     if(!is_user_logged_in()) {
  18.  
  19.         // check to make sure user registration is enabled
  20.         $registration_enabled = get_option('users_can_register');
  21.  
  22.         // only show the registration form if allowed
  23.         if($registration_enabled) {
  24.            
  25.             $output = "<div class='ds-form'>";
  26.                 $output .= "<form action='' method='POST' id='form--" . $ccap . "' class='spu-disable-ajax'>";
  27.                 $output .= "<fieldset>";
  28.            
  29.                     $output .= "<label for='ds_name' class='blind'>השם שלך</label>";
  30.                     $output .= "<input name='ds_name' type='text' placeholder='השם שלך' required/>";
  31.              
  32.                     $output .= "<label for='ds_email' class='blind'>כתובת אימייל</label>";
  33.                     $output .= "<input name='ds_email' type='email' placeholder='כתובת אימייל' required/>";
  34.              
  35.                     $output .= "<label for='ds_phone' class='blind'>מספר טלפון</label>";
  36.                     $output .= "<input name='ds_phone' type='tel' placeholder='מספר טלפון' pattern='^0\d([\d]{0,1})([-]{0,1})\d{7}$' required/>";
  37.              
  38.                     $output .= "<input type='hidden' name='ds_register_nonce' value='" . wp_create_nonce('ds-register-nonce') . "'/>";
  39.              
  40.                     $output .= "<input type='hidden' name='ds_ccap' value='access_s2member_ccap_" . esc_html__($atts['ccap']) . "'/>";
  41.                    
  42.                     $output .= "<input type='hidden' name='ds_link' value='" . esc_html__($atts['link']) . "'/>";
  43.                    
  44.                     $output .= "<input type='hidden' name='ds_subject_name' value='" . esc_html__($atts['name']) . "'/>";
  45.              
  46.                     $output .= "<input type='submit' value='קדימה!'/>";
  47.             $output .= "</fieldset>";
  48.           $output .= "</form>";
  49.         $output .= "</div>";
  50.         } else {
  51.             $output = "שגיאה: רישום משתמשים סגור";
  52.         }
  53.         return $output;
  54.     }
  55. }
  56. add_shortcode('ds_form', 'itamar_ds_registration_form');
  57.  
  58. // register a new user
  59. function itamar_add_new_member() {
  60.     if (isset( $_POST["ds_email"] ) && wp_verify_nonce($_POST['ds_register_nonce'], 'ds-register-nonce')) {
  61.         $user_name      = $_POST["ds_name"];
  62.         $user_email     = $_POST["ds_email"];
  63.         $user_phone     = $_POST["ds_phone"];
  64.         $user_ccap      = $_POST["ds_ccap"];
  65.         $link           = $_POST["ds_link"];
  66.         $subject_name   = $_POST["ds_subject_name"];
  67.         $user_pass      = wp_generate_password(8);
  68.        
  69.         // this is required for username checks
  70.         require_once(ABSPATH . WPINC . '/registration.php');
  71.        
  72.        
  73.         if(!is_email($user_email)) {
  74.             //invalid email
  75.             itamar_errors()->add('email_invalid','אימייל לא תקין');
  76.             $error_message = 1;
  77.         }
  78.         if($user_phone == '') {
  79.             // empty phone
  80.             itamar_errors()->add('password_empty', 'יש להזין מספר טלפון');
  81.             $error_message = 3;
  82.         }      
  83.         if($user_ccap == '' || !isset($user_ccap)) {
  84.             // error with shortcode
  85.             itamar_errors()->add('general_error', 'שגיאה כללית. נא לפנות למנהל האתר.');
  86.             $error_message = 4;
  87.         }
  88.         if(email_exists($user_email)) {
  89.             //Email address already registered
  90.             $user = get_user_by( 'email', $user_email );
  91.             if( !user_can( $user, 'access_s2member_level0' ) ){
  92.                 itamar_errors()->add('email_used_ok', 'אין שגיאה');
  93.                 $error_message = 5;
  94.                 $user->add_role( 'subscriber' );
  95.                 $user->add_cap( $user_ccap );
  96.                 update_user_meta( $user->ID, 'billing_phone', sanitize_text_field( $user_phone ) );
  97.                 wp_redirect( $link . '?link=' . $link . '&message=error&errorm=' . $error_message ); exit;
  98.             }else{
  99.                 itamar_errors()->add('email_used', 'האימייל נמצא בשימוש, יכול להיות שיש לך כבר מנוי או כניסה חינם?');
  100.                 $error_message = 2;
  101.             }
  102.         }
  103.        
  104.         $errors = itamar_errors()->get_error_messages();
  105.        
  106.        
  107.         // only create the user in if there are no errors
  108.         if(empty($errors)) {
  109.            
  110.             $new_user_id = wp_insert_user(array(
  111.                     'user_login'        => $user_email,
  112.                     'user_pass'         => $user_pass,
  113.                     'user_email'        => $user_email,
  114.                     'first_name'        => $user_name,
  115.                     'user_registered'   => date('Y-m-d H:i:s'),
  116.                     'role'              => 'subscriber',
  117.                     'description'           => sanitize_text_field( $user_phone ), //add phone number as description for Zapier sync purposes                  
  118.                 )
  119.             );
  120.             if($new_user_id) {
  121.                 // add custom capability to new added user
  122.                 $user = new WP_User( $new_user_id );
  123.                 $user->add_cap( $user_ccap );
  124.                
  125.                 update_user_meta( $new_user_id, 'billing_phone', sanitize_text_field( $user_phone ) );
  126.                
  127.                 // send an email to the admin alerting them of the registration
  128.                 itamar_new_user_notification($link, $user_name, $user_email, $user_phone, $subject_name, $user_pass);
  129.                
  130.                 // send the newly created user to the provided link after logging them in
  131.                 /*
  132.                 $output = "<h3>מעולה! נפתחו בפניך מגוון כתבות חדשות בנושא " . $subject_name . "</h3>";
  133.                 $output .= "<p><a class='call-to-action' title='רשימת הכתבות שנפתחו' href='" . $link . "'>כל הכתבות</a></p>";
  134.                 $output .= "<p><small>שם משתמש וסיסמה נשלחו אליך לאימייל וישמשו אותך להתחברות בעתיד</small></p>";
  135.                
  136.                 return $output;
  137.                 */
  138.                 // send the newly created user to a welcoming
  139.                 wp_redirect( $link . '?link=' . $link . '&message=welcome' ); exit;
  140.             }
  141.            
  142.         }else{
  143.             wp_redirect( $link . '?link=' . $link . '&message=error&errorm=' . $error_message ); exit;
  144.         }
  145.    
  146.     }
  147. }
  148. add_action('init', 'itamar_add_new_member');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement