emailkit.php
233 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 | class Emailkit { |
| 11 | |
| 12 | private $is_already_installed = ''; |
| 13 | |
| 14 | /** |
| 15 | * Constructor for initializing the class. |
| 16 | * |
| 17 | * @access public |
| 18 | * @return void |
| 19 | */ |
| 20 | public function __construct() { |
| 21 | |
| 22 | do_action('edit_with_emailkit_loaded'); |
| 23 | |
| 24 | $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; |
| 25 | $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : ''; |
| 26 | |
| 27 | if ( 'wc-settings' !== $page || 'email' !== $tab ) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | if( !function_exists('get_plugins') ) { |
| 32 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 33 | } |
| 34 | |
| 35 | $installed_plugins = get_plugins(); |
| 36 | $this->is_already_installed = isset($installed_plugins['emailkit/EmailKit.php']) ? 1 : 0; |
| 37 | $message = '<div class="edit-with-emailkit-wrapper"><div class="edit-with-emailkit-banner-left"><div class="edit-with-emailkit-banner-main"><div class="edit-with-emailkit-banner-logo"><img src="'.plugin_dir_url( __FILE__ ).'/assets/logo.svg" /></div><div class="edit-with-emailkit-banner-middle"><div class="edit-with-emailkit-banner-title">Edit your woocommerce emails with EmailKit builder for free.</div><p>Get 22+ WooCommerce elements, 14+ templates, Shortcode support, and more for absolutely free with EmailKit - The Best WordPress Email Customizer.✨</p></div></div></div><div class="edit-with-emailkit-banner-right"><div class="emailkit-install-activate-btn">Get EmailKit</div></div></div>'; |
| 38 | $dismissed_coutner = get_option('elementskit-lite-edit_with_emailkit_banner_dismissed_'.get_current_user_id(), 0); |
| 39 | $notice_showing_delay_time = (3600 * 24 * 15); |
| 40 | |
| 41 | if($dismissed_coutner == 1){ |
| 42 | $notice_showing_delay_time = (3600 * 24 * 30); |
| 43 | }elseif($dismissed_coutner == 2){ |
| 44 | $notice_showing_delay_time = (3600 * 24 * 180); |
| 45 | }elseif($dismissed_coutner >= 3){ |
| 46 | $notice_showing_delay_time = (3600 * 24 * 1825); |
| 47 | } |
| 48 | |
| 49 | \Oxaim\Libs\Notice::instance('elementskit-lite', 'edit_with_emailkit_banner') |
| 50 | ->set_dismiss('user', $notice_showing_delay_time) |
| 51 | ->set_message($message) |
| 52 | ->call(); |
| 53 | |
| 54 | add_action('admin_head', [$this, 'emailkit_admin_head']); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Added script and style for Edit with EmailKit in admin head |
| 59 | * |
| 60 | * @access public |
| 61 | * @return void |
| 62 | */ |
| 63 | public function emailkit_admin_head() { |
| 64 | ?> |
| 65 | <style> |
| 66 | .notice-elementskit-lite-edit_with_emailkit_banner{ |
| 67 | background-image: url("<?php echo esc_url(plugin_dir_url( __FILE__ ).'/assets/notification.svg') ?>") !important; |
| 68 | color: #fff; |
| 69 | background-size: cover !important; |
| 70 | background-repeat: no-repeat !important; |
| 71 | background-position: center center !important; |
| 72 | border: none !important; |
| 73 | } |
| 74 | .notice-elementskit-lite-edit_with_emailkit_banner .edit-with-emailkit-banner-main{ |
| 75 | display: flex; |
| 76 | gap: 15px; |
| 77 | } |
| 78 | .notice-elementskit-lite-edit_with_emailkit_banner button::before{ |
| 79 | color: #ffffff; |
| 80 | } |
| 81 | |
| 82 | .notice-elementskit-lite-edit_with_emailkit_banner .notice-container-full-width{ |
| 83 | padding: 0; |
| 84 | margin: 0 !important; |
| 85 | } |
| 86 | .notice-elementskit-lite-edit_with_emailkit_banner .notice-container-full-width .edit-with-emailkit-wrapper{ |
| 87 | display: flex; |
| 88 | justify-content: space-between; |
| 89 | align-items: center; |
| 90 | } |
| 91 | .notice-elementskit-lite-edit_with_emailkit_banner .notice-container-full-width .edit-with-emailkit-wrapper .edit-with-emailkit-banner-right{ |
| 92 | margin-right: 60px; |
| 93 | } |
| 94 | |
| 95 | .notice-elementskit-lite-edit_with_emailkit_banner .notice-container-full-width .edit-with-emailkit-wrapper .edit-with-emailkit-banner-left .edit-with-emailkit-banner-main .edit-with-emailkit-banner-logo{ |
| 96 | padding: 24px 8px 0px 8px; |
| 97 | border-left: 6px solid #13d5ff; |
| 98 | background: #FFFFFF1A; |
| 99 | } |
| 100 | .notice-elementskit-lite-edit_with_emailkit_banner .notice-container-full-width .edit-with-emailkit-wrapper .edit-with-emailkit-banner-left .edit-with-emailkit-banner-middle{ |
| 101 | padding: 20px 4px; |
| 102 | } |
| 103 | .notice-elementskit-lite-edit_with_emailkit_banner .notice-container-full-width .edit-with-emailkit-wrapper .edit-with-emailkit-banner-right .emailkit-install-activate-btn{ |
| 104 | font-size: 14px; |
| 105 | font-weight: 500; |
| 106 | color: #070C14; |
| 107 | padding: 15px 22px; |
| 108 | background: #EBFF00; |
| 109 | cursor: pointer; |
| 110 | border-radius: 4px; |
| 111 | } |
| 112 | .edit-with-emailkit-banner-title{ |
| 113 | font-size: 24px; |
| 114 | font-weight: 600; |
| 115 | margin-bottom: 8px; |
| 116 | } |
| 117 | </style> |
| 118 | <script type="text/javascript"> |
| 119 | |
| 120 | jQuery( document ).ready( function( $ ) { |
| 121 | |
| 122 | const emailKitInstallBtn = document.querySelector('.emailkit-install-activate-btn'); |
| 123 | |
| 124 | if( !emailKitInstallBtn ) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | const isAlreadyInstalled = "<?php echo esc_attr($this->is_already_installed) ?>"; |
| 129 | let installationUrl = "<?php echo esc_url($this->installation_url('emailkit/EmailKit.php')) ?>"; |
| 130 | let activationUrl = "<?php echo esc_url($this->activation_url('emailkit/EmailKit.php')) ?>"; |
| 131 | installationUrl = installationUrl?.replace(/&/g, '&'); |
| 132 | activationUrl = activationUrl?.replace(/&/g, '&'); |
| 133 | |
| 134 | function emailkit_install_active_plugin(ajaxurl, success_callback, beforeText) { |
| 135 | try { |
| 136 | $.ajax({ |
| 137 | type: "GET", |
| 138 | url: ajaxurl, |
| 139 | beforeSend: () => { |
| 140 | emailKitInstallBtn.innerHTML = beforeText; |
| 141 | }, |
| 142 | success: (response) => { |
| 143 | if (success_callback) { |
| 144 | success_callback(); |
| 145 | }else{ |
| 146 | location.reload(); |
| 147 | } |
| 148 | }, |
| 149 | error: function (error) { |
| 150 | console.error(error); |
| 151 | } |
| 152 | }); |
| 153 | } catch (error) { |
| 154 | console.error("An error occurred:", error); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | emailKitInstallBtn.addEventListener('click', function(e) { |
| 159 | |
| 160 | e.preventDefault(); |
| 161 | |
| 162 | if(isAlreadyInstalled === '0'){ |
| 163 | emailkit_install_active_plugin.call(this, installationUrl, () => { |
| 164 | emailkit_install_active_plugin.call(this, activationUrl, null, 'Activating...'); |
| 165 | }, 'Installing...'); |
| 166 | } else if (isAlreadyInstalled === '1') { |
| 167 | emailkit_install_active_plugin.call(this, activationUrl, null, 'Activating...'); |
| 168 | } |
| 169 | }); |
| 170 | }); |
| 171 | </script> |
| 172 | <?php |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get plugin installation url |
| 177 | * |
| 178 | * @access public |
| 179 | * @param string |
| 180 | * @return string |
| 181 | */ |
| 182 | public function installation_url( $pluginName ) { |
| 183 | $action = 'install-plugin'; |
| 184 | $pluginSlug = $this->get_plugin_slug( $pluginName ); |
| 185 | |
| 186 | return wp_nonce_url( |
| 187 | add_query_arg( |
| 188 | array( |
| 189 | 'action' => $action, |
| 190 | 'plugin' => $pluginSlug |
| 191 | ), |
| 192 | admin_url( 'update.php' ) |
| 193 | ), |
| 194 | $action . '_' . $pluginSlug |
| 195 | ); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Get plugin slug |
| 200 | * |
| 201 | * @access public |
| 202 | * @param string |
| 203 | * @return string |
| 204 | */ |
| 205 | public function get_plugin_slug( $name ) { |
| 206 | $split = explode( '/', $name ); |
| 207 | |
| 208 | return isset( $split[0] ) ? $split[0] : null; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Get plugin activation url |
| 213 | * |
| 214 | * @access public |
| 215 | * @param string |
| 216 | * @return string |
| 217 | */ |
| 218 | public function activation_url( $pluginName ) { |
| 219 | |
| 220 | return wp_nonce_url( add_query_arg( |
| 221 | array( |
| 222 | 'action' => 'activate', |
| 223 | 'plugin' => $pluginName, |
| 224 | 'plugin_status' => 'all', |
| 225 | 'paged' => '1&s', |
| 226 | ), |
| 227 | admin_url( 'plugins.php' ) |
| 228 | ), 'activate-plugin_' . $pluginName ); |
| 229 | } |
| 230 | |
| 231 | } |
| 232 | } |
| 233 |