| 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 = sanitize_text_field( |
| 56 |
$_SERVER['HTTP_CF_CONNECTING_IP'] ?? |
| 57 |
$_SERVER['HTTP_CLIENT_IP'] ?? |
| 58 |
$_SERVER['HTTP_X_FORWARDED_FOR'] ?? |
| 59 |
$_SERVER['REMOTE_ADDR'] ?? '' |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
$user_agents = ''; |
| 64 |
if ( in_array( 'userAgent', $settings, true ) ) { |
| 65 |
$user_agents = sanitize_text_field( |
| 66 |
$_SERVER['HTTP_USER_AGENT'] ?? '' |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
// insert data to entry |
| 71 |
$id = Query::add_new_entry( |
| 72 |
$this->validateformdata->form_info['info']['type'], |
| 73 |
$this->validateformdata->form_info['info']['postId'], |
| 74 |
$this->validateformdata->form_info['info']['email'], |
| 75 |
$ip, |
| 76 |
$user_agents, |
| 77 |
$this->validateformdata->form_info['data'], |
| 78 |
$this->validateformdata->form_info['info']['config']['block_id'], |
| 79 |
boolval( $this->validateformdata->form_info['info']['config']['emailVerification'] ?? false ) |
| 80 |
); |
| 81 |
|
| 82 |
if ( $id ) { |
| 83 |
$this->validateformdata->state_data['submission_id'] = $id; |
| 84 |
$this->validateformdata->set_message( __( 'Success!', 'ablocks' ) ); |
| 85 |
return; |
| 86 |
} |
| 87 |
$this->validateformdata->set_error_message( __( 'Failed to save data', 'ablocks' ) ); |
| 88 |
} |
| 89 |
} |
| 90 |
|