Changeset 3366120
- Timestamp:
- 09/23/2025 02:14:52 AM (4 months ago)
- Location:
- ad-unblock/trunk
- Files:
-
- 4 edited
-
ad-unblock.php (modified) (7 diffs)
-
assets/js/admin.js (modified) (2 diffs)
-
readme.txt (modified) (2 diffs)
-
uninstall.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
ad-unblock/trunk/ad-unblock.php
r3342583 r3366120 4 4 * Plugin Name: Ad Unblock 5 5 * Description: Integrates your WordPress site with Ad Unblock service to recover ad revenue lost to ad blockers. 6 * Version: 1. 0.66 * Version: 1.1.0 7 7 * Author: Ad Unblock 8 8 * Author URI: https://ad-unblock.com … … 17 17 } 18 18 19 define('AD_UNBLOCK_VERSION', '1. 0.6');19 define('AD_UNBLOCK_VERSION', '1.1.0'); 20 20 define('AD_UNBLOCK_PLUGIN_DIR', plugin_dir_path(__FILE__)); 21 21 define('AD_UNBLOCK_PLUGIN_URL', plugin_dir_url(__FILE__)); … … 380 380 <strong><?php esc_html_e('Cache Expires:', 'ad-unblock'); ?></strong> 381 381 <?php if ($status['cache_expires']): ?> 382 <?php echo esc_html($status['cache_expires']); ?> 382 <span class="cache-expires-time" data-timestamp="<?php echo esc_attr($status['cache_expires']); ?>"> 383 <?php echo esc_html(date('Y-m-d H:i:s', $status['cache_expires'])); ?> (server time) 384 </span> 383 385 <br><small>(<?php echo esc_html($status['time_until_expiry']); ?>s remaining)</small> 384 386 <?php else: ?> … … 486 488 $cache_key = 'ad_unblock_script_sources_' . get_current_blog_id(); 487 489 $error_cache_key = $cache_key . '_error'; 488 $cached = get_transient($cache_key); 489 490 // Check if we're in an error state 491 $error_cached = get_transient($error_cache_key); 492 493 if (false !== $cached && false === $error_cached) { 494 return $cached; 495 } 496 497 // If we have cached data but are in error state, try again after shorter interval 498 if (false !== $cached && false !== $error_cached) { 499 // Try API call, but have fallback to stale cache 500 $response = wp_remote_get(self::API_ENDPOINT, array('timeout' => 10)); 501 502 if (!is_wp_error($response) && 200 === wp_remote_retrieve_response_code($response)) { 503 // Success! Clear error state and update cache 504 delete_transient($error_cache_key); 505 $body = wp_remote_retrieve_body($response); 506 $script_sources = json_decode($body, true); 507 508 if (is_array($script_sources) && !empty($script_sources)) { 509 set_transient($cache_key, $script_sources, self::CACHE_DURATION); 510 return $script_sources; 511 } 490 491 // Get cached data and check if it's valid 492 $cached_data = get_transient($cache_key); 493 $is_cache_valid = $this->is_cache_data_valid($cached_data); 494 $is_in_error_state = $this->is_in_error_state($error_cache_key); 495 496 // Return valid cache if not in error state 497 if ($is_cache_valid && !$is_in_error_state) { 498 return $cached_data['data']; 499 } 500 501 // Try to fetch fresh data from API 502 $fresh_data = $this->fetch_script_sources_from_api(); 503 504 // If API call succeeded, update cache and return fresh data 505 if (false !== $fresh_data) { 506 $this->refresh_script_sources_cache($cache_key, $error_cache_key, $fresh_data); 507 return $fresh_data; 508 } 509 510 // API failed - set error state 511 $this->set_error_state($error_cache_key); 512 513 // Return stale cache if available, otherwise empty array 514 if ($cached_data && isset($cached_data['data']) && is_array($cached_data['data'])) { 515 return $cached_data['data']; 516 } 517 518 return array(); 519 } 520 521 /** 522 * Check if cached data is valid and not expired 523 */ 524 private function is_cache_data_valid($cached_data) 525 { 526 return false !== $cached_data && 527 is_array($cached_data) && 528 isset($cached_data['timestamp'], $cached_data['data']) && 529 is_array($cached_data['data']) && 530 !empty($cached_data['data']) && 531 (time() - $cached_data['timestamp']) < self::CACHE_DURATION; 532 } 533 534 /** 535 * Check if we're currently in an error state 536 */ 537 private function is_in_error_state($error_cache_key) 538 { 539 $error_data = get_transient($error_cache_key); 540 if (false === $error_data || !is_array($error_data) || !isset($error_data['timestamp'])) { 541 return false; 542 } 543 544 // Consider error state active for 60 seconds 545 return (time() - $error_data['timestamp']) < 60; 546 } 547 548 /** 549 * Fetch script sources from API 550 */ 551 private function fetch_script_sources_from_api() 552 { 553 $response = wp_remote_get(self::API_ENDPOINT, array('timeout' => 10)); 554 555 if (is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) { 556 return false; 557 } 558 559 $body = wp_remote_retrieve_body($response); 560 if (empty($body)) { 561 return false; 562 } 563 564 $script_sources = json_decode($body, true); 565 566 // Validate JSON structure and content 567 if (!is_array($script_sources) || empty($script_sources)) { 568 return false; 569 } 570 571 // Additional validation: ensure all items are strings (URLs) 572 foreach ($script_sources as $source) { 573 if (!is_string($source) || empty($source)) { 574 return false; 512 575 } 513 514 // Still failing, return stale cache and extend error period 515 set_transient($error_cache_key, true, 60); 516 return $cached; 517 } 518 519 // No cache at all, try API 520 $response = wp_remote_get(self::API_ENDPOINT, array('timeout' => 10)); 521 522 if (is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) { 523 // Set error state and return empty (no previous cache to fall back to) 524 set_transient($error_cache_key, true, 60); 525 return array(); 526 } 527 528 $body = wp_remote_retrieve_body($response); 529 $script_sources = json_decode($body, true); 530 531 if (!is_array($script_sources) || empty($script_sources)) { 532 set_transient($error_cache_key, true, 60); 533 return array(); 534 } 535 536 // Success! 537 set_transient($cache_key, $script_sources, self::CACHE_DURATION); 576 } 577 538 578 return $script_sources; 579 } 580 581 /** 582 * Refresh script sources cache with fresh data and clear error state 583 */ 584 private function refresh_script_sources_cache($cache_key, $error_cache_key, $script_sources) 585 { 586 // Clear error state since we have fresh data 587 delete_transient($error_cache_key); 588 589 // Store fresh data with timestamp 590 $cache_data = array( 591 'data' => $script_sources, 592 'timestamp' => time() 593 ); 594 set_transient($cache_key, $cache_data, WEEK_IN_SECONDS); 595 } 596 597 /** 598 * Set error state with timestamp 599 */ 600 private function set_error_state($error_cache_key) 601 { 602 $error_data = array( 603 'timestamp' => time() 604 ); 605 set_transient($error_cache_key, $error_data, HOUR_IN_SECONDS); 539 606 } 540 607 … … 559 626 $script_url = reset($script_sources); 560 627 561 // Get cache status for meta tag628 // Get cache status and full cached data for meta tag 562 629 $cache_status = $this->get_cache_status(); 563 630 $cache_key = 'ad_unblock_script_sources_' . get_current_blog_id(); 631 $cached_data = get_transient($cache_key); 632 564 633 // Prepare cache details for meta tag 565 634 $cache_details = array( … … 569 638 'sources_count' => $cache_status['cache_data_count'], 570 639 'expires_in' => $cache_status['time_until_expiry'], 571 'timestamp' => time() 640 'timestamp' => time(), 641 'cached_sources' => $script_sources, 642 'cache_timestamp' => (false !== $cached_data && is_array($cached_data) && isset($cached_data['timestamp'])) ? $cached_data['timestamp'] : null 572 643 ); 573 644 … … 721 792 $cache_key = 'ad_unblock_script_sources_' . get_current_blog_id(); 722 793 $error_cache_key = $cache_key . '_error'; 723 $cached = get_transient($cache_key); 724 $error_cached = get_transient($error_cache_key); 725 $timeout = get_option('_transient_timeout_' . $cache_key); 794 $cached_data = get_transient($cache_key); 795 $error_data = get_transient($error_cache_key); 796 797 $has_cache = false; 798 $cache_timestamp = null; 799 $cache_data_count = 0; 800 $time_until_expiry = 0; 801 $cache_expires = null; 802 803 // Check if we have valid cached data with timestamp 804 if (false !== $cached_data && is_array($cached_data) && isset($cached_data['timestamp'], $cached_data['data'])) { 805 $has_cache = true; 806 $cache_timestamp = $cached_data['timestamp']; 807 $cache_data_count = is_array($cached_data['data']) ? count($cached_data['data']) : 0; 808 $cache_expiry_time = $cache_timestamp + self::CACHE_DURATION; 809 $time_until_expiry = max(0, $cache_expiry_time - time()); 810 $cache_expires = $cache_expiry_time; 811 } 812 813 // Check error state - only consider recent errors (within 60 seconds) 814 $has_error = false; 815 if (false !== $error_data && is_array($error_data) && isset($error_data['timestamp'])) { 816 $has_error = (time() - $error_data['timestamp']) < 60; 817 } 726 818 727 819 return array( 728 'has_cache' => false !== $cached,729 'has_error' => false !== $error_cached,730 'cache_expires' => $ timeout ? date('Y-m-d H:i:s', $timeout) : null,731 'cache_data_count' => is_array($cached) ? count($cached) : 0,732 'time_until_expiry' => $time out ? max(0, $timeout - time()) : 0820 'has_cache' => $has_cache, 821 'has_error' => $has_error, 822 'cache_expires' => $cache_expires, 823 'cache_data_count' => $cache_data_count, 824 'time_until_expiry' => $time_until_expiry 733 825 ); 734 826 } -
ad-unblock/trunk/assets/js/admin.js
r3342583 r3366120 1 1 jQuery(document).ready(function ($) { 2 // Convert server timestamps to local time 3 $('.cache-expires-time').each(function() { 4 var timestamp = $(this).data('timestamp'); 5 if (timestamp) { 6 var date = new Date(timestamp * 1000); 7 $(this).text(date.toLocaleString() + ' (local time)'); 8 } 9 }); 2 10 // Initialize Select2 dropdowns 3 11 $(".ad-unblock-term-selector").each(function () { … … 129 137 var expirationHtml = ""; 130 138 if (status.cache_expires) { 139 var expirationDate = new Date(status.cache_expires * 1000); 131 140 expirationHtml = 132 status.cache_expires + 133 "<br><small>(" + 141 '<span class="cache-expires-time" data-timestamp="' + status.cache_expires + '">' + 142 expirationDate.toLocaleString() + " (local time)" + 143 "</span><br><small>(" + 134 144 status.time_until_expiry + 135 145 "s remaining)</small>"; -
ad-unblock/trunk/readme.txt
r3342583 r3366120 4 4 Requires at least: 5.0 5 5 Tested up to: 6.8 6 Stable tag: 1. 0.66 Stable tag: 1.1.0 7 7 Requires PHP: 7.0 8 8 License: GPLv2 or later … … 75 75 == Changelog == 76 76 77 = 1.1.0 = 78 * Fix an issue that cached resource is not refreshed properly when external cache object is enabled 79 77 80 = 1.0.6 = 78 81 * Improved cache management -
ad-unblock/trunk/uninstall.php
r3342583 r3366120 4 4 * 5 5 * @link https://ad-unblock.com 6 * @since 1. 0.66 * @since 1.1.0 7 7 * 8 8 * @package Ad_Unblock
Note: See TracChangeset
for help on using the changeset viewer.