This commit is contained in:
2025-11-18 16:39:33 +00:00
parent c70394d367
commit df1f90c6cd
9673 changed files with 3464697 additions and 34 deletions
@@ -0,0 +1,81 @@
<?php
namespace WprAddons\Classes\Modules\Forms;
use Elementor\Utils;
use WprAddons\Classes\Utilities;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* WPR_Actions_Status setup
*
* @since 3.4.6
*/
class WPR_Actions_Status {
public function __construct() {
add_action('wp_ajax_wpr_update_form_action_meta', [$this, 'wpr_update_form_action_meta']);
add_action('wp_ajax_nopriv_wpr_update_form_action_meta', [$this, 'wpr_update_form_action_meta']);
}
// In your PHP file
public function wpr_update_form_action_meta() {
$nonce = $_POST['nonce'];
if ( !wp_verify_nonce( $nonce, 'wpr-addons-js' ) ) {
return; // Get out of here, the nonce is rotten!
}
// Validate custom token
// $custom_token = $_POST['custom_token'];
// if ( is_user_logged_in() ) {
// // For logged-in users, validate against their user ID
// $user_id = get_current_user_id();
// $stored_token = get_transient( 'wpr_custom_token_' . $user_id );
// } else {
// // For non-logged-in users, use the guest token from the cookie
// if ( isset( $_COOKIE['wpr_guest_token'] ) ) {
// $guest_id = sanitize_text_field( $_COOKIE['wpr_guest_token'] );
// $stored_token = get_transient( 'wpr_custom_guest_token_' . $guest_id );
// } else {
// wp_send_json_error( 'Invalid token.' );
// return;
// }
// }
// if ( ! $stored_token || $custom_token !== $stored_token ) {
// wp_send_json_error( 'Invalid token.' );
// return;
// }
$post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
$action_name = isset($_POST['action_name']) ? sanitize_text_field($_POST['action_name']) : '';
$status = isset($_POST['status']) ? sanitize_text_field($_POST['status']) : '';
$message = isset($_POST['message']) ? sanitize_text_field($_POST['message']) : '';
$meta_value = [
'status' => $status,
'message' => $message
];
$actions_whitelist = [
'wpr_form_builder_email',
'wpr_form_builder_submissions',
'wpr_form_builder_mailchimp',
'wpr_form_builder_webhook'
];
if ($post_id && $action_name && $status && in_array($action_name, $actions_whitelist)) {
update_post_meta($post_id, '_action_' . $action_name, $meta_value);
wp_send_json_success('Post meta updated successfully');
} else {
wp_send_json_error('Invalid data provided');
}
}
}
new WPR_Actions_Status();
@@ -0,0 +1,179 @@
<?php
namespace WprAddons\Classes\Modules\Forms;
use Elementor\Utils;
use WprAddons\Classes\Utilities;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* WPR_Send_Email setup
*
* @since 3.4.6
*/
class WPR_File_Upload {
public function __construct() {
add_action('wp_ajax_wpr_addons_upload_file', [$this, 'handle_file_upload']);
add_action('wp_ajax_nopriv_wpr_addons_upload_file', [$this, 'handle_file_upload']);
}
public function handle_file_upload() {
// $email_fields = get_option('wpr_email_fields_' . $_POST['wpr_form_id']); remove if necessary
if (!isset($_POST['wpr_addons_nonce']) || !wp_verify_nonce($_POST['wpr_addons_nonce'], 'wpr-addons-js')) {
wp_send_json_error(array(
'message' => esc_html__('Security check failed.', 'wpr-addons'),
));
}
if ( !isset($_POST['form_field_id']) || !get_option( 'wpr_form_upload_field_in_use_' . sanitize_text_field( $_POST['form_field_id'] ) ) ) {
wp_send_json_error(['message' => 'Permission denied'], 403);
}
// Get the max_file_size value from the file_size control.
$max_file_size = isset($_POST['max_file_size']) ? floatval(sanitize_text_field($_POST['max_file_size'])) : 0; // Replace this with the value from the file_size control.
if ($max_file_size <= 0) {
$max_file_size = wp_max_upload_size() / pow(1024, 2); //MB
}
// Check if a file is uploaded.
if (isset($_FILES['uploaded_file'])) {
$file = $_FILES['uploaded_file'];
// Check if the uploaded file size exceeds the allowed limit.
if ($file['size'] > $max_file_size * 1024 * 1024) {
wp_send_json_error(array(
'cause' => 'filesize',
'sizes' => [
$max_file_size * 1024 * 1024,
$file['size']
],
'message' => 'File size exceeds the allowed limit.'
));
}
// valid file type?
if ( !$this->file_validity( $file ) ) {
wp_send_json_error(array(
'cause' => 'filetype',
'message' => esc_html__('File type is not valid.', 'wpr-addons')
));
}
if ( 'click' == $_POST['triggering_event'] ) {
// Set the upload directory.
$upload_dir = wp_upload_dir();
$upload_path = $upload_dir['basedir'] . '/wpr-addons/forms';
wp_mkdir_p( $upload_path );
// Generate a unique file name.
$filename = wp_unique_filename($upload_path, $file['name']);
// Move the uploaded file to the uploads directory.
if (move_uploaded_file($file['tmp_name'], $upload_path . '/' . $filename)) {
// Return the uploaded file's URL.
wp_send_json_success(array(
'url' => $upload_dir['baseurl'] . '/wpr-addons/forms/' . $filename
));
} else {
wp_send_json_error(array(
'message' => esc_html__('Failed to upload the file.', 'wpr-addons')
));
}
} else {
wp_send_json_success(array(
'message' => esc_html__('File validation passed', 'wpr-addons')
));
}
}
if ( 'click' == $_POST['triggering_event'] ) {
// Folder not being created during interaction was causing issues
$upload_dir = wp_upload_dir();
$upload_path = $upload_dir['basedir'] . '/wpr-addons/forms';
wp_mkdir_p( $upload_path );
wp_send_json_error(array(
'message' => esc_html__('No file was uploaded.', 'wpr-addons'),
'files' => $_FILES['uploaded_file']
));
}
}
// private function file_validity( $field, $file ) {
private function file_validity( $file ) {
$whitelist = ['jpg', 'jpeg', 'png', 'gif', 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'odt', 'avi', 'ogg', 'm4a', 'mov', 'mp3', 'mp4', 'mpg', 'wav', 'wmv', 'txt'];
// File type validation
if ( empty( $_POST['allowed_file_types'] ) ) {
$allowed_file_types = 'jpg,jpeg,png,gif,pdf,doc,docx,ppt,pptx,odt,avi,ogg,m4a,mov,mp3,mp4,mpg,wav,wmv,txt';
} else {
$allowed_file_types = $_POST['allowed_file_types'];
}
if (!wp_check_filetype($file['name'])['ext']) {
return false;
}
$f_extension = pathinfo( $file['name'], PATHINFO_EXTENSION );
$f_extension = strtolower( $f_extension );
$allowed_file_types = explode( ',', $allowed_file_types );
$allowed_file_types = array_map( 'trim', $allowed_file_types );
$allowed_file_types = array_map( 'strtolower', $allowed_file_types );
return ( in_array( $f_extension, $allowed_file_types ) && in_array( $f_extension, $whitelist ) && !in_array( $f_extension, $this->get_exclusion_list() ) );
}
private function get_exclusion_list() {
static $exclusionlist = false;
if ( ! $exclusionlist ) {
$exclusionlist = [
'php',
'php3',
'php4',
'php5',
'php6',
'phps',
'php7',
'phtml',
'shtml',
'pht',
'swf',
'html',
'asp',
'aspx',
'cmd',
'csh',
'bat',
'htm',
'hta',
'jar',
'exe',
'com',
'js',
'lnk',
'htaccess',
'htpasswd',
'phtml',
'ps1',
'ps2',
'py',
'rb',
'tmp',
'cgi',
'svg',
'svgz'
];
}
return $exclusionlist;
}
}
new WPR_File_Upload();
@@ -0,0 +1,78 @@
<?php
namespace WprAddons\Classes\Modules\Forms;
use Elementor\Utils;
use WprAddons\Classes\Utilities;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* WPR_Actions_Status setup
*
* @since 3.4.6
*/
class WPR_Recaptcha_Handler {
public function __construct() {
add_action('wp_ajax_wpr_verify_recaptcha', [$this, 'wpr_verify_recaptcha']);
add_action('wp_ajax_nopriv_wpr_verify_recaptcha', [$this, 'wpr_verify_recaptcha']);
}
// In your PHP file
public function wpr_verify_recaptcha() {
$recaptcha_response = $_POST['g-recaptcha-response'];
$is_valid_recaptcha = $this->check_recaptcha($recaptcha_response);
if ($is_valid_recaptcha[0] && $is_valid_recaptcha[1] >= get_option('wpr_recaptcha_v3_score')) {
// Proceed with form processing
wp_send_json_success(array(
'message' => 'Recaptcha Success',
'score' => $is_valid_recaptcha[1]
));
} else {
// Handle the invalid reCAPTCHA case
wp_send_json_error(array(
'message' => 'Recaptcha Error',
'score' => $is_valid_recaptcha[1],
'results' => [
$is_valid_recaptcha[0],
$is_valid_recaptcha[1] >= get_option('wpr_recaptcha_v3_score')
]
));
}
}
public function check_recaptcha($recaptcha_response) {
$secret_key = get_option('wpr_recaptcha_v3_secret_key');
$remote_ip = $_SERVER['REMOTE_ADDR'];
$response = wp_remote_post('https://www.google.com/recaptcha/api/siteverify', array(
'body' => array(
'secret' => $secret_key,
'response' => $recaptcha_response,
'remoteip' => $remote_ip
)
));
if (is_wp_error($response)) {
// Handle the error accordingly
return false;
}
$decoded_response = json_decode(wp_remote_retrieve_body($response), true);
$score = $decoded_response['score'];
if ($decoded_response['success'] === true) {
// reCAPTCHA verification passed
return [true, $score];
} else {
// reCAPTCHA verification failed
return [false, $score];
}
}
}
new WPR_Recaptcha_Handler();
@@ -0,0 +1,314 @@
<?php
namespace WprAddons\Classes\Modules\Forms;
use Elementor\Utils;
use WprAddons\Classes\Utilities;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* WPR_Send_Email setup
*
* @since 3.4.6
*/
class WPR_Send_Email {
public function __construct() {
add_action('wp_ajax_wpr_form_builder_email' , [$this, 'send_email']);
add_action('wp_ajax_nopriv_wpr_form_builder_email',[$this, 'send_email']);
}
public function send_email() {
$nonce = $_POST['nonce'];
if ( !wp_verify_nonce( $nonce, 'wpr-addons-js' ) ) {
return; // Get out of here, the nonce is rotten!
}
$message_body = [];
foreach ($_POST['form_content'] as $field) {
if ($field[0] === 'email') {
if (!is_email($field[1])) {
// The field is an email, but it is not a valid email address
// Take action or abort function execution here
wp_send_json_error(array(
'action' => 'wpr_form_builder_email',
'message' => esc_html__('Email provided is invalid', 'wpr-addons'),
'status' => 'error'
));
}
}
}
// Rest of your function code here (if needed)
$content_type = get_option('wpr_email_content_type_'. $_POST['wpr_form_id']);
$line_break = 'html' === $content_type ? '<br>' : "\n";
$email_fields = trim(get_option('wpr_email_fields_' . $_POST['wpr_form_id']));
if ( $email_fields === '[all-fields]' || str_contains($email_fields, '[all-fields]') ) {
// Function to replace the shortcode with the form value
$replace_shortcode_with_value = function ($matches) {
$field_id = $matches[1];
foreach ($_POST['form_content'] as $key => $value) {
$key_parts = explode('-', $key);
$last_part = end($key_parts);
if ($last_part === $field_id) {
return is_array($value[1]) ? implode("\n", $value[1]) : $value[1];
}
}
return ''; // Return an empty string if the field id is not found
};
// Function to replace the shortcode with the form value
$all_fields_content = [];
foreach ($_POST['form_content'] as $key => $value) {
$all_fields_content[] = is_array($value[1]) ? trim($value[2]) . ': ' . implode("\n", $value[1]) : trim($value[2]) . ': ' . $value[1];
}
$all_fields_content = implode("\n", $all_fields_content);
// Process user input to replace shortcodes
$processed_message = str_replace('[all-fields]', $all_fields_content, $email_fields);
$processed_message = preg_replace_callback(
'/\[id="([^"]+)"\]/',
$replace_shortcode_with_value,
$processed_message
);
} else {
// Function to replace the shortcode with the form value
$replace_shortcode_with_value = function ($matches) {
$field_id = $matches[1];
foreach ($_POST['form_content'] as $key => $value) {
$key_parts = explode('-', $key);
$last_part = end($key_parts);
if ($last_part === $field_id) {
return is_array($value[1]) ? trim($value[2]) . ': ' . implode("\n", $value[1]) : trim($value[2]) . ': ' . $value[1];
}
}
return ''; // Return an empty string if the field id is not found
};
// Process user input to replace shortcodes
$processed_message = preg_replace_callback(
'/\[id="([^"]+)"\]/',
$replace_shortcode_with_value,
$email_fields
);
}
$meta_keys = get_option('wpr_meta_keys_'. $_POST['wpr_form_id']);
$meta_fields = [];
foreach ( $meta_keys as $metadata_type ) {
switch ( $metadata_type ) {
case 'date':
$meta_fields['date'] = [
'title' => esc_html__( 'Date', 'wpr-addons' ),
'value' => date_i18n( get_option( 'date_format' ) ),
];
break;
case 'time':
$meta_fields['time'] = [
'title' => esc_html__( 'Time', 'wpr-addons' ),
'value' => date_i18n( get_option( 'time_format' ) ),
];
break;
case 'page_url':
$meta_fields['page_url'] = [
'title' => esc_html__( 'Page URL', 'wpr-addons' ),
// 'value' => get_option('wpr_referrer_'. $_POST['wpr_form_id']) ? esc_url_raw( wp_unslash( get_option('wpr_referrer_'. $_POST['wpr_form_id']) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
'value' => get_option('wpr_referrer_'. $_POST['wpr_form_id']) ? get_option('wpr_referrer_'. $_POST['wpr_form_id']) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
];
break;
case 'page_title':
$meta_fields['page_title'] = [
'title' => esc_html__( 'Page Title', 'wpr-addons' ),
// 'value' => get_option('wpr_referrer_title_'. $_POST['wpr_form_id']) ? sanitize_text_field( wp_unslash( get_option('wpr_referrer_title_'. $_POST['wpr_form_id']) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
'value' => get_option('wpr_referrer_title_'. $_POST['wpr_form_id']) ? get_option('wpr_referrer_title_'. $_POST['wpr_form_id']) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
];
break;
case 'user_agent':
$meta_fields['user_agent'] = [
'title' => esc_html__( 'User Agent', 'wpr-addons' ),
'value' => isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_textarea_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '',
];
break;
case 'remote_ip':
$meta_fields['remote_ip'] = [
'title' => esc_html__( 'Remote IP', 'wpr-addons' ),
'value' => Utilities::get_client_ip(),
];
break;
case 'credit':
$meta_fields['credit'] = [
'title' => esc_html__( 'Powered by', 'wpr-addons' ),
'value' => esc_html__( 'Royal Addons', 'wpr-addons' ), // is it necessary ?
];
break;
}
}
$email_meta = [];
foreach( $meta_fields as $key => $value ) {
$email_meta[] = $value['title'] . ': ' . $value['value'];
}
$to = get_option('wpr_email_to_'. $_POST['wpr_form_id']);
$to = preg_replace_callback(
'/\[id="(\w+)"\]/',
function ($matches) {
return $this->get_field_value($matches[1]);
},
$to
);
$subject = get_option('wpr_email_subject_'. $_POST['wpr_form_id']);
$subject = preg_replace_callback(
'/\[id="(\w+)"\]/',
function ($matches) {
return $this->get_field_value($matches[1]);
},
$subject
);
if ( $processed_message ) {
$message_body[] = $processed_message;
}
// Prepare the message body with correct line breaks
if ($content_type === 'html') {
// Replace all instances of \n with <br> in each message body item
foreach ($message_body as &$item) {
$item = nl2br($item);
}
unset($item); // Break the reference with the last element
}
$body = implode($line_break, $message_body) . $line_break . '-----' . $line_break . implode($line_break, $email_meta);
$cc_header = '';
if ( !empty( get_option('wpr_cc_header_'. $_POST['wpr_form_id']) ) ) {
$cc_header = 'Cc: ' . get_option('wpr_cc_header_'. $_POST['wpr_form_id']);
$cc_header = preg_replace_callback(
'/\[id="(\w+)"\]/',
function ($matches) {
return $this->get_field_value($matches[1]);
},
$cc_header
);
}
$bcc_header = '';
if ( !empty( get_option('wpr_bcc_header_'. $_POST['wpr_form_id']) ) ) {
$bcc_header = 'Bcc: ' . get_option('wpr_bcc_header_'. $_POST['wpr_form_id']);
$bcc_header = preg_replace_callback(
'/\[id="(\w+)"\]/',
function ($matches) {
return $this->get_field_value($matches[1]);
},
$bcc_header
);
}
if ( !empty( get_option('wpr_reply_to_'. $_POST['wpr_form_id']) ) && !empty(get_option('wpr_email_from_name_'. $_POST['wpr_form_id'])) && !empty(get_option('wpr_email_from_'. $_POST['wpr_form_id'])) ) {
preg_match_all('/id="([^"]+)"/', get_option('wpr_reply_to_'. $_POST['wpr_form_id']), $matche);
$reply_to_field_id = $matche[1];
preg_match_all('/id="([^"]+)"/', get_option('wpr_email_from_name_'. $_POST['wpr_form_id']), $matche);
$email_from_name_field_id = $matche[1];
preg_match_all('/id="([^"]+)"/', get_option('wpr_email_from_'. $_POST['wpr_form_id']), $matche);
$email_from_field_id = $matche[1];
foreach ( $_POST['form_content'] as $key => $value ) {
$key_parts = explode('-', $key);
$last_part = end($key_parts);
if (in_array($last_part, $reply_to_field_id)) {
$reply_to_address = $value[1];
}
if (in_array($last_part, $email_from_name_field_id)) {
$email_from_name = $value[1];
}
if (in_array($last_part, $email_from_field_id)) {
$email_from_mail = $value[1];
}
}
if ( !isset($reply_to_address) || empty($reply_to_address) ) {
$reply_to_address = get_option('wpr_reply_to_'. $_POST['wpr_form_id']);
}
if ( !isset($email_from_name) || empty($email_from_name) ) {
$email_from_name = get_option('wpr_email_from_name_'. $_POST['wpr_form_id']);
}
if ( !isset($email_from_mail) || empty($email_from_mail) ) {
$email_from_mail = get_option('wpr_email_from_'. $_POST['wpr_form_id']);
}
$reply_to = 'Reply-To: ' . $reply_to_address;
}
$email_from = sprintf( 'From: %s <%s>' . "\r\n", $email_from_name, $email_from_mail );
$headers = array('Content-Type: text/'. $content_type .'; charset=UTF-8', $email_from, $cc_header, $bcc_header, $reply_to);
// Send email using wp_mail() function
$sent = wp_mail( $to, $subject, $body, $headers);
if ( $sent ) {
wp_send_json_success(array(
'action' => 'wpr_form_builder_email',
'message' => esc_html__('Message sent successfully', 'wpr-addons'),
'status' => 'success',
'details' => json_encode($message_body)
));
} else {
wp_send_json_error(array(
'action' => 'wpr_form_builder_email',
'message' => esc_html__('Message could not be sent', 'wpr-addons'),
'status' => 'error',
'details' => json_encode($message_body)
));
}
}
public function get_field_value($field_id) {
foreach ($_POST['form_content'] as $key => $field) {
$key_parts = explode('-', $key);
$last_part = end($key_parts);
if ($last_part === $field_id) {
return $field[1];
}
}
return ''; // Return an empty string if the field id is not found
}
}
new WPR_Send_Email();
@@ -0,0 +1,77 @@
<?php
namespace WprAddons\Classes\Modules\Forms;
use Elementor\Utils;
use WprAddons\Classes\Utilities;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* WPR_Send_Webhook setup
*
* @since 3.4.6
*/
class WPR_Send_Webhook {
public function __construct() {
add_action('wp_ajax_wpr_form_builder_webhook' , [$this, 'send_webhook']);
add_action('wp_ajax_nopriv_wpr_form_builder_webhook',[$this, 'send_webhook']);
}
public function send_webhook() {
$nonce = $_POST['nonce'];
if ( !wp_verify_nonce( $nonce, 'wpr-addons-js' ) ) {
return; // Get out of here, the nonce is rotten!
}
$message_body = [];
foreach ( $_POST['form_content'] as $key => $value ) {
if ( is_array($value[1]) ) {
if ( empty($value[2]) ) {
$message_body[trim($key)] = implode("\n", $value[1]);
} else {
$message_body[trim($value[2])] = implode("\n", $value[1]);
}
} else {
if ( empty($value2) ) {
$message_body[trim($key)] = $value[1];
} else {
$message_body[trim($value[2])] = $value[1];
}
}
}
$message_body['form_id'] = $_POST['wpr_form_id'];
$message_body['form_name'] = $_POST['form_name'];
$args = [
'body' => $message_body,
];
$response = wp_remote_post( trim(get_option('wpr_webhook_url_' . $_POST['wpr_form_id'])), $args );
if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
wp_send_json_error(array(
'action' => 'wpr_form_builder_webhook',
'message' => esc_html__('Webhook error', 'wpr-addons'),
'status' => 'error',
'details' => json_encode($message_body)
));
// throw new \Exception( 'Webhook error.' );
} else {
wp_send_json_success(array(
'action' => 'wpr_form_builder_webhook',
'message' => esc_html__('Webhook success', 'wpr-addons'),
'status' => 'success',
'details' => json_encode($message_body)
));
}
}
}
new WPR_Send_Webhook();
@@ -0,0 +1,119 @@
<?php
namespace WprAddons\Classes\Modules\Forms;
use Elementor\Utils;
use Elementor\Group_Control_Image_Size;
use WprAddons\Classes\Utilities;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* WPR_Form_Builder_Submissions setup
*
* @since 3.4.6
*/
class WPR_Form_Builder_Submissions {
public function __construct() {
add_action('wp_ajax_wpr_form_builder_submissions' , [$this, 'add_to_submissions']);
add_action('wp_ajax_nopriv_wpr_form_builder_submissions',[$this, 'add_to_submissions']);
add_action('save_post', [$this, 'update_submissions_post_meta']);
}
public function add_to_submissions() {
$nonce = $_POST['nonce'];
if ( !wp_verify_nonce( $nonce, 'wpr-addons-js' ) ) {
return; // Get out of here, the nonce is rotten!
}
$new = [
'post_status' => 'publish',
'post_type' => 'wpr_submissions'
];
$post_id = wp_insert_post( $new );
foreach ($_POST['form_content'] as $key => $value ) {
update_post_meta($post_id, $key, [$value[0], $value[1], $value[2]]);
}
$sanitized_form_name = sanitize_text_field($_POST['form_name']);
$sanitized_form_id = sanitize_text_field($_POST['form_id']);
$sanitized_form_page = sanitize_text_field($_POST['form_page']);
$sanitized_form_page_id = sanitize_text_field($_POST['form_page_id']);
update_post_meta($post_id, 'wpr_form_name', $sanitized_form_name);
update_post_meta($post_id, 'wpr_form_id', $sanitized_form_id);
update_post_meta($post_id, 'wpr_form_page', $sanitized_form_page);
update_post_meta($post_id, 'wpr_form_page_id', $sanitized_form_page_id);
update_post_meta($post_id, 'wpr_user_agent', sanitize_textarea_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ));
update_post_meta($post_id, 'wpr_user_ip', Utilities::get_client_ip());
if( $post_id ) {
wp_send_json_success(array(
'action' => 'wpr_form_builder_submissions',
'post_id' => $post_id,
'message' => esc_html__('Submission created successfully', 'wpr-addons'),
'status' => 'success',
'content' => $_POST['form_content']
));
} else {
wp_send_json_success(array(
'action' => 'wpr_form_builder_submissions',
'post_id' => $post_id,
'message' => esc_html__('Submit action failed', 'wpr-addons'),
'status' => 'error'
));
}
}
public function update_submissions_post_meta($post_id) {
// Only allow users with edit permissions
if ( ! current_user_can('edit_post', $post_id) ) {
return;
}
if ( isset($_POST['wpr_submission_changes']) && ! empty($_POST['wpr_submission_changes']) ) {
$changes = json_decode(stripslashes($_POST['wpr_submission_changes']), true);
if ( ! is_array($changes) ) {
return;
}
// List of disallowed meta keys
$disallowed_keys = [
'_elementor_data',
'_elementor_controls_usage',
'_wp_attached_file',
// Add more if needed
];
foreach ( $changes as $key => $value ) {
// Skip blacklisted keys
if ( in_array($key, $disallowed_keys, true) ) {
continue;
}
// Sanitize key and value
$safe_key = sanitize_key($key);
if ( is_string($value) ) {
$safe_value = sanitize_text_field($value);
} elseif ( is_array($value) ) {
$safe_value = array_map('sanitize_text_field', $value);
} else {
$safe_value = sanitize_text_field((string) $value);
}
update_post_meta($post_id, $safe_key, $safe_value);
}
}
}
}
new WPR_Form_Builder_Submissions();
@@ -0,0 +1,161 @@
<?php
namespace WprAddons\Classes\Modules\Forms;
use Elementor\Utils;
use Elementor\Group_Control_Image_Size;
use WprAddons\Classes\Utilities;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* WPR_Subscribe_Mailchimp setup
*
* @since 3.4.6
*/
class WPR_Subscribe_Mailchimp {
public function __construct() {
// For form builder
add_action( 'wp_ajax_wpr_form_builder_mailchimp', [$this, 'wpr_form_builder_mailchimp'] );
add_action( 'wp_ajax_nopriv_wpr_form_builder_mailchimp', [$this, 'wpr_form_builder_mailchimp'] );
}
/**
** Mailchimp AJAX Subscribe
*/
public static function wpr_form_builder_mailchimp() {
$nonce = $_POST['nonce'];
if ( !wp_verify_nonce( $nonce, 'wpr-addons-js' ) ) {
return; // Get out of here, the nonce is rotten!
}
// API Key
$api_key = get_option('wpr_mailchimp_api_key') ? get_option('wpr_mailchimp_api_key') : '';
// Validate API key format
if (!preg_match('/^[0-9a-f]{32}-[a-z]{2}[0-9]{1,2}$/', $api_key)) {
wp_send_json_error('Invalid API key format');
return;
}
$api_parts = explode('-', $api_key);
if (count($api_parts) !== 2) {
wp_send_json_error('Invalid API key format');
return;
}
// Validate datacenter suffix
$api_key_suffix = $api_parts[1];
if (!preg_match('/^[a-z]{2}[0-9]{1,2}$/', $api_key_suffix)) {
wp_send_json_error('Invalid API datacenter');
return;
}
// List ID with sanitization
$list_id = isset($_POST['listId']) ? sanitize_text_field(wp_unslash($_POST['listId'])) : '';
// Get Available Fields (PHPCS - fields are sanitized later on input)
$fields = isset($_POST['form_data']) ? $_POST['form_data'] : []; // phpcs:ignore
$group_ids = isset($fields['group_id']) ? array_map('sanitize_text_field', array_map('trim', explode(',', wp_unslash($fields['group_id'])))) : [];
// Merge Additional Fields
$merge_fields = [
'FNAME' => !empty( $fields['first_name_field'] ) ? sanitize_text_field($fields['first_name_field']) : '',
'LNAME' => !empty( $fields['last_name_field'] ) ? sanitize_text_field($fields['last_name_field']) : '',
'PHONE' => !empty ( $fields['phone_field'] ) ? sanitize_text_field($fields['phone_field']) : '',
'BIRTHDAY' => !empty ( $fields['birthday_field'] ) ? sanitize_text_field($fields['birthday_field']) : '',
];
$requiredKeys = ['address_field', 'country_field', 'city_field', 'state_field', 'zip_field'];
if ( !empty(array_intersect_key($fields, array_flip($requiredKeys))) ) {
$merge_fields = array_merge($merge_fields, [
'ADDRESS' => [
'addr1' => !empty ( $fields['address_field'] ) ? sanitize_text_field($fields['address_field']) : 'none',
'country' => !empty ( $fields['country_field'] ) ? sanitize_text_field($fields['country_field']) : 'none',
'city' => !empty ( $fields['city_field'] ) ? sanitize_text_field($fields['city_field']) : 'none',
'state' => !empty ( $fields['state_field'] ) ? sanitize_text_field($fields['state_field']) : 'none',
'zip' =>!empty ( $fields['zip_field'] ) ? sanitize_text_field($fields['zip_field']) : 'none',
]
]);
}
// API URL
$api_url = 'https://'. $api_key_suffix .'.api.mailchimp.com/3.0/lists/'. $list_id .'/members/'. md5(strtolower(sanitize_text_field($fields['email_field'])));
$api_body = [
'email_address' => sanitize_text_field($fields[ 'email_field' ]),
'status' => 'subscribed',
'merge_fields' => $merge_fields
];
if ( !empty($group_ids) ) {
$api_body['interests'] = self::group_ids_to_interests_array($group_ids);
}
// API Args
$api_args = [
'method' => 'PUT',
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'apikey '. $api_key,
],
'body' => json_encode($api_body),
];
// Send Request
$request = wp_remote_post( $api_url, $api_args );
if ( ! is_wp_error($request) ) {
$request = json_decode( wp_remote_retrieve_body($request) );
// Set Status
if ( ! empty($request) ) {
if ($request->status == 'subscribed') {
wp_send_json_success(array(
'action' => 'wpr_form_builder_mailchimp',
'status' => 'success',
'message' => 'Mailchimp subscription was successful',
'request' => $request
));
} else {
wp_send_json_error([
'action' => 'wpr_form_builder_mailchimp',
'status' => 'error',
'message' => 'Mailchimp subscription failed',
'request' => $request
]);
}
}
} else {
wp_send_json_error([
'action' => 'wpr_form_builder_mailchimp',
'status' => 'error',
'message' => 'Mailchimp subscription failed',
'request' => $request
]);
}
}
public static function group_ids_to_interests_array($group_ids) {
$interests_array = [];
foreach ($group_ids as $group_id) {
$interests_array[$group_id] = true;
}
return $interests_array;
}
}
new WPR_Subscribe_Mailchimp();