/** * Post edit screen: "Search a new image" button. * * The AI query plus the image download can take a few seconds, so the button * reports progress and updates the cover preview when it finishes. */ (function ($) { 'use strict'; $(document).on('click', '#aap-refresh-image-btn', function (e) { e.preventDefault(); var $btn = $(this); var $feedback = $('#aap-refresh-image-feedback'); var i18n = (window.aapEditor && aapEditor.i18n) || {}; if ($btn.data('busy')) return; $btn.data('busy', true).prop('disabled', true); $feedback.removeClass('is-error is-success').addClass('is-working').text(i18n.working || '...'); $.post(aapEditor.ajaxurl, { action: 'aap_refresh_image', nonce: $btn.data('nonce'), post_id: $btn.data('post') }) .done(function (res) { if (res && res.success && res.data && res.data.updated) { $feedback.removeClass('is-working').addClass('is-success').text(i18n.success || 'OK'); applyThumbnail(res.data); } else if (res && res.success) { $feedback.removeClass('is-working').addClass('is-error').text(i18n.no_image || 'No image found.'); } else { $feedback.removeClass('is-working').addClass('is-error').text(i18n.error || 'Error.'); } }) .fail(function () { $feedback.removeClass('is-working').addClass('is-error').text(i18n.error || 'Error.'); }) .always(function () { $btn.data('busy', false).prop('disabled', false); }); }); /** * Swap the preview inside the core "Featured image" box without reloading. */ function applyThumbnail(data) { if (!data.thumbnail) return; var $img = $('#postimagediv #postimagediv-thumbnail img, #postimagediv .inside img').first(); if ($img.length) { $img.attr('src', data.thumbnail); } } }(jQuery));