Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * registration for digital sheet
- */
- // registration form shortcode
- function itamar_ds_registration_form($atts) {
- $atts = shortcode_atts(
- array(
- 'ccap' => '',
- 'link' => '',
- 'name' => '',
- ), $atts, 'ds_form' );
- // only show the registration form to non-logged-in members
- if(!is_user_logged_in()) {
- // check to make sure user registration is enabled
- $registration_enabled = get_option('users_can_register');
- // only show the registration form if allowed
- if($registration_enabled) {
- $output = "<div class='ds-form'>";
- $output .= "<form action='' method='POST' id='form--" . $ccap . "' class='spu-disable-ajax'>";
- $output .= "<fieldset>";
- $output .= "<label for='ds_name' class='blind'>השם שלך</label>";
- $output .= "<input name='ds_name' type='text' placeholder='השם שלך' required/>";
- $output .= "<label for='ds_email' class='blind'>כתובת אימייל</label>";
- $output .= "<input name='ds_email' type='email' placeholder='כתובת אימייל' required/>";
- $output .= "<label for='ds_phone' class='blind'>מספר טלפון</label>";
- $output .= "<input name='ds_phone' type='tel' placeholder='מספר טלפון' pattern='^0\d([\d]{0,1})([-]{0,1})\d{7}$' required/>";
- $output .= "<input type='hidden' name='ds_register_nonce' value='" . wp_create_nonce('ds-register-nonce') . "'/>";
- $output .= "<input type='hidden' name='ds_ccap' value='access_s2member_ccap_" . esc_html__($atts['ccap']) . "'/>";
- $output .= "<input type='hidden' name='ds_link' value='" . esc_html__($atts['link']) . "'/>";
- $output .= "<input type='hidden' name='ds_subject_name' value='" . esc_html__($atts['name']) . "'/>";
- $output .= "<input type='submit' value='קדימה!'/>";
- $output .= "</fieldset>";
- $output .= "</form>";
- $output .= "</div>";
- } else {
- $output = "שגיאה: רישום משתמשים סגור";
- }
- return $output;
- }
- }
- add_shortcode('ds_form', 'itamar_ds_registration_form');
- // register a new user
- function itamar_add_new_member() {
- if (isset( $_POST["ds_email"] ) && wp_verify_nonce($_POST['ds_register_nonce'], 'ds-register-nonce')) {
- $user_name = $_POST["ds_name"];
- $user_email = $_POST["ds_email"];
- $user_phone = $_POST["ds_phone"];
- $user_ccap = $_POST["ds_ccap"];
- $link = $_POST["ds_link"];
- $subject_name = $_POST["ds_subject_name"];
- $user_pass = wp_generate_password(8);
- // this is required for username checks
- require_once(ABSPATH . WPINC . '/registration.php');
- if(!is_email($user_email)) {
- //invalid email
- itamar_errors()->add('email_invalid','אימייל לא תקין');
- $error_message = 1;
- }
- if($user_phone == '') {
- // empty phone
- itamar_errors()->add('password_empty', 'יש להזין מספר טלפון');
- $error_message = 3;
- }
- if($user_ccap == '' || !isset($user_ccap)) {
- // error with shortcode
- itamar_errors()->add('general_error', 'שגיאה כללית. נא לפנות למנהל האתר.');
- $error_message = 4;
- }
- if(email_exists($user_email)) {
- //Email address already registered
- $user = get_user_by( 'email', $user_email );
- if( !user_can( $user, 'access_s2member_level0' ) ){
- itamar_errors()->add('email_used_ok', 'אין שגיאה');
- $error_message = 5;
- $user->add_role( 'subscriber' );
- $user->add_cap( $user_ccap );
- update_user_meta( $user->ID, 'billing_phone', sanitize_text_field( $user_phone ) );
- wp_redirect( $link . '?link=' . $link . '&message=error&errorm=' . $error_message ); exit;
- }else{
- itamar_errors()->add('email_used', 'האימייל נמצא בשימוש, יכול להיות שיש לך כבר מנוי או כניסה חינם?');
- $error_message = 2;
- }
- }
- $errors = itamar_errors()->get_error_messages();
- // only create the user in if there are no errors
- if(empty($errors)) {
- $new_user_id = wp_insert_user(array(
- 'user_login' => $user_email,
- 'user_pass' => $user_pass,
- 'user_email' => $user_email,
- 'first_name' => $user_name,
- 'user_registered' => date('Y-m-d H:i:s'),
- 'role' => 'subscriber',
- 'description' => sanitize_text_field( $user_phone ), //add phone number as description for Zapier sync purposes
- )
- );
- if($new_user_id) {
- // add custom capability to new added user
- $user = new WP_User( $new_user_id );
- $user->add_cap( $user_ccap );
- update_user_meta( $new_user_id, 'billing_phone', sanitize_text_field( $user_phone ) );
- // send an email to the admin alerting them of the registration
- itamar_new_user_notification($link, $user_name, $user_email, $user_phone, $subject_name, $user_pass);
- // send the newly created user to the provided link after logging them in
- /*
- $output = "<h3>מעולה! נפתחו בפניך מגוון כתבות חדשות בנושא " . $subject_name . "</h3>";
- $output .= "<p><a class='call-to-action' title='רשימת הכתבות שנפתחו' href='" . $link . "'>כל הכתבות</a></p>";
- $output .= "<p><small>שם משתמש וסיסמה נשלחו אליך לאימייל וישמשו אותך להתחברות בעתיד</small></p>";
- return $output;
- */
- // send the newly created user to a welcoming
- wp_redirect( $link . '?link=' . $link . '&message=welcome' ); exit;
- }
- }else{
- wp_redirect( $link . '?link=' . $link . '&message=error&errorm=' . $error_message ); exit;
- }
- }
- }
- add_action('init', 'itamar_add_new_member');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement