$seconds, 'display' => sprintf( /* translators: %s: human-readable interval */ __( 'TransformX – %s', 'ai-publisher' ), human_time_diff( 0, $seconds ) ), ]; return $schedules; } /* --------------------------------------------------------------------- * * Event Scheduling Helpers * ------------------------------------------------------------------- */ private static function clamp_qty( $raw ): int { $qty = absint( $raw ); if ( $qty < 1 ) { $qty = 1; } elseif ( $qty > self::MAX_QTY ) { $qty = self::MAX_QTY; } return $qty; } /** * Ensure WP-Cron is scheduled per the current Schedule settings. * * Called on plugin activation and when schedule settings change. */ public static function maybe_reschedule(): void { // Clear and reschedule on settings change to avoid duplicates. wp_clear_scheduled_hook( self::EVENT_HOOK ); $sched = Settings::schedule(); if ( 'manual' === $sched['interval'] || empty( $sched['count'] ) ) { return; } $interval = $sched['interval']; $count = absint( $sched['count'] ); $seconds = ( 'day' === $interval ) ? max( self::MIN_SECS, (int) ( DAY_IN_SECONDS / $count ) ) : max( self::MIN_SECS, (int) ( WEEK_IN_SECONDS / $count ) ); $interval_key = 'aipub_interval'; if ( ! wp_next_scheduled( self::EVENT_HOOK ) ) { wp_schedule_event( time() + $seconds, $interval_key, self::EVENT_HOOK ); } } /** * Schedule the Cron event when missing, based on current Schedule settings. * * Called on plugins_loaded to ensure an event exists for automatic runs. */ public static function schedule_if_missing(): void { $sched = Settings::schedule(); if ( 'manual' === $sched['interval'] || empty( $sched['count'] ) ) { return; } $count = absint( $sched['count'] ); $seconds = ( 'day' === $sched['interval'] ) ? max( self::MIN_SECS, (int) ( DAY_IN_SECONDS / $count ) ) : max( self::MIN_SECS, (int) ( WEEK_IN_SECONDS / $count ) ); if ( ! wp_next_scheduled( self::EVENT_HOOK ) ) { wp_schedule_event( time() + $seconds, 'aipub_interval', self::EVENT_HOOK ); } } /** * Remove any scheduled events for post generation. */ public static function clear(): void { wp_clear_scheduled_hook( self::EVENT_HOOK ); } /* --------------------------------------------------------------------- * * Cron Callback * ------------------------------------------------------------------- */ public static function maybe_generate_post(): void { aipub_debug_log( 'TransformX: cron trigger' ); $api_key = get_option( 'aipub_openai_api_key' ); $brand_ok = Brand_Strategy::is_complete(); $keywords = Context::load_json( 'keywords.json' ); $ready = $api_key && $brand_ok && $keywords; if ( ! $ready ) { if ( ! $api_key ) { aipub_debug_log( 'TransformX: missing OpenAI API key' ); } elseif ( ! $brand_ok ) { aipub_debug_log( 'TransformX: brand strategy incomplete' ); } elseif ( ! $keywords ) { aipub_debug_log( 'TransformX: missing keywords.json' ); } return; } list( $allowed, $msg ) = Limits::can_generate(); if ( ! $allowed ) { return; } // Prevent PHP timeouts during long-running post-generation if ( wp_doing_cron() && function_exists( 'ini_set' ) ) { // phpcs:ignore WordPress.PHP.DiscouragedFunctions.Discouraged,Squiz.PHP.DiscouragedFunctions.Discouraged -- allow long-running cron @ini_set( 'max_execution_time', '0' ); } ignore_user_abort(true); if ( get_transient( 'aipub_cron_lock' ) ) { return; // another run active } set_transient( 'aipub_cron_lock', 1, 10 * MINUTE_IN_SECONDS ); try { if ( class_exists( '\\AiPublisher\\Generator' ) ) { $post_id = ( new Generator() )->generate_post(); if ( $post_id ) { aipub_debug_log( 'TransformX: generated post ' . $post_id ); } } else { throw new \RuntimeException( 'Generator class not found.' ); } } catch ( \Throwable $e ) { aipub_debug_log( 'TransformX: ' . $e->getMessage() ); } delete_transient( 'aipub_cron_lock' ); } } add_action( 'plugins_loaded', [ '\\AiPublisher\\Cron', 'init' ], 20 ); add_action( 'plugins_loaded', [ '\\AiPublisher\\Cron', 'schedule_if_missing' ], 25 );