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

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

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