, notice: ?string} */ public function settings(): array { $raw = get_option( self::OPTION, array() ); if ( ! is_array( $raw ) ) { $raw = array(); } $interval = isset( $raw['interval'] ) && is_string( $raw['interval'] ) && in_array( $raw['interval'], self::INTERVALS, true ) ? $raw['interval'] : 'hourly'; /** @var ?array $last_result */ $last_result = isset( $raw['last_result'] ) && is_array( $raw['last_result'] ) ? $raw['last_result'] : null; return array( 'enabled' => (bool) ( $raw['enabled'] ?? false ), 'interval' => $interval, // Defaults to true to match the import form's checkbox, which // ships checked. Defaulting the other way would make the first // cycle after a default import strip the organizer block from // every description it had just added. // Off by default now that organizer details render on the event // page. A site that already configured sync keeps whatever it // chose — this only changes what an unconfigured site starts at. 'append_organizer' => (bool) ( $raw['append_organizer'] ?? false ), 'target_login' => isset( $raw['target_login'] ) && is_string( $raw['target_login'] ) && '' !== $raw['target_login'] ? $raw['target_login'] : null, 'last_sync' => isset( $raw['last_sync'] ) && is_string( $raw['last_sync'] ) && '' !== $raw['last_sync'] ? $raw['last_sync'] : null, 'last_result' => $last_result, 'notice' => isset( $raw['notice'] ) && is_string( $raw['notice'] ) && '' !== $raw['notice'] ? $raw['notice'] : null, ); } /** * Persist sync settings and bring the cron schedule in line. * * @throws \InvalidArgumentException On an unknown interval. */ public function configure( bool $enabled, string $interval, bool $append_organizer, ?string $target_login ): void { if ( ! in_array( $interval, self::INTERVALS, true ) ) { throw new \InvalidArgumentException( sprintf( 'Unknown sync interval "%s".', esc_html( $interval ) ) ); } $this->save( array( 'enabled' => $enabled, 'interval' => $interval, 'append_organizer' => $append_organizer, 'target_login' => $target_login, 'notice' => null, ) ); $this->reschedule(); } /** * Keep the schedule in repair: enabled wants a cron event at the * configured interval, disabled wants none. Safe to call on every * request (the remote-calendar sync pattern). */ public function register(): void { $this->reschedule(); } /** * One cron firing: sync changes since the last successful cycle, then * record the new window. A failed cycle records the error, keeps the * previous window (the engine wrote nothing), and never throws into * the cron request. An overlapping firing is silently skipped. */ public function run_cycle(): void { $settings = $this->settings(); if ( ! $settings['enabled'] ) { return; } if ( ! $this->reader()->is_available() ) { $this->save( array( 'enabled' => false, 'notice' => __( 'TEC sync disabled itself: no The Events Calendar data was found. Re-enable it from the Import screen if TEC comes back.', 'agenticdaisy-calendar' ), ) ); $this->reschedule(); return; } if ( false !== get_transient( self::LOCK ) ) { return; } set_transient( self::LOCK, '1', self::LOCK_SECONDS ); try { $this->do_cycle( $settings ); } catch ( \Throwable $e ) { $this->save( array( 'last_result' => array( 'time' => gmdate( 'Y-m-d H:i:s' ), 'error' => $e->getMessage(), ), ) ); } finally { delete_transient( self::LOCK ); } } /** * One manual cycle (CLI "sync now"), independent of whether scheduled * sync is enabled. Uses and advances the same last-sync window. * * @return array{import_id: int, added: int, updated: int, unchanged: int, removed: int, instances: int, categories: int, tags: int, skipped: int, warnings: list} * @throws \RuntimeException When no TEC data exists or a cycle is * already running. * @throws \Throwable On sync failure — nothing was written and the * window did not advance. */ public function run_now(): array { if ( ! $this->reader()->is_available() ) { throw new \RuntimeException( esc_html__( 'No data from The Events Calendar was found in this site\'s database.', 'agenticdaisy-calendar' ) ); } if ( false !== get_transient( self::LOCK ) ) { throw new \RuntimeException( esc_html__( 'A TEC sync cycle is already running.', 'agenticdaisy-calendar' ) ); } set_transient( self::LOCK, '1', self::LOCK_SECONDS ); try { return $this->do_cycle( $this->settings() ); } finally { delete_transient( self::LOCK ); } } /** * The cycle itself: read changes since the (overlap-padded) last * window, mirror them, record the new window. The caller holds the * lock and decides how failures surface. * * @param array{enabled: bool, interval: string, append_organizer: bool, target_login: ?string, last_sync: ?string, last_result: ?array, notice: ?string} $settings Current settings. * @return array{import_id: int, added: int, updated: int, unchanged: int, removed: int, instances: int, categories: int, tags: int, skipped: int, warnings: list} * @throws \Throwable On sync failure. */ private function do_cycle( array $settings ): array { $cycle_start = gmdate( 'Y-m-d H:i:s' ); $since = null; if ( null !== $settings['last_sync'] ) { $since = ( new DateTimeImmutable( $settings['last_sync'], new DateTimeZone( 'UTC' ) ) ) ->modify( '-' . self::OVERLAP_SECONDS . ' seconds' ); } $report = $this->tab()->run_sync( $settings['append_organizer'], $settings['target_login'], $since ); $this->save( array( 'last_sync' => $cycle_start, 'last_result' => array( 'time' => $cycle_start, 'added' => $report['added'], 'updated' => $report['updated'], 'unchanged' => $report['unchanged'], 'removed' => $report['removed'], 'skipped' => $report['skipped'], ), ) ); return $report; } /** * Forget the incremental window so the next cycle scans everything. * * Called when the mirror is deleted ("Remove all TEC events"). Cycles * only ask TEC for posts modified since last_sync, so a timestamp that * outlived the mirror would make a later re-enable copy across just the * events edited in between and silently omit all the rest. */ public function forget_window(): void { $this->save( array( 'last_sync' => null, 'last_result' => null, ) ); } /** * Merge changes into the stored option, preserving unrelated keys. * * @param array $changes Keys to overwrite. */ private function save( array $changes ): void { $raw = get_option( self::OPTION, array() ); if ( ! is_array( $raw ) ) { $raw = array(); } update_option( self::OPTION, array_merge( $raw, $changes ), false ); } private function reschedule(): void { $settings = $this->settings(); $scheduled = wp_next_scheduled( self::HOOK ); if ( ! $settings['enabled'] ) { if ( false !== $scheduled ) { wp_clear_scheduled_hook( self::HOOK ); } return; } if ( false !== $scheduled ) { if ( wp_get_schedule( self::HOOK ) === $settings['interval'] ) { return; } wp_clear_scheduled_hook( self::HOOK ); } wp_schedule_event( time(), $settings['interval'], self::HOOK ); } private function reader(): TecReaderInterface { $this->reader ??= new WpdbTecReader(); return $this->reader; } private function tab(): TecMigrationTab { $this->tab ??= TecMigrationTab::create(); return $this->tab; } }