| @@ -76,19 +76,16 @@ | ||
| 76 | 76 | add_action( 'wp_ajax_wpforo_ai_duplicate_task', [ $this, 'ajax_duplicate_task' ] ); |
| 77 | 77 | add_action( 'wp_ajax_wpforo_ai_get_task_stats', [ $this, 'ajax_get_task_stats' ] ); |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - // Register cron hooks (2 args: task_id, board_id) | |
| 80 | + // Register cron callbacks unconditionally so any already-scheduled | |
| 81 | + // event (from a prior connected state or from a single-event task) | |
| 82 | + // still has a handler. Scheduling of the recurring task-checker is | |
| 83 | + // gated by AI connection — see schedule_cron_jobs(). | |
| 81 | 84 | add_action( 'wpforo_ai_execute_task', [ $this, 'cron_execute_task' ], 10, 2 ); |
| 82 | 85 | add_action( 'wpforo_ai_check_scheduled_tasks', [ $this, 'cron_check_scheduled_tasks' ] ); |
| 83 | - // Hook for run_on_approval tasks (3 args: task_id, topic_id, board_id) | |
| 84 | 86 | add_action( 'wpforo_ai_execute_task_for_topic', [ $this, 'cron_execute_task_for_topic' ], 10, 3 ); |
| 85 | 87 | |
| 86 | - // Schedule the task checker if not already scheduled | |
| 87 | - if ( ! wp_next_scheduled( 'wpforo_ai_check_scheduled_tasks' ) ) { | |
| 88 | - wp_schedule_event( time(), 'hourly', 'wpforo_ai_check_scheduled_tasks' ); | |
| 89 | - } | |
| 90 | - | |
| 91 | 88 | // Check and reschedule overdue tasks when admin page loads |
| 92 | 89 | add_action( 'admin_init', [ $this, 'reschedule_overdue_tasks' ] ); |
| 93 | 90 | |
| 94 | 91 | // Hook into topic/post approval for run_on_approval tasks |
| @@ -97,8 +94,35 @@ | ||
| 97 | 94 | |
| 98 | 95 | // Also hook into topic/post creation for run_on_approval tasks (for content created with status=0) |
| 99 | 96 | add_action( 'wpforo_after_add_topic', [ $this, 'on_topic_created' ], 20, 2 ); |
| 100 | 97 | add_action( 'wpforo_after_add_post', [ $this, 'on_post_created' ], 20, 3 ); |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * Schedule the recurring task-checker cron. | |
| 102 | + * | |
| 103 | + * Called from AIClient::register_ai_crons() on tenant connect. | |
| 104 | + */ | |
| 105 | + public function schedule_cron_jobs() { | |
| 106 | + if ( ! wp_next_scheduled( 'wpforo_ai_check_scheduled_tasks' ) ) { | |
| 107 | + wp_schedule_event( time(), 'hourly', 'wpforo_ai_check_scheduled_tasks' ); | |
| 108 | + } | |
| 109 | + } | |
| 110 | + | |
| 111 | + /** | |
| 112 | + * Unschedule the recurring task-checker cron. | |
| 113 | + * | |
| 114 | + * Called from AIClient::unregister_ai_crons() on tenant disconnect. | |
| 115 | + * Single-event tasks (wpforo_ai_execute_task / _for_topic) are cleared | |
| 116 | + * elsewhere on a per-task basis; the recurring checker is the only one | |
| 117 | + * managed here. | |
| 118 | + */ | |
| 119 | + public function unschedule_cron_jobs() { | |
| 120 | + $ts = wp_next_scheduled( 'wpforo_ai_check_scheduled_tasks' ); | |
| 121 | + if ( $ts ) { | |
| 122 | + wp_unschedule_event( $ts, 'wpforo_ai_check_scheduled_tasks' ); | |
| 123 | + } | |
| 124 | + wp_clear_scheduled_hook( 'wpforo_ai_check_scheduled_tasks' ); | |
| 101 | 125 | } |
| 102 | 126 | |
| 103 | 127 | /** |
| 104 | 128 | * Reschedule overdue active tasks |