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 / MetformEmailSettings.php

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

280 lines 13.8 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($form_id, $mf_template_type);
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 <?php
96 printf(
97 /* translators: %s: "EmailKit Pro" product name, wrapped in a <strong> tag. */
98 esc_html__( 'Get %s - the drag-and-drop builder to customize your confirmation emails.', 'emailkit' ),
99 '<strong>' . esc_html__( 'EmailKit Pro', 'emailkit' ) . '</strong>'
100 );
101 ?>
102 </span>
103 <a class="upgrade-button" target="_blank" href="https://wpmet.com/plugin/emailkit/pricing/">
104 <span><svg xmlns="http://www.w3.org/2000/svg" width="13" height="14" viewBox="0 0 13 14" fill="none">
105 <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" />
106 <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" />
107 </svg>
108 </span> <?php esc_html_e( 'Upgrade to Pro', 'emailkit' ); ?>
109 </a>
110 </div>
111 <div id="dummy-emailkit-metform-button-wrap">
112 <button disabled class="dummy-emailkit-metform-btn">
113 <span>
114 <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" fill="none">
115 <path d="M8 14.3164H15" stroke="#0D1427" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
116 <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" />
117 </svg>
118 </span>
119 <?php echo esc_html(__('Edit With Emailkit', 'emailkit')); ?>
120 </button>
121 <div class="dummy-info-tooltip">
122 <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none">
123 <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" />
124 <path d="M9 12.2V9" stroke="#54565C" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
125 <path d="M9 5.80005H9.008" stroke="#54565C" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
126 </svg>
127 </div>
128 </div>
129 </div>
130 <div id="emailkit-metform-button-wrap">
131 <button class="emailkit-metform-btn metform-go-to-emailkit-builder-btn emailkit-metform-edit-button"
132 target="_blank"
133 href="<?php echo esc_url($builder_url) ?>"
134 data-editor-template-url="<?php echo esc_attr($demo_url); ?>"
135 data-emailkit-email-type="<?php echo esc_attr($emailkit_email_type); ?>"
136 data-emailkit-template-title="<?php echo esc_attr($emailkit_template_title); ?>"
137 data-emailkit-template-type="<?php echo esc_attr($template_type); ?>"
138 data-emailkit-form="<?php echo esc_attr($form_id); ?>"
139 data-emailkit-template="<?php echo esc_attr($emailkit_email_type); ?>&emailkit_template_type=<?php echo esc_attr($emailkit_template_title); ?>">
140 <span>
141 <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" fill="none">
142 <path d="M8 14.3164H15" stroke="#0D1427" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
143 <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" />
144 </svg>
145 </span>
146 <?php echo esc_html(__('Edit With Emailkit', 'emailkit')); ?>
147 </button>
148 <div class="info-tooltip">
149 <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none">
150 <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" />
151 <path d="M9 12.2V9" stroke="#54565C" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
152 <path d="M9 5.80005H9.008" stroke="#54565C" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
153 </svg>
154 <!-- <div class="icon">ℹ️</div> -->
155 <div class="metform-info-tooltip-text">
156 <?php esc_html_e( "Customize your form confirmation email with EmailKit's drag and drop builder.", 'emailkit' ); ?>
157 </div>
158
159 </div>
160 </div>
161 <?php
162 }
163 /**
164 * Get EmailKit post ID based on template type
165 */
166 public function get_emailkit_post_id($form_id, $template_type)
167 {
168 $args = [
169 'post_type' => 'emailkit',
170 'posts_per_page' => 1,
171 'fields' => 'ids', // Only retrieve post IDs for efficiency
172 'meta_query' => [
173 'relation' => 'OR',
174 [
175 'relation' => 'AND',
176 [
177 'key' => 'emailkit_template_type',
178 'value' => $template_type,
179 'compare' => '='
180 ],
181 [
182 'key' => 'emailkit_template_status',
183 'value' => 'active',
184 'compare' => '='
185 ]
186 ]
187 ]
188 ];
189
190 $query = new WP_Query($args);
191 $post_ids = $query->posts;
192
193 return !empty($post_ids) ? $post_ids[0] : null;
194 }
195
196 /**
197 * Find EmailKit template based on template type
198 */
199 public function find_emailkit_template($template_type)
200 {
201 $templates = TemplateList::get_templates();
202
203 // Check if this is a form-specific template
204 $is_form_template = strpos($template_type, 'metform_form_') === 0;
205 $form_id = $is_form_template ? str_replace('metform_form_', '', $template_type) : null;
206
207 foreach ($templates as $key => $value) {
208 // For form templates, match by ID
209 if ($is_form_template && isset($value['id']) && $value['id'] == $form_id) {
210 return [
211 'file' => $value['file'] ?? '',
212 'mail_type' => 'metform',
213 'template_type' => $template_type,
214 'title' => $value['template_title'] ?? esc_html__('Confirmation Mail To User', 'emailkit'),
215 ];
216 }
217 }
218
219 // Fallback to first available metform template if no exact match
220 foreach ($templates as $value) {
221 if (($value['mail_type'] ?? '') === 'metform') {
222 return [
223 'file' => $value['file'] ?? '',
224 'mail_type' => 'metform',
225 'template_type' => $template_type,
226 'title' => $value['template_title'] ?? esc_html__('Confirmation Mail To User', 'emailkit'),
227 ];
228 }
229 }
230
231 return [];
232 }
233
234 /**
235 * Customize the MetForm user email body using an EmailKit template
236 *
237 * @param string $body The default email body
238 * @param int $form_id The ID of the MetForm form
239 * @param array $form_data The submitted form data
240 * @param array $file_info The uploaded file information
241 * @return string The modified email body
242 */
243 public function use_emailkit_template_for_user_email($body, $form_id, $form_data, $file_info, $form_settings)
244 {
245
246 // Define the template type
247 $template_type = 'metform_form_' . $form_id;
248
249 // Get the post_id using the reusable method
250 $post_id = $this->get_emailkit_post_id($form_id, $template_type);
251
252 // If no form-specific template exists, return original MetForm content
253 if (!$post_id) {
254 return $body;
255 }
256
257 // Retrieve the EmailKit template HTML from post meta
258 $template_html = get_post_meta($post_id, 'emailkit_template_content_html', true);
259
260 // If we have template content, use it
261 if (!empty($template_html)) {
262 $template_content = $template_html;
263
264 // Append formatted form data if the "attach submission copy" setting is enabled
265 if (
266 isset($form_settings['user_email_attach_submission_copy']) &&
267 $form_settings['user_email_attach_submission_copy'] === '1'
268 ) {
269 $form_html = \MetForm\Core\Entries\Form_Data::format_data_for_mail($form_id, $form_data, $file_info);
270 $template_content .= $form_html;
271 }
272
273 return $template_content;
274 }
275
276 // Fallback to original MetForm content if no template content found
277 return $body;
278 }
279 }
280