process_auto_queue(); } elseif ( class_exists( '\\Accessibility_Audio_TTS\\Admin\\Admin_Secure' ) ) { $admin = new \Accessibility_Audio_TTS\Admin\Admin_Secure(); $admin->process_auto_queue(); } else { accessibility_audio_tts_debug_log( 'CRON ERROR: Neither Admin_Cron_Manager nor Admin_Secure class found' ); } } catch ( \Exception $e ) { accessibility_audio_tts_debug_log( 'CRON ERROR in process_queue: ' . $e->getMessage() ); } } /** * Execute background posts processing. * * @param array $args Arguments passed to the hook. */ public function execute_background_posts( $args = array() ): void { accessibility_audio_tts_debug_log( 'CRON: accessibility_audio_tts_process_background_posts triggered' ); try { if ( class_exists( '\\Accessibility_Audio_TTS\\Admin\\Admin_Secure' ) ) { $admin = new \Accessibility_Audio_TTS\Admin\Admin_Secure(); $admin->process_background_posts( $args ); } else { accessibility_audio_tts_debug_log( 'CRON ERROR: Admin_Secure class not found' ); } } catch ( \Exception $e ) { accessibility_audio_tts_debug_log( 'CRON ERROR in background_posts: ' . $e->getMessage() ); } } /** * Execute security cleanup. */ public function execute_security_cleanup(): void { accessibility_audio_tts_debug_log( 'CRON: accessibility_audio_tts_security_cleanup triggered' ); try { if ( class_exists( '\\Accessibility_Audio_TTS\\Admin\\Admin_Cron_Manager' ) ) { $cron_manager = new \Accessibility_Audio_TTS\Admin\Admin_Cron_Manager(); $cron_manager->cleanup_security_data(); } else { accessibility_audio_tts_debug_log( 'CRON ERROR: Admin_Cron_Manager class not found' ); } } catch ( \Exception $e ) { accessibility_audio_tts_debug_log( 'CRON ERROR in security_cleanup: ' . $e->getMessage() ); } } /** * Execute a custom CRON job. * * @param string $job_name The name of the job. * @param callable $callback The callback to execute. * @param array $args Arguments to pass to the callback. * @return bool True on success, false on failure. */ public function execute_custom_job( string $job_name, callable $callback, array $args = array() ): bool { accessibility_audio_tts_debug_log( "CRON: Executing custom job: {$job_name}" ); try { call_user_func_array( $callback, $args ); accessibility_audio_tts_debug_log( "CRON: Successfully executed custom job: {$job_name}" ); return true; } catch ( \Exception $e ) { accessibility_audio_tts_debug_log( "CRON ERROR in custom job {$job_name}: " . $e->getMessage() ); return false; } } /** * Get execution statistics for CRON jobs. * * @return array Statistics about CRON job executions. */ public function get_execution_stats(): array { $stats = array(); // Get next scheduled times for all our hooks. $hooks = array( 'accessibility_audio_tts_process_queue', 'accessibility_audio_tts_security_cleanup', ); foreach ( $hooks as $hook ) { $next_run = wp_next_scheduled( $hook ); $stats[ $hook ] = array( 'next_run' => $next_run, 'next_run_formatted' => $next_run ? gmdate( 'Y-m-d H:i:s', $next_run ) : 'Not scheduled', 'is_scheduled' => (bool) $next_run, ); } return $stats; } /** * Check if CRON is working properly. * * @return bool True if CRON is working, false otherwise. */ public function is_cron_working(): bool { // Check if WP-Cron is disabled. if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) { accessibility_audio_tts_debug_log( 'WP-Cron is disabled via DISABLE_WP_CRON constant' ); return false; } // Check if any of our events are scheduled. $hooks = array( 'accessibility_audio_tts_process_queue', 'accessibility_audio_tts_security_cleanup', ); $scheduled_count = 0; foreach ( $hooks as $hook ) { if ( wp_next_scheduled( $hook ) ) { ++$scheduled_count; } } return $scheduled_count > 0; } }