PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.2.8
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.2.8
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 / vendor / wpmet / utility-package / src / Emailkit / Emailkit.php

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

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