| @@ -12,8 +12,9 @@ | ||
| 12 | 12 | |
| 13 | 13 | use ABlocks\Blocks\FormBuilder\ValidateFormData; |
| 14 | 14 | use ABlocks\Blocks\FormBuilder\Actions\SaveFormData; |
| 15 | 15 | use ABlocks\Blocks\FormBuilder\Actions\SendEmail; |
| 16 | +use ABlocks\Blocks\FormBuilder\Actions\SendEmails; | |
| 16 | 17 | use ABlocks\Blocks\FormBuilder\Actions\Subscribe; |
| 17 | 18 | |
| 18 | 19 | use ABlocks\Blocks\FormBuilder\Subscribe\Mailchimp; |
| 19 | 20 | use Exception; |
| @@ -20,49 +21,11 @@ | ||
| 20 | 21 | |
| 21 | 22 | class FormBuilder extends AbstractAjaxHandler { |
| 22 | 23 | public function __construct() { |
| 23 | 24 | $this->actions = array( |
| 24 | - 'form_builder_login_handler' => array( | |
| 25 | - 'callback' => array( $this, 'form_builder_login_handler' ), | |
| 26 | - 'allow_visitor_action' => true, | |
| 27 | - 'fields' => array( | |
| 28 | - 'username' => 'string', | |
| 29 | - 'password' => 'string', | |
| 30 | - 'rememberme' => 'string', | |
| 31 | - 'current_post_id' => 'integer', | |
| 32 | - 'block_id' => 'string', | |
| 33 | - ), | |
| 34 | - ), | |
| 35 | - 'form_builder_registration_handler' => array( | |
| 36 | - 'callback' => array( $this, 'form_builder_registration_handler' ), | |
| 37 | - 'allow_visitor_action' => true, | |
| 38 | - 'fields' => array( | |
| 39 | - 'current_post_id' => 'integer', | |
| 40 | - 'block_id' => 'string', | |
| 41 | - 'username' => 'string', | |
| 42 | - 'email' => 'email', | |
| 43 | - 'password' => 'string', | |
| 44 | - 'confirm_password' => 'string', | |
| 45 | - 'current_post_id' => 'integer', | |
| 46 | - ) | |
| 47 | - ), | |
| 48 | - 'form_builder_forget_password_handler' => array( | |
| 49 | - 'callback' => array( $this, 'form_builder_forget_password_handler' ), | |
| 50 | - 'allow_visitor_action' => true, | |
| 51 | - 'fields' => array( | |
| 52 | - 'email' => 'string', | |
| 53 | - ) | |
| 54 | - ), | |
| 55 | - 'form_builder_submit_handler' => array( | |
| 56 | - 'callback' => array( $this, 'handle_form_submission' ), | |
| 57 | - 'allow_visitor_action' => true, | |
| 58 | - 'fields' => array( | |
| 59 | - 'current_post_id' => 'string', | |
| 60 | - 'block_id' => 'string', | |
| 61 | - ) | |
| 62 | - ), | |
| 63 | 25 | 'form_builder_action_setting_data' => array( |
| 64 | 26 | 'callback' => array( $this, 'action_setting_data' ), |
| 27 | + 'capability' => 'edit_posts', | |
| 65 | 28 | 'fields' => array( |
| 66 | 29 | 'type' => 'string', |
| 67 | 30 | 'api' => 'string', |
| 68 | 31 | 'list_id' => 'string', |
| @@ -74,48 +37,37 @@ | ||
| 74 | 37 | ), |
| 75 | 38 | ); |
| 76 | 39 | |
| 77 | 40 | } |
| 78 | - public function form_builder_login_handler( $payload ) { | |
| 79 | - $block_data = Helper::get_block_attributes( $payload['current_post_id'], $payload['block_id'], 'ablocks/form-builder' ); | |
| 80 | 41 | |
| 81 | - $redirect_url = ''; | |
| 82 | - if ( $block_data['parentAttributes']['loginRedirect'] ?? false ) { | |
| 83 | - $redirect_url = \ABlocks\Blocks\FormBuilder\Helper::merge_query_params( | |
| 84 | - $block_data['parentAttributes']['link']['href'] ?? '', | |
| 85 | - $block_data['parentAttributes']['link']['keyValue'] ?? '', | |
| 86 | - true | |
| 87 | - ); | |
| 42 | + public function prepare_res( array $data, array $block_data, string $redirect_url ) : array { | |
| 43 | + $formType = $block_data['parentAttributes']['formType'] ?? ''; | |
| 44 | + | |
| 45 | + // confirmationType based on formType | |
| 46 | + if ( $formType == 'login' ) { | |
| 47 | + $confirmationType = ( $block_data['parentAttributes']['loginRedirect'] ?? false ) ? 'redirect' : 'success'; | |
| 48 | + } elseif ( $formType == 'registration' ) { | |
| 49 | + $confirmationType = ( $block_data['parentAttributes']['registerRedirect'] ?? false ) ? 'redirect' : 'success'; | |
| 50 | + } else { | |
| 51 | + $confirmationType = $block_data['parentAttributes']['confirmationType'] ?? 'success'; | |
| 88 | 52 | } |
| 89 | 53 | |
| 90 | - $username = $payload['username']; | |
| 91 | - $password = $payload['password']; | |
| 92 | - $remember = isset( $payload['rememberme'] ) && $payload['rememberme'] === 'on'; | |
| 54 | + return array_merge( | |
| 55 | + $data, | |
| 56 | + [ | |
| 57 | + 'afterFormSubmission' => $block_data['parentAttributes']['afterFormSubmission'] ?? 'reset', | |
| 58 | + 'confirmationType' => $confirmationType, | |
| 59 | + 'confirmationNotice' => $block_data['parentAttributes']['confirmationNotice'] ?? $data['message'] ?? __( 'Form successfully submitted!', 'ablocks' ), | |
| 60 | + 'redirect_url' => esc_url( $redirect_url ), | |
| 61 | + 'no_follow' => $block_data['parentAttributes']['link']['noFollow'] ?? '', | |
| 62 | + 'link_target' => $block_data['parentAttributes']['link']['linkTarget'] ?? '', | |
| 63 | + 'formType' => $formType, | |
| 64 | + ] | |
| 65 | + ); | |
| 66 | + } | |
| 93 | 67 | |
| 94 | - $secure_cookie = is_ssl(); | |
| 95 | - $user_signon = wp_signon( array( | |
| 96 | - 'user_login' => $username, | |
| 97 | - 'user_password' => $password, | |
| 98 | - 'remember' => $remember, | |
| 99 | - ), $secure_cookie ); | |
| 100 | 68 | |
| 101 | - if ( is_wp_error( $user_signon ) ) { | |
| 102 | - wp_send_json_error( [ 'message' => $user_signon->get_error_message() ] ); | |
| 103 | - } | |
| 104 | 69 | |
| 105 | - wp_set_current_user( $user_signon->ID ); | |
| 106 | - | |
| 107 | - if ( empty( $redirect_url ) ) { | |
| 108 | - $redirect_url = home_url( '/' ); | |
| 109 | - } | |
| 110 | - wp_send_json_success([ | |
| 111 | - 'message' => __( 'You have logged in successfully. Redirecting...', 'ablocks' ), | |
| 112 | - 'redirect_url' => esc_url( $redirect_url ), | |
| 113 | - 'no_follow' => $block_data['parentAttributes']['link']['noFollow'] ?? '', | |
| 114 | - 'link_target' => $block_data['parentAttributes']['link']['linkTarget'] ?? '', | |
| 115 | - ]); | |
| 116 | - } | |
| 117 | - | |
| 118 | 70 | public function registration_form_setting_data( $payload ) { |
| 119 | 71 | $roles = [ |
| 120 | 72 | 'default' => __( 'Default', 'ablocks' ) |
| 121 | 73 | ]; |
| @@ -126,77 +78,9 @@ | ||
| 126 | 78 | } |
| 127 | 79 | } |
| 128 | 80 | wp_send_json_success( $roles ); |
| 129 | 81 | } |
| 130 | - public function form_builder_registration_handler( $payload ) { | |
| 131 | - $block_data = Helper::get_block_attributes( $payload['current_post_id'], $payload['block_id'], 'ablocks/form-builder' ); | |
| 132 | 82 | |
| 133 | - if ( ! get_option( 'users_can_register' ) ) { | |
| 134 | - wp_send_json_error([ | |
| 135 | - 'message' => __( 'User registration is turned off. Turn it on to allow user registration.', 'ablocks' ) | |
| 136 | - ]); | |
| 137 | - } | |
| 138 | - | |
| 139 | - if ( ( $block_data['parentAttributes']['formType'] ?? '' ) !== 'registration' ) { | |
| 140 | - wp_send_json_error([ | |
| 141 | - 'message' => __( 'Error.', 'ablocks' ) | |
| 142 | - ]); | |
| 143 | - } | |
| 144 | - | |
| 145 | - $redirect_url = ''; | |
| 146 | - if ( $block_data['parentAttributes']['registerRedirect'] ?? false ) { | |
| 147 | - $redirect_url = \ABlocks\Blocks\FormBuilder\Helper::merge_query_params( | |
| 148 | - $block_data['parentAttributes']['link']['href'] ?? '', | |
| 149 | - $block_data['parentAttributes']['link']['keyValue'] ?? '', | |
| 150 | - true | |
| 151 | - ); | |
| 152 | - } | |
| 153 | - | |
| 154 | - $errors = $this->validate_registration_data( $payload ); | |
| 155 | - if ( $errors ) { | |
| 156 | - wp_send_json_error( [ 'message' => $errors ] ); | |
| 157 | - } | |
| 158 | - | |
| 159 | - $user_id = wp_create_user( $payload['username'], $payload['password'], $payload['email'] ); | |
| 160 | - if ( is_wp_error( $user_id ) ) { | |
| 161 | - wp_send_json_error( [ 'message' => $user_id->get_error_message() ] ); | |
| 162 | - } | |
| 163 | - $role = ( $block_data['parentAttributes']['roleSlug'] ?? '' ); | |
| 164 | - if ( | |
| 165 | - ! empty( $role ) && | |
| 166 | - strtolower( $role ) !== 'default' && | |
| 167 | - array_key_exists( $role, wp_roles()->roles ) | |
| 168 | - ) { | |
| 169 | - $user = new \WP_User( $user_id ); | |
| 170 | - $user->set_role( $role ); | |
| 171 | - } | |
| 172 | - | |
| 173 | - $custom_fields = $this->get_custom_fields( $_POST ); | |
| 174 | - | |
| 175 | - if ( ! empty( $block_data ) ) { | |
| 176 | - $sorted_fields = Helper::sorted_input_fields_by_input_type( $block_data['innerBlocks'] ); | |
| 177 | - $schema = Helper::generate_schema_using_form_data( $sorted_fields ); | |
| 178 | - $sanitized_fields = Sanitizer::sanitize_payload( $schema, $custom_fields ); | |
| 179 | - | |
| 180 | - array_walk($sanitized_fields, function ( $value, $key ) use ( $user_id ) { | |
| 181 | - update_user_meta( $user_id, 'ablocks_' . $key, $value ); | |
| 182 | - }); | |
| 183 | - } | |
| 184 | - | |
| 185 | - wp_set_current_user( $user_id ); | |
| 186 | - wp_set_auth_cookie( $user_id ); | |
| 187 | - | |
| 188 | - if ( empty( $redirect_url ) ) { | |
| 189 | - $redirect_url = home_url( '/' ); | |
| 190 | - } | |
| 191 | - wp_send_json_success([ | |
| 192 | - 'message' => __( 'Registration completed successfully. Redirecting...', 'ablocks' ), | |
| 193 | - 'redirect_url' => esc_url( $redirect_url ), | |
| 194 | - 'no_follow' => $block_data['parentAttributes']['link']['noFollow'] ?? '', | |
| 195 | - 'link_target' => $block_data['parentAttributes']['link']['linkTarget'] ?? '', | |
| 196 | - ]); | |
| 197 | - } | |
| 198 | - | |
| 199 | 83 | private function validate_registration_data( $payload ) { |
| 200 | 84 | if ( empty( $payload['username'] ) || ! validate_username( $payload['username'] ) ) { |
| 201 | 85 | return 'Invalid username.'; |
| 202 | 86 | } elseif ( username_exists( $payload['username'] ) ) { |
| @@ -220,64 +104,8 @@ | ||
| 220 | 104 | |
| 221 | 105 | private function get_custom_fields( $form_data ) { |
| 222 | 106 | $reserved_keys = [ 'username', 'email', 'password', 'confirm_password', 'current_post_id', 'block_id', 'security', 'action' ]; |
| 223 | 107 | return array_diff_key( $form_data, array_flip( $reserved_keys ) ); |
| 224 | - } | |
| 225 | - | |
| 226 | - public function form_builder_forget_password_handler( $payload ) { | |
| 227 | - if ( empty( $payload['email'] ?? '' ) ) { | |
| 228 | - wp_send_json_error([ | |
| 229 | - 'message' => __( 'email field is required.', 'ablocks' ) | |
| 230 | - ], 400); | |
| 231 | - } | |
| 232 | - | |
| 233 | - if ( ! is_email( $payload['email'] ) ) { | |
| 234 | - wp_send_json_error([ | |
| 235 | - 'message' => __( 'provide a valid email.', 'ablocks' ) | |
| 236 | - ], 400); | |
| 237 | - } | |
| 238 | - | |
| 239 | - if ( ! email_exists( $payload['email'] ) ) { | |
| 240 | - wp_send_json_error([ | |
| 241 | - 'message' => __( 'this email is not exists.', 'ablocks' ) | |
| 242 | - ], 400); | |
| 243 | - } | |
| 244 | - $error = retrieve_password( $payload['email'] ); | |
| 245 | - if ( is_wp_error( $error ) ) { | |
| 246 | - wp_send_json_error([ | |
| 247 | - 'message' => esc_html( $error->get_error_message() ) | |
| 248 | - ], 500); | |
| 249 | - } | |
| 250 | - wp_send_json_success([ | |
| 251 | - 'message' => __( 'Password reset email is sent.', 'ablocks' ) | |
| 252 | - ]); | |
| 253 | - } | |
| 254 | - | |
| 255 | - public function handle_form_submission( $payload ) { | |
| 256 | - $fields_to_skip = [ 'current_post_id', 'block_id', 'security', 'action' ]; | |
| 257 | - $all_fields = array_diff_key( $_POST, array_flip( $fields_to_skip ) ); | |
| 258 | - $block_data = Helper::get_block_attributes( $payload['current_post_id'], $payload['block_id'], 'ablocks/form-builder' ); | |
| 259 | - | |
| 260 | - $actions = apply_filters('ablocks/form_builder/actions', [ | |
| 261 | - SaveFormData::class, | |
| 262 | - SendEmail::class, | |
| 263 | - Subscribe::class, | |
| 264 | - ]); | |
| 265 | - | |
| 266 | - $validate_data = new ValidateFormData( $block_data, $all_fields ); | |
| 267 | - | |
| 268 | - $validate_data->actions( $actions ); | |
| 269 | - | |
| 270 | - $output = $validate_data->get_output(); | |
| 271 | - if ( $validate_data->has_error() ) { | |
| 272 | - $output['message'] = $validate_data->get_error_message(); | |
| 273 | - wp_send_json_error( $output ); | |
| 274 | - } elseif ( $validate_data->has_message() ) { | |
| 275 | - $output['message'] = $validate_data->get_message(); | |
| 276 | - wp_send_json_success( $output ); | |
| 277 | - } | |
| 278 | - | |
| 279 | - wp_send_json_error( [ 'message' => __( 'Action is not defined.', 'ablocks' ) ] ); | |
| 280 | 108 | } |
| 281 | 109 | |
| 282 | 110 | public function action_setting_data( $payload ) { |
| 283 | 111 | $type = $payload['type'] ?? ''; |