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

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

228 lines 7.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\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 }
34
35 public function check_template($request) {
36 if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
37 return [
38 'status' => 'fail',
39 'message' => [__('Nonce mismatch.', 'emailkit')]
40 ];
41 }
42
43 if (!is_user_logged_in() || !current_user_can('publish_posts')) {
44 return [
45 'status' => 'fail',
46 'message' => [__('Access denied.', 'emailkit')]
47 ];
48 }
49
50 $form_id = $request->get_param('form_id');
51 $template_type = 'metform_form_' . $form_id;
52
53 $args = [
54 'post_type' => 'emailkit',
55 'meta_query' => [
56 'relation' => 'OR',
57 [
58 'relation' => 'AND',
59 [
60 'key' => 'emailkit_template_type',
61 'value' => $template_type,
62 'compare' => '='
63 ],
64 [
65 'key' => 'emailkit_template_status',
66 'value' => 'active',
67 'compare' => '='
68 ]
69 ]
70 ],
71 'posts_per_page' => 1,
72 'fields' => 'ids'
73 ];
74
75 $existing_templates = get_posts($args);
76
77 if (!empty($existing_templates)) {
78 $template_id = $existing_templates[0];
79 return new WP_REST_Response([
80 'success' => true,
81 'data' => [
82 'exists' => true,
83 'builder_url' => admin_url("post.php?post={$template_id}&action=emailkit-builder")
84 ]
85 ], 200);
86 } else {
87 $args = [
88 'post_type' => 'emailkit',
89 'meta_query' => [
90 'relation' => 'OR',
91 [
92 'relation' => 'AND',
93 [
94 'key' => 'emailkit_template_type',
95 'value' => $template_type,
96 'compare' => '='
97 ]
98 ]
99 ],
100 'posts_per_page' => 1,
101 'fields' => 'ids'
102 ];
103
104 $existing_templates = get_posts($args);
105
106 if (!empty($existing_templates)) {
107 $template_id = $existing_templates[0];
108 return new WP_REST_Response([
109 'success' => true,
110 'data' => [
111 'exists' => true,
112 'builder_url' => admin_url("post.php?post={$template_id}&action=emailkit-builder")
113 ]
114 ], 200);
115 }
116 }
117
118 return new WP_REST_Response([
119 'success' => true,
120 'data' => [
121 'exists' => false,
122 ]
123 ], 200);
124 }
125
126 public function create_template($request) {
127 // Verify nonce and permissions
128 if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
129 return [
130 'status' => 'fail',
131 'message' => [__('Nonce mismatch.', 'emailkit')]
132 ];
133 }
134
135 if (!is_user_logged_in() || !current_user_can('publish_posts')) {
136 return [
137 'status' => 'fail',
138 'message' => [__('Access denied.', 'emailkit')]
139 ];
140 }
141
142 $form_id = $request->get_param('form_id');
143 $form_title = $request->get_param('form_title') ?? __('MetForm', 'emailkit');
144 $template_type = 'metform_form_' . $form_id;
145 $template_title = $request->get_param('template_title');
146
147 // First check for existing templates (both old and new format)
148 $existing_template = $this->get_existing_template($form_id);
149 if ($existing_template) {
150 return new WP_REST_Response([
151 'success' => false,
152 'message' => __('A template already exists for this form.', 'emailkit'),
153 'data' => [
154 'builder_url' => admin_url("post.php?post={$existing_template}&action=emailkit-builder")
155 ]
156 ], 400);
157 }
158
159 // Get template content
160 $template = '';
161 $html = '';
162 if (!empty($request->get_param('emailkit-editor-template')) && trim($request->get_param('emailkit-editor-template')) !== '') {
163 $template_path = $request->get_param('emailkit-editor-template');
164 $template = file_exists($template_path) ? file_get_contents($template_path) : '';
165 $html_path = str_replace("content.json", "content.html", $template_path);
166 $html = file_exists($html_path) ? file_get_contents($html_path) : '';
167 }
168
169 // Create new emailkit post
170 $post_id = wp_insert_post([
171 'post_title' => $template_title,
172 'post_type' => 'emailkit',
173 'post_status' => 'publish',
174 'meta_input' => [
175 'emailkit_template_type' => $template_type,
176 'emailkit_form_id' => $form_id,
177 'emailkit_template_status' => 'active',
178 'emailkit_template_content_html' => $html,
179 'emailkit_template_content_object' => $template,
180 'emailkit_email_type' => $request->get_param('emailkit_email_type'),
181 ]
182 ]);
183
184 if (is_wp_error($post_id)) {
185 return new WP_REST_Response([
186 'success' => false,
187 'message' => __('Failed to create template', 'emailkit')
188 ], 400);
189 }
190
191 return new WP_REST_Response([
192 'success' => true,
193 'data' => [
194 'builder_url' => admin_url("post.php?post={$post_id}&action=emailkit-builder"),
195 'post_id' => $post_id
196 ]
197 ], 200);
198 }
199
200 private function get_existing_template($form_id) {
201 $template_type = 'metform_form_' . $form_id;
202
203 $args = [
204 'post_type' => 'emailkit',
205 'meta_query' => [
206 'relation' => 'OR',
207 [
208 'relation' => 'AND',
209 [
210 'key' => 'emailkit_template_type',
211 'value' => $template_type,
212 'compare' => '='
213 ],
214 [
215 'key' => 'emailkit_template_status',
216 'value' => 'active',
217 'compare' => '='
218 ]
219 ]
220 ],
221 'posts_per_page' => 1,
222 'fields' => 'ids'
223 ];
224
225 $existing = get_posts($args);
226 return !empty($existing) ? $existing[0] : false;
227 }
228 }