insert( $tables['tasks'], [ 'task_type' => sanitize_text_field($type), 'task_status' => 'pending', 'task_payload' => wp_json_encode($payload, JSON_UNESCAPED_UNICODE), 'risk_level' => sanitize_text_field($risk), 'created_at' => current_time('mysql'), ], ['%s', '%s', '%s', '%s', '%s'] ); return absint($wpdb->insert_id); } public static function complete($id, $result = []) { global $wpdb; $tables = AIWEST_Database::tables(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->update( $tables['tasks'], [ 'task_status' => 'completed', 'task_result' => wp_json_encode($result, JSON_UNESCAPED_UNICODE), 'completed_at' => current_time('mysql'), ], ['id' => absint($id)], ['%s', '%s', '%s'], ['%d'] ); } public static function recent($limit = 20) { global $wpdb; $tables = AIWEST_Database::tables(); $limit = max(1, min(100, absint($limit))); // The task screen needs current execution state from the private plugin table. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return $wpdb->get_results($wpdb->prepare('SELECT * FROM %i ORDER BY id DESC LIMIT %d', $tables['tasks'], $limit)); } }