PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.4.0
EmailKit – Email Customizer for WooCommerce & WP v1.4.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 / MetaBox.php

MetaBox.php in EmailKit – Email Customizer for WooCommerce & WP 1.4.0, at includes/Admin/MetaBox.php

365 lines 14.6 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;
4
5 use EmailKit\Admin\Emails\Helpers\Utils;
6 use WP_Query;
7
8 defined('ABSPATH') || exit;
9 /**
10 * Add MetaBoxes
11 */
12 class MetaBox
13 {
14 /**
15 * @var array
16 */
17 public $template_types = [];
18
19 public function __construct()
20 {
21 $this->template_types = [
22
23 "New Order" => esc_html__('New Order - WC ', 'emailkit'),
24 "Cancelled order" => esc_html__('Cancelled order - WC ', 'emailkit'),
25 "Failed Order" => esc_html__('Failed Order - WC ', 'emailkit'),
26 "Order On Hold" => esc_html__('Order On Hold - WC ', 'emailkit'),
27 "Processing Order" => esc_html__('Processing Order - WC ', 'emailkit'),
28 "Completed Order" => esc_html__('Completed Order - WC ', 'emailkit'),
29 "Refunded Order" => esc_html__('Refunded Order - WC ', 'emailkit'),
30 "Customer Invoice" => esc_html__('Customer Invoice - WC ', 'emailkit'),
31 "Customer Note" => esc_html__('Customer Note - WC ', 'emailkit'),
32 "Reset Password" => esc_html__('Reset Password - WC ', 'emailkit'),
33 "New Account" => esc_html__('New Account - WC ', 'emailkit'),
34 ];
35
36 add_action('add_meta_boxes', [$this, 'add']);
37 add_action('save_post', [$this, 'save']);
38 }
39
40 public function add()
41 {
42
43 add_meta_box("metaBox_id", "Email Details", [$this, 'emailTemplate'], ["emailkit"], "advanced", "high", null);
44 }
45
46 /**
47 * MetaBox Template
48 */
49 public function emailTemplate($object)
50 {
51
52 wp_nonce_field(basename(__FILE__), "meta-box-nonce");
53
54
55 ?>
56 <div style="margin-top:20px;">
57 <label for="emailkit_template_subject" style="font-weight:bold"><?php esc_html_e('Email Subject', 'emailkit'); ?></label> <br><br>
58
59 <input type="text" name="emailkit_template_subject" id="emailkit_template_subject" size="30" style="width:100% !important;" value="<?php echo esc_html(get_post_meta($object->ID, "emailkit_template_subject", true)); ?>">
60 <br> <br>
61
62 <label for="emailkit_template_type" style="font-weight:bold"><?php esc_html_e('Email Template Types', 'emailkit'); ?></label> <br> <br>
63
64 <select name="emailkit_template_type" id="emailkit_template_type" style="width:100% !important;">
65 <option value=""><?php esc_html_e('Select Template Types', 'emailkit'); ?></option>
66 <?php
67
68 foreach ($this->template_types as $key => $template_type) {
69 ?>
70 <option value="<?php echo esc_attr($key); ?>" <?php echo esc_html($key == get_post_meta($object->ID, "emailkit_template_type", true) ? 'selected' : ''); ?>>
71 <?php echo esc_attr($template_type); ?> </option>
72 <?php } ?>
73 </select> <br> <br>
74
75 <label for="emailkit_template_content_html" style="font-weight:bold"><?php esc_html_e('Email Template HTML', 'emailkit'); ?></label> <br><br>
76
77 <textarea readonly class="email_template_html" rows="10" cols="50" name="emailkit_template_content_html" style="width:100% !important;"><?php echo esc_html(get_post_meta($object->ID, "emailkit_template_content_html", true)); ?></textarea>
78 <br> <br>
79 <button class="emailkit--open-editor button button-primary button-large"><?php esc_html_e('Edit Email Template', 'emailkit'); ?></button>
80 <br> <br>
81
82 <input type="hidden" name="post_for_update" id="post_for_update" value="<?php echo isset($object->ID) ? esc_attr($object->ID) : ''; ?>">
83 <label for="emailkit_template_content_object" style="font-weight:bold"><?php esc_html_e('Email Template Object', 'emailkit'); ?></label> <br><br>
84
85 <textarea readonly class="email-template-object" rows="5" cols="20" name="emailkit_template_content_object" style="width:100% !important;"><?php echo esc_html(get_post_meta($object->ID, "emailkit_template_content_object", true)); ?></textarea>
86 <br> <br>
87
88 <div class="flex-row-x">
89 <label class="subtitile" for="emailkit_template_status" style="font-weight:bold"><?php esc_html_e('Status(Active/Inactive):', 'emailkit'); ?> </label>
90
91 <?php
92 $status = esc_html(get_post_meta($object->ID, "emailkit_template_status", 'Active'));
93 $checked_on = $status == 'Active' ? "checked" : "";
94 $toggle_label = $status == 'Active' ? "Active" : "Inactive";
95 $status_value = $status == 'Active' ? "active" : "inactive";
96 ?>
97
98 <div class="toggle-switch">
99 <input name="emailkit_template_status" type="checkbox" id="toggle-switch" <?php echo esc_attr($checked_on); ?>>
100 <label for="toggle-switch"></label>
101 </div>
102
103 <input type="hidden" name="emailkit_template_status_value" value="<?php echo esc_attr($status_value); ?>">
104
105 <span id="toggle-label"><?php echo esc_html($toggle_label); ?></span>
106
107 </div>
108 <style>
109 .flex-row-x {
110 display: flex;
111 align-items: center;
112 }
113
114 .toggle-switch {
115 position: relative;
116 display: inline-block;
117 width: 60px;
118 }
119
120 .postbox-header .hndle.ui-sortable-handle {
121 font-size: 1.1rem !important;
122 }
123 #post-body #post-body-content {
124
125 margin-bottom: 0 !important;
126 }
127
128 .inside label,
129 .subtitile {
130 font-size: 15px;
131 }
132
133 .toggle-switch input[type="checkbox"] {
134 opacity: 0;
135 width: 0;
136 height: 0;
137 }
138
139 .toggle-switch label {
140 position: absolute;
141 top: -1px;
142 left: 10px;
143 right: 0;
144 bottom: 0;
145 height: 22px;
146 width: 50px;
147 background-color: #ccc;
148 border-radius: 34px;
149 cursor: pointer;
150 }
151
152 .toggle-switch label:before {
153 position: absolute;
154 top: 4px;
155 content: "";
156 height: 15px;
157 width: 15px;
158 left: 4px;
159 bottom: 4px;
160 background-color: white;
161 border-radius: 50%;
162 transition: all 0.2s;
163 }
164
165 .toggle-switch input[type="checkbox"]:checked+label {
166 background-color: #2196F3;
167 }
168
169 .toggle-switch input[type="checkbox"]:checked+label:before {
170 transform: translateX(26px);
171 }
172
173 #toggle-label {
174 margin-left: 10px;
175 font-weight: bold;
176 }
177 </style>
178
179 <script>
180 var toggleSwitch = document.querySelector('input[name="emailkit_template_status"]');
181 var toggleLabel = document.querySelector('#toggle-label');
182
183 toggleSwitch.addEventListener('change', function() {
184 toggleLabel.textContent = this.checked ? "Active" : "Inactive";
185 });
186 </script>
187 </div>
188 <style>
189 #emailkit-fullscreen-overlay {
190 position: fixed;
191 width: 100%;
192 height: 100%;
193 background-color: #ffffff;
194 top: 0;
195 left: 0;
196 z-index: 999999;
197 }
198
199 .emailkit-overlay-close-btn {
200 position: absolute;
201 right: 20px;
202 top: 10px;
203 }
204 </style>
205 <script>
206
207
208 if (localStorage.getItem('editorState')) {
209 localStorage.removeItem('editorState');
210 }
211 var url = new URL(location.href);
212
213
214 if (localStorage.getItem('emailkit_template_subject') && (url.searchParams.get('post') == localStorage.getItem('post_for_update')) ) {
215 document.getElementById("emailkit_template_subject").value = localStorage.getItem('emailkit_template_subject');
216 }
217
218 if (localStorage.getItem('emailkit_template_type') && (url.searchParams.get('post') == localStorage.getItem('post_for_update'))) {
219 document.getElementById("emailkit_template_type").value = localStorage.getItem('emailkit_template_type');
220 }
221
222 document.querySelector('#publish').addEventListener('click', e => {
223
224
225 if (localStorage.getItem('emailkit_template_subject')) {
226 localStorage.removeItem('emailkit_template_subject');
227 }
228 if (localStorage.getItem('emailkit_template_type')) {
229 localStorage.removeItem('emailkit_template_type');
230 }
231 if (localStorage.getItem('post_for_update')) {
232 localStorage.removeItem('post_for_update');
233 }
234 });
235
236
237 document.querySelector('.emailkit--open-editor').addEventListener('click', e => {
238
239 e.preventDefault();
240
241 if (localStorage.getItem('emailkit_template_subject')) {
242 localStorage.removeItem('emailkit_template_subject');
243 }
244 if (localStorage.getItem('emailkit_template_type')) {
245 localStorage.removeItem('emailkit_template_type');
246 }
247 if (localStorage.getItem('post_for_update')) {
248 localStorage.removeItem('post_for_update');
249 }
250
251 localStorage.setItem('emailkit_template_subject',document.getElementById("emailkit_template_subject").value)
252 localStorage.setItem('emailkit_template_type',document.getElementById("emailkit_template_type").value)
253 localStorage.setItem('post_for_update',document.getElementById("post_for_update")?.value)
254
255 var newActionValue = 'emailkit-builder';
256
257 // Set 'post_id' parameter
258 url.searchParams.set('post', document.getElementById("post_for_update")?.value);
259
260 // Update or add 'action' parameter
261 url.searchParams.set('action', newActionValue);
262
263 // Replace 'post-new.php' with 'post.php' in the URL
264 url.pathname = url.pathname.replace('post-new.php', 'post.php');
265
266 location.href = url.toString();
267 });
268
269 </script>
270 <?php
271 }
272
273 /**
274 * Save metaBox values
275 */
276 public function save($post_id)
277 {
278
279 // We have Verified The nonce
280 if (!isset($_POST['meta-box-nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['meta-box-nonce'])), basename(__FILE__))) {
281 return;
282 }
283
284 // Check for permission
285 if (!is_user_logged_in() || !current_user_can('administrator')) {
286 return $post_id;
287 }
288
289 // We have Verified The nonce
290 if (isset($_POST['post_for_update']) && '' != $_POST['post_for_update']) {
291 $post_id = sanitize_text_field(wp_unslash($_POST['post_for_update']));
292 }
293
294 if (isset($_POST['emailkit_template_subject'])) {
295 update_post_meta($post_id, 'emailkit_template_subject', sanitize_text_field(wp_unslash($_POST['emailkit_template_subject'])));
296 }
297
298 $emailkit_template_type = isset($_POST['emailkit_template_type']) ? sanitize_text_field(wp_unslash($_POST['emailkit_template_type'])) : '';
299 if (isset($_POST['emailkit_template_type']) && isset($this->template_types[$emailkit_template_type])) {
300
301 update_post_meta($post_id, 'emailkit_template_type', $emailkit_template_type);
302 }
303
304 if (isset($_POST['emailkit_template_content_html'])) {
305
306 $template_html = sanitize_post( wp_unslash($_POST['emailkit_template_content_html']), 'raw');
307 update_post_meta($post_id, 'emailkit_template_content_html', $template_html);
308 }
309
310 if (isset($_POST['emailkit_template_content_object'])) {
311 $template_obj = sanitize_text_field(wp_unslash($_POST['emailkit_template_content_object']));
312 update_post_meta($post_id, 'emailkit_template_content_object', $template_obj);
313 }
314
315
316 if (isset($_POST["emailkit_template_status"])) {
317 $type = sanitize_text_field(wp_unslash($_POST['emailkit_template_type']));
318 $this->deactivateTemplateTypes($type);
319 update_post_meta($post_id, 'emailkit_template_status', 'Active');
320 } else {
321 update_post_meta($post_id, 'emailkit_template_status', 'Inactive');
322 }
323
324 global $wpdb;
325
326 $new_title = sanitize_text_field(wp_unslash($_POST['emailkit_template_subject']));
327
328 $wpdb->update(
329 $wpdb->posts,
330 array('post_title' => $new_title),
331 array('ID' => $post_id),
332 array('%s'),
333 array('%d')
334 );
335 }
336 public function deactivateTemplateTypes($type)
337 {
338
339 $query = array(
340 'post_type' => 'emailkit',
341 'meta_query' => array(
342 array(
343 'key' => 'emailkit_template_type',
344 'value' => $type,
345 'compare' => '=',
346 ),
347 array(
348 'key' => 'emailkit_template_status',
349 'value' => 'Active',
350 'compare' => '=',
351 ),
352 'relation' => 'AND',
353 'fields' => 'ids'
354 )
355 );
356
357 $data = new \WP_Query($query);
358 if (isset($data)) {
359 $postsIds = wp_list_pluck($data->posts, 'ID');
360 foreach ($postsIds as $id) {
361 update_post_meta($id, 'emailkit_template_status', 'Inactive');
362 }
363 }
364 }
365 }