addons
11 years ago
external
11 years ago
rules
11 years ago
class-popup-admin.php
11 years ago
class-popup-base.php
11 years ago
class-popup-database.php
11 years ago
class-popup-help.php
11 years ago
class-popup-item.php
11 years ago
class-popup-posttype.php
11 years ago
class-popup-public.php
11 years ago
class-popup-rule.php
11 years ago
config-defaults.php
11 years ago
class-popup-database.php
885 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Provides the database level functions for the popups. |
| 5 | */ |
| 6 | class IncPopupDatabase { |
| 7 | |
| 8 | // Use `self::db_prefix( IP_TABLE );` to get the full table name. |
| 9 | const IP_TABLE = 'popover_ip_cache'; |
| 10 | |
| 11 | /** |
| 12 | * Returns the singleton instance of the popup database class. |
| 13 | * |
| 14 | * @since 4.6 |
| 15 | */ |
| 16 | static public function instance() { |
| 17 | static $Inst = null; |
| 18 | |
| 19 | if ( null === $Inst ) { |
| 20 | $Inst = new IncPopupDatabase(); |
| 21 | } |
| 22 | |
| 23 | return $Inst; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Checks the database version and migrates to latest version is required. |
| 28 | * |
| 29 | * @since 4.6 |
| 30 | */ |
| 31 | static public function check_db() { |
| 32 | // Update the DB if required. |
| 33 | if ( ! IncPopupDatabase::db_is_current() ) { |
| 34 | IncPopupDatabase::db_update(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Checks the state of the database and returns true when the DB has a valid |
| 40 | * format for the current plugin version. |
| 41 | * |
| 42 | * @since 4.6 |
| 43 | * @return boolean Database state (true = everything okay) |
| 44 | */ |
| 45 | static public function db_is_current() { |
| 46 | $cur_version = self::_get_option( 'popover_installed', 0 ); |
| 47 | |
| 48 | // When no DB-Values exist yet then don't even show the notice in |
| 49 | // the Network dashboard. |
| 50 | if ( ! $cur_version ) { |
| 51 | IncPopupDatabase::set_flag( 'network_dismiss', true ); |
| 52 | } |
| 53 | |
| 54 | return $cur_version == PO_BUILD; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Adds the correct DB prefix to the table and returns the full name. |
| 59 | * Takes the setting PO_GLOBAL into account. |
| 60 | * |
| 61 | * @since 4.6 |
| 62 | * @param string $table Table name without prefix. |
| 63 | * @return string The prefixed table name. |
| 64 | */ |
| 65 | static public function db_prefix( $table ) { |
| 66 | global $wpdb; |
| 67 | static $Prefixed = array(); |
| 68 | |
| 69 | // Handle issues where the blog is switched in code (switch_to_blog) |
| 70 | if ( is_multisite() ) { |
| 71 | global $blog_id; |
| 72 | } else { |
| 73 | $blog_id = 0; |
| 74 | } |
| 75 | |
| 76 | if ( ! isset( $Prefixed[ $blog_id ] ) ) { |
| 77 | $Prefixed[ $blog_id ] = array(); |
| 78 | } |
| 79 | |
| 80 | if ( ! isset( $Prefixed[ $blog_id ][ $table ] ) ) { |
| 81 | $Prefixed[$table] = $table; |
| 82 | |
| 83 | if ( defined( 'PO_GLOBAL' ) && true == PO_GLOBAL ) { |
| 84 | if ( ! empty( $wpdb->base_prefix ) ) { |
| 85 | $Prefixed[ $blog_id ][ $table ] = $wpdb->base_prefix . $table; |
| 86 | } else { |
| 87 | $Prefixed[ $blog_id ][ $table ] = $wpdb->prefix . $table; |
| 88 | } |
| 89 | } else { |
| 90 | $Prefixed[ $blog_id ][ $table ] = $wpdb->prefix . $table; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return $Prefixed[ $blog_id ][ $table ]; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Setup or migrate the database to current plugin version. |
| 99 | * |
| 100 | * This function uses error suppression on purpose. |
| 101 | * |
| 102 | * @since 4.6 |
| 103 | */ |
| 104 | static public function db_update() { |
| 105 | // Required for dbDelta() |
| 106 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 107 | |
| 108 | global $wpdb; |
| 109 | |
| 110 | $charset_collate = ''; |
| 111 | |
| 112 | if ( ! empty( $wpdb->charset ) ) { |
| 113 | $charset_collate = ' DEFAULT CHARACTER SET ' . $wpdb->charset; |
| 114 | } |
| 115 | |
| 116 | if ( ! empty( $wpdb->collate ) ) { |
| 117 | $charset_collate .= ' COLLATE ' . $wpdb->collate; |
| 118 | } |
| 119 | |
| 120 | $tbl_popover = self::db_prefix( 'popover' ); |
| 121 | $tbl_ip_cache = self::db_prefix( 'popover_ip_cache' ); |
| 122 | $count = 0; |
| 123 | |
| 124 | if ( $wpdb->get_var( 'SHOW TABLES LIKE "' . $tbl_popover . '" ' ) == $tbl_popover ) { |
| 125 | // Create a column in old table to monitor migration status. |
| 126 | $sql = "CREATE TABLE {$tbl_popover} ( |
| 127 | id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 128 | popover_title varchar(250) DEFAULT NULL, |
| 129 | popover_content text, |
| 130 | popover_settings text, |
| 131 | popover_order bigint(20) DEFAULT '0', |
| 132 | popover_active int(11) DEFAULT '0', |
| 133 | migrated tinyint DEFAULT '0', |
| 134 | PRIMARY KEY (id) |
| 135 | ) {$charset_collate};"; |
| 136 | |
| 137 | dbDelta( $sql ); |
| 138 | |
| 139 | // Migrate to custom post type. |
| 140 | $sql = " |
| 141 | SELECT |
| 142 | id, |
| 143 | popover_title, |
| 144 | popover_content, |
| 145 | popover_settings, |
| 146 | popover_order, |
| 147 | popover_active |
| 148 | FROM {$tbl_popover} |
| 149 | WHERE migrated=0 |
| 150 | "; |
| 151 | $res = $wpdb->get_results( $sql ); |
| 152 | |
| 153 | // Name mapping of conditions/rules from build 5 -> 6. |
| 154 | $mapping = array( |
| 155 | 'isloggedin' => 'login', 'loggedin' => 'no_login', |
| 156 | 'onurl' => 'url', 'notonurl' => 'no_url', |
| 157 | 'incountry' => 'country', 'notincountry' => 'no_country', |
| 158 | 'advanced_urls' => 'adv_url', 'not-advanced_urls' => 'no_adv_url', |
| 159 | 'categories' => 'category', 'not-categories' => 'no_category', |
| 160 | 'post_types' => 'posttype', 'not-post_types' => 'no_posttype', |
| 161 | 'xprofile_value' => 'xprofile', 'not-xprofile_value' => 'no_xprofile', |
| 162 | 'supporter' => 'no_prosite', |
| 163 | 'searchengine' => 'searchengine', |
| 164 | 'commented' => 'no_comment', |
| 165 | 'internal' => 'no_internal', |
| 166 | 'referrer' => 'referrer', |
| 167 | 'count' => 'count', |
| 168 | 'max_width' => 'width', |
| 169 | 'wp_roles_rule' => 'role', |
| 170 | 'membership_level' => 'membership', |
| 171 | //'on_exit' => 'on_exit', |
| 172 | //'on_click' => 'on_click', |
| 173 | ); |
| 174 | |
| 175 | // Translate style to new keys |
| 176 | $style_mapping = array( |
| 177 | 'Default' => 'old-default', |
| 178 | 'Default Fixed' => 'old-fixed', |
| 179 | 'Dark Background Fixed' => 'old-fullbackground', |
| 180 | ); |
| 181 | |
| 182 | // Migrate data from build 5 to build 6! |
| 183 | foreach ( $res as $item ) { |
| 184 | // Confirm the item was not migrated, just to be sure... |
| 185 | // This is one-time code, we don't care for performance here. |
| 186 | $sql = " |
| 187 | SELECT 1 status |
| 188 | FROM {$tbl_popover} |
| 189 | WHERE id=%s AND migrated=0 |
| 190 | "; |
| 191 | $sql = $wpdb->prepare( $sql, $item->id ); |
| 192 | $status = $wpdb->get_var( $sql ); |
| 193 | if ( '1' != $status ) { continue; } |
| 194 | |
| 195 | $raw = maybe_unserialize( $item->popover_settings ); |
| 196 | $checks = explode( ',', @$raw['popover_check']['order'] ); |
| 197 | foreach ( $checks as $ind => $key ) { |
| 198 | if ( isset( $mapping[$key] ) ) { |
| 199 | $checks[$ind] = $mapping[$key]; |
| 200 | } else { |
| 201 | unset( $checks[$ind] ); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if ( isset( $style_mapping[ @$raw['popover_style'] ] ) ) { |
| 206 | $style = $style_mapping[ @$raw['popover_style'] ]; |
| 207 | } else { |
| 208 | $style = @$raw['popover_style']; |
| 209 | } |
| 210 | |
| 211 | $colors = array( |
| 212 | 'col1' => @$raw['popover_colour']['back'], |
| 213 | 'col2' => @$raw['popover_colour']['fore'], |
| 214 | ); |
| 215 | |
| 216 | $display = 'delay'; |
| 217 | if ( isset( $raw['on_exit'] ) ) { $display = 'leave'; } |
| 218 | if ( isset( $raw['on_click'] ) ) { $display = 'click'; } |
| 219 | |
| 220 | $custom_colors = false; |
| 221 | if ( 'FFFFFF' != $colors['col1'] ) { $custom_colors = true; } |
| 222 | if ( '000000' != $colors['col2'] ) { $custom_colors = true; } |
| 223 | |
| 224 | $custom_size = true; |
| 225 | if ( ! empty( $raw['popover_size']['usejs'] ) ) { $custom_size = false; } |
| 226 | if ( 'no' != @$raw['popover_usejs'] ) { $custom_size = false; } |
| 227 | |
| 228 | $data = array( |
| 229 | 'name' => $item->popover_title, |
| 230 | 'content' => $item->popover_content, |
| 231 | 'order' => $item->popover_order, |
| 232 | 'active' => (true == $item->popover_active), |
| 233 | 'size' => @$raw['popover_size'], |
| 234 | 'color' => $colors, |
| 235 | 'custom_colors' => $custom_colors, |
| 236 | 'custom_size' => $custom_size, |
| 237 | 'style' => $style, |
| 238 | 'can_hide' => ('no' == @$raw['popoverhideforeverlink']), |
| 239 | 'close_hides' => ('no' != @$raw['popover_close_hideforever']), |
| 240 | 'hide_expire' => absint( @$raw['popover_hideforever_expiry'] ), |
| 241 | 'display' => $display, |
| 242 | 'display_data' => array( |
| 243 | 'delay' => absint( @$raw['popoverdelay'] ), |
| 244 | 'delay_type' => 's', |
| 245 | 'click' => @$raw['on_click']['selector'], |
| 246 | 'click_multi' => ! empty( $raw['on_click']['selector'] ), |
| 247 | ), |
| 248 | 'rule' => $checks, |
| 249 | 'rule_data' => array( |
| 250 | 'count' => @$raw['popover_count'], |
| 251 | 'referrer' => @$raw['popover_ereg'], |
| 252 | 'exit' => @$raw['on_exit'], |
| 253 | 'url' => @$raw['onurl'], |
| 254 | 'no_url' => @$raw['notonurl'], |
| 255 | 'adv_url' => @$raw['advanced_urls']['urls'], |
| 256 | 'no_adv_url' => @$raw['not-advanced_urls']['urls'], |
| 257 | 'country' => @$raw['incountry'], |
| 258 | 'no_country' => @$raw['notincountry'], |
| 259 | 'category' => @$raw['categories'], |
| 260 | 'no_category' => @$raw['not-categories'], |
| 261 | 'posttype' => @$raw['post_types'], |
| 262 | 'no_posttype' => @$raw['not-post_types'], |
| 263 | 'xprofile' => @$raw['xprofile_value'], |
| 264 | 'no_xprofile' => @$raw['not-xprofile_value'], |
| 265 | 'width' => array( |
| 266 | 'min' => @$raw['max_width']['width'], |
| 267 | ), |
| 268 | ) |
| 269 | ); |
| 270 | |
| 271 | // Save the popup as custom posttype. |
| 272 | $popup = new IncPopupItem( $data ); |
| 273 | $popup->save( false ); |
| 274 | |
| 275 | // Mark Popup as migrated |
| 276 | $sql = " |
| 277 | UPDATE {$tbl_popover} |
| 278 | SET migrated=1 |
| 279 | WHERE id=%s |
| 280 | "; |
| 281 | $sql = $wpdb->prepare( $sql, $item->id ); |
| 282 | $wpdb->query( $sql ); |
| 283 | |
| 284 | // Advance counter. |
| 285 | $count += 1; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | self::refresh_order(); |
| 290 | |
| 291 | // Create or update the IP cache table. |
| 292 | $sql = " |
| 293 | CREATE TABLE {$tbl_ip_cache} ( |
| 294 | IP varchar(12) NOT NULL DEFAULT '', |
| 295 | country varchar(2) DEFAULT NULL, |
| 296 | cached bigint(20) DEFAULT NULL, |
| 297 | PRIMARY KEY (IP), |
| 298 | KEY cached (cached) |
| 299 | ) {$charset_collate};"; |
| 300 | |
| 301 | dbDelta( $sql ); |
| 302 | |
| 303 | if ( $count > 0 ) { |
| 304 | lib2()->ui->admin_message( |
| 305 | sprintf( |
| 306 | __( |
| 307 | '<strong>WordPress PopUp</strong><br />' . |
| 308 | 'Your installation was successfully updated to use the ' . |
| 309 | 'latest version of the plugin!<br />' . |
| 310 | '<em>Note: Some PopUp options changed or were replaced. ' . |
| 311 | 'You should have a look at your <a href="%s">PopUps</a> ' . |
| 312 | 'to see if they still look as intended.</em>', PO_LANG |
| 313 | ), |
| 314 | admin_url( 'edit.php?post_type=' . IncPopupItem::POST_TYPE ) |
| 315 | ) |
| 316 | ); |
| 317 | } |
| 318 | |
| 319 | // Migrate the Plugin Settings. |
| 320 | $old_settings = IncPopupDatabase::_get_option( 'popover-settings', array() ); |
| 321 | $settings = array(); |
| 322 | $cur_method = @$old_settings['loadingmethod']; |
| 323 | switch ( $cur_method ) { |
| 324 | case '': |
| 325 | case 'external': $cur_method = 'ajax'; break; |
| 326 | case 'frontloading': $cur_method = 'front'; break; |
| 327 | } |
| 328 | $settings['loadingmethod'] = $cur_method; |
| 329 | |
| 330 | // Migrate Add-Ons to new settings. |
| 331 | // Add-Ons were always saved in the local Options-table by old version. |
| 332 | self::before_db(); |
| 333 | $addons = get_option( 'popover_activated_addons', array() ); |
| 334 | self::after_db(); |
| 335 | |
| 336 | $rules = array( |
| 337 | 'class-popup-rule-browser.php', |
| 338 | 'class-popup-rule-geo.php', |
| 339 | 'class-popup-rule-popup.php', |
| 340 | 'class-popup-rule-referrer.php', |
| 341 | 'class-popup-rule-url.php', |
| 342 | 'class-popup-rule-user.php', |
| 343 | 'class-popup-rule-prosite.php', |
| 344 | ); |
| 345 | |
| 346 | foreach ( $addons as $addon ) { |
| 347 | switch ( $addon ) { |
| 348 | case 'anonymous_loading.php': |
| 349 | case 'testheadfooter.php': |
| 350 | /* Integrated; no option. */ break; |
| 351 | case 'localgeodatabase.php': |
| 352 | $settings['geo_db'] = true; break; |
| 353 | case 'rules-advanced_url.php': |
| 354 | $rules[] = 'class-popup-rule-advurl.php'; break; |
| 355 | case 'rules-categories.php': |
| 356 | $rules[] = 'class-popup-rule-category.php'; break; |
| 357 | case 'rules-max_width.php': |
| 358 | $rules[] = 'class-popup-rule-width.php'; break; |
| 359 | case 'rules-on_exit.php': |
| 360 | $rules[] = 'class-popup-rule-events.php'; break; |
| 361 | case 'rules-onclick.php': |
| 362 | $rules[] = 'class-popup-rule-events.php'; break; |
| 363 | case 'rules-post_types.php': |
| 364 | $rules[] = 'class-popup-rule-posttype.php'; break; |
| 365 | case 'rules-xprofile_value.php': |
| 366 | $rules[] = 'class-popup-rule-xprofile.php'; break; |
| 367 | case 'rules-membership.php': |
| 368 | $rules[] = 'class-popup-rule-membership.php'; break; |
| 369 | case 'rules-wp_roles.php': |
| 370 | $rules[] = 'class-popup-rule-role.php'; break; |
| 371 | } |
| 372 | } |
| 373 | $settings['rules'] = $rules; |
| 374 | |
| 375 | self::set_settings( $settings ); |
| 376 | |
| 377 | // Save the new DB version to options table. |
| 378 | self::_set_option( 'popover_installed', PO_BUILD ); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Returns a popup object. |
| 383 | * |
| 384 | * @since 4.6 |
| 385 | * @param int $post_id ID of the PopUp |
| 386 | * @param bool $clear If TRUE then a new object will be fetched from DB and |
| 387 | * not from cache. |
| 388 | * @return IncPopupItem |
| 389 | */ |
| 390 | static public function get( $post_id, $clear = false ) { |
| 391 | $post_id = absint( $post_id ); |
| 392 | |
| 393 | if ( $clear ) { |
| 394 | $popup = false; |
| 395 | } else { |
| 396 | $popup = wp_cache_get( $post_id, IncPopupItem::POST_TYPE ); |
| 397 | } |
| 398 | |
| 399 | if ( false === $popup ) { |
| 400 | self::before_db(); |
| 401 | $popup = new IncPopupItem( $post_id ); |
| 402 | self::after_db(); |
| 403 | wp_cache_set( $post_id, $popup, IncPopupItem::POST_TYPE ); |
| 404 | } |
| 405 | return $popup; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Returns an array with all IDs of the active popups. |
| 410 | * The IDs are in the correct order (as defined in the admin list) |
| 411 | * |
| 412 | * @since 4.6 |
| 413 | * @return array List of active popups. |
| 414 | */ |
| 415 | static public function get_active_ids() { |
| 416 | global $wpdb; |
| 417 | static $List = null; |
| 418 | |
| 419 | if ( null === $List ) { |
| 420 | self::before_db(); |
| 421 | // Using get_posts() adds support for WPML |
| 422 | $active_posts = get_posts( |
| 423 | array( |
| 424 | 'post_type' => IncPopupItem::POST_TYPE, |
| 425 | 'suppress_filters' => 0, |
| 426 | 'posts_per_page' => -1, |
| 427 | ) |
| 428 | ); |
| 429 | foreach ( $active_posts as $post ) { |
| 430 | $List[] = $post->ID; |
| 431 | } |
| 432 | self::after_db(); |
| 433 | } |
| 434 | |
| 435 | return $List; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Returns the next available value for the popup position (order) |
| 440 | * |
| 441 | * @since 4.6 |
| 442 | * @return int Next free order position; bottom of the list. |
| 443 | */ |
| 444 | static public function next_order() { |
| 445 | global $wpdb; |
| 446 | self::before_db(); |
| 447 | |
| 448 | $sql = " |
| 449 | SELECT menu_order |
| 450 | FROM {$wpdb->posts} |
| 451 | WHERE post_type=%s |
| 452 | ORDER BY menu_order DESC |
| 453 | LIMIT 1 |
| 454 | "; |
| 455 | $sql = $wpdb->prepare( |
| 456 | $sql, |
| 457 | IncPopupItem::POST_TYPE |
| 458 | ); |
| 459 | //wp_die( $sql ); |
| 460 | $pos = $wpdb->get_var( $sql ); |
| 461 | self::after_db(); |
| 462 | |
| 463 | return absint( $pos ) + 1; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Updates the order of all popups that are not in trash. |
| 468 | * |
| 469 | * @since 4.6 |
| 470 | */ |
| 471 | static public function refresh_order() { |
| 472 | global $wpdb; |
| 473 | self::before_db(); |
| 474 | |
| 475 | // 1. Set all trashed popups to order=999999. |
| 476 | $sql_fix = " |
| 477 | UPDATE {$wpdb->posts} |
| 478 | SET menu_order=999999 |
| 479 | WHERE post_type=%s AND post_status=%s |
| 480 | "; |
| 481 | $sql = $wpdb->prepare( |
| 482 | $sql_fix, |
| 483 | IncPopupItem::POST_TYPE, |
| 484 | 'trash' |
| 485 | ); |
| 486 | $wpdb->query( $sql ); |
| 487 | |
| 488 | // 2. Get all active/inactive popups in correct order. |
| 489 | $sql_get = " |
| 490 | SELECT ID, menu_order |
| 491 | FROM {$wpdb->posts} |
| 492 | WHERE post_type=%s |
| 493 | AND post_status IN (%s, %s) |
| 494 | ORDER BY menu_order |
| 495 | "; |
| 496 | $sql = $wpdb->prepare( |
| 497 | $sql_get, |
| 498 | IncPopupItem::POST_TYPE, |
| 499 | 'publish', |
| 500 | 'draft' |
| 501 | ); |
| 502 | $list = $wpdb->get_results( $sql ); |
| 503 | |
| 504 | // 3. Update the menu order of all active/inactive popups. |
| 505 | $sql_fix = " |
| 506 | UPDATE {$wpdb->posts} |
| 507 | SET menu_order=%d |
| 508 | WHERE post_type=%s AND ID=%d |
| 509 | "; |
| 510 | foreach ( $list as $ind => $data ) { |
| 511 | $sql = $wpdb->prepare( |
| 512 | $sql_fix, |
| 513 | ($ind + 1), |
| 514 | IncPopupItem::POST_TYPE, |
| 515 | $data->ID |
| 516 | ); |
| 517 | $wpdb->query( $sql ); |
| 518 | } |
| 519 | self::after_db(); |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Count active PopUps |
| 524 | * |
| 525 | * @since 4.6 |
| 526 | * @param int $id Optional. Don't count this PopUp in the results. |
| 527 | * @return int Number of active PopUps |
| 528 | */ |
| 529 | static public function count_active( $id = '' ) { |
| 530 | global $wpdb; |
| 531 | |
| 532 | if ( ! is_scalar( $id ) ) { $id = ''; } |
| 533 | $sql = " |
| 534 | SELECT COUNT(1) |
| 535 | FROM {$wpdb->posts} |
| 536 | WHERE post_type=%s AND post_status=%s AND ID!=%s |
| 537 | "; |
| 538 | $sql = $wpdb->prepare( $sql, IncPopupItem::POST_TYPE, 'publish', $id ); |
| 539 | $count = $wpdb->get_var( $sql ); |
| 540 | |
| 541 | return $count; |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Deactivate all active PopUps |
| 546 | * |
| 547 | * @since 4.6 |
| 548 | */ |
| 549 | static public function deactivate_all() { |
| 550 | global $wpdb; |
| 551 | |
| 552 | $sql = " |
| 553 | UPDATE {$wpdb->posts} |
| 554 | SET post_status=%s |
| 555 | WHERE post_type=%s AND post_status=%s |
| 556 | "; |
| 557 | $sql = $wpdb->prepare( $sql, 'draft', IncPopupItem::POST_TYPE, 'publish' ); |
| 558 | $wpdb->query( $sql ); |
| 559 | } |
| 560 | |
| 561 | |
| 562 | /*==============================*\ |
| 563 | ================================== |
| 564 | == == |
| 565 | == SETTINGS == |
| 566 | == == |
| 567 | ================================== |
| 568 | \*==============================*/ |
| 569 | |
| 570 | /** |
| 571 | * Returns the list of available loading methods. |
| 572 | * |
| 573 | * @since 4.6.1.1 |
| 574 | * @return array Loading methods displayed in the Settings screen. |
| 575 | */ |
| 576 | static public function get_loading_methods() { |
| 577 | static $Loading_methods = null; |
| 578 | |
| 579 | if ( null === $Loading_methods ) { |
| 580 | $Loading_methods = array(); |
| 581 | |
| 582 | $Loading_methods[] = (object) array( |
| 583 | 'id' => 'footer', |
| 584 | 'label' => __( 'Page Footer', PO_LANG ), |
| 585 | 'info' => __( |
| 586 | 'Include PopUp as part of your site\'s HTML (no AJAX call).', |
| 587 | PO_LANG |
| 588 | ), |
| 589 | ); |
| 590 | |
| 591 | $Loading_methods[] = (object) array( |
| 592 | 'id' => 'ajax', |
| 593 | 'label' => __( 'WordPress AJAX', PO_LANG ), |
| 594 | 'info' => __( |
| 595 | 'Load PopUp separately from the page via a WordPress AJAX call. ' . |
| 596 | 'This is the best option if you use caching.', |
| 597 | PO_LANG |
| 598 | ), |
| 599 | ); |
| 600 | |
| 601 | $Loading_methods[] = (object) array( |
| 602 | 'id' => 'front', |
| 603 | 'label' => __( 'Custom AJAX', PO_LANG ), |
| 604 | 'info' => __( |
| 605 | 'Load PopUp separately from the page via a custom front-end AJAX call.', |
| 606 | PO_LANG |
| 607 | ), |
| 608 | ); |
| 609 | |
| 610 | /** |
| 611 | * Allow addons to register additional loading methods. |
| 612 | * |
| 613 | * @var array |
| 614 | */ |
| 615 | $Loading_methods = apply_filters( |
| 616 | 'popup-settings-loading-method', |
| 617 | $Loading_methods |
| 618 | ); |
| 619 | } |
| 620 | |
| 621 | return $Loading_methods; |
| 622 | } |
| 623 | |
| 624 | /** |
| 625 | * Returns the plugin settings. |
| 626 | * |
| 627 | * @since 4.6 |
| 628 | * @return array. |
| 629 | */ |
| 630 | static public function get_settings() { |
| 631 | $defaults = array( |
| 632 | 'loadingmethod' => 'ajax', |
| 633 | 'geo_lookup' => 'hostip', |
| 634 | 'geo_db' => false, |
| 635 | 'rules' => array( |
| 636 | 'class-popup-rule-browser.php', |
| 637 | 'class-popup-rule-geo.php', |
| 638 | 'class-popup-rule-popup.php', |
| 639 | 'class-popup-rule-referrer.php', |
| 640 | 'class-popup-rule-url.php', |
| 641 | 'class-popup-rule-user.php', |
| 642 | 'class-popup-rule-prosite.php', |
| 643 | ) |
| 644 | ); |
| 645 | |
| 646 | $data = (array) self::_get_option( 'inc_popup-config', array() ); |
| 647 | |
| 648 | if ( ! is_array( $data ) ) { $data = array(); } |
| 649 | foreach ( $defaults as $key => $def_value ) { |
| 650 | if ( ! isset( $data[$key] ) ) { |
| 651 | $data[$key] = $def_value; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | return $data; |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * Saves the plugin settings. |
| 660 | * |
| 661 | * @since 4.6 |
| 662 | * @param array $value The value to save. |
| 663 | */ |
| 664 | static public function set_settings( $value ) { |
| 665 | self::_set_option( 'inc_popup-config', $value ); |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Returns the value of a named flag of the current user. |
| 670 | * Table: User-Meta |
| 671 | * |
| 672 | * @since 4.6 |
| 673 | * @param string $key |
| 674 | * @return mixed |
| 675 | */ |
| 676 | static public function get_flag( $key ) { |
| 677 | $data = get_user_meta( get_current_user_id(), 'po_data', true ); |
| 678 | if ( is_object( $data ) ) { $data = (array) $data; } |
| 679 | if ( ! is_array( $data ) ) { $data = array(); } |
| 680 | |
| 681 | return isset( $data[$key] ) ? $data[$key] : ''; |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Saves a flag for the current user. |
| 686 | * Table: User-Meta |
| 687 | * |
| 688 | * @since 4.6 |
| 689 | * @param string $key |
| 690 | * @param mixed $value |
| 691 | */ |
| 692 | static public function set_flag( $key, $value ) { |
| 693 | $data = get_user_meta( get_current_user_id(), 'po_data', true ); |
| 694 | if ( is_object( $data ) ) { $data = (array) $data; } |
| 695 | if ( ! is_array( $data ) ) { $data = array(); } |
| 696 | $data[$key] = $value; |
| 697 | |
| 698 | update_user_meta( get_current_user_id(), 'po_data', $data ); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Internal function to get a option value from correct options table. |
| 703 | * Table: Blog-Options |
| 704 | * |
| 705 | * @since 4.6 |
| 706 | */ |
| 707 | static protected function _get_option( $key, $default ) { |
| 708 | $value = $default; |
| 709 | self::before_db(); |
| 710 | |
| 711 | if ( IncPopup::use_global() ) { |
| 712 | $value = get_site_option( $key, $default ); |
| 713 | } else { |
| 714 | $value = get_option( $key, $default ); |
| 715 | } |
| 716 | |
| 717 | self::after_db(); |
| 718 | return $value; |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * Internal function to save a value to the correct options table. |
| 723 | * Table: Blog-Options |
| 724 | * |
| 725 | * @since 4.6 |
| 726 | */ |
| 727 | static protected function _set_option( $key, $value ) { |
| 728 | self::before_db(); |
| 729 | |
| 730 | if ( IncPopup::use_global() ) { |
| 731 | update_site_option( $key, $value ); |
| 732 | } else { |
| 733 | update_option( $key, $value ); |
| 734 | } |
| 735 | |
| 736 | self::after_db(); |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * Selects the correct database, in case the PO_GLOBAL flag is true. |
| 741 | * |
| 742 | * @since 4.6 |
| 743 | */ |
| 744 | static public function before_db() { |
| 745 | if ( IncPopup::use_global() ) { |
| 746 | switch_to_blog( BLOG_ID_CURRENT_SITE ); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * Selects the correct database, in case the PO_GLOBAL flag is true. |
| 752 | * |
| 753 | * @since 4.6 |
| 754 | */ |
| 755 | static public function after_db() { |
| 756 | if ( IncPopup::use_global() ) { |
| 757 | restore_current_blog(); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | |
| 762 | /*==============================*\ |
| 763 | ================================== |
| 764 | == == |
| 765 | == IP CACHE == |
| 766 | == == |
| 767 | ================================== |
| 768 | \*==============================*/ |
| 769 | |
| 770 | |
| 771 | /** |
| 772 | * Returns the country code that is associated with the IP Address from the |
| 773 | * local cache table. |
| 774 | * |
| 775 | * @since 4.6 |
| 776 | * @param string $ip The IP Address. |
| 777 | * @return string Associated country code (or empty string). |
| 778 | */ |
| 779 | static public function get_country( $ip ) { |
| 780 | global $wpdb; |
| 781 | $Country = array(); |
| 782 | |
| 783 | if ( ! isset( $Country[$ip] ) ) { |
| 784 | $ip_table = self::db_prefix( self::IP_TABLE ); |
| 785 | $sql = " |
| 786 | SELECT country |
| 787 | FROM {$ip_table} |
| 788 | WHERE IP = %s |
| 789 | "; |
| 790 | $sql = $wpdb->prepare( $sql, $ip ); |
| 791 | $Country[$ip] = $wpdb->get_var( $sql ); |
| 792 | |
| 793 | if ( null === $Country[$ip] ) { $Country[$ip] = ''; } |
| 794 | } |
| 795 | |
| 796 | return $Country[$ip]; |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Adds information to the IP-Cache table. |
| 801 | * |
| 802 | * @since 4.6 |
| 803 | * @param string $ip The IP Address. |
| 804 | * @param string $country Country code. |
| 805 | */ |
| 806 | static public function add_ip( $ip, $country ) { |
| 807 | global $wpdb; |
| 808 | |
| 809 | $ip_table = self::db_prefix( self::IP_TABLE ); |
| 810 | |
| 811 | // Delete the cached data, if it already exists. |
| 812 | $sql = " |
| 813 | DELETE FROM $ip_table |
| 814 | WHERE IP = %s |
| 815 | "; |
| 816 | $sql = $wpdb->prepare( $sql, $ip ); |
| 817 | $wpdb->query( $sql ); |
| 818 | |
| 819 | // Insert the new dataset. |
| 820 | $sql = " |
| 821 | INSERT INTO $ip_table (IP, country, cached) |
| 822 | VALUES (%s, %s, %s) |
| 823 | "; |
| 824 | $sql = $wpdb->prepare( $sql, $ip, $country, time() ); |
| 825 | $wpdb->query( $sql ); |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Clears the IP-Country cache |
| 830 | * |
| 831 | * @since 4.6.1.1 |
| 832 | */ |
| 833 | static public function clear_ip_cache() { |
| 834 | global $wpdb; |
| 835 | |
| 836 | $ip_table = self::db_prefix( self::IP_TABLE ); |
| 837 | |
| 838 | // Delete the cached data, if it already exists. |
| 839 | $sql = "TRUNCATE TABLE $ip_table"; |
| 840 | $wpdb->query( $sql ); |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * Returns a list of available ip-resolution services. |
| 845 | * |
| 846 | * @since 4.6.1.1 |
| 847 | * @return array List of available webservices. |
| 848 | */ |
| 849 | static public function get_geo_services() { |
| 850 | static $Geo_service = null; |
| 851 | |
| 852 | if ( null === $Geo_service ) { |
| 853 | $Geo_service = array(); |
| 854 | |
| 855 | $Geo_service['hostip'] = (object) array( |
| 856 | 'label' => 'Host IP', |
| 857 | 'url' => 'http://api.hostip.info/country.php?ip=%ip%', |
| 858 | 'type' => 'text', |
| 859 | ); |
| 860 | |
| 861 | $Geo_service['telize'] = (object) array( |
| 862 | 'label' => 'Telize', |
| 863 | 'url' => 'http://www.telize.com/geoip/%ip%', |
| 864 | 'type' => 'json', |
| 865 | 'field' => 'country_code', |
| 866 | ); |
| 867 | |
| 868 | $Geo_service['freegeo'] = (object) array( |
| 869 | 'label' => 'Free Geo IP', |
| 870 | 'url' => 'http://freegeoip.net/json/%ip%', |
| 871 | 'type' => 'json', |
| 872 | 'field' => 'country_code', |
| 873 | ); |
| 874 | |
| 875 | /** |
| 876 | * Allow other modules/plugins to register a geo service. |
| 877 | */ |
| 878 | $Geo_service = apply_filters( 'popup-geo-services', $Geo_service ); |
| 879 | } |
| 880 | |
| 881 | return $Geo_service; |
| 882 | } |
| 883 | |
| 884 | } |
| 885 |