document.addEventListener('DOMContentLoaded', function() { // pestañas (si las hay) const tabs = document.querySelectorAll('.sc-editor-tabs li'); const contents = document.querySelectorAll('.tab-content'); if (tabs.length && contents.length) { tabs.forEach(tab => { tab.addEventListener('click', () => { tabs.forEach(t => t.classList.remove('active')); contents.forEach(c => c.classList.remove('active')); tab.classList.add('active'); const id = 'tab-' + tab.dataset.tab; const el = document.getElementById(id); if (el) el.classList.add('active'); }); }); } // Detección de hooks vía AJAX const detectBtn = document.getElementById('sc-detect-hooks'); if (detectBtn) { detectBtn.addEventListener('click', function() { const phpCode = document.getElementById('php_code') ? document.getElementById('php_code').value : ''; const nonce = document.getElementById('sc-detect-nonce') ? document.getElementById('sc-detect-nonce').value : ''; const msgEl = document.getElementById('sc-detect-hooks-message'); if (msgEl) msgEl.textContent = 'Detectando...'; const ajaxUrl = (window.simply_code_ajax && window.simply_code_ajax.ajax_url) ? window.simply_code_ajax.ajax_url : (typeof ajaxurl !== 'undefined' ? ajaxurl : '/'); const form = new FormData(); form.append('action', 'simply_code_detect_hooks'); form.append('nonce', nonce); form.append('php_code', phpCode); fetch(ajaxUrl, { method: 'POST', credentials: 'same-origin', body: form }).then(function(resp) { return resp.json(); }).then(function(data) { if (!data) { if (msgEl) msgEl.textContent = 'Respuesta inválida'; return; } if (data.success) { const hooks = data.data.hooks || {}; const critical = data.data.critical_hooks || {}; const container = document.getElementById('sc-hooks-list'); if (container) { container.innerHTML = ''; if (Object.keys(hooks).length === 0) { container.innerHTML = '

No se detectaron hooks.

'; } else { for (const h in hooks) { const info = hooks[h] || {}; const row = document.createElement('div'); row.className = 'sc-hook-row'; const html = '' + escapeHtml(h) + ' ' + escapeHtml(info.type || '') + '
' + 'Prioridad: ' + (critical[h] ? '⚠️ Hook crítico' : ''); row.innerHTML = html; container.appendChild(row); } } } if (msgEl) msgEl.textContent = 'Hooks detectados'; } else { if (msgEl) msgEl.textContent = data.data && data.data.message ? data.data.message : 'Error al detectar hooks'; } }).catch(function(err) { if (msgEl) msgEl.textContent = 'Error AJAX'; console.error(err); }); }); } // helpers function escapeHtml(str) { if (!str) return ''; return String(str).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); } function escapeAttr(str) { return escapeHtml(str).replace(/'/g, '''); } });