← Back
Editing: class-cron.php
<?php defined( 'ABSPATH' ) || exit; class ADM_Alt_Cron { const BATCH_SIZE = 5; const CHAIN_DELAY = 60; // seconds between chained batches for the nightly auto-scan // 2 AM IST = 20:30 UTC const DAILY_HOUR_UTC = 20; const DAILY_MIN_UTC = 30; public static function init() { add_action( 'adm_alt_cron_daily', [ __CLASS__, 'start_scan' ] ); add_action( 'adm_alt_cron_process', [ __CLASS__, 'process_batch' ] ); } // ── Schedule daily cron on activation ──────────────────────────────────── public static function schedule() { if ( ! wp_next_scheduled( 'adm_alt_cron_daily' ) ) { wp_schedule_event( self::next_run_timestamp(), 'daily', 'adm_alt_cron_daily' ); } } public static function unschedule() { wp_clear_scheduled_hook( 'adm_alt_cron_daily' ); wp_clear_scheduled_hook( 'adm_alt_cron_process' ); delete_option( 'adm_alt_running' ); delete_option( 'adm_alt_cron_mode' ); } // ── Next 2 AM IST (20:30 UTC) timestamp ────────────────────────────────── public static function next_run_timestamp(): int { $now = time(); $today = mktime( self::DAILY_HOUR_UTC, self::DAILY_MIN_UTC, 0, (int) gmdate( 'n' ), (int) gmdate( 'j' ), (int) gmdate( 'Y' ) ); return $now < $today ? $today : $today + DAY_IN_SECONDS; } // ── Start nightly auto-scan ─────────────────────────────────────────────── public static function start_scan() { if ( get_option( 'adm_alt_running' ) ) { return; // manual scan is running, skip nightly } // Skip if location not configured — GPS would not be set correctly if ( get_option( 'adm_alt_gps_lat', '' ) === '' ) { return; } if ( function_exists( 'set_time_limit' ) ) { @set_time_limit( 0 ); } $count = ADM_Alt_Scanner::build_queue( true ); // only images missing alt text if ( $count === 0 ) { return; } 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', 1 ); ADM_Alt_DB_Handler::clear_logs(); ADM_Alt_Geocoder::get_coords(); self::process_batch(); } // ── Process one batch and chain the next ────────────────────────────────── public static function process_batch() { if ( ! get_option( 'adm_alt_running' ) ) return; if ( get_option( 'adm_alt_stop_flag' ) ) { update_option( 'adm_alt_running', 0 ); update_option( 'adm_alt_cron_mode', 0 ); return; } if ( function_exists( 'set_time_limit' ) ) { @set_time_limit( 0 ); } $coords = get_option( 'adm_alt_gps_coords' ); $location_hint = get_option( 'adm_alt_business_address', '' ) ?: ( $coords['address'] ?? '' ); $items = ADM_Alt_DB_Handler::dequeue( self::BATCH_SIZE ); if ( empty( $items ) ) { update_option( 'adm_alt_running', 0 ); update_option( 'adm_alt_cron_mode', 0 ); update_option( 'adm_alt_last_run', time() ); return; } foreach ( $items as $item ) { $item['location_hint'] = $location_hint; self::process_one( $item, $coords ?: null ); sleep( 1 ); } $done = (int) get_option( 'adm_alt_done', 0 ) + count( $items ); update_option( 'adm_alt_done', $done ); if ( ADM_Alt_DB_Handler::queue_count() > 0 ) { wp_schedule_single_event( time() + self::CHAIN_DELAY, 'adm_alt_cron_process' ); } else { update_option( 'adm_alt_running', 0 ); update_option( 'adm_alt_cron_mode', 0 ); update_option( 'adm_alt_last_run', time() ); } } private static function process_one( array $item, ?array $coords ) { $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.'; } } ADM_Alt_DB_Handler::log( $log ); } }
Save File
Cancel