← Back
Editing: class-processor.php
<?php defined( 'ABSPATH' ) || exit; class ADM_Alt_Processor { const BATCH_SIZE = 1; public static function init() { add_action( 'wp_ajax_adm_alt_start', [ __CLASS__, 'ajax_start' ] ); add_action( 'wp_ajax_adm_alt_batch', [ __CLASS__, 'ajax_batch' ] ); add_action( 'wp_ajax_adm_alt_progress', [ __CLASS__, 'ajax_progress' ] ); add_action( 'wp_ajax_adm_alt_stop', [ __CLASS__, 'ajax_stop' ] ); add_action( 'wp_ajax_adm_alt_reset', [ __CLASS__, 'ajax_reset' ] ); add_action( 'wp_ajax_adm_alt_retry_errors', [ __CLASS__, 'ajax_retry_errors' ] ); } // ── AJAX: Start ─────────────────────────────────────────────────────────── public static function ajax_start() { check_ajax_referer( 'adm_alt_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_die( 'Forbidden', 403 ); // Check location first — before touching any state $lat = get_option( 'adm_alt_gps_lat', '' ); $lng = get_option( 'adm_alt_gps_lng', '' ); if ( $lat === '' || $lng === '' ) { wp_send_json_error( 'Location not configured. Go to Settings and enter the practice name, website, latitude and longitude.' ); return; } $only_missing = ! empty( $_POST['only_missing'] ); $count = ADM_Alt_Scanner::build_queue( $only_missing ); update_option( 'adm_alt_running', 1 ); update_option( 'adm_alt_stop_flag', 0 ); update_option( 'adm_alt_total', $count ); update_option( 'adm_alt_done', 0 ); update_option( 'adm_alt_cron_mode', 0 ); ADM_Alt_DB_Handler::clear_logs(); wp_send_json_success( [ 'total' => $count ] ); } // ── AJAX: Process one batch ─────────────────────────────────────────────── public static function ajax_batch() { check_ajax_referer( 'adm_alt_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_die( 'Forbidden', 403 ); if ( get_option( 'adm_alt_stop_flag' ) ) { update_option( 'adm_alt_running', 0 ); wp_send_json_success( [ 'done' => true, 'stopped' => true ] ); return; } $items = ADM_Alt_DB_Handler::dequeue( self::BATCH_SIZE ); if ( empty( $items ) ) { update_option( 'adm_alt_running', 0 ); wp_send_json_success( [ 'done' => true ] ); return; } $coords = get_option( 'adm_alt_gps_coords' ); $location_hint = get_option( 'adm_alt_business_address', '' ) ?: ( $coords['address'] ?? '' ); if ( function_exists( 'set_time_limit' ) ) { @set_time_limit( 0 ); } $results = []; foreach ( $items as $item ) { $item['location_hint'] = $location_hint; $result = self::process_one( $item, $coords ?: null ); $results[] = $result; } $done = (int) get_option( 'adm_alt_done', 0 ) + count( $items ); update_option( 'adm_alt_done', $done ); wp_send_json_success( [ 'done' => false, 'results' => $results, 'db_error' => get_option( 'adm_alt_db_error', '' ), 'progress' => [ 'done' => $done, 'total' => (int) get_option( 'adm_alt_total', 0 ), 'queue' => ADM_Alt_DB_Handler::queue_count(), ], ] ); } private static function process_one( array $item, ?array $coords ): array { $att_id = (int) $item['attachment_id']; $old_alt = get_post_meta( $att_id, '_wp_attachment_image_alt', true ) ?: ''; $gen = ADM_Alt_AI_Generator::generate( $item ); $log = [ 'attachment_id' => $att_id, 'image_url' => $item['image_url'], 'page_title' => $item['page_title'] ?? '', 'old_alt' => $old_alt, 'new_alt' => $gen['new_alt'] ?? '', 'status' => $gen['status'], 'error_msg' => $gen['error_msg'] ?? null, 'gps_written' => 0, 'gps_error' => null, ]; if ( $gen['status'] === 'applied' ) { update_post_meta( $att_id, '_wp_attachment_image_alt', $gen['new_alt'] ); if ( $coords ) { $ok = ADM_Alt_Geocoder::write_gps( $att_id, $coords['lat'], $coords['lng'] ); $log['gps_written'] = $ok ? 1 : 0; if ( ! $ok ) { $log['gps_error'] = 'EXIF write failed (Imagick unavailable or unsupported format).'; } } } ADM_Alt_DB_Handler::log( $log ); $log['thumb_url'] = wp_get_attachment_image_url( $att_id, 'thumbnail' ) ?: $item['image_url']; $log['full_url'] = wp_get_attachment_url( $att_id ) ?: $item['image_url']; return $log; } // ── AJAX: Progress ──────────────────────────────────────────────────────── public static function ajax_progress() { check_ajax_referer( 'adm_alt_nonce', 'nonce' ); wp_send_json_success( [ 'running' => (bool) get_option( 'adm_alt_running' ), 'done' => (int) get_option( 'adm_alt_done', 0 ), 'total' => (int) get_option( 'adm_alt_total', 0 ), 'queue' => ADM_Alt_DB_Handler::queue_count(), ] ); } // ── AJAX: Stop ──────────────────────────────────────────────────────────── public static function ajax_stop() { check_ajax_referer( 'adm_alt_nonce', 'nonce' ); update_option( 'adm_alt_stop_flag', 1 ); wp_send_json_success(); } // ── AJAX: Retry all errors ──────────────────────────────────────────────── public static function ajax_retry_errors() { check_ajax_referer( 'adm_alt_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_die( 'Forbidden', 403 ); global $wpdb; $table = $wpdb->prefix . ADM_ALT_LOG_TABLE; $row = $wpdb->get_row( "SELECT * FROM $table WHERE status = 'error' ORDER BY id ASC LIMIT 1", ARRAY_A ); if ( ! $row ) { wp_send_json_success( [ 'done' => true ] ); return; } $coords = get_option( 'adm_alt_gps_coords' ); $location_hint = get_option( 'adm_alt_business_address', '' ) ?: ( $coords['address'] ?? '' ); $item = [ 'attachment_id' => $row['attachment_id'], 'image_url' => $row['image_url'], 'page_title' => $row['page_title'], 'location_hint' => $location_hint, ]; $gen = ADM_Alt_AI_Generator::generate( $item ); $update = [ 'status' => $gen['status'], 'new_alt' => $gen['new_alt'] ?? '', 'error_msg' => $gen['error_msg'] ?? null, 'gps_written' => 0, 'gps_error' => null, ]; if ( $gen['status'] === 'applied' ) { update_post_meta( (int) $row['attachment_id'], '_wp_attachment_image_alt', $gen['new_alt'] ); if ( $coords ) { $ok = ADM_Alt_Geocoder::write_gps( (int) $row['attachment_id'], $coords['lat'], $coords['lng'] ); $update['gps_written'] = $ok ? 1 : 0; if ( ! $ok ) $update['gps_error'] = 'EXIF write failed.'; } } $wpdb->update( $table, $update, [ 'id' => (int) $row['id'] ], [ '%s', '%s', '%s', '%d', '%s' ], [ '%d' ] ); $remaining = (int) $wpdb->get_var( "SELECT COUNT(*) FROM $table WHERE status = 'error'" ); usleep( 1200000 ); wp_send_json_success( [ 'done' => ( $remaining === 0 ), 'remaining' => $remaining, 'result' => array_merge( $item, $update, [ 'id' => $row['id'] ] ), ] ); } // ── AJAX: Reset / clear logs ────────────────────────────────────────────── public static function ajax_reset() { check_ajax_referer( 'adm_alt_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_die( 'Forbidden', 403 ); ADM_Alt_DB_Handler::clear_logs(); ADM_Alt_DB_Handler::clear_queue(); update_option( 'adm_alt_running', 0 ); update_option( 'adm_alt_done', 0 ); update_option( 'adm_alt_total', 0 ); update_option( 'adm_alt_cron_mode', 0 ); wp_send_json_success(); } }
Save File
Cancel