|null */ public function current(): ?array { global $wpdb; $raw = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", self::OPTION ) ); if ( null === $raw ) { return null; } $run = maybe_unserialize( $raw ); return is_array( $run ) ? $run : null; } /** * Whether a run is in progress. * * @return bool */ public function is_running(): bool { $run = $this->current(); return null !== $run && self::RUNNING === $run['status']; } /** * Starts a new run. * * @param string $scope Library scope. * @param int $total Images in scope. * @return array */ public function start( string $scope, int $total ): array { $now = time(); $run = array( 'id' => wp_generate_uuid4(), 'status' => self::RUNNING, 'scope' => $scope, 'job' => LibraryScanner::SCOPE_CONVERT === $scope ? self::JOB_CONVERT : self::JOB_OPTIMIZE, 'total' => $total, 'cursor' => 0, 'started_at' => $now, 'resumed_at' => $now, 'active_seconds' => 0, 'finished_at' => 0, ); $this->write( $run ); return $run; } /** * Merges changes into the run. * * @param array $changes Fields to change. * @return void */ public function update( array $changes ): void { $run = $this->current(); if ( null !== $run ) { $this->write( array_merge( $run, $changes ) ); } } /** * Pauses a running run. Finished images stay finished. * * @return void */ public function pause(): void { $run = $this->current(); if ( null === $run || self::RUNNING !== $run['status'] ) { return; } $run['status'] = self::PAUSED; $run['active_seconds'] += max( 0, time() - (int) $run['resumed_at'] ); $this->write( $run ); } /** * Resumes a paused run from the start of its scope. * * Images handled before the pause no longer match the scope, so restarting the cursor * picks up exactly the ones whose queued jobs were skipped while paused. * * @return void */ public function resume(): void { $run = $this->current(); if ( null === $run || self::PAUSED !== $run['status'] ) { return; } $run['status'] = self::RUNNING; $run['resumed_at'] = time(); $run['cursor'] = 0; $this->write( $run ); } /** * Cancels the run. Images already processed keep their results. * * @return void */ public function cancel(): void { $run = $this->current(); if ( null === $run || ! in_array( $run['status'], array( self::RUNNING, self::PAUSED ), true ) ) { return; } if ( self::RUNNING === $run['status'] ) { $run['active_seconds'] += max( 0, time() - (int) $run['resumed_at'] ); } $run['status'] = self::CANCELLED; $run['finished_at'] = time(); $this->write( $run ); } /** * Marks the run finished. * * @return void */ public function complete(): void { $run = $this->current(); if ( null === $run ) { return; } $run['status'] = self::COMPLETED; $run['active_seconds'] += max( 0, time() - (int) $run['resumed_at'] ); $run['finished_at'] = time(); $this->write( $run ); delete_option( self::CURRENT_OPTION ); } /** * Records the file being processed, for display. * * @param string $filename File name. * @return void */ public function set_current_file( string $filename ): void { update_option( self::CURRENT_OPTION, $filename, false ); } /** * Builds the progress report. * * @return array */ public function progress(): array { $run = $this->current(); if ( null === $run ) { return array( 'status' => 'idle' ); } $is_convert = self::JOB_CONVERT === ( $run['job'] ?? self::JOB_OPTIMIZE ); $counts = $this->scanner->run_counts( (string) $run['id'], $is_convert ? AttachmentState::META_CONVERT_STATUS : AttachmentState::META_STATUS ); $done = ( $counts['optimized'] ?? 0 ) + ( $counts['already_optimal'] ?? 0 ); $skipped = $counts['skipped'] ?? 0; $failed = $counts['failed'] ?? 0; $processed = $done + $skipped + $failed; $total = max( (int) $run['total'], $processed ); $active = (int) $run['active_seconds']; if ( self::RUNNING === $run['status'] ) { $active += max( 0, time() - (int) $run['resumed_at'] ); } $eta = null; if ( self::RUNNING === $run['status'] && $processed > 0 && $total > $processed ) { $eta = (int) ceil( ( $total - $processed ) * $active / $processed ); } wp_cache_delete( self::CURRENT_OPTION, 'options' ); return array( 'id' => $run['id'], 'status' => $run['status'], 'scope' => $run['scope'], 'job' => $is_convert ? self::JOB_CONVERT : self::JOB_OPTIMIZE, 'total' => $total, 'processed' => $processed, 'done' => $done, 'skipped' => $skipped, 'failed' => $failed, 'current_file' => self::RUNNING === $run['status'] ? (string) get_option( self::CURRENT_OPTION, '' ) : '', 'eta_seconds' => $eta, 'started_at' => (int) $run['started_at'], 'finished_at' => (int) $run['finished_at'], ); } /** * Writes the option. * * @param array $run Run state. * @return void */ private function write( array $run ): void { wp_cache_delete( self::OPTION, 'options' ); update_option( self::OPTION, $run, false ); } }