(function() { tinymce.create('tinymce.plugins.AIRPPlugin', { init: function(editor, url) { function ensureAIRP() { if (typeof window.AIRP === 'undefined') { alert('AI Related Posts non è inizializzato correttamente.'); return false; } return true; } function createModal() { if (document.getElementById('airp-modal')) { return; } var style = document.createElement('style'); style.type = 'text/css'; style.textContent = '' + '#airp-modal-backdrop{position:fixed;z-index:100000;top:0;left:0;width:100%;height:100%;' + 'background:rgba(0,0,0,0.3);display:flex;align-items:center;justify-content:center;}' + '#airp-modal{background:#fff;border:1px solid #ccc;border-radius:4px;max-width:600px;width:95%;' + 'max-height:80%;overflow:hidden;box-shadow:0 4px 12px rgba(0,0,0,0.2);font-family:sans-serif;}' + '#airp-modal-header{padding:8px 12px;border-bottom:1px solid #eee;font-weight:bold;display:flex;justify-content:space-between;align-items:center;}' + '#airp-modal-body{padding:10px 12px;overflow-y:auto;max-height:calc(80vh - 80px);}' + '#airp-modal-footer{padding:8px 12px;border-top:1px solid #eee;text-align:right;}' + '#airp-status{font-size:11px;color:#555;margin-left:6px;}' + '.airp-chip{display:inline-block;margin:2px;padding:2px 6px;background:#f0f0f0;border-radius:12px;font-size:11px;cursor:default;}' + '.airp-chip-remove{cursor:pointer;margin-left:4px;}' + '#airp-keywords-list{border:1px solid #ddd;min-height:28px;padding:4px;background:#fff;}' + '#airp-results-list{border:1px solid #ddd;margin-top:6px;padding:4px;background:#fafafa;max-height:200px;overflow:auto;}' + '#airp-results-list ul{margin:0;padding-left:16px;}' + '#airp-results-list li{margin-bottom:4px;cursor:pointer;}' + '#airp-results-list li span.airp-post-title{text-decoration:underline;color:#2271b1;}' + '#airp-results-list small{display:block;font-size:11px;color:#666;}' + '#airp-modal button{font-size:12px;}'; document.head.appendChild(style); var backdrop = document.createElement('div'); backdrop.id = 'airp-modal-backdrop'; var modal = document.createElement('div'); modal.id = 'airp-modal'; var header = document.createElement('div'); header.id = 'airp-modal-header'; header.innerHTML = 'AI Related Posts'; var closeBtn = document.createElement('button'); closeBtn.type = 'button'; closeBtn.textContent = '×'; closeBtn.style.border = 'none'; closeBtn.style.background = 'transparent'; closeBtn.style.fontSize = '18px'; closeBtn.style.cursor = 'pointer'; closeBtn.onclick = function() { document.body.removeChild(backdrop); }; header.appendChild(closeBtn); var body = document.createElement('div'); body.id = 'airp-modal-body'; body.innerHTML = '' + '

Trova articoli correlati al contenuto di questo articolo e linkali direttamente dal testo selezionato.

' + '

' + '' + '' + '

' + '

Keyword (max 3):

' + '
Nessuna keyword.
' + '

' + '
' + '' + '' + '

' + '
' + '

' + '' + '

' + '
Nessuna ricerca effettuata.
'; var footer = document.createElement('div'); footer.id = 'airp-modal-footer'; var closeFooterBtn = document.createElement('button'); closeFooterBtn.type = 'button'; closeFooterBtn.className = 'button'; closeFooterBtn.textContent = 'Chiudi'; closeFooterBtn.onclick = function() { document.body.removeChild(backdrop); }; footer.appendChild(closeFooterBtn); modal.appendChild(header); modal.appendChild(body); modal.appendChild(footer); backdrop.appendChild(modal); document.body.appendChild(backdrop); attachModalLogic(editor); } function attachModalLogic(editor) { var maxKeywords = (window.AIRP && AIRP.max_keywords) ? parseInt(AIRP.max_keywords, 10) : 3; if (!maxKeywords || maxKeywords < 1) { maxKeywords = 3; } var statusEl = document.getElementById('airp-status'); var listEl = document.getElementById('airp-keywords-list'); var newKwInput = document.getElementById('airp-new-keyword'); var btnFind = document.getElementById('airp-btn-find'); var btnAdd = document.getElementById('airp-add-keyword'); var btnSearch = document.getElementById('airp-btn-search'); var resultsList = document.getElementById('airp-results-list'); function setStatus(msg) { if (!statusEl) return; statusEl.textContent = msg || ''; } function getKeywordsArray() { var chips = listEl.querySelectorAll('.airp-chip'); var kws = []; chips.forEach(function(ch) { var kw = ch.getAttribute('data-keyword'); if (kw && kws.indexOf(kw) === -1) { kws.push(kw); } }); return kws; } function renderKeywords(keywords) { listEl.innerHTML = ''; if (!keywords || !keywords.length) { listEl.innerHTML = 'Nessuna keyword.'; return; } keywords.forEach(function(kw) { var chip = document.createElement('span'); chip.className = 'airp-chip'; chip.setAttribute('data-keyword', kw); chip.textContent = kw + ' '; var x = document.createElement('span'); x.className = 'airp-chip-remove'; x.textContent = '×'; x.onclick = function(e) { e.stopPropagation(); chip.remove(); if (!getKeywordsArray().length) { listEl.innerHTML = 'Nessuna keyword.'; } setStatus(''); }; chip.appendChild(x); listEl.appendChild(chip); }); } function addKeyword(kw) { kw = (kw || '').trim(); if (!kw) { setStatus('Inserisci una keyword valida.'); return; } var current = getKeywordsArray(); if (current.indexOf(kw) !== -1) { setStatus('Questa keyword è già presente.'); return; } if (current.length >= maxKeywords) { setStatus('Puoi usare al massimo ' + maxKeywords + ' keyword.'); return; } current.push(kw); renderKeywords(current); setStatus(''); } btnAdd.addEventListener('click', function() { var kw = newKwInput.value; newKwInput.value = ''; addKeyword(kw); }); newKwInput.addEventListener('keypress', function(e) { if (e.key === 'Enter') { e.preventDefault(); btnAdd.click(); } }); btnFind.addEventListener('click', function() { if (!ensureAIRP()) return; setStatus('Analisi in corso...'); var content = editor.getContent({ format: 'html' }) || ''; var titleEl = document.getElementById('title'); var title = titleEl ? titleEl.value || '' : ''; if (!content && !title) { setStatus('Contenuto vuoto: scrivi o incolla il testo prima di analizzare.'); return; } var data = new FormData(); data.append('action', 'airp_find_keywords'); data.append('nonce', AIRP.nonce); data.append('post_id', AIRP.post_id || 0); data.append('content', content); data.append('title', title); fetch(AIRP.ajax_url, { method: 'POST', credentials: 'same-origin', body: data }) .then(function(res) { return res.json(); }) .then(function(json) { if (!json || !json.success) { var msg = (json && json.data && json.data.message) ? json.data.message : 'Errore sconosciuto.'; setStatus('Errore: ' + msg); return; } var keywords = json.data.keywords || []; if (keywords.length > maxKeywords) { keywords = keywords.slice(0, maxKeywords); } renderKeywords(keywords); setStatus('Keyword trovate.'); }) .catch(function(err) { console.error('AIRP find_keywords error', err); setStatus('Errore AJAX.'); }); }); btnSearch.addEventListener('click', function() { if (!ensureAIRP()) return; var kws = getKeywordsArray(); if (!kws.length) { setStatus('Nessuna keyword: trovane o aggiungine almeno una.'); return; } setStatus('Ricerca in corso...'); var data = new FormData(); data.append('action', 'airp_search_related'); data.append('nonce', AIRP.nonce); data.append('post_id', AIRP.post_id || 0); data.append('keywords', kws.join(',')); fetch(AIRP.ajax_url, { method: 'POST', credentials: 'same-origin', body: data }) .then(function(res) { return res.json(); }) .then(function(json) { resultsList.innerHTML = ''; if (!json || !json.success) { var msg = (json && json.data && json.data.message) ? json.data.message : 'Errore sconosciuto.'; resultsList.innerHTML = '

' + msg + '

'; setStatus('Nessun risultato.'); return; } var posts = json.data.posts || []; if (!posts.length) { resultsList.innerHTML = '

Nessun articolo correlato trovato.

'; setStatus('Nessun risultato.'); return; } var ul = document.createElement('ul'); posts.forEach(function(p) { var li = document.createElement('li'); var titleSpan = document.createElement('span'); titleSpan.className = 'airp-post-title'; titleSpan.textContent = p.title; var dateSmall = document.createElement('small'); dateSmall.textContent = p.date; li.appendChild(titleSpan); li.appendChild(dateSmall); li.addEventListener('click', function() { // test selezione var selectedText = editor.selection.getContent({ format: 'text' }) || ''; if (!selectedText) { selectedText = p.title; } var encodedText = editor.dom.encode(selectedText); var href = p.url; editor.insertContent( '' + encodedText + '' ); setStatus('Link inserito.'); }); ul.appendChild(li); }); resultsList.appendChild(ul); setStatus('Articoli trovati: ' + posts.length); }) .catch(function(err) { console.error('AIRP search_related error', err); resultsList.innerHTML = '

Errore AJAX.

'; setStatus('Errore AJAX.'); }); }); } editor.addButton('airp_button', { text: 'AI Related Posts', icon: false, tooltip: 'Trova articoli correlati e linkali al testo selezionato', onclick: function() { if (!ensureAIRP()) { return; } createModal(); } }); } }); tinymce.PluginManager.add('airp_plugin', tinymce.plugins.AIRPPlugin); })();