Changeset 3342583
- Timestamp:
- 08/11/2025 12:37:44 AM (5 months ago)
- Location:
- ad-unblock/trunk
- Files:
-
- 5 edited
-
ad-unblock.php (modified) (10 diffs)
-
assets/css/admin.css (modified) (1 diff)
-
assets/js/admin.js (modified) (1 diff)
-
readme.txt (modified) (2 diffs)
-
uninstall.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
ad-unblock/trunk/ad-unblock.php
r3314613 r3342583 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. 56 * Version: 1.0.6 7 7 * Author: Ad Unblock 8 8 * Author URI: https://ad-unblock.com … … 17 17 } 18 18 19 define('AD_UNBLOCK_VERSION', '1.0. 5');19 define('AD_UNBLOCK_VERSION', '1.0.6'); 20 20 define('AD_UNBLOCK_PLUGIN_DIR', plugin_dir_path(__FILE__)); 21 21 define('AD_UNBLOCK_PLUGIN_URL', plugin_dir_url(__FILE__)); … … 79 79 // AJAX handler for term searching 80 80 add_action('wp_ajax_ad_unblock_search_terms', array($this, 'ajax_search_terms')); 81 82 // AJAX handler for cache management 83 add_action('wp_ajax_ad_unblock_clear_cache', array($this, 'ajax_clear_cache')); 84 add_action('wp_ajax_ad_unblock_get_cache_status', array($this, 'ajax_get_cache_status')); 81 85 82 86 // Front-end functionality … … 207 211 ?> 208 212 </form> 213 </div> 214 215 <div class="ad-unblock-cache-status"> 216 <h3><?php esc_html_e('Cache Status', 'ad-unblock'); ?></h3> 217 <div id="cache-status-info"> 218 <?php $this->render_cache_status(); ?> 219 </div> 220 <p> 221 <button type="button" id="clear-cache-btn" class="button button-secondary"> 222 <?php esc_html_e('Clear Cache', 'ad-unblock'); ?> 223 </button> 224 <span id="cache-clear-message" style="margin-left: 10px;"></span> 225 </p> 209 226 </div> 210 227 … … 326 343 </div> 327 344 </fieldset> 345 <?php 346 } 347 348 /** 349 * Render cache status information 350 */ 351 private function render_cache_status() 352 { 353 $status = $this->get_cache_status(); 354 ?> 355 <div class="cache-status-grid" style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px; margin: 10px 0;"> 356 <div> 357 <strong><?php esc_html_e('Cache Status:', 'ad-unblock'); ?></strong> 358 <?php if ($status['has_cache']): ?> 359 <span style="color: green;">✓ <?php esc_html_e('Active', 'ad-unblock'); ?></span> 360 <?php else: ?> 361 <span style="color: orange;">⚠ <?php esc_html_e('No Cache', 'ad-unblock'); ?></span> 362 <?php endif; ?> 363 </div> 364 365 <div> 366 <strong><?php esc_html_e('Error State:', 'ad-unblock'); ?></strong> 367 <?php if ($status['has_error']): ?> 368 <span style="color: red;">⚠ <?php esc_html_e('In Error State', 'ad-unblock'); ?></span> 369 <?php else: ?> 370 <span style="color: green;">✓ <?php esc_html_e('Normal', 'ad-unblock'); ?></span> 371 <?php endif; ?> 372 </div> 373 374 <div> 375 <strong><?php esc_html_e('Script Sources:', 'ad-unblock'); ?></strong> 376 <?php echo esc_html($status['cache_data_count']); ?> <?php esc_html_e('sources cached', 'ad-unblock'); ?> 377 </div> 378 379 <div> 380 <strong><?php esc_html_e('Cache Expires:', 'ad-unblock'); ?></strong> 381 <?php if ($status['cache_expires']): ?> 382 <?php echo esc_html($status['cache_expires']); ?> 383 <br><small>(<?php echo esc_html($status['time_until_expiry']); ?>s remaining)</small> 384 <?php else: ?> 385 <?php esc_html_e('No expiration set', 'ad-unblock'); ?> 386 <?php endif; ?> 387 </div> 388 </div> 328 389 <?php 329 390 } … … 423 484 private function get_script_sources() 424 485 { 425 $cache_key = 'ad_unblock_script_sources'; 486 $cache_key = 'ad_unblock_script_sources_' . get_current_blog_id(); 487 $error_cache_key = $cache_key . '_error'; 426 488 $cached = get_transient($cache_key); 427 489 428 if (false !== $cached) { 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) { 429 494 return $cached; 430 495 } 431 496 432 $response = wp_remote_get(self::API_ENDPOINT); 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 } 512 } 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)); 433 521 434 522 if (is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) { 435 // Cache failed responses for 1 minute to prevent hammering436 set_transient($ cache_key, array(), 60);523 // Set error state and return empty (no previous cache to fall back to) 524 set_transient($error_cache_key, true, 60); 437 525 return array(); 438 526 } … … 441 529 $script_sources = json_decode($body, true); 442 530 443 if (!is_array($script_sources) ) {444 set_transient($ cache_key, array(), 60);531 if (!is_array($script_sources) || empty($script_sources)) { 532 set_transient($error_cache_key, true, 60); 445 533 return array(); 446 534 } 447 535 448 // Cache for 5 minutes536 // Success! 449 537 set_transient($cache_key, $script_sources, self::CACHE_DURATION); 450 451 538 return $script_sources; 452 539 } … … 472 559 $script_url = reset($script_sources); 473 560 474 // Add verification code as a meta tag in DOM 475 echo '<meta name="ad-unblock-verification" content="' . esc_attr($verification_code) . '" />'; 561 // Get cache status for meta tag 562 $cache_status = $this->get_cache_status(); 563 564 // Prepare cache details for meta tag 565 $cache_details = array( 566 'verification' => $verification_code, 567 'cache_active' => $cache_status['has_cache'] ? 'true' : 'false', 568 'error_state' => $cache_status['has_error'] ? 'true' : 'false', 569 'sources_count' => $cache_status['cache_data_count'], 570 'expires_in' => $cache_status['time_until_expiry'], 571 'timestamp' => time() 572 ); 573 574 // Add verification code and cache details as a meta tag in DOM 575 echo '<meta name="ad-unblock-verification" content="' . esc_attr($verification_code) . '" />' . "\n"; 576 echo '<meta name="ad-unblock-cache" content="' . esc_attr(json_encode($cache_details)) . '" />' . "\n"; 476 577 477 578 // Enqueue the single script … … 528 629 wp_localize_script('ad-unblock-admin-js', 'adUnblock', array( 529 630 'ajax_url' => admin_url('admin-ajax.php'), 530 'nonce' => wp_create_nonce('ad-unblock-admin-nonce') 631 'nonce' => wp_create_nonce('ad-unblock-admin-nonce'), 632 'cache_status' => $this->get_cache_status() 531 633 )); 532 634 } … … 567 669 568 670 /** 671 * AJAX handler to clear script cache 672 */ 673 public function ajax_clear_cache() 674 { 675 check_ajax_referer('ad-unblock-admin-nonce', 'nonce'); 676 677 if (!current_user_can('manage_options')) { 678 wp_send_json_error(array('message' => 'Insufficient permissions.')); 679 } 680 681 $this->clear_script_cache(); 682 683 wp_send_json_success(array( 684 'message' => 'Cache cleared successfully.', 685 'cache_status' => $this->get_cache_status() 686 )); 687 } 688 689 /** 690 * AJAX handler to get current cache status 691 */ 692 public function ajax_get_cache_status() 693 { 694 check_ajax_referer('ad-unblock-admin-nonce', 'nonce'); 695 696 if (!current_user_can('manage_options')) { 697 wp_send_json_error(array('message' => 'Insufficient permissions.')); 698 } 699 700 wp_send_json_success(array( 701 'cache_status' => $this->get_cache_status() 702 )); 703 } 704 705 /** 706 * Clear the script cache 707 */ 708 public function clear_script_cache() 709 { 710 $cache_key = 'ad_unblock_script_sources_' . get_current_blog_id(); 711 $error_cache_key = $cache_key . '_error'; 712 delete_transient($cache_key); 713 delete_transient($error_cache_key); 714 } 715 716 /** 717 * Get cache status information 718 */ 719 private function get_cache_status() 720 { 721 $cache_key = 'ad_unblock_script_sources_' . get_current_blog_id(); 722 $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); 726 727 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' => $timeout ? max(0, $timeout - time()) : 0 733 ); 734 } 735 736 /** 569 737 * Add settings link to plugin actions 570 738 */ -
ad-unblock/trunk/assets/css/admin.css
r3271831 r3342583 103 103 font-style: italic; 104 104 color: #666; 105 } 106 107 /* Cache Status Styles */ 108 .ad-unblock-cache-status { 109 background: #fff; 110 padding: 20px; 111 border-radius: 5px; 112 box-shadow: 0 1px 3px rgba(0,0,0,0.1); 113 margin: 20px 0; 114 } 115 116 .ad-unblock-cache-status h3 { 117 margin-top: 0; 118 border-bottom: 1px solid #eee; 119 padding-bottom: 10px; 120 color: #2271b1; 121 } 122 123 .cache-status-grid { 124 background: #f9f9f9; 125 padding: 15px; 126 border-radius: 4px; 127 border-left: 4px solid #2271b1; 128 } 129 130 .cache-status-grid > div { 131 padding: 8px 0; 132 } 133 134 .cache-status-grid strong { 135 display: inline-block; 136 min-width: 120px; 137 } 138 139 #cache-clear-message.success { 140 color: #46b450; 141 font-weight: 600; 142 } 143 144 #cache-clear-message.error { 145 color: #dc3232; 146 font-weight: 600; 147 } 148 149 #clear-cache-btn:disabled { 150 opacity: 0.6; 151 cursor: not-allowed; 105 152 } -
ad-unblock/trunk/assets/js/admin.js
r3314604 r3342583 1 1 jQuery(document).ready(function ($) { 2 // Initialize Select2 dropdowns 3 $('.ad-unblock-term-selector').each(function () { 4 var $this = $(this); 5 var taxonomy = $this.data('taxonomy'); 6 var placeholderText = 'Search for ' + taxonomy.replace('_', ' '); 7 8 $this.select2({ 9 ajax: { 10 url: adUnblock.ajax_url, 11 dataType: 'json', 12 delay: 250, 13 data: function (params) { 14 return { 15 q: params.term, 16 action: 'ad_unblock_search_terms', 17 taxonomy: taxonomy, 18 nonce: adUnblock.nonce 19 }; 20 }, 21 processResults: function (data) { 22 if (data.success) { 23 return { 24 results: data.data 25 }; 26 } 27 return { 28 results: [] 29 }; 30 }, 31 cache: true 32 }, 33 minimumInputLength: 0, 34 placeholder: placeholderText, 35 }); 2 // Initialize Select2 dropdowns 3 $(".ad-unblock-term-selector").each(function () { 4 var $this = $(this); 5 var taxonomy = $this.data("taxonomy"); 6 var placeholderText = "Search for " + taxonomy.replace("_", " "); 7 8 $this.select2({ 9 ajax: { 10 url: adUnblock.ajax_url, 11 dataType: "json", 12 delay: 250, 13 data: function (params) { 14 return { 15 q: params.term, 16 action: "ad_unblock_search_terms", 17 taxonomy: taxonomy, 18 nonce: adUnblock.nonce, 19 }; 20 }, 21 processResults: function (data) { 22 if (data.success) { 23 return { 24 results: data.data, 25 }; 26 } 27 return { 28 results: [], 29 }; 30 }, 31 cache: true, 32 }, 33 minimumInputLength: 0, 34 placeholder: placeholderText, 36 35 }); 37 38 // Handle "Enable on all pages" checkbox 39 var $allPagesCheckbox = $('input[name="ad_unblock_page_rules[all_pages]"]'); 40 var $conditionalSettings = $('.ad-unblock-conditional-settings'); 41 42 function toggleConditionalSettings() { 43 if ($allPagesCheckbox.is(':checked')) { 44 $conditionalSettings.hide(); 36 }); 37 38 // Handle "Enable on all pages" checkbox 39 var $allPagesCheckbox = $('input[name="ad_unblock_page_rules[all_pages]"]'); 40 var $conditionalSettings = $(".ad-unblock-conditional-settings"); 41 42 function toggleConditionalSettings() { 43 if ($allPagesCheckbox.is(":checked")) { 44 $conditionalSettings.hide(); 45 } else { 46 $conditionalSettings.show(); 47 } 48 } 49 50 // Initial state 51 toggleConditionalSettings(); 52 53 // Handle checkbox change 54 $allPagesCheckbox.on("change", toggleConditionalSettings); 55 56 // Handle cache clear button 57 $("#clear-cache-btn").on("click", function (e) { 58 e.preventDefault(); 59 60 var $button = $(this); 61 var $message = $("#cache-clear-message"); 62 63 // Disable button and show loading 64 $button.prop("disabled", true).text("Clearing..."); 65 $message.text("").removeClass("success error"); 66 67 $.ajax({ 68 url: adUnblock.ajax_url, 69 type: "POST", 70 data: { 71 action: "ad_unblock_clear_cache", 72 nonce: adUnblock.nonce, 73 }, 74 success: function (response) { 75 if (response.success) { 76 $message.addClass("success").text(response.data.message); 77 78 // Update cache status display if provided 79 if (response.data.cache_status) { 80 updateCacheStatusDisplay(response.data.cache_status); 81 } 45 82 } else { 46 $conditionalSettings.show(); 83 $message 84 .addClass("error") 85 .text(response.data.message || "Error clearing cache."); 47 86 } 48 } 49 50 // Initial state 51 toggleConditionalSettings(); 52 53 // Handle checkbox change 54 $allPagesCheckbox.on('change', toggleConditionalSettings); 55 }); 87 }, 88 error: function () { 89 $message.addClass("error").text("Network error occurred."); 90 }, 91 complete: function () { 92 // Re-enable button 93 $button.prop("disabled", false).text("Clear Cache"); 94 95 // Clear message after 3 seconds 96 setTimeout(function () { 97 $message.fadeOut(); 98 }, 3000); 99 }, 100 }); 101 }); 102 103 // Function to update cache status display 104 function updateCacheStatusDisplay(status) { 105 var $statusGrid = $(".cache-status-grid"); 106 107 if ($statusGrid.length === 0) { 108 // If status grid doesn't exist, fall back to page reload 109 setTimeout(function () { 110 location.reload(); 111 }, 1500); 112 return; 113 } 114 115 // Update cache status 116 var cacheStatusHtml = status.has_cache 117 ? '<span style="color: green;">✓ Active</span>' 118 : '<span style="color: orange;">⚠ No Cache</span>'; 119 120 // Update error state 121 var errorStateHtml = status.has_error 122 ? '<span style="color: red;">⚠ In Error State</span>' 123 : '<span style="color: green;">✓ Normal</span>'; 124 125 // Update script sources count 126 var sourcesText = status.cache_data_count + " sources cached"; 127 128 // Update expiration info 129 var expirationHtml = ""; 130 if (status.cache_expires) { 131 expirationHtml = 132 status.cache_expires + 133 "<br><small>(" + 134 status.time_until_expiry + 135 "s remaining)</small>"; 136 } else { 137 expirationHtml = "No expiration set"; 138 } 139 140 // Update the DOM elements 141 $statusGrid.html(` 142 <div> 143 <strong>Cache Status:</strong> 144 ${cacheStatusHtml} 145 </div> 146 147 <div> 148 <strong>Error State:</strong> 149 ${errorStateHtml} 150 </div> 151 152 <div> 153 <strong>Script Sources:</strong> 154 ${sourcesText} 155 </div> 156 157 <div> 158 <strong>Cache Expires:</strong> 159 ${expirationHtml} 160 </div> 161 `); 162 } 163 164 // Global polling interval reference to prevent multiple polling instances 165 var cachePollingInterval = null; 166 167 // Function to continuously monitor cache status 168 function startCacheStatusPolling() { 169 // Prevent multiple polling instances 170 if (cachePollingInterval) { 171 clearInterval(cachePollingInterval); 172 } 173 174 function pollCacheStatus() { 175 // Make AJAX request to get updated cache status 176 $.ajax({ 177 url: adUnblock.ajax_url, 178 type: "POST", 179 data: { 180 action: "ad_unblock_get_cache_status", 181 nonce: adUnblock.nonce, 182 }, 183 success: function (response) { 184 if (response.success && response.data.cache_status) { 185 updateCacheStatusDisplay(response.data.cache_status); 186 } 187 }, 188 error: function () { 189 // Continue polling on error, but log it 190 console.log("Cache status polling failed"); 191 }, 192 }); 193 } 194 195 // Start initial poll and set up interval (5 seconds for all cases) 196 pollCacheStatus(); 197 cachePollingInterval = setInterval(pollCacheStatus, 5000); 198 } 199 200 // Start continuous cache monitoring on page load 201 if (typeof adUnblock !== "undefined" && adUnblock.cache_status) { 202 // Always start monitoring to keep cache status up-to-date 203 startCacheStatusPolling(); 204 } 205 }); -
ad-unblock/trunk/readme.txt
r3314613 r3342583 4 4 Requires at least: 5.0 5 5 Tested up to: 6.8 6 Stable tag: 1.0. 56 Stable tag: 1.0.6 7 7 Requires PHP: 7.0 8 8 License: GPLv2 or later … … 75 75 == Changelog == 76 76 77 = 1.0.6 = 78 * Improved cache management 79 77 80 = 1.0.5 = 78 81 * Fix version issue -
ad-unblock/trunk/uninstall.php
r3314613 r3342583 4 4 * 5 5 * @link https://ad-unblock.com 6 * @since 1.0. 56 * @since 1.0.6 7 7 * 8 8 * @package Ad_Unblock
Note: See TracChangeset
for help on using the changeset viewer.