[ 'post', 'page' ], 'post_status' => 'publish', 'numberposts' => 300, 'orderby' => 'date', 'order' => 'DESC', ] ); // Helper D2 : pastille d'aide « ? » charte (AiforyaTooltip). $text déjà traduit. $tip = static function ( $text ) { printf( '', esc_attr( $text ) ); }; ?>

$nonce, 'enregistrement' => __( 'Saving…', 'aiforya-faq-generator' ), 'faqEnregistree' => __( 'FAQ saved.', 'aiforya-faq-generator' ), 'chargement' => __( 'Loading…', 'aiforya-faq-generator' ), 'detectionEnCours' => __( 'Detecting…', 'aiforya-faq-generator' ), 'erreurReseau' => __( 'Network error.', 'aiforya-faq-generator' ), 'supprimerDefinitivementLaFaq' => __( 'Permanently delete the FAQ of this post?', 'aiforya-faq-generator' ), 'remplacerLesQuestionsActuelles' => __( 'Replace the current questions with the ones detected in the post?', 'aiforya-faq-generator' ), 'faqSupprimee' => __( 'FAQ deleted.', 'aiforya-faq-generator' ), ) ); $js = <<<'JS' var L = window.aiforyaFaqGeneratorTabFaqL10n || {}; (function () { if (typeof jQuery === 'undefined') { return; } jQuery(function ($) { var nonce = L.nonce; var $select = $('#aiforya-fg-post-select'); var $detect = $('#aiforya-fg-detect'); var $card = $('#aiforya-fg-editor-card'); var $items = $('#aiforya-fg-items'); var $count = $('#aiforya-fg-count'); var $status = $('#aiforya-fg-status'); var $saveStatus = $('#aiforya-fg-save-status'); var tpl = document.getElementById('aiforya-fg-item-template'); var i18n = { saving: L.enregistrement, saved: L.faqEnregistree, loading: L.chargement, detecting:L.detectionEnCours, netErr: L.erreurReseau, confirmDelete: L.supprimerDefinitivementLaFaq, confirmDetect: L.remplacerLesQuestionsActuelles, deleted: L.faqSupprimee }; function toast(type, msg) { if (window.AiforyaToast && window.AiforyaToast[type]) { window.AiforyaToast[type](msg); } } function renumber() { var rows = $items.find('.aiforya-fg-item'); rows.each(function (idx) { $(this).find('.aiforya-fg-item-num').text('#' + (idx + 1)); }); $count.text(rows.length); } function addItem(question, answer) { var node = tpl.content.cloneNode(true); var $row = $(node.querySelector('.aiforya-fg-item')); $row.find('.aiforya-fg-q').val(question || ''); $row.find('.aiforya-fg-a').val(answer || ''); $items.append($row); renumber(); } function clearItems() { $items.empty(); renumber(); } function currentPostId() { return parseInt($select.val(), 10) || 0; } function loadFaq(postId) { if (!postId) { $card.hide(); return; } $status.css('color', '#94a3b8').text(i18n.loading); $.post(ajaxurl, { action: 'aiforya_fg_get_post_content', nonce: nonce, post_id: postId }).done(function (res) { clearItems(); if (res && res.success && res.data && res.data.items && res.data.items.length) { res.data.items.forEach(function (it) { addItem(it.question, it.answer); }); } else { addItem('', ''); } $card.show(); $status.text(''); }).fail(function () { $status.css('color', '#f87171').text(i18n.netErr); }); } function collectItems() { var out = []; $items.find('.aiforya-fg-item').each(function () { var q = $(this).find('.aiforya-fg-q').val(); var a = $(this).find('.aiforya-fg-a').val(); out.push({ question: q, answer: a }); }); return out; } function saveFaq() { var postId = currentPostId(); if (!postId) { return; } $saveStatus.css('color', '#94a3b8').text(i18n.saving); $.post(ajaxurl, { action: 'aiforya_fg_save_faq', nonce: nonce, post_id: postId, items: collectItems() }).done(function (res) { if (res && res.success) { $saveStatus.css('color', '#4ade80').text(res.data.message || i18n.saved); toast('success', res.data.message || i18n.saved); } else { var msg = (res && res.data && res.data.message) || i18n.netErr; $saveStatus.css('color', '#f87171').text(msg); toast('error', msg); } }).fail(function () { $saveStatus.css('color', '#f87171').text(i18n.netErr); toast('error', i18n.netErr); }); } function detectFaq() { var postId = currentPostId(); if (!postId) { return; } $status.css('color', '#94a3b8').text(i18n.detecting); $.post(ajaxurl, { action: 'aiforya_fg_detect_faq', nonce: nonce, post_id: postId }).done(function (res) { if (res && res.success && res.data && res.data.items && res.data.items.length) { clearItems(); res.data.items.forEach(function (it) { addItem(it.question, it.answer); }); $status.css('color', '#4ade80').text(res.data.message || ''); toast('success', res.data.message || ''); } else { var msg = (res && res.data && res.data.message) || i18n.netErr; $status.css('color', '#f87171').text(msg); toast('info', msg); } }).fail(function () { $status.css('color', '#f87171').text(i18n.netErr); toast('error', i18n.netErr); }); } function askConfirm(message, onOk) { if (window.AiforyaConfirm && typeof window.AiforyaConfirm.ask === 'function') { window.AiforyaConfirm.ask(message, onOk); } else { onOk(); } } $select.on('change', function () { var has = currentPostId() > 0; $detect.prop('disabled', !has); $saveStatus.text(''); loadFaq(currentPostId()); }); $('#aiforya-fg-add').on('click', function () { addItem('', ''); }); $items.on('click', '.aiforya-fg-remove', function () { $(this).closest('.aiforya-fg-item').remove(); renumber(); }); $('#aiforya-fg-save').on('click', saveFaq); $detect.on('click', function () { if ($items.find('.aiforya-fg-item').length > 0 && collectItems().some(function (it) { return (it.question || '').trim() !== ''; })) { askConfirm(i18n.confirmDetect, detectFaq); } else { detectFaq(); } }); $('#aiforya-fg-delete').on('click', function () { var postId = currentPostId(); if (!postId) { return; } askConfirm(i18n.confirmDelete, function () { $.post(ajaxurl, { action: 'aiforya_fg_delete_faq', nonce: nonce, post_id: postId }).done(function (res) { clearItems(); addItem('', ''); var msg = (res && res.data && res.data.message) || i18n.deleted; $saveStatus.css('color', '#94a3b8').text(msg); toast('success', msg); }).fail(function () { toast('error', i18n.netErr); }); }); }); }); })(); JS; wp_add_inline_script( $handle, $js ); ?>