'aiotsa_task_home', 'robots' => 'aiotsa_task_robots', 'sitemap' => 'aiotsa_task_sitemap', 'variants' => 'aiotsa_task_variants', 'ssl' => 'aiotsa_task_ssl', 'dns' => 'aiotsa_task_dns', 'hostinfo' => 'aiotsa_task_hostinfo', 'files' => 'aiotsa_task_files', 'aifetch' => 'aiotsa_task_aifetch', 'wp' => 'aiotsa_task_wp', 'policy' => 'aiotsa_task_policy', 'psi' => 'aiotsa_task_psi', 'posts_plan' => 'aiotsa_task_posts_plan', 'posts' => 'aiotsa_task_posts', 'links_plan' => 'aiotsa_task_links_plan', 'links' => 'aiotsa_task_links', 'finalize' => 'aiotsa_task_finalize']); } /** Progress groups shown to the user: group => [label, task types]. */ function aiotsa_groups() { return ['site' => ['Technical, security and domain checks', ['home', 'robots', 'sitemap', 'variants', 'ssl', 'dns', 'hostinfo', 'whois', 'blacklist', 'files', 'aifetch', 'wp']], 'policy' => ['AdSense and trust pages', ['policy']], 'speed' => ['Speed (Google PageSpeed)', ['psi']], 'posts' => ['Content: every page and post', ['posts_plan', 'posts']], 'links' => ['Links', ['links_plan', 'links']], 'comp' => ['Competitor comparison', ['comp', 'comp_pages']], 'final' => ['Scores and report', ['finalize']], 'ai' => ['AI Insights (Google Gemini)', ['ai']]]; // the last groups appear only when an add-on adds those steps } function aiotsa_plan($mode, $opts) { $t = []; if ($mode === 'page') $types = ['home', 'psi', 'posts_plan', 'links_plan']; else { $types = ['home', 'robots', 'sitemap', 'variants', 'ssl', 'dns', 'hostinfo', 'files', 'aifetch']; if ($mode === 'own') $types[] = 'wp'; $types = array_merge($types, ['policy', 'psi', 'posts_plan', 'links_plan']); } if (($opts['scope'] ?? '') === 'quick') $types = array_values(array_diff($types, ['psi', 'posts_plan', 'links_plan'])); foreach ($types as $x) $t[] = ['t' => $x, 'a' => null, 's' => 0]; $t[] = ['t' => 'finalize', 'a' => null, 's' => 0]; return apply_filters('aiotsa_plan', $t, $mode, $opts); } function aiotsa_tasks_insert(&$A, $add) { if ($add) array_splice($A['tasks'], ($A['_i'] ?? -1) + 1, 0, $add); } function aiotsa_start($target, $mode, $opts) { $target = aiotsa_norm_url($target); if ($target === '') return new WP_Error('aiotsa_url', __('Please enter a valid website address.', 'aiot-site-audit')); if ($mode === 'own' && !aiotsa_is_own($target)) $mode = 'page'; if (!in_array($mode, apply_filters('aiotsa_modes', ['own', 'page']), true)) return new WP_Error('aiotsa_mode', __('This kind of audit is not available.', 'aiot-site-audit')); if ($mode !== 'page') $target = aiotsa_origin($target) . '/'; if ($mode === 'own') $target = home_url('/'); $opts = apply_filters('aiotsa_start_opts', $opts, $mode); $langs = aiotsa_langs(); if (empty($opts['lang']) || !isset($langs[$opts['lang']])) $opts['lang'] = aiotsa_settings()['lang']; $id = aiotsa_audit_create($target, $mode, $opts); aiotsa_audit_save($id, ['tasks' => aiotsa_plan($mode, $opts), 'summary' => ['ctx' => []]]); aiotsa_kick(5); return $id; } function aiotsa_kick($in = 30) { if (!wp_next_scheduled('aiotsa_cron_tick')) wp_schedule_single_event(time() + $in, 'aiotsa_cron_tick'); } function aiotsa_tick_token() { return hash_hmac('sha256', 'aiotsa-tick', wp_salt('nonce')); } function aiotsa_spawn() { wp_remote_post(rest_url('aiotsa/v1/bg'), ['blocking' => false, 'timeout' => 1, 'sslverify' => false, 'body' => ['k' => aiotsa_tick_token()]]); } add_action('aiotsa_cron_tick', function () { $id = aiotsa_audit_active(); if (!$id) return; aiotsa_run($id, 18); if (aiotsa_audit_active()) { aiotsa_kick(60); aiotsa_spawn(); } }); function aiotsa_lock() { global $wpdb; $now = time(); add_option('aiotsa_lock', '0', '', false); $n = $wpdb->query($wpdb->prepare("UPDATE {$wpdb->options} SET option_value = %s WHERE option_name = 'aiotsa_lock' AND CAST(option_value AS UNSIGNED) < %d", (string) $now, $now - 70)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery wp_cache_delete('aiotsa_lock', 'options'); return $n > 0; } function aiotsa_unlock() { global $wpdb; $wpdb->query("UPDATE {$wpdb->options} SET option_value = '0' WHERE option_name = 'aiotsa_lock'"); wp_cache_delete('aiotsa_lock', 'options'); } // phpcs:ignore WordPress.DB.DirectDatabaseQuery function aiotsa_status_of($id) { global $wpdb; return (string) $wpdb->get_var($wpdb->prepare('SELECT status FROM %i WHERE id = %d', aiotsa_tbl('audits'), $id)); } // phpcs:ignore WordPress.DB.DirectDatabaseQuery /** Work on one audit for up to $budget seconds. Safe to call from the browser, WP-Cron and the background request at once. */ function aiotsa_run($id, $budget = 10) { if (!aiotsa_lock()) return false; $start = microtime(true); $A = aiotsa_audit_get($id); $map = aiotsa_task_map(); if (!$A) { aiotsa_unlock(); return false; } if ($A['status'] === 'queued') { $act = aiotsa_audit_active(); if ($act && $act !== $A['id']) { aiotsa_unlock(); return false; } $A['status'] = 'running'; aiotsa_audit_save($id, ['status' => 'running']); } if ($A['status'] !== 'running') { aiotsa_unlock(); return false; } if (function_exists('set_time_limit')) @set_time_limit(max(60, $budget + 50)); // phpcs:ignore while (true) { $i = null; foreach ($A['tasks'] as $k => $t) if ((int) $t['s'] === 0) { $i = $k; break; } if ($i === null) { $A['status'] = 'done'; aiotsa_audit_save($id, ['status' => 'done', 'finished' => current_time('mysql', true), 'score' => $A['score'], 'tasks' => $A['tasks'], 'results' => $A['results'], 'summary' => $A['summary']]); aiotsa_unlock(); aiotsa_done($A); return true; } $t = $A['tasks'][$i]; $A['_i'] = $i; $fn = $map[$t['t']] ?? null; try { if ($fn && function_exists($fn)) { if ($t['a'] === null) $fn($A); else $fn($A, $t['a']); } $A['tasks'][$i]['s'] = 1; } catch (\Throwable $e) { $A['tasks'][$i]['n'] = ($t['n'] ?? 0) + 1; if ($A['tasks'][$i]['n'] >= 2) { $A['tasks'][$i]['s'] = 2; $A['tasks'][$i]['e'] = mb_substr($e->getMessage(), 0, 200); } } unset($A['_i']); if (aiotsa_status_of($id) !== 'running') { aiotsa_audit_save($id, ['tasks' => $A['tasks'], 'results' => $A['results'], 'summary' => $A['summary']]); aiotsa_unlock(); return false; } aiotsa_audit_save($id, ['tasks' => $A['tasks'], 'results' => $A['results'], 'summary' => $A['summary'], 'score' => $A['score']]); update_option('aiotsa_lock', (string) time(), false); if (microtime(true) - $start > $budget) break; } aiotsa_unlock(); return true; } function aiotsa_progress($A) { $g = []; $done = 0; $total = count($A['tasks']); $cur = ''; foreach (aiotsa_groups() as $k => [$label, $types]) { $n = 0; $d = 0; $f = 0; foreach ($A['tasks'] as $t) if (in_array($t['t'], $types, true)) { $n++; if ((int) $t['s'] > 0) $d++; if ((int) $t['s'] === 2) $f++; } if (!$n) continue; $st = $d >= $n ? 'done' : ($d > 0 ? 'running' : 'pending'); $g[] = ['k' => $k, 'label' => aiotsa_t($label, $A['lang']), 'n' => $n, 'd' => $d, 'f' => $f, 'st' => $st]; } foreach ($A['tasks'] as $t) if ((int) $t['s'] > 0) $done++; $first = true; foreach ($g as &$x) { if ($x['st'] !== 'done' && $first) { if ($x['st'] === 'pending') $x['st'] = 'running'; $first = false; } elseif ($x['st'] === 'running' && !$first) $x['st'] = 'pending'; } unset($x); $C = $A['summary']['ctx'] ?? []; $pct = $total ? (int) floor(100 * $done / $total) : 0; if ($A['status'] === 'done') $pct = 100; $el = max(1, time() - strtotime($A['created'] . ' UTC')); $eta = ($done > 3 && $pct < 100) ? (int) round($el * ($total - $done) / $done) : 0; return ['id' => $A['id'], 'status' => $A['status'], 'pct' => $pct, 'groups' => $g, 'posts_done' => (int) ($C['posts_done'] ?? 0), 'posts_total' => (int) ($C['posts_total'] ?? 0), 'eta' => $eta, 'score' => $A['score'], 'target' => $A['target'], 'mode' => $A['mode']]; } /** After an audit finishes: notice, e-mail, housekeeping, next in line. */ function aiotsa_done($A) { update_option('aiotsa_notice', ['id' => $A['id'], 'score' => $A['score'], 'target' => $A['target']], false); if ($A['mode'] === 'own' && ($A['opts']['scope'] ?? 'all') !== 'quick') update_option('aiotsa_last', ['id' => $A['id'], 'score' => $A['score'], 'time' => time(), 'sec' => $A['summary']['sec'] ?? [], 'adsense' => $A['summary']['adsense'] ?? ''], false); do_action('aiotsa_audit_done', $A); aiotsa_prune(); if (aiotsa_audit_active()) aiotsa_kick(5); } function aiotsa_set_status($id, $to) { $A = aiotsa_audit_get($id); if (!$A) return false; $ok = ['pause' => ['running', 'queued'], 'resume' => ['paused'], 'cancel' => ['running', 'queued', 'paused']]; if (!isset($ok[$to]) || !in_array($A['status'], $ok[$to], true)) return false; aiotsa_audit_save($id, ['status' => ['pause' => 'paused', 'resume' => 'queued', 'cancel' => 'cancelled'][$to]]); if ($to === 'resume') aiotsa_kick(5); return true; } /** Run again the tasks that failed, or one named task type (used by "Re-check"). */ function aiotsa_retry($id, $types = null) { $A = aiotsa_audit_get($id); if (!$A || !in_array($A['status'], ['done', 'failed', 'cancelled'], true)) return false; $n = 0; foreach ($A['tasks'] as &$t) if (($types === null && (int) $t['s'] === 2) || ($types !== null && in_array($t['t'], $types, true))) { $t['s'] = 0; $t['n'] = 0; $n++; } unset($t); if (!$n) return false; $has = false; foreach ($A['tasks'] as &$t) if ($t['t'] === 'finalize') { $t['s'] = 0; $has = true; } unset($t); aiotsa_audit_save($id, ['tasks' => $A['tasks'], 'status' => 'queued']); aiotsa_kick(5); return true; }