$methods, 'callback' => $cb, 'permission_callback' => $perm ?: $adm]); }; $err = function ($e) { return new WP_REST_Response(['error' => is_wp_error($e) ? $e->get_error_message() : (string) $e], 400); }; $r('/bg', 'POST', function ($q) { if (!hash_equals(aiotsa_tick_token(), (string) $q->get_param('k'))) return new WP_REST_Response(['ok' => false], 403); $id = aiotsa_audit_active(); if ($id) { aiotsa_run($id, 20); if (aiotsa_audit_active()) aiotsa_spawn(); } return ['ok' => true]; }, '__return_true'); $r('/audits', 'GET', function () { return ['list' => aiotsa_audit_list(), 'active' => aiotsa_audit_active()]; }); $r('/audits', 'POST', function ($q) use ($err) { $mode = in_array($q['mode'], apply_filters('aiotsa_modes', ['own', 'page']), true) ? $q['mode'] : 'own'; $pages = []; foreach ((array) $q['pages'] as $k => $v) $pages[sanitize_key($k)] = esc_url_raw((string) $v); $opts = ['lang' => sanitize_key((string) $q['lang']), 'scope' => in_array($q['scope'], ['all', 'quick'], true) ? $q['scope'] : 'all', 'pages' => $pages, 'max' => max(1, min(5000, (int) ($q['max'] ?: 2000)))]; $opts = apply_filters('aiotsa_rest_opts', $opts, $q); $id = aiotsa_start($mode === 'own' ? home_url('/') : (string) $q['target'], $mode, $opts); if (is_wp_error($id)) return $err($id); if ($mode === 'own') { $S = aiotsa_settings(); $S['pages'] = array_merge($S['pages'], array_filter($pages)); update_option('aiotsa_settings', $S, false); } return ['id' => $id, 'progress' => aiotsa_progress(aiotsa_audit_get($id))]; }); $r('/audits/(?P\d+)', 'GET', function ($q) use ($err) { $A = aiotsa_audit_get((int) $q['id']); return $A ? aiotsa_progress($A) : $err('Not found'); }); $r('/audits/(?P\d+)/tick', 'POST', function ($q) use ($err) { $id = (int) $q['id']; $act = aiotsa_audit_active(); if ($act) aiotsa_run($act, 7); $A = aiotsa_audit_get($id); return $A ? aiotsa_progress($A) : $err('Not found'); }); $r('/audits/(?P\d+)/action', 'POST', function ($q) use ($err) { $id = (int) $q['id']; $do = (string) $q['do']; if ($do === 'delete') { aiotsa_audit_delete($id); return ['ok' => true]; } if ($do === 'retry') return ['ok' => aiotsa_retry($id)]; return ['ok' => aiotsa_set_status($id, $do)]; }); $r('/audits/(?P\d+)/report', 'GET', function ($q) use ($err) { $A = aiotsa_audit_get((int) $q['id']); if (!$A) return $err('Not found'); $lang = sanitize_key((string) $q['lang']); if (!isset(aiotsa_langs()[$lang])) $lang = $A['lang']; $rep = aiotsa_report($A, $lang); $ex = []; foreach (apply_filters('aiotsa_export_types', ['print']) as $t) $ex[$t] = html_entity_decode(aiotsa_export_url($t, $A['id'], $lang)); return apply_filters('aiotsa_rest_report', ['html' => aiotsa_report_html($rep, 'admin'), 'status' => $A['status'], 'lang' => $lang, 'export' => $ex, 'target' => $A['target'], 'mode' => $A['mode'], 'failed' => count(array_filter($A['tasks'], function ($t) { return (int) $t['s'] === 2; }))], $A, $lang); }); $r('/audits/(?P\d+)/rescan', 'POST', function ($q) use ($err) { $A = aiotsa_audit_get((int) $q['id']); $pid = (int) $q['post']; if (!$A || $A['mode'] !== 'own' || $A['status'] !== 'done' || !get_post($pid)) return $err(__('This page cannot be checked again.', 'aiot-site-audit')); aiotsa_items_clear($A['id'], 'post', $pid); aiotsa_task_posts($A, [$pid]); aiotsa_task_finalize($A); aiotsa_audit_save($A['id'], ['results' => $A['results'], 'summary' => $A['summary'], 'score' => $A['score']]); $m = get_post_meta($pid, '_aiotsa_score', true); return ['ok' => true, 'score' => $A['score'], 'post_score' => is_array($m) ? (int) $m['s'] : 0]; }); $r('/detect', 'GET', function () { $A = ['mode' => 'own', 'opts' => ['pages' => aiotsa_settings()['pages']], 'summary' => ['ctx' => []]]; return ['pages' => aiotsa_find_pages($A)]; }); $r('/keys/test', 'POST', function ($q) use ($err) { $w = (string) $q['which']; $x = apply_filters('aiotsa_key_test', null, $w); if ($x !== null) return is_wp_error($x) ? $err($x) : $x; if ($w === 'psi') { $k = aiotsa_key('psi'); if ($k === '') return $err(__('Add a key first.', 'aiot-site-audit')); $x = wp_remote_get('https://www.googleapis.com/pagespeedonline/v5/runPagespeed?' . http_build_query(['url' => 'https://www.google.com/', 'strategy' => 'mobile', 'category' => 'performance', 'key' => $k]), ['timeout' => 45]); if (is_wp_error($x)) return $err($x); $j = json_decode(wp_remote_retrieve_body($x), true); return isset($j['lighthouseResult']) ? ['ok' => true] : $err($j['error']['message'] ?? __('Google did not accept this key.', 'aiot-site-audit')); } return $err('Unknown key'); }); $r('/tools/robots', 'POST', function ($q) { $bots = []; $known = aiotsa_ai_bots(); foreach ((array) $q['bots'] as $b => $v) if (isset($known[$b]) && in_array($v, ['allow', 'block', 'none'], true) && $v !== 'none') $bots[$b] = $v; $o = ['on' => !empty($q['on']) ? 1 : 0, 'bots' => $bots]; if (!empty($q['save'])) update_option('aiotsa_robots', $o, false); return ['text' => aiotsa_robots_text($bots), 'saved' => !empty($q['save']), 'physical' => aiotsa_robots_physical()]; }); $r('/tools/llms', 'POST', function ($q) { if (!empty($q['generate'])) return ['text' => aiotsa_llms_generate()]; update_option('aiotsa_llms', ['on' => !empty($q['on']) ? 1 : 0, 'text' => sanitize_textarea_field((string) $q['text'])], false); return ['saved' => true, 'url' => home_url('/llms.txt'), 'physical' => file_exists(ABSPATH . 'llms.txt')]; }); $r('/tools/links', 'GET', function () { return ['rows' => aiotsa_link_ideas(), 'audit' => aiotsa_audit_latest('own')]; }); });