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 / Api / CheckForm.php

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

439 lines 16.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\Api;
4
5 use WP_Error;
6 use WP_REST_Response;
7
8 defined('ABSPATH') || exit;
9
10 class CheckForm
11 {
12 public function __construct()
13 {
14 add_action('rest_api_init', function () {
15 // Check if template exists
16 register_rest_route('emailkit/v1', 'check-template', [
17 'methods' => 'GET',
18 'callback' => [$this, 'check_template'],
19 'permission_callback' => function () {
20 return current_user_can('edit_posts');
21 },
22 ]);
23
24 // Create new template
25 register_rest_route('emailkit/v1', 'create-template', [
26 'methods' => 'POST',
27 'callback' => [$this, 'create_template'],
28 'permission_callback' => function () {
29 return current_user_can('edit_posts');
30 },
31 ]);
32
33 // Check if PopupKit template exists
34 register_rest_route('emailkit/v1', 'check-popup-template', [
35 'methods' => 'GET',
36 'callback' => [$this, 'check_popup_template'],
37 'permission_callback' => function () {
38 return current_user_can('edit_posts');
39 },
40 ]);
41
42 // Create new PopupKit template
43 register_rest_route('emailkit/v1', 'create-popup-template', [
44 'methods' => 'POST',
45 'callback' => [$this, 'create_popup_template'],
46 'permission_callback' => function () {
47 return current_user_can('edit_posts');
48 },
49 ]);
50 });
51 }
52
53 public function check_template($request) {
54 if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
55 return [
56 'status' => 'fail',
57 'message' => [__('Nonce mismatch.', 'emailkit')]
58 ];
59 }
60
61 if (!is_user_logged_in() || !current_user_can('manage_options')) {
62 return [
63 'status' => 'fail',
64 'message' => [__('Access denied.', 'emailkit')]
65 ];
66 }
67
68 $form_id = $request->get_param('form_id');
69 $template_type = 'metform_form_' . $form_id;
70
71 $args = [
72 'post_type' => 'emailkit',
73 'meta_query' => [
74 'relation' => 'OR',
75 [
76 'relation' => 'AND',
77 [
78 'key' => 'emailkit_template_type',
79 'value' => $template_type,
80 'compare' => '='
81 ],
82 [
83 'key' => 'emailkit_template_status',
84 'value' => 'active',
85 'compare' => '='
86 ]
87 ]
88 ],
89 'posts_per_page' => 1,
90 'fields' => 'ids'
91 ];
92
93 $existing_templates = get_posts($args);
94
95 if (!empty($existing_templates)) {
96 $template_id = $existing_templates[0];
97 return new WP_REST_Response([
98 'success' => true,
99 'data' => [
100 'exists' => true,
101 'builder_url' => admin_url("post.php?post={$template_id}&action=emailkit-builder")
102 ]
103 ], 200);
104 } else {
105 $args = [
106 'post_type' => 'emailkit',
107 'meta_query' => [
108 'relation' => 'OR',
109 [
110 'relation' => 'AND',
111 [
112 'key' => 'emailkit_template_type',
113 'value' => $template_type,
114 'compare' => '='
115 ]
116 ]
117 ],
118 'posts_per_page' => 1,
119 'fields' => 'ids'
120 ];
121
122 $existing_templates = get_posts($args);
123
124 if (!empty($existing_templates)) {
125 $template_id = $existing_templates[0];
126 return new WP_REST_Response([
127 'success' => true,
128 'data' => [
129 'exists' => true,
130 'builder_url' => admin_url("post.php?post={$template_id}&action=emailkit-builder")
131 ]
132 ], 200);
133 }
134 }
135
136 return new WP_REST_Response([
137 'success' => true,
138 'data' => [
139 'exists' => false,
140 ]
141 ], 200);
142 }
143
144 public function create_template($request) {
145 // Verify nonce and permissions
146 if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
147 return [
148 'status' => 'fail',
149 'message' => [__('Nonce mismatch.', 'emailkit')]
150 ];
151 }
152
153 if (!is_user_logged_in() || !current_user_can('publish_posts')) {
154 return [
155 'status' => 'fail',
156 'message' => [__('Access denied.', 'emailkit')]
157 ];
158 }
159
160 $form_id = $request->get_param('form_id');
161 $form_title = $request->get_param('form_title') ?? __('MetForm', 'emailkit');
162 $template_type = 'metform_form_' . $form_id;
163 $template_title = $request->get_param('template_title');
164
165 // First check for existing templates (both old and new format)
166 $existing_template = $this->get_existing_template($form_id);
167 if ($existing_template) {
168 return new WP_REST_Response([
169 'success' => false,
170 'message' => __('A template already exists for this form.', 'emailkit'),
171 'data' => [
172 'builder_url' => admin_url("post.php?post={$existing_template}&action=emailkit-builder")
173 ]
174 ], 400);
175 }
176
177 // Get template content
178 $template = '';
179 $html = '';
180 if (!empty($request->get_param('emailkit-editor-template')) && trim($request->get_param('emailkit-editor-template')) !== '') {
181 $template_path = $request->get_param('emailkit-editor-template');
182 $real_path = realpath($template_path);
183 $allowed_base_paths = [
184 wp_upload_dir()['basedir'] . '/emailkit/templates/',
185 EMAILKIT_DIR . 'includes/templates/',
186 ];
187
188 $real_allowed_base_paths = [];
189 foreach ($allowed_base_paths as $allowed_base_path) {
190 $real_allowed_base_path = realpath($allowed_base_path);
191 if ($real_allowed_base_path !== false) {
192 $real_allowed_base_paths[] = $real_allowed_base_path;
193 }
194 }
195
196 if ($real_path === false || empty($real_allowed_base_paths)) {
197 return new WP_REST_Response(['success' => false, 'message' => __('Invalid template path', 'emailkit')], 400);
198 }
199
200 $is_allowed_path = false;
201 foreach ($real_allowed_base_paths as $real_allowed_base_path) {
202 if (strpos($real_path, $real_allowed_base_path) === 0) {
203 $is_allowed_path = true;
204 break;
205 }
206 }
207
208 if (!$is_allowed_path) {
209 return new WP_REST_Response(['success' => false, 'message' => __('Invalid template path', 'emailkit')], 400);
210 }
211
212 $template = file_exists($real_path) ? file_get_contents($real_path) : '';
213 $html_path = str_replace("content.json", "content.html", $real_path);
214
215 // Validate HTML path as well
216 $real_html_path = realpath($html_path);
217 if ($real_html_path !== false) {
218 foreach ($real_allowed_base_paths as $real_allowed_base_path) {
219 if (strpos($real_html_path, $real_allowed_base_path) === 0) {
220 $html = file_exists($real_html_path) ? file_get_contents($real_html_path) : '';
221 break;
222 }
223 }
224 }
225 }
226
227 // Create new emailkit post
228 $post_id = wp_insert_post([
229 'post_title' => sanitize_text_field($template_title),
230 'post_type' => 'emailkit',
231 'post_status' => 'publish',
232 'meta_input' => [
233 'emailkit_template_type' => sanitize_text_field($template_type),
234 'emailkit_form_id' => absint($form_id),
235 'emailkit_template_status' => 'active',
236 'emailkit_template_content_html' => wp_kses_post($html),
237 'emailkit_template_content_object' => $template,
238 'emailkit_email_type' => sanitize_text_field($request->get_param('emailkit_email_type')),
239 ]
240 ]);
241
242 if (is_wp_error($post_id)) {
243 return new WP_REST_Response([
244 'success' => false,
245 'message' => __('Failed to create template', 'emailkit')
246 ], 400);
247 }
248
249 return new WP_REST_Response([
250 'success' => true,
251 'data' => [
252 'builder_url' => admin_url("post.php?post={$post_id}&action=emailkit-builder"),
253 'post_id' => $post_id
254 ]
255 ], 200);
256 }
257
258 private function get_existing_template($form_id) {
259 $template_type = 'metform_form_' . $form_id;
260
261 $args = [
262 'post_type' => 'emailkit',
263 'meta_query' => [
264 'relation' => 'OR',
265 [
266 'relation' => 'AND',
267 [
268 'key' => 'emailkit_template_type',
269 'value' => $template_type,
270 'compare' => '='
271 ],
272 [
273 'key' => 'emailkit_template_status',
274 'value' => 'active',
275 'compare' => '='
276 ]
277 ]
278 ],
279 'posts_per_page' => 1,
280 'fields' => 'ids'
281 ];
282
283 $existing = get_posts($args);
284 return !empty($existing) ? $existing[0] : false;
285 }
286
287 public function check_popup_template($request) {
288 if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
289 return ['status' => 'fail', 'message' => [__('Nonce mismatch.', 'emailkit')]];
290 }
291
292 if (!is_user_logged_in() || !current_user_can('edit_posts')) {
293 return ['status' => 'fail', 'message' => [__('Access denied.', 'emailkit')]];
294 }
295
296 $popup_id = absint($request->get_param('popup_id'));
297 $template_type = 'popupkit_popup_' . $popup_id;
298
299 $args = [
300 'post_type' => 'emailkit',
301 'meta_query' => [[
302 'key' => 'emailkit_template_type',
303 'value' => $template_type,
304 'compare' => '='
305 ]],
306 'posts_per_page' => 1,
307 'fields' => 'ids'
308 ];
309
310 $existing = get_posts($args);
311
312 if (!empty($existing)) {
313 return new WP_REST_Response([
314 'success' => true,
315 'data' => [
316 'exists' => true,
317 'builder_url' => admin_url("post.php?post={$existing[0]}&action=emailkit-builder")
318 ]
319 ], 200);
320 }
321
322 return new WP_REST_Response([
323 'success' => true,
324 'data' => ['exists' => false]
325 ], 200);
326 }
327
328 public function create_popup_template($request) {
329 if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
330 return ['status' => 'fail', 'message' => [__('Nonce mismatch.', 'emailkit')]];
331 }
332
333 if (!is_user_logged_in() || !current_user_can('publish_posts')) {
334 return ['status' => 'fail', 'message' => [__('Access denied.', 'emailkit')]];
335 }
336
337 $popup_id = absint($request->get_param('popup_id'));
338 $template_type = 'popupkit_popup_' . $popup_id;
339 $template_title = sanitize_text_field($request->get_param('template_title') ?? __('PopupKit', 'emailkit'));
340
341 // Prevent duplicate templates
342 $existing = $this->get_existing_popup_template($popup_id);
343 if ($existing) {
344 return new WP_REST_Response([
345 'success' => false,
346 'message' => __('A template already exists for this popup.', 'emailkit'),
347 'data' => ['builder_url' => admin_url("post.php?post={$existing}&action=emailkit-builder")]
348 ], 400);
349 }
350
351 // Optionally load a starter template file
352 $template = '';
353 $html = '';
354 if (!empty($request->get_param('emailkit-editor-template')) && trim($request->get_param('emailkit-editor-template')) !== '') {
355 $template_path = $request->get_param('emailkit-editor-template');
356 $real_path = realpath($template_path);
357
358 if ($real_path === false || !$this->is_allowed_template_path($real_path)) {
359 return new WP_REST_Response(['success' => false, 'message' => __('Invalid template path', 'emailkit')], 400);
360 }
361
362 $template = file_exists($real_path) ? file_get_contents($real_path) : '';
363 $html_path = str_replace('content.json', 'content.html', $real_path);
364 $real_html = realpath($html_path);
365 if ($real_html !== false && $this->is_allowed_template_path($real_html)) {
366 $html = file_exists($real_html) ? file_get_contents($real_html) : '';
367 }
368 }
369
370 $post_id = wp_insert_post([
371 'post_title' => $template_title,
372 'post_type' => 'emailkit',
373 'post_status' => 'publish',
374 'meta_input' => [
375 'emailkit_template_type' => $template_type,
376 'emailkit_popup_id' => $popup_id,
377 'emailkit_template_status' => 'active',
378 'emailkit_template_content_html' => wp_kses_post($html),
379 'emailkit_template_content_object'=> $template,
380 'emailkit_email_type' => sanitize_text_field($request->get_param('emailkit_email_type') ?? 'popupkit'),
381 ]
382 ]);
383
384 if (is_wp_error($post_id)) {
385 return new WP_REST_Response(['success' => false, 'message' => __('Failed to create template', 'emailkit')], 400);
386 }
387
388 return new WP_REST_Response([
389 'success' => true,
390 'data' => [
391 'builder_url' => admin_url("post.php?post={$post_id}&action=emailkit-builder"),
392 'post_id' => $post_id
393 ]
394 ], 200);
395 }
396
397 /**
398 * Whether a resolved (realpath'd) file sits inside a directory we ship templates from.
399 *
400 * Starter templates registered by TemplateList live in the plugin directory,
401 * user-imported ones in the uploads directory.
402 *
403 * @param string $real_path Already passed through realpath().
404 * @return bool
405 */
406 private function is_allowed_template_path($real_path) {
407 $allowed_base_paths = [
408 wp_upload_dir()['basedir'] . '/emailkit/templates/',
409 EMAILKIT_DIR . 'includes/templates/',
410 ];
411
412 foreach ($allowed_base_paths as $allowed_base_path) {
413 $real_base = realpath($allowed_base_path);
414 if ($real_base !== false && strpos($real_path, $real_base) === 0) {
415 return true;
416 }
417 }
418
419 return false;
420 }
421
422 private function get_existing_popup_template($popup_id) {
423 $template_type = 'popupkit_popup_' . absint($popup_id);
424
425 $args = [
426 'post_type' => 'emailkit',
427 'meta_query' => [[
428 'key' => 'emailkit_template_type',
429 'value' => $template_type,
430 'compare' => '='
431 ]],
432 'posts_per_page' => 1,
433 'fields' => 'ids'
434 ];
435
436 $existing = get_posts($args);
437 return !empty($existing) ? $existing[0] : false;
438 }
439 }