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