PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.2.14
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.2.14
2.2.14 2.2.13 2.2.12 2.2.11 2.2.10 2.2.9 2.2.8 2.2.7 2.2.6 2.2.5 2.2.4 2.2.3 trunk 1.0.0 1.0.1 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2
table-builder-block / scoped / deps / wpmet / utility-package / src / Emailkit / Emailkit.php

Emailkit.php in TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables 2.2.14, at scoped/deps/wpmet/utility-package/src/Emailkit/Emailkit.php

543 lines 18.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TableBuilderScopedDeps\Wpmet\UtilityPackage\Emailkit;
4
5 use TableBuilderScopedDeps\WP_Query;
6 defined('ABSPATH') || exit;
7 if (!class_exists('TableBuilderScopedDeps\Wpmet\UtilityPackage\Emailkit\Emailkit')) {
8 class Emailkit
9 {
10 private $installed_plugins = [];
11 private $activated_plugins = [];
12 /**
13 * Constructor for initializing the class.
14 *
15 * @access public
16 * @return void
17 */
18 public function __construct()
19 {
20 do_action('edit_with_emailkit_loaded');
21 add_action('wp_ajax_emailkit_get_builder_url', [$this, 'emailkit_get_builder_url']);
22 if (!function_exists('is_plugin_active')) {
23 // Include necessary WordPress files
24 require_once ABSPATH . 'wp-admin/includes/plugin.php';
25 }
26 if (is_plugin_active('woocommerce/woocommerce.php') && !is_plugin_active('emailkit/EmailKit.php')) {
27 add_filter('woocommerce_email_setting_columns', [$this, 'emailkit_email_setting_columns']);
28 add_action('woocommerce_email_setting_column_template', array($this, 'emailkit_column_template'));
29 add_action('admin_enqueue_scripts', [$this, 'enqueue_script']);
30 add_action('admin_head', [$this, 'emailkit_admin_head']);
31 $this->collect_installed_plugins();
32 $this->collect_activated_plugins();
33 }
34 }
35 /**
36 * Get builder url
37 *
38 * @access public
39 * @return void
40 */
41 public function emailkit_get_builder_url()
42 {
43 if (!isset($_POST['emailkit_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['emailkit_nonce'])), 'wp_rest')) {
44 return ['status' => 'fail', 'message' => ['Nonce mismatch.']];
45 }
46 // This builds/returns an edit-post URL for the matching emailkit template,
47 // so require the same capability the WooCommerce > Settings > Emails page
48 // (where this is called from) is itself gated behind.
49 if (!current_user_can('manage_woocommerce')) {
50 return ['status' => 'fail', 'message' => ['Permission denied.']];
51 }
52 $wc_template_type = isset($_POST['emailkit_template_type']) ? sanitize_text_field(wp_unslash($_POST['emailkit_template_type'])) : '';
53 $post_id = $this->get_emailkit_post_id($wc_template_type);
54 if ($post_id) {
55 $builder_url = admin_url("post.php?post={$post_id}&action=emailkit-builder");
56 wp_send_json(['builder_url' => $builder_url]);
57 }
58 $emailkit_template = $this->find_emailkit_template($wc_template_type);
59 if (isset($emailkit_template)) {
60 $demo_url = isset($emailkit_template['file']) ? $emailkit_template['file'] : '';
61 $emailkit_email_type = isset($emailkit_template['mail_type']) ? $emailkit_template['mail_type'] : '';
62 $emailkit_template_title = isset($emailkit_template['title']) ? $emailkit_template['title'] : '';
63 $template_type = isset($emailkit_template['template_type']) ? $emailkit_template['template_type'] : '';
64 }
65 wp_send_json(['emailkit_editor_template' => $demo_url, 'emailkit_email_type' => $emailkit_email_type, 'emailkit_template_type' => $template_type, 'emailkit_template_title' => $emailkit_template_title, 'emailkit_template_status' => 'active']);
66 }
67 /**
68 * Add heading on woocommerce email settings column
69 *
70 * @access public
71 * @param array
72 * @return array
73 */
74 public function emailkit_email_setting_columns($array)
75 {
76 if (isset($array['actions'])) {
77 unset($array['actions']);
78 return array_merge($array, array('template' => 'EmailKit', 'actions' => ''));
79 }
80 return $array;
81 }
82 /**
83 * Add template on woocommerce email settings column
84 *
85 * @access public
86 * @param array
87 * @return array
88 */
89 public function emailkit_column_template($email)
90 {
91 $wc_template_type = $email->id;
92 $plugin_name = 'emailkit/EmailKit.php';
93 $installation_url = $this->installation_url($plugin_name);
94 $activation_url = $this->activation_url($plugin_name);
95 $plugin_data = $this->get_plugin_status($plugin_name);
96 $plugin_status = isset($plugin_data['status']) ? $plugin_data['status'] : '';
97 $plugin_status_label = isset($plugin_data['status']) ? $plugin_data['status'] == 'activated' ? 'activated' : '' : '';
98 ?>
99
100 <td class="wc-email-settings-table-template wpmet-emailkit-install-btn-wrapper">
101 <button class="wpmet-emailkit-install-activate emailkit-open-new-form-editor-modal wpmet-woocom-editwithemailkit <?php
102 echo esc_attr($plugin_status_label);
103 ?>"
104 style="width: 160px"
105 target="_blank"
106 href="<?php
107 echo esc_url($installation_url);
108 ?>"
109 data-activation_url="<?php
110 echo esc_url($activation_url);
111 ?>"
112 data-plugin_status="<?php
113 echo esc_attr($plugin_status);
114 ?>"
115 data-wc-template-type="<?php
116 echo esc_attr($wc_template_type);
117 ?>">
118 Edit With Emailkit
119 </button>
120 <p class="wpmet-emailkit-install-error-msg" style="color: #b82441; font-weight: 500; font-size: 13px; display: none;">Please try again</p>
121 </td>
122 <?php
123 }
124 /**
125 * Collect installed and activated plugins
126 *
127 * @access public
128 * @return void
129 */
130 public function collect_installed_plugins()
131 {
132 if (!function_exists('get_plugins')) {
133 include_once ABSPATH . 'wp-admin/includes/plugin.php';
134 }
135 foreach (get_plugins() as $key => $plugin) {
136 array_push($this->installed_plugins, $key);
137 }
138 }
139 /**
140 * Collect activated plugins
141 *
142 * @access public
143 * @return void
144 */
145 public function collect_activated_plugins()
146 {
147 foreach (apply_filters('active_plugins', get_option('active_plugins')) as $plugin) {
148 array_push($this->activated_plugins, $plugin);
149 }
150 }
151 /**
152 * Check if plugin is installed
153 *
154 * @access public
155 * @param string
156 * @return bool
157 */
158 public function check_installed_plugin($name)
159 {
160 return in_array($name, $this->installed_plugins);
161 }
162 /**
163 * Check if plugin is activated
164 *
165 * @access public
166 * @param string
167 * @return bool
168 */
169 public function check_activated_plugin($name)
170 {
171 return in_array($name, $this->activated_plugins);
172 }
173 /**
174 * Get plugin status
175 *
176 * @access public
177 * @param string
178 * @return array
179 */
180 public function get_plugin_status($name)
181 {
182 $data = ["url" => "", "activation_url" => "", "installation_url" => "", "title" => "", "status" => ""];
183 if ($this->check_installed_plugin($name)) {
184 if ($this->check_activated_plugin($name)) {
185 $data['title'] = __('Activated', 'table-builder-block');
186 $data['status'] = "activated";
187 } else {
188 $data['title'] = __('Activate Now', 'table-builder-block');
189 $data['status'] = 'installed';
190 $data['activation_url'] = $this->activation_url($name);
191 }
192 } else {
193 $data['title'] = __('Install Now', 'table-builder-block');
194 $data['status'] = 'not_installed';
195 $data['installation_url'] = $this->installation_url($name);
196 $data['activation_url'] = $this->activation_url($name);
197 }
198 return $data;
199 }
200 /**
201 * Get plugin slug
202 *
203 * @access public
204 * @param string
205 * @return string
206 */
207 public function get_plugin_slug($name)
208 {
209 $split = explode('/', $name);
210 return isset($split[0]) ? $split[0] : null;
211 }
212 /**
213 * Get plugin installation url
214 *
215 * @access public
216 * @param string
217 * @return string
218 */
219 public function installation_url($pluginName)
220 {
221 $action = 'install-plugin';
222 $pluginSlug = $this->get_plugin_slug($pluginName);
223 return wp_nonce_url(add_query_arg(array('action' => $action, 'plugin' => $pluginSlug), admin_url('update.php')), $action . '_' . $pluginSlug);
224 }
225 /**
226 * Get plugin activation url
227 *
228 * @access public
229 * @param string
230 * @return string
231 */
232 public function activation_url($pluginName)
233 {
234 return wp_nonce_url(add_query_arg(array('action' => 'activate', 'plugin' => $pluginName, 'plugin_status' => 'all', 'paged' => '1&s'), admin_url('plugins.php')), 'activate-plugin_' . $pluginName);
235 }
236 /**
237 * Get emailkit post id
238 *
239 * @access public
240 * @param string
241 * @return string
242 */
243 private function get_emailkit_post_id($wc_template_type)
244 {
245 $args = array('post_type' => 'emailkit', 'posts_per_page' => -1, 'meta_query' => array(
246 'relation' => 'AND',
247 // Use AND relation for matching both conditions
248 array('key' => 'emailkit_template_type', 'value' => $wc_template_type),
249 ));
250 $query = new WP_Query($args);
251 $post_ids = array();
252 if ($query->have_posts()) {
253 while ($query->have_posts()) {
254 $query->the_post();
255 $post_ids[] = get_the_ID();
256 }
257 wp_reset_postdata();
258 }
259 return $post_ids[0] ?? null;
260 }
261 /**
262 * Enqueue script
263 *
264 * @access public
265 * @return void
266 */
267 function enqueue_script()
268 {
269 ?>
270 <script>
271 var emailkit_woocommerce = {
272 ajaxurl: "<?php
273 echo esc_url(admin_url('admin-ajax.php'));
274 ?>",
275 nonce: "<?php
276 echo esc_attr(wp_create_nonce('emailkit_nonce'));
277 ?>",
278 rest_url: "<?php
279 echo esc_url(get_rest_url(null, 'emailkit/v1/'));
280 ?>",
281 rest_nonce: "<?php
282 echo esc_attr(wp_create_nonce('wp_rest'));
283 ?>"
284 }
285 </script>
286 <?php
287 }
288 /**
289 * Find emailkit template
290 *
291 * @access public
292 * @param string
293 * @return array
294 */
295 function find_emailkit_template($wc_template_type)
296 {
297 if (class_exists('TableBuilderScopedDeps\EmailKit\Admin\TemplateList') && class_exists('TableBuilderScopedDeps\EmailKit\Admin\Emails\EmailLists') && method_exists('TableBuilderScopedDeps\EmailKit\Admin\TemplateList', 'get_templates') && method_exists('TableBuilderScopedDeps\EmailKit\Admin\Emails\EmailLists', 'woocommerce_email')) {
298 $templates = \TableBuilderScopedDeps\EmailKit\Admin\TemplateList::get_templates();
299 $template_title = \TableBuilderScopedDeps\Emailkit\Admin\Emails\EmailLists::woocommerce_email($wc_template_type);
300 foreach ($templates as $key => $value) {
301 $email_type = $value['mail_type'];
302 $template_email_type = $value['title'];
303 if ($email_type == 'woocommerce' && $wc_template_type == $template_email_type) {
304 return ['file' => $value['file'], 'mail_type' => $email_type, 'template_type' => $wc_template_type, 'title' => $template_title];
305 }
306 }
307 }
308 return [];
309 }
310 /**
311 * Enqueue style and script
312 *
313 * @access public
314 * @return void
315 */
316 public function emailkit_admin_head()
317 {
318 ?>
319
320 <style>
321 .wpmet-onboard-dashboard .wpmet-plugin-install-activate {
322 cursor: no-drop;
323 background-color: #E8E9EF;
324 color: #5D5E65;
325 border-color: #E8E9EF;
326 }
327 .wpmet-emailkit-install-btn-wrapper .emailkit-open-new-form-editor-modal{
328 background-color: #227BFF;
329 border: none;
330 font-size: 14px;
331 font-weight: 500;
332 line-height: 35px;
333 color: #fff;
334 border-radius: 4px;
335 padding: 1px 23px 2px;
336 -webkit-box-sizing: border-box;
337 box-sizing: border-box;
338 cursor: pointer;
339 transition: all .3s;
340 }
341 .wpmet-emailkit-install-btn-wrapper .emailkit-open-new-form-editor-modal:hover{
342 background:#1168E9;
343 }
344
345 .wpmet-emailkit-install-btn-wrapper .emailkit-open-new-form-editor-modal:disabled{
346 background-color: #7aa2de;
347 cursor: not-allowed;
348 }
349 .wpmet-emailkit-install-btn-wrapper .emailkit-open-new-form-editor-modal:disabled:hover{
350 background-color: #7aa2de;
351 }
352
353 .emailkit-slider-loader{
354 pointer-events: none;
355 position: relative;
356 }
357
358 .emailkit-slider-loader::after {
359 content: "";
360 display: inline-block;
361 width: 8px;
362 height: 8px;
363 border: 3px solid #f9f9f9f1;
364 border-radius: 50%;
365 border-top-color: #210d0d00;
366 position: absolute;
367 left: 6px;
368 bottom: 13px;
369 z-index: 99;
370 animation: spin 1s ease-in-out infinite;
371 -webkit-animation: spin 1s ease-in-out infinite;
372 }
373 @keyframes spin {
374 to {
375 -webkit-transform: rotate(360deg);
376 }
377 }
378 </style>
379 <script type="text/javascript">
380
381 jQuery( document ).ready( function( $ ) {
382
383 function disableBtn(el, exceptEl = '', isTrue = false){
384
385 if(exceptEl !== ""){
386 jQuery(el).not(exceptEl).each((index, item) => {
387 jQuery(item).prop('disabled', isTrue);
388 })
389 } else {
390 jQuery(el).each((index, item) => {
391 jQuery(item).prop('disabled', isTrue);
392 })
393 }
394
395
396 }
397
398
399 let _emailkit_self = "";
400 $('.wpmet-emailkit-install-activate').on('click', function(e){
401
402 e.preventDefault();
403 _emailkit_self = this;
404 jQuery(_emailkit_self).addClass('emailkit-slider-loader');
405 if(jQuery(_emailkit_self).next()){
406 jQuery(_emailkit_self).next().css('display', 'none');
407 }
408
409 disableBtn('.wpmet-woocom-editwithemailkit', _emailkit_self, true);
410
411 jQuery('.wpmet-woocom-editwithemailkit').not(_emailkit_self).each((index, item) => {
412 jQuery(item).prop('disabled', true);
413 })
414 let installation_url = $(this).attr('href');
415 let activation_url = $(this).attr('data-activation_url');
416 let plugin_status = $(this).data('plugin_status');
417 let templateType = $(this).data('wc-template-type');
418
419 if($(this).hasClass('wpmet-plugin-install-activate') || $(this).hasClass('activated')){
420 return false;
421 }
422
423 if(plugin_status == 'not_installed'){
424 wpmet_install_active_plugin.call(this, installation_url, () => {
425 wpmet_install_active_plugin.call(this, activation_url, null, 'Activating...', 'Activated', templateType);
426 }, 'Installing...', 'Installed');
427 } else if (plugin_status == 'installed') {
428 wpmet_install_active_plugin.call(this, activation_url, null, 'Activating...', 'Activated', templateType);
429 }
430 });
431
432 // installing plugin
433 function wpmet_install_active_plugin(ajaxurl, success_callback, beforeText, successText, templateType) {
434 try {
435 $.ajax({
436 type: "GET",
437 url: ajaxurl,
438 beforeSend: () => {
439 $(this).addClass('wpmet-plugin-install-activate');
440 if (beforeText) {
441 $(this).html(beforeText);
442 }
443 },
444 success: (response) => {
445 $(this).removeClass('wpmet-plugin-install-activate');
446
447 if (ajaxurl.indexOf('action=activate') >= 0) {
448 $(this).addClass('activated');
449 sendToBuilderProcessing(templateType);
450 }
451
452 $(this).html('Proceeding...');
453
454 if (success_callback) {
455 success_callback();
456 }
457 },
458 error: function (error) {
459 jQuery(_emailkit_self).remove('emailkit-slider-loader');
460 jQuery(_emailkit_self).next().css('display', 'block');
461 disableBtn('.wpmet-woocom-editwithemailkit', false);
462 console.error(error);
463 }
464 });
465 } catch (error) {
466
467 console.error("An error occurred:", error);
468 }
469 }
470
471 function sendToBuilderProcessing( templateType ){
472 try {
473 $.ajax({
474 url: emailkit_woocommerce.ajaxurl,
475 method: 'POST',
476 data: {
477 'emailkit_nonce': emailkit_woocommerce.rest_nonce,
478 'action': 'emailkit_get_builder_url',
479 'emailkit_template_type': templateType
480 },
481 success: function (response) {
482
483 let builderUrl = response?.builder_url;
484
485 if (builderUrl) {
486
487 window.location.href = builderUrl;
488
489 return false;
490 }
491 rdirectToBuilder(response);
492 },
493 error: function (error) {
494 jQuery(_emailkit_self).remove('emailkit-slider-loader');
495 jQuery(_emailkit_self).next().css('display', 'block');
496 disableBtn('.wpmet-woocom-editwithemailkit', false);
497 console.error(error);
498 }
499 });
500 } catch (error) {
501
502 console.error("An error occurred:", error);
503 }
504 }
505
506 function rdirectToBuilder(response){
507 try {
508 $.ajax({
509 url: emailkit_woocommerce.rest_url + 'template-data',
510 method: 'POST',
511 headers: {
512 'X-WP-Nonce': emailkit_woocommerce.rest_nonce,
513 },
514 data: {
515 'emailkit-editor-template': response.emailkit_editor_template,
516 'emailkit_email_type': response.emailkit_email_type,
517 'emailkit_template_type': response.emailkit_template_type,
518 'emailkit_template_status': 'active'
519 },
520 success: function (response) {
521 jQuery(_emailkit_self).remove('emailkit-slider-loader');
522 window.location.href = response.data.builder_url;
523 },
524 error: function (error) {
525 jQuery(_emailkit_self).remove('emailkit-slider-loader');
526 disableBtn('.wpmet-woocom-editwithemailkit', false);
527 console.error(error);
528 }
529 });
530 } catch (error) {
531
532 console.error("An error occurred:", error);
533 }
534 }
535
536 _emailkit_self = "";
537 });
538 </script>
539 <?php
540 }
541 }
542 }
543