sync_schedule(); } /** * Schedule, reschedule, or unschedule the scan cron to match the * current mode/interval settings. * * @return void */ private function sync_schedule(): void { $mode = (string) aiou_get_setting( 'duplicates', 'automation', 'schedule_mode', 'off' ); $interval = (string) aiou_get_setting( 'duplicates', 'automation', 'schedule_interval', 'daily' ); $next = wp_next_scheduled( self::CRON_HOOK ); if ( 'off' === $mode ) { if ( $next ) { wp_clear_scheduled_hook( self::CRON_HOOK ); } return; } $current_interval = $next ? wp_get_schedule( self::CRON_HOOK ) : false; if ( ! $next || $current_interval !== $interval ) { if ( $next ) { wp_clear_scheduled_hook( self::CRON_HOOK ); } wp_schedule_event( time(), $interval, self::CRON_HOOK ); } } /** * The scheduled scan's cron callback. * * @return void */ public function run_scheduled_scan(): void { $mode = (string) aiou_get_setting( 'duplicates', 'automation', 'schedule_mode', 'off' ); if ( 'off' === $mode ) { return; } $pairs = DuplicateFinder::find_pairs(); if ( 'report' === $mode ) { /* translators: %d: number of duplicates found */ $this->log( sprintf( __( 'Scheduled scan (report only): %d duplicate(s) found. Nothing was deleted.', 'all-in-one-utilities' ), count( $pairs ) ) ); if ( ! empty( $pairs ) ) { $this->send_status_email( $pairs, false ); } return; } $removed = array(); foreach ( $pairs as $pair ) { if ( DuplicateFinder::remove( $pair['id'], DuplicateFinder::force_delete(), $pair['org_id'] ) ) { $removed[] = $pair; } } /* translators: %d: number of duplicates removed */ $this->log( sprintf( __( 'Scheduled scan: %d duplicate(s) moved to Trash.', 'all-in-one-utilities' ), count( $removed ) ) ); if ( ! empty( $removed ) ) { $this->send_status_email( $removed, true ); } } /** * Append one entry to the capped activity log. * * @param string $message Log message. * @return void */ public function log( string $message ): void { $entries = get_option( self::LOG_OPTION, array() ); if ( ! is_array( $entries ) ) { $entries = array(); } $entries[] = array( 'time' => current_time( 'mysql' ), 'message' => $message, ); if ( count( $entries ) > self::LOG_CAP ) { $entries = array_slice( $entries, -self::LOG_CAP ); } update_option( self::LOG_OPTION, $entries, false ); } /** * The activity log, most recent first. * * @return array */ public static function get_log(): array { $entries = get_option( self::LOG_OPTION, array() ); if ( ! is_array( $entries ) ) { return array(); } return array_reverse( $entries ); } /** * Send the "Scheduled Scan Report" email (Settings → Duplicate Posts → * Notifications), if it's switched on. * * @param array $pairs The pairs this run found/removed. * @param bool $deleted Whether this run deleted (true) or only reported (false). * @return void */ private function send_status_email( array $pairs, bool $deleted ): void { $email = aiou()->emails()->get( DuplicatePostsReportEmail::ID ); if ( $email instanceof DuplicatePostsReportEmail ) { $email->send_report( $pairs, $deleted ); } } }