PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.6.0
EmailKit – Email Customizer for WooCommerce & WP v1.6.0
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 / MetformEmailSettings.php

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

274 lines 13.4 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 use EmailKit\Admin\TemplateList;
7 use EmailKit\Admin\Emails\Helpers\Utils;
8
9 defined('ABSPATH') || exit;
10
11 class MetformEmailSettings
12 {
13
14 public function __construct()
15 {
16
17 add_action('after_confirmation_mail_to_user_switch', [$this, 'metform_settings_content'], 10, 1);
18 add_action('admin_enqueue_scripts', [$this, 'enqueue_metform_scripts']);
19 add_filter('metform_confirmation_user_email_body', [$this, 'use_emailkit_template_for_user_email'], 10, 5);
20 }
21
22 /**
23 * Enqueue JavaScript for MetForm EmailKit integration
24 */
25 public function enqueue_metform_scripts()
26 {
27
28 wp_enqueue_script("emailkit-admin-mf-js", EMAILKIT_ADMIN . 'EmailSettings/MFintegration.js', ['jquery'], EMAILKIT_VERSION, true);
29 wp_localize_script(
30 'emailkit-admin-mf-js',
31 'metform',
32 array(
33 'ajaxurl' => admin_url('admin-ajax.php'),
34 'nonce' => wp_create_nonce('emailkit_nonce'),
35 'rest_url' => esc_url(get_rest_url(null, 'emailkit/v1/')),
36 'rest_nonce' => wp_create_nonce('wp_rest')
37 )
38 );
39 }
40
41 /**
42 * Add EmailKit settings content to MetForm notification settings
43 */
44 public function metform_settings_content($email)
45 {
46 $form_id = isset($email['id']) ? $email['id'] : 0;
47 $mf_template_type = 'metform_form_' . $form_id; // Use form-specific template type
48
49 $builder_url = '';
50 $demo_url = '';
51 $emailkit_email_type = '';
52 $emailkit_template_title = '';
53 $template_type = '';
54
55 // Check for existing template with form-specific type
56 $post_id = $this->get_emailkit_post_id($mf_template_type, $form_id);
57
58 // If no form-specific template exists, check for generic metform template
59 if (null === $post_id) {
60 $emailkit_template = $this->find_emailkit_template($mf_template_type);
61
62 if (isset($emailkit_template)) {
63 $demo_url = $emailkit_template['file'] ?? '';
64 $emailkit_email_type = $emailkit_template['mail_type'] ?? '';
65 $emailkit_template_title = $emailkit_template['title'] ?? '';
66 $template_type = $emailkit_template['template_type'] ?? '';
67 }
68 } else {
69 $builder_url = admin_url("post.php?post={$post_id}&action=emailkit-builder");
70 }
71
72 $this->render_emailkit_edit_btn($builder_url, $demo_url, $emailkit_email_type, $emailkit_template_title, $template_type, $form_id);
73 }
74
75
76 /**
77 * Render the EmailKit edit button for MetForm
78 *
79 * @param string $builder_url The URL to the EmailKit builder for the specific template
80 * @param string $demo_url The URL to the demo template
81 * @param string $emailkit_email_type The type of email for EmailKit
82 * @param string $emailkit_template_title The title of the EmailKit template
83 * @param string $template_type The type of template (e.g., 'metform_form_123')
84 * @param int $form_id The ID of the MetForm form
85 *
86 * @return void
87 */
88 public function render_emailkit_edit_btn($builder_url, $demo_url, $emailkit_email_type, $emailkit_template_title, $template_type, $form_id)
89
90 {
91 ?>
92 <div id="need-emailkit-pro-btn-wrap" class="emailkit-metform-btn need-emailkit-pro-btn">
93 <div class="emailkit-upgrade-notice">
94 <span class="emailkit-upgrade-text">
95 Get <strong>EmailKit Pro</strong> - the drag-and-drop builder to <br> customize your confirmation emails.
96 </span>
97 <a class="upgrade-button" target="_blank" href="https://wpmet.com/plugin/metform/pricing/">
98 <span><svg xmlns="http://www.w3.org/2000/svg" width="13" height="14" viewBox="0 0 13 14" fill="none">
99 <path d="M10.6 6.3999H2.2C1.53726 6.3999 1 6.93716 1 7.5999V11.7999C1 12.4626 1.53726 12.9999 2.2 12.9999H10.6C11.2627 12.9999 11.8 12.4626 11.8 11.7999V7.5999C11.8 6.93716 11.2627 6.3999 10.6 6.3999Z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
100 <path d="M3.40015 6.4V4C3.40015 3.20435 3.71622 2.44129 4.27883 1.87868C4.84144 1.31607 5.6045 1 6.40015 1C7.1958 1 7.95886 1.31607 8.52147 1.87868C9.08408 2.44129 9.40015 3.20435 9.40015 4V6.4" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
101 </svg>
102 </span> Upgrade to Pro
103 </a>
104 </div>
105 <div id="dummy-emailkit-metform-button-wrap">
106 <button disabled class="dummy-emailkit-metform-btn">
107 <span>
108 <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" fill="none">
109 <path d="M8 14.3164H15" stroke="#0D1427" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
110 <path d="M11.5 1.48325C11.8094 1.17383 12.2291 1 12.6667 1C12.8833 1 13.0979 1.04268 13.2981 1.12559C13.4982 1.20851 13.6801 1.33004 13.8333 1.48325C13.9865 1.63646 14.1081 1.81834 14.191 2.01852C14.2739 2.2187 14.3166 2.43325 14.3166 2.64992C14.3166 2.86659 14.2739 3.08113 14.191 3.28131C14.1081 3.48149 13.9865 3.66337 13.8333 3.81658L4.11111 13.5388L1 14.3166L1.77778 11.2055L11.5 1.48325Z" stroke="#0D1427" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
111 </svg>
112 </span>
113 <?php echo esc_html(__('Edit With Emailkit', 'emailkit')); ?>
114 </button>
115 <div class="dummy-info-tooltip">
116 <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none">
117 <path d="M9 17C13.4183 17 17 13.4183 17 9C17 4.58172 13.4183 1 9 1C4.58172 1 1 4.58172 1 9C1 13.4183 4.58172 17 9 17Z" stroke="#54565C" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
118 <path d="M9 12.2V9" stroke="#54565C" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
119 <path d="M9 5.80005H9.008" stroke="#54565C" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
120 </svg>
121 </div>
122 </div>
123 </div>
124 <div id="emailkit-metform-button-wrap">
125 <button class="emailkit-metform-btn metform-go-to-emailkit-builder-btn emailkit-metform-edit-button"
126 target="_blank"
127 href="<?php echo esc_url($builder_url) ?>"
128 data-editor-template-url="<?php echo esc_attr($demo_url); ?>"
129 data-emailkit-email-type="<?php echo esc_attr($emailkit_email_type); ?>"
130 data-emailkit-template-title="<?php echo esc_attr($emailkit_template_title); ?>"
131 data-emailkit-template-type="<?php echo esc_attr($template_type); ?>"
132 data-emailkit-form="<?php echo esc_attr($form_id); ?>"
133 data-emailkit-template="<?php echo esc_attr($emailkit_email_type); ?>&emailkit_template_type=<?php echo esc_attr($emailkit_template_title); ?>">
134 <span>
135 <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" fill="none">
136 <path d="M8 14.3164H15" stroke="#0D1427" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
137 <path d="M11.5 1.48325C11.8094 1.17383 12.2291 1 12.6667 1C12.8833 1 13.0979 1.04268 13.2981 1.12559C13.4982 1.20851 13.6801 1.33004 13.8333 1.48325C13.9865 1.63646 14.1081 1.81834 14.191 2.01852C14.2739 2.2187 14.3166 2.43325 14.3166 2.64992C14.3166 2.86659 14.2739 3.08113 14.191 3.28131C14.1081 3.48149 13.9865 3.66337 13.8333 3.81658L4.11111 13.5388L1 14.3166L1.77778 11.2055L11.5 1.48325Z" stroke="#0D1427" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
138 </svg>
139 </span>
140 <?php echo esc_html(__('Edit With Emailkit', 'emailkit')); ?>
141 </button>
142 <div class="info-tooltip">
143 <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none">
144 <path d="M9 17C13.4183 17 17 13.4183 17 9C17 4.58172 13.4183 1 9 1C4.58172 1 1 4.58172 1 9C1 13.4183 4.58172 17 9 17Z" stroke="#54565C" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
145 <path d="M9 12.2V9" stroke="#54565C" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
146 <path d="M9 5.80005H9.008" stroke="#54565C" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
147 </svg>
148 <!-- <div class="icon">ℹ️</div> -->
149 <div class="metform-info-tooltip-text">
150 Customize your form confirmation email with EmailKit's drag and drop builder.
151 </div>
152
153 </div>
154 </div>
155 <?php
156 }
157 /**
158 * Get EmailKit post ID based on template type
159 */
160 public function get_emailkit_post_id($form_id, $template_type)
161 {
162 $args = [
163 'post_type' => 'emailkit',
164 'posts_per_page' => 1,
165 'fields' => 'ids', // Only retrieve post IDs for efficiency
166 'meta_query' => [
167 'relation' => 'OR',
168 [
169 'relation' => 'AND',
170 [
171 'key' => 'emailkit_template_type',
172 'value' => $template_type,
173 'compare' => '='
174 ],
175 [
176 'key' => 'emailkit_template_status',
177 'value' => 'active',
178 'compare' => '='
179 ]
180 ]
181 ]
182 ];
183
184 $query = new WP_Query($args);
185 $post_ids = $query->posts;
186
187 return !empty($post_ids) ? $post_ids[0] : null;
188 }
189
190 /**
191 * Find EmailKit template based on template type
192 */
193 public function find_emailkit_template($template_type)
194 {
195 $templates = TemplateList::get_templates();
196
197 // Check if this is a form-specific template
198 $is_form_template = strpos($template_type, 'metform_form_') === 0;
199 $form_id = $is_form_template ? str_replace('metform_form_', '', $template_type) : null;
200
201 foreach ($templates as $key => $value) {
202 // For form templates, match by ID
203 if ($is_form_template && isset($value['id']) && $value['id'] == $form_id) {
204 return [
205 'file' => $value['file'] ?? '',
206 'mail_type' => 'metform',
207 'template_type' => $template_type,
208 'title' => $value['template_title'] ?? esc_html__('Confirmation Mail To User', 'emailkit'),
209 ];
210 }
211 }
212
213 // Fallback to first available metform template if no exact match
214 foreach ($templates as $value) {
215 if (($value['mail_type'] ?? '') === 'metform') {
216 return [
217 'file' => $value['file'] ?? '',
218 'mail_type' => 'metform',
219 'template_type' => $template_type,
220 'title' => $value['template_title'] ?? esc_html__('Confirmation Mail To User', 'emailkit'),
221 ];
222 }
223 }
224
225 return [];
226 }
227
228 /**
229 * Customize the MetForm user email body using an EmailKit template
230 *
231 * @param string $body The default email body
232 * @param int $form_id The ID of the MetForm form
233 * @param array $form_data The submitted form data
234 * @param array $file_info The uploaded file information
235 * @return string The modified email body
236 */
237 public function use_emailkit_template_for_user_email($body, $form_id, $form_data, $file_info, $form_settings)
238 {
239
240 // Define the template type
241 $template_type = 'metform_form_' . $form_id;
242
243 // Get the post_id using the reusable method
244 $post_id = $this->get_emailkit_post_id($form_id, $template_type);
245
246 // If no form-specific template exists, return original MetForm content
247 if (!$post_id) {
248 return $body;
249 }
250
251 // Retrieve the EmailKit template HTML from post meta
252 $template_html = get_post_meta($post_id, 'emailkit_template_content_html', true);
253
254 // If we have template content, use it
255 if (!empty($template_html)) {
256 $template_content = $template_html;
257
258 // Append formatted form data if the "attach submission copy" setting is enabled
259 if (
260 isset($form_settings['user_email_attach_submission_copy']) &&
261 $form_settings['user_email_attach_submission_copy'] === '1'
262 ) {
263 $form_html = \MetForm\Core\Entries\Form_Data::format_data_for_mail($form_id, $form_data, $file_info);
264 $template_content .= $form_html;
265 }
266
267 return $template_content;
268 }
269
270 // Fallback to original MetForm content if no template content found
271 return $body;
272 }
273 }
274