Recommended
2 years ago
Support
2 years ago
Cross.php
2 years ago
Fallback.php
2 years ago
Fields.php
2 years ago
Helper.php
2 years ago
I18n.php
2 years ago
Plugin.php
2 years ago
Popup.php
2 years ago
PostType.php
2 years ago
Review.php
2 years ago
Settings.php
2 years ago
Shortcode.php
2 years ago
Upgrade.php
2 years ago
Upgrade.php
311 lines
| 1 | <?php |
| 2 | namespace NTA_WhatsApp; |
| 3 | |
| 4 | use NTA_WhatsApp\Fields; |
| 5 | |
| 6 | defined('ABSPATH') || exit; |
| 7 | class Upgrade |
| 8 | { |
| 9 | protected static $instance = null; |
| 10 | |
| 11 | public static function getInstance() |
| 12 | { |
| 13 | if (null == self::$instance) { |
| 14 | self::$instance = new self; |
| 15 | self::$instance->doHooks(); |
| 16 | } |
| 17 | return self::$instance; |
| 18 | } |
| 19 | |
| 20 | public function __construct() |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | public function doHooks(){ |
| 25 | add_action('admin_init', [$this, 'init']); |
| 26 | } |
| 27 | |
| 28 | public function init(){ |
| 29 | $restored = get_option('nta_wa_restored', false); |
| 30 | $restoredBackground = get_option('nta_wa_background_restored', false); |
| 31 | |
| 32 | if ($restored === false || $restored == 0) { |
| 33 | $old_posts = get_posts(array( |
| 34 | 'post_type' => 'whatsapp-accounts', |
| 35 | 'post_status' => 'any', |
| 36 | 'numberposts' => -1, |
| 37 | 'fields' => 'ids' |
| 38 | )); |
| 39 | if (count($old_posts) > 0) { |
| 40 | $this->runBackground($restoredBackground); |
| 41 | if ($restoredBackground == 1) { |
| 42 | add_action('admin_notices', array($this, 'renderNotice')); |
| 43 | add_action('wp_ajax_njt_wa_restore', array($this, 'runRestore')); |
| 44 | } |
| 45 | } else { |
| 46 | update_option("nta_wa_restored", 1); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public function runBackground($restoredBackground){ |
| 52 | if ($restoredBackground === false || $restoredBackground == 0) { |
| 53 | try { |
| 54 | update_option("nta_wa_background_restored", 1); |
| 55 | $this->restoreMeta(); |
| 56 | $this->restoreOption(); |
| 57 | update_option('nta_wa_restored', 1); |
| 58 | } catch (Exception $e) { |
| 59 | update_option("nta_wa_background_restored", 1); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public function runRestore(){ |
| 65 | check_ajax_referer('nta_wa_restore_nonce', 'nonce', true); |
| 66 | try { |
| 67 | $this->restoreMeta(true); |
| 68 | $this->restoreOption(); |
| 69 | update_option('nta_wa_restored', 1); |
| 70 | } catch (Exception $e) { |
| 71 | wp_send_json_error(array( |
| 72 | 'message' => __('Please contact us! we can\'t restore your accounts!', 'ninjateam-whatsapp'), |
| 73 | 'content' => $e->getMessage(), |
| 74 | )); |
| 75 | } |
| 76 | |
| 77 | wp_send_json_success(array('message' => __('Restored Successfully!', 'ninjateam-whatsapp'))); |
| 78 | } |
| 79 | |
| 80 | public function renderNotice(){ |
| 81 | ?> |
| 82 | <div class="notice notice-error is-dismissible" id="njt-wa-restore-wrapper"> |
| 83 | <div style="font-size: 1.3em; font-weight: 600; margin-top: 1em;"> |
| 84 | <?php _e('WhatsApp database update required', 'ninjateam-whatsapp')?> |
| 85 | </div> |
| 86 | <p> |
| 87 | <span><?php _e('WhatsApp has been updated! To use the latest version, you have to update your database to make your WhatsApp accounts work correctly.', 'ninjateam-whatsapp')?></span> |
| 88 | <div> |
| 89 | <button class="button button-primary" id="nta-wa-restore"> |
| 90 | <strong><?php _e('Update WhatsApp Database', 'ninjateam-whatsapp')?></strong> |
| 91 | </button> |
| 92 | </div> |
| 93 | </p> |
| 94 | </div> |
| 95 | <script> |
| 96 | jQuery(document).ready(function() { |
| 97 | jQuery('#nta-wa-restore').click(function() { |
| 98 | jQuery(this).addClass("nta-updating-message") |
| 99 | jQuery.ajax({ |
| 100 | url: ajaxurl, |
| 101 | type: 'POST', |
| 102 | dataType: 'json', |
| 103 | data: { |
| 104 | 'action': 'njt_wa_restore', |
| 105 | 'nonce': '<?php echo wp_create_nonce('nta_wa_restore_nonce') ?>' |
| 106 | } |
| 107 | }).done(function(result) { |
| 108 | if (result.success) { |
| 109 | jQuery('#nta-wa-restore').removeClass("nta-updating-message") |
| 110 | jQuery('#nta-wa-restore').hide() |
| 111 | jQuery('#njt-wa-restore-wrapper span').html(result.data.message) |
| 112 | } else { |
| 113 | alert(result.data.message) |
| 114 | console.log("Error", result.data.content) |
| 115 | } |
| 116 | }); |
| 117 | }) |
| 118 | }); |
| 119 | </script> |
| 120 | <style> |
| 121 | .nta-updating-message::before { |
| 122 | vertical-align: bottom; |
| 123 | animation: rotation 2s infinite linear; |
| 124 | color: #f56e28; |
| 125 | content: "\f463"; |
| 126 | display: inline-block; |
| 127 | font: normal 20px/1 dashicons; |
| 128 | -webkit-font-smoothing: antialiased; |
| 129 | -moz-osx-font-smoothing: grayscale; |
| 130 | vertical-align: middle; |
| 131 | margin-bottom: 3px; |
| 132 | } |
| 133 | </style> |
| 134 | <?php |
| 135 | } |
| 136 | |
| 137 | public function daysOfWeekWorkingParse($old_meta) |
| 138 | { |
| 139 | $results = array(); |
| 140 | $daysOfWeek = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; |
| 141 | |
| 142 | foreach ($daysOfWeek as $dayKey) { |
| 143 | $timeString = explode("-", $old_meta["nta_{$dayKey}_working"]); |
| 144 | $results[$dayKey] = [ |
| 145 | 'isWorkingOnDay' => $old_meta["nta_{$dayKey}"] == 'checked' ? 'ON' : 'OFF', |
| 146 | 'workHours' => [ |
| 147 | [ |
| 148 | 'startTime' => $timeString[0], |
| 149 | 'endTime' => $timeString[1], |
| 150 | ], |
| 151 | ] |
| 152 | ]; |
| 153 | } |
| 154 | return $results; |
| 155 | } |
| 156 | |
| 157 | public function cleanOldRestored($old_posts){ |
| 158 | foreach ($old_posts as $old_post) { |
| 159 | delete_post_meta($old_post->ID, 'nta_wa_account_info'); |
| 160 | delete_post_meta($old_post->ID, 'nta_wa_button_styles'); |
| 161 | delete_post_meta($old_post->ID, 'nta_wa_widget_show'); |
| 162 | delete_post_meta($old_post->ID, 'nta_wa_widget_position'); |
| 163 | delete_post_meta($old_post->ID, 'nta_wa_wc_show'); |
| 164 | delete_post_meta($old_post->ID, 'nta_wa_wc_position'); |
| 165 | } |
| 166 | |
| 167 | delete_option('nta_wa_widget_styles'); |
| 168 | delete_option('nta_wa_widget_display'); |
| 169 | delete_option('nta_wa_woocommerce'); |
| 170 | delete_option('nta_wa_analytics'); |
| 171 | } |
| 172 | |
| 173 | public function restoreMeta($cleanBefore = false) |
| 174 | { |
| 175 | $old_posts = get_posts(array( |
| 176 | 'post_type' => 'whatsapp-accounts', |
| 177 | 'post_status' => 'any', |
| 178 | 'numberposts' => -1, |
| 179 | 'meta_query' => array( |
| 180 | array( |
| 181 | 'key' => 'nta_whatsapp_accounts', |
| 182 | 'value' => NULL, |
| 183 | 'compare' => '!=', |
| 184 | ) |
| 185 | ) |
| 186 | )); |
| 187 | |
| 188 | if ( $cleanBefore === true ) { |
| 189 | $this->cleanOldRestored($old_posts); |
| 190 | } |
| 191 | |
| 192 | if (count($old_posts) > 0) { |
| 193 | foreach ($old_posts as $old_post) { |
| 194 | $old_meta_info = get_post_meta($old_post->ID, 'nta_whatsapp_accounts', true); |
| 195 | if ($old_meta_info !== false) { |
| 196 | $new_meta_info = array( |
| 197 | 'accountName' => $old_post->post_title, |
| 198 | 'title' => $old_meta_info['nta_title'], |
| 199 | 'number' => $old_meta_info['nta_group_number'], |
| 200 | 'willBeBackText' => $old_meta_info['nta_offline_text'], |
| 201 | 'dayOffsText' => $old_meta_info['nta_over_time'], |
| 202 | 'predefinedText' => $old_meta_info['nta_predefined_text'], |
| 203 | 'isAlwaysAvailable' => (isset($old_meta_info['nta_button_available']) && $old_meta_info['nta_button_available'] == 'ON') ? 'ON' : 'OFF', |
| 204 | 'daysOfWeekWorking' => $this->daysOfWeekWorkingParse($old_meta_info), |
| 205 | ); |
| 206 | |
| 207 | $widget_show = $old_meta_info['nta_active'] == 'active' ? 'ON' : 'OFF'; |
| 208 | $wc_show = $old_meta_info['wo_active'] == 'active' ? 'ON' : 'OFF'; |
| 209 | |
| 210 | $widget_position = $old_meta_info['position']; |
| 211 | $wc_position = $old_meta_info['wo_position']; |
| 212 | } |
| 213 | |
| 214 | $old_button_styles = get_post_meta($old_post->ID, 'nta_wabutton_style', true); |
| 215 | |
| 216 | if ($old_button_styles === false) { |
| 217 | $new_meta_styles = array( |
| 218 | 'type' => "round", |
| 219 | 'backgroundColor' => "#2DB742", |
| 220 | 'textColor' => "#fff", |
| 221 | 'label' => __("Need Help? Chat with us", "ninjateam-whatsapp"), |
| 222 | 'width' => 300, |
| 223 | 'height' => 64, |
| 224 | ); |
| 225 | } else { |
| 226 | $new_meta_styles = array( |
| 227 | 'type' => empty($old_button_styles['button_style']) ? "round" : $old_button_styles['button_style'], |
| 228 | 'backgroundColor' => empty($old_button_styles['button_back_color']) ? "#2DB742" : $old_button_styles['button_back_color'], |
| 229 | 'textColor' => empty($old_button_styles['button_text_color']) ? "#fff" : $old_button_styles['button_text_color'], |
| 230 | 'label' => empty($old_button_styles['button-text']) ? 'Need Help? Chat with us' : $old_button_styles['button-text'], |
| 231 | 'width' => 300, |
| 232 | 'height' => 64, |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | update_post_meta($old_post->ID, 'nta_wa_account_info', $new_meta_info); |
| 237 | update_post_meta($old_post->ID, 'nta_wa_button_styles', $new_meta_styles); |
| 238 | update_post_meta($old_post->ID, 'nta_wa_widget_show', $widget_show); |
| 239 | update_post_meta($old_post->ID, 'nta_wa_widget_position', $widget_position); |
| 240 | update_post_meta($old_post->ID, 'nta_wa_wc_show', $wc_show); |
| 241 | update_post_meta($old_post->ID, 'nta_wa_wc_position', $wc_position); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | public static function restoreOption(){ |
| 247 | $old_option = get_option('nta_whatsapp_setting', false); |
| 248 | if ($old_option !== false) { |
| 249 | $new_option_styles = array( |
| 250 | 'title' => $old_option['widget_name'], |
| 251 | 'responseText' => $old_option['widget_responseText'], |
| 252 | 'description' => $old_option['widget_description'], |
| 253 | 'backgroundColor' => $old_option['back_color'], |
| 254 | 'textColor' => $old_option['text_color'], |
| 255 | 'scrollHeight' => 500, |
| 256 | 'isShowScroll' => 'OFF', |
| 257 | 'isShowResponseText' => 'ON', |
| 258 | 'isShowPoweredBy' => 'ON', |
| 259 | |
| 260 | 'btnLabel' => $old_option['widget_label'], |
| 261 | 'btnLabelWidth' => 156, |
| 262 | 'btnPosition' => $old_option['widget_position'], |
| 263 | 'btnLeftDistance' => 30, |
| 264 | 'btnRightDistance' => 30, |
| 265 | 'btnBottomDistance' => 30, |
| 266 | 'isShowBtnLabel' => 'ON', |
| 267 | |
| 268 | 'isShowGDPR' => (isset($old_option['show_gdpr']) && $old_option['show_gdpr'] == 'ON') ? 'ON' : 'OFF', |
| 269 | 'gdprContent' => $old_option['widget_gdpr'], |
| 270 | ); |
| 271 | |
| 272 | $new_option_display = array( |
| 273 | 'displayCondition' => $old_option['display-pages'] == 'show' ? 'includePages' : 'excludePages', |
| 274 | 'includePages' => !empty($old_option['nta-wa-show-pages']) ? $old_option['nta-wa-show-pages'] : [], |
| 275 | 'excludePages' => !empty($old_option['nta-wa-hide-pages']) ? $old_option['nta-wa-hide-pages'] : [], |
| 276 | 'showOnDesktop' => (isset($old_option['show_on_desktop']) && $old_option['show_on_desktop'] == 'ON') ? 'ON' : 'OFF', |
| 277 | 'showOnMobile' => (isset($old_option['show_on_mobile']) && $old_option['show_on_mobile'] == 'ON') ? 'ON' : 'OFF', |
| 278 | 'time_symbols' => 'h:m' |
| 279 | ); |
| 280 | |
| 281 | |
| 282 | } else { |
| 283 | $new_option_styles = Fields::getWidgetStyles(); |
| 284 | $new_option_display = Fields::getWidgetDisplay(); |
| 285 | } |
| 286 | |
| 287 | update_option('nta_wa_widget_styles', $new_option_styles); |
| 288 | update_option('nta_wa_widget_display', $new_option_display); |
| 289 | |
| 290 | $old_option = get_option('nta_wa_woobutton_setting', false); |
| 291 | if ($old_option !== false) { |
| 292 | $new_option = array( |
| 293 | 'position' => $old_option['nta_woo_button_position'], |
| 294 | 'isShow' => !empty($old_option['nta_woo_button_status']) ? 'ON' : 'OFF' |
| 295 | ); |
| 296 | } else { |
| 297 | $new_option = Fields::getWoocommerceSetting(); |
| 298 | } |
| 299 | update_option('nta_wa_woocommerce', $new_option); |
| 300 | |
| 301 | $old_option = get_option('nta_wa_ga_setting', false); |
| 302 | if ($old_option !== false) { |
| 303 | $new_option = array( |
| 304 | 'enabledGoogle' => 'ON', |
| 305 | 'enabledFacebook' => 'OFF' |
| 306 | ); |
| 307 | } |
| 308 | update_option('nta_wa_analytics', $new_option); |
| 309 | } |
| 310 | } |
| 311 |