| 1 |
/** |
| 2 |
* Scripts for Settings > Image Sources |
| 3 |
*/ |
| 4 |
jQuery( document ).ready( |
| 5 |
function($) { |
| 6 |
// Initial setup |
| 7 |
isc_thumbnail_input_checkstate(); |
| 8 |
isc_caption_checkstate(); |
| 9 |
isc_ai_images_checkstate(); |
| 10 |
isc_licenses_checkstate(); |
| 11 |
isc_toggle_caption_position(); |
| 12 |
isc_toggle_module_sections(); |
| 13 |
|
| 14 |
// Event handlers |
| 15 |
$( '#isc-settings-overlay-enable' ).on( 'click', function(){ isc_caption_checkstate() } ); |
| 16 |
$( '#isc-settings-ai-images-enable' ).on( 'click', function(){ isc_ai_images_checkstate() } ); |
| 17 |
$( '#isc-settings-licenses-enable' ).on( 'click', function(){ isc_licenses_checkstate() } ); |
| 18 |
$( '.isc-settings-standard-source input' ).on( 'change', isc_toggle_standard_source_text ); |
| 19 |
$( '#thumbnail-size-select, #use-thumbnail' ).on( 'change', function(){ isc_thumbnail_input_checkstate(); } ); |
| 20 |
$( '#isc-settings-caption-style input' ).on( 'change', function(){ isc_toggle_caption_position(); } ); |
| 21 |
$( '#isc-settings-global-list-indexed-images' ).on( 'change', function(){ isc_show_reindex_warning(); } ); |
| 22 |
$( '#isc-settings-plugin-modules input[type="checkbox"]').on('change', function() { isc_toggle_module_sections(); }); |
| 23 |
$( '#isc-settings-plugin-images-only' ).on( 'change', function() { |
| 24 |
const enabled = this.checked; |
| 25 |
$( '#isc-settings-plugin-images-only-indexer' ).toggleClass( 'hidden' ); |
| 26 |
$( '#isc-settings-plugin-images-only-cleanup-wrapper' ).toggleClass( 'hidden', !enabled ); |
| 27 |
}); |
| 28 |
|
| 29 |
// Copy log URL to clipboard |
| 30 |
$( '#isc-copy-log-url-btn' ).on( 'click', function() { |
| 31 |
const urlField = document.getElementById( 'isc-log-url-field' ); |
| 32 |
if ( ! urlField ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
const showSuccess = function() { |
| 37 |
$( '#isc-copy-log-url-success' ).stop( true, true ).fadeIn().delay( 2000 ).fadeOut(); |
| 38 |
}; |
| 39 |
|
| 40 |
const fallbackCopy = function() { |
| 41 |
urlField.focus(); |
| 42 |
urlField.select(); |
| 43 |
urlField.setSelectionRange( 0, urlField.value.length ); |
| 44 |
if ( document.execCommand( 'copy' ) ) { |
| 45 |
showSuccess(); |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
if ( navigator.clipboard?.writeText ) { |
| 50 |
navigator.clipboard.writeText( urlField.value ).then( showSuccess ).catch( fallbackCopy ); |
| 51 |
} else { |
| 52 |
fallbackCopy(); |
| 53 |
} |
| 54 |
} ); |
| 55 |
|
| 56 |
// Download log file |
| 57 |
$( '#isc-download-log-btn' ).on( 'click', function() { |
| 58 |
// Use AJAX to download the file via WordPress backend |
| 59 |
$.ajax({ |
| 60 |
type: 'POST', |
| 61 |
url: ajaxurl, |
| 62 |
data: { |
| 63 |
action: 'isc_download_log', |
| 64 |
nonce: isc.ajaxNonce, |
| 65 |
}, |
| 66 |
xhrFields: { |
| 67 |
responseType: 'blob' |
| 68 |
}, |
| 69 |
success: function( response, status, xhr ) { |
| 70 |
// Get filename from Content-Disposition header or use default |
| 71 |
const disposition = xhr.getResponseHeader( 'Content-Disposition' ); |
| 72 |
let filename = 'image-source-control.log'; |
| 73 |
if ( disposition && disposition.indexOf( 'filename=' ) !== -1 ) { |
| 74 |
const filenameMatch = disposition.match( /filename="?([^"]+)"?/ ); |
| 75 |
if ( filenameMatch && filenameMatch[1] ) { |
| 76 |
filename = filenameMatch[1]; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// Create a blob URL and trigger download |
| 81 |
const blob = new Blob( [response], { type: 'text/plain' } ); |
| 82 |
const url = window.URL.createObjectURL( blob ); |
| 83 |
const link = document.createElement( 'a' ); |
| 84 |
link.href = url; |
| 85 |
link.download = filename; |
| 86 |
document.body.appendChild( link ); |
| 87 |
link.click(); |
| 88 |
document.body.removeChild( link ); |
| 89 |
window.URL.revokeObjectURL( url ); |
| 90 |
}, |
| 91 |
error: function( xhr ) { |
| 92 |
// Show error message |
| 93 |
console.error( 'Error downloading log file:', xhr.statusText ); |
| 94 |
let errorMsg = 'Error downloading log file.'; |
| 95 |
if (xhr.status) { |
| 96 |
errorMsg += ' HTTP status: ' + xhr.status + ' (' + xhr.statusText + ').'; |
| 97 |
} |
| 98 |
// Try to extract a more specific error message from the response, if available |
| 99 |
if (xhr.responseText) { |
| 100 |
errorMsg += '\nDetails: ' + xhr.responseText; |
| 101 |
} |
| 102 |
alert(errorMsg); |
| 103 |
} |
| 104 |
}); |
| 105 |
} ); |
| 106 |
|
| 107 |
// Toggle log URL field visibility when checkbox is changed |
| 108 |
$( '#isc-enable-log-checkbox' ).on( 'change', function() { |
| 109 |
$( '#isc-log-url-wrapper' ).toggle( this.checked ); |
| 110 |
}); |
| 111 |
|
| 112 |
// Show and update preview when a position option is clicked |
| 113 |
$('#isc-settings-caption-pos-options button').on( 'click', function (event) { |
| 114 |
// Stop propagation to prevent document click event from hiding the preview immediately |
| 115 |
event.stopPropagation(); |
| 116 |
|
| 117 |
$('#isc-settings-caption-pos-options button.selected').removeClass('selected'); |
| 118 |
$(this).addClass('selected'); |
| 119 |
$('#isc-settings-caption-position').val($(this).data('position')); |
| 120 |
|
| 121 |
var iframe = document.createElement('iframe'); |
| 122 |
iframe.src = isc_settings.baseurl + 'admin/templates/settings/preview/caption-preview.html' + "?position=" + $(this).data('position') + "&pretext=" + encodeURIComponent($('#source-pretext').val()); |
| 123 |
iframe.width = "250"; |
| 124 |
iframe.height = "181"; |
| 125 |
|
| 126 |
var preview_container = $('#isc-settings-caption-preview'); |
| 127 |
preview_container.find('iframe').remove(); |
| 128 |
preview_container.append(iframe); |
| 129 |
preview_container.removeClass('hidden'); |
| 130 |
}); |
| 131 |
|
| 132 |
// Hide the preview when the mouse leaves the option area or a click occurs outside the option area |
| 133 |
$(document).on('click mouseout', function (event) { |
| 134 |
if (!$(event.target).closest('#isc-settings-caption-pos-options').length) { |
| 135 |
$('#isc-settings-caption-preview').addClass('hidden'); |
| 136 |
} |
| 137 |
}); |
| 138 |
|
| 139 |
$('#isc-signup-nl').prop( 'disabled', false ); |
| 140 |
$('#isc-signup-nl').on( 'click', function() { |
| 141 |
$('#isc-signup-nl').prop( 'disabled', true ); |
| 142 |
$('#isc-signup-loader').removeClass('hidden'); |
| 143 |
$.ajax({ |
| 144 |
type: 'POST', |
| 145 |
url: ajaxurl, |
| 146 |
data: { |
| 147 |
action: 'newsletter_signup', |
| 148 |
nonce: isc.ajaxNonce, |
| 149 |
}, |
| 150 |
dataType: 'json', |
| 151 |
success: function( response ) { |
| 152 |
if ( ! response.success ) { |
| 153 |
$('#isc-signup-nl-error').removeClass('hidden').html(response.message); |
| 154 |
} else { |
| 155 |
$('#isc-signup-nl-success').removeClass('hidden').html(response.message); |
| 156 |
} |
| 157 |
$('#isc-signup-loader').addClass('hidden'); |
| 158 |
} |
| 159 |
}); |
| 160 |
}); |
| 161 |
// close the newsletter signup box without signing up |
| 162 |
$('#isc_settings_section_signup .postbox-header .dashicons-no-alt').on( 'click', function() { |
| 163 |
$('#isc-signup-nl').prop( 'disabled', true ); |
| 164 |
$('#isc-signup-loader').removeClass('hidden'); |
| 165 |
$.ajax({ |
| 166 |
type: 'POST', |
| 167 |
url: ajaxurl, |
| 168 |
data: { |
| 169 |
action: 'newsletter_close', |
| 170 |
nonce: isc.ajaxNonce, |
| 171 |
}, |
| 172 |
dataType: 'json', |
| 173 |
success: function( response ) { |
| 174 |
$('#isc_settings_section_signup').remove(); |
| 175 |
} |
| 176 |
}); |
| 177 |
}); |
| 178 |
|
| 179 |
// Add special characters to the source-pretext field by clicking on a button |
| 180 |
document.querySelectorAll('#source-pretext-buttons button').forEach( function( button ) { |
| 181 |
button.addEventListener('click', function() { |
| 182 |
var inputField = document.getElementById('source-pretext'); |
| 183 |
inputField.value += this.textContent; // Use the button's content |
| 184 |
inputField.focus(); // Optionally, refocus on the input field |
| 185 |
}); |
| 186 |
}); |
| 187 |
// Show the buttons when the input is focused |
| 188 |
document.getElementById('source-pretext').addEventListener('focus', function() { |
| 189 |
document.getElementById('source-pretext-buttons').classList.remove('hidden'); |
| 190 |
}); |
| 191 |
} |
| 192 |
); |
| 193 |
|
| 194 |
/** |
| 195 |
* Toggle visibility of module-related sections based on checkbox states |
| 196 |
*/ |
| 197 |
function isc_toggle_module_sections() { |
| 198 |
Object.entries( isc.moduleSections ).forEach(([module, sections]) => { |
| 199 |
const checkbox = document.querySelector(`#isc-settings-plugin-modules input[value="${module}"]`); |
| 200 |
if (checkbox) { |
| 201 |
sections.forEach(sectionId => { |
| 202 |
const sectionElement = document.getElementById(sectionId); |
| 203 |
const section = sectionElement ? sectionElement.querySelector('.inside') : null; |
| 204 |
if (section) { |
| 205 |
if (checkbox.checked) { |
| 206 |
section.classList.remove('hidden'); |
| 207 |
} else { |
| 208 |
section.classList.add('hidden'); |
| 209 |
} |
| 210 |
} |
| 211 |
}); |
| 212 |
} |
| 213 |
}); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Toggle the state of the thumbnail size options |
| 218 |
*/ |
| 219 |
function isc_thumbnail_input_checkstate(){ |
| 220 |
// enable the thumbnail size select field when thumbnails are enabled in general |
| 221 |
if ( document.getElementById( 'use-thumbnail' ).checked ) { |
| 222 |
document.getElementById( 'thumbnail-size-select' ).classList.remove( 'hidden' ); |
| 223 |
} else { |
| 224 |
document.getElementById( 'thumbnail-size-select' ).classList.add( 'hidden' ) |
| 225 |
} |
| 226 |
|
| 227 |
// toggle the state of the thumbnail custom size options in the plugin settings to only enable them if the "custom" thumbnails size is used |
| 228 |
if ( 'custom' === document.getElementById( 'thumbnail-size-select' ).value && document.getElementById( 'use-thumbnail' ).checked ) { |
| 229 |
document.getElementById( 'isc-settings-custom-size' ).classList.remove( 'hidden' ); |
| 230 |
} else { |
| 231 |
document.getElementById( 'isc-settings-custom-size' ).classList.add( 'hidden' ); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Toggle the state of the options in the Overlay settings |
| 237 |
*/ |
| 238 |
function isc_caption_checkstate() { |
| 239 |
var overlay_enabled = document.getElementById( 'isc-settings-overlay-enable' ); |
| 240 |
|
| 241 |
if ( ! overlay_enabled ) { |
| 242 |
return; |
| 243 |
} |
| 244 |
var elements = document.querySelectorAll( '.isc_settings_section_overlay .form-table tr:not(:first-of-type)' ); |
| 245 |
if ( overlay_enabled.checked ) { |
| 246 |
Array.prototype.forEach.call( elements, function(el, i) { |
| 247 |
el.style.display = 'table-row'; |
| 248 |
} ); |
| 249 |
} else { |
| 250 |
Array.prototype.forEach.call( elements, function(el, i) { |
| 251 |
el.style.display = 'none'; |
| 252 |
} ); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Toggle the state of the options in the AI images settings |
| 258 |
*/ |
| 259 |
function isc_ai_images_checkstate() { |
| 260 |
var ai_images_enabled = document.getElementById( 'isc-settings-ai-images-enable' ); |
| 261 |
|
| 262 |
if ( ! ai_images_enabled ) { |
| 263 |
return; |
| 264 |
} |
| 265 |
var elements = document.querySelectorAll( '.isc_settings_section_ai_images .form-table tr:not(:first-of-type)' ); |
| 266 |
if ( ai_images_enabled.checked ) { |
| 267 |
Array.prototype.forEach.call( elements, function(el, i) { |
| 268 |
el.style.display = 'table-row'; |
| 269 |
} ); |
| 270 |
} else { |
| 271 |
Array.prototype.forEach.call( elements, function(el, i) { |
| 272 |
el.style.display = 'none'; |
| 273 |
} ); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Toggle the state of the options in the Licenses settings |
| 279 |
*/ |
| 280 |
function isc_licenses_checkstate() { |
| 281 |
var licenses_enabled = document.getElementById( 'isc-settings-licenses-enable' ); |
| 282 |
|
| 283 |
if ( ! licenses_enabled ) { |
| 284 |
return; |
| 285 |
} |
| 286 |
var elements = document.querySelectorAll( '.isc_settings_section_licenses .form-table tr:not(:first-of-type)' ); |
| 287 |
if ( licenses_enabled.checked ) { |
| 288 |
Array.prototype.forEach.call( elements, function(el, i) { |
| 289 |
el.style.display = 'table-row'; |
| 290 |
} ); |
| 291 |
} else { |
| 292 |
Array.prototype.forEach.call( elements, function(el, i) { |
| 293 |
el.style.display = 'none'; |
| 294 |
} ); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Toggle the state of the Custom Text option in the Standard Sources settings |
| 300 |
*/ |
| 301 |
function isc_toggle_standard_source_text() { |
| 302 |
var standard_source_custom = document.getElementById( 'isc-custom-text-select' ); |
| 303 |
|
| 304 |
if ( ! standard_source_custom ) { |
| 305 |
return; |
| 306 |
} |
| 307 |
|
| 308 |
if ( standard_source_custom.checked ) { |
| 309 |
document.getElementById( 'isc-custom-text' ).removeAttribute( 'disabled' ); |
| 310 |
} else { |
| 311 |
document.getElementById( 'isc-custom-text' ).setAttribute( 'disabled', 'disabled' ); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Toggle the visibility of the position option when the caption style is changed |
| 317 |
*/ |
| 318 |
function isc_toggle_caption_position() { |
| 319 |
var caption_style = document.querySelector( '#isc-settings-caption-style input:checked' ); |
| 320 |
|
| 321 |
if ( ! caption_style ) { |
| 322 |
return; |
| 323 |
} |
| 324 |
|
| 325 |
if ( caption_style.value === 'none' ) { |
| 326 |
document.getElementById( 'isc-settings-caption-position-options-wrapper' ).classList.add( 'hidden' ); |
| 327 |
// add class also to the sibling H4 element |
| 328 |
document.getElementById( 'isc-settings-caption-position-options-wrapper' ).previousElementSibling.classList.add( 'hidden' ); |
| 329 |
} else { |
| 330 |
document.getElementById( 'isc-settings-caption-position-options-wrapper' ).classList.remove( 'hidden' ); |
| 331 |
// remove class also from the sibling H4 element |
| 332 |
document.getElementById( 'isc-settings-caption-position-options-wrapper' ).previousElementSibling.classList.remove( 'hidden' ); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Toggle the visibility of the warning about a reindex when changing the Index-Images option for Global Lists |
| 338 |
*/ |
| 339 |
function isc_show_reindex_warning() { |
| 340 |
document.getElementById( 'isc-settings-global-list-indexed-images-warning' ).classList.remove( 'hidden' ); |
| 341 |
} |