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