/**
  * Setup or migrate the database to current plugin version.
  *
  * This function uses error suppression on purpose.
  *
  * @since  4.6
  */
 public static function db_update()
 {
     // Required for dbDelta()
     require_once ABSPATH . 'wp-admin/includes/upgrade.php';
     global $wpdb;
     $charset_collate = '';
     if (!empty($wpdb->charset)) {
         $charset_collate = ' DEFAULT CHARACTER SET ' . $wpdb->charset;
     }
     if (!empty($wpdb->collate)) {
         $charset_collate .= ' COLLATE ' . $wpdb->collate;
     }
     $tbl_popover = self::db_prefix('popover');
     $tbl_ip_cache = self::db_prefix('popover_ip_cache');
     $count = 0;
     if ($wpdb->get_var('SHOW TABLES LIKE "' . $tbl_popover . '" ') == $tbl_popover) {
         // Create a column in old table to monitor migration status.
         $sql = "CREATE TABLE {$tbl_popover} (\r\n\t\t\t\tid bigint(20) unsigned NOT NULL AUTO_INCREMENT,\r\n\t\t\t\tpopover_title varchar(250) DEFAULT NULL,\r\n\t\t\t\tpopover_content text,\r\n\t\t\t\tpopover_settings text,\r\n\t\t\t\tpopover_order bigint(20) DEFAULT '0',\r\n\t\t\t\tpopover_active int(11) DEFAULT '0',\r\n\t\t\t\tmigrated tinyint DEFAULT '0',\r\n\t\t\t\tPRIMARY KEY  (id)\r\n\t\t\t) {$charset_collate};";
         dbDelta($sql);
         // Migrate to custom post type.
         $sql = "\r\n\t\t\tSELECT\r\n\t\t\t\tid,\r\n\t\t\t\tpopover_title,\r\n\t\t\t\tpopover_content,\r\n\t\t\t\tpopover_settings,\r\n\t\t\t\tpopover_order,\r\n\t\t\t\tpopover_active\r\n\t\t\tFROM {$tbl_popover}\r\n\t\t\tWHERE migrated=0\r\n\t\t\t";
         $res = $wpdb->get_results($sql);
         // Name mapping of conditions/rules from build 5 -> 6.
         $mapping = array('isloggedin' => 'login', 'loggedin' => 'no_login', 'onurl' => 'url', 'notonurl' => 'no_url', 'incountry' => 'country', 'notincountry' => 'no_country', 'advanced_urls' => 'adv_url', 'not-advanced_urls' => 'no_adv_url', 'categories' => 'category', 'not-categories' => 'no_category', 'post_types' => 'posttype', 'not-post_types' => 'no_posttype', 'xprofile_value' => 'xprofile', 'not-xprofile_value' => 'no_xprofile', 'supporter' => 'no_prosite', 'searchengine' => 'searchengine', 'commented' => 'no_comment', 'internal' => 'no_internal', 'referrer' => 'referrer', 'count' => 'count', 'max_width' => 'width', 'wp_roles_rule' => 'role', 'membership_level' => 'membership');
         // Translate style to new keys
         $style_mapping = array('Default' => 'old-default', 'Default Fixed' => 'old-fixed', 'Dark Background Fixed' => 'old-fullbackground');
         // Migrate data from build 5 to build 6!
         foreach ($res as $item) {
             // Confirm the item was not migrated, just to be sure...
             // This is one-time code, we don't care for performance here.
             $sql = "\r\n\t\t\t\t\tSELECT 1 status\r\n\t\t\t\t\tFROM {$tbl_popover}\r\n\t\t\t\t\tWHERE id=%s AND migrated=0\r\n\t\t\t\t";
             $sql = $wpdb->prepare($sql, $item->id);
             $status = $wpdb->get_var($sql);
             if ('1' != $status) {
                 continue;
             }
             $raw = maybe_unserialize($item->popover_settings);
             $checks = explode(',', @$raw['popover_check']['order']);
             foreach ($checks as $ind => $key) {
                 if (isset($mapping[$key])) {
                     $checks[$ind] = $mapping[$key];
                 } else {
                     unset($checks[$ind]);
                 }
             }
             if (isset($style_mapping[@$raw['popover_style']])) {
                 $style = $style_mapping[@$raw['popover_style']];
             } else {
                 $style = @$raw['popover_style'];
             }
             $colors = array('col1' => @$raw['popover_colour']['back'], 'col2' => @$raw['popover_colour']['fore']);
             $display = 'delay';
             if (isset($raw['on_exit'])) {
                 $display = 'leave';
             }
             if (isset($raw['on_click'])) {
                 $display = 'click';
             }
             $custom_colors = false;
             if ('FFFFFF' != $colors['col1']) {
                 $custom_colors = true;
             }
             if ('000000' != $colors['col2']) {
                 $custom_colors = true;
             }
             $custom_size = true;
             if (!empty($raw['popover_size']['usejs'])) {
                 $custom_size = false;
             }
             if ('no' != @$raw['popover_usejs']) {
                 $custom_size = false;
             }
             $data = array('name' => $item->popover_title, 'content' => $item->popover_content, 'order' => $item->popover_order, 'active' => true == $item->popover_active, 'size' => @$raw['popover_size'], 'color' => $colors, 'custom_colors' => $custom_colors, 'custom_size' => $custom_size, 'style' => $style, 'can_hide' => 'no' == @$raw['popoverhideforeverlink'], 'close_hides' => 'no' != @$raw['popover_close_hideforever'], 'hide_expire' => absint(@$raw['popover_hideforever_expiry']), 'display' => $display, 'display_data' => array('delay' => absint(@$raw['popoverdelay']), 'delay_type' => 's', 'click' => @$raw['on_click']['selector'], 'click_multi' => !empty($raw['on_click']['selector'])), 'rule' => $checks, 'rule_data' => array('count' => @$raw['popover_count'], 'referrer' => @$raw['popover_ereg'], 'exit' => @$raw['on_exit'], 'url' => @$raw['onurl'], 'no_url' => @$raw['notonurl'], 'adv_url' => @$raw['advanced_urls']['urls'], 'no_adv_url' => @$raw['not-advanced_urls']['urls'], 'country' => @$raw['incountry'], 'no_country' => @$raw['notincountry'], 'category' => @$raw['categories'], 'no_category' => @$raw['not-categories'], 'posttype' => @$raw['post_types'], 'no_posttype' => @$raw['not-post_types'], 'xprofile' => @$raw['xprofile_value'], 'no_xprofile' => @$raw['not-xprofile_value'], 'width' => array('min' => @$raw['max_width']['width'])));
             // Save the popup as custom posttype.
             $popup = new IncPopupItem($data);
             $popup->save(false);
             // Mark Popup as migrated
             $sql = "\r\n\t\t\t\t\tUPDATE {$tbl_popover}\r\n\t\t\t\t\tSET migrated=1\r\n\t\t\t\t\tWHERE id=%s\r\n\t\t\t\t";
             $sql = $wpdb->prepare($sql, $item->id);
             $wpdb->query($sql);
             // Advance counter.
             $count += 1;
         }
     }
     self::refresh_order();
     // Create or update the IP cache table.
     $sql = "\r\n\t\tCREATE TABLE {$tbl_ip_cache} (\r\n\t\t\tIP varchar(12) NOT NULL DEFAULT '',\r\n\t\t\tcountry varchar(2) DEFAULT NULL,\r\n\t\t\tcached bigint(20) DEFAULT NULL,\r\n\t\t\tPRIMARY KEY  (IP),\r\n\t\t\tKEY cached (cached)\r\n\t\t) {$charset_collate};";
     dbDelta($sql);
     if ($count > 0) {
         lib2()->ui->admin_message(sprintf(__('<strong>PopUp Pro</strong><br />' . 'Your installation was successfully updated to use the ' . 'latest version of the plugin!<br />' . '<em>Note: Some PopUp options changed or were replaced. ' . 'You should have a look at your <a href="%s">PopUps</a> ' . 'to see if they still look as intended.</em>', PO_LANG), admin_url('edit.php?post_type=' . IncPopupItem::POST_TYPE)));
     }
     // Migrate the Plugin Settings.
     $old_settings = IncPopupDatabase::_get_option('popover-settings', array());
     $settings = array();
     $cur_method = @$old_settings['loadingmethod'];
     switch ($cur_method) {
         case '':
         case 'external':
             $cur_method = 'ajax';
             break;
         case 'frontloading':
             $cur_method = 'front';
             break;
     }
     $settings['loadingmethod'] = $cur_method;
     // Migrate Add-Ons to new settings.
     // Add-Ons were always saved in the local Options-table by old version.
     self::before_db();
     $addons = get_option('popover_activated_addons', array());
     self::after_db();
     $rules = array('class-popup-rule-browser.php', 'class-popup-rule-geo.php', 'class-popup-rule-popup.php', 'class-popup-rule-referrer.php', 'class-popup-rule-url.php', 'class-popup-rule-user.php', 'class-popup-rule-prosite.php');
     foreach ($addons as $addon) {
         switch ($addon) {
             case 'anonymous_loading.php':
             case 'testheadfooter.php':
                 /* Integrated; no option. */
                 break;
             case 'localgeodatabase.php':
                 $settings['geo_db'] = true;
                 break;
             case 'rules-advanced_url.php':
                 $rules[] = 'class-popup-rule-advurl.php';
                 break;
             case 'rules-categories.php':
                 $rules[] = 'class-popup-rule-category.php';
                 break;
             case 'rules-max_width.php':
                 $rules[] = 'class-popup-rule-width.php';
                 break;
             case 'rules-on_exit.php':
                 $rules[] = 'class-popup-rule-events.php';
                 break;
             case 'rules-onclick.php':
                 $rules[] = 'class-popup-rule-events.php';
                 break;
             case 'rules-post_types.php':
                 $rules[] = 'class-popup-rule-posttype.php';
                 break;
             case 'rules-xprofile_value.php':
                 $rules[] = 'class-popup-rule-xprofile.php';
                 break;
             case 'rules-membership.php':
                 $rules[] = 'class-popup-rule-membership.php';
                 break;
             case 'rules-wp_roles.php':
                 $rules[] = 'class-popup-rule-role.php';
                 break;
         }
     }
     $settings['rules'] = $rules;
     self::set_settings($settings);
     // Save the new DB version to options table.
     self::_set_option('popover_installed', PO_BUILD);
 }