__( 'You are not allowed to do that.', 'sharelyai' ) ), 403 ); } check_ajax_referer( self::ACTION ); wp_send_json_success( $this->state() ); } /** * Everything the screen shows that can change while a sync runs. * * Formatted here rather than in JavaScript: `number_format_i18n()` knows * the site's locale and the browser does not know the site's locale. * * @return array */ public function state(): array { if ( ! $this->connection->is_connected() ) { return array( 'connected' => false, 'running' => false, ); } $sync = new Sync( $this->connection ); $run = $sync->run(); $stats = $sync->stats(); $total = (int) ( $run['total'] ?? 0 ); $done = (int) ( $run['done'] ?? 0 ); $running = $sync->is_running(); return array( 'connected' => true, 'running' => $running, 'run' => array( 'total' => $total, 'done' => $done, 'percent' => $total > 0 ? min( 100, (int) round( ( $done / $total ) * 100 ) ) : 0, 'text' => sprintf( /* translators: 1: entries done, 2: entries in total. */ __( '%1$s of %2$s done.', 'sharelyai' ), number_format_i18n( $done ), number_format_i18n( $total ) ), ), 'scope' => array( 'in_scope' => number_format_i18n( $stats['in_scope'] ), 'synced' => number_format_i18n( $stats['synced'] ), 'percent' => $stats['in_scope'] > 0 ? min( 100, (int) round( ( $stats['synced'] / $stats['in_scope'] ) * 100 ) ) : 0, 'waiting' => sprintf( /* translators: 1: entries waiting, 2: entries that failed. */ __( '%1$s waiting · %2$s failed', 'sharelyai' ), number_format_i18n( $stats['queued'] ), number_format_i18n( $stats['failed'] ) ), ), 'recent' => $this->recent( $sync ), ); } /** * The last few entries, newest first — the part that shows it is moving. * * @param Sync $sync The sync service. * * @return array */ private function recent( Sync $sync ): array { $recent = array(); foreach ( array_slice( $sync->log(), 0, 5 ) as $event ) { $title = (string) $event['title']; $recent[] = array( 'title' => '' !== $title ? $title : '#' . (int) $event['post_id'], 'outcome' => Status::outcome_label( (string) $event['outcome'] ), 'failed' => 'failed' === $event['outcome'], ); } return $recent; } }