PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.53
E2Pdf – Export Pdf Tool for WordPress v1.32.53
1.32.53 1.32.51 1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 All 74 releases
← All changes | classes/extension/e2pdf-divi.php +1668 -1120 1.07.11 → 1.32.53 View file →
@@ -1,14 +1,12 @@
1 1 <?php
2 2
3 3 /**
4 - * E2pdf Divi Extension
5 - *
6 - * @copyright Copyright 2017 https://e2pdf.com
7 - * @license GPL v2
8 - * @version 1
9 - * @link https://e2pdf.com
10 - * @since 0.00.01
4 + * File: /extension/e2pdf-divi.php
5 + *
6 + * @package E2Pdf
7 + * @license GPLv3
8 + * @link https://e2pdf.com
11 9 */
12 10 if (!defined('ABSPATH')) {
13 11 die('Access denied.');
14 12 }
@@ -15,43 +13,30 @@
15 13
16 14 class Extension_E2pdf_Divi extends Model_E2pdf_Model {
17 15
18 16 private $options;
19 - private $info = array(
17 + private $info = [
20 18 'key' => 'divi',
21 - 'title' => 'Divi'
22 - );
19 + 'title' => 'Divi Forms',
20 + ];
23 21
24 - function __construct() {
25 - parent::__construct();
26 - }
27 -
28 - /**
29 - * Get info about extension
30 - *
31 - * @param string $key - Key to get assigned extension info value
32 - *
33 - * @return array|string - Extension Key and Title or Assigned extension info value
34 - */
22 + // info
35 23 public function info($key = false) {
36 24 if ($key && isset($this->info[$key])) {
37 25 return $this->info[$key];
38 26 } else {
39 - return array(
40 - $this->info['key'] => $this->info['title']
41 - );
27 + return [
28 + $this->info['key'] => $this->info['title'],
29 + ];
42 30 }
43 31 }
44 32
45 - /**
46 - * Check if needed plugin active
47 - *
48 - * @return bool - Activated/Not Activated plugin
49 - */
33 + // active
50 34 public function active() {
51 - if (file_exists(get_template_directory() . "/et-pagebuilder/et-pagebuilder.php")) {
52 - require_once(get_template_directory() . "/et-pagebuilder/et-pagebuilder.php");
53 - if (defined('ET_BUILDER_THEME')) {
35 + if (file_exists(get_template_directory() . '/et-pagebuilder/et-pagebuilder.php')) {
36 + return true;
37 + } else {
38 + if (defined('E2PDF_DIVI_EXTENSION') || $this->helper->load('extension')->is_plugin_active('divi-builder/divi-builder.php')) {
54 39 return true;
55 40 }
56 41 }
57 42 return false;
@@ -56,120 +41,211 @@
56 41 }
57 42 return false;
58 43 }
59 44
60 - /**
61 - * Set option
62 - *
63 - * @param string $attr - Key of option
64 - * @param string $value - Value of option
65 - *
66 - * @return bool - Status of setting option
67 - */
45 + // set
68 46 public function set($key, $value) {
69 47 if (!isset($this->options)) {
70 48 $this->options = new stdClass();
71 49 }
50 + $this->options->$key = $value;
51 + switch ($key) {
52 + case 'dataset':
53 + $this->set('cached_entry', []);
54 + $this->set('cached_meta', []);
55 + if ($this->get('item') && $this->get('dataset')) {
56 + $entry = new Model_E2pdf_Dataset();
57 + if ($entry->load($this->get('dataset'), $this->get('item'), 'divi')) {
58 + $cached_entry = $entry->get('entry');
59 + $this->set('cached_entry', $cached_entry);
72 60
73 - $this->options->$key = $value;
61 + $meta_data = [];
62 + if ($cached_entry && is_array($cached_entry)) {
63 + // Divi5
64 + if (!empty($cached_entry['E2PDF_VERSION'])) {
65 + foreach ($cached_entry['fields'] as $field_key => $field) {
66 + if (isset($field['type'])) {
67 + switch ($field['type']) {
68 + case 'signature-pad':
69 + $meta_data['%%' . $field_key . '%%'] = $this->pwh_dcfh_resolve_file_url($field['value'], $cached_entry['options']);
70 + break;
71 + case 'file':
72 + $attachments = is_array($field['value']) ? $field['value'] : explode(',', $field['value']);
73 + foreach ($attachments as $attachment_key => $attachment) {
74 + $attachments[$attachment_key] = $this->pwh_dcfh_resolve_file_url($attachment, $cached_entry['options']);
75 + }
76 + $meta_data['%%' . $field_key . '%%'] = implode(',', $attachments);
77 + break;
78 + default:
79 + $meta_data['%%' . $field_key . '%%'] = is_array($field['value']) ? implode(', ', $field['value']) : $field['value'];
80 + break;
81 + }
82 + }
83 + }
84 + } elseif (!empty($cached_entry['ET_CORE_VERSION'])) {
85 + foreach ($cached_entry as $field_key => $value) {
86 + $meta_data['%%' . $field_key . '%%'] = is_array($value) ? implode(', ', $value) : $value;
87 + }
88 + if (!isset($meta_data['%%e2pdf_entry_id%%'])) {
89 + $meta_data['%%e2pdf_entry_id%%'] = (int) $this->get('dataset');
90 + }
91 + } else {
92 + // Divi4
93 + $processed_fields_values = [];
94 + $et_contact_proccess = array_search('et_contact_proccess', $cached_entry);
95 + $et_pb_contact_form_num = $et_contact_proccess === false ? 0 : str_replace('et_pb_contactform_submit_', '', $et_contact_proccess);
96 + $current_form_fields = isset($cached_entry['et_pb_contact_email_fields_' . $et_pb_contact_form_num]) ? $cached_entry['et_pb_contact_email_fields_' . $et_pb_contact_form_num] : '';
97 + if ('' !== $current_form_fields) {
98 + $fields_data_json = str_replace('\\', '', $current_form_fields);
99 + $fields_data_array = json_decode($fields_data_json, true);
100 + if (!empty($fields_data_array)) {
101 + foreach ($fields_data_array as $field_value) {
102 + $processed_fields_values[$field_value['original_id']]['value'] = isset($cached_entry[$field_value['field_id']]) ? $cached_entry[$field_value['field_id']] : '';
103 + $processed_fields_values[$field_value['original_id']]['label'] = $field_value['field_label'];
104 +
105 + $is_file = isset($cached_entry[$field_value['field_id'] . '_is_file']) && $cached_entry[$field_value['field_id'] . '_is_file'] == 'yes';
106 + $is_signature_pad = isset($cached_entry[$field_value['field_id'] . '_is_signature_pad']) && $cached_entry[$field_value['field_id'] . '_is_signature_pad'] == 'yes';
107 +
108 + if (($is_signature_pad || $is_file) && $processed_fields_values[$field_value['original_id']]['value']) {
109 + $subdir = isset($cached_entry['_subdir']) ? $cached_entry['_subdir'] : '';
110 + if (isset($cached_entry['_save_files_to_media']) && $cached_entry['_save_files_to_media'] == 'on') {
111 + if ($subdir && !file_exists(path_join(wp_upload_dir()['basedir'] . $subdir, $processed_fields_values[$field_value['original_id']]['value'])) && preg_match('/^\/\d{4}\/\d{2}$/', $subdir)) {
112 + $tmpsubdir = '/' . date('Y/m', strtotime(str_replace('/', '-', ltrim($subdir, '/')) . ' -1 month'));
113 + if (file_exists(path_join(wp_upload_dir()['basedir'] . $tmpsubdir, $processed_fields_values[$field_value['original_id']]['value']))) {
114 + $subdir = $tmpsubdir;
115 + }
116 + }
117 + $processed_fields_values[$field_value['original_id']]['value'] = path_join(wp_upload_dir()['baseurl'] . $subdir, $processed_fields_values[$field_value['original_id']]['value']);
118 + } else {
119 + $contact_form_id = isset($cached_entry['_unique_id']) ? $cached_entry['_unique_id'] : '';
120 + if (function_exists('pwh_dcfh_file_helpers') && $contact_form_id) {
121 + if ($subdir && !file_exists(pwh_dcfh_file_helpers()::get_form_upload_dir($contact_form_id, $subdir, $processed_fields_values[$field_value['original_id']]['value'])) && preg_match('/^\d{4}\/\d{2}$/', $subdir)) {
122 + $tmpsubdir = date('Y/m', strtotime(str_replace('/', '-', ltrim($subdir, '/')) . " -1 month"));
123 + if (file_exists(pwh_dcfh_file_helpers()::get_form_upload_dir($contact_form_id, $tmpsubdir, $processed_fields_values[$field_value['original_id']]['value']))) {
124 + $subdir = $tmpsubdir;
125 + }
126 + }
127 + $processed_fields_values[$field_value['original_id']]['value'] = pwh_dcfh_file_helpers()::get_form_upload_url($contact_form_id, $subdir, $processed_fields_values[$field_value['original_id']]['value']);
128 + } elseif (function_exists('ks_pac_dcfh_file_helper') && $contact_form_id) {
129 + if ($subdir && !file_exists(ks_pac_dcfh_file_helper()::get_form_upload_dir($contact_form_id, $subdir, $processed_fields_values[$field_value['original_id']]['value'])) && preg_match('/^\d{4}\/\d{2}$/', $subdir)) {
130 + $tmpsubdir = date('Y/m', strtotime(str_replace('/', '-', ltrim($subdir, '/')) . " -1 month"));
131 + if (file_exists(ks_pac_dcfh_file_helper()::get_form_upload_dir($contact_form_id, $tmpsubdir, $processed_fields_values[$field_value['original_id']]['value']))) {
132 + $subdir = $tmpsubdir;
133 + }
134 + }
135 + $processed_fields_values[$field_value['original_id']]['value'] = ks_pac_dcfh_file_helper()::get_form_upload_url($contact_form_id, $subdir, $processed_fields_values[$field_value['original_id']]['value']);
136 + } else {
137 + $processed_fields_values[$field_value['original_id']]['value'] = '';
138 + }
139 + }
140 + }
141 + }
142 + }
143 + }
144 + if (!isset($processed_fields_values['_wp_http_referer'])) {
145 + $processed_fields_values['_wp_http_referer'] = [
146 + 'value' => isset($cached_entry['_wp_http_referer']) ? $cached_entry['_wp_http_referer'] : '',
147 + 'label' => '_wp_http_referer',
148 + ];
149 + }
150 + if (!isset($processed_fields_values['e2pdf_entry_id'])) {
151 + $processed_fields_values['e2pdf_entry_id'] = [
152 + 'value' => (int) $this->get('dataset'),
153 + 'label' => 'e2pdf_entry_id',
154 + ];
155 + }
156 + foreach ($processed_fields_values as $field_key => $field_value) {
157 + $meta_data['%%' . $field_key . '%%'] = $field_value['value'];
158 + }
159 + }
160 + }
161 + $this->set('cached_meta', $meta_data);
162 + }
163 + }
164 + break;
165 + default:
166 + break;
167 + }
74 168 }
75 169
76 - /**
77 - * Get option by key
78 - *
79 - * @param string $key - Key to get assigned option value
80 - *
81 - * @return mixed
82 - */
170 + // get
83 171 public function get($key) {
84 172 if (isset($this->options->$key)) {
85 173 $value = $this->options->$key;
86 - return $value;
87 - } elseif ($key == 'args') {
88 - return array();
89 174 } else {
90 - return false;
175 + switch ($key) {
176 + case 'args':
177 + case 'cached_entry':
178 + case 'cached_meta':
179 + $value = [];
180 + break;
181 + default:
182 + $value = false;
183 + break;
184 + }
91 185 }
186 + return $value;
92 187 }
93 188
94 - /**
95 - * Get items to work with
96 - *
97 - * @return array() - List of available items
98 - */
189 + // items
99 190 public function items() {
100 191 global $wpdb;
101 -
102 - $condition = array(
103 - 'post_content' => array(
192 + $condition = [
193 + 'post_content' => [
104 194 'condition' => 'LIKE',
105 195 'value' => '%et_pb_contact_form%',
106 - 'type' => '%s'
107 - ),
108 - 'post_type' => array(
196 + 'type' => '%s',
197 + 'or' => [
198 + [
199 + 'post_content' => [
200 + 'condition' => 'LIKE',
201 + 'value' => '%wp:divi/contact-form%',
202 + 'type' => '%s',
203 + ],
204 + ],
205 + ],
206 + ],
207 + 'post_type' => [
109 208 'condition' => '<>',
110 - 'value' => array(
209 + 'value' => [
111 210 'revision',
112 - 'et_pb_layout'
113 - ),
114 - 'type' => '%s'
115 - ),
116 - );
117 -
118 - $order_condition = array(
211 + 'et_pb_layout',
212 + ],
213 + 'type' => '%s',
214 + ],
215 + ];
216 + $order_condition = [
119 217 'orderby' => 'id',
120 218 'order' => 'desc',
121 - );
122 -
219 + ];
123 220 $where = $this->helper->load('db')->prepare_where($condition);
124 221 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
222 + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
223 + $posts = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . $wpdb->prefix . 'posts`' . $where['sql'] . $orderby . '', $where['filter']));
125 224
126 - $posts = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . 'posts' . $where['sql'] . $orderby . "", $where['filter']));
127 -
128 - $content = array();
225 + $content = [];
129 226 foreach ($posts as $key => $post) {
130 - if ($forms_labels = $this->get_forms($post->post_content)) {
131 - foreach ($forms_labels as $form_key => $form_value) {
132 - $content[] = $this->item($form_key);
133 - }
227 + foreach ($this->get_forms($post->post_content) as $form_key => $form_value) {
228 + $content[] = $this->item($form_key);
134 229 }
135 230 }
136 231 return $content;
137 232 }
138 233
139 - /**
140 - * Parse available forms from pages
141 - *
142 - * @param string $content - Page content
143 - *
144 - * @return array() - Forms list
145 - */
234 + // get forms
146 235 public function get_forms($content) {
236 + $forms = [];
147 237
148 - $forms = array();
149 - if (false !== strpos($content, 'et_pb_contact_form')) {
150 - $shortcode_tags = array(
238 + if ($this->divi4_form($content)) {
239 + $shortcode_tags = [
151 240 'et_pb_contact_form',
152 - );
153 -
241 + ];
154 242 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
155 243 $tagnames = array_intersect($shortcode_tags, $matches[1]);
156 -
157 244 if (!empty($tagnames)) {
158 -
159 - $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
160 -
161 - preg_match_all("/$pattern/", $content, $shortcodes);
245 + preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $content, $shortcodes);
162 246 foreach ($shortcodes[0] as $key => $shortcode_value) {
163 -
164 - $shortcode = array();
165 - $shortcode[1] = $shortcodes[1][$key];
166 - $shortcode[2] = $shortcodes[2][$key];
167 - $shortcode[3] = $shortcodes[3][$key];
168 - $shortcode[4] = $shortcodes[4][$key];
169 - $shortcode[5] = $shortcodes[5][$key];
170 - $shortcode[6] = $shortcodes[6][$key];
171 -
247 + $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
172 248 preg_match_all('/admin_label="(.*?)"/', $shortcode[3], $labels);
173 249 if (isset($labels['1'])) {
174 250 foreach ($labels['1'] as $label) {
175 251 $forms[$label] = $label;
@@ -176,533 +252,338 @@
176 252 }
177 253 }
178 254 }
179 255 }
256 + } elseif ($this->divi5_form($content)) {
257 + $blocks = $this->get_forms_blocks(parse_blocks($content));
258 + foreach ($blocks as $block) {
259 + $label = !empty($block['attrs']['module']['meta']['adminLabel']['desktop']['value']) ? $block['attrs']['module']['meta']['adminLabel']['desktop']['value'] : '';
260 + if ($label) {
261 + $forms[$label] = $label;
262 + }
263 + }
180 264 }
181 -
182 265 return $forms;
183 266 }
184 267
185 - /**
186 - * Get entries for export
187 - *
188 - * @param string $item - Item
189 - * @param string $name - Entries names
190 - *
191 - * @return array() - Entries list
192 - */
193 - public function datasets($item = false, $name = false) {
194 -
268 + // datasets
269 + public function datasets($item_id = false, $name = false) {
195 270 global $wpdb;
196 -
197 - $datasets = array();
198 -
199 - if ($item) {
200 - $condition = array(
201 - 'extension' => array(
271 + $datasets = [];
272 + if ($item_id) {
273 + $condition = [
274 + 'extension' => [
202 275 'condition' => '=',
203 276 'value' => 'divi',
204 - 'type' => '%s'
205 - ),
206 - 'item' => array(
277 + 'type' => '%s',
278 + ],
279 + 'item' => [
207 280 'condition' => '=',
208 - 'value' => $item,
209 - 'type' => '%s'
210 - ),
211 - );
212 -
213 - $order_condition = array(
281 + 'value' => $item_id,
282 + 'type' => '%s',
283 + ],
284 + ];
285 + $order_condition = [
214 286 'orderby' => 'ID',
215 287 'order' => 'desc',
216 - );
217 -
288 + ];
218 289 $where = $this->helper->load('db')->prepare_where($condition);
219 290 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
291 + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
292 + $entries = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . $wpdb->prefix . 'e2pdf_datasets`' . $where['sql'] . $orderby . '', $where['filter']));
220 293
221 - $datasets_tmp = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . 'e2pdf_datasets' . $where['sql'] . $orderby . "", $where['filter']));
222 -
223 - if ($datasets_tmp) {
224 - foreach ($datasets_tmp as $key => $dataset) {
225 - $this->set('item', $item);
226 - $this->set('dataset', $dataset->ID);
227 - $dataset_title = $this->render($name);
228 - if (!$dataset_title) {
229 - $dataset_title = $dataset->ID;
294 + if ($entries) {
295 + $this->set('item', $item_id);
296 + foreach ($entries as $key => $entry) {
297 + $this->set('dataset', $entry->ID);
298 + $entry_title = $this->render($name);
299 + if (!$entry_title) {
300 + $entry_title = $entry->ID;
230 301 }
231 - $datasets[] = array(
232 - 'key' => $dataset->ID,
233 - 'value' => $dataset_title
234 - );
302 + $datasets[] = [
303 + 'key' => $entry->ID,
304 + 'value' => $entry_title,
305 + ];
235 306 }
236 307 }
237 308 }
238 -
239 309 return $datasets;
240 310 }
241 311
242 - /**
243 - * Get dataset
244 - *
245 - * @param int $dataset - Dataset ID
246 - *
247 - * @return object - Dataset
248 - */
249 - public function dataset($dataset = false) {
312 + // get dataset actions
313 + public function get_dataset_actions($dataset_id = false) {
314 + $dataset_id = (int) $dataset_id;
315 + if (!$dataset_id) {
316 + return;
317 + }
318 + $actions = new stdClass();
319 + $actions->view = false;
320 + $actions->delete = true;
321 + return $actions;
322 + }
250 323
251 - $dataset = (int) $dataset;
252 -
253 - if (!$dataset) {
324 + // get template actions
325 + public function get_template_actions($template = false) {
326 + $template = (int) $template;
327 + if (!$template) {
254 328 return;
255 329 }
330 + $actions = new stdClass();
331 + $actions->delete = true;
332 + return $actions;
333 + }
256 334
257 - $data = new stdClass();
258 - $data->url = false;
335 + // get forms blocks
336 + public function get_forms_blocks($blocks) {
337 + $forms = [];
338 + foreach ($blocks as $block) {
339 + if ('divi/contact-form' === $block['blockName']) {
340 + $forms[] = $block;
341 + }
342 + if (!empty($block['innerBlocks'])) {
343 + $nested_forms = $this->get_forms_blocks($block['innerBlocks']);
344 + $forms = array_merge($forms, $nested_forms);
345 + }
346 + }
347 + return $forms;
348 + }
259 349
260 - return $data;
350 + // get fields blocks
351 + public function get_fields_blocks($form) {
352 + $fields = [];
353 + if (!empty($form['innerBlocks'])) {
354 + foreach ($form['innerBlocks'] as $block) {
355 + if ('divi/contact-field' === $block['blockName']) {
356 + $field_type = !empty($block['attrs']['fieldItem']['advanced']['type']['desktop']['value']) ? $block['attrs']['fieldItem']['advanced']['type']['desktop']['value'] : '';
357 + $field_label = !empty($block['attrs']['fieldItem']['innerContent']['desktop']['value']) ? $block['attrs']['fieldItem']['innerContent']['desktop']['value'] : '';
358 + $field_id = !empty($block['attrs']['fieldItem']['advanced']['id']['desktop']['value']) ? $block['attrs']['fieldItem']['advanced']['id']['desktop']['value'] : '';
359 + $fields[] = [
360 + 'field_type' => $field_type,
361 + 'field_title' => $field_label,
362 + 'field_id' => $field_id,
363 + ];
364 + }
365 + if (!empty($block['innerBlocks'])) {
366 + $nested_fields = $this->get_fields_blocks($block);
367 + $fields = array_merge($fields, $nested_fields);
368 + }
369 + }
370 + }
371 + return $fields;
261 372 }
262 373
263 - /**
264 - * Get item
265 - *
266 - * @param string $item - Item
267 - *
268 - * @return object - Item
269 - */
270 - public function item($item = false) {
271 -
272 - $form = new stdClass();
273 -
274 - if (!$item && $this->get('item')) {
275 - $item = $this->get('item');
374 + // item
375 + public function item($item_id = false) {
376 + if (!$item_id && $this->get('item')) {
377 + $item_id = $this->get('item');
276 378 }
277 -
278 - if ($item) {
279 - $form->id = (string) $item;
280 - $form->name = $item;
281 - $post = $this->get_post($item);
282 - $form->url = isset($post->ID) ? $this->helper->get_url(array('post' => $post->ID, 'action' => 'edit'), 'post.php?') : 'javascript:void(0);';
379 + $item = new stdClass();
380 + if ($item_id) {
381 + $item->id = (string) $item_id;
382 + $item->name = $item_id;
383 + $form = $this->get_form($item_id);
384 + if (isset($form->ID)) {
385 + $item->url = $this->helper->get_url(
386 + [
387 + 'post' => $form->ID,
388 + 'action' => 'edit',
389 + ], 'post.php?'
390 + );
391 + } else {
392 + $item->url = '';
393 + }
283 394 } else {
284 - $form->id = '';
285 - $form->name = '';
286 - $form->url = 'javascript:void(0);';
395 + $item->id = '';
396 + $item->name = '';
397 + $item->url = '';
287 398 }
288 - return $form;
399 + return $item;
289 400 }
290 401
291 - /**
292 - * Get post
293 - *
294 - * @param string $item - Form label
295 - *
296 - * @return object - Post
297 - */
298 - public function get_post($item = false) {
402 + // get form
403 + public function get_form($item_id = false) {
299 404 global $wpdb;
300 -
301 405 $item_post = false;
302 -
303 - $condition = array(
304 - 'post_content' => array(
406 + $condition = [
407 + 'post_content' => [
305 408 'condition' => 'LIKE',
306 - 'value' => '%admin_label="' . $item . '"%',
307 - 'type' => '%s'
308 - ),
309 - 'post_type' => array(
409 + 'value' => '%admin_label="' . $item_id . '"%',
410 + 'type' => '%s',
411 + 'or' => [
412 + [
413 + 'post_content' => [
414 + 'condition' => 'LIKE',
415 + 'value' => '%"meta":{"adminLabel":{"desktop":{"value":"' . $item_id . '"%',
416 + 'type' => '%s',
417 + ],
418 + ],
419 + ],
420 + ],
421 + 'post_type' => [
310 422 'condition' => '<>',
311 - 'value' => array(
423 + 'value' => [
312 424 'revision',
313 - 'et_pb_layout'
314 - ),
315 - 'type' => '%s'
316 - ),
317 - );
425 + 'et_pb_layout',
426 + ],
427 + 'type' => '%s',
428 + ],
429 + ];
318 430
319 - $order_condition = array(
431 + $order_condition = [
320 432 'orderby' => 'id',
321 433 'order' => 'desc',
322 - );
434 + ];
323 435
324 436 $where = $this->helper->load('db')->prepare_where($condition);
325 437 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
326 -
327 - $posts = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . 'posts' . $where['sql'] . $orderby . "", $where['filter']));
328 - foreach ($posts as $key => $post) {
329 - if ($forms_labels = $this->get_forms($post->post_content)) {
330 - if (in_array($item, $forms_labels)) {
331 - $item_post = $post;
332 - break;
333 - }
438 + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
439 + $posts = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . $wpdb->prefix . 'posts`' . $where['sql'] . $orderby . '', $where['filter']));
440 + foreach ($posts as $post) {
441 + if (in_array($item_id, $this->get_forms($post->post_content))) {
442 + $item_post = $post;
443 + break;
334 444 }
335 445 }
336 446 return $item_post;
337 447 }
338 448
339 - /**
340 - * Render value according to content
341 - *
342 - * @param string $value - Content
343 - * @param string $type - Type of rendering value
344 - * @param array $field - Field details
345 - *
346 - * @return string - Fully rendered value
347 - */
348 - public function render($value, $type = false, $field = array()) {
349 -
350 - $value = $this->render_shortcodes($value, $type, $field);
351 - $value = $this->strip_shortcodes($value);
352 -
353 - if ($type === 'value' && isset($field['type']) && $field['type'] === 'e2pdf-checkbox' && isset($field['properties']['option'])) {
354 - $option = $this->render($field['properties']['option']);
355 - $options = explode(', ', $value);
356 - $option_options = explode(', ', $option);
357 - if (is_array($options) && is_array($option_options) && !array_diff($option_options, $options)) {
358 - return $option;
359 - } else {
360 - return "";
361 - }
449 + // render
450 + public function render($value, $field = [], $convert_shortcodes = true, $raw = false) {
451 + $value = $this->render_shortcodes($value, $field);
452 + if (!$raw) {
453 + $value = $this->strip_shortcodes($value);
454 + $value = $this->convert_shortcodes($value, $convert_shortcodes, isset($field['type']) && $field['type'] == 'e2pdf-html' ? true : false);
455 + $value = $this->helper->load('field')->render_checkbox($value, $this, $field);
362 456 }
363 -
364 - if ($type === 'value') {
365 - $value = $this->convert_shortcodes($value, true);
366 - }
367 -
368 457 return $value;
369 458 }
370 459
371 - /**
372 - * Render shortcodes which available in this extension
373 - *
374 - * @param string $value - Content
375 - * @param string $type - Type of rendering value
376 - * @param array $field - Field details
377 - *
378 - * @return string - Value with rendered shortcodes
379 - */
380 - public function render_shortcodes($value, $type = false, $field = array()) {
381 - global $wpdb;
382 -
383 - $dataset = $this->get('dataset');
384 - $item = $this->get('item');
385 - $user_id = $this->get('user_id');
386 - $args = $this->get('args');
387 - $template_id = $this->get('template_id') ? $this->get('template_id') : '0';
388 - $element_id = isset($field['element_id']) ? $field['element_id'] : '0';
389 -
460 + // render shortcodes
461 + public function render_shortcodes($value, $field = []) {
462 + $element_id = isset($field['element_id']) ? $field['element_id'] : false;
390 463 if ($this->verify()) {
391 -
392 - $args = apply_filters('e2pdf_extension_render_shortcodes_args', $args, $element_id, $template_id, $item, $dataset);
393 -
394 - $condition = array(
395 - 'ID' => array(
396 - 'condition' => '=',
397 - 'value' => $dataset,
398 - 'type' => '%d'
399 - ),
400 - 'item' => array(
401 - 'condition' => '=',
402 - 'value' => $item,
403 - 'type' => '%s'
404 - ),
405 - 'extension' => array(
406 - 'condition' => '=',
407 - 'value' => 'divi',
408 - 'type' => '%s'
409 - ),
464 + if (false !== strpos($value, '[')) {
465 + $value = $this->helper->load('field')->pre_shortcodes($value, $this, $field);
466 + $value = $this->helper->load('field')->inner_shortcodes($value, $this, $field);
467 + $value = $this->helper->load('field')->wrapper_shortcodes($value, $this, $field);
468 + }
469 + $value = $this->helper->load('field')->do_shortcodes($value, $this, $field);
470 + if (!empty($this->get('cached_meta'))) {
471 + $value = $this->helper->load('convert')->stritr($value, $this->get('cached_meta'));
472 + }
473 + $value = $this->helper->load('field')->render(
474 + apply_filters('e2pdf_extension_render_shortcodes_pre_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false),
475 + $this,
476 + $field
410 477 );
411 -
412 - $where = $this->helper->load('db')->prepare_where($condition);
413 -
414 - $entry = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . 'e2pdf_datasets' . $where['sql'] . "", $where['filter']));
415 -
416 - $processed_fields_values = array();
417 - if ($entry) {
418 - $post = unserialize($entry->entry);
419 - if ($post && is_array($post)) {
420 -
421 - $et_pb_contact_form_num = 0;
422 -
423 - $current_form_fields = isset($post['et_pb_contact_email_fields_' . $et_pb_contact_form_num]) ? $post['et_pb_contact_email_fields_' . $et_pb_contact_form_num] : '';
424 - $hidden_form_fields = isset($post['et_pb_contact_email_hidden_fields_' . $et_pb_contact_form_num]) ? $post['et_pb_contact_email_hidden_fields_' . $et_pb_contact_form_num] : false;
425 - $processed_fields_values = array();
426 -
427 - if ('' !== $current_form_fields) {
428 - $fields_data_json = str_replace('\\', '', $current_form_fields);
429 - $fields_data_array = json_decode($fields_data_json, true);
430 -
431 - if (!empty($fields_data_array)) {
432 - foreach ($fields_data_array as $index => $field_value) {
433 - $processed_fields_values[$field_value['original_id']]['value'] = isset($post[$field_value['field_id']]) ? $post[$field_value['field_id']] : '';
434 - $processed_fields_values[$field_value['original_id']]['label'] = $field_value['field_label'];
435 - }
436 - }
437 - }
438 - }
439 -
440 - $replace = array();
441 - foreach ($processed_fields_values as $field_key => $field_value) {
442 - $replace["%%" . $field_key . "%%"] = wp_strip_all_tags($field_value['value']);
443 - }
444 -
445 - if (false !== strpos($value, '[')) {
446 -
447 - $shortcode_tags = array(
448 - 'e2pdf-user',
449 - 'e2pdf-arg'
450 - );
451 - preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
452 - $tagnames = array_intersect($shortcode_tags, $matches[1]);
453 -
454 - if (!empty($tagnames)) {
455 -
456 - $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
457 -
458 - preg_match_all("/$pattern/", $value, $shortcodes);
459 -
460 - foreach ($shortcodes[0] as $key => $shortcode_value) {
461 - $shortcode = array();
462 - $shortcode[1] = $shortcodes[1][$key];
463 - $shortcode[2] = $shortcodes[2][$key];
464 - $shortcode[3] = $shortcodes[3][$key];
465 - $shortcode[4] = $shortcodes[4][$key];
466 - $shortcode[5] = $shortcodes[5][$key];
467 - $shortcode[6] = $shortcodes[6][$key];
468 -
469 - $atts = shortcode_parse_atts($shortcode[3]);
470 -
471 - if ($shortcode['2'] == 'e2pdf-user') {
472 - if (!isset($atts['id']) && $user_id) {
473 - $shortcode[3] .= " id=\"" . $user_id . "\"";
474 - $value = str_replace($shortcode_value, "[" . $shortcode['2'] . $shortcode['3'] . "]", $value);
475 - }
476 - } elseif ($shortcode['2'] == 'e2pdf-arg') {
477 - if (isset($atts['key']) && isset($args[$atts['key']])) {
478 - $sub_value = $this->strip_shortcodes($args[$atts['key']]);
479 - $value = str_replace($shortcode_value, $sub_value, $value);
480 - } else {
481 - $value = str_replace($shortcode_value, '', $value);
482 - }
483 - }
484 - }
485 - }
486 -
487 - $shortcode_tags = array(
488 - 'e2pdf-format-number',
489 - 'e2pdf-format-date',
490 - 'e2pdf-format-output',
491 - );
492 - $shortcode_tags = apply_filters('e2pdf_extension_render_shortcodes_tags', $shortcode_tags);
493 - preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
494 - $tagnames = array_intersect($shortcode_tags, $matches[1]);
495 -
496 - if (!empty($tagnames)) {
497 -
498 - $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
499 -
500 - preg_match_all("/$pattern/", $value, $shortcodes);
501 - foreach ($shortcodes[0] as $key => $shortcode_value) {
502 - $shortcode = array();
503 - $shortcode[1] = $shortcodes[1][$key];
504 - $shortcode[2] = $shortcodes[2][$key];
505 - $shortcode[3] = $shortcodes[3][$key];
506 - $shortcode[4] = $shortcodes[4][$key];
507 - $shortcode[5] = $shortcodes[5][$key];
508 - $shortcode[6] = $shortcodes[6][$key];
509 -
510 - if (!$shortcode['5']) {
511 - $sub_value = '';
512 - } elseif (isset($field['type']) && ($field['type'] === 'e2pdf-image' || $field['type'] === 'e2pdf-signature')) {
513 - $sub_value = $this->render($shortcode['5'], $type);
514 - } else {
515 - $sub_value = $this->render($shortcode['5'], $type, $field);
516 - }
517 - $value = str_replace($shortcode_value, "[" . $shortcode['2'] . $shortcode['3'] . "]" . $sub_value . "[/" . $shortcode['2'] . "]", $value);
518 - }
519 - }
520 - }
521 -
522 - $value = do_shortcode($value);
523 -
524 - if ($entry) {
525 - $value = $this->helper->load('convert')->stritr($value, $replace);
526 - }
527 -
528 - if (isset($field['type']) && ($field['type'] === 'e2pdf-image' || $field['type'] === 'e2pdf-signature')) {
529 - $esig = isset($field['properties']['esig']) && $field['properties']['esig'] ? true : false;
530 - if ($esig) {
531 - //process e-signature
532 - $value = "";
533 - } else {
534 - if (!$this->helper->load('image')->get_image($value)) {
535 - $value = $this->strip_shortcodes($value);
536 - if (
537 - $value &&
538 - trim($value) != "" &&
539 - extension_loaded('gd') &&
540 - function_exists('imagettftext')
541 - ) {
542 - if (isset($field['properties']['text_color']) && $field['properties']['text_color']) {
543 - $penColour = $this->helper->load('convert')->to_hex_color($field['properties']['text_color']);
544 - } else {
545 - $penColour = array(0x14, 0x53, 0x94);
546 - }
547 -
548 - $default_options = array(
549 - 'imageSize' => array(isset($field['width']) ? $field['width'] : '400', isset($field['height']) ? $field['height'] : '150'),
550 - 'bgColour' => 'transparent',
551 - 'penColour' => $penColour
552 - );
553 -
554 - $options = array();
555 - $options = apply_filters('e2pdf_image_sig_output_options', $options, $element_id, $template_id);
556 - $options = array_merge($default_options, $options);
557 -
558 - $model_e2pdf_font = new Model_E2pdf_Font();
559 -
560 - $font = false;
561 - if (isset($field['properties']['text_font']) && $field['properties']['text_font']) {
562 - $font = $model_e2pdf_font->get_font_path($field['properties']['text_font']);
563 - }
564 - if (!$font) {
565 - $font = $model_e2pdf_font->get_font_path('Noto Sans');
566 - }
567 -
568 - $size = 150;
569 - if (isset($field['properties']['text_font_size']) && $field['properties']['text_font_size']) {
570 - $size = $field['properties']['text_font_size'];
571 - }
572 -
573 - $model_e2pdf_signature = new Model_E2pdf_Signature();
574 - $value = $model_e2pdf_signature->ttf_signature($value, $size, $font, $options);
575 - } else {
576 - $value = "";
577 - }
578 - }
579 - }
580 - } else {
581 - $value = $this->convert_shortcodes($value);
582 - }
583 - }
584 478 }
585 -
586 - $value = apply_filters('e2pdf_extension_render_shortcodes_value', $value, $element_id, $template_id, $item, $dataset);
587 -
588 - return $value;
479 + return apply_filters(
480 + 'e2pdf_extension_render_shortcodes_value', $value, $element_id, $this->get('template_id'), $this->get('item'), $this->get('dataset'), false, false
481 + );
589 482 }
590 483
591 - /**
592 - * Strip unused shortcodes
593 - *
594 - * @param string $value - Content
595 - *
596 - * @return string - Value with removed unused shortcodes
597 - */
484 + // strip shortcodes
598 485 public function strip_shortcodes($value) {
599 - $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', "", $value);
600 - $value = preg_replace('~(?:%%/?)[^/%%]+/?%%~s', "", $value);
486 + $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', '', $value);
487 + $value = preg_replace('~%%[^%%]*%%~', '', $value);
601 488 return $value;
602 489 }
603 490
604 - /**
605 - * Convert "shortcodes" inside value string
606 - *
607 - * @param string $value - Value string
608 - * @param bool $to - Convert From/To
609 - *
610 - * @return string - Converted value
611 - */
612 - public function convert_shortcodes($value, $to = false) {
491 + // convert shortcodes
492 + public function convert_shortcodes($value, $to = false, $html = false) {
613 493 if ($value) {
614 494 if ($to) {
615 - $value = str_replace("&#91;", "[", $value);
616 - //$value = str_replace("&#37;", "%", $value);
495 + $value = stripslashes_deep($value);
496 + $value = str_replace('&#91;', '[', $value);
497 + if (!$html) {
498 + $value = wp_specialchars_decode($value, ENT_QUOTES);
499 + }
617 500 } else {
618 - $value = str_replace("[", "&#91;", $value);
619 - //$value = str_replace("%", "&#37;", $value);
501 + $value = str_replace('[', '&#91;', $value);
620 502 }
621 503 }
622 504 return $value;
623 505 }
624 506
507 + // auto
625 508 public function auto() {
626 509
627 - $response = array();
628 - $elements = array();
510 + $elements = [];
629 511
630 512 if ($this->get('item')) {
631 - $post = $this->get_post($this->get('item'));
513 + $post = $this->get_form($this->get('item'));
632 514 if ($post && isset($post->post_content)) {
633 515
516 + $source = '';
634 517 $content = $post->post_content;
635 518
636 - if (false !== strpos($content, '[')) {
637 - $shortcode_tags = array(
519 + if ($this->divi4_form($content)) {
520 + $shortcode_tags = [
638 521 'et_pb_contact_form',
639 - );
640 -
522 + ];
641 523 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
642 524 $tagnames = array_intersect($shortcode_tags, $matches[1]);
643 -
644 525 if (!empty($tagnames)) {
645 -
646 - $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
647 -
648 - preg_match_all("/$pattern/", $content, $shortcodes);
649 -
526 + preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $content, $shortcodes);
650 527 foreach ($shortcodes[0] as $key => $shortcode_value) {
651 -
652 - $shortcode = array();
653 - $shortcode[1] = $shortcodes[1][$key];
654 - $shortcode[2] = $shortcodes[2][$key];
655 - $shortcode[3] = $shortcodes[3][$key];
656 - $shortcode[4] = $shortcodes[4][$key];
657 - $shortcode[5] = $shortcodes[5][$key];
658 - $shortcode[6] = $shortcodes[6][$key];
659 -
528 + $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
660 529 $atts = shortcode_parse_atts($shortcode[3]);
661 530 if (isset($atts['admin_label']) && $atts['admin_label'] == $this->get('item')) {
662 -
663 - require_once(ET_BUILDER_DIR . 'class-et-builder-element.php');
664 - require_once(ET_BUILDER_DIR . 'functions.php');
665 - require_once(ET_BUILDER_DIR . 'ab-testing.php');
666 - require_once(ET_BUILDER_DIR . 'class-et-global-settings.php');
667 - require_once(ET_BUILDER_DIR . 'module/ContactForm.php');
668 - require_once(ET_BUILDER_DIR . 'module/ContactFormItem.php');
669 -
670 - new ET_Builder_Module_Contact_Form();
671 - new ET_Builder_Module_Contact_Form_Item();
672 -
531 + $this->divi4_load();
673 532 $source = do_shortcode($shortcode_value);
533 + }
534 + }
535 + }
536 + } elseif ($this->divi5_form($content)) {
537 + $blocks = $this->get_forms_blocks(parse_blocks($content));
538 + foreach ($blocks as $key => $block) {
539 + $label = !empty($block['attrs']['module']['meta']['adminLabel']['desktop']['value']) ? $block['attrs']['module']['meta']['adminLabel']['desktop']['value'] : '';
540 + if ($label == $this->get('item')) {
541 + $source = render_block($block);
542 + break;
543 + }
544 + }
545 + }
674 546
675 - libxml_use_internal_errors(true);
676 - $dom = new DOMDocument();
677 - if (function_exists('mb_convert_encoding')) {
678 - $html = $dom->loadHTML(mb_convert_encoding($source, 'HTML-ENTITIES', 'UTF-8'));
679 - } else {
680 - $html = $dom->loadHTML('<?xml encoding="UTF-8">' . $source);
681 - }
682 - libxml_clear_errors();
547 + if ($source) {
548 + $dom = new DOMDocument();
549 + $html = $this->helper->load('convert')->load_html($source, $dom, true);
550 + if ($html) {
551 + $xml = $this->helper->load('xml');
552 + $xpath = new DomXPath($dom);
683 553
684 - if ($html) {
685 - $xpath = new DomXPath($dom);
686 - $blocks = $xpath->query("//*[contains(@class, 'et_pb_contact_field')]");
687 - foreach ($blocks as $element) {
554 + // remove parent by class
555 + $remove_parent_by_class = [
556 + 'et_pb_contact_captcha_question',
557 + ];
558 + foreach ($remove_parent_by_class as $key => $class) {
559 + $xml_elements = $xpath->query("//*[contains(@class, '{$class}')]/parent::*");
560 + foreach ($xml_elements as $xml_element) {
561 + $xml_element->parentNode->removeChild($xml_element);
562 + }
563 + }
688 564
689 - $data_type = $element->attributes->getNamedItem("data-type")->nodeValue;
565 + $blocks = $xpath->query("//*[contains(@class, 'et_pb_contact_field')]");
566 + foreach ($blocks as $element) {
690 567
691 - if ($data_type == 'radio') {
568 + if ($xml->get_node_value($element, 'data-type') == 'radio') {
692 569
693 - $label = $xpath->query(".//label", $element)->item(0);
694 - $check_handler = $xpath->query(".//input[contains(@class, 'et_pb_checkbox_handle')]", $element)->item(0);
570 + $label = $xpath->query('.//label', $element)->item(0);
571 + $check_handler = $xpath->query(".//input[contains(@class, 'et_pb_checkbox_handle')]", $element)->item(0);
695 572
696 - $name = "";
697 - if ($check_handler) {
698 - $name = "%%" . $check_handler->attributes->getNamedItem("data-original_id")->nodeValue . "%%";
699 - }
573 + $name = '';
574 + if ($check_handler) {
575 + $name = '%%' . $xml->get_node_value($check_handler, 'data-original_id') . '%%';
576 + }
700 577
701 - $elements[] = $this->auto_field($element, array(
578 + if ($label) {
579 + $elements[] = $this->auto_field(
580 + $element,
581 + [
702 582 'type' => 'e2pdf-html',
703 583 'block' => true,
704 - 'properties' => array(
584 + 'class' => $xml->get_node_value($element, 'class'),
585 + 'properties' => [
705 586 'top' => '20',
706 587 'left' => '20',
707 588 'right' => '20',
708 589 'width' => '100%',
@@ -707,52 +588,73 @@
707 588 'right' => '20',
708 589 'width' => '100%',
709 590 'height' => 'auto',
710 591 'value' => $label->nodeValue,
711 - )
712 - ));
592 + ],
593 + ]
594 + );
595 + }
713 596
714 - $fields = $xpath->query("//*[contains(@class, 'et_pb_contact_field_radio')]", $element);
715 - foreach ($fields as $field) {
716 - $radio_label = $xpath->query(".//label", $field)->item(0);
717 - $radio = $xpath->query(".//input[@type='radio']", $field)->item(0);
597 + $fields = $xpath->query(".//*[contains(@class, 'et_pb_contact_field_radio')]", $element);
598 + foreach ($fields as $field) {
599 + $radio_label = $xpath->query('.//label', $field)->item(0);
600 + $radio = $xpath->query(".//input[@type='radio']", $field)->item(0);
718 601
719 - $elements[] = $this->auto_field($field, array(
602 + if (
603 + $radio->attributes->getNamedItem('data-original_id') &&
604 + $radio->attributes->getNamedItem('value')
605 + ) {
606 + $elements[] = $this->auto_field(
607 + $field,
608 + [
720 609 'type' => 'e2pdf-radio',
721 - 'properties' => array(
610 + 'class' => $xml->get_node_value($field, 'class'),
611 + 'properties' => [
722 612 'top' => '5',
723 613 'width' => 'auto',
724 614 'height' => 'auto',
725 - 'value' => "%%" . $radio->attributes->getNamedItem("data-original_id")->nodeValue . "%%",
726 - 'option' => $radio->attributes->getNamedItem("value")->nodeValue,
727 - 'group' => "%%" . $radio->attributes->getNamedItem("data-original_id")->nodeValue . "%%",
728 - )
729 - ));
730 - $elements[] = $this->auto_field($radio, array(
615 + 'value' => '%%' . $xml->get_node_value($radio, 'data-original_id') . '%%',
616 + 'option' => sanitize_text_field($xml->get_node_value($radio, 'value')),
617 + 'group' => '%%' . $xml->get_node_value($radio, 'data-original_id') . '%%',
618 + ],
619 + ]
620 + );
621 + }
622 +
623 + if ($radio_label) {
624 + $elements[] = $this->auto_field(
625 + $radio,
626 + [
731 627 'type' => 'e2pdf-html',
732 628 'float' => true,
733 - 'properties' => array(
629 + 'class' => $xml->get_node_value($radio, 'class'),
630 + 'properties' => [
734 631 'left' => '5',
735 632 'width' => '100%',
736 633 'height' => 'auto',
737 - 'value' => $radio_label->nodeValue
738 - )
739 - ));
740 - }
741 - } elseif ($data_type == 'checkbox') {
634 + 'value' => $radio_label->nodeValue,
635 + ],
636 + ]
637 + );
638 + }
639 + }
640 + } elseif ($xml->get_node_value($element, 'data-type') == 'checkbox') {
641 + $label = $xpath->query('.//label', $element)->item(0);
642 + $check_handler = $xpath->query(".//input[contains(@class, 'et_pb_checkbox_handle')]", $element)->item(0);
742 643
743 - $label = $xpath->query(".//label", $element)->item(0);
744 - $check_handler = $xpath->query(".//input[contains(@class, 'et_pb_checkbox_handle')]", $element)->item(0);
644 + $name = '';
645 + if ($check_handler) {
646 + $name = '%%' . $xml->get_node_value($check_handler, 'data-original_id') . '%%';
647 + }
745 648
746 - $name = "";
747 - if ($check_handler) {
748 - $name = "%%" . $check_handler->attributes->getNamedItem("data-original_id")->nodeValue . "%%";
749 - }
750 -
751 - $elements[] = $this->auto_field($element, array(
649 + if ($label) {
650 + $elements[] = $this->auto_field(
651 + $element,
652 + [
752 653 'type' => 'e2pdf-html',
753 654 'block' => true,
754 - 'properties' => array(
655 + 'class' => $xml->get_node_value($element, 'class'),
656 + 'properties' => [
755 657 'top' => '20',
756 658 'left' => '20',
757 659 'right' => '20',
758 660 'width' => '100%',
@@ -757,102 +659,156 @@
757 659 'right' => '20',
758 660 'width' => '100%',
759 661 'height' => 'auto',
760 662 'value' => $label->nodeValue,
761 - )
762 - ));
663 + ],
664 + ]
665 + );
666 + }
763 667
668 + $fields = $xpath->query(".//*[contains(@class, 'et_pb_contact_field_checkbox')]", $element);
669 + foreach ($fields as $field) {
670 + $checkbox_label = $xpath->query('.//label', $field)->item(0);
671 + $checkbox = $xpath->query(".//input[@type='checkbox']", $field)->item(0);
764 672
765 - $fields = $xpath->query("//*[contains(@class, 'et_pb_contact_field_checkbox')]", $element);
673 + $elements[] = $this->auto_field(
674 + $field,
675 + [
676 + 'type' => 'e2pdf-checkbox',
677 + 'class' => $xml->get_node_value($field, 'class'),
678 + 'properties' => [
679 + 'top' => '5',
680 + 'width' => 'auto',
681 + 'height' => 'auto',
682 + 'value' => $name,
683 + 'option' => sanitize_text_field($xml->get_node_value($checkbox, 'value')),
684 + ],
685 + ]
686 + );
766 687
767 - foreach ($fields as $field) {
768 - $checkbox_label = $xpath->query(".//label", $field)->item(0);
769 - $checkbox = $xpath->query(".//input[@type='checkbox']", $field)->item(0);
770 -
771 -
772 - $elements[] = $this->auto_field($field, array(
773 - 'type' => 'e2pdf-checkbox',
774 - 'properties' => array(
775 - 'top' => '5',
776 - 'width' => 'auto',
777 - 'height' => 'auto',
778 - 'value' => $name,
779 - 'option' => $checkbox->attributes->getNamedItem("value")->nodeValue
780 - )
781 - ));
782 - $elements[] = $this->auto_field($checkbox, array(
688 + if ($checkbox_label) {
689 + $elements[] = $this->auto_field(
690 + $checkbox,
691 + [
783 692 'type' => 'e2pdf-html',
784 693 'float' => true,
785 - 'properties' => array(
694 + 'class' => $xml->get_node_value($checkbox, 'class'),
695 + 'properties' => [
786 696 'left' => '5',
787 697 'width' => '100%',
788 698 'height' => 'auto',
789 - 'value' => $checkbox_label->nodeValue
790 - )
791 - ));
792 - }
793 - } else {
699 + 'value' => $checkbox_label->nodeValue,
700 + ],
701 + ]
702 + );
703 + }
704 + }
705 + } else {
706 + $label = $xpath->query('.//label', $element)->item(0);
707 + $input_text = $xpath->query(".//input[@type='text']", $element)->item(0);
708 + $select = $xpath->query('.//select', $element)->item(0);
709 + $textarea = $xpath->query('.//textarea', $element)->item(0);
794 710
795 - $label = $xpath->query(".//label", $element)->item(0);
796 - $input_text = $xpath->query(".//input[@type='text']", $element)->item(0);
797 - $select = $xpath->query(".//select", $element)->item(0);
798 - $textarea = $xpath->query(".//textarea", $element)->item(0);
711 + if ($label && ($input_text || $select || $textarea)) {
712 + $elements[] = $this->auto_field(
713 + $element,
714 + [
715 + 'type' => 'e2pdf-html',
716 + 'block' => true,
717 + 'class' => $xml->get_node_value($element, 'class'),
718 + 'properties' => [
719 + 'top' => '20',
720 + 'left' => '20',
721 + 'right' => '20',
722 + 'width' => '100%',
723 + 'height' => 'auto',
724 + 'value' => $label->nodeValue,
725 + ],
726 + ]
727 + );
728 + }
799 729
800 - if ($label && ($input_text || $select || $textarea)) {
801 - $elements[] = $this->auto_field($element, array(
802 - 'type' => 'e2pdf-html',
803 - 'block' => true,
804 - 'properties' => array(
805 - 'top' => '20',
806 - 'left' => '20',
807 - 'right' => '20',
730 + if ($input_text) {
731 + $data_type = $input_text->parentNode->getAttribute('data-type');
732 + if ($data_type === 'signature-pad') {
733 + $elements[] = $this->auto_field(
734 + $input_text,
735 + [
736 + 'type' => 'e2pdf-signature',
737 + 'properties' => [
738 + 'top' => '5',
808 739 'width' => '100%',
809 - 'height' => 'auto',
810 - 'value' => $label->nodeValue,
811 - )
812 - ));
813 - }
814 -
815 - if ($input_text) {
816 - $elements[] = $this->auto_field($input_text, array(
817 - 'type' => 'e2pdf-input',
818 - 'properties' => array(
740 + 'height' => '150',
741 + 'dimension' => '1',
742 + 'block_dimension' => '1',
743 + 'value' => '%%' . $xml->get_node_value($input_text, 'data-original_id') . '%%',
744 + ],
745 + ]
746 + );
747 + } elseif ($data_type === 'file') {
748 + $elements[] = $this->auto_field(
749 + $input_text,
750 + [
751 + 'type' => 'e2pdf-textarea',
752 + 'class' => $xml->get_node_value($input_text, 'class'),
753 + 'properties' => [
819 754 'top' => '5',
820 755 'width' => '100%',
821 - 'height' => 'auto',
822 - 'value' => "%%{$input_text->attributes->getNamedItem("data-original_id")->nodeValue}%%",
823 - )
824 - ));
825 - } elseif ($select) {
826 - $options_tmp = array();
827 - $options = $xpath->query(".//option", $select);
828 - foreach ($options as $option) {
829 - $options_tmp[] = $option->attributes->getNamedItem("value")->nodeValue;
830 - }
831 -
832 - $elements[] = $this->auto_field($select, array(
833 - 'type' => 'e2pdf-select',
834 - 'properties' => array(
756 + 'height' => '150',
757 + 'value' => '%%' . $xml->get_node_value($input_text, 'data-original_id') . '%%',
758 + ],
759 + ]
760 + );
761 + } else {
762 + $elements[] = $this->auto_field(
763 + $input_text,
764 + [
765 + 'type' => 'e2pdf-input',
766 + 'class' => $xml->get_node_value($input_text, 'class'),
767 + 'properties' => [
835 768 'top' => '5',
836 769 'width' => '100%',
837 770 'height' => 'auto',
838 - 'options' => implode("\n", $options_tmp),
839 - 'value' => "%%{$select->attributes->getNamedItem("data-original_id")->nodeValue}%%",
840 - )
841 - ));
842 - } elseif ($textarea) {
843 - $elements[] = $this->auto_field($textarea, array(
844 - 'type' => 'e2pdf-textarea',
845 - 'properties' => array(
846 - 'top' => '5',
847 - 'width' => '100%',
848 - 'height' => '150',
849 - 'value' => "%%{$textarea->attributes->getNamedItem("data-original_id")->nodeValue}%%",
850 - )
851 - ));
852 - }
853 - }
771 + 'value' => '%%' . $xml->get_node_value($input_text, 'data-original_id') . '%%',
772 + ],
773 + ]
774 + );
854 775 }
776 + } elseif ($select) {
777 + $options_tmp = [];
778 + $options = $xpath->query('.//option', $select);
779 + foreach ($options as $option) {
780 + $options_tmp[] = sanitize_text_field($xml->get_node_value($option, 'value'));
781 + }
782 +
783 + $elements[] = $this->auto_field(
784 + $select,
785 + [
786 + 'type' => 'e2pdf-select',
787 + 'class' => $xml->get_node_value($select, 'class'),
788 + 'properties' => [
789 + 'top' => '5',
790 + 'width' => '100%',
791 + 'height' => 'auto',
792 + 'options' => implode("\n", $options_tmp),
793 + 'value' => '%%' . $xml->get_node_value($select, 'data-original_id') . '%%',
794 + ],
795 + ]
796 + );
797 + } elseif ($textarea) {
798 + $elements[] = $this->auto_field(
799 + $textarea,
800 + [
801 + 'type' => 'e2pdf-textarea',
802 + 'class' => $xml->get_node_value($textarea, 'class'),
803 + 'properties' => [
804 + 'top' => '5',
805 + 'width' => '100%',
806 + 'height' => '150',
807 + 'value' => '%%' . $xml->get_node_value($textarea, 'data-original_id') . '%%',
808 + ],
809 + ]
810 + );
855 811 }
856 812 }
857 813 }
858 814 }
@@ -858,83 +814,53 @@
858 814 }
859 815 }
860 816 }
861 817 }
862 -
863 - $response['page'] = array(
864 - 'bottom' => '20',
865 - 'top' => '20',
866 - 'right' => '20',
867 - 'left' => '20'
868 - );
869 - $response['elements'] = $elements;
870 -
871 - return $response;
818 + return [
819 + 'page' => [
820 + 'bottom' => '20',
821 + 'top' => '20',
822 + 'right' => '20',
823 + 'left' => '20',
824 + ],
825 + 'elements' => $elements,
826 + ];
872 827 }
873 828
874 - /**
875 - * Convert Field name to Value
876 - * @since 0.01.34
877 - *
878 - * @param string $name - Field name
879 - *
880 - * @return bool|string - Converted value or false
881 - */
829 + // auto map
882 830 public function auto_map($name = false) {
883 831 $item = $this->get('item');
884 832 if ($item) {
885 - $post = $this->get_post($item);
833 + $post = $this->get_form($item);
886 834 if ($post && isset($post->post_content)) {
887 835 $content = $post->post_content;
888 -
889 - if (false !== strpos($content, '[')) {
890 - $shortcode_tags = array(
836 + if ($this->divi4_form($content)) {
837 + $shortcode_tags = [
891 838 'et_pb_contact_form',
892 - );
893 -
839 + ];
894 840 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
895 841 $tagnames = array_intersect($shortcode_tags, $matches[1]);
896 -
897 842 if (!empty($tagnames)) {
898 -
899 - $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
900 -
901 - preg_match_all("/$pattern/", $content, $shortcodes);
902 -
843 + preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $content, $shortcodes);
903 844 foreach ($shortcodes[0] as $key => $shortcode_value) {
904 -
905 - $shortcode = array();
906 - $shortcode[1] = $shortcodes[1][$key];
907 - $shortcode[2] = $shortcodes[2][$key];
908 - $shortcode[3] = $shortcodes[3][$key];
909 - $shortcode[4] = $shortcodes[4][$key];
910 - $shortcode[5] = $shortcodes[5][$key];
911 - $shortcode[6] = $shortcodes[6][$key];
912 -
845 + $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
913 846 $atts = shortcode_parse_atts($shortcode[3]);
914 847 if (isset($atts['admin_label']) && $atts['admin_label'] == $this->get('item')) {
915 -
916 848 $field_content = $shortcode_value;
917 - $field_shortcode_tags = array(
849 + $field_shortcode_tags = [
918 850 'et_pb_contact_field',
919 - );
920 -
851 + ];
921 852 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $field_content, $field_matches);
922 853 $field_tagnames = array_intersect($field_shortcode_tags, $field_matches[1]);
923 -
924 854 if (!empty($field_tagnames)) {
925 -
926 - $field_pattern = $this->helper->load('shortcode')->get_shortcode_regex($field_tagnames);
927 -
928 - preg_match_all("/$field_pattern/", $field_content, $field_shortcodes);
929 -
855 + preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($field_tagnames) . '/', $field_content, $field_shortcodes);
930 856 foreach ($field_shortcodes[0] as $field_key => $field_shortcode_value) {
931 - $field_shortcode = array();
857 + $field_shortcode = [];
932 858 $field_shortcode[3] = $field_shortcodes[3][$field_key];
933 - $field_atts = shortcode_parse_atts($field_shortcode[3]);
934 - if (isset($field_atts['field_title']) && isset($field_atts['field_id'])) {
935 - if ($field_atts['field_title'] == $name || $field_atts['field_id'] == $name) {
936 - return "%%" . strtolower($field_atts['field_id']) . "%%";
859 + $field = shortcode_parse_atts($field_shortcode[3]);
860 + if (isset($field['field_title']) && isset($field['field_id'])) {
861 + if ($field['field_title'] == $name || $field['field_id'] == $name) {
862 + return '%%' . strtolower($field['field_id']) . '%%';
937 863 }
938 864 }
939 865 }
940 866 }
@@ -940,8 +866,26 @@
940 866 }
941 867 }
942 868 }
943 869 }
870 + } elseif ($this->divi5_form($content)) {
871 + $blocks = $this->get_forms_blocks(parse_blocks($content));
872 + foreach ($blocks as $key => $block) {
873 + $label = !empty($block['attrs']['module']['meta']['adminLabel']['desktop']['value']) ? $block['attrs']['module']['meta']['adminLabel']['desktop']['value'] : '';
874 + if ($label == $this->get('item')) {
875 + $fields = $this->get_fields_blocks($block);
876 + if (!empty($fields)) {
877 + foreach ($fields as $field) {
878 + if (isset($field['field_title']) && isset($field['field_id'])) {
879 + if ($field['field_title'] == $name || $field['field_id'] == $name) {
880 + return '%%' . strtolower($field['field_id']) . '%%';
881 + }
882 + }
883 + }
884 + }
885 + }
886 + break;
887 + }
944 888 }
945 889 }
946 890 }
947 891
@@ -947,18 +891,10 @@
947 891
948 892 return false;
949 893 }
950 894
951 - /**
952 - * Generate field for Auto PDF
953 - *
954 - * @param object $field - Formidable field object
955 - * @param string $type - Field type
956 - * @param array $options - Field additional options
957 - *
958 - * @return array - Prepared auto field
959 - */
960 - public function auto_field($field = false, $element = array()) {
895 + // auto field
896 + public function auto_field($field = false, $element = []) {
961 897
962 898 if (!$field) {
963 899 return false;
964 900 }
@@ -970,16 +906,17 @@
970 906 if (!isset($element['float'])) {
971 907 $element['float'] = false;
972 908 }
973 909
974 - $classes = array();
975 - if (isset($field->attributes->getNamedItem("class")->nodeValue)) {
976 - $classes = explode(" ", $field->attributes->getNamedItem("class")->nodeValue);
910 + $classes = [];
911 + if (isset($element['class']) && $element['class']) {
912 + $classes = explode(' ', $element['class']);
913 + unset($element['class']);
977 914 }
978 915
979 - $float_classes = array(
916 + $float_classes = [
980 917 'et_pb_contact_field_half',
981 - );
918 + ];
982 919 $array_intersect = array_intersect($classes, $float_classes);
983 920
984 921 if (!empty($array_intersect) && $element['block']) {
985 922 $element['float'] = true;
@@ -1002,52 +939,17 @@
1002 939
1003 940 return $element;
1004 941 }
1005 942
1006 - /**
1007 - * Verify if item and dataset exists
1008 - *
1009 - * @return bool - item and dataset exists
1010 - */
943 + // verify
1011 944 public function verify() {
1012 - global $wpdb;
1013 - $item = $this->get('item');
1014 - $dataset = $this->get('dataset');
1015 -
1016 - if ($item && $dataset) {
1017 - $condition = array(
1018 - 'ID' => array(
1019 - 'condition' => '=',
1020 - 'value' => $dataset,
1021 - 'type' => '%d'
1022 - ),
1023 - 'extension' => array(
1024 - 'condition' => '=',
1025 - 'value' => 'divi',
1026 - 'type' => '%s'
1027 - ),
1028 - 'item' => array(
1029 - 'condition' => '=',
1030 - 'value' => $item,
1031 - 'type' => '%s'
1032 - ),
1033 - );
1034 -
1035 - $where = $this->helper->load('db')->prepare_where($condition);
1036 - $entry = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . 'e2pdf_datasets' . $where['sql'] . "", $where['filter']));
1037 -
1038 - if ($entry) {
1039 - return true;
1040 - }
945 + if ($this->get('cached_entry')) {
946 + return true;
1041 947 }
1042 948 return false;
1043 949 }
1044 950
1045 - /**
1046 - * Init Visual Mapper data
1047 - *
1048 - * @return bool|string - HTML data source for Visual Mapper
1049 - */
951 + // visual mapper
1050 952 public function visual_mapper() {
1051 953
1052 954 $source = '';
1053 955 $html = '';
@@ -1052,129 +954,139 @@
1052 954 $source = '';
1053 955 $html = '';
1054 956
1055 957 if ($this->get('item')) {
1056 - $post = $this->get_post($this->get('item'));
958 + $post = $this->get_form($this->get('item'));
1057 959 if ($post && isset($post->post_content)) {
1058 -
1059 960 $content = $post->post_content;
1060 961
1061 - if (false !== strpos($content, '[')) {
1062 - $shortcode_tags = array(
962 + if ($this->divi4_form($content)) {
963 + $shortcode_tags = [
1063 964 'et_pb_contact_form',
1064 - );
1065 -
965 + ];
1066 966 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
1067 967 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1068 -
1069 968 if (!empty($tagnames)) {
1070 -
1071 - $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
1072 -
1073 - preg_match_all("/$pattern/", $content, $shortcodes);
1074 -
969 + preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $content, $shortcodes);
1075 970 foreach ($shortcodes[0] as $key => $shortcode_value) {
1076 -
1077 - $shortcode = array();
1078 - $shortcode[1] = $shortcodes[1][$key];
1079 - $shortcode[2] = $shortcodes[2][$key];
1080 - $shortcode[3] = $shortcodes[3][$key];
1081 - $shortcode[4] = $shortcodes[4][$key];
1082 - $shortcode[5] = $shortcodes[5][$key];
1083 - $shortcode[6] = $shortcodes[6][$key];
1084 -
971 + $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1085 972 $atts = shortcode_parse_atts($shortcode[3]);
1086 973 if (isset($atts['admin_label']) && $atts['admin_label'] == $this->get('item')) {
1087 -
1088 - require_once(ET_BUILDER_DIR . 'class-et-builder-element.php');
1089 - require_once(ET_BUILDER_DIR . 'functions.php');
1090 - require_once(ET_BUILDER_DIR . 'ab-testing.php');
1091 - require_once(ET_BUILDER_DIR . 'class-et-global-settings.php');
1092 - require_once(ET_BUILDER_DIR . 'module/ContactForm.php');
1093 - require_once(ET_BUILDER_DIR . 'module/ContactFormItem.php');
1094 -
1095 - new ET_Builder_Module_Contact_Form();
1096 - new ET_Builder_Module_Contact_Form_Item();
1097 -
974 + $this->divi4_load();
1098 975 $source = do_shortcode($shortcode_value);
976 + }
977 + }
978 + }
979 + } elseif ($this->divi5_form($content)) {
980 + $blocks = $this->get_forms_blocks(parse_blocks($content));
981 + foreach ($blocks as $key => $block) {
982 + $label = !empty($block['attrs']['module']['meta']['adminLabel']['desktop']['value']) ? $block['attrs']['module']['meta']['adminLabel']['desktop']['value'] : '';
983 + if ($label == $this->get('item')) {
984 + $source = render_block($block);
985 + break;
986 + }
987 + }
988 + }
1099 989
1100 - if ($source) {
1101 - libxml_use_internal_errors(true);
1102 - $dom = new DOMDocument();
1103 - if (function_exists('mb_convert_encoding')) {
1104 - $html = $dom->loadHTML(mb_convert_encoding($source, 'HTML-ENTITIES', 'UTF-8'));
1105 - } else {
1106 - $html = $dom->loadHTML('<?xml encoding="UTF-8">' . $source);
1107 - }
1108 - libxml_clear_errors();
1109 - }
990 + if ($source) {
991 + $dom = new DOMDocument();
992 + $html = $this->helper->load('convert')->load_html($source, $dom, true);
993 + }
994 + if (!$source) {
995 + return '<div class="e2pdf-vm-error">' . __("The form source is empty or doesn't exist", 'e2pdf') . '</div>';
996 + } elseif (!$html) {
997 + return '<div class="e2pdf-vm-error">' . __('The form could not be parsed due the incorrect HTML', 'e2pdf') . '</div>';
998 + } else {
999 + $xml = $this->helper->load('xml');
1000 + $xpath = new DomXPath($dom);
1110 1001
1111 - if (!$source) {
1112 - return __('Form source is empty', 'e2pdf');
1113 - } elseif (!$html) {
1114 - return __('Form could not be parsed due incorrect HTML', 'e2pdf');
1115 - } else {
1002 + // remove by name
1003 + $remove_by_name = [
1004 + 'et_pb_contactform_submit_0',
1005 + '_wpnonce-et-pb-contact-form-submitted-0',
1006 + '_wp_http_referer',
1007 + ];
1008 + foreach ($remove_by_name as $key => $name) {
1009 + $xml_elements = $xpath->query('//*[@name="' . $name . '"]');
1010 + foreach ($xml_elements as $xml_element) {
1011 + $xml_element->parentNode->removeChild($xml_element);
1012 + }
1013 + }
1116 1014
1117 - $xpath = new DomXPath($dom);
1118 - // Replace names
1119 - $fields = $xpath->query("//*[contains(@name, 'et_pb_contact_')]");
1120 - foreach ($fields as $element) {
1121 - if ($element->attributes->getNamedItem("name")) {
1122 - $element->attributes->getNamedItem("name")->nodeValue = "%%" . $element->attributes->getNamedItem("data-original_id")->nodeValue . "%%";
1123 - }
1124 - }
1015 + // replace names
1016 + $fields = $xpath->query("//*[contains(@name, 'et_pb_contact_')]");
1017 + foreach ($fields as $xml_element) {
1018 + $xml->set_node_value($xml_element, 'name', '%%' . $xml->get_node_value($xml_element, 'data-original_id') . '%%');
1019 + }
1125 1020
1126 - $checkboxes = $xpath->query("//*[contains(@class, 'et_pb_contact_field') and @data-type='checkbox']");
1127 - foreach ($checkboxes as $element) {
1021 + // checkboxes
1022 + $checkboxes = $xpath->query("//*[contains(@class, 'et_pb_contact_field') and @data-type='checkbox']");
1023 + foreach ($checkboxes as $xml_element) {
1024 + $check_handler = $xpath->query(".//input[contains(@class, 'et_pb_checkbox_handle')]", $xml_element)->item(0);
1025 + $name = '';
1026 + if ($check_handler) {
1027 + $name = '%%' . $xml->get_node_value($check_handler, 'data-original_id') . '%%';
1028 + }
1029 + $checks = $xpath->query(".//input[@type='checkbox']", $xml_element);
1030 + foreach ($checks as $check) {
1031 + $xml->set_node_value($check, 'name', $name);
1032 + $xml->set_node_value($check, 'value', sanitize_text_field($xml->get_node_value($check, 'value')));
1033 + }
1034 + }
1128 1035
1129 - $check_handler = $xpath->query(".//input[contains(@class, 'et_pb_checkbox_handle')]", $element)->item(0);
1036 + // uploads
1037 + $uploads = $xpath->query("//*[contains(@class, 'et_pb_contact_hidden_files')]");
1038 + foreach ($uploads as $xml_element) {
1039 + $xml->set_node_value($xml_element, 'type', 'file');
1040 + }
1130 1041
1131 - $name = "";
1132 - if ($check_handler) {
1133 - $name = "%%" . $check_handler->attributes->getNamedItem("data-original_id")->nodeValue . "%%";
1134 - }
1042 + $radios = $xpath->query("//input[@type='radio']");
1043 + foreach ($radios as $xml_element) {
1044 + $xml->set_node_value($xml_element, 'value', sanitize_text_field($xml->get_node_value($xml_element, 'value')));
1045 + }
1135 1046
1136 - $checks = $xpath->query(".//input[@type='checkbox']", $element);
1137 - foreach ($checks as $check) {
1138 - $attr = $dom->createAttribute('name');
1139 - $attr->value = $name;
1140 - $check->appendChild($attr);
1141 - }
1142 - }
1047 + $selects = $xpath->query('//select/option');
1048 + foreach ($selects as $xml_element) {
1049 + $xml->set_node_value($xml_element, 'value', sanitize_text_field($xml->get_node_value($xml_element, 'value')));
1050 + }
1143 1051
1144 - $remove_by_class = array(
1145 - 'et_pb_contact_submit',
1146 - 'et_pb_contactform_validate_field'
1147 - );
1148 - foreach ($remove_by_class as $key => $class) {
1149 - $elements = $xpath->query("//*[contains(@class, '{$class}')]");
1150 - foreach ($elements as $element) {
1151 - $element->parentNode->removeChild($element);
1152 - }
1153 - }
1052 + // remove by class
1053 + $remove_by_class = [
1054 + 'et_pb_contact_submit',
1055 + 'et_pb_contactform_validate_field',
1056 + 'et_pb_checkbox_handle',
1057 + 'et_pb_form_bottom_stepper_container',
1058 + 'stepper__progress',
1059 + ];
1060 + foreach ($remove_by_class as $key => $class) {
1061 + $xml_elements = $xpath->query("//*[contains(@class, '{$class}')]");
1062 + foreach ($xml_elements as $xml_element) {
1063 + $xml_element->parentNode->removeChild($xml_element);
1064 + }
1065 + }
1154 1066
1155 - $remove_parent_by_class = array(
1156 - 'et_pb_contact_captcha_question'
1157 - );
1158 - foreach ($remove_parent_by_class as $key => $class) {
1159 - $elements = $xpath->query("//*[contains(@class, '{$class}')]/parent::*");
1160 - foreach ($elements as $element) {
1161 - $element->parentNode->removeChild($element);
1162 - }
1163 - }
1067 + $xml_elements = $xpath->query('//*[@data-step]');
1068 + foreach ($xml_elements as $xml_element) {
1069 + $xml_element->removeAttribute('style');
1070 + }
1164 1071
1165 - if ($xpath->query("//form")->item(0)) {
1166 - $autocomplete = $dom->createAttribute('autocomplete');
1167 - $autocomplete->value = 'off';
1168 - $xpath->query("//form")->item(0)->appendChild($autocomplete);
1169 - }
1170 - }
1171 -
1172 - return $dom->saveHTML();
1173 - }
1072 + // remove parent by class
1073 + $remove_parent_by_class = [
1074 + 'et_pb_contact_captcha_question',
1075 + ];
1076 + foreach ($remove_parent_by_class as $key => $class) {
1077 + $xml_elements = $xpath->query("//*[contains(@class, '{$class}')]/parent::*");
1078 + foreach ($xml_elements as $xml_element) {
1079 + $xml_element->parentNode->removeChild($xml_element);
1174 1080 }
1175 1081 }
1176 1082 }
1083 +
1084 + if (defined('LIBXML_HTML_NOIMPLIED') && defined('LIBXML_HTML_NODEFDTD')) {
1085 + return str_replace(['<html>', '</html>'], '', $dom->saveHTML());
1086 + } else {
1087 + return $dom->saveHTML();
1088 + }
1177 1089 }
1178 1090 }
1179 1091
1180 1092 return false;
@@ -1179,56 +1091,64 @@
1179 1091
1180 1092 return false;
1181 1093 }
1182 1094
1095 + // filter mail
1183 1096 public function filter_wp_mail($args) {
1097 +
1098 + if (!$this->get('process')) {
1099 + return $args;
1100 + }
1101 +
1102 + if ((defined('PWH_DCFH_PLUGIN_VERSION') && version_compare(PWH_DCFH_PLUGIN_VERSION, '1.5.1', '>=')) || defined('KS_PAC_DCFH_PLUGIN_FILE')) {
1103 + $args['message'] = preg_replace('/(\{)((e2pdf-download|e2pdf-view|e2pdf-save|e2pdf-attachment|e2pdf-adobesign|e2pdf-zapier)[^\}]*?)(\})/', '[$2]', $args['message']);
1104 + }
1105 +
1184 1106 if (isset($args['message'])) {
1185 1107 if (false !== strpos($args['message'], '[')) {
1186 - $shortcode_tags = array(
1108 + $shortcode_tags = [
1187 1109 'e2pdf-download',
1188 1110 'e2pdf-save',
1189 1111 'e2pdf-attachment',
1190 - 'e2pdf-adobesign'
1191 - );
1192 -
1112 + 'e2pdf-adobesign',
1113 + 'e2pdf-zapier',
1114 + ];
1193 1115 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $args['message'], $matches);
1194 1116 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1195 -
1196 1117 if (!empty($tagnames)) {
1197 -
1198 - $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
1199 -
1200 - preg_match_all("/$pattern/", $args['message'], $shortcodes);
1201 -
1118 + preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $args['message'], $shortcodes);
1202 1119 foreach ($shortcodes[0] as $key => $shortcode_value) {
1203 -
1204 - $shortcode = array();
1205 - $shortcode[1] = $shortcodes[1][$key];
1206 - $shortcode[2] = $shortcodes[2][$key];
1207 - $shortcode[3] = $shortcodes[3][$key];
1208 - $shortcode[4] = $shortcodes[4][$key];
1209 - $shortcode[5] = $shortcodes[5][$key];
1210 - $shortcode[6] = $shortcodes[6][$key];
1211 -
1120 + $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1212 1121 $atts = shortcode_parse_atts($shortcode[3]);
1213 -
1214 - $file = false;
1215 -
1216 1122 if (!isset($atts['apply'])) {
1217 - $shortcode[3] .= " apply=\"true\"";
1123 + $shortcode[3] .= ' apply="true"';
1218 1124 }
1219 -
1220 1125 if (!isset($atts['filter'])) {
1221 - $shortcode[3] .= " filter=\"true\"";
1126 + $shortcode[3] .= ' filter="true"';
1222 1127 }
1223 -
1224 - if (($shortcode[2] === 'e2pdf-save' && isset($atts['attachment']) && $atts['attachment'] == 'true') || $shortcode[2] === 'e2pdf-attachment') {
1128 + $file = false;
1129 + if ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) {
1225 1130 $file = do_shortcode_tag($shortcode);
1226 1131 if ($file) {
1227 - if ($shortcode[2] != 'e2pdf-save' && !isset($atts['pdf'])) {
1132 + $tmp = false;
1133 + if (substr($file, 0, 4) === 'tmp:') {
1134 + $file = substr($file, 4);
1135 + $tmp = true;
1136 + }
1137 + if ($shortcode[2] === 'e2pdf-save' || isset($atts['pdf'])) {
1138 + if ($tmp) {
1139 + $this->helper->add('divi_attachments', $file);
1140 + }
1141 + } else {
1228 1142 $this->helper->add('divi_attachments', $file);
1229 1143 }
1230 1144 $args['attachments'][] = $file;
1145 + if (!has_action('wp_mail_succeeded', [$this, 'filter_wp_mail_attachments'])) {
1146 + add_action('wp_mail_succeeded', [$this, 'filter_wp_mail_attachments']);
1147 + }
1148 + if (!has_action('wp_mail_failed', [$this, 'filter_wp_mail_attachments'])) {
1149 + add_action('wp_mail_failed', [$this, 'filter_wp_mail_attachments']);
1150 + }
1231 1151 }
1232 1152 $args['message'] = str_replace($shortcode_value, '', $args['message']);
1233 1153 } else {
1234 1154 if (is_array($args['headers']) && !in_array('Content-Type: text/html; charset=UTF-8', $args['headers'])) {
@@ -1234,379 +1154,1007 @@
1234 1154 if (is_array($args['headers']) && !in_array('Content-Type: text/html; charset=UTF-8', $args['headers'])) {
1235 1155 $args['headers'][] = 'Content-Type: text/html; charset=UTF-8';
1236 1156 }
1237 1157 $args['message'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $args['message']);
1238 - $args['message'] = str_replace("\r\n", "<br/>", $args['message']);
1158 + $args['message'] = str_replace("\r\n", '<br/>', $args['message']);
1239 1159 }
1240 1160 }
1241 1161 }
1242 1162 }
1243 1163 }
1244 -
1245 - $wp_mail = array(
1164 + $wp_mail = [
1246 1165 'to' => $args['to'],
1247 1166 'subject' => $args['subject'],
1248 1167 'message' => $args['message'],
1249 1168 'headers' => $args['headers'],
1250 1169 'attachments' => $args['attachments'],
1251 - );
1170 + ];
1171 + return $wp_mail;
1172 + }
1252 1173
1253 - return $wp_mail;
1174 + public function filter_wp_mail_attachments() {
1175 + $files = $this->helper->get('divi_attachments');
1176 + if (is_array($files) && !empty($files)) {
1177 + foreach ($files as $key => $file) {
1178 + $this->helper->delete_dir(dirname($file) . '/');
1179 + }
1180 + $this->helper->deset('divi_attachments');
1181 + }
1254 1182 }
1255 1183
1256 - /**
1257 - * Load actions for this extension
1258 - */
1259 - public function load_actions() {
1260 -
1184 + // load actions
1185 + public function load_actions() { // phpcs:ignore Squiz.WhiteSpace.SuperfluousWhitespace.EndLine
1186 + add_action('et_pb_contact_form_submit', [$this, 'action_et_pb_contact_form_submit'], 11);
1261 1187 }
1262 1188
1263 - /**
1264 - * Load filters for this extension
1265 - */
1189 + public function action_et_pb_contact_form_submit() {
1190 + if ($this->divi5()) {
1191 + add_filter('wp_mail', [$this, 'filter_wp_mail'], 1000);
1192 + }
1193 + }
1194 +
1195 + // load filters
1266 1196 public function load_filters() {
1267 - add_filter('et_pb_module_shortcode_attributes', array($this, 'filter_et_pb_module_shortcode_attributes'), 30, 5);
1268 - add_filter('et_module_shortcode_output', array($this, 'filter_et_module_shortcode_output'), 30, 3);
1197 + // Divi Contact Form Helper Confirmation Email compatibility fix
1198 + if (defined('PWH_DCFH_PLUGIN_FILE') || defined('KS_PAC_DCFH_PLUGIN_FILE')) {
1199 + add_filter('et_pb_module_shortcode_attributes', [$this, 'filter_et_pb_module_shortcode_attributes'], 9, 5);
1200 + } else {
1201 + add_filter('et_pb_module_shortcode_attributes', [$this, 'filter_et_pb_module_shortcode_attributes'], 30, 5);
1202 + }
1203 + add_filter('et_module_shortcode_output', [$this, 'filter_et_module_shortcode_output'], 30, 3);
1204 +
1205 + // Divi 5
1206 + add_filter('divi_module_library_register_module_attrs', [$this, 'filter_divi_module_library_register_module_attrs'], 11, 2);
1269 1207 }
1270 1208
1271 - public function filter_et_pb_module_shortcode_attributes($props, $attrs, $render_slug, $address, $content) {
1272 - global $wpdb;
1209 + public function filter_divi_module_library_register_module_attrs($module_attrs, $filter_args) {
1273 1210
1274 - if ($render_slug && $render_slug == 'et_pb_contact_form' && !empty($_POST) && $et_contact_proccess = array_search('et_contact_proccess', $_POST)) {
1211 + if ('divi/contact-form' !== $filter_args['name']) {
1212 + return $module_attrs;
1213 + }
1275 1214
1276 - $et_pb_contact_form_num = str_replace("et_pb_contactform_submit_", "", $et_contact_proccess);
1215 + if (!isset($_SERVER['REQUEST_METHOD']) || 'POST' !== $_SERVER['REQUEST_METHOD']) {
1216 + return $module_attrs;
1217 + }
1277 1218
1278 - $et_contact_error = false;
1279 - $current_form_fields = isset($_POST['et_pb_contact_email_fields_' . $et_pb_contact_form_num]) ? $_POST['et_pb_contact_email_fields_' . $et_pb_contact_form_num] : '';
1280 - $hidden_form_fields = isset($_POST['et_pb_contact_email_hidden_fields_' . $et_pb_contact_form_num]) ? $_POST['et_pb_contact_email_hidden_fields_' . $et_pb_contact_form_num] : false;
1281 - $contact_email = '';
1282 - $processed_fields_values = array();
1219 + $module_id = !empty($filter_args['id']) ? $filter_args['id'] : '';
1220 + $store_instance = !empty($filter_args['storeInstance']) ? $filter_args['storeInstance'] : 0;
1283 1221
1284 - $nonce_result = isset($_POST['_wpnonce-et-pb-contact-form-submitted-' . $et_pb_contact_form_num]) && wp_verify_nonce($_POST['_wpnonce-et-pb-contact-form-submitted-' . $et_pb_contact_form_num], 'et-pb-contact-form-submit') ? true : false;
1222 + if (
1223 + !class_exists('\ET\Builder\FrontEnd\BlockParser\BlockParserStore') ||
1224 + !method_exists('\ET\Builder\FrontEnd\BlockParser\BlockParserStore', 'get')
1225 + ) {
1226 + return $module_attrs;
1227 + }
1285 1228
1286 - if ($nonce_result && isset($_POST['et_pb_contactform_submit_' . $et_pb_contact_form_num]) && empty($_POST['et_pb_contactform_validate_' . $et_pb_contact_form_num])) {
1287 - if ('' !== $current_form_fields) {
1288 - $fields_data_json = str_replace('\\', '', $current_form_fields);
1289 - $fields_data_array = json_decode($fields_data_json, true);
1229 + $module = \ET\Builder\FrontEnd\BlockParser\BlockParserStore::get($module_id, $store_instance);
1290 1230
1291 - // check whether captcha field is not empty
1292 - if ('on' === $captcha && (!isset($_POST['et_pb_contact_captcha_' . $et_pb_contact_form_num]) || empty($_POST['et_pb_contact_captcha_' . $et_pb_contact_form_num]) )) {
1293 - $et_contact_error = true;
1294 - }
1231 + if (!$module) {
1232 + return $module_attrs;
1233 + }
1295 1234
1296 - // check all fields on current form and generate error message if needed
1297 - if (!empty($fields_data_array)) {
1298 - foreach ($fields_data_array as $index => $value) {
1299 - // check all the required fields, generate error message if required field is empty
1300 - $field_value = isset($_POST[$value['field_id']]) ? trim($_POST[$value['field_id']]) : '';
1235 + if (
1236 + !class_exists('\ET\Builder\Packages\ModuleLibrary\ContactForm\ContactFormUtils') ||
1237 + !method_exists('\ET\Builder\Packages\ModuleLibrary\ContactForm\ContactFormUtils', 'get_unique_id') ||
1238 + !class_exists('\ET\Builder\Packages\ModuleLibrary\ContactForm\ContactFormHandler') ||
1239 + !class_exists('\ET\Builder\Packages\ModuleLibrary\ContactField\ContactFieldModule') ||
1240 + !method_exists('\ET\Builder\Packages\ModuleLibrary\ContactField\ContactFieldModule', 'get_field_unique_id')
1241 + ) {
1242 + return $module_attrs;
1243 + }
1301 1244
1302 - if ('required' === $value['required_mark'] && empty($field_value) && !is_numeric($field_value)) {
1303 - $et_contact_error = true;
1304 - continue;
1305 - }
1245 + $unique_id = \ET\Builder\Packages\ModuleLibrary\ContactForm\ContactFormUtils::get_unique_id($module_attrs, (array) $module);
1306 1246
1307 - // additional check for email field
1308 - if ('email' === $value['field_type'] && 'required' === $value['required_mark'] && !empty($field_value)) {
1309 - $contact_email = isset($_POST[$value['field_id']]) ? sanitize_email($_POST[$value['field_id']]) : '';
1247 + $submitted = isset($_POST['_wpnonce-et-pb-contact-form-submitted-' . $unique_id]);
1248 + if (!$submitted) {
1249 + return $module_attrs;
1250 + }
1310 1251
1311 - if (!empty($contact_email) && !is_email($contact_email)) {
1312 - $et_contact_error = true;
1313 - }
1314 - }
1252 + if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce-et-pb-contact-form-submitted-' . $unique_id])), 'et-pb-contact-form-submit-' . $unique_id)) {
1253 + return $module_attrs;
1254 + }
1315 1255
1316 - // prepare the array of processed field values in convenient format
1317 - if (false === $et_contact_error) {
1318 - $processed_fields_values[$value['original_id']]['value'] = $field_value;
1319 - $processed_fields_values[$value['original_id']]['label'] = $value['field_label'];
1320 - }
1321 - }
1322 - }
1256 + $use_basic_captcha = $module_attrs['module']['advanced']['spamProtection']['desktop']['value']['useBasicCaptcha'] ?? 'on';
1257 + $use_spam_service = $module_attrs['module']['advanced']['spamProtection']['desktop']['value']['enabled'] ?? 'off';
1258 +
1259 + if ('on' === $use_basic_captcha && 'off' === $use_spam_service) {
1260 + $captcha_answer = sanitize_text_field(wp_unslash($_POST['et_pb_contact_captcha_' . $unique_id] ?? ''));
1261 + $first_digit = sanitize_text_field(wp_unslash($_POST['et_pb_contact_captcha_first_digit_' . $unique_id] ?? ''));
1262 + $second_digit = sanitize_text_field(wp_unslash($_POST['et_pb_contact_captcha_second_digit_' . $unique_id] ?? ''));
1263 +
1264 + if (!$captcha_answer || ((int) $first_digit + (int) $second_digit) !== (int) $captcha_answer) {
1265 + return $module_attrs;
1266 + }
1267 + } elseif ('on' === $use_spam_service) {
1268 + $provider = $module_attrs['module']['advanced']['spamProtection']['desktop']['value']['provider'] ?? 'recaptcha';
1269 + $account = $module_attrs['module']['advanced']['spamProtection']['desktop']['value']['account'] ?? '';
1270 + $min_score = (float) ($module_attrs['module']['advanced']['spamProtection']['desktop']['value']['minScore'] ?? 0.0);
1271 +
1272 + if (empty($_POST['token']) || !SpamProtectionService::validate_token($provider, $account, $min_score)) {
1273 + return $module_attrs;
1274 + }
1275 + }
1276 +
1277 + $messages = [
1278 + 'successMessage' => isset($module_attrs['module']['advanced']['successMessage']['desktop']['value']) ? $module_attrs['module']['advanced']['successMessage']['desktop']['value'] : '',
1279 + ];
1280 + if (defined('KS_PAC_DCFH_PLUGIN_FILE')) {
1281 + $send_admin_email = isset($module_attrs['adminEmail']['advanced']['adminEmailEnabled']['desktop']['value']) ? $module_attrs['adminEmail']['advanced']['adminEmailEnabled']['desktop']['value'] : 'on';
1282 + if ($send_admin_email === 'on') {
1283 + $admin_email_rich_text = isset($module_attrs['adminEmail']['advanced']['emailRichTextEnabled']['desktop']['value']) ? $module_attrs['adminEmail']['advanced']['emailRichTextEnabled']['desktop']['value'] : 'off';
1284 + if ($admin_email_rich_text === 'on') {
1285 + $messages['adminEmailRichTextMessage'] = isset($module_attrs['adminEmail']['innerContent']['emailRichTextMessage']['desktop']['value']) ? $this->decode_shortcodes($module_attrs['adminEmail']['innerContent']['emailRichTextMessage']['desktop']['value']) : '';
1323 1286 } else {
1324 - $et_contact_error = true;
1287 + $messages['email'] = isset($module_attrs['email']['innerContent']['desktop']['value']) ? $module_attrs['email']['innerContent']['desktop']['value'] : '';
1325 1288 }
1289 + }
1290 + $send_user_email = isset($module_attrs['confirmationEmail']['advanced']['sendEmailToSubmitter']['desktop']['value']) ? $module_attrs['confirmationEmail']['advanced']['sendEmailToSubmitter']['desktop']['value'] : 'off';
1291 + if ($send_user_email === 'on') {
1292 + $user_email_rich_text = isset($module_attrs['confirmationEmail']['advanced']['emailMessageRichTextEnabled']['desktop']['value']) ? $module_attrs['confirmationEmail']['advanced']['emailMessageRichTextEnabled']['desktop']['value'] : 'off';
1293 + if ($user_email_rich_text === 'on') {
1294 + $messages['confirmationEmailRichTextMessage'] = isset($module_attrs['confirmationEmail']['innerContent']['emailRichTextMessage']['desktop']['value']) ? $this->decode_shortcodes($module_attrs['confirmationEmail']['innerContent']['emailRichTextMessage']['desktop']['value']) : '';
1295 + } else {
1296 + $messages['confirmationEmailMessage'] = isset($module_attrs['confirmationEmail']['innerContent']['emailMessage']['desktop']['value']) ? $module_attrs['confirmationEmail']['innerContent']['emailMessage']['desktop']['value'] : '';
1297 + }
1298 + }
1299 + } else {
1300 + $messages['email'] = isset($module_attrs['email']['innerContent']['desktop']['value']) ? $module_attrs['email']['innerContent']['desktop']['value'] : '';
1301 + }
1302 +
1303 + foreach ($messages as $message_key => $message) {
1304 + if (false !== strpos($message, '[')) {
1305 + $shortcode_tags = [
1306 + 'e2pdf-download',
1307 + 'e2pdf-save',
1308 + 'e2pdf-view',
1309 + 'e2pdf-adobesign',
1310 + 'e2pdf-zapier',
1311 + 'e2pdf-attachment',
1312 + ];
1313 + preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $message, $matches);
1314 + $tagnames = array_intersect($shortcode_tags, $matches[1]);
1315 + if (!empty($tagnames)) {
1316 + $this->set('process', true);
1317 + } else {
1318 + unset($messages[$message_key]);
1319 + }
1326 1320 } else {
1327 - $et_contact_error = true;
1321 + unset($messages[$message_key]);
1328 1322 }
1323 + }
1329 1324
1330 - if (!$et_contact_error && $nonce_result) {
1325 + $_POST = $this->sanitize($_POST);
1331 1326
1332 - $dataset = false;
1327 + if (!$this->get('process')) {
1328 + return $module_attrs;
1329 + }
1333 1330
1334 - $success_message = '';
1335 - $custom_message = '';
1331 + if (
1332 + !class_exists('\ET\Builder\Packages\ModuleLibrary\ModuleRegistration') ||
1333 + !method_exists('\ET\Builder\Packages\ModuleLibrary\ModuleRegistration', 'get_default_attrs')) {
1334 + return $module_attrs;
1335 + }
1336 1336
1337 - if (isset($props['success_message'])) {
1338 - $success_message = str_replace(array('&#91;', '&#93;', '&quot;'), array('[', ']', '"'), $props['success_message']);
1339 - }
1337 + $fields_raw = [];
1338 + $filtered_field_attrs = [];
1339 + $fields = \ET\Builder\FrontEnd\BlockParser\BlockParserStore::get_children($module_id, $store_instance);
1340 + $default_child_attrs = \ET\Builder\Packages\ModuleLibrary\ModuleRegistration::get_default_attrs(
1341 + 'divi/contact-field'
1342 + );
1343 + foreach ($fields as $field) {
1344 + // Skip nested modules (Text, Button, Divider, etc.), only process Contact Field modules.
1345 + if ('divi/contact-field' !== $field->blockName) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- This is a property of the WP Core class.
1346 + continue;
1347 + }
1348 + $merged_child_attrs = array_replace_recursive(
1349 + $default_child_attrs,
1350 + $field->attrs ?? []
1351 + );
1352 + $child_filter_args = [
1353 + 'id' => $field->id,
1354 + 'name' => $field->blockName,
1355 + 'parentId' => $module_id,
1356 + 'parentName' => 'divi/contact-form',
1357 + 'parentAttrs' => $module_attrs,
1358 + 'storeInstance' => $store_instance,
1359 + ];
1360 + $filtered_field_attrs[$field->id] = apply_filters(
1361 + 'divi_module_library_register_module_attrs',
1362 + $merged_child_attrs,
1363 + $child_filter_args
1364 + );
1340 1365
1341 - if (false !== strpos($success_message, '[')) {
1366 + // Use pre-filtered field attributes if provided, otherwise use field attributes from storage.
1367 + $field_attrs = !empty($filtered_field_attrs[$field->id]) ? $filtered_field_attrs[$field->id] : $field->attrs;
1368 + $field_unique_id = \ET\Builder\Packages\ModuleLibrary\ContactField\ContactFieldModule::get_field_unique_id($field->id, $store_instance);
1369 + $field_type = $field_attrs['fieldItem']['advanced']['type']['desktop']['value'] ?? 'input';
1370 + $field_id = $field_attrs['fieldItem']['advanced']['id']['desktop']['value'] ?? '';
1371 + $field_key = strtolower($field_id);
1372 + $field_label = $field_attrs['fieldItem']['innerContent']['desktop']['value'] ?? '';
1342 1373
1343 - $shortcode_tags = array(
1344 - 'e2pdf-download',
1345 - 'e2pdf-save',
1346 - 'e2pdf-view',
1347 - 'e2pdf-adobesign'
1348 - );
1374 + if (!$field_label) {
1375 + $field_label = __('New Field', 'et_builder_5');
1376 + }
1349 1377
1350 - preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $success_message, $matches);
1351 - $tagnames = array_intersect($shortcode_tags, $matches[1]);
1378 + $fields_raw[$field_key]['id'] = $field_id;
1379 + $fields_raw[$field_key]['label'] = $field_label;
1380 + $fields_raw[$field_key]['type'] = $field_type;
1381 + $fields_raw[$field_key]['allowedSymbols'] = $field_attrs['fieldItem']['advanced']['allowedSymbols']['desktop']['value'] ?? '';
1382 + $fields_raw[$field_key]['maxLength'] = $field_attrs['fieldItem']['advanced']['maxLength']['desktop']['value'] ?? '';
1383 + $fields_raw[$field_key]['minLength'] = $field_attrs['fieldItem']['advanced']['minLength']['desktop']['value'] ?? '';
1384 + $fields_raw[$field_key]['isRequired'] = 'on' === ($field_attrs['fieldItem']['advanced']['required']['desktop']['value'] ?? 'on');
1352 1385
1353 - if (!empty($tagnames)) {
1386 + $fields_raw[$field_key]['conditionalLogic'] = [
1387 + 'enabled' => 'on' === ($field_attrs['conditionalLogic']['advanced']['enable']['desktop']['value'] ?? 'off'),
1388 + 'matchAll' => 'on' === ($field_attrs['conditionalLogic']['advanced']['relation']['desktop']['value'] ?? 'off'),
1389 + 'rules' => $field_attrs['conditionalLogic']['innerContent']['desktop']['value'] ?? [],
1390 + ];
1354 1391
1355 - $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
1392 + if ('text' === $field_type) {
1393 + $fields_raw[$field_key]['value'] = isset($_POST[$field_unique_id]) ? sanitize_textarea_field(wp_unslash($_POST[$field_unique_id])) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated and sanitized.
1394 + } elseif ('checkbox' === $field_type) {
1395 + // For checkbox fields, decode and sanitize immediately, store as array.
1396 + $raw_value = isset($_POST[$field_unique_id]) ? wp_unslash($_POST[$field_unique_id]) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated and sanitized.
1356 1397
1357 - preg_match_all("/$pattern/", $success_message, $shortcodes);
1398 + if ('' !== $raw_value) {
1399 + // Split the URL-encoded comma-separated values.
1400 + $parts = explode(',', $raw_value);
1401 + $parts = array_map('urldecode', $parts);
1402 + $parts = array_map('sanitize_text_field', $parts);
1403 + $parts = array_map('trim', $parts);
1404 + $fields_raw[$field_key]['value'] = $parts;
1405 + } else {
1406 + $fields_raw[$field_key]['value'] = [];
1407 + }
1408 + } else {
1409 + $fields_raw[$field_key]['value'] = isset($_POST[$field_unique_id]) ? sanitize_text_field(wp_unslash($_POST[$field_unique_id])) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated and sanitized.
1410 + }
1358 1411
1359 - foreach ($shortcodes[0] as $key => $shortcode_value) {
1412 + switch ($field_type) {
1413 + case 'checkbox':
1414 + $options = $field_attrs['fieldItem']['advanced']['checkboxOptions']['desktop']['value'] ?? [];
1415 + $fields_raw[$field_key]['options'] = is_array($options) ? array_map(
1416 + function ($option) {
1417 + return wp_strip_all_tags($option['value'] ?? '');
1418 + },
1419 + $options
1420 + ) : [];
1421 + break;
1422 + case 'radio':
1423 + $options = $field_attrs['fieldItem']['advanced']['radioOptions']['desktop']['value'] ?? [];
1424 + $fields_raw[$field_key]['options'] = is_array($options) ? array_map(
1425 + function ($option) {
1426 + return sanitize_text_field($option['value'] ?? '');
1427 + },
1428 + $options
1429 + ) : [];
1430 + break;
1431 + case 'select':
1432 + $options = $field_attrs['fieldItem']['advanced']['selectOptions']['desktop']['value'] ?? [];
1433 + $fields_raw[$field_key]['options'] = is_array($options) ? array_map(
1434 + function ($option) {
1435 + return sanitize_text_field($option['value'] ?? '');
1436 + },
1437 + $options
1438 + ) : [];
1439 + break;
1440 + default:
1441 + $fields_raw[$field_key]['options'] = [];
1442 + break;
1443 + }
1444 + }
1360 1445
1361 - $item = false;
1446 + $refClass = new ReflectionClass('\ET\Builder\Packages\ModuleLibrary\ContactForm\ContactFormHandler');
1447 + $obj = $refClass->newInstanceWithoutConstructor();
1362 1448
1363 - $shortcode = array();
1364 - $shortcode[1] = $shortcodes[1][$key];
1365 - $shortcode[2] = $shortcodes[2][$key];
1366 - $shortcode[3] = $shortcodes[3][$key];
1367 - $shortcode[4] = $shortcodes[4][$key];
1368 - $shortcode[5] = $shortcodes[5][$key];
1369 - $shortcode[6] = $shortcodes[6][$key];
1449 + try {
1450 + $refFields = $refClass->getProperty('_fields_raw');
1451 + $refFields->setAccessible(true);
1452 + $refFields->setValue($obj, $fields_raw);
1370 1453
1371 - $atts = shortcode_parse_atts($shortcode[3]);
1454 + $refError = $refClass->getProperty('_error');
1455 + $refError->setAccessible(true);
1456 + $refError->setValue($obj, new WP_Error());
1457 + } catch (\ReflectionException $e) {
1458 + return $module_attrs;
1459 + }
1372 1460
1373 - if (($shortcode[2] === 'e2pdf-save' && isset($atts['attachment']) && $atts['attachment'] == 'true') || $shortcode[2] === 'e2pdf-attachment') {
1374 -
1375 - } else {
1376 - if (!isset($atts['dataset']) && isset($atts['id'])) {
1377 - $template = new Model_E2pdf_Template();
1378 - $template->load($atts['id']);
1461 + $obj->validate_fields();
1462 + $errorObj = $refError->getValue($obj);
1379 1463
1380 - if ($template->get('extension') === 'divi') {
1381 - $item = $template->get('item');
1464 + if ($errorObj->has_errors()) {
1465 + return $module_attrs;
1466 + }
1382 1467
1383 - if (!$dataset) {
1384 - $serialized = serialize($_POST);
1385 - $entry = array(
1386 - 'extension' => 'divi',
1387 - 'item' => $item,
1388 - 'entry' => $serialized
1389 - );
1390 - $wpdb->insert($wpdb->prefix . 'e2pdf_datasets', $entry);
1391 - $dataset = $wpdb->insert_id;
1392 - }
1393 - $atts['dataset'] = $dataset;
1394 - $shortcode[3] .= " dataset=\"{$dataset}\" ";
1395 - }
1396 - }
1468 + $fields_raw = $refFields->getValue($obj);
1397 1469
1398 - if (!isset($atts['apply'])) {
1399 - $shortcode[3] .= " apply=\"true\"";
1400 - }
1470 + $item = !empty($module_attrs['module']['meta']['adminLabel']['desktop']['value']) ? $module_attrs['module']['meta']['adminLabel']['desktop']['value'] : '';
1471 + if (!$item) {
1472 + return $module_attrs;
1473 + }
1401 1474
1402 - $props['success_message'] = str_replace(str_replace(array('[', ']'), array('&#91;', '&#93;'), $shortcode_value), "[" . $shortcode[2] . $shortcode[3] . "]", $props['success_message']);
1475 + $data = [
1476 + 'E2PDF_VERSION' => '1.32.40',
1477 + 'fields' => [],
1478 + 'options' => [
1479 + 'timestamp' => current_time('mysql'),
1480 + 'saveFilesToMedia' => !empty($module_attrs['entryPreferences']['advanced']['saveFilesToMedia']['desktop']['value']) ? $module_attrs['entryPreferences']['advanced']['saveFilesToMedia']['desktop']['value'] : '',
1481 + 'uniqueId' => !empty($module_attrs['module']['advanced']['uniqueId']['desktop']['value']) ? $module_attrs['module']['advanced']['uniqueId']['desktop']['value'] : '',
1482 + ],
1483 + ];
1484 +
1485 + if (!empty($fields_raw) && is_array($fields_raw)) {
1486 + foreach ($fields_raw as $field_key => $field_raw) {
1487 + if (isset($field_raw['value'])) {
1488 + $data['fields'][$field_key] = [
1489 + 'type' => isset($field_raw['type']) ? $field_raw['type'] : '',
1490 + 'value' => $field_raw['value'],
1491 + ];
1492 + }
1493 + }
1494 + }
1495 + if (empty($data['fields']['_wp_http_referer']) && !empty($_POST['_wp_http_referer'])) {
1496 + $data['fields']['_wp_http_referer'] = [
1497 + 'type' => '',
1498 + 'value' => esc_url_raw($_POST['_wp_http_referer']),
1499 + ];
1500 + }
1501 +
1502 + $entry = new Model_E2pdf_Dataset();
1503 + $entry->set('extension', 'divi');
1504 + $entry->set('item', $item);
1505 + $entry->set('entry', $data);
1506 + $dataset = $entry->save();
1507 +
1508 + foreach ($messages as $message_key => $message) {
1509 + if (false !== strpos($message, '[')) {
1510 + $shortcode_tags = [
1511 + 'e2pdf-download',
1512 + 'e2pdf-save',
1513 + 'e2pdf-view',
1514 + 'e2pdf-adobesign',
1515 + 'e2pdf-zapier',
1516 + 'e2pdf-attachment',
1517 + ];
1518 + preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $message, $matches);
1519 + $tagnames = array_intersect($shortcode_tags, $matches[1]);
1520 + if (!empty($tagnames)) {
1521 + preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $message, $shortcodes);
1522 + foreach ($shortcodes[0] as $key => $shortcode_value) {
1523 + $item = false;
1524 + $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1525 + $atts = shortcode_parse_atts($shortcode[3]);
1526 +
1527 + if (!isset($atts['dataset']) && isset($atts['id'])) {
1528 + $template = new Model_E2pdf_Template();
1529 + $template->load($atts['id']);
1530 + if ($template->get('extension') === 'divi') {
1531 + $item = $template->get('item');
1532 + $atts['dataset'] = $dataset;
1533 + $shortcode[3] .= ' dataset="' . $dataset . '"';
1403 1534 }
1404 1535 }
1536 + if (!isset($atts['apply'])) {
1537 + $shortcode[3] .= ' apply="true"';
1538 + }
1539 + if (!isset($atts['iframe_download'])) {
1540 + $shortcode[3] .= ' iframe_download="true"';
1541 + }
1542 + switch ($message_key) {
1543 + case 'successMessage':
1544 + $module_attrs['module']['advanced']['successMessage']['desktop']['value'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $module_attrs['module']['advanced']['successMessage']['desktop']['value']);
1545 + break;
1546 + case 'email':
1547 + $module_attrs['email']['innerContent']['desktop']['value'] = str_replace($shortcode_value, '[' . $shortcode[2] . $shortcode[3] . ']', $module_attrs['email']['innerContent']['desktop']['value']);
1548 + break;
1549 + case 'adminEmailRichTextMessage':
1550 + case 'confirmationEmailMessage':
1551 + case 'confirmationEmailRichTextMessage':
1552 + $messages[$message_key] = str_replace($shortcode_value, '{' . $shortcode[2] . $shortcode[3] . '}', $messages[$message_key]);
1553 + break;
1554 + default:
1555 + break;
1556 + }
1405 1557 }
1406 1558 }
1559 + }
1560 + }
1407 1561
1408 - if (isset($props['custom_message'])) {
1409 - $custom_message = str_replace(array('&#91;', '&#93;'), array('[', ']'), $props['custom_message']);
1410 - }
1562 + foreach ($messages as $message_key => $message) {
1563 + switch ($message_key) {
1564 + case 'adminEmailRichTextMessage':
1565 + $class = '\KS_PAC_DCFH\Frontend\Controllers\ContactFormDataRepository';
1566 + if (class_exists($class) && method_exists($class, 'instance')) {
1567 + $contact_form_rep = $class::instance();
1568 + $contact_form_rep->admin_message = $messages[$message_key];
1569 + }
1570 + break;
1571 + case 'confirmationEmailMessage':
1572 + case 'confirmationEmailRichTextMessage':
1573 + $class = '\KS_PAC_DCFH\Frontend\Services\ConfirmationEmailService';
1574 + if (class_exists($class) && method_exists($class, 'instance')) {
1575 + $contact_form_rep = $class::instance();
1576 + $contact_form_rep->email_message = $messages[$message_key];
1577 + }
1578 + break;
1579 + default:
1580 + break;
1581 + }
1582 + }
1583 + return $module_attrs;
1584 + }
1411 1585
1412 - if (false !== strpos($custom_message, '[')) {
1413 - $shortcode_tags = array(
1414 - 'e2pdf-download',
1415 - 'e2pdf-save',
1416 - 'e2pdf-attachment',
1417 - 'e2pdf-adobesign'
1418 - );
1586 + // filter shortcode attributes
1587 + public function filter_et_pb_module_shortcode_attributes($props, $attrs, $render_slug, $address, $content) {
1419 1588
1420 - preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $custom_message, $matches);
1421 - $tagnames = array_intersect($shortcode_tags, $matches[1]);
1589 + if ($render_slug !== 'et_pb_contact_form') {
1590 + return $props;
1591 + }
1422 1592
1423 - if (!empty($tagnames)) {
1593 + $et_contact_proccess = array_search('et_contact_proccess', (array) $_POST);
1594 + if ($et_contact_proccess === false) {
1595 + return $props;
1596 + }
1424 1597
1425 - $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
1598 + $success_message = isset($props['success_message']) ? $this->decode_shortcodes($props['success_message'], true) : '';
1426 1599
1427 - preg_match_all("/$pattern/", $custom_message, $shortcodes);
1600 + if (false !== strpos($success_message, '[')) {
1601 + $shortcode_tags = [
1602 + 'e2pdf-download',
1603 + 'e2pdf-save',
1604 + 'e2pdf-view',
1605 + 'e2pdf-adobesign',
1606 + 'e2pdf-zapier',
1607 + ];
1608 + preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $success_message, $matches);
1609 + $tagnames = array_intersect($shortcode_tags, $matches[1]);
1610 + if (!empty($tagnames)) {
1611 + $this->set('process', true);
1612 + } else {
1613 + $success_message = '';
1614 + }
1615 + }
1428 1616
1429 - add_filter('wp_mail', array($this, 'filter_wp_mail'), 10, 2);
1617 + $admin_email_rich_text = isset($props['use_custom_message_richtext']) && $props['use_custom_message_richtext'] == 'on' && isset($props['custom_message_richtext']) ? true : false;
1618 + if ($admin_email_rich_text) {
1619 + $admin_email_message = $this->decode_shortcodes($props['custom_message_richtext']);
1620 + } else {
1621 + $admin_email_message = isset($props['custom_message']) ? $this->decode_shortcodes($props['custom_message']) : '';
1622 + }
1623 + if (false !== strpos($admin_email_message, '[')) {
1624 + $shortcode_tags = [
1625 + 'e2pdf-download',
1626 + 'e2pdf-save',
1627 + 'e2pdf-attachment',
1628 + 'e2pdf-adobesign',
1629 + 'e2pdf-zapier',
1630 + ];
1631 + preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $admin_email_message, $matches);
1632 + $tagnames = array_intersect($shortcode_tags, $matches[1]);
1633 + if (!empty($tagnames)) {
1634 + $this->set('process', true);
1635 + } else {
1636 + $admin_email_message = '';
1637 + }
1638 + }
1430 1639
1431 - foreach ($shortcodes[0] as $key => $shortcode_value) {
1432 - $item = false;
1640 + /* Divi Contact Form Helper Confirmation Email and Confirmation Email RichText */
1641 + $user_email_rich_text = isset($props['use_confirmation_email']) && $props['use_confirmation_email'] == 'on' && isset($props['use_confirmation_message_richtext']) && $props['use_confirmation_message_richtext'] == 'on' && isset($props['confirmation_message_richtext']) ? true : false;
1642 + if ($user_email_rich_text) {
1643 + $user_email_message = $this->decode_shortcodes($props['confirmation_message_richtext']);
1644 + } else {
1645 + $user_email_message = isset($props['use_confirmation_email']) && $props['use_confirmation_email'] == 'on' && isset($props['confirmation_email_message']) ? $this->decode_shortcodes($props['confirmation_email_message']) : '';
1646 + }
1647 + if (false !== strpos($user_email_message, '[')) {
1648 + $shortcode_tags = [
1649 + 'e2pdf-download',
1650 + 'e2pdf-save',
1651 + 'e2pdf-attachment',
1652 + 'e2pdf-adobesign',
1653 + 'e2pdf-zapier',
1654 + ];
1655 + preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $user_email_message, $matches);
1656 + $tagnames = array_intersect($shortcode_tags, $matches[1]);
1657 + if (!empty($tagnames)) {
1658 + $this->set('process', true);
1659 + } else {
1660 + $user_email_message = '';
1661 + }
1662 + }
1433 1663
1434 - $shortcode = array();
1435 - $shortcode[1] = $shortcodes[1][$key];
1436 - $shortcode[2] = $shortcodes[2][$key];
1437 - $shortcode[3] = $shortcodes[3][$key];
1438 - $shortcode[4] = $shortcodes[4][$key];
1439 - $shortcode[5] = $shortcodes[5][$key];
1440 - $shortcode[6] = $shortcodes[6][$key];
1664 + $_POST = $this->sanitize($_POST);
1441 1665
1442 - $atts = shortcode_parse_atts($shortcode[3]);
1666 + // check if E2Pdf shortcodes exist in messages
1667 + if (!$this->get('process')) {
1668 + return $props;
1669 + }
1443 1670
1671 + $data = $_POST;
1672 + if (isset($props['_unique_id'])) {
1673 + $data['_unique_id'] = $props['_unique_id'];
1674 + }
1675 + if (isset($props['save_files_to_media']) && $props['save_files_to_media'] == 'on') {
1676 + $data['_save_files_to_media'] = $props['save_files_to_media'];
1677 + $data['_subdir'] = wp_upload_dir()['subdir'];
1678 + } elseif (function_exists('pwh_dcfh_file_helpers')) {
1679 + $subdir = pwh_dcfh_file_helpers()::get_subdir();
1680 + if (is_array($subdir)) {
1681 + $data['_subdir'] = implode('/', $subdir);
1682 + }
1683 + } elseif (function_exists('ks_pac_dcfh_file_helper')) {
1684 + $subdir = ks_pac_dcfh_file_helper()::get_subdir();
1685 + if (is_array($subdir)) {
1686 + $data['_subdir'] = implode('/', $subdir);
1687 + }
1688 + }
1689 +
1690 + $captcha = isset($props['captcha']) ? $props['captcha'] : '';
1691 + $use_spam_service = isset($props['use_spam_service']) ? $props['use_spam_service'] : 'off';
1692 + $et_pb_contact_form_num = str_replace('et_pb_contactform_submit_', '', $et_contact_proccess);
1693 + $et_contact_error = false;
1694 + $current_form_fields = isset($data['et_pb_contact_email_fields_' . $et_pb_contact_form_num]) ? $data['et_pb_contact_email_fields_' . $et_pb_contact_form_num] : '';
1695 + $contact_email = '';
1696 + $nonce_result = isset($data['_wpnonce-et-pb-contact-form-submitted-' . $et_pb_contact_form_num]) && wp_verify_nonce($data['_wpnonce-et-pb-contact-form-submitted-' . $et_pb_contact_form_num], 'et-pb-contact-form-submit') ? true : false;
1697 +
1698 + if ($nonce_result && isset($data['et_pb_contactform_submit_' . $et_pb_contact_form_num]) && empty($data['et_pb_contact_et_number_' . $et_pb_contact_form_num])) {
1699 + if ('' !== $current_form_fields) {
1700 + $fields_data_json = str_replace('\\', '', $current_form_fields);
1701 + $fields_data_array = json_decode($fields_data_json, true);
1702 +
1703 + // check whether captcha field is not empty
1704 + if ('on' === $captcha && 'off' === $use_spam_service && (!isset($data['et_pb_contact_captcha_' . $et_pb_contact_form_num]) || empty($data['et_pb_contact_captcha_' . $et_pb_contact_form_num]))) {
1705 + $et_contact_error = true;
1706 + } elseif ('on' === $use_spam_service) {
1707 + if (class_exists('ET_Builder_Element')) {
1708 + $contact_form = ET_Builder_Element::get_module('et_pb_contact_form');
1709 + if ($contact_form && is_object($contact_form) && method_exists($contact_form, 'is_spam_submission')) {
1710 + $contact_form->props = $props;
1711 + if ($contact_form->is_spam_submission()) {
1712 + if (!empty($_POST['token'])) {
1713 + unset($_POST['token']);
1714 + }
1715 + $et_contact_error = true;
1716 + } else {
1717 + $props['use_spam_service'] = 'off';
1718 + $props['captcha'] = 'off';
1719 + }
1720 + }
1721 + }
1722 + }
1723 +
1724 + // check all fields on current form and generate error message if needed
1725 + if (!empty($fields_data_array)) {
1726 + foreach ($fields_data_array as $value) {
1727 + if (isset($value['field_id']) && 'et_pb_contact_et_number_' . $et_pb_contact_form_num === $value['field_id']) {
1728 + continue;
1729 + }
1730 + /* Check all the required fields, generate error message if required field is empty */
1731 + $field_value = isset($data[$value['field_id']]) ? trim($data[$value['field_id']]) : '';
1732 + if ('required' === $value['required_mark'] && empty($field_value) && !is_numeric($field_value)) {
1733 + $et_contact_error = true;
1734 + continue;
1735 + }
1736 + /* Additional check for email field */
1737 + if ('email' === $value['field_type'] && 'required' === $value['required_mark'] && !empty($field_value)) {
1738 + $contact_email = isset($data[$value['field_id']]) ? sanitize_email($data[$value['field_id']]) : '';
1739 + if (!empty($contact_email) && !is_email($contact_email)) {
1740 + $et_contact_error = true;
1741 + }
1742 + }
1743 + }
1744 + }
1745 + } else {
1746 + $et_contact_error = true;
1747 + }
1748 + } else {
1749 + $et_contact_error = true;
1750 + }
1751 +
1752 + if (!$et_contact_error && $nonce_result) {
1753 + // divi contact form helper 2.1.3 bug fix wp_mail filter removed at maybe_filter_wp_mail
1754 + if (defined('KS_PAC_DCFH_PLUGIN_VERSION') && version_compare(KS_PAC_DCFH_PLUGIN_VERSION, '2.1.3', '>=')) {
1755 + add_filter(
1756 + 'wp_mail', function ($args) {
1757 + return $args;
1758 + }, 999
1759 + );
1760 + }
1761 + add_filter('wp_mail', [$this, 'filter_wp_mail'], 1000);
1762 +
1763 + $dataset = false;
1764 + if (false !== strpos($success_message, '[')) {
1765 + $shortcode_tags = [
1766 + 'e2pdf-download',
1767 + 'e2pdf-save',
1768 + 'e2pdf-view',
1769 + 'e2pdf-adobesign',
1770 + 'e2pdf-zapier',
1771 + ];
1772 + preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $success_message, $matches);
1773 + $tagnames = array_intersect($shortcode_tags, $matches[1]);
1774 + if (!empty($tagnames)) {
1775 + preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $success_message, $shortcodes);
1776 + foreach ($shortcodes[0] as $key => $shortcode_value) {
1777 + $item = false;
1778 + $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1779 + $atts = shortcode_parse_atts($shortcode[3]);
1780 + if ($this->helper->load('shortcode')->is_attachment($shortcode, $atts)) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
1781 + } else {
1444 1782 if (!isset($atts['dataset']) && isset($atts['id'])) {
1445 1783 $template = new Model_E2pdf_Template();
1446 1784 $template->load($atts['id']);
1447 1785 if ($template->get('extension') === 'divi') {
1448 1786 $item = $template->get('item');
1449 -
1450 1787 if (!$dataset) {
1451 - $serialized = serialize($_POST);
1452 - $entry = array(
1453 - 'extension' => 'divi',
1454 - 'item' => $item,
1455 - 'entry' => $serialized
1456 - );
1457 - $wpdb->insert($wpdb->prefix . 'e2pdf_datasets', $entry);
1458 - $dataset = $wpdb->insert_id;
1788 + $entry = new Model_E2pdf_Dataset();
1789 + $entry->set('extension', 'divi');
1790 + $entry->set('item', $item);
1791 + $entry->set('entry', $data);
1792 + $dataset = $entry->save();
1459 1793 }
1460 1794 $atts['dataset'] = $dataset;
1461 - $shortcode[3] .= " dataset=\"{$dataset}\"";
1795 + $shortcode[3] .= ' dataset="' . $dataset . '"';
1462 1796 }
1463 1797 }
1798 + if (!isset($atts['apply'])) {
1799 + $shortcode[3] .= ' apply="true"';
1800 + }
1801 + if (!isset($atts['iframe_download'])) {
1802 + $shortcode[3] .= ' iframe_download="true"';
1803 + }
1804 + $props['success_message'] = str_replace($this->encode_shortcodes($shortcode_value), '[' . $shortcode[2] . $shortcode[3] . ']', $props['success_message']);
1805 + }
1806 + }
1807 + }
1808 + }
1464 1809
1465 - $props['custom_message'] = str_replace(str_replace(array('[', ']'), array('&#91;', '&#93;'), $shortcode_value), "[" . $shortcode[2] . $shortcode[3] . "]", $props['custom_message']);
1810 + if (false !== strpos($admin_email_message, '[')) {
1811 + $shortcode_tags = [
1812 + 'e2pdf-download',
1813 + 'e2pdf-save',
1814 + 'e2pdf-attachment',
1815 + 'e2pdf-adobesign',
1816 + 'e2pdf-zapier',
1817 + ];
1818 + preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $admin_email_message, $matches);
1819 + $tagnames = array_intersect($shortcode_tags, $matches[1]);
1820 + if (!empty($tagnames)) {
1821 + preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $admin_email_message, $shortcodes);
1822 + foreach ($shortcodes[0] as $key => $shortcode_value) {
1823 + $item = false;
1824 + $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1825 + $atts = shortcode_parse_atts($shortcode[3]);
1826 + if (!isset($atts['dataset']) && isset($atts['id'])) {
1827 + $template = new Model_E2pdf_Template();
1828 + $template->load($atts['id']);
1829 + if ($template->get('extension') === 'divi') {
1830 + $item = $template->get('item');
1831 + if (!$dataset) {
1832 + $entry = new Model_E2pdf_Dataset();
1833 + $entry->set('extension', 'divi');
1834 + $entry->set('item', $item);
1835 + $entry->set('entry', $data);
1836 + $dataset = $entry->save();
1837 + }
1838 + $atts['dataset'] = $dataset;
1839 + $shortcode[3] .= ' dataset="' . $dataset . '"';
1840 + }
1466 1841 }
1842 + if ($admin_email_rich_text) {
1843 + if ((defined('PWH_DCFH_PLUGIN_VERSION') && version_compare(PWH_DCFH_PLUGIN_VERSION, '1.5.1', '>=')) || defined('KS_PAC_DCFH_PLUGIN_FILE')) {
1844 + $props['custom_message_richtext'] = str_replace($this->encode_shortcodes($shortcode_value), '{' . $shortcode[2] . $shortcode[3] . '}', $props['custom_message_richtext']);
1845 + } else {
1846 + $props['custom_message_richtext'] = str_replace($this->encode_shortcodes($shortcode_value), '[' . $shortcode[2] . $shortcode[3] . ']', $props['custom_message_richtext']);
1847 + }
1848 + } else {
1849 + $props['custom_message'] = str_replace($this->encode_shortcodes($shortcode_value), '[' . $shortcode[2] . $shortcode[3] . ']', $props['custom_message']);
1850 + }
1467 1851 }
1468 1852 }
1469 1853 }
1854 +
1855 + if (false !== strpos($user_email_message, '[')) {
1856 + $shortcode_tags = [
1857 + 'e2pdf-download',
1858 + 'e2pdf-save',
1859 + 'e2pdf-attachment',
1860 + 'e2pdf-adobesign',
1861 + 'e2pdf-zapier',
1862 + ];
1863 + preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $user_email_message, $matches);
1864 + $tagnames = array_intersect($shortcode_tags, $matches[1]);
1865 + if (!empty($tagnames)) {
1866 + preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $user_email_message, $shortcodes);
1867 + foreach ($shortcodes[0] as $key => $shortcode_value) {
1868 + $item = false;
1869 + $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1870 + $atts = shortcode_parse_atts($shortcode[3]);
1871 + if (!isset($atts['dataset']) && isset($atts['id'])) {
1872 + $template = new Model_E2pdf_Template();
1873 + $template->load($atts['id']);
1874 + if ($template->get('extension') === 'divi') {
1875 + $item = $template->get('item');
1876 + if (!$dataset) {
1877 + $entry = new Model_E2pdf_Dataset();
1878 + $entry->set('extension', 'divi');
1879 + $entry->set('item', $item);
1880 + $entry->set('entry', $data);
1881 + $dataset = $entry->save();
1882 + }
1883 + $atts['dataset'] = $dataset;
1884 + $shortcode[3] .= ' dataset="' . $dataset . '"';
1885 + }
1886 + }
1887 + if ((defined('PWH_DCFH_PLUGIN_VERSION') && version_compare(PWH_DCFH_PLUGIN_VERSION, '1.5.1', '>=')) || defined('KS_PAC_DCFH_PLUGIN_FILE')) {
1888 + if ($user_email_rich_text) {
1889 + $props['confirmation_message_richtext'] = str_replace($this->encode_shortcodes($shortcode_value), '{' . $shortcode[2] . $shortcode[3] . '}', $props['confirmation_message_richtext']);
1890 + } else {
1891 + $props['confirmation_email_message'] = str_replace($this->encode_shortcodes($shortcode_value), '{' . $shortcode[2] . $shortcode[3] . '}', $props['confirmation_email_message']);
1892 + }
1893 + } else {
1894 + if ($user_email_rich_text) {
1895 + $props['confirmation_message_richtext'] = str_replace($this->encode_shortcodes($shortcode_value), '[' . $shortcode[2] . $shortcode[3] . ']', $props['confirmation_message_richtext']);
1896 + } else {
1897 + $props['confirmation_email_message'] = str_replace($this->encode_shortcodes($shortcode_value), '[' . $shortcode[2] . $shortcode[3] . ']', $props['confirmation_email_message']);
1898 + }
1899 + }
1900 + }
1901 + }
1902 + }
1470 1903 }
1471 1904
1472 1905 return $props;
1473 1906 }
1474 1907
1908 + // filter shortcode output
1475 1909 public function filter_et_module_shortcode_output($output, $render_slug, $form) {
1476 1910
1477 - if ($render_slug && $render_slug == 'et_pb_contact_form' && !empty($_POST) && $et_contact_proccess = array_search('et_contact_proccess', $_POST)) {
1911 + if ($render_slug !== 'et_pb_contact_form') {
1912 + return $output;
1913 + }
1478 1914
1479 - $et_pb_contact_form_num = str_replace("et_pb_contactform_submit_", "", $et_contact_proccess);
1915 + $et_contact_proccess = array_search('et_contact_proccess', (array) $_POST);
1916 + if ($et_contact_proccess === false) {
1917 + return $output;
1918 + }
1480 1919
1481 - $nonce_result = isset($_POST['_wpnonce-et-pb-contact-form-submitted-' . $et_pb_contact_form_num]) && wp_verify_nonce($_POST['_wpnonce-et-pb-contact-form-submitted-' . $et_pb_contact_form_num], 'et-pb-contact-form-submit') ? true : false;
1920 + $et_pb_contact_form_num = str_replace(['pwh_dcfh_et_pb_contactform_submit_', 'et_pb_contactform_submit_'], ['', ''], $et_contact_proccess);
1921 + $nonce_result = isset($_POST['_wpnonce-et-pb-contact-form-submitted-' . $et_pb_contact_form_num]) && wp_verify_nonce($_POST['_wpnonce-et-pb-contact-form-submitted-' . $et_pb_contact_form_num], 'et-pb-contact-form-submit') ? true : false;
1482 1922
1483 - if ($nonce_result && isset($_POST['et_pb_contactform_submit_' . $et_pb_contact_form_num]) && empty($_POST['et_pb_contactform_validate_' . $et_pb_contact_form_num])) {
1484 -
1485 - $dataset = false;
1486 -
1487 - $success_message = '';
1488 - $custom_message = '';
1489 -
1490 - if (isset($form->props['success_message'])) {
1491 - $success_message = str_replace(array('&#91;', '&#93;', '&quot;'), array('[', ']', '"'), $form->props['success_message']);
1492 - }
1493 -
1494 - if (false !== strpos($success_message, '[')) {
1495 - $shortcode_tags = array(
1496 - 'e2pdf-download',
1497 - 'e2pdf-save',
1498 - 'e2pdf-view',
1499 - 'e2pdf-adobesign'
1500 - );
1501 -
1502 - preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $success_message, $matches);
1503 - $tagnames = array_intersect($shortcode_tags, $matches[1]);
1504 -
1505 - if (!empty($tagnames)) {
1506 -
1507 - $pattern = $this->helper->load('shortcode')->get_shortcode_regex($tagnames);
1508 -
1509 - preg_match_all("/$pattern/", $success_message, $shortcodes);
1510 -
1511 - foreach ($shortcodes[0] as $key => $shortcode_value) {
1512 -
1513 - $shortcode = array();
1514 - $shortcode[1] = $shortcodes[1][$key];
1515 - $shortcode[2] = $shortcodes[2][$key];
1516 - $shortcode[3] = $shortcodes[3][$key];
1517 - $shortcode[4] = $shortcodes[4][$key];
1518 - $shortcode[5] = $shortcodes[5][$key];
1519 - $shortcode[6] = $shortcodes[6][$key];
1520 -
1521 - $output = str_replace(str_replace(array('"'), array('&quot;'), $shortcode_value), do_shortcode_tag($shortcode), $output);
1923 + if ($nonce_result && (
1924 + (isset($_POST['et_pb_contactform_submit_' . $et_pb_contact_form_num]) && empty($_POST['et_pb_contact_et_number_' . $et_pb_contact_form_num])) ||
1925 + (isset($_POST['pwh_dcfh_et_pb_contactform_submit_' . $et_pb_contact_form_num]) && $_POST['pwh_dcfh_et_pb_contactform_submit_' . $et_pb_contact_form_num] == 'et_contact_proccess')
1926 + )
1927 + ) {
1928 + $success_message = isset($form->props['success_message']) ? $this->decode_shortcodes($form->props['success_message'], true) : '';
1929 + if (false !== strpos($success_message, '[')) {
1930 + $shortcode_tags = [
1931 + 'e2pdf-download',
1932 + 'e2pdf-save',
1933 + 'e2pdf-view',
1934 + 'e2pdf-adobesign',
1935 + 'e2pdf-zapier',
1936 + ];
1937 + preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $success_message, $matches);
1938 + $tagnames = array_intersect($shortcode_tags, $matches[1]);
1939 + if (!empty($tagnames)) {
1940 + preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $success_message, $shortcodes);
1941 + foreach ($shortcodes[0] as $key => $shortcode_value) {
1942 + $shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key);
1943 + if (isset($form->props['enable_multistep']) && 'on' === $form->props['enable_multistep']) {
1944 + $output = str_replace($shortcode_value, do_shortcode_tag($shortcode), $output);
1945 + } else {
1946 + $output = str_replace(str_replace(['"'], ['&quot;'], $shortcode_value), do_shortcode_tag($shortcode), $output);
1522 1947 }
1523 1948 }
1524 1949 }
1525 1950 }
1526 -
1527 - remove_filter('wp_mail', array($this, 'filter_wp_mail'), 10);
1528 -
1529 - $files = $this->helper->get('divi_attachments');
1530 - if (is_array($files) && !empty($files)) {
1531 - foreach ($files as $key => $file) {
1532 - $this->helper->delete_dir(dirname($file) . '/');
1533 - }
1534 - $this->helper->deset('divi_attachments');
1535 - }
1536 1951 }
1537 -
1538 1952 return $output;
1539 1953 }
1540 1954
1541 - /**
1542 - * Delete dataset for template
1543 - *
1544 - * @param int $template_id - Template ID
1545 - * @param int $dataset - Dataset ID
1546 - *
1547 - * @return bool - Result of removing items
1548 - */
1955 + // delete item
1549 1956 public function delete_item($template_id = false, $dataset = false) {
1550 1957 global $wpdb;
1551 -
1552 1958 $template = new Model_E2pdf_Template();
1553 1959 if ($template_id && $dataset && $template->load($template_id)) {
1554 1960 if ($template->get('extension') === 'divi' && $template->get('item')) {
1555 -
1556 1961 $item = $template->get('item');
1557 -
1558 - $where = array(
1962 + $where = [
1559 1963 'ID' => $dataset,
1560 1964 'item' => $item,
1561 - 'extension' => 'divi'
1562 - );
1965 + 'extension' => 'divi',
1966 + ];
1563 1967 $wpdb->delete($wpdb->prefix . 'e2pdf_datasets', $where);
1564 1968 return true;
1565 1969 }
1566 1970 }
1567 -
1568 1971 return false;
1569 1972 }
1570 1973
1571 - /**
1572 - * Delete all datasets for Template
1573 - *
1574 - * @param int $template_id - Template ID
1575 - *
1576 - * @return bool - Result of removing items
1577 - */
1974 + // delete items
1578 1975 public function delete_items($template_id = false) {
1579 1976 global $wpdb;
1580 -
1581 1977 $template = new Model_E2pdf_Template();
1582 -
1583 1978 if ($template_id && $template->load($template_id)) {
1584 1979 if ($template->get('extension') === 'divi' && $template->get('item')) {
1980 + $where = [
1981 + 'item' => $template->get('item'),
1982 + 'extension' => 'divi',
1983 + ];
1984 + $wpdb->delete($wpdb->prefix . 'e2pdf_datasets', $where);
1985 + return true;
1986 + }
1987 + }
1988 + return false;
1989 + }
1585 1990
1586 - $item = $template->get('item');
1991 + // styles
1992 + public function styles($item_id = false) {
1993 + $styles = [];
1994 + if ($this->divi5()) {
1995 + $assets_list = \ET\Builder\FrontEnd\Assets\DynamicAssetsUtils::get_assets_list(
1996 + [
1997 + 'prefix' => \ET\Builder\FrontEnd\Assets\DynamicAssetsUtils::get_dynamic_assets_path(),
1998 + 'suffix' => '',
1999 + 'specialty_suffix' => '',
2000 + ]
2001 + );
2002 + foreach ($assets_list as $list) {
2003 + if (!empty($list['css']) && is_array($list['css'])) {
2004 + foreach ($list['css'] as $css) {
2005 + $styles[] = $this->wp_convert_path_to_url($css);
2006 + }
2007 + }
2008 + }
2009 + }
2010 + $styles[] = plugins_url('css/extension/divi.css?v=' . time(), $this->helper->get('plugin_file_path'));
2011 + return $styles;
2012 + }
1587 2013
1588 - $where = array(
1589 - 'item' => $item,
1590 - 'extension' => 'divi'
1591 - );
1592 - $wpdb->delete($wpdb->prefix . 'e2pdf_datasets', $where);
1593 - return true;
2014 + public function wp_convert_path_to_url($path) {
2015 + // Standardize slashes for Windows/Linux compatibility
2016 + $path = wp_normalize_path($path);
2017 + $root = wp_normalize_path(ABSPATH);
2018 +
2019 + // Replace the server root with the Site URL
2020 + return str_replace($root, site_url('/'), $path);
2021 + }
2022 +
2023 + // sanitize
2024 + public function sanitize($value) {
2025 + if (is_array($value)) {
2026 + foreach ($value as $k => $v) {
2027 + $value[$k] = $this->sanitize($v);
1594 2028 }
2029 + return $value;
1595 2030 }
2031 + if (!is_string($value)) {
2032 + return $value;
2033 + }
2034 + if (stripos($value, 'e2pdf-') === false) {
2035 + return $value;
2036 + }
2037 + return preg_replace('/(?:\[|&#91;|\{)(e2pdf-download|e2pdf-view|e2pdf-save|e2pdf-attachment|e2pdf-adobesign|e2pdf-zapier)[^\]}\r\n]*(?:\]|&#93;|\})/i', '', $value);
2038 + }
1596 2039
2040 + // decode shortcodes
2041 + public function decode_shortcodes($value = '', $quote = false) {
2042 + return preg_replace_callback(
2043 + '/(&#91;)((e2pdf-download|e2pdf-view|e2pdf-save|e2pdf-attachment|e2pdf-adobesign|e2pdf-zapier)[^&]*?)(&#93;)/',
2044 + function ($m) use ($quote) {
2045 + $shortcode = $m[2];
2046 + if ($quote !== false) {
2047 + $shortcode = str_replace('&quot;', '"', $shortcode);
2048 + }
2049 +
2050 + return '[' . $shortcode . ']';
2051 + },
2052 + $value
2053 + );
2054 + }
2055 +
2056 + // encode shortcodes
2057 + public function encode_shortcodes($value = '', $quote = false) {
2058 + return preg_replace_callback(
2059 + '/(\[)((e2pdf-download|e2pdf-view|e2pdf-save|e2pdf-attachment|e2pdf-adobesign|e2pdf-zapier)[^\]]*?)(\])/',
2060 + function ($m) use ($quote) {
2061 + $shortcode = $m[2];
2062 + if ($quote !== false) {
2063 + $shortcode = str_replace('"', '&quot;', $shortcode);
2064 + }
2065 +
2066 + return '&#91;' . $shortcode . '&#93;';
2067 + },
2068 + $value
2069 + );
2070 + }
2071 +
2072 + // divi4 load
2073 + public function divi4_load() {
2074 + if (defined('ET_BUILDER_DIR') && !$this->divi5()) {
2075 + require_once ET_BUILDER_DIR . 'class-et-builder-element.php';
2076 + require_once ET_BUILDER_DIR . 'functions.php';
2077 + require_once ET_BUILDER_DIR . 'ab-testing.php';
2078 + require_once ET_BUILDER_DIR . 'class-et-global-settings.php';
2079 + if (file_exists(ET_BUILDER_DIR . 'module/type/WithSpamProtection.php')) {
2080 + require_once ET_BUILDER_DIR . 'module/type/WithSpamProtection.php';
2081 + }
2082 + require_once ET_BUILDER_DIR . 'module/ContactForm.php';
2083 + require_once ET_BUILDER_DIR . 'module/ContactFormItem.php';
2084 + new ET_Builder_Module_Contact_Form();
2085 + new ET_Builder_Module_Contact_Form_Item();
2086 + }
2087 + }
2088 +
2089 + // is divi5
2090 + public function divi5() {
2091 + if (defined('ET_CORE_VERSION') && version_compare(ET_CORE_VERSION, '4.99', '>')) {
2092 + return true;
2093 + }
1597 2094 return false;
1598 2095 }
1599 2096
1600 - /**
1601 - * Get styles for generating Map Field function
1602 - *
1603 - * @return array - List of css files to load
1604 - */
1605 - public function styles() {
1606 - $styles = array(
1607 - plugins_url('css/extension/divi.css?v=' . time(), $this->helper->get('plugin_file_path'))
2097 + // is divi4 form
2098 + public function divi4_form($content = '') {
2099 + return (false !== strpos($content, '[et_pb_contact_form'));
2100 + }
2101 +
2102 + // is divi 5 form
2103 + public function divi5_form($content = '') {
2104 + return (false !== strpos($content, 'wp:divi/contact-form'));
2105 + }
2106 +
2107 + // pwh dcfh resolve file url
2108 + public function pwh_dcfh_resolve_file_url($file, $options) {
2109 +
2110 + if (!$file) {
2111 + return $file;
2112 + }
2113 +
2114 + if (!function_exists('ks_pac_dcfh_app') || !function_exists('path_join')) {
2115 + return $file;
2116 + }
2117 +
2118 + $contact_form_id = $options['uniqueId'];
2119 + $timestamp = strtotime($options['timestamp']);
2120 + $saveFilesToMedia = $options['saveFilesToMedia'] === 'on' ? true : false;
2121 +
2122 + $upload_dir = path_join(
2123 + 'pwh-dcfh-uploads',
2124 + 'forms/' . $contact_form_id . '/'
1608 2125 );
1609 - return $styles;
2126 + $upload_path = ks_pac_dcfh_app()->file_utils()::get_wp_upload_dir(
2127 + $upload_dir,
2128 + 'basedir'
2129 + );
2130 + $upload_url = ks_pac_dcfh_app()->file_utils()::get_wp_upload_dir(
2131 + $upload_dir,
2132 + 'baseurl'
2133 + );
2134 + if ($saveFilesToMedia) {
2135 + $wp_upload_dir = wp_upload_dir();
2136 + $upload_path = $wp_upload_dir['basedir'];
2137 + $upload_url = $wp_upload_dir['baseurl'];
2138 + }
2139 + $dates = [
2140 + date('Y/m', $timestamp),
2141 + date('Y/m', strtotime('-1 month', $timestamp)),
2142 + date('Y/m', strtotime('+1 month', $timestamp)),
2143 + ];
2144 +
2145 + foreach ($dates as $date) {
2146 + $date_path = path_join(
2147 + $upload_path,
2148 + $date
2149 + );
2150 + if (@file_exists(path_join($date_path, $file))) {
2151 + $file = path_join(
2152 + trailingslashit($upload_url) . $date,
2153 + $file
2154 + );
2155 + break;
2156 + }
2157 + }
2158 + return $file;
1610 2159 }
1611 -
1612 2160 }