PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.6.9
EmailKit – Email Customizer for WooCommerce & WP v1.6.9
1.6.9 1.6.8 1.6.7 trunk 1.0.0 1.2.0 1.4.0 1.4.5 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6
emailkit / includes / Admin / EmailSettings / MetformShortcodes.php

MetformShortcodes.php in EmailKit – Email Customizer for WooCommerce & WP 1.6.9, at includes/Admin/EmailSettings/MetformShortcodes.php

184 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace EmailKit\Admin\EmailSettings;
4
5 use WP_Query;
6
7 defined('ABSPATH') || exit;
8
9 /**
10 * Simple MetForm Shortcode Processor for EmailKit
11 *
12 * Handles MetForm shortcode replacement in EmailKit email templates.
13 * Supports {{}} and [] bracket formats.
14 *
15 * @since 1.0.0
16 */
17 class MetformShortcodes
18 {
19 /**
20 * Form data for shortcode processing
21 *
22 * @var array
23 */
24 private $form_data = [];
25
26 /**
27 * Initialize the shortcode processor
28 */
29 public function __construct()
30 {
31 add_filter('metform_confirmation_user_email_body', [$this, 'process_shortcodes'], 15, 5);
32 }
33
34 /**
35 * Process MetForm shortcodes in EmailKit email templates
36 *
37 * @param string $body Email body content
38 * @param int $form_id Form ID
39 * @param array $form_data Form submission data
40 * @param array $file_info File upload info
41 * @param array $form_settings Form settings
42 * @return string Processed email body
43 */
44 public function process_shortcodes($body, $form_id, $form_data, $file_info, $form_settings)
45 {
46 if (!class_exists('\\MetForm\\Plugin') || empty($form_data)) {
47 return $body;
48 }
49
50 // Check if EmailKit template exists for this form
51 $template_type = 'metform_form_' . $form_id;
52 $post_id = $this->get_emailkit_post_id($form_id, $template_type);
53
54 // Only process shortcodes if EmailKit template exists
55 if (!$post_id) {
56 // No EmailKit template found, return original MetForm body without processing
57 return $body;
58 }
59
60 // EmailKit template exists, get template content and process shortcodes
61 $this->form_data = $form_data;
62 $emailkit_body = $this->use_emailkit_template_for_user_email($body, $form_id, $form_data, $file_info, $form_settings);
63
64 // Process shortcodes in EmailKit template
65 $processed_body = $this->replace_shortcodes($emailkit_body);
66
67 return $processed_body;
68 }
69
70 /**
71 * Replace all MetForm shortcodes with form data values
72 *
73 * @param string $body Email body content
74 * @return string Processed email body
75 */
76 private function replace_shortcodes($body)
77 {
78 // Replace direct field shortcodes (both formats)
79 foreach ($this->form_data as $field_name => $field_value) {
80 if (empty($field_value)) {
81 continue;
82 }
83
84 $value = is_array($field_value) ? implode(', ', $field_value) : $field_value;
85 $escaped_value = esc_html($value);
86
87 // Replace {{field}} and [field] formats
88 $body = str_replace('{{' . $field_name . '}}', $escaped_value, $body);
89 $body = str_replace('[' . $field_name . ']', $escaped_value, $body);
90
91
92 // Replace EmailKit span-wrapped shortcodes
93 $pattern = '/<span data-shortcode="\{\{' . preg_quote($field_name, '/') . '\}\}">([^<]*)<\/span>/';
94 $replacement = '<span data-shortcode="{{' . $field_name . '}}">' . $escaped_value . '</span>';
95 $body = preg_replace($pattern, $replacement, $body);
96 }
97
98 return $body;
99 }
100
101 /**
102 * Use EmailKit template for user email if available
103 *
104 * @param string $body Email body content
105 * @param int $form_id Form ID
106 * @param array $form_data Form submission data
107 * @param array $file_info File upload info
108 * @param array $form_settings Form settings
109 * @return string Processed email body
110 */
111 public function use_emailkit_template_for_user_email($body, $form_id, $form_data, $file_info, $form_settings)
112 {
113 // Define the template type
114 $template_type = 'metform_form_' . $form_id;
115
116 // Get the post_id using the reusable method
117 $post_id = $this->get_emailkit_post_id($form_id, $template_type);
118
119 // If no form-specific template exists, return original MetForm content
120 if (!$post_id) {
121 return $body;
122 }
123
124 // Retrieve the EmailKit template HTML from post meta
125 $template_html = get_post_meta($post_id, 'emailkit_template_content_html', true);
126
127 // If we have template content, use it
128 if (!empty($template_html)) {
129 $template_content = $template_html;
130
131 // Append formatted form data if the "attach submission copy" setting is enabled
132 if (
133 isset($form_settings['user_email_attach_submission_copy']) &&
134 $form_settings['user_email_attach_submission_copy'] === '1'
135 ) {
136 $form_html = \MetForm\Core\Entries\Form_Data::format_data_for_mail($form_id, $form_data, $file_info);
137 $template_content .= $form_html;
138 }
139
140 return $template_content;
141 }
142
143 // Fallback to original MetForm content if no template content found
144 return $body;
145 }
146
147 /**
148 * Get EmailKit post ID based on template type
149 *
150 * @param int $form_id Form ID
151 * @param string $template_type Template type
152 * @return int|null Post ID or null if not found
153 */
154 public function get_emailkit_post_id($form_id, $template_type)
155 {
156 $args = [
157 'post_type' => 'emailkit',
158 'posts_per_page' => 1,
159 'fields' => 'ids', // Only retrieve post IDs for efficiency
160 'meta_query' => [
161 'relation' => 'OR',
162 [
163 'relation' => 'AND',
164 [
165 'key' => 'emailkit_template_type',
166 'value' => $template_type,
167 'compare' => '='
168 ],
169 [
170 'key' => 'emailkit_template_status',
171 'value' => 'active',
172 'compare' => '='
173 ]
174 ]
175 ]
176 ];
177
178 $query = new WP_Query($args);
179 $post_ids = $query->posts;
180
181 return !empty($post_ids) ? $post_ids[0] : null;
182 }
183
184 }