/**
  * Append data to the popup javascript-variable.
  *
  * @since  4.6
  * @param  array $data Data collection that is printed to javascript.
  * @param  IncPopupItem $popup The original popup object.
  * @return array Modified data collection.
  */
 public function append_data_width($script_data, $popup)
 {
     if ($popup->uses_rule('width')) {
         lib2()->array->equip($popup->rule_data, 'width');
         $data = $this->sanitize_values($popup->rule_data['width']);
         if ($data['max'] >= $this->max_width) {
             $data['max'] = 0;
         }
         $script_data['width_min'] = $data['min'];
         $script_data['width_max'] = $data['max'];
         if (!isset($script_data['script'])) {
             $script_data['script'] = '';
         }
         $script_data['script'] .= $this->script_width();
     }
     return $script_data;
 }
 /**
  * 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);
 }
    /**
     * Outputs the contents of a specific column.
     *
     * @since  4.6.0
     * @param  string $column The column-key (defined in post_columns above).
     * @param  int $post_id The ID of the popup.
     */
    public static function post_column_content($column, $post_id)
    {
        $popup = IncPopupDatabase::get($post_id);
        switch ($column) {
            case 'po_name':
                $can_edit = current_user_can('edit_post', $post_id) && 'trash' !== $popup->status;
                $post_type_object = get_post_type_object(IncPopupItem::POST_TYPE);
                $actions = array();
                if ($can_edit) {
                    $actions['edit'] = array('url' => get_edit_post_link($post_id), 'title' => __('Edit this PopUp', PO_LANG), 'attr' => '', 'label' => __('Edit', PO_LANG));
                }
                if ($can_edit && 'active' === $popup->status) {
                    $the_url = 'edit.php?post_type=%1$s&post_id=%2$s&action=deactivate';
                    $the_url = admin_url(sprintf($the_url, IncPopupItem::POST_TYPE, $post_id));
                    $the_url = wp_nonce_url($the_url, 'deactivate-post_' . $post_id);
                    $actions['deactivate'] = array('url' => $the_url, 'title' => __('Deactivate this PopUp', PO_LANG), 'attr' => '', 'label' => __('Deactivate', PO_LANG));
                }
                if ($can_edit && 'inactive' === $popup->status) {
                    $the_url = 'edit.php?post_type=%1$s&post_id=%2$s&action=activate';
                    $the_url = admin_url(sprintf($the_url, IncPopupItem::POST_TYPE, $post_id));
                    $the_url = wp_nonce_url($the_url, 'activate-post_' . $post_id);
                    $actions['activate'] = array('url' => $the_url, 'title' => __('Activate this PopUp', PO_LANG), 'attr' => '', 'label' => __('Activate', PO_LANG));
                }
                $actions['popup_preview'] = array('url' => '#', 'title' => __('Preview this PopUp', PO_LANG), 'attr' => 'class="po-preview" data-id="' . $post_id . '"', 'label' => __('Preview', PO_LANG));
                if (current_user_can('delete_post', $post_id)) {
                    if ('trash' === $popup->status) {
                        $the_url = $post_type_object->_edit_link . '&amp;action=untrash';
                        $the_url = admin_url(sprintf($the_url, $post_id));
                        $the_url = wp_nonce_url($the_url, 'untrash-post_' . $post_id);
                        $actions['untrash'] = array('url' => $the_url, 'title' => __('Restore this PopUp from the Trash', PO_LANG), 'attr' => '', 'label' => __('Restore', PO_LANG));
                    } elseif (EMPTY_TRASH_DAYS) {
                        $actions['trash'] = array('url' => get_delete_post_link($post_id), 'title' => __('Move this PopUp to the Trash', PO_LANG), 'attr' => 'class="submitdelete"', 'label' => __('Trash', PO_LANG));
                    }
                    if ('trash' === $popup->status || !EMPTY_TRASH_DAYS) {
                        $actions['delete'] = array('url' => get_delete_post_link($post_id, '', true), 'title' => __('Delete this PopUp permanently', PO_LANG), 'attr' => 'class="submitdelete"', 'label' => __('Delete Permanently', PO_LANG));
                    }
                }
                if ($can_edit) {
                    ?>
					<a href="<?php 
                    echo esc_url(get_edit_post_link($post_id));
                    ?>
"
						title="<?php 
                    _e('Edit this PopUp', PO_LANG);
                    ?>
">
						<span class="the-title"><?php 
                    echo esc_html($popup->name);
                    ?>
</span>
					</a>
				<?php 
                } else {
                    ?>
					<span class="the-title"><?php 
                    echo esc_html($popup->name);
                    ?>
</span>
				<?php 
                }
                ?>
				<div class="row-actions">
				<?php 
                $action_count = count($actions);
                $i = 0;
                foreach ($actions as $action => $item) {
                    $i += 1;
                    $sep = $i == $action_count ? '' : ' | ';
                    ?>
					<span class="<?php 
                    echo esc_attr($action);
                    ?>
">
					<a href="<?php 
                    echo esc_url($item['url']);
                    ?>
"
						title="<?php 
                    echo esc_attr($item['title']);
                    ?>
"
						<?php 
                    echo '' . $item['attr'];
                    ?>
>
						<?php 
                    echo esc_html($item['label']);
                    ?>
					</a><?php 
                    echo esc_html($sep);
                    ?>
					</span>
					<?php 
                }
                ?>
				</div>
				<?php 
                break;
            case 'po_cond':
                $rule_count = 0;
                ?>
				<div class="rules">
				<?php 
                foreach ($popup->rule as $key) {
                    ?>
					<?php 
                    $label = IncPopupItem::condition_label($key);
                    ?>
					<?php 
                    if (empty($label)) {
                        continue;
                    }
                    ?>
					<?php 
                    $rule_count += 1;
                    ?>
					<span class="rule"><?php 
                    echo esc_html($label);
                    ?>
</span>
				<?php 
                }
                ?>
				<?php 
                if (!$rule_count) {
                    ?>
					<span class="rule-always"><?php 
                    _e('Always Show PopUp', PO_LANG);
                    ?>
</span>
				<?php 
                }
                ?>
				</div>
				<?php 
                break;
            case 'po_pos':
                if ('trash' === $popup->status) {
                    echo '-';
                } else {
                    $order = $popup->order;
                    if (!is_numeric($order) || $order > 999999) {
                        $order = '';
                    }
                    ?>
					<span class="the-pos"><?php 
                    echo esc_html($order);
                    ?>
</span>
					<?php 
                }
                break;
            case 'po_state':
                $title = $popup->status_label($popup->status);
                ?>
				<span title="<?php 
                echo esc_attr($title);
                ?>
">
					<i class="status-icon dashicons"></i>
				</span>
				<?php 
                break;
        }
    }
 /**
  * Action handler that allows us to add a popup via WordPress hook.
  *
  * @since  4.7.1
  * @param  string $contents The PopUp contents.
  * @param  array $options PopUp options.
  */
 public function show_popup($contents, $options = array())
 {
     $this->script_data['popup'] = lib2()->array->get($this->script_data['popup']);
     $popup = new IncPopupItem();
     $data = lib2()->array->get($options);
     $data['content'] = $contents;
     $popup->populate($data);
     $popup->script_data['manual'] = true;
     // 1. Add the popup to the global popup-list.
     $this->popups[] = $popup;
     // 2. Enqueue the popup in the page footer.
     $item = $popup->get_script_data(false);
     unset($item['html']);
     unset($item['styles']);
     $this->script_data['popup'][] = $item;
     $this->load_scripts();
     $this->enqueue_footer();
 }