| 1 |
<?php |
| 2 |
|
| 3 |
namespace King_Addons; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Create_Submission |
| 10 |
{ |
| 11 |
|
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
add_action('wp_ajax_king_addons_form_builder_submissions', [$this, 'add_to_submissions']); |
| 15 |
add_action('wp_ajax_nopriv_king_addons_form_builder_submissions', [$this, 'add_to_submissions']); |
| 16 |
add_action('save_post', [$this, 'update_submissions_post_meta']); |
| 17 |
} |
| 18 |
|
| 19 |
public function add_to_submissions() |
| 20 |
{ |
| 21 |
|
| 22 |
$nonce = $_POST['nonce']; |
| 23 |
|
| 24 |
if (!wp_verify_nonce($nonce, 'king-addons-js')) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
$new = [ |
| 29 |
'post_status' => 'publish', |
| 30 |
'post_type' => 'king-addons-fb-sub' |
| 31 |
]; |
| 32 |
|
| 33 |
$post_id = wp_insert_post($new); |
| 34 |
foreach ($_POST['form_content'] as $key => $value) { |
| 35 |
update_post_meta($post_id, $key, [$value[0], $value[1], $value[2]]); |
| 36 |
} |
| 37 |
|
| 38 |
$sanitized_form_name = sanitize_text_field($_POST['form_name']); |
| 39 |
$sanitized_form_id = sanitize_text_field($_POST['form_id']); |
| 40 |
$sanitized_form_page = sanitize_text_field($_POST['form_page']); |
| 41 |
$sanitized_form_page_id = sanitize_text_field($_POST['form_page_id']); |
| 42 |
|
| 43 |
update_post_meta($post_id, 'king_addons_form_name', $sanitized_form_name); |
| 44 |
update_post_meta($post_id, 'king_addons_form_id', $sanitized_form_id); |
| 45 |
update_post_meta($post_id, 'king_addons_form_page', $sanitized_form_page); |
| 46 |
update_post_meta($post_id, 'king_addons_form_page_id', $sanitized_form_page_id); |
| 47 |
update_post_meta($post_id, 'king_addons_user_agent', sanitize_textarea_field(wp_unslash($_SERVER['HTTP_USER_AGENT']))); |
| 48 |
update_post_meta($post_id, 'king_addons_user_ip', Core::getClientIP()); |
| 49 |
|
| 50 |
if ($post_id) { |
| 51 |
wp_send_json_success(array( |
| 52 |
'action' => 'king_addons_form_builder_submissions', |
| 53 |
'post_id' => $post_id, |
| 54 |
'message' => esc_html__('Submission created successfully', 'king-addons'), |
| 55 |
'status' => 'success', |
| 56 |
'content' => $_POST['form_content'] |
| 57 |
)); |
| 58 |
} else { |
| 59 |
wp_send_json_success(array( |
| 60 |
'action' => 'king_addons_form_builder_submissions', |
| 61 |
'post_id' => $post_id, |
| 62 |
'message' => esc_html__('Submit action failed', 'king-addons'), |
| 63 |
'status' => 'error' |
| 64 |
)); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public function update_submissions_post_meta($post_id) |
| 69 |
{ |
| 70 |
if (isset($_POST['king_addons_submission_changes']) && !empty($_POST['king_addons_submission_changes'])) { |
| 71 |
$changes = json_decode(stripslashes($_POST['king_addons_submission_changes']), true); |
| 72 |
|
| 73 |
foreach ($changes as $key => $value) { |
| 74 |
update_post_meta($post_id, $key, $value); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
new Create_Submission(); |