← Back
Editing: class-db-handler.php
<?php defined( 'ABSPATH' ) || exit; class ADM_Alt_DB_Handler { public static function install() { global $wpdb; $charset = $wpdb->get_charset_collate(); // Check if table is missing the page_title column (old schema) — if so, drop and recreate $table = $wpdb->prefix . ADM_ALT_LOG_TABLE; $cols = $wpdb->get_col( "SHOW COLUMNS FROM $table", 0 ); if ( $cols && ! in_array( 'page_title', $cols, true ) ) { $wpdb->query( "DROP TABLE IF EXISTS $table" ); } $log = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}" . ADM_ALT_LOG_TABLE . " ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, attachment_id BIGINT UNSIGNED NOT NULL DEFAULT 0, image_url TEXT NOT NULL, page_title TEXT, old_alt TEXT, new_alt TEXT, status VARCHAR(20) NOT NULL DEFAULT 'applied', gps_written TINYINT(1) NOT NULL DEFAULT 0, gps_error TEXT, error_msg TEXT, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY attachment_id (attachment_id), KEY status (status) ) $charset;"; $queue = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}" . ADM_ALT_QUEUE_TABLE . " ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, attachment_id BIGINT UNSIGNED NOT NULL, image_url TEXT NOT NULL, page_title TEXT, queued_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id) ) $charset;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $log ); dbDelta( $queue ); update_option( 'adm_alt_db_version', ADM_ALT_VERSION ); } public static function maybe_migrate() { // Always run install — dbDelta is safe on existing tables (adds missing columns, never removes) self::install(); } // ── Queue ───────────────────────────────────────────────────────────────── public static function enqueue( array $items ) { global $wpdb; $table = $wpdb->prefix . ADM_ALT_QUEUE_TABLE; foreach ( array_chunk( $items, 500 ) as $chunk ) { $values = []; $placeholders = []; foreach ( $chunk as $item ) { $placeholders[] = '(%d, %s, %s)'; $values[] = (int) $item['attachment_id']; $values[] = esc_url_raw( $item['image_url'] ); $values[] = sanitize_text_field( $item['page_title'] ?? '' ); } $sql = "INSERT INTO $table (attachment_id, image_url, page_title) VALUES " . implode( ', ', $placeholders ); $wpdb->query( $wpdb->prepare( $sql, ...$values ) ); } } public static function dequeue( int $limit ): array { global $wpdb; $table = $wpdb->prefix . ADM_ALT_QUEUE_TABLE; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table ORDER BY id ASC LIMIT %d", $limit ), ARRAY_A ); if ( $rows ) { $ids = implode( ',', array_map( 'intval', array_column( $rows, 'id' ) ) ); $wpdb->query( "DELETE FROM $table WHERE id IN ($ids)" ); } return $rows ?: []; } public static function queue_count(): int { global $wpdb; return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}" . ADM_ALT_QUEUE_TABLE ); } public static function clear_queue() { global $wpdb; $wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}" . ADM_ALT_QUEUE_TABLE ); } // ── Log ─────────────────────────────────────────────────────────────────── public static function log( array $data ): bool { global $wpdb; $table = $wpdb->prefix . ADM_ALT_LOG_TABLE; // Ensure table exists before every insert $exists = $wpdb->get_var( "SHOW TABLES LIKE '$table'" ); if ( ! $exists ) { self::install(); } $result = $wpdb->insert( $table, [ 'attachment_id' => (int) ( $data['attachment_id'] ?? 0 ), 'image_url' => esc_url_raw( $data['image_url'] ?? '' ), 'page_title' => sanitize_text_field( $data['page_title'] ?? '' ), 'old_alt' => $data['old_alt'] ?? '', 'new_alt' => $data['new_alt'] ?? '', 'status' => $data['status'] ?? 'applied', 'gps_written' => (int) ( $data['gps_written'] ?? 0 ), 'gps_error' => $data['gps_error'] ?? null, 'error_msg' => $data['error_msg'] ?? null, ], [ '%d', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s' ] ); if ( $result === false ) { // Store the DB error so dashboard can surface it update_option( 'adm_alt_db_error', $wpdb->last_error . ' | Table: ' . $table ); } else { delete_option( 'adm_alt_db_error' ); } return $result !== false; } public static function get_logs( int $limit = 100, int $offset = 0, string $status = '' ): array { global $wpdb; $table = $wpdb->prefix . ADM_ALT_LOG_TABLE; if ( $status ) { return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table WHERE status = %s ORDER BY created_at DESC LIMIT %d OFFSET %d", $status, $limit, $offset ), ARRAY_A ); } return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table ORDER BY created_at DESC LIMIT %d OFFSET %d", $limit, $offset ), ARRAY_A ); } public static function count_logs( string $status = '' ): int { global $wpdb; $table = $wpdb->prefix . ADM_ALT_LOG_TABLE; if ( $status ) { return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $table WHERE status = %s", $status ) ); } return (int) $wpdb->get_var( "SELECT COUNT(*) FROM $table" ); } public static function clear_logs() { global $wpdb; $wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}" . ADM_ALT_LOG_TABLE ); } public static function rollback_all() { global $wpdb; $rows = $wpdb->get_results( "SELECT attachment_id, old_alt FROM {$wpdb->prefix}" . ADM_ALT_LOG_TABLE . " WHERE status = 'applied'", ARRAY_A ); foreach ( $rows as $row ) { update_post_meta( (int) $row['attachment_id'], '_wp_attachment_image_alt', $row['old_alt'] ); } $wpdb->query( "UPDATE {$wpdb->prefix}" . ADM_ALT_LOG_TABLE . " SET status = 'rolled_back' WHERE status = 'applied'" ); } }
Save File
Cancel