← All changes
|
includes/blocks/form-builder/actions/save-form-data.php
+165
-0
2.7.6
→
2.14.0
View file →
| @@ -1,0 +1,165 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\Blocks\FormBuilder\Actions; | |
| 3 | + | |
| 4 | +use ABlocks\Blocks\FormBuilder\Actions\Interfaces\FormSubmissionAction; | |
| 5 | +use ABlocks\Blocks\FormBuilder\ValidateFormData; | |
| 6 | +use ABlocks\Blocks\FormBuilder\Query; | |
| 7 | + | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + exit; | |
| 10 | +} | |
| 11 | +/** | |
| 12 | + * Save Form data to db | |
| 13 | + */ | |
| 14 | +final class SaveFormData implements FormSubmissionAction { | |
| 15 | + | |
| 16 | + /** @var $validateformdata */ | |
| 17 | + private ValidateFormData $validateformdata; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * Contructor | |
| 21 | + * | |
| 22 | + * @param ValidateFormData $obj | |
| 23 | + */ | |
| 24 | + public function __construct( ValidateFormData $obj ) { | |
| 25 | + $this->validateformdata = $obj; | |
| 26 | + } | |
| 27 | + | |
| 28 | + public function action() { | |
| 29 | + if ( | |
| 30 | + $this->validateformdata->form_info['info']['type'] === 'contact' && | |
| 31 | + ! in_array( 'submission', $this->validateformdata->form_info['info']['actions'], true ) | |
| 32 | + ) { | |
| 33 | + return; | |
| 34 | + } | |
| 35 | + | |
| 36 | + // detemine this request coming from subscription form & email is not exists | |
| 37 | + if ( | |
| 38 | + $this->validateformdata->form_info['info']['type'] === 'subscription' && | |
| 39 | + Query::is_email_exists( $this->validateformdata->form_info['info']['email'], 'subscription' ) | |
| 40 | + ) { | |
| 41 | + $this->validateformdata->set_error_message( __( 'Already subscribed.', 'ablocks' ) ); | |
| 42 | + $this->validateformdata->state_data['dont_send_subs_email'] = true; | |
| 43 | + return; | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * By default both userIp, userAgent are in submissionMetaData, but here it is not available, an empty array. | |
| 48 | + * when a single element is selected, the single element became available | |
| 49 | + * but when both are selected it is not available | |
| 50 | + */ | |
| 51 | + $settings = $this->validateformdata->form_info['info']['config']['submissionMetaData'] ?? []; | |
| 52 | + | |
| 53 | + $ip = ''; | |
| 54 | + if ( in_array( 'userIp', $settings, true ) ) { | |
| 55 | + $ip = wp_unslash( | |
| 56 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 57 | + $_SERVER['HTTP_CF_CONNECTING_IP'] ?? | |
| 58 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 59 | + $_SERVER['HTTP_CLIENT_IP'] ?? | |
| 60 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 61 | + $_SERVER['HTTP_X_FORWARDED_FOR'] ?? | |
| 62 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 63 | + $_SERVER['REMOTE_ADDR'] ?? '' | |
| 64 | + ); | |
| 65 | + $ip = sanitize_text_field( $ip ); | |
| 66 | + } | |
| 67 | + | |
| 68 | + $user_agents = ''; | |
| 69 | + if ( in_array( 'userAgent', $settings, true ) ) { | |
| 70 | + $user_agents = sanitize_text_field( | |
| 71 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 72 | + wp_unslash( $_SERVER['HTTP_USER_AGENT'] ?? '' ) | |
| 73 | + ); | |
| 74 | + } | |
| 75 | + | |
| 76 | + // insert data to entry | |
| 77 | + $id = Query::add_new_entry( | |
| 78 | + $this->validateformdata->form_info['info']['type'], | |
| 79 | + $this->validateformdata->form_info['info']['postId'], | |
| 80 | + $this->validateformdata->form_info['info']['email'], | |
| 81 | + $ip, | |
| 82 | + $user_agents, | |
| 83 | + $this->validateformdata->form_info['data'], | |
| 84 | + $this->validateformdata->form_info['info']['config']['block_id'], | |
| 85 | + boolval( $this->validateformdata->form_info['info']['config']['emailVerification'] ?? false ) | |
| 86 | + ); | |
| 87 | + | |
| 88 | + if ( $id ) { | |
| 89 | + $this->validateformdata->state_data['submission_id'] = $id; | |
| 90 | + $this->validateformdata->set_message( __( 'Success!', 'ablocks' ) ); | |
| 91 | + | |
| 92 | + if ( | |
| 93 | + 'subscription' === $this->validateformdata->form_info['info']['type'] && | |
| 94 | + isset($this->validateformdata->form_info['info']['config']['subscriberToUser']) && | |
| 95 | + (bool) $this->validateformdata->form_info['info']['config']['subscriberToUser'] === true | |
| 96 | + ) { | |
| 97 | + $this->create_user( | |
| 98 | + $this->validateformdata->form_info['data'] | |
| 99 | + ); | |
| 100 | + } | |
| 101 | + return; | |
| 102 | + } | |
| 103 | + $this->validateformdata->set_error_message( __( 'Failed to save data', 'ablocks' ) ); | |
| 104 | + } | |
| 105 | + | |
| 106 | + function create_user( array $data ) : ?int { | |
| 107 | + if ( ! isset( $data['email']['value'] ) || ! is_email( $data['email']['value'] ) ) { | |
| 108 | + return null; | |
| 109 | + } | |
| 110 | + if ( email_exists( $data['email']['value'] ) ) { | |
| 111 | + return null; | |
| 112 | + } | |
| 113 | + | |
| 114 | + $username = sanitize_user( $data['email']['value'] ); | |
| 115 | + | |
| 116 | + $user_data = [ | |
| 117 | + 'user_login' => $username, | |
| 118 | + 'user_email' => $data['email']['value'], | |
| 119 | + 'user_pass' => isset( $data['password']['value'] ) ? $data['password']['value'] : wp_generate_password(), | |
| 120 | + 'role' => 'subscriber', | |
| 121 | + ]; | |
| 122 | + | |
| 123 | + if ( isset( $data['first_name']['value'] ) ) { | |
| 124 | + $user_data['first_name'] = sanitize_text_field( $data['first_name']['value'] ); | |
| 125 | + } | |
| 126 | + | |
| 127 | + if ( isset( $data['last_name']['value'] ) ) { | |
| 128 | + $user_data['last_name'] = sanitize_text_field( $data['last_name']['value'] ); | |
| 129 | + } | |
| 130 | + | |
| 131 | + $user_id = wp_insert_user( $user_data ); | |
| 132 | + | |
| 133 | + if ( is_wp_error( $user_id ) ) { | |
| 134 | + return null; | |
| 135 | + } | |
| 136 | + | |
| 137 | + $user = get_user_by( 'id', $user_id ); | |
| 138 | + $password = $user_data['user_pass']; | |
| 139 | + | |
| 140 | + $subject = 'Your New Account Credentials'; | |
| 141 | + $message = sprintf( 'Hello %s, | |
| 142 | + | |
| 143 | + Your account has been created successfully. Below are your login details: | |
| 144 | + | |
| 145 | + Username: %s | |
| 146 | + Password: %s | |
| 147 | + | |
| 148 | + You can log in to your account here: %s | |
| 149 | + | |
| 150 | + If you did not create this account, please contact us immediately.', | |
| 151 | + $user->first_name ? $user->first_name : 'User', | |
| 152 | + $user->user_login, | |
| 153 | + $password, | |
| 154 | + wp_login_url() | |
| 155 | + ); | |
| 156 | + | |
| 157 | + $headers = [ | |
| 158 | + 'Content-Type: text/html; charset=UTF-8', | |
| 159 | + ]; | |
| 160 | + | |
| 161 | + $mail_sent = wp_mail( $user->user_email, $subject, nl2br( $message ), $headers ); | |
| 162 | + return $user_id; | |
| 163 | + } | |
| 164 | + | |
| 165 | +} | |