| @@ -1,21 +1,27 @@ | ||
| 1 | 1 | jQuery(document).ready(function ($) { |
| 2 | 2 | |
| 3 | 3 | $('button[name="tptn_cache_clear"]').on('click', function () { |
| 4 | - if (confirm(top_ten_admin_data.strings.confirm_message)) { | |
| 5 | - var $button = $(this); | |
| 4 | + var $button = $(this); | |
| 5 | + var networkWide = '1' === $button.attr('data-network-wide'); | |
| 6 | + var confirmMessage = networkWide && top_ten_admin_data.strings.network_confirm_message | |
| 7 | + ? top_ten_admin_data.strings.network_confirm_message | |
| 8 | + : top_ten_admin_data.strings.confirm_message; | |
| 9 | + | |
| 10 | + if (confirm(confirmMessage)) { | |
| 6 | 11 | var originalText = $button.text(); |
| 7 | 12 | var clearingText = top_ten_admin_data.strings.clearing_text ? top_ten_admin_data.strings.clearing_text : 'Clearing...'; |
| 8 | 13 | $button.prop('disabled', true).text(clearingText).append(' <span class="spinner is-active"></span>'); |
| 9 | - clearCache($button, originalText); | |
| 14 | + clearCache($button, originalText, networkWide); | |
| 10 | 15 | } |
| 11 | 16 | }); |
| 12 | 17 | |
| 13 | 18 | // Function to clear the cache. |
| 14 | - function clearCache($button, originalText) { | |
| 19 | + function clearCache($button, originalText, networkWide) { | |
| 15 | 20 | $.post(ajaxurl, { |
| 16 | 21 | action: 'tptn_clear_cache', |
| 17 | - security: top_ten_admin_data.security | |
| 22 | + security: top_ten_admin_data.security, | |
| 23 | + network_wide: networkWide ? '1' : '0' | |
| 18 | 24 | }, function (response) { |
| 19 | 25 | if (response.success) { |
| 20 | 26 | alert(response.data.message); |
| 21 | 27 | } else { |
| @@ -33,112 +39,100 @@ | ||
| 33 | 39 | /** |
| 34 | 40 | * Handle style changes and enforce valid thumbnail location settings. |
| 35 | 41 | */ |
| 36 | 42 | function tptnHandleThumbnailStyleChange() { |
| 37 | - var $styleSelect = $('select[name="tptn_settings[tptn_styles]"]'); | |
| 38 | - if (!$styleSelect.length) { | |
| 43 | + var styleSelect = document.querySelector('select[name="tptn_settings[tptn_styles]"]'); | |
| 44 | + if (!styleSelect) { | |
| 39 | 45 | return; |
| 40 | 46 | } |
| 41 | 47 | |
| 42 | - var $postThumbOptions = $('input[name="tptn_settings[post_thumb_op]"]'); | |
| 43 | - if (!$postThumbOptions.length) { | |
| 44 | - return; | |
| 45 | - } | |
| 48 | + var strings = top_ten_admin_data.strings || {}; | |
| 46 | 49 | |
| 47 | - var strings = top_ten_admin_data.strings || {}; | |
| 48 | - var styleRules = { | |
| 49 | - 'left_thumbs': { | |
| 50 | - allowed: ['inline', 'thumbs_only'], | |
| 51 | - fallback: 'inline', | |
| 52 | - message: strings.left_thumbs_message | |
| 53 | - }, | |
| 54 | - 'grid_thumbs': { | |
| 55 | - allowed: ['inline', 'thumbs_only'], | |
| 56 | - fallback: 'inline', | |
| 57 | - message: strings.grid_thumbs_message || strings.left_thumbs_message | |
| 58 | - }, | |
| 59 | - 'text_only': { | |
| 60 | - force: 'text_only', | |
| 61 | - disableAll: true, | |
| 62 | - message: strings.text_only_message | |
| 63 | - } | |
| 50 | + var messageConfig = { | |
| 51 | + 'left_thumbs': 'left_thumbs_message', | |
| 52 | + 'grid_thumbs': 'grid_thumbs_message', | |
| 53 | + 'text_only': 'text_only_message' | |
| 64 | 54 | }; |
| 65 | 55 | |
| 66 | - function removeMessage() { | |
| 67 | - var $container = $postThumbOptions.first().closest('td'); | |
| 68 | - if (!$container.length) { | |
| 69 | - $container = $postThumbOptions.first().parent(); | |
| 56 | + function updateMessage(selectedStyle) { | |
| 57 | + var messageKey = messageConfig[selectedStyle]; | |
| 58 | + var postThumbOptions = document.querySelectorAll('input[name="tptn_settings[post_thumb_op]"]'); | |
| 59 | + if (postThumbOptions.length === 0) { | |
| 60 | + return; | |
| 70 | 61 | } |
| 71 | - $container.find('.tptn-js-message').remove(); | |
| 72 | - } | |
| 73 | 62 | |
| 74 | - function addMessage(message) { | |
| 75 | - if (!message) { | |
| 63 | + var container = postThumbOptions[0].closest('td'); | |
| 64 | + if (!container) { | |
| 76 | 65 | return; |
| 77 | 66 | } |
| 78 | 67 | |
| 79 | - var $container = $postThumbOptions.first().closest('td'); | |
| 80 | - if (!$container.length) { | |
| 81 | - $container = $postThumbOptions.first().parent(); | |
| 68 | + var existingMessage = container.querySelector('.tptn-js-message'); | |
| 69 | + if (!messageKey) { | |
| 70 | + if (existingMessage) { | |
| 71 | + existingMessage.remove(); | |
| 72 | + } | |
| 73 | + return; | |
| 82 | 74 | } |
| 83 | 75 | |
| 84 | - var $existingMessage = $container.find('.tptn-js-message'); | |
| 85 | - if ($existingMessage.length) { | |
| 86 | - $existingMessage.text(message); | |
| 87 | - return; | |
| 76 | + var messageText = strings[messageKey] || ''; | |
| 77 | + if (!existingMessage) { | |
| 78 | + var message = document.createElement('p'); | |
| 79 | + message.className = 'description tptn-js-message'; | |
| 80 | + message.style.color = '#9B0800'; | |
| 81 | + message.textContent = messageText; | |
| 82 | + container.appendChild(message); | |
| 83 | + } else if (existingMessage.textContent !== messageText) { | |
| 84 | + existingMessage.textContent = messageText; | |
| 88 | 85 | } |
| 89 | - | |
| 90 | - $container.append( | |
| 91 | - $('<p />', { | |
| 92 | - 'class': 'description tptn-js-message', | |
| 93 | - 'css': { 'color': '#9B0800' }, | |
| 94 | - 'text': message | |
| 95 | - }) | |
| 96 | - ); | |
| 97 | 86 | } |
| 98 | 87 | |
| 99 | - function updateFieldStates() { | |
| 100 | - var selectedStyle = $styleSelect.val(); | |
| 101 | - var $checked = $postThumbOptions.filter(':checked'); | |
| 102 | - var rule = styleRules[selectedStyle] || null; | |
| 88 | + function updateFields() { | |
| 89 | + var selectedStyle = styleSelect.value; | |
| 90 | + var postThumbOptions = document.querySelectorAll('input[name="tptn_settings[post_thumb_op]"]'); | |
| 103 | 91 | |
| 104 | - removeMessage(); | |
| 105 | - $postThumbOptions.prop('disabled', false); | |
| 92 | + // Update the inline message first. | |
| 93 | + updateMessage(selectedStyle); | |
| 106 | 94 | |
| 107 | - if (!rule) { | |
| 108 | - return; | |
| 109 | - } | |
| 95 | + // Reset all radios to enabled, then re-disable as needed. | |
| 96 | + postThumbOptions.forEach(function (el) { | |
| 97 | + el.disabled = false; | |
| 98 | + }); | |
| 110 | 99 | |
| 111 | - if (rule.disableAll) { | |
| 112 | - $postThumbOptions.prop('disabled', true); | |
| 113 | - } | |
| 100 | + // Special handling for left/grid thumbs: restrict post_thumb_op to inline, after, or thumbs_only. | |
| 101 | + if ('left_thumbs' === selectedStyle || 'grid_thumbs' === selectedStyle) { | |
| 102 | + var allowed = ['inline', 'after', 'thumbs_only']; | |
| 103 | + var currentChecked = document.querySelector('input[name="tptn_settings[post_thumb_op]"]:checked'); | |
| 104 | + var needsFallback = !currentChecked || -1 === allowed.indexOf(currentChecked.value); | |
| 114 | 105 | |
| 115 | - if (rule.allowed && rule.allowed.length) { | |
| 116 | - $postThumbOptions.each(function () { | |
| 117 | - var $option = $(this); | |
| 118 | - var value = $option.val(); | |
| 119 | - if (-1 === rule.allowed.indexOf(value)) { | |
| 120 | - $option.prop('disabled', true); | |
| 106 | + postThumbOptions.forEach(function (el) { | |
| 107 | + if (-1 === allowed.indexOf(el.value)) { | |
| 108 | + el.disabled = true; | |
| 121 | 109 | } |
| 122 | 110 | }); |
| 123 | 111 | |
| 124 | - if (!$checked.length || -1 === rule.allowed.indexOf($checked.val())) { | |
| 125 | - var fallback = rule.fallback || rule.allowed[0]; | |
| 126 | - $postThumbOptions.filter('[value="' + fallback + '"]').prop('checked', true); | |
| 112 | + if (needsFallback) { | |
| 113 | + var inlineRadio = document.querySelector('input[name="tptn_settings[post_thumb_op]"][value="inline"]'); | |
| 114 | + if (inlineRadio) { | |
| 115 | + inlineRadio.checked = true; | |
| 116 | + } | |
| 127 | 117 | } |
| 128 | 118 | } |
| 129 | 119 | |
| 130 | - if (rule.force) { | |
| 131 | - $postThumbOptions.filter('[value="' + rule.force + '"]').prop('checked', true); | |
| 132 | - } | |
| 120 | + // Special handling for text_only: force post_thumb_op to text_only. | |
| 121 | + if ('text_only' === selectedStyle) { | |
| 122 | + postThumbOptions.forEach(function (el) { | |
| 123 | + el.disabled = true; | |
| 124 | + }); | |
| 133 | 125 | |
| 134 | - if (rule.message) { | |
| 135 | - addMessage(rule.message); | |
| 126 | + var textOnlyRadio = document.querySelector('input[name="tptn_settings[post_thumb_op]"][value="text_only"]'); | |
| 127 | + if (textOnlyRadio) { | |
| 128 | + textOnlyRadio.checked = true; | |
| 129 | + } | |
| 136 | 130 | } |
| 137 | 131 | } |
| 138 | 132 | |
| 139 | - updateFieldStates(); | |
| 140 | - $styleSelect.on('change', updateFieldStates); | |
| 133 | + updateFields(); | |
| 134 | + styleSelect.addEventListener('change', updateFields); | |
| 141 | 135 | } |
| 142 | 136 | |
| 143 | 137 | tptnHandleThumbnailStyleChange(); |
| 144 | 138 | |
| @@ -247,8 +241,45 @@ | ||
| 247 | 241 | }); |
| 248 | 242 | |
| 249 | 243 | $(function () { |
| 250 | 244 | var $tabsContainer = $("#dashboard-historical-visits"); |
| 245 | + if (!$tabsContainer.length) { | |
| 246 | + return; | |
| 247 | + } | |
| 248 | + | |
| 249 | + function loadDashboardTab($panel) { | |
| 250 | + if (!$panel.length || $panel.data('tptn-loaded') || $panel.data('tptn-loading')) { | |
| 251 | + return; | |
| 252 | + } | |
| 253 | + var $content = $panel.find('.tptn-dashboard-tab-content'); | |
| 254 | + if (!$content.length) { | |
| 255 | + return; | |
| 256 | + } | |
| 257 | + | |
| 258 | + $panel.data('tptn-loading', true).attr('aria-busy', 'true'); | |
| 259 | + $.post( | |
| 260 | + tptn_chart_data.tabs_url || ajaxurl, | |
| 261 | + { | |
| 262 | + action: 'tptn_dashboard_tab', | |
| 263 | + security: tptn_chart_data.security, | |
| 264 | + tab: $panel.data('tptn-tab'), | |
| 265 | + network: parseInt($tabsContainer.data('tptn-network'), 10) || 0 | |
| 266 | + }, | |
| 267 | + function (response) { | |
| 268 | + if (response && response.success && response.data && typeof response.data.html !== 'undefined') { | |
| 269 | + $content.html(response.data.html).data('tptn-loaded', true); | |
| 270 | + } else { | |
| 271 | + $content.html('<p>' + tptn_chart_data.tab_error + '</p>'); | |
| 272 | + } | |
| 273 | + }, | |
| 274 | + 'json' | |
| 275 | + ).fail(function () { | |
| 276 | + $content.html('<p>' + tptn_chart_data.tab_error + '</p>'); | |
| 277 | + }).always(function () { | |
| 278 | + $panel.data('tptn-loading', false).removeAttr('aria-busy'); | |
| 279 | + }); | |
| 280 | + } | |
| 281 | + | |
| 251 | 282 | $tabsContainer.tabs({ |
| 252 | 283 | create: function (event, ui) { |
| 253 | 284 | $(ui.tab.find("a")).addClass("nav-tab-active"); |
| 254 | 285 | }, |
| @@ -254,8 +285,9 @@ | ||
| 254 | 285 | }, |
| 255 | 286 | activate: function (event, ui) { |
| 256 | 287 | $(ui.oldTab.find("a")).removeClass("nav-tab-active"); |
| 257 | 288 | $(ui.newTab.find("a")).addClass("nav-tab-active"); |
| 289 | + loadDashboardTab(ui.newPanel); | |
| 258 | 290 | } |
| 259 | 291 | }); |
| 260 | 292 | |
| 261 | 293 | // Wait for the tabs to be fully initialized |