<?php if (!defined('DASHBOARD_ROOT')) { http_response_code(403); exit; } if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['_mkt_ad_engine'] ?? '') !== '') { while (ob_get_level()) { ob_end_clean(); } header('Content-Type: application/json'); CSRF::enforce(); $act = $_POST['_mkt_ad_engine']; if ($act === 'load_status') { try { $campaigns = Database::fetchAll("SELECT id, name, status, budget, spend, impressions, clicks, conversions, created_at FROM marketing_campaigns WHERE status_code != 7003 ORDER BY created_at DESC LIMIT 20"); } catch (\Throwable $e) { $campaigns = []; } exit(json_encode(['ok' => true, 'campaigns' => $campaigns])); } exit(json_encode(['ok' => false, 'error' => 'Unknown action'])); } ?> <script nonce="<?= $cspNonce ?> "> (function(){ var panel = document.getElementById('panel-ad_engine'); if (!panel) return; function load(){ var fd = new FormData(); fd.append('_mkt_ad_engine','load_status'); fd.append('_csrf', getCSRF()); fetch('', {method:'POST',body:fd,credentials:'same-origin'}) .then(function(r){return r.json()}) .then(function(d){ render(d.campaigns || []); }) .catch(function(){ render([]); }); } function render(campaigns){ var h = '<div class="mkt-section"><div class="mkt-section-hdr"><span class="mkt-icon" style="color:var(--blue)">●</span><span class="mkt-title">AD ENGINE</span><span class="mkt-badge" style="color:var(--text-dim)">META ADS API</span></div>'; if(!campaigns.length){ h += '<div style="text-align:center;padding:40px;color:var(--text-dim);font-size:12px">No active ad campaigns. Connect Meta Business API in Settings to get started.</div>'; } else { h += '<table class="data-table"><thead><tr><th>Campaign</th><th>Status</th><th>Budget</th><th>Spend</th><th>Impressions</th><th>Clicks</th><th>Conv</th></tr></thead><tbody>'; campaigns.forEach(function(c){ var sc = c.status==='active'?'var(--green)':'var(--text-dim)'; h += '<tr><td style="font-weight:700">'+c.name+'</td><td><span style="color:'+sc+';font-size:8px;font-weight:900;letter-spacing:1px">'+c.status.toUpperCase()+'</span></td><td>$'+(parseFloat(c.budget)||0).toFixed(2)+'</td><td>$'+(parseFloat(c.spend)||0).toFixed(2)+'</td><td>'+(c.impressions||0)+'</td><td>'+(c.clicks||0)+'</td><td>'+(c.conversions||0)+'</td></tr>'; }); h += '</tbody></table>'; } h += '</div>'; panel.innerHTML = h; } load(); })(); </script>