assets
1 year ago
messages
1 year ago
register-user-action.php
1 year ago
register-user.php
1 year ago
register-user-action.php
268 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JFB_Modules\Actions_V2\Register_User; |
| 4 | |
| 5 | use Jet_Form_Builder\Actions\Action_Handler; |
| 6 | use JFB_Modules\Actions_V2\Update_User\Properties\User_Meta_Property; |
| 7 | use Jet_Form_Builder\Actions\Types\Base; |
| 8 | use Jet_Form_Builder\Classes\Tools; |
| 9 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 10 | |
| 11 | /** |
| 12 | * Define Base_Type class |
| 13 | */ |
| 14 | class Register_User_Action extends Base { |
| 15 | |
| 16 | public function get_name() { |
| 17 | return __( 'Register User', 'jet-form-builder' ); |
| 18 | } |
| 19 | |
| 20 | public function get_id() { |
| 21 | return 'register_user'; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @throws Action_Exception |
| 26 | */ |
| 27 | public function can_register() { |
| 28 | $allow_register = $this->settings['allow_register'] ?? false; |
| 29 | $role_can_register = $this->settings['role_can_register'] ?? false; |
| 30 | |
| 31 | if ( $allow_register && ! $role_can_register ) { |
| 32 | throw new Action_Exception( 'failed', '`role_can_register` is empty' ); |
| 33 | } |
| 34 | |
| 35 | if ( is_user_logged_in() ) { |
| 36 | $user = wp_get_current_user(); |
| 37 | |
| 38 | if ( $allow_register && ! in_array( $role_can_register, $user->roles, true ) ) { |
| 39 | throw new Action_Exception( 'not_enough_cap' ); |
| 40 | } |
| 41 | |
| 42 | if ( ! $allow_register ) { |
| 43 | if ( isset( $this->settings['add_user_id'] ) && $this->settings['add_user_id'] ) { |
| 44 | jet_fb_action_handler()->response_data['user_id'] = (int) $user->ID; |
| 45 | jet_fb_context()->update_request( $user->ID, 'user_id' ); |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | throw new Action_Exception( 'already_logged_in' ); |
| 50 | } |
| 51 | } elseif ( $allow_register ) { |
| 52 | throw new Action_Exception( 'not_logged_in' ); |
| 53 | } |
| 54 | |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param array $request |
| 60 | * @param Action_Handler $handler |
| 61 | * |
| 62 | * @return mixed|void |
| 63 | * @throws Action_Exception |
| 64 | */ |
| 65 | public function do_action( array $request, Action_Handler $handler ) { |
| 66 | if ( ! $this->can_register() ) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | $fields_map = ! empty( $this->settings['fields_map'] ) ? $this->settings['fields_map'] : array(); |
| 71 | |
| 72 | // Prepare fields |
| 73 | $username = false; |
| 74 | $email = false; |
| 75 | $password = false; |
| 76 | $fname = false; |
| 77 | $lname = false; |
| 78 | $user_url = false; |
| 79 | $remember_field = $this->settings['remember_me_field'] ?? ''; |
| 80 | $is_remember = ! empty( $request[ $remember_field ] ); |
| 81 | |
| 82 | // If fields map for login, password or email is not set - abort but allow submit form (its not user fault) |
| 83 | if ( empty( $fields_map['login'] ) || empty( $fields_map['email'] ) || empty( $fields_map['password'] ) ) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Validate username |
| 89 | */ |
| 90 | $raw_username = ! empty( $request[ $fields_map['login'] ] ) ? $request[ $fields_map['login'] ] : false; |
| 91 | |
| 92 | if ( ! $raw_username ) { |
| 93 | throw new Action_Exception( 'empty_username' ); |
| 94 | } |
| 95 | |
| 96 | $username = sanitize_user( $raw_username ); |
| 97 | |
| 98 | if ( $username !== $raw_username ) { |
| 99 | throw new Action_Exception( 'sanitize_user' ); |
| 100 | } |
| 101 | |
| 102 | if ( username_exists( $username ) ) { |
| 103 | throw new Action_Exception( 'username_exists' ); |
| 104 | } |
| 105 | // username - ok |
| 106 | |
| 107 | /** |
| 108 | * Validate email |
| 109 | */ |
| 110 | $raw_email = ! empty( $request[ $fields_map['email'] ] ) ? $request[ $fields_map['email'] ] : false; |
| 111 | |
| 112 | if ( ! $raw_email ) { |
| 113 | throw new Action_Exception( 'empty_email' ); |
| 114 | } |
| 115 | |
| 116 | $email = sanitize_email( $raw_email ); |
| 117 | |
| 118 | if ( $email !== $raw_email ) { |
| 119 | throw new Action_Exception( 'empty_email' ); |
| 120 | } |
| 121 | |
| 122 | if ( email_exists( $email ) ) { |
| 123 | |
| 124 | throw new Action_Exception( 'email_exists' ); |
| 125 | } |
| 126 | // email - ok |
| 127 | |
| 128 | /** |
| 129 | * Validate password |
| 130 | */ |
| 131 | $password = ! empty( $request[ $fields_map['password'] ] ) ? $request[ $fields_map['password'] ] : false; |
| 132 | |
| 133 | if ( ! $password ) { |
| 134 | throw new Action_Exception( 'empty_password' ); |
| 135 | } |
| 136 | |
| 137 | if ( ! empty( $fields_map['confirm_password'] ) ) { |
| 138 | |
| 139 | if ( Tools::is_wp_password_hash( $password ) ) { |
| 140 | $confirm_password = $password; |
| 141 | } else { |
| 142 | $confirm_password = ! empty( $request[ $fields_map['confirm_password'] ] ) ? $request[ $fields_map['confirm_password'] ] : false; |
| 143 | |
| 144 | if ( $confirm_password !== $password ) { |
| 145 | throw new Action_Exception( 'password_mismatch' ); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | // password - ok |
| 150 | |
| 151 | if ( ! empty( $fields_map['first_name'] ) ) { |
| 152 | $fname = ! empty( $request[ $fields_map['first_name'] ] ) ? $request[ $fields_map['first_name'] ] : false; |
| 153 | } |
| 154 | |
| 155 | if ( ! empty( $fields_map['last_name'] ) ) { |
| 156 | $lname = ! empty( $request[ $fields_map['last_name'] ] ) ? $request[ $fields_map['last_name'] ] : false; |
| 157 | } |
| 158 | |
| 159 | if ( ! empty( $fields_map['user_url'] ) ) { |
| 160 | $user_url = ! empty( $request[ $fields_map['user_url'] ] ) ? $request[ $fields_map['user_url'] ] : false; |
| 161 | } |
| 162 | |
| 163 | $metafields_map = ! empty( $this->settings['meta_fields_map'] ) ? $this->settings['meta_fields_map'] : array(); |
| 164 | $metadata = array(); |
| 165 | |
| 166 | if ( ! empty( $metafields_map ) ) { |
| 167 | foreach ( $metafields_map as $form_field => $meta_field ) { |
| 168 | /** |
| 169 | * We need this because WordPress automatically use this on insert to the database |
| 170 | */ |
| 171 | $meta_field = remove_accents( $meta_field ); |
| 172 | |
| 173 | /** |
| 174 | * @since 3.1.6 |
| 175 | */ |
| 176 | if ( in_array( $meta_field, User_Meta_Property::get_restricted_keys(), true ) ) { |
| 177 | continue; |
| 178 | } |
| 179 | if ( ! empty( $request[ $form_field ] ) ) { |
| 180 | $metadata[ $meta_field ] = $request[ $form_field ]; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | $userarr = array( |
| 186 | 'user_pass' => $password, |
| 187 | 'user_login' => $username, |
| 188 | 'user_email' => $email, |
| 189 | 'first_name' => $fname, |
| 190 | 'last_name' => $lname, |
| 191 | 'user_url' => $user_url, |
| 192 | ); |
| 193 | |
| 194 | if ( ! empty( $this->settings['user_role'] ) && 'administrator' !== $this->settings['user_role'] ) { |
| 195 | $userarr['role'] = $this->settings['user_role']; |
| 196 | } |
| 197 | |
| 198 | $user_id = wp_insert_user( $userarr ); |
| 199 | |
| 200 | if ( ! is_wp_error( $user_id ) ) { |
| 201 | |
| 202 | if ( Tools::is_wp_password_hash( $password ) ) { |
| 203 | global $wpdb; |
| 204 | |
| 205 | $wpdb->update( |
| 206 | $wpdb->users, |
| 207 | array( 'user_pass' => $password ), |
| 208 | array( 'ID' => $user_id ), |
| 209 | array( '%s' ), |
| 210 | array( '%d' ) |
| 211 | ); |
| 212 | } |
| 213 | |
| 214 | if ( ! empty( $metadata ) ) { |
| 215 | foreach ( $metadata as $meta_key => $meta_value ) { |
| 216 | |
| 217 | if ( $this->is_repeater_val( $meta_value ) ) { |
| 218 | |
| 219 | $prepared_value = array(); |
| 220 | |
| 221 | foreach ( $meta_value as $index => $row ) { |
| 222 | |
| 223 | $prepared_row = array(); |
| 224 | |
| 225 | foreach ( $row as $item_key => $item_value ) { |
| 226 | |
| 227 | $item_key = ! empty( $metafields_map[ $item_key ] ) ? Tools::sanitize_text_field( $metafields_map[ $item_key ] ) : $item_key; |
| 228 | |
| 229 | $prepared_row[ $item_key ] = $item_value; |
| 230 | } |
| 231 | |
| 232 | $prepared_value[ 'item-' . $index ] = $prepared_row; |
| 233 | } |
| 234 | |
| 235 | $meta_value = $prepared_value; |
| 236 | } |
| 237 | |
| 238 | update_user_meta( $user_id, $meta_key, $meta_value ); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if ( ! empty( $this->settings['log_in'] ) ) { |
| 243 | |
| 244 | $user = wp_signon( |
| 245 | array( |
| 246 | 'user_login' => $username, |
| 247 | 'user_password' => $password, |
| 248 | 'remember' => $is_remember, |
| 249 | ) |
| 250 | ); |
| 251 | |
| 252 | if ( ! is_wp_error( $user ) ) { |
| 253 | wp_set_current_user( $user->ID ); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if ( ! empty( $this->settings['add_user_id'] ) && $this->settings['add_user_id'] ) { |
| 258 | $handler->response_data['user_id'] = $user_id; |
| 259 | jet_fb_context()->update_request( $user_id, 'user_id' ); |
| 260 | } |
| 261 | } else { |
| 262 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 263 | throw new Action_Exception( 'failed', $userarr ); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | } |
| 268 |