PluginProbe
PDF for WPForms + Drag and Drop Template Builder / trunk
PDF for WPForms + Drag and Drop Template Builder vtrunk
7.2.0 7.1.0 trunk 2.5.0 3.0.0 6.2.1 6.3.1 6.5.0 6.5.1 6.5.2
pdf-for-wpforms / wpforms / index.php

index.php in PDF for WPForms + Drag and Drop Template Builder trunk, at wpforms/index.php

1,297 lines 49.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH'))
3 exit; // Exit if accessed directly
4 global $yeepdf_settings_wpforms;
5 class Superaddons_Pdf_Creator_Wpfroms_Backend
6 {
7 private $attachments_notifications = array();
8 private $created = false;
9 public $form_data;
10 public $fields;
11 public $entry_id;
12 public $entry;
13 function __construct()
14 {
15 add_filter("yeepdf_shortcodes", array($this, "add_shortcode"));
16 add_action("yeepdf_head_settings", array($this, "add_head_settings"));
17 add_action("save_post_yeepdf", array($this, 'save_metabox'), 10, 2);
18 add_filter("wpforms_entry_details_sidebar_actions_link", array($this, "add_link_download"), 10, 3);
19 add_action("wpforms_form_settings_panel_content", array($this, "add_tab_settings"));
20 add_filter("wpforms_builder_settings_sections", array($this, "add_tab_settings_sections"));
21 add_action("wpforms_process_entry_saved", array($this, "save_pdf"), 10, 4);
22 add_filter("wpforms_emails_send_email_data", array($this, "add_attachments"), 10, 2);
23 add_filter("wpforms_emails_mailer_get_message", array($this, "wpforms_emails_mailer_get_message"), 10, 2);
24 add_filter("wpforms_builder_strings", array($this, "wpforms_builder_strings"), 10);
25 add_filter("wpforms_smarttags_process_value", array($this, "wpforms_smarttags_process_value"), 10, 3);
26 add_filter("yeepdf_add_libs", array($this, "yeepdf_add_libs"));
27 //resend
28 add_filter("wpforms_entry_email_before_send", array($this, "resend"), 10);
29 add_action('admin_enqueue_scripts', array($this, "settings_js"));
30 add_action('yeepdf_add_css', array($this, "add_css_order_summary"));
31 //add_action( 'wpforms_entries_init', array($this, 'regenerate_pdf' ), 15 );
32 add_action('wpforms_entry_details_init', array($this, 'wpforms_entry_details_init'), 15);
33 add_action("wpforms_email_send_after", array($this, "yeepdf_remove_all_file"));
34 add_shortcode('yeepdf_download_wpforms', array($this, 'yeepdf_download_wpforms'));
35 add_action('wp_ajax_yeepdf_wp_get_entries', array($this, "yeepdf_wp_get_entries"));
36 add_filter('yeepdf_output_html', array($this, 'yeepdf_output_html'), 10, 2);
37 add_filter('yeepdf_builder_shortcode', array($this, 'builder_shortcode'));
38 add_action('wpforms_form_settings_pdf_single_after', array($this, 'wpforms_form_settings_pdf_single_after'), 10);
39 add_filter("yeepdf_setup_id", array($this, "yeepdf_setup_id"), 10, 2);
40 add_filter("yeepdf_setup_type", array($this, "yeepdf_setup_type"));
41 add_filter("yeepdf_setup_forms", array($this, "yeepdf_setup_forms"), 10, 2);
42 add_filter("wpforms_frontend_confirmation_message", array($this, "wpforms_frontend_confirmation_message"), 10);
43 }
44 function wpforms_frontend_confirmation_message($confirmation_message)
45 {
46 return do_shortcode($confirmation_message);
47 }
48 function yeepdf_setup_type($type)
49 {
50 return "wpforms";
51 }
52 function yeepdf_setup_id($value, $post_id)
53 {
54 $form_id = get_post_meta($post_id, '_pdfcreator_wpforms', true);
55 if ($form_id != "") {
56 return $form_id;
57 }
58 $check = get_option("yeepdf_wpforms_setup");
59 if ($check == $post_id) {
60 return 0;
61 }
62 return $value;
63 }
64 function yeepdf_setup_forms($forms, $post_id)
65 {
66 $args = array(
67 'numberposts' => -1,
68 'post_type' => 'wpforms'
69 );
70 $wpforms = get_posts($args);
71 if ($wpforms) {
72 foreach ($wpforms as $post) {
73 $form_id = $post->ID;
74 $form_title = $post->post_title;
75 $forms[$form_id] = $form_title;
76 }
77 } else {
78 $forms[0] = esc_html__("No form", "pdf-for-wpforms");
79 }
80 return $forms;
81 }
82 function wpforms_form_settings_pdf_single_after()
83 {
84 ?>
85 <div class="wpforms-panel-field email-msg wpforms-panel-field-text pro_disable pro_disable_padding">
86 <label for="wpforms-panel-field-pdf_creator-1-yeepdf_name_number_pro"
87 class="inline"><?php esc_attr_e("PDF Password", "pdf-for-wpforms") ?></label>
88 <input type="text" disabled class="">
89 </div>
90 <div class="wpforms-panel-field email-msg wpforms-panel-field-text pro_disable pro_disable_padding">
91 <label for="wpforms-panel-field-pdf_creator-1-yeepdf_name_number_pro"
92 class="inline"><?php esc_attr_e("Save PDF to Dropbox", "pdf-for-wpforms") ?></label>
93 <input type="text" disabled class="">
94 <p><?php esc_html_e("Save this pdf to dropbox", "pdf-for-gravityforms"); ?> </p>
95 </div>
96 <div class="wpforms-panel-field email-msg wpforms-panel-field-checkbox pro_disable pro_disable_padding">
97 <input type="checkbox" disabled class="">
98 <label for="wpforms-panel-field-pdf_creator-1-yeepdf_name_number_pro"
99 class="inline"><?php esc_attr_e("Conditional Logic", "pdf-for-wpforms") ?></label>
100 <p class="note">
101 <?php esc_attr_e("allows you to enable or disable a PDF if a user responded a specific way.", "pdf-for-wpforms") ?>
102 </p>
103 </div>
104 <?php
105 }
106 function builder_shortcode($shortcodes)
107 {
108 global $post, $wpdb;
109 if ((isset($post->post_type) && $post->post_type == "yeepdf") || (isset($_GET["post_type"]) && $_GET["post_type"] == "yeepdf")) {
110 if (isset($post->ID)) {
111 $id_entry = get_post_meta($post->ID, '_pdfcreator_wpforms_entry', true);
112 if ($id_entry != "" && $id_entry != 0) {
113 $table_wp_submissions = $wpdb->prefix . "wpforms_entry_fields";
114 $results = $wpdb->get_results(
115 $wpdb->prepare(
116 "SELECT id,value,field_id FROM $table_wp_submissions WHERE entry_id = %s ORDER BY entry_id DESC LIMIT 200",
117 $id_entry
118 ),
119 ARRAY_A
120 );
121 foreach ($results as $rs) {
122 $shortcodes["{field_id='" . $rs["field_id"] . "'}"] = $rs["value"];
123 $shortcodes['{field_id="' . $rs["field_id"] . '"}'] = $rs["value"];
124 }
125 }
126 $form_id = get_post_meta($post->ID, '_pdfcreator_wpforms', true);
127 if (!empty($form_id)) {
128 $form = wpforms()->form->get(absint($form_id));
129 if (!empty($form->post_content)) {
130 $f_data = wpforms_decode($form->post_content);
131 if (!empty($f_data['fields'])) {
132 foreach ($f_data['fields'] as $f_id => $f_val) {
133 if ($f_val['type'] === 'html' && isset($f_val['code'])) {
134 $shortcodes["{field_id='" . $f_id . "'}"] = $f_val['code'];
135 $shortcodes['{field_id="' . $f_id . '"}'] = $f_val['code'];
136 } elseif ($f_val['type'] === 'content' && isset($f_val['content'])) {
137 $shortcodes["{field_id='" . $f_id . "'}"] = $f_val['content'];
138 $shortcodes['{field_id="' . $f_id . '"}'] = $f_val['content'];
139 }
140 }
141 }
142 }
143 }
144 }
145 }
146 return $shortcodes;
147 }
148 function yeepdf_output_html($html, $data_attrs)
149 {
150 global $wpdb;
151 $table_wp_submissions = $wpdb->prefix . "wpforms_entry_fields";
152 $query = $wpdb->prepare('SHOW TABLES LIKE %s', $wpdb->esc_like($table_wp_submissions));
153 if ($wpdb->get_var($query) == $table_wp_submissions) {
154 $template_id = $data_attrs["id_template"];
155 $id_entry = get_post_meta($template_id, '_pdfcreator_wpforms_entry', true);
156 $shortcodes = array();
157 if ($id_entry != "" && $id_entry != 0) {
158 $results = $wpdb->get_results(
159 $wpdb->prepare(
160 "SELECT id,value,field_id FROM $table_wp_submissions WHERE entry_id = %s ORDER BY entry_id DESC LIMIT 200",
161 $id_entry
162 ),
163 ARRAY_A
164 );
165 foreach ($results as $rs) {
166 $shortcodes["{field_id='" . $rs["field_id"] . "'}"] = $rs["value"];
167 $shortcodes['{field_id="' . $rs["field_id"] . '"}'] = $rs["value"];
168 }
169 }
170 $form_id = get_post_meta($template_id, '_pdfcreator_wpforms', true);
171 if (!empty($form_id)) {
172 $form = wpforms()->form->get(absint($form_id));
173 if (!empty($form->post_content)) {
174 $f_data = wpforms_decode($form->post_content);
175 if (!empty($f_data['fields'])) {
176 foreach ($f_data['fields'] as $f_id => $f_val) {
177 if ($f_val['type'] === 'html' && isset($f_val['code'])) {
178 $shortcodes["{field_id='" . $f_id . "'}"] = $f_val['code'];
179 $shortcodes['{field_id="' . $f_id . '"}'] = $f_val['code'];
180 } elseif ($f_val['type'] === 'content' && isset($f_val['content'])) {
181 $shortcodes["{field_id='" . $f_id . "'}"] = $f_val['content'];
182 $shortcodes['{field_id="' . $f_id . '"}'] = $f_val['content'];
183 }
184 }
185 }
186 }
187 }
188 if (!empty($shortcodes)) {
189 $html = str_replace(array_keys($shortcodes), array_values($shortcodes), $html);
190 }
191 }
192 return $html;
193 }
194 function yeepdf_wp_get_entries()
195 {
196 global $wpdb;
197 $table_wp_submissions = $wpdb->prefix . "wpforms_entries";
198 $query = $wpdb->prepare('SHOW TABLES LIKE %s', $wpdb->esc_like($table_wp_submissions));
199 if ($wpdb->get_var($query) == $table_wp_submissions) {
200 $data = sanitize_text_field($_POST["form_id"]);
201 if ($data != "" && $data > 0) {
202 $results = $wpdb->get_results(
203 $wpdb->prepare(
204 "SELECT entry_id,fields FROM $table_wp_submissions WHERE form_id = %s ORDER BY entry_id DESC LIMIT 20",
205 $data
206 ),
207 ARRAY_A
208 );
209 if (count($results) > 0) {
210 foreach ($results as $rs) {
211 $check = "";
212 $name = "";
213 $data_json = json_decode($rs["fields"], true);
214 if (count($data_json) > 0) {
215 foreach ($data_json as $data_fields) {
216 $name = $data_fields["name"] . " - " . $rs["entry_id"];
217 break;
218 }
219 }
220 ?>
221 <option value="<?php echo esc_attr($rs["entry_id"]) ?>">
222 <?php echo esc_html($name) ?>
223 </option>
224 <?php
225 }
226 }
227 }
228 }
229 die();
230 }
231 function yeepdf_remove_all_file()
232 {
233 $check_settings = get_option("pdf_creator_save_pdf");
234 if ($check_settings == "yes") {
235 Yeepdf_Settings_Main::destroy_all_files();
236 }
237 }
238 function yeepdf_download_wpforms($atts, $content = "")
239 {
240 return get_option("yeepdf_download_wpforms");
241 }
242 function add_css_order_summary()
243 {
244 ?>
245 .wpforms-order-summary-preview,
246 .wpforms-order-summary-preview th,
247 .wpforms-order-summary-preview td{
248 border-collapse: collapse;
249 padding: 10px;
250 text-align: left;
251 }
252 .wpforms-order-summary-preview td {
253 border: none !important;
254 }
255 .wpforms-order-summary-preview tr{
256 border: 1px solid #888888;
257 }
258 .wpforms-order-summary-preview-total {
259 font-weight: bold;
260 }
261 .wpforms-order-summary-placeholder-hidden,.wpforms-order-summary-placeholder {
262 display: none !important;
263 }
264 .wpforms-order-summary-item-quantity {
265 text-align: center !important;
266 }
267 <?php
268 }
269 public function is_pro()
270 {
271 if (method_exists('wpforms', "is_pro")) {
272 return wpforms()->is_pro();
273 } else {
274 return false;
275 }
276 }
277 function settings_js()
278 {
279 global $post;
280 $add_libs = false;
281 if ((isset($post->post_type) && $post->post_type == "yeepdf") || (isset($_GET["post_type"]) && $_GET["post_type"] == "yeepdf")) {
282 $add_libs = true;
283 }
284 $add_libs = apply_filters("yeepdf_add_libs", $add_libs);
285 if ((isset($_GET["page"]) && $_GET["page"] == "wpforms-builder") || $add_libs) {
286 wp_enqueue_script('yeepdf_wpforms', BUIDER_PDF_WPFORMS_PLUGIN_URL . 'wpforms/yeepdf-wpforms.js', array("jquery"));
287 wp_localize_script("yeepdf_wpforms", "yeepdf_wpforms", array("pro" => "pro"));
288 }
289 }
290 function yeepdf_add_libs($add)
291 {
292 if (isset($_GET["page"]) && $_GET["page"] == "wpforms-builder") {
293 $add = true;
294 }
295 return $add;
296 }
297 function wpforms_smarttags_process_value($content, $tag_name, $form_data)
298 {
299 if (isset($form_data["cover_r_to_br"]) && $form_data["cover_r_to_br"] && isset($content)) {
300 $new_content = nl2br($content);
301 $content = apply_filters("yeepdf_format_br", $new_content, $content);
302 }
303 return $content;
304 }
305 function wpforms_builder_strings($strings)
306 {
307 $add = array(
308 'yeepdf_creator_delete' => esc_html__('Are you sure you want to delete this pdf?', "pdf-for-wpforms"),
309 'yeepdf_creator_prompt' => esc_html__('Enter a pdf name', "pdf-for-wpforms"),
310 'yeepdf_creator_ph' => esc_html__('Eg: User pdf', "pdf-for-wpforms"),
311 'yeepdf_creator_error' => esc_html__('You must provide a pdf name', "pdf-for-wpforms")
312 );
313 return array_merge($strings, $add);
314 }
315 public function clear_empty_rules($conditionals)
316 {
317 if (empty($conditionals) || !is_array($conditionals)) {
318 return [];
319 }
320 foreach ($conditionals as $group_id => $group) {
321 if (empty($group) || !is_array($group)) {
322 unset($conditionals[$group_id]);
323 continue;
324 }
325 foreach ($group as $rule_id => $rule) {
326 if (!isset($rule['field']) || '' === $rule['field']) {
327 unset($conditionals[$group_id][$rule_id]);
328 }
329 }
330 if (empty($conditionals[$group_id])) {
331 unset($conditionals[$group_id]);
332 }
333 }
334 return $conditionals;
335 }
336 function process_pdf_conditionals($process, $fields, $form_data, $id)
337 {
338 $settings = $form_data['settings'];
339 if (
340 empty($settings['yeepdf_creator'][$id]['conditional_logic']) ||
341 empty($settings['yeepdf_creator'][$id]['conditional_type']) ||
342 empty($settings['yeepdf_creator'][$id]['conditionals'])
343 ) {
344 return $process;
345 }
346 $conditionals = $this->clear_empty_rules($settings['yeepdf_creator'][$id]['conditionals']);
347 if (empty($conditionals)) {
348 return $process;
349 }
350 $type = $settings['yeepdf_creator'][$id]['conditional_type'];
351 if (function_exists("wpforms_conditional_logic")) {
352 $process = wpforms_conditional_logic()->process($fields, $form_data, $conditionals);
353 }
354 if ('stop' === $type) {
355 $process = !$process;
356 }
357 if (!$process) {
358 }
359 return $process;
360 }
361 function add_link_download($action_links, $entry, $form_data)
362 {
363 $upload_dir = wp_upload_dir();
364 $datas = wpforms()->entry_meta->get_meta(array("entry_id" => $entry->entry_id, "type" => "pdf_link"));
365 $url_re = get_admin_url() . "admin.php?page=wpforms-entries&view=details&entry_id=" . $entry->entry_id . "&action=regenerate_pdf";
366 $url_re = wp_nonce_url($url_re, 'regenerate_pdf');
367 $action_links["pdf_recreate"] = array(
368 'url' => $url_re,
369 'icon' => 'dashicons-media-text',
370 'label' => esc_html__("Re-generate PDF", "pdf-for-wpforms"),
371 );
372 if (isset($datas[0]->data)) {
373 $path_main = json_decode($datas[0]->data, true);
374 if (count($path_main) > 0) {
375 $i = 1;
376 foreach ($path_main as $url) {
377 $pdf_name = pathinfo(wp_basename(parse_url($url, PHP_URL_PATH)), PATHINFO_FILENAME);
378 $action_links["pdf" . $i] = array(
379 'url' => $url,
380 'target' => 'blank',
381 'icon' => 'dashicons-media-text',
382 'label' => !empty($pdf_name) ? esc_html($pdf_name . ".pdf") : esc_html__("Download PDF", "pdf-for-wpforms") . " " . $i,
383 );
384 $i++;
385 }
386 }
387 }
388 return $action_links;
389 }
390 function wpforms_entry_details_init($wpforms)
391 {
392 if (empty($_GET['action']) || $_GET['action'] !== 'regenerate_pdf') {
393 return;
394 }
395 $this->regenerate_pdf($wpforms);
396 printf(
397 '<div class="%1$s"><p>%2$s</p></div>', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
398 esc_attr(implode(' ', array("notice", "notice-success", "wpforms-notice"))),
399 'Re-generated PDF' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
400 );
401 }
402 //click resend pdf
403 function regenerate_pdf($wpforms)
404 {
405 if (empty($_GET['action']) || $_GET['action'] !== 'regenerate_pdf') {
406 return;
407 }
408 // Security check.
409 if (empty($_GET['_wpnonce']) || !wp_verify_nonce(sanitize_key($_GET['_wpnonce']), 'regenerate_pdf')) {
410 return;
411 }
412 if ($this->is_pro()) {
413 $fields = wpforms_decode($wpforms->entry->fields);
414 $entry = (array) wpforms()->entry->get($wpforms->entry->entry_id);
415 $form_data = wpforms_decode($wpforms->form->post_content);
416 $entry_id = $wpforms->entry->entry_id;
417 $entry_fields = json_decode($entry["fields"], true);
418 $entry["fields"] = $entry_fields;
419 $this->save_pdf($fields, $entry, $form_data, $entry_id);
420 }
421 }
422 //click resend notify
423 function resend($emails)
424 {
425 if ($this->is_pro()) {
426 $fields = $emails->fields;
427 $entry = (array) wpforms()->entry->get($emails->entry_id);
428 $form_data = $emails->form_data;
429 $entry_id = $emails->entry_id;
430 $entry_fields = json_decode($entry["fields"], true);
431 $entry["fields"] = $entry_fields;
432 $this->save_pdf($fields, $entry, $form_data, $entry_id);
433 }
434 return $emails;
435 }
436 function save_pdf($fields, $entry, $form_data, $entry_id)
437 {
438 foreach ($form_data["fields"] as $id => $field) {
439 if ($field["type"] == "repeater") {
440 $columns = $field['columns'];
441 $all_base_ids = [];
442 foreach ($columns as $column) {
443 if (!empty($column['fields']) && is_array($column['fields'])) {
444 $all_base_ids = array_merge($all_base_ids, $column['fields']);
445 }
446 }
447 //var_dump($all_base_ids);
448 $rows = [];
449 $i = 1;
450 while (true) {
451 $suffix = ($i === 1) ? '' : '_' . $i;
452 // Kiểm tra field đầu tiên của danh sách có tồn tại ở hàng này không
453 $check_id = $all_base_ids[0] . $suffix;
454 if (!isset($fields[$check_id])) {
455 break; // Hết hàng thì nghỉ
456 }
457 $row_data = [];
458 foreach ($all_base_ids as $id) {
459 $current_key = $id . $suffix;
460 // Lấy value và name để làm bảng cho chuẩn
461 $row_data[$id] = [
462 'label' => $fields[$current_key]['name'] ?? "Field $id",
463 'value' => $fields[$current_key]['value'] ?? ''
464 ];
465 }
466 $rows[] = $row_data;
467 $i++;
468 }
469 $table_html = '';
470 if (!empty($rows)) {
471 $table_html .= '<table class="yee-repeater-table" border="1" style="border-collapse: collapse; width: 100%; text-align: left; margin: 15px 0;">';
472 // Header (Lấy label từ hàng đầu tiên)
473 $table_html .= '<thead style="background: #f8f8f8;"><tr>';
474 foreach ($rows[0] as $data) {
475 $table_html .= '<th style="padding: 10px; border: 1px solid #ddd;">' . esc_html($data['label']) . '</th>';
476 }
477 $table_html .= '</tr></thead>';
478 // Body (Dữ liệu các hàng)
479 $table_html .= '<tbody>';
480 foreach ($rows as $row) {
481 $table_html .= '<tr>';
482 foreach ($row as $data) {
483 $table_html .= '<td style="padding: 10px; border: 1px solid #ddd;">' . esc_html($data['value']) . '</td>';
484 }
485 $table_html .= '</tr>';
486 }
487 $table_html .= '</tbody></table>';
488 } else {
489 $table_html = '<p></p>';
490 }
491 $entry["fields"][$field["id"]] = [
492 "name" => $field["label"],
493 "value" => $table_html,
494 "id" => $field["id"],
495 "type" => "text"
496 ];
497 $fields[$field["id"]] = [
498 "name" => $field["label"],
499 "value" => $table_html,
500 "id" => $field["id"],
501 "type" => "text"
502 ];
503 $form_data[$field["id"]] = $table_html;
504 }
505 }
506 $this->form_data = $form_data;
507 $this->fields = $fields;
508 $this->entry_id = $entry_id;
509 $this->entry = $entry;
510 if ($this->created) {
511 return;
512 }
513 $this->created = true;
514 $form_id = $form_data["id"];
515 $form_data["cover_r_to_br"] = true;
516 $attachments_notifications = $this->attachments_notifications;
517 $attachments = array();
518 $upload_dir = wp_upload_dir();
519 $form_settings = $form_data["settings"];
520 //cập nhật settings mới
521 if (!isset($form_settings["yeepdf_creator_enable"])) {
522 if (isset($form_settings["pdf_creator_enable"])) {
523 $form_settings["yeepdf_creator_enable"] = $form_settings["pdf_creator_enable"];
524 }
525 }
526 if (!isset($form_settings["yeepdf_creator"])) {
527 if (isset($form_settings["pdf_creator"])) {
528 $form_settings["yeepdf_creator"] = $form_settings["pdf_creator"];
529 }
530 }
531 if (isset($form_settings["yeepdf_creator_enable"]) && $form_settings["yeepdf_creator_enable"] == 1) {
532 $pdfs = $form_settings["yeepdf_creator"];
533 $data_entry = array();
534 foreach ($entry as $k => $v) {
535 if ($k == "fields") {
536 foreach ($v as $key => $vl) {
537 if (is_array($vl)) {
538 if (isset($vl["value"])) {
539 if (is_array($vl["value"])) {
540 $vl = implode(", ", $vl["value"]);
541 } else {
542 $vl = $vl["value"];
543 }
544 } else {
545 $vl = implode(", ", $vl);
546 }
547 }
548 $vl = str_replace("\r", "<br />", $vl);
549 $data_entry["{field_id='" . $key . "'}"] = $vl;
550 }
551 } else {
552 if (is_array($v)) {
553 $v = array_filter($v, function ($item) {
554 return is_scalar($item);
555 });
556 $v = implode(", ", $v);
557 }
558 if (is_string($v)) {
559 $vl = str_replace("\r", "<br />", $v);
560 $data_entry['{' . $k . '}'] = $v;
561 }
562 }
563 }
564 foreach ($pdfs as $id => $pdf) {
565 $list_notifitions = array();
566 foreach ($pdf as $pdf_key => $pdf_value) {
567 if (preg_match("/pdf_notifications_/", $pdf_key)) {
568 if ($pdf_value == 1) {
569 $keys = explode("_", $pdf_key);
570 $list_notifitions[] = end($keys);
571 }
572 }
573 }
574 $template_id = $pdf["template_id"];
575 $name = "";
576 if (isset($pdf["yeepdf_name"])) {
577 $name = $pdf["yeepdf_name"];
578 }
579 $password = "";
580 if (isset($pdf["yeepdf_password"])) {
581 $password = $pdf["yeepdf_password"];
582 }
583 $conditional_logic = "";
584 if (isset($pdf["conditional_logic"])) {
585 $conditional_logic = $pdf["conditional_logic"];
586 }
587 $process = $this->process_pdf_conditionals(true, $fields, $form_data, $id);
588 if (!$process) {
589 continue;
590 }
591 if ($name == "") {
592 $name = "contact-form";
593 } else {
594 $name = wpforms_process_smart_tags($name, $form_data, $fields, $entry_id);
595 }
596 if ($password != "") {
597 $password = wpforms_process_smart_tags($password, $form_data, $fields, $entry_id);
598 }
599 $name = sanitize_file_name($name);
600 $data_send_settings = array(
601 "id_template" => $template_id,
602 "type" => "html",
603 "name" => $name,
604 "datas" => $data_entry,
605 "return_html" => true,
606 );
607 $message = Yeepdf_Create_PDF::pdf_creator_preview($data_send_settings);
608 $message = $this->add_all_fields($message, $form_data, $fields, $entry_id);
609 if (!empty($form_data['fields'])) {
610 $special_tags = [];
611 foreach ($form_data['fields'] as $f_id => $f_val) {
612 if ($f_val['type'] === 'html' && isset($f_val['code'])) {
613 $special_tags["{field_id='" . $f_id . "'}"] = $f_val['code'];
614 $special_tags['{field_id="' . $f_id . '"}'] = $f_val['code'];
615 $special_tags["{field_html_id='" . $f_id . "'}"] = $f_val['code'];
616 $special_tags['{field_html_id="' . $f_id . '"}'] = $f_val['code'];
617 } elseif ($f_val['type'] === 'content' && isset($f_val['content'])) {
618 $special_tags["{field_id='" . $f_id . "'}"] = $f_val['content'];
619 $special_tags['{field_id="' . $f_id . '"}'] = $f_val['content'];
620 $special_tags["{field_html_id='" . $f_id . "'}"] = $f_val['content'];
621 $special_tags['{field_html_id="' . $f_id . '"}'] = $f_val['content'];
622 }
623 }
624 if (!empty($special_tags)) {
625 $message = str_replace(array_keys($special_tags), array_values($special_tags), $message);
626 }
627 }
628 $message = wpforms_process_smart_tags($message, $form_data, $fields, $entry_id);
629 $pattern = '/<tr[^>]*class="[^"]*wpforms-order-summary-placeholder-hidden[^"]*"[^>]*>.*?<\/tr>/is';
630 $message = preg_replace($pattern, '', $message);
631 if (isset($pdf["yeepdf_save_dropbox"]) && $pdf["yeepdf_save_dropbox"] == 1) {
632 $save_dropbox = true;
633 } else {
634 $save_dropbox = false;
635 }
636 $data_send_settings_download = array(
637 "id_template" => $template_id,
638 "type" => "upload",
639 "name" => $name,
640 "datas" => $data_entry,
641 "html" => $message,
642 "password" => $password,
643 "save_dropbox" => $save_dropbox,
644 );
645 $data_send_settings_download = apply_filters("pdf_before_render_datas", $data_send_settings_download);
646 $folder_uploads = Yeepdf_Create_PDF::pdf_creator_preview($data_send_settings_download);
647 $attachments[] = $folder_uploads["url"];
648 update_option("yeepdf_download_wpforms", $folder_uploads["url"]);
649 foreach ($list_notifitions as $notifi) {
650 $attachments_notifications[$notifi][] = $folder_uploads["path"];
651 }
652 }
653 if ($this->is_pro()) {
654 wpforms()->entry_meta->add(
655 [
656 'entry_id' => absint($entry_id),
657 'form_id' => absint($form_id),
658 'user_id' => get_current_user_id(),
659 'type' => 'pdf_link',
660 'data' => json_encode($attachments),
661 ],
662 'entry_meta'
663 );
664 }
665 $this->attachments_notifications = $attachments_notifications;
666 }
667 }
668 function add_attachments($email, $email_obj)
669 {
670 $attachments = array();
671 $attachments_notifications = $this->attachments_notifications;
672 if (!isset($email_obj->form_data, $email_obj->notification_id, $email_obj->fields)) {
673 return $email;
674 }
675 $form_data = $email_obj->form_data;
676 $notification_id = $email_obj->notification_id;
677 $entry_fields = $email_obj->fields;
678 if (array_key_exists($notification_id, $attachments_notifications)) {
679 $link_wp = get_option("yeepdf_download_wpforms");
680 $email['message'] = str_replace("[yeepdf_download_wpforms]", $link_wp, $email["message"]);
681 $email['attachments'] = array_merge((array) $email['attachments'], $attachments_notifications[$notification_id]);
682 }
683 return $email;
684 }
685 function wpforms_emails_mailer_get_message($message, $mailer)
686 {
687 $link_wp = get_option("yeepdf_download_wpforms");
688 $message = str_replace(array("[yeepdf_download_wpforms]"), $link_wp, $message);
689 return $message;
690 }
691 function add_tab_settings_sections($sections)
692 {
693 $sections["yeepdf_creator"] = esc_html__("PDF", "pdf-for-wpforms");
694 return $sections;
695 }
696 function add_tab_settings($settings)
697 {
698 ?>
699 <div class="wpforms-panel-content-section wpforms-panel-content-section-yeepdf_creator" data-panel="yeepdf_creator">
700 <?php $this->form_settings_pdf_creator($settings) ?>
701 </div>
702 <?php
703 }
704 public function form_settings_pdf_creator($settings)
705 {
706 $form_id = $settings->form->ID;
707 $form_settings = !empty($settings->form_data['settings']) ? $settings->form_data['settings'] : [];
708 $form_data = !empty($settings->form_data) ? $settings->form_data : [];
709 //nếu không tồn tại settings mới thì lấy settings củ
710 if (!isset($form_settings['yeepdf_creator_enable'])) {
711 if (isset($form_settings['pdf_creator_enable'])) {
712 $form_settings["yeepdf_creator_enable"] = $form_settings['pdf_creator_enable'];
713 }
714 }
715 if (!isset($form_settings['yeepdf_creator'])) {
716 if (isset($form_settings['pdf_creator'])) {
717 $form_settings["yeepdf_creator"] = $form_settings['pdf_creator'];
718 }
719 }
720 $form_data["settings"] = $form_settings;
721 $pdfs = is_array($form_settings) && isset($form_settings['yeepdf_creator']) ? $form_settings['yeepdf_creator'] : [];
722 if (empty($pdfs)) {
723 $next_id = 2;
724 $pdfs[1]['template_id'] = !empty($form_settings['yeepdf_creator_template_id']) ? $form_settings['yeepdf_creator_template_id'] : '';
725 $pdfs[1]['name'] = !empty($form_settings['yeepdf_creator_name']) ? $form_settings['yeepdf_creator_name'] : '';
726 $pdfs[1]['password'] = !empty($form_settings['yeepdf_creator_password']) ? $form_settings['yeepdf_creator_password'] : '';
727 } else {
728 $next_id = max(array_keys($pdfs)) + 1;
729 }
730 $default_pdfs_key = min(array_keys($pdfs));
731 $hidden = empty($form_settings['yeepdf_creator_enable']) ? 'hidden' : '';
732 ?>
733 <div class="wpforms-panel-content-section-title">
734 <span id="wpforms-builder-settings-yeepdf_creator-title">
735 <?php esc_html_e('PDF', "pdf-for-wpforms") ?>
736 </span>
737 <button class="wpforms-yeepdf_creator-add wpforms-builder-settings-block-add" data-block-type="yeepdf_creator"
738 data-next-id="<?php echo absint($next_id) ?>"><?php esc_html_e("Add New PDF", "pdf-for-wpforms") ?></button>
739 </div>
740 <?php
741 wpforms_panel_field(
742 'toggle',
743 'settings',
744 'yeepdf_creator_enable',
745 $form_data,
746 esc_html__('Enable PDF', "pdf-for-wpforms"),
747 [
748 'value' => empty($form_settings['yeepdf_creator_enable']) ? 0 : 1,
749 'class' => 'pdfcreator_datas_enable',
750 'input_class' => 'pdfcreator_datas_enable',
751 'data' => array("tab" => ".wpforms-yeepdf_creator")
752 ]
753 );
754 $yeepdfs = get_posts(array('post_type' => 'yeepdf', 'post_status' => 'publish', 'numberposts' => -1));
755 $list_templates = array();
756 if ($yeepdfs) {
757 foreach ($yeepdfs as $post) {
758 $post_id = $post->ID;
759 $list_templates[$post_id] = $post->post_title;
760 }
761 } else {
762 $list_templates[-1] = esc_html__("No template", "pdf-for-wpforms");
763 }
764 $notifications_datas = is_array($form_settings) && isset($form_settings['notifications']) ? $form_settings['notifications'] : [];
765 if (empty($notifications_datas)) {
766 $notifications_datas[1]['name'] = "Default";
767 }
768 foreach ($pdfs as $id => $pdf) {
769 $name = !empty($pdf['name']) ? $pdf['name'] : esc_html__('Default PDF', "pdf-for-wpforms");
770 $closed_state = '';
771 $toggle_state = '<i class="fa fa-chevron-circle-up"></i>';
772 $block_classes = 'wpforms-yeepdf_creator wpforms-builder-settings-block ' . $hidden;
773 $from_name_after = apply_filters('wpforms_builder_yeepdf_creator_from_name_after', '', $settings->form_data, $id);
774 if (!empty($settings->form_data['id']) && 'closed' === wpforms_builder_settings_block_get_state($settings->form_data['id'], $id, 'yeepdf_creator')) {
775 $closed_state = 'style="display:none"';
776 $toggle_state = '<i class="fa fa-chevron-circle-down"></i>';
777 }
778 if ($default_pdfs_key === $id) {
779 $block_classes .= ' wpforms-builder-settings-block-default';
780 }
781 do_action('wpforms_form_settings_yeepdf_creator_single_before', $settings, $id);
782 ?>
783 <div class="<?php echo esc_attr($block_classes); ?>" data-block-type="yeepdf_creator"
784 data-block-id="<?php echo absint($id); ?>">
785 <div class="wpforms-builder-settings-block-header">
786 <div class="wpforms-builder-settings-block-actions">
787 <?php do_action('wpforms_form_settings_yeepdf_creator_single_action', $id, $pdf, $settings); ?>
788 <button class="wpforms-builder-settings-block-clone"
789 title="<?php esc_attr_e('Clone', "pdf-for-wpforms"); ?>"><i class="fa fa-copy"></i></button><!--
790 --><button class="wpforms-builder-settings-block-delete"
791 title="<?php esc_attr_e('Delete', "pdf-for-wpforms"); ?>"><i class="fa fa-trash-o"></i></button><!--
792 --><button class="wpforms-builder-settings-block-toggle"
793 title="<?php esc_attr_e('Open / Close', "pdf-for-wpforms"); ?>">
794 <?php echo $toggle_state; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
795 ?>
796 </button>
797 </div>
798 <div class="wpforms-builder-settings-block-name-holder">
799 <span class="wpforms-builder-settings-block-name">
800 <?php echo esc_html($name); ?>
801 </span>
802 <div class="wpforms-builder-settings-block-name-edit">
803 <input type="text" name="settings[yeepdf_creator][<?php echo absint($id); ?>][name]"
804 value="<?php echo esc_attr($name); ?>">
805 </div>
806 <button class="wpforms-builder-settings-block-edit"
807 title="<?php esc_attr_e('Edit', "pdf-for-wpforms"); ?>"><i class="fa fa-pencil"></i></button>
808 </div>
809 </div>
810 <div class="wpforms-builder-settings-block-content" <?php echo $closed_state; ?>>
811 <?php
812 $class_logic_type_upload = "";
813 $class_logic_type_id = "";
814 wpforms_panel_field(
815 'select',
816 'yeepdf_creator',
817 'template_id',
818 $form_data,
819 esc_html__('Choose template', "pdf-for-wpforms"),
820 [
821 'options' => $list_templates,
822 'parent' => 'settings',
823 'subsection' => $id,
824 'input_id' => 'wpforms-panel-field-pdf-1-' . $id,
825 'input_class' => 'wpforms-panel-field-pdf-1',
826 'class' => 'gform-kitchen-sink-template_id ' . $class_logic_type_id,
827 ]
828 );
829 ?>
830 <div class="wpforms-panel-field email-msg wpforms-panel-field-text">
831 <label><?php esc_html_e('Attachment pdf in Notifications', "pdf-for-wpforms") ?></label>
832 </div>
833 <?php
834 foreach ($notifications_datas as $id_n => $notifications_data) {
835 if (isset($notifications_data["notification_name"])) {
836 $notification_name = $notifications_data["notification_name"];
837 } else {
838 $notification_name = "Default Notification";
839 }
840 wpforms_panel_field(
841 'checkbox',
842 'yeepdf_creator',
843 'pdf_notifications_' . $id_n,
844 $form_data,
845 esc_html($notification_name),
846 [
847 'tooltip' => esc_html__("Send the PDF as an email attachment for the selected notifications.", "pdf-for-wpforms"),
848 'parent' => 'settings',
849 'subsection' => $id,
850 ]
851 );
852 }
853 wpforms_panel_field(
854 'text',
855 'yeepdf_creator',
856 'yeepdf_name',
857 $form_data,
858 esc_html__('PDF Template Custom Name', "pdf-for-wpforms"),
859 [
860 'rows' => 6,
861 'default' => "",
862 'smarttags' => [
863 'type' => 'all',
864 ],
865 'parent' => 'settings',
866 'subsection' => $id,
867 'class' => 'email-msg',
868 'after' => '<p class="note">{name}.pdf</p>',
869 ]
870 );
871 do_action('wpforms_form_settings_pdf_single_after', $settings, $id);
872 ?>
873 </div><!-- /.wpforms-builder-settings-block-content -->
874 </div><!-- /.wpforms-builder-settings-block -->
875 <?php
876 }
877 }
878 function wpforms_process_smart_tags(
879 $value,
880 $tag_name,
881 $form_data,
882 $fields,
883 $entry_id,
884 $smart_tag_object
885 ) {
886 $lists = preg_split('/\r\n|[\r\n]/', $value);
887 $datas = array();
888 foreach ($lists as $image) {
889 $image = trim($image);
890 if (preg_match('/^(http|https):\/\/(.*?)\.(png|JPEG|jpeg|jpg|gif|PNG|JPG|GIF)$/i', $image)) {
891 $datas[] = '<img style="width:150px;height:39px" src="' . $image . '" />';
892 }
893 }
894 if (count($datas) > 0) {
895 return implode("</br>", $datas);
896 }
897 return $value;
898 }
899 function add_head_settings($post)
900 {
901 global $wpdb;
902 if (isset($_GET["post"])) {
903 $post_id = sanitize_text_field($_GET["post"]);
904 } else {
905 $post_id = $post->ID;
906 }
907 $data = get_post_meta($post_id, '_pdfcreator_wpforms', true);
908 ?>
909 <div class="yeepdf-testting-order">
910 <select name="pdfcreator_wpforms" class="builder_pdf_woo_testing pdfcreator_wpforms">
911 <option value='-1'>--- <?php esc_html_e("WPForms", "pdf-for-wpforms") ?> ---</option>
912 <?php
913 $args = array(
914 'numberposts' => -1,
915 'post_type' => 'wpforms'
916 );
917 $wpforms = get_posts($args);
918 if ($wpforms) {
919 foreach ($wpforms as $post) {
920 $form_id = $post->ID;
921 $form_title = $post->post_title;
922 ?>
923 <option <?php selected($data, $form_id) ?> value="<?php echo esc_attr($form_id) ?>">
924 <?php echo esc_html($form_title) ?></option>
925 <?php
926 }
927 } else {
928 ?>
929 <option value='0'><?php esc_html_e("Create a form", "pdf-for-wpforms") ?></option>
930 <?php
931 }
932 ?>
933 </select>
934 <select name="pdfcreator_wpforms_entry" id="pdfcreator_wpforms_entry">
935 <option value="0"><?php esc_attr_e("Sample to show", "pdf-for-elementor") ?></option>
936 <?php
937 $table_wp_submissions = $wpdb->prefix . "wpforms_entries";
938 $query = $wpdb->prepare('SHOW TABLES LIKE %s', $wpdb->esc_like($table_wp_submissions));
939 if ($wpdb->get_var($query) == $table_wp_submissions) {
940 if ($data != "" && $data > 0) {
941 $results = $wpdb->get_results(
942 $wpdb->prepare(
943 "SELECT entry_id,fields FROM $table_wp_submissions WHERE form_id = %s ORDER BY entry_id DESC LIMIT 20",
944 $data
945 ),
946 ARRAY_A
947 );
948 if (count($results) > 0) {
949 $id_entry = get_post_meta($post->ID, '_pdfcreator_wpforms_entry', true);
950 foreach ($results as $rs) {
951 $check = "";
952 $name = "";
953 $data_json = json_decode($rs["fields"], true);
954 if (count($data_json) > 0) {
955 foreach ($data_json as $data_fields) {
956 $name = $data_fields["name"] . " - " . $rs["entry_id"];
957 break;
958 }
959 }
960 if ($rs["entry_id"] == $id_entry) {
961 $check = 'selected';
962 }
963 ?>
964 <option <?php echo esc_attr($check) ?> value="<?php echo esc_attr($rs["entry_id"]) ?>">
965 <?php echo esc_html($name) ?>
966 </option>
967 <?php
968 }
969 }
970 }
971 }
972 ?>
973 </select>
974 </div>
975 <?php
976 }
977 function save_metabox($post_id, $post)
978 {
979 if (isset($_POST['pdfcreator_wpforms'])) {
980 $id = sanitize_text_field($_POST['pdfcreator_wpforms']);
981 $entry = sanitize_text_field($_POST['pdfcreator_wpforms_entry']);
982 update_post_meta($post_id, '_pdfcreator_wpforms', $id);
983 update_post_meta($post_id, '_pdfcreator_wpforms_entry', $entry);
984 }
985 }
986 function add_shortcode($shortcode)
987 {
988 if (isset($_GET["post"])) {
989 $post_id = sanitize_text_field($_GET["post"]);
990 $form_id = get_post_meta($post_id, '_pdfcreator_wpforms', true);
991 $fields = array();
992 if ($form_id) {
993 $inner_shortcode = array(
994 "{form_name}" => "Form Name",
995 "{form_id}" => "Form ID",
996 "{entry_id}" => "Entry ID",
997 "{entry_details_url}" => "Entry Details URL",
998 "{all_fields}" => "{all_fields}",
999 "{all_fields_full}" => "{all_fields_full}",
1000 );
1001 $form = wpforms()->form->get(absint($form_id));
1002 // If the form doesn't exists, abort.
1003 if (empty($form)) {
1004 return $shortcode;
1005 }
1006 // Pull and format the form data out of the form object.
1007 $form_data = !empty($form->post_content) ? wpforms_decode($form->post_content) : '';
1008 // Setup the form fields.
1009 $form_fields = !empty($form_data['fields']) && is_array($form_data['fields']) ? $form_data['fields'] : [];
1010 if (is_array($form_fields)) {
1011 foreach ($form_fields as $id => $datas) {
1012 $label = $id;
1013 if (!empty($datas["label"])) {
1014 $label = $datas["label"];
1015 } elseif (!empty($datas["name"])) {
1016 $label = $datas["name"];
1017 } elseif (!empty($datas["type"])) {
1018 $label = ucfirst($datas["type"]) . " #" . $id;
1019 }
1020 $inner_shortcode["{field_id='" . $id . "'}"] = $label;
1021 }
1022 }
1023 $inner_shortcode["{entry_id}"] = 'Entry Id';
1024 $shortcode["WPForms"] = $inner_shortcode;
1025 }
1026 }
1027 return $shortcode;
1028 }
1029 function add_all_fields($message, $form_data, $fields, $entry_id)
1030 {
1031 $pattern = '/\{(all_fields(_full)?)([^}]*)\}/i';
1032 if (!preg_match($pattern, $message)) {
1033 return $message;
1034 }
1035
1036 $message = preg_replace_callback($pattern, function ($matches) {
1037 $tag = strtolower($matches[1]);
1038 $attrs_str = $matches[3] ?? '';
1039 $is_full = ($tag === 'all_fields_full');
1040
1041 $allowed_ids = [];
1042 $excluded_ids = [];
1043 if (!empty($attrs_str)) {
1044 if (preg_match('/(?:fields|include)\s*=\s*["\']([^"\']+)["\']/i', $attrs_str, $m)) {
1045 $allowed_ids = array_map('trim', explode(',', $m[1]));
1046 }
1047 if (preg_match('/exclude\s*=\s*["\']([^"\']+)["\']/i', $attrs_str, $m)) {
1048 $excluded_ids = array_map('trim', explode(',', $m[1]));
1049 }
1050 }
1051
1052 return $this->process_field_values($is_full, $allowed_ids, $excluded_ids);
1053 }, $message);
1054
1055 return $message;
1056 }
1057 public function process_field_values($is_full = false, $allowed_ids = [], $excluded_ids = [])
1058 {
1059 // If both fields and form_data fields are empty, return an empty message.
1060 if (empty($this->fields) && empty($this->form_data['fields'])) {
1061 return '';
1062 }
1063 // If no message was generated, create an empty message.
1064 $default_message = esc_html__('An empty form was submitted.', 'wpforms-lite');
1065 /**
1066 * Filter whether to display empty fields in the email.
1067 *
1068 * @since 1.8.5
1069 * @deprecated 1.8.5.2
1070 *
1071 * @param bool $show_empty_fields Whether to display empty fields in the email.
1072 */
1073 $show_empty_fields = apply_filters_deprecated( // phpcs:disable WPForms.Comments.ParamTagHooks.InvalidParamTagsQuantity
1074 'wpforms_emails_notifications_display_empty_fields',
1075 [false],
1076 '1.8.5.2 of the WPForms plugin',
1077 'wpforms_email_display_empty_fields'
1078 );
1079 /** This filter is documented in /includes/emails/class-emails.php */
1080 $show_empty_fields = apply_filters( // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
1081 'wpforms_email_display_empty_fields',
1082 false
1083 );
1084 $message = $this->process_html_message($show_empty_fields, $is_full, $allowed_ids, $excluded_ids);
1085 return empty($message) ? $default_message : $message;
1086 }
1087 public function process_html_message($show_empty_fields = false, $is_full = false, $allowed_ids = [], $excluded_ids = [])
1088 { // phpcs:ignore Generic.Metrics.CyclomaticComplexity
1089 $message = '<table border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse; border-spacing: 0px; padding: 0px; vertical-align: top;">';
1090 /**
1091 * Filter the list of field types to display in the email.
1092 *
1093 * @since 1.8.5
1094 * @deprecated 1.8.5.2
1095 *
1096 * @param array $other_fields List of field types.
1097 * @param array $form_data Form data.
1098 */
1099 $default_other_fields = $is_full ? ['html', 'content', 'divider', 'pagebreak'] : [];
1100 $other_fields = apply_filters_deprecated( // phpcs:disable WPForms.Comments.ParamTagHooks.InvalidParamTagsQuantity
1101 'wpforms_emails_notifications_display_other_fields',
1102 [$default_other_fields, $this->form_data],
1103 '1.8.5.2 of the WPForms plugin',
1104 'wpforms_email_display_other_fields'
1105 );
1106 /** This filter is documented in /includes/emails/class-emails.php */
1107 $other_fields = apply_filters( // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
1108 'wpforms_email_display_other_fields',
1109 $default_other_fields,
1110 $this
1111 );
1112 if (empty($this->form_data['fields']) || !is_array($this->form_data['fields'])) {
1113 return '';
1114 }
1115 foreach ($this->form_data['fields'] as $field_id => $field) {
1116 if (!empty($allowed_ids) && !in_array((string) $field_id, $allowed_ids, true)) {
1117 continue;
1118 }
1119 if (!empty($excluded_ids) && in_array((string) $field_id, $excluded_ids, true)) {
1120 continue;
1121 }
1122
1123 $field_type = !empty($field['type']) ? $field['type'] : '';
1124 $is_special_field = false;
1125
1126 // Check if the field is empty in $this->fields.
1127 if (empty($this->fields[$field_id])) {
1128 // Check if the field type is in $other_fields, otherwise skip.
1129 if (empty($other_fields) || !in_array($field_type, $other_fields, true)) {
1130 continue;
1131 }
1132 // Handle specific field types.
1133 list($field_name, $field_val) = $this->process_special_field_values($field);
1134 $is_special_field = true;
1135 } else {
1136 // Handle fields that are not empty in $this->fields.
1137 if (!$show_empty_fields && (!isset($this->fields[$field_id]['value']) || (string) $this->fields[$field_id]['value'] === '')) {
1138 continue;
1139 }
1140 $field_name = isset($this->fields[$field_id]['name']) ? $this->fields[$field_id]['name'] : '';
1141 $field_val = empty($this->fields[$field_id]['value']) && !is_numeric($this->fields[$field_id]['value']) ? '<em>' . esc_html__('(empty)', 'wpforms-lite') . '</em>' : $this->fields[$field_id]['value'];
1142 }
1143
1144 // Render special non-input fields
1145 if ($is_special_field) {
1146 if (in_array($field_type, ['html', 'content'], true)) {
1147 $message .= '<tr style="padding: 0px; vertical-align: top;">';
1148 if (!empty($field['name']) && $field['name'] !== esc_html__('HTML / Code Block', 'wpforms-lite')) {
1149 $message .= '<td style="overflow-wrap: break-word; vertical-align: top; font-weight: normal; padding: 25px 10px 25px 0px; margin: 0px; font-size: 15px; color: rgb(51, 51, 51); border-bottom: 1px solid rgb(226, 226, 226); min-width: 113px; line-height: 22px; border-collapse: collapse;">
1150 <strong style="margin-bottom: 0px;">' . esc_html($field['name']) . '</strong>
1151 </td>';
1152 $message .= '<td valign="middle" style="overflow-wrap: break-word; font-weight: normal; padding: 25px 0px; margin: 0px; font-size: 15px; color: rgb(51, 51, 51);line-height: 20px; border-bottom: 1px solid rgb(226, 226, 226); vertical-align: middle; border-collapse: collapse;"> ' . $field_val . ' </td>';
1153 } else {
1154 $message .= '<td colspan="2" style="overflow-wrap: break-word; font-weight: normal; padding: 15px 0px; margin: 0px; font-size: 15px; color: rgb(51, 51, 51); border-bottom: 1px solid rgb(226, 226, 226); line-height: 20px; vertical-align: middle; border-collapse: collapse;"> ' . $field_val . ' </td>';
1155 }
1156 $message .= '</tr>';
1157 continue;
1158 }
1159
1160 if (in_array($field_type, ['divider', 'pagebreak'], true)) {
1161 $message .= '<tr style="padding: 0px; vertical-align: top;">';
1162 $message .= '<td colspan="2" style="text-align: center; overflow-wrap: break-word; font-weight: bold; padding: 15px 0px; margin: 0px; font-size: 15px; color: rgb(51, 51, 51); border-bottom: 1px solid rgb(226, 226, 226); line-height: 20px; border-collapse: collapse;">' . $field_name . (!empty($field_val) ? '<div style="font-weight:normal;font-size:13px;color:#777;margin-top:5px;">' . $field_val . '</div>' : '') . '</td>';
1163 $message .= '</tr>';
1164 continue;
1165 }
1166 }
1167
1168 // Set a default field name if empty.
1169 if (empty($field_name) && $field_name !== null) {
1170 $field_name = $this->get_default_field_name($field_id);
1171 }
1172 /** This filter is documented in src/SmartTags/SmartTag/FieldHtmlId.php.*/
1173 $field_val = apply_filters( // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
1174 'wpforms_html_field_value',
1175 $field_val,
1176 isset($this->fields[$field_id]) ? $this->fields[$field_id] : $field,
1177 $this->form_data,
1178 'email-html'
1179 );
1180 // Replace new lines with <br/> tags.
1181 $field_val = str_replace(["\r\n", "\r", "\n"], '<br/>', $field_val);
1182 // Append the field item to the message.
1183 $message .= '<tr style="padding: 0px; vertical-align: top;">';
1184 $message .= '<td style="overflow-wrap: break-word; vertical-align: top; font-weight: normal; padding: 25px 10px 25px 0px; margin: 0px; font-size: 15px; color: rgb(51, 51, 51); border-bottom: 1px solid rgb(226, 226, 226); min-width: 113px; line-height: 22px; border-collapse: collapse;">
1185 <strong style="margin-bottom: 0px;">' . $field_name . '</strong>
1186 </td>';
1187 $message .= '<td valign="middle" style="overflow-wrap: break-word; font-weight: normal; padding: 25px 0px; margin: 0px; font-size: 15px; color: rgb(51, 51, 51);line-height: 20px; border-bottom: 1px solid rgb(226, 226, 226); vertical-align: middle; border-collapse: collapse;"> ' . $field_val . ' </td>';
1188 $message .= '</tr>';
1189 }
1190 return $message . '</table>';
1191 }
1192 public function get_default_field_name($field_id)
1193 {
1194 return sprintf( /* translators: %1$d - field ID. */
1195 esc_html__('Field ID #%1$d', 'wpforms-lite'),
1196 absint($field_id)
1197 );
1198 }
1199 public function process_special_field_values($field)
1200 { // phpcs:ignore Generic.Metrics.CyclomaticComplexity
1201 $field_name = null;
1202 $field_val = null;
1203 // Use a switch-case statement to handle specific field types.
1204 switch ($field['type']) {
1205 case 'divider':
1206 $field_name = !empty($field['label']) ? str_repeat('&mdash;', 3) . ' ' . $field['label'] . ' ' . str_repeat('&mdash;', 3) : null;
1207 $field_val = !empty($field['description']) ? $field['description'] : '';
1208 break;
1209 case 'pagebreak':
1210 // Skip if position is 'bottom'.
1211 if (!empty($field['position']) && $field['position'] === 'bottom') {
1212 break;
1213 }
1214 $title = !empty($field['title']) ? $field['title'] : esc_html__('Page Break', 'wpforms-lite');
1215 $field_name = str_repeat('&mdash;', 6) . ' ' . $title . ' ' . str_repeat('&mdash;', 6);
1216 break;
1217 case 'html':
1218 // Skip if the field is conditionally hidden.
1219 if ($this->is_field_conditionally_hidden($field['id'])) {
1220 break;
1221 }
1222 $field_name = !empty($field['name']) ? $field['name'] : esc_html__('HTML / Code Block', 'wpforms-lite');
1223 $field_val = $field['code'];
1224 break;
1225 case 'content':
1226 // Skip if the field is conditionally hidden.
1227 if ($this->is_field_conditionally_hidden($field['id'])) {
1228 break;
1229 }
1230 $field_name = esc_html__('Content', 'wpforms-lite');
1231 $field_val = $field['content'];
1232 break;
1233 default:
1234 $field_name = '';
1235 $field_val = '';
1236 break;
1237 }
1238 return [$field_name, $field_val];
1239 }
1240 public function is_field_conditionally_hidden($field_id)
1241 {
1242 return !empty($this->form_data['fields'][$field_id]['conditionals']) && !wpforms_conditional_logic_fields()->field_is_visible($this->form_data, $field_id);
1243 }
1244 public function wrap_content_with_table_row($content, $form_data, $fields, $entry_id)
1245 { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh
1246 // If the content is empty, return it as is.
1247 if (empty($content)) {
1248 return $content;
1249 }
1250 // Process the smart tags in the content.
1251 $processed_content = $this->process_tag($content, $form_data, $fields, $entry_id);
1252 // If the content doesn't contain any smart tags, wrap it in a table row, and return early.
1253 // Don't go beyond this point if the content doesn't contain any smart tags.
1254 if (!preg_match('/{\w+}/', $processed_content)) {
1255 //return '<tr class="smart-tag"><td class="field-name field-value" colspan="2">' . $processed_content . '</td></tr>';
1256 return $processed_content;
1257 }
1258 // Split the content into lines and remove empty lines.
1259 $lines = array_filter(explode("\n", $content), 'strlen');
1260 // Initialize an empty string to store the modified content.
1261 $modified_content = '';
1262 // Iterate through each line.
1263 foreach ($lines as $line) {
1264 // Trim the line.
1265 $trimmed_line = $this->process_tag(trim($line), $form_data, $fields, $entry_id);
1266 // Extract tags at the beginning of the line.
1267 preg_match('/^(?:\{[^}]+}\s*)+/i', $trimmed_line, $before_line_tags);
1268 if (!empty($before_line_tags[0])) {
1269 // Include the extracted tags at the beginning to the modified content.
1270 $modified_content .= trim($before_line_tags[0]);
1271 // Remove the extracted tags from the trimmed line.
1272 $trimmed_line = trim(substr($trimmed_line, strlen($before_line_tags[0])));
1273 }
1274 // Extract all smart tags from the remaining content.
1275 preg_match_all('/\{([^}]+)}/i', $trimmed_line, $after_line_tags);
1276 // Remove the smart tags from the content.
1277 $content_without_smart_tags = str_replace($after_line_tags[0], '', $trimmed_line);
1278 if (!empty($content_without_smart_tags)) {
1279 // Wrap the content without the smart tags in a new table row.
1280 //$modified_content .= '<tr class="smart-tag"><td class="field-name field-value" colspan="2">' . $content_without_smart_tags . '</td></tr>';
1281 $modified_content .= $content_without_smart_tags;
1282 }
1283 if (!empty($after_line_tags[0])) {
1284 // Move all smart tags to the end of the line after the closing </tr> tag.
1285 $modified_content .= implode(' ', $after_line_tags[0]);
1286 }
1287 }
1288 // Return the modified content.
1289 return $modified_content;
1290 }
1291 public function process_tag($input = '', $form_data = '', $fields = '', $entry_id = '')
1292 {
1293 return wpforms_process_smart_tags($input, $form_data, $fields, $entry_id);
1294 }
1295 }
1296 $yeepdf_settings_wpforms = new Superaddons_Pdf_Creator_Wpfroms_Backend;
1297