prefix . 'aiotsa_' . $n; } function aiotsa_db_install() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $c = $wpdb->get_charset_collate(); dbDelta("CREATE TABLE " . aiotsa_tbl('audits') . " ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, target varchar(255) NOT NULL DEFAULT '', mode varchar(20) NOT NULL DEFAULT 'own', status varchar(20) NOT NULL DEFAULT 'queued', lang varchar(8) NOT NULL DEFAULT 'en', score int(11) NOT NULL DEFAULT 0, created datetime NOT NULL, finished datetime DEFAULT NULL, token varchar(40) NOT NULL DEFAULT '', opts longtext, tasks longtext, results longtext, summary longtext, PRIMARY KEY (id), KEY status (status) ) $c;"); dbDelta("CREATE TABLE " . aiotsa_tbl('items') . " ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, audit_id bigint(20) unsigned NOT NULL DEFAULT 0, kind varchar(20) NOT NULL DEFAULT 'post', ref bigint(20) unsigned NOT NULL DEFAULT 0, url varchar(500) NOT NULL DEFAULT '', score int(11) NOT NULL DEFAULT 0, data longtext, PRIMARY KEY (id), KEY audit_id (audit_id, kind) ) $c;"); dbDelta("CREATE TABLE " . aiotsa_tbl('log') . " ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, created datetime NOT NULL, post_id bigint(20) unsigned NOT NULL DEFAULT 0, field varchar(40) NOT NULL DEFAULT '', old_value longtext, new_value longtext, undone tinyint(1) NOT NULL DEFAULT 0, PRIMARY KEY (id) ) $c;"); update_option('aiotsa_db', AIOTSA_VERSION, false); } function aiotsa_audit_get($id) { global $wpdb; $r = $wpdb->get_row($wpdb->prepare('SELECT * FROM %i WHERE id = %d', aiotsa_tbl('audits'), $id), ARRAY_A); // phpcs:ignore WordPress.DB.DirectDatabaseQuery if (!$r) return null; foreach (['opts', 'tasks', 'results', 'summary'] as $k) { $v = json_decode((string) $r[$k], true); $r[$k] = is_array($v) ? $v : []; } $r['id'] = (int) $r['id']; $r['score'] = (int) $r['score']; return $r; } function aiotsa_audit_save($id, $fields) { global $wpdb; foreach (['opts', 'tasks', 'results', 'summary'] as $k) if (isset($fields[$k]) && is_array($fields[$k])) $fields[$k] = wp_json_encode($fields[$k]); $wpdb->update(aiotsa_tbl('audits'), $fields, ['id' => (int) $id]); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } function aiotsa_audit_create($target, $mode, $opts) { global $wpdb; $wpdb->insert(aiotsa_tbl('audits'), ['target' => $target, 'mode' => $mode, 'status' => 'queued', 'lang' => $opts['lang'] ?? 'en', 'created' => current_time('mysql', true), // phpcs:ignore WordPress.DB.DirectDatabaseQuery 'token' => wp_generate_password(32, false), 'opts' => wp_json_encode($opts), 'tasks' => '[]', 'results' => '{}', 'summary' => '{}']); return (int) $wpdb->insert_id; } function aiotsa_audit_list($limit = 30) { global $wpdb; return $wpdb->get_results($wpdb->prepare('SELECT id, target, mode, status, lang, score, created, finished FROM %i ORDER BY id DESC LIMIT %d', aiotsa_tbl('audits'), $limit), ARRAY_A); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } function aiotsa_audit_delete($id) { global $wpdb; $wpdb->delete(aiotsa_tbl('audits'), ['id' => (int) $id]); $wpdb->delete(aiotsa_tbl('items'), ['audit_id' => (int) $id]); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } function aiotsa_audit_latest($mode = 'own', $status = 'done', $before = 0) { global $wpdb; $before = (int) $before; $id = $wpdb->get_var($wpdb->prepare('SELECT id FROM %i WHERE mode = %s AND status = %s AND (%d = 0 OR id < %d) ORDER BY id DESC LIMIT 1', aiotsa_tbl('audits'), $mode, $status, $before, $before)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery return $id ? (int) $id : 0; } function aiotsa_audit_active() { global $wpdb; $id = $wpdb->get_var($wpdb->prepare("SELECT id FROM %i WHERE status IN ('running','queued') ORDER BY id ASC LIMIT 1", aiotsa_tbl('audits'))); // phpcs:ignore WordPress.DB.DirectDatabaseQuery return $id ? (int) $id : 0; } /** The audit to show on the Audit tab: running, waiting or paused. */ function aiotsa_audit_current() { global $wpdb; $id = $wpdb->get_var($wpdb->prepare("SELECT id FROM %i WHERE status IN ('running','queued','paused') ORDER BY id DESC LIMIT 1", aiotsa_tbl('audits'))); // phpcs:ignore WordPress.DB.DirectDatabaseQuery return $id ? (int) $id : 0; } function aiotsa_item_add($audit, $kind, $ref, $url, $score, $data) { global $wpdb; $wpdb->insert(aiotsa_tbl('items'), ['audit_id' => $audit, 'kind' => $kind, 'ref' => $ref, 'url' => substr($url, 0, 500), 'score' => $score, 'data' => wp_json_encode($data)]); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } function aiotsa_items($audit, $kind) { global $wpdb; $rows = $wpdb->get_results($wpdb->prepare('SELECT ref, url, score, data FROM %i WHERE audit_id = %d AND kind = %s ORDER BY id ASC', aiotsa_tbl('items'), $audit, $kind), ARRAY_A); // phpcs:ignore WordPress.DB.DirectDatabaseQuery foreach ($rows as &$r) { $d = json_decode((string) $r['data'], true); $r['data'] = is_array($d) ? $d : []; $r['ref'] = (int) $r['ref']; $r['score'] = (int) $r['score']; } return $rows; } function aiotsa_items_clear($audit, $kind, $ref = null) { global $wpdb; $w = ['audit_id' => $audit, 'kind' => $kind]; if ($ref !== null) $w['ref'] = $ref; $wpdb->delete(aiotsa_tbl('items'), $w); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } /** Keep only the newest N finished audits. */ function aiotsa_prune() { global $wpdb; $keep = max(2, (int) aiotsa_settings()['keep']); $ids = $wpdb->get_col($wpdb->prepare("SELECT id FROM %i WHERE status IN ('done','cancelled','failed') ORDER BY id DESC LIMIT 500 OFFSET %d", aiotsa_tbl('audits'), $keep)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery foreach ($ids as $id) aiotsa_audit_delete($id); }