PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.02.02
E2Pdf – Export Pdf Tool for WordPress v1.02.02
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 1.08.00 1.08.06 All 72 releases
e2pdf / classes / extension / e2pdf-divi.php

e2pdf-divi.php in E2Pdf – Export Pdf Tool for WordPress 1.02.02, at classes/extension/e2pdf-divi.php

1,422 lines 60.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
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
11 */
12 if (!defined('ABSPATH')) {
13 die('Access denied.');
14 }
15
16 class Extension_E2pdf_Divi extends Model_E2pdf_Model {
17
18 private $options;
19
20 function __construct() {
21 parent::__construct();
22 }
23
24 /**
25 * Get info about extension
26 *
27 * @return array() - Extension key and name
28 */
29 public function info() {
30 return array(
31 'divi' => __('Divi', 'e2pdf')
32 );
33 }
34
35 /**
36 * Check if needed plugin active
37 *
38 * @return bool - Activated/Not Activated plugin
39 */
40 public function active() {
41 if (file_exists(get_template_directory() . "/et-pagebuilder/et-pagebuilder.php")) {
42 require_once(get_template_directory() . "/et-pagebuilder/et-pagebuilder.php");
43 if (defined('ET_BUILDER_THEME')) {
44 return true;
45 }
46 }
47 return false;
48 }
49
50 /**
51 * Check if export item function available
52 *
53 * @return bool - Item export available/not available
54 */
55 public function export() {
56 return false;
57 }
58
59 /**
60 * Set option
61 *
62 * @param string $attr - Key of option
63 * @param string $value - Value of option
64 *
65 * @return bool - Status of setting option
66 */
67 public function set($key, $value) {
68 if (!isset($this->options)) {
69 $this->options = new stdClass();
70 }
71
72 $this->options->$key = $value;
73 }
74
75 /**
76 * Get option by key
77 *
78 * @param string $key - Key to get assigned option value
79 *
80 * @return mixed
81 */
82 public function get($key) {
83 if (isset($this->options->$key)) {
84 $value = $this->options->$key;
85 return $value;
86 } else {
87 return false;
88 }
89 }
90
91 /**
92 * Get items to work with
93 *
94 * @return array() - List of available items
95 */
96 public function get_items() {
97 global $wpdb;
98
99 $condition = array(
100 'post_content' => array(
101 'condition' => 'LIKE',
102 'value' => '%et_pb_contact_form%',
103 'type' => '%s'
104 ),
105 'post_type' => array(
106 'condition' => '<>',
107 'value' => array(
108 'revision',
109 'et_pb_layout'
110 ),
111 'type' => '%s'
112 ),
113 );
114
115 $order_condition = array(
116 'orderby' => 'id',
117 'order' => 'desc',
118 );
119
120 $where = $this->helper->load('db')->prepare_where($condition);
121 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
122
123 $posts = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . 'posts' . $where['sql'] . $orderby . "", $where['filter']));
124
125 $forms = array();
126 foreach ($posts as $key => $post) {
127 if ($forms_labels = $this->get_forms($post->post_content)) {
128
129 foreach ($forms_labels as $form_key => $form_value) {
130 $forms[] = array(
131 'key' => $form_key,
132 'value' => $form_value
133 );
134 }
135 }
136 }
137 return $forms;
138 }
139
140 /**
141 * Parse available forms from pages
142 *
143 * @param string $content - Page content
144 *
145 * @return array() - Forms list
146 */
147 public function get_forms($content) {
148
149 $forms = array();
150 if (false !== strpos($content, 'et_pb_contact_form')) {
151 $shortcode_tags = array(
152 'et_pb_contact_form',
153 );
154
155 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
156 $tagnames = array_intersect($shortcode_tags, $matches[1]);
157
158 if (!empty($tagnames)) {
159 $pattern = get_shortcode_regex($tagnames);
160
161 preg_match_all("/$pattern/", $content, $shortcodes);
162 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
172 preg_match_all('/admin_label="(.*?)"/', $shortcode[3], $labels);
173 if (isset($labels['1'])) {
174 foreach ($labels['1'] as $label) {
175 $forms[$label] = $label;
176 }
177 }
178 }
179 }
180 }
181
182 return $forms;
183 }
184
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 get_datasets($item = false, $name = false) {
194
195 global $wpdb;
196
197 $datasets = array();
198
199 if ($item) {
200 $condition = array(
201 'extension' => array(
202 'condition' => '=',
203 'value' => 'divi',
204 'type' => '%s'
205 ),
206 'item' => array(
207 'condition' => '=',
208 'value' => $item,
209 'type' => '%s'
210 ),
211 );
212
213 $order_condition = array(
214 'orderby' => 'ID',
215 'order' => 'desc',
216 );
217
218 $where = $this->helper->load('db')->prepare_where($condition);
219 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
220
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;
230 }
231 $datasets[] = array(
232 'key' => $dataset->ID,
233 'value' => $dataset_title
234 );
235 }
236 }
237 }
238
239 return $datasets;
240 }
241
242 /**
243 * Get dataset
244 *
245 * @param int $dataset - Dataset ID
246 *
247 * @return object - Dataset
248 */
249 public function get_dataset($dataset = false) {
250
251 $dataset = (int) $dataset;
252
253 if (!$dataset) {
254 return;
255 }
256
257 $data = new stdClass();
258 $data->url = false;
259
260 return $data;
261 }
262
263 /**
264 * Get item
265 *
266 * @param string $item - Item
267 *
268 * @return object - Item
269 */
270 public function get_item($item = false) {
271
272 $form = new stdClass();
273
274 if ($item) {
275 $form->name = $item;
276 $post = $this->get_post($item);
277 $form->url = isset($post->ID) ? $this->helper->get_url(array('post' => $post->ID, 'action' => 'edit'), 'post.php?') : 'javascript:void(0);';
278 } else {
279 $form->name = '';
280 $form->url = 'javascript:void(0);';
281 }
282 return $form;
283 }
284
285 /**
286 * Get post
287 *
288 * @param string $item_id - Form label
289 *
290 * @return object - Post
291 */
292 public function get_post($item_id = false) {
293 global $wpdb;
294
295 $item_post = false;
296
297 $condition = array(
298 'post_content' => array(
299 'condition' => 'LIKE',
300 'value' => '%admin_label="' . $item_id . '"%',
301 'type' => '%s'
302 ),
303 'post_type' => array(
304 'condition' => '<>',
305 'value' => array(
306 'revision',
307 'et_pb_layout'
308 ),
309 'type' => '%s'
310 ),
311 );
312
313 $order_condition = array(
314 'orderby' => 'id',
315 'order' => 'desc',
316 );
317
318 $where = $this->helper->load('db')->prepare_where($condition);
319 $orderby = $this->helper->load('db')->prepare_orderby($order_condition);
320
321 $posts = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . 'posts' . $where['sql'] . $orderby . "", $where['filter']));
322 foreach ($posts as $key => $post) {
323 if ($forms_labels = $this->get_forms($post->post_content)) {
324 if (in_array($item_id, $forms_labels)) {
325 $item_post = $post;
326 break;
327 }
328 }
329 }
330 return $item_post;
331 }
332
333 /**
334 * Render value according to content
335 *
336 * @param string $value - Content
337 * @param string $type - Type of rendering value
338 * @param array $field - Field details
339 *
340 * @return string - Fully rendered value
341 */
342 public function render($value, $type = false, $field = array()) {
343
344 $value = $this->render_shortcodes($value, $type, $field);
345 $value = $this->strip_shortcodes($value);
346
347 if ($type === 'value' && isset($field['type']) && $field['type'] === 'e2pdf-checkbox' && isset($field['properties']['option'])) {
348 $option = $this->render($field['properties']['option']);
349 $options = explode(', ', $value);
350 $option_options = explode(', ', $option);
351 if (is_array($options) && is_array($option_options) && !array_diff($option_options, $options)) {
352 return $option;
353 } else {
354 return "";
355 }
356 }
357
358 if ($type === 'value') {
359 $value = str_replace("&#91;", "[", $value);
360 }
361
362 return $value;
363 }
364
365 /**
366 * Render shortcodes which available in this extension
367 *
368 * @param string $value - Content
369 * @param string $type - Type of rendering value
370 * @param array $field - Field details
371 *
372 * @return string - Value with rendered shortcodes
373 */
374 public function render_shortcodes($value, $type = false, $field = array()) {
375 global $wpdb;
376
377 $dataset_id = $this->get('dataset');
378 $item_id = $this->get('item');
379
380 $condition = array(
381 'ID' => array(
382 'condition' => '=',
383 'value' => $dataset_id,
384 'type' => '%d'
385 ),
386 'item' => array(
387 'condition' => '=',
388 'value' => $item_id,
389 'type' => '%s'
390 ),
391 'extension' => array(
392 'condition' => '=',
393 'value' => 'divi',
394 'type' => '%s'
395 ),
396 );
397
398 $where = $this->helper->load('db')->prepare_where($condition);
399
400 $dataset = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . 'e2pdf_datasets' . $where['sql'] . "", $where['filter']));
401
402 $processed_fields_values = array();
403 if ($dataset) {
404 $post = unserialize($dataset->entry);
405 if ($post && is_array($post)) {
406
407 $et_pb_contact_form_num = 0;
408
409 $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] : '';
410 $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;
411 $processed_fields_values = array();
412
413 if ('' !== $current_form_fields) {
414 $fields_data_json = str_replace('\\', '', $current_form_fields);
415 $fields_data_array = json_decode($fields_data_json, true);
416
417 if (!empty($fields_data_array)) {
418 foreach ($fields_data_array as $index => $field_value) {
419 $processed_fields_values[$field_value['original_id']]['value'] = isset($post[$field_value['field_id']]) ? $post[$field_value['field_id']] : '';
420 $processed_fields_values[$field_value['original_id']]['label'] = $field_value['field_label'];
421 }
422 }
423 }
424 }
425
426 $replace = array();
427 foreach ($processed_fields_values as $field_key => $field_value) {
428 $replace["%%" . $field_key . "%%"] = wp_strip_all_tags($field_value['value']);
429 }
430
431 if (false !== strpos($value, '[')) {
432
433 $shortcode_tags = array(
434 'e2pdf-format-number',
435 'e2pdf-format-date',
436 'e2pdf-format-output',
437 );
438 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $value, $matches);
439 $tagnames = array_intersect($shortcode_tags, $matches[1]);
440
441 if (!empty($tagnames)) {
442
443 $pattern = get_shortcode_regex($tagnames);
444 preg_match_all("/$pattern/", $value, $shortcodes);
445 foreach ($shortcodes[0] as $key => $shortcode_value) {
446 $shortcode = array();
447 $shortcode[1] = $shortcodes[1][$key];
448 $shortcode[2] = $shortcodes[2][$key];
449 $shortcode[3] = $shortcodes[3][$key];
450 $shortcode[4] = $shortcodes[4][$key];
451 $shortcode[5] = $shortcodes[5][$key];
452 $shortcode[6] = $shortcodes[6][$key];
453
454 if (isset($shortcode['5'])) {
455 $shortcode['5'] = $this->helper->load('convert')->stritr($shortcode['5'], $replace);
456 $sub_value = $this->strip_shortcodes($shortcode['5']);
457 $value = str_replace($shortcode_value, "[" . $shortcode['2'] . $shortcode['3'] . "]" . $sub_value . "[/" . $shortcode['2'] . "]", $value);
458 }
459 }
460 }
461 }
462
463 $value = do_shortcode($value);
464
465 if ($dataset) {
466 $value = $this->helper->load('convert')->stritr($value, $replace);
467 }
468
469 if (isset($field['type']) && ($field['type'] === 'e2pdf-image' || $field['type'] === 'e2pdf-signature')) {
470 $esig = isset($field['properties']['esig']) && $field['properties']['esig'] ? true : false;
471 if ($esig) {
472 //process e-signature
473 $value = "";
474 } else {
475 if (!$this->helper->load('image')->get_image($value)) {
476 $value = $this->strip_shortcodes($value);
477 if (
478 $value &&
479 trim($value) != "" &&
480 extension_loaded('gd') &&
481 function_exists('imagettftext')
482 ) {
483 if (isset($field['properties']['text_color']) && $field['properties']['text_color']) {
484 $penColour = $this->helper->load('convert')->to_hex_color($field['properties']['text_color']);
485 } else {
486 $penColour = array(0x14, 0x53, 0x94);
487 }
488
489 $default_options = array(
490 'imageSize' => array(isset($field['width']) ? $field['width'] : '400', isset($field['height']) ? $field['height'] : '150'),
491 'bgColour' => 'transparent',
492 'penColour' => $penColour
493 );
494
495 $options = array();
496 $options = array_merge($default_options, $options);
497
498 $model_e2pdf_font = new Model_E2pdf_Font();
499
500 $font = false;
501 if (isset($field['properties']['text_font']) && $field['properties']['text_font']) {
502 $font = $model_e2pdf_font->get_font_path($field['properties']['text_font']);
503 }
504 if (!$font) {
505 $font = $model_e2pdf_font->get_font_path('Noto Sans');
506 }
507
508 $size = 150;
509 if (isset($field['properties']['text_font_size']) && $field['properties']['text_font_size']) {
510 $size = $field['properties']['text_font_size'];
511 }
512
513 $model_e2pdf_signature = new Model_E2pdf_Signature();
514 $value = $model_e2pdf_signature->ttf_signature($value, $size, $font, $options);
515 } else {
516 $value = "";
517 }
518 }
519 }
520 } else {
521 $value = str_replace("[", '&#91;', $value);
522 }
523 }
524
525 return $value;
526 }
527
528 /**
529 * Strip unused shortcodes
530 *
531 * @param string $value - Content
532 *
533 * @return string - Value with removed unused shortcodes
534 */
535 public function strip_shortcodes($value) {
536 $value = preg_replace('~(?:\[/?)[^/\]]+/?\]~s', "", $value);
537 $value = preg_replace('~(?:%%/?)[^/%%]+/?%%~s', "", $value);
538 return $value;
539 }
540
541 public function auto() {
542
543 $response = array();
544 $elements = array();
545
546 if ($this->get('item')) {
547 $post = $this->get_post($this->get('item'));
548 if ($post && isset($post->post_content)) {
549
550 $content = $post->post_content;
551
552 if (false !== strpos($content, '[')) {
553 $shortcode_tags = array(
554 'et_pb_contact_form',
555 );
556
557 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
558 $tagnames = array_intersect($shortcode_tags, $matches[1]);
559
560 if (!empty($tagnames)) {
561 $pattern = get_shortcode_regex($tagnames);
562
563 preg_match_all("/$pattern/", $content, $shortcodes);
564
565 foreach ($shortcodes[0] as $key => $shortcode_value) {
566
567 $shortcode = array();
568 $shortcode[1] = $shortcodes[1][$key];
569 $shortcode[2] = $shortcodes[2][$key];
570 $shortcode[3] = $shortcodes[3][$key];
571 $shortcode[4] = $shortcodes[4][$key];
572 $shortcode[5] = $shortcodes[5][$key];
573 $shortcode[6] = $shortcodes[6][$key];
574
575 $atts = shortcode_parse_atts($shortcode[3]);
576 if (isset($atts['admin_label']) && $atts['admin_label'] == $this->get('item')) {
577
578 require_once(ET_BUILDER_DIR . 'class-et-builder-element.php');
579 require_once(ET_BUILDER_DIR . 'functions.php');
580 require_once(ET_BUILDER_DIR . 'ab-testing.php');
581 require_once(ET_BUILDER_DIR . 'class-et-global-settings.php');
582 require_once(ET_BUILDER_DIR . 'module/ContactForm.php');
583 require_once(ET_BUILDER_DIR . 'module/ContactFormItem.php');
584
585 new ET_Builder_Module_Contact_Form();
586 new ET_Builder_Module_Contact_Form_Item();
587
588
589 $source = do_shortcode($shortcode_value);
590
591 libxml_use_internal_errors(true);
592 $dom = new DOMDocument;
593 $html = $dom->loadHTML($source);
594 libxml_clear_errors();
595
596 if ($html) {
597 $xpath = new DomXPath($dom);
598 $blocks = $xpath->query("//*[contains(@class, 'et_pb_contact_field')]");
599 foreach ($blocks as $element) {
600
601 $data_type = $element->attributes->getNamedItem("data-type")->nodeValue;
602
603 if ($data_type == 'radio') {
604
605 $label = $xpath->query(".//label", $element)->item(0);
606 $check_handler = $xpath->query(".//input[contains(@class, 'et_pb_checkbox_handle')]", $element)->item(0);
607
608 $name = "";
609 if ($check_handler) {
610 $name = "%%" . $check_handler->attributes->getNamedItem("data-original_id")->nodeValue . "%%";
611 }
612
613 $elements[] = $this->auto_field($element, 'e2pdf-html', array(
614 'top' => '20',
615 'left' => '20',
616 'right' => '20',
617 'block' => true,
618 'properties' => array(
619 'value' => $label->nodeValue,
620 )
621 ));
622
623 $top = 0;
624 $fields = $xpath->query("//*[contains(@class, 'et_pb_contact_field_radio')]", $element);
625
626 foreach ($fields as $field) {
627 $radio_label = $xpath->query(".//label", $field)->item(0);
628 $radio = $xpath->query(".//input[@type='radio']", $field)->item(0);
629
630 $elements[] = $this->auto_field($field, 'e2pdf-radio', array(
631 'top' => $top,
632 'properties' => array(
633 'width' => '15',
634 'height' => '15',
635 'value' => "%%" . $radio->attributes->getNamedItem("data-original_id")->nodeValue . "%%",
636 'option' => $radio->attributes->getNamedItem("value")->nodeValue,
637 'group' => $radio->attributes->getNamedItem("data-original_id")->nodeValue,
638 )
639 ));
640 $elements[] = $this->auto_field($radio, 'e2pdf-html', array(
641 'top' => $top,
642 'left' => '20',
643 'properties' => array(
644 'value' => $radio_label->nodeValue
645 )
646 ));
647
648 $top = $top + 20;
649 }
650 } elseif ($data_type == 'checkbox') {
651
652 $label = $xpath->query(".//label", $element)->item(0);
653 $check_handler = $xpath->query(".//input[contains(@class, 'et_pb_checkbox_handle')]", $element)->item(0);
654
655 $name = "";
656 if ($check_handler) {
657 $name = "%%" . $check_handler->attributes->getNamedItem("data-original_id")->nodeValue . "%%";
658 }
659
660
661 $elements[] = $this->auto_field($element, 'e2pdf-html', array(
662 'top' => '20',
663 'left' => '20',
664 'right' => '20',
665 'block' => true,
666 'properties' => array(
667 'value' => $label->nodeValue,
668 )
669 ));
670
671 $top = 0;
672 $fields = $xpath->query("//*[contains(@class, 'et_pb_contact_field_checkbox')]", $element);
673
674 foreach ($fields as $field) {
675 $checkbox_label = $xpath->query(".//label", $field)->item(0);
676 $checkbox = $xpath->query(".//input[@type='checkbox']", $field)->item(0);
677
678
679 $elements[] = $this->auto_field($field, 'e2pdf-checkbox', array(
680 'top' => $top,
681 'properties' => array(
682 'width' => '15',
683 'height' => '15',
684 'value' => $name,
685 'option' => $checkbox->attributes->getNamedItem("value")->nodeValue
686 )
687 ));
688 $elements[] = $this->auto_field($checkbox, 'e2pdf-html', array(
689 'top' => $top,
690 'left' => '20',
691 'properties' => array(
692 'value' => $checkbox_label->nodeValue
693 )
694 ));
695
696 $top = $top + 20;
697 }
698 } else {
699
700 $label = $xpath->query(".//label", $element)->item(0);
701 $input_text = $xpath->query(".//input[@type='text']", $element)->item(0);
702 $select = $xpath->query(".//select", $element)->item(0);
703 $textarea = $xpath->query(".//textarea", $element)->item(0);
704
705 if ($label && ($input_text || $select || $textarea)) {
706 $elements[] = $this->auto_field($element, 'e2pdf-html', array(
707 'top' => '20',
708 'left' => '20',
709 'right' => '20',
710 'block' => true,
711 'properties' => array(
712 'value' => $label->nodeValue,
713 )
714 ));
715 }
716
717 if ($input_text) {
718 $elements[] = $this->auto_field($input_text, 'e2pdf-input', array(
719 'properties' => array(
720 'value' => "%%{$input_text->attributes->getNamedItem("data-original_id")->nodeValue}%%",
721 )
722 ));
723 } elseif ($select) {
724 $options_tmp = array();
725 $options = $xpath->query(".//option", $select);
726 foreach ($options as $option) {
727 $options_tmp[] = $option->attributes->getNamedItem("value")->nodeValue;
728 }
729
730 $elements[] = $this->auto_field($select, 'e2pdf-select', array(
731 'properties' => array(
732 'options' => implode("\n", $options_tmp),
733 'value' => "%%{$select->attributes->getNamedItem("data-original_id")->nodeValue}%%",
734 )
735 ));
736 } elseif ($textarea) {
737 $elements[] = $this->auto_field($textarea, 'e2pdf-textarea', array(
738 'properties' => array(
739 'value' => "%%{$textarea->attributes->getNamedItem("data-original_id")->nodeValue}%%",
740 )
741 ));
742 }
743 }
744 }
745 }
746 }
747 }
748 }
749 }
750 }
751 }
752
753 $response['page'] = array(
754 'bottom' => '20',
755 'top' => '20',
756 );
757 $response['elements'] = $elements;
758
759 return $response;
760 }
761
762 /**
763 * Convert Field name to Value
764 * @since 0.01.34
765 *
766 * @param string $name - Field name
767 *
768 * @return bool|string - Converted value or false
769 */
770 public function auto_map($name = false) {
771 $item = $this->get('item');
772 if ($item) {
773 $post = $this->get_post($item);
774 if ($post && isset($post->post_content)) {
775 $content = $post->post_content;
776
777 if (false !== strpos($content, '[')) {
778 $shortcode_tags = array(
779 'et_pb_contact_form',
780 );
781
782 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
783 $tagnames = array_intersect($shortcode_tags, $matches[1]);
784
785 if (!empty($tagnames)) {
786 $pattern = get_shortcode_regex($tagnames);
787
788 preg_match_all("/$pattern/", $content, $shortcodes);
789
790 foreach ($shortcodes[0] as $key => $shortcode_value) {
791
792 $shortcode = array();
793 $shortcode[1] = $shortcodes[1][$key];
794 $shortcode[2] = $shortcodes[2][$key];
795 $shortcode[3] = $shortcodes[3][$key];
796 $shortcode[4] = $shortcodes[4][$key];
797 $shortcode[5] = $shortcodes[5][$key];
798 $shortcode[6] = $shortcodes[6][$key];
799
800
801 $atts = shortcode_parse_atts($shortcode[3]);
802 if (isset($atts['admin_label']) && $atts['admin_label'] == $this->get('item')) {
803
804 $field_content = $shortcode_value;
805 $field_shortcode_tags = array(
806 'et_pb_contact_field',
807 );
808
809 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $field_content, $field_matches);
810 $field_tagnames = array_intersect($field_shortcode_tags, $field_matches[1]);
811
812 if (!empty($field_tagnames)) {
813 $field_pattern = get_shortcode_regex($field_tagnames);
814
815 preg_match_all("/$field_pattern/", $field_content, $field_shortcodes);
816
817 foreach ($field_shortcodes[0] as $field_key => $field_shortcode_value) {
818 $field_shortcode = array();
819 $field_shortcode[3] = $field_shortcodes[3][$field_key];
820 $field_atts = shortcode_parse_atts($field_shortcode[3]);
821 if (isset($field_atts['field_title']) && isset($field_atts['field_id'])) {
822 if ($field_atts['field_title'] == $name || $field_atts['field_id'] == $name) {
823 return "%%" . strtolower($field_atts['field_id']) . "%%";
824 }
825 }
826 }
827 }
828 }
829 }
830 }
831 }
832 }
833 }
834
835 return false;
836 }
837
838 /**
839 * Generate field for Auto PDF
840 *
841 * @param object $field - Formidable field object
842 * @param string $type - Field type
843 * @param array $options - Field additional options
844 *
845 * @return array - Prepared auto field
846 */
847 public function auto_field($field = false, $type = false, $options = array()) {
848
849 if (!$field || !$type) {
850 return false;
851 }
852
853 $element = array(
854 'type' => $type,
855 'properties' => isset($options['properties']) ? $options['properties'] : array(),
856 );
857
858 $element['top'] = isset($options['top']) ? $options['top'] : '0';
859 $element['left'] = isset($options['left']) ? $options['left'] : '0';
860 $element['right'] = isset($options['right']) ? $options['right'] : '0';
861 $element['block'] = isset($options['block']) ? $options['block'] : false;
862
863 $classes = array();
864 if (isset($field->attributes->getNamedItem("class")->nodeValue)) {
865 $classes = explode(" ", $field->attributes->getNamedItem("class")->nodeValue);
866 }
867
868 $float_classes = array(
869 'et_pb_contact_field_half',
870 );
871 $array_intersect = array_intersect($classes, $float_classes);
872
873 if (!empty($array_intersect)) {
874 $element['float'] = true;
875 };
876
877 $primary_class = false;
878 if (!empty($array_intersect)) {
879 $primary_class = end($array_intersect);
880 }
881
882 if ($element['block']) {
883 switch ($primary_class) {
884 case 'et_pb_contact_field_half':
885 $element['width'] = '50%';
886 break;
887 default:
888 break;
889 }
890 }
891
892 return $element;
893 }
894
895 /**
896 * Verify if form and dataset exists
897 *
898 * @return bool - Exists/Not exists
899 */
900 public function verify() {
901 global $wpdb;
902 $item_id = $this->get('item');
903 $dataset_id = $this->get('dataset');
904
905 if ($item_id && $dataset_id) {
906 $condition = array(
907 'ID' => array(
908 'condition' => '=',
909 'value' => $dataset_id,
910 'type' => '%d'
911 ),
912 'extension' => array(
913 'condition' => '=',
914 'value' => 'divi',
915 'type' => '%s'
916 ),
917 'item' => array(
918 'condition' => '=',
919 'value' => $item_id,
920 'type' => '%s'
921 ),
922 );
923
924 $where = $this->helper->load('db')->prepare_where($condition);
925 $dataset = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . 'e2pdf_datasets' . $where['sql'] . "", $where['filter']));
926
927 if ($dataset) {
928 return true;
929 }
930 }
931 return false;
932 }
933
934 /**
935 * Visual Mapper for Mapping field
936 *
937 * @return bool|string - Prepared form or false
938 */
939 public function visual_mapper() {
940 if ($this->get('item')) {
941 $post = $this->get_post($this->get('item'));
942 if ($post && isset($post->post_content)) {
943
944 $content = $post->post_content;
945
946 if (false !== strpos($content, '[')) {
947 $shortcode_tags = array(
948 'et_pb_contact_form',
949 );
950
951 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches);
952 $tagnames = array_intersect($shortcode_tags, $matches[1]);
953
954
955
956 if (!empty($tagnames)) {
957 $pattern = get_shortcode_regex($tagnames);
958
959 preg_match_all("/$pattern/", $content, $shortcodes);
960
961 foreach ($shortcodes[0] as $key => $shortcode_value) {
962
963 $shortcode = array();
964 $shortcode[1] = $shortcodes[1][$key];
965 $shortcode[2] = $shortcodes[2][$key];
966 $shortcode[3] = $shortcodes[3][$key];
967 $shortcode[4] = $shortcodes[4][$key];
968 $shortcode[5] = $shortcodes[5][$key];
969 $shortcode[6] = $shortcodes[6][$key];
970
971
972 $atts = shortcode_parse_atts($shortcode[3]);
973 if (isset($atts['admin_label']) && $atts['admin_label'] == $this->get('item')) {
974
975 require_once(ET_BUILDER_DIR . 'class-et-builder-element.php');
976 require_once(ET_BUILDER_DIR . 'functions.php');
977 require_once(ET_BUILDER_DIR . 'ab-testing.php');
978 require_once(ET_BUILDER_DIR . 'class-et-global-settings.php');
979 require_once(ET_BUILDER_DIR . 'module/ContactForm.php');
980 require_once(ET_BUILDER_DIR . 'module/ContactFormItem.php');
981
982 new ET_Builder_Module_Contact_Form();
983 new ET_Builder_Module_Contact_Form_Item();
984
985 $source = do_shortcode($shortcode_value);
986
987 libxml_use_internal_errors(true);
988 $dom = new DOMDocument;
989 $html = $dom->loadHTML($source);
990 libxml_clear_errors();
991
992 if (!$html) {
993 return __('Form could not be parsed due incorrect HTML', 'e2pdf');
994 } else {
995
996 $xpath = new DomXPath($dom);
997 // Replace names
998 $fields = $xpath->query("//*[contains(@name, 'et_pb_contact_')]");
999 foreach ($fields as $element) {
1000 $element->attributes->getNamedItem("name")->nodeValue = "%%" . $element->attributes->getNamedItem("data-original_id")->nodeValue . "%%";
1001 }
1002
1003 $checkboxes = $xpath->query("//*[contains(@class, 'et_pb_contact_field') and @data-type='checkbox']");
1004 foreach ($checkboxes as $element) {
1005
1006 $check_handler = $xpath->query(".//input[contains(@class, 'et_pb_checkbox_handle')]", $element)->item(0);
1007
1008 $name = "";
1009 if ($check_handler) {
1010 $name = "%%" . $check_handler->attributes->getNamedItem("data-original_id")->nodeValue . "%%";
1011 }
1012
1013 $checks = $xpath->query(".//input[@type='checkbox']", $element);
1014 foreach ($checks as $check) {
1015 $attr = $dom->createAttribute('name');
1016 $attr->value = $name;
1017 $check->appendChild($attr);
1018 }
1019 }
1020
1021 $remove_by_class = array(
1022 'et_pb_contact_submit',
1023 'et_pb_contactform_validate_field'
1024 );
1025 foreach ($remove_by_class as $key => $class) {
1026 $elements = $xpath->query("//*[contains(@class, '{$class}')]");
1027 foreach ($elements as $element) {
1028 $element->parentNode->removeChild($element);
1029 }
1030 }
1031
1032 $remove_parent_by_class = array(
1033 'et_pb_contact_captcha_question'
1034 );
1035 foreach ($remove_parent_by_class as $key => $class) {
1036 $elements = $xpath->query("//*[contains(@class, '{$class}')]/parent::*");
1037 foreach ($elements as $element) {
1038 $element->parentNode->removeChild($element);
1039 }
1040 }
1041
1042 if ($xpath->query("//form")->item(0)) {
1043 $autocomplete = $dom->createAttribute('autocomplete');
1044 $autocomplete->value = 'off';
1045 $xpath->query("//form")->item(0)->appendChild($autocomplete);
1046 }
1047 }
1048
1049 return $dom->saveHTML();
1050 }
1051 }
1052 }
1053 }
1054 }
1055 }
1056
1057 return false;
1058 }
1059
1060 /**
1061 * Overwrite shortcodes
1062 */
1063 public function action_overwrite_shortcodes() {
1064 remove_shortcode('et_pb_contact_form');
1065 add_shortcode('et_pb_contact_form', array($this, 'shortcode_et_pb_contact_form'));
1066 }
1067
1068 public function filter_wp_mail($args) {
1069 if (isset($args['message'])) {
1070 if (false !== strpos($args['message'], '[')) {
1071 $shortcode_tags = array(
1072 'e2pdf-download',
1073 'e2pdf-save',
1074 'e2pdf-attachment',
1075 'e2pdf-adobesign'
1076 );
1077
1078 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $args['message'], $matches);
1079 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1080
1081 if (!empty($tagnames)) {
1082 $pattern = get_shortcode_regex($tagnames);
1083
1084 preg_match_all("/$pattern/", $args['message'], $shortcodes);
1085
1086 foreach ($shortcodes[0] as $key => $shortcode_value) {
1087
1088 $shortcode = array();
1089 $shortcode[1] = $shortcodes[1][$key];
1090 $shortcode[2] = $shortcodes[2][$key];
1091 $shortcode[3] = $shortcodes[3][$key];
1092 $shortcode[4] = $shortcodes[4][$key];
1093 $shortcode[5] = $shortcodes[5][$key];
1094 $shortcode[6] = $shortcodes[6][$key];
1095
1096 $atts = shortcode_parse_atts($shortcode[3]);
1097
1098 if ($shortcode[2] === 'e2pdf-attachment' || $shortcode[2] === 'e2pdf-save' || $shortcode[2] === 'e2pdf-adobesign') {
1099 $shortcode[3] .= " apply=\"true\"";
1100 }
1101
1102 if ($shortcode[2] === 'e2pdf-attachment') {
1103 $file = do_shortcode_tag($shortcode);
1104 if ($file) {
1105 $this->helper->add('divi_attachments', $file);
1106 $args['attachments'][] = $file;
1107 }
1108 $args['message'] = str_replace($shortcode_value, '', $args['message']);
1109 } else {
1110 if (is_array($args['headers']) && !in_array('Content-Type: text/html; charset=UTF-8', $args['headers'])) {
1111 $args['headers'][] = 'Content-Type: text/html; charset=UTF-8';
1112 }
1113 $args['message'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $args['message']);
1114 $args['message'] = str_replace("\r\n", "<br/>", $args['message']);
1115 }
1116 }
1117 }
1118 }
1119 }
1120
1121 $wp_mail = array(
1122 'to' => $args['to'],
1123 'subject' => $args['subject'],
1124 'message' => $args['message'],
1125 'headers' => $args['headers'],
1126 'attachments' => $args['attachments'],
1127 );
1128
1129 return $wp_mail;
1130 }
1131
1132 /**
1133 * Load actions for this extension
1134 */
1135 public function load_actions() {
1136 add_action('wp', array($this, 'action_overwrite_shortcodes'), 99);
1137 }
1138
1139 public function shortcode_et_pb_contact_form($shortcode_atts, $content = null, $function_name, $parent_address = '', $global_parent = '', $global_parent_type = '') {
1140 global $wpdb;
1141
1142 if (class_exists('ET_Builder_Module_Contact_Form')) {
1143
1144 $dataset_id = false;
1145
1146 $et_pb_contact = new ET_Builder_Module_Contact_Form();
1147
1148 if (isset($_POST['_wpnonce-et-pb-contact-form-submitted'])) {
1149
1150 $et_pb_contact_form_num = 0;
1151
1152 $nonce_result = isset($_POST['_wpnonce-et-pb-contact-form-submitted']) && wp_verify_nonce($_POST['_wpnonce-et-pb-contact-form-submitted'], 'et-pb-contact-form-submit') ? true : false;
1153
1154 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])) {
1155 if ('' !== $current_form_fields) {
1156 $fields_data_json = str_replace('\\', '', $current_form_fields);
1157 $fields_data_array = json_decode($fields_data_json, true);
1158
1159 // check whether captcha field is not empty
1160 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]) )) {
1161 $et_error_message .= sprintf('<p class="et_pb_contact_error_text">%1$s</p>', esc_html__('Make sure you entered the captcha.', 'et_builder'));
1162 $et_contact_error = true;
1163 }
1164
1165 // check all fields on current form and generate error message if needed
1166 if (!empty($fields_data_array)) {
1167 foreach ($fields_data_array as $index => $value) {
1168 // check all the required fields, generate error message if required field is empty
1169 if ('required' === $value['required_mark'] && empty($_POST[$value['field_id']])) {
1170 $et_error_message .= sprintf('<p class="et_pb_contact_error_text">%1$s</p>', esc_html__('Make sure you fill in all required fields.', 'et_builder'));
1171 $et_contact_error = true;
1172 continue;
1173 }
1174
1175 // additional check for email field
1176 if ('email' === $value['field_type'] && 'required' === $value['required_mark'] && !empty($_POST[$value['field_id']])) {
1177 $contact_email = sanitize_email($_POST[$value['field_id']]);
1178 if (!is_email($contact_email)) {
1179 $et_error_message .= sprintf('<p class="et_pb_contact_error_text">%1$s</p>', esc_html__('Invalid Email.', 'et_builder'));
1180 $et_contact_error = true;
1181 }
1182 }
1183
1184 // prepare the array of processed field values in convenient format
1185 if (false === $et_contact_error) {
1186 $processed_fields_values[$value['original_id']]['value'] = isset($_POST[$value['field_id']]) ? $_POST[$value['field_id']] : '';
1187 $processed_fields_values[$value['original_id']]['label'] = $value['field_label'];
1188 }
1189 }
1190 }
1191 } else {
1192 $et_error_message .= sprintf('<p class="et_pb_contact_error_text">%1$s</p>', esc_html__('Make sure you fill in all required fields.', 'et_builder'));
1193 $et_contact_error = true;
1194 }
1195 } else {
1196 if (false === $nonce_result && isset($_POST['et_pb_contactform_submit_' . $et_pb_contact_form_num]) && empty($_POST['et_pb_contactform_validate_' . $et_pb_contact_form_num])) {
1197 $et_error_message .= sprintf('<p class="et_pb_contact_error_text">%1$s</p>', esc_html__('Please refresh the page and try again.', 'et_builder'));
1198 }
1199 $et_contact_error = true;
1200 }
1201
1202 if (!$et_contact_error && $nonce_result) {
1203 if (isset($shortcode_atts['success_message'])) {
1204 $shortcode_atts['success_message'] = str_replace(array('%91', '%93', '%22'), array('[', ']', '"'), $shortcode_atts['success_message']);
1205 if (false !== strpos($shortcode_atts['success_message'], '[')) {
1206
1207 $shortcode_tags = array(
1208 'e2pdf-download',
1209 'e2pdf-save',
1210 'e2pdf-view',
1211 'e2pdf-adobesign'
1212 );
1213
1214 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $shortcode_atts['success_message'], $matches);
1215 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1216
1217 if (!empty($tagnames)) {
1218 $pattern = get_shortcode_regex($tagnames);
1219
1220 preg_match_all("/$pattern/", $shortcode_atts['success_message'], $shortcodes);
1221
1222 foreach ($shortcodes[0] as $key => $shortcode_value) {
1223
1224 $item = false;
1225
1226 $shortcode = array();
1227 $shortcode[1] = $shortcodes[1][$key];
1228 $shortcode[2] = $shortcodes[2][$key];
1229 $shortcode[3] = $shortcodes[3][$key];
1230 $shortcode[4] = $shortcodes[4][$key];
1231 $shortcode[5] = $shortcodes[5][$key];
1232 $shortcode[6] = $shortcodes[6][$key];
1233
1234 $atts = shortcode_parse_atts($shortcode[3]);
1235
1236 if (!array_key_exists('dataset', $atts) && array_key_exists('id', $atts)) {
1237 $template = new Model_E2pdf_Template();
1238 $template->load($atts['id']);
1239 if ($template->get('extension') === 'divi') {
1240 $item = $template->get('item');
1241
1242 if (!$dataset_id) {
1243 $serialized = serialize($_POST);
1244 $dataset = array(
1245 'extension' => 'divi',
1246 'item' => $item,
1247 'entry' => $serialized
1248 );
1249 $wpdb->insert($wpdb->prefix . 'e2pdf_datasets', $dataset);
1250 $dataset_id = $wpdb->insert_id;
1251 }
1252 $atts['dataset'] = $dataset_id;
1253 $shortcode[3] .= " dataset=\"{$dataset_id}\" ";
1254 }
1255 }
1256
1257 if ($shortcode[2] === 'e2pdf-attachment' || $shortcode[2] === 'e2pdf-save') {
1258 $shortcode[3] .= " apply=\"true\"";
1259 }
1260
1261 $shortcode_atts['success_message'] = str_replace($shortcode_value, do_shortcode_tag($shortcode), $shortcode_atts['success_message']);
1262 }
1263 }
1264 }
1265 }
1266
1267
1268 if (isset($shortcode_atts['custom_message'])) {
1269
1270 $shortcode_atts['custom_message'] = str_replace(array('%91', '%93', '%22'), array('[', ']', '"'), $shortcode_atts['custom_message']);
1271
1272 if (false !== strpos($shortcode_atts['custom_message'], '[')) {
1273 $shortcode_tags = array(
1274 'e2pdf-download',
1275 'e2pdf-save',
1276 'e2pdf-attachment',
1277 'e2pdf-adobesign'
1278 );
1279
1280 preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $shortcode_atts['custom_message'], $matches);
1281 $tagnames = array_intersect($shortcode_tags, $matches[1]);
1282
1283 if (!empty($tagnames)) {
1284 $pattern = get_shortcode_regex($tagnames);
1285
1286 preg_match_all("/$pattern/", $shortcode_atts['custom_message'], $shortcodes);
1287
1288 add_filter('wp_mail', array($this, 'filter_wp_mail'), 10, 2);
1289
1290 foreach ($shortcodes[0] as $key => $shortcode_value) {
1291 $item = false;
1292
1293 $shortcode = array();
1294 $shortcode[1] = $shortcodes[1][$key];
1295 $shortcode[2] = $shortcodes[2][$key];
1296 $shortcode[3] = $shortcodes[3][$key];
1297 $shortcode[4] = $shortcodes[4][$key];
1298 $shortcode[5] = $shortcodes[5][$key];
1299 $shortcode[6] = $shortcodes[6][$key];
1300
1301 $atts = shortcode_parse_atts($shortcode[3]);
1302
1303 if (!array_key_exists('dataset', $atts) && array_key_exists('id', $atts)) {
1304 $template = new Model_E2pdf_Template();
1305 $template->load($atts['id']);
1306 if ($template->get('extension') === 'divi') {
1307 $item = $template->get('item');
1308
1309 if (!$dataset_id) {
1310 $serialized = serialize($_POST);
1311 $dataset = array(
1312 'extension' => 'divi',
1313 'item' => $item,
1314 'entry' => $serialized
1315 );
1316 $wpdb->insert($wpdb->prefix . 'e2pdf_datasets', $dataset);
1317 $dataset_id = $wpdb->insert_id;
1318 }
1319 $atts['dataset'] = $dataset_id;
1320 $shortcode[3] .= " dataset=\"{$dataset_id}\"";
1321 }
1322 }
1323 $new_shortcode = "[" . $shortcode[2] . $shortcode[3] . "]";
1324 $shortcode_atts['custom_message'] = str_replace($shortcode_value, $new_shortcode, $shortcode_atts['custom_message']);
1325 }
1326 }
1327 }
1328 }
1329 }
1330 }
1331
1332 $output = $et_pb_contact->_shortcode_callback($shortcode_atts, $content, $function_name, $parent_address, $global_parent, $global_parent_type);
1333
1334 remove_filter('wp_mail', array($this, 'filter_wp_mail'), 10);
1335
1336 $files = $this->helper->get('divi_attachments');
1337 if (is_array($files) && !empty($files)) {
1338 foreach ($files as $key => $file) {
1339 $this->helper->delete_dir(dirname($file) . '/');
1340 }
1341 $this->helper->deset('divi_attachments');
1342 }
1343
1344 return html_entity_decode($output, ENT_QUOTES);
1345 }
1346 }
1347
1348 /**
1349 * Delete dataset for template
1350 *
1351 * @param int $template_id - Template ID
1352 * @param int $dataset_id - Dataset ID
1353 *
1354 * @return bool - Result of removing items
1355 */
1356 public function delete_item($template_id = false, $dataset_id = false) {
1357 global $wpdb;
1358
1359 $template = new Model_E2pdf_Template();
1360 if ($template_id && $dataset_id && $template->load($template_id)) {
1361 $extension = new Model_E2pdf_Extension();
1362 if ($template->get('extension') === 'divi' && $extension->load($template->get('extension'))) {
1363
1364 $item_id = $template->get('item');
1365
1366 $where = array(
1367 'ID' => $dataset_id,
1368 'item' => $item_id,
1369 'extension' => 'divi'
1370 );
1371 $wpdb->delete($wpdb->prefix . 'e2pdf_datasets', $where);
1372 return true;
1373 }
1374 }
1375
1376 return false;
1377 }
1378
1379 /**
1380 * Delete all datasets for Template
1381 *
1382 * @param int $template_id - Template ID
1383 *
1384 * @return bool - Result of removing items
1385 */
1386 public function delete_items($template_id = false) {
1387 global $wpdb;
1388
1389 $template = new Model_E2pdf_Template();
1390
1391 if ($template_id && $template->load($template_id)) {
1392 $extension = new Model_E2pdf_Extension();
1393 if ($template->get('extension') === 'divi' && $extension->load($template->get('extension'))) {
1394
1395 $item_id = $template->get('item');
1396
1397 $where = array(
1398 'item' => $item_id,
1399 'extension' => 'divi'
1400 );
1401 $wpdb->delete($wpdb->prefix . 'e2pdf_datasets', $where);
1402 return true;
1403 }
1404 }
1405
1406 return false;
1407 }
1408
1409 /**
1410 * Get styles for generating Map Field function
1411 *
1412 * @return array - List of css files to load
1413 */
1414 public function get_styles() {
1415 $styles = array(
1416 plugins_url('css/extension/divi.css?v=' . time(), $this->helper->get('plugin_file_path'))
1417 );
1418 return $styles;
1419 }
1420
1421 }
1422