$args Positional arguments. * @param array $assoc_args Options. */ public function sync( $args, $assoc_args ): void { unset( $args ); $sync = new Sync( $this->connection ); $started = $sync->start_full(); if ( ! $started['ok'] ) { \WP_CLI::error( $started['message'] ); } if ( 0 === $started['total'] ) { \WP_CLI::success( 'Nothing to send: no published content is in scope.' ); return; } \WP_CLI::log( sprintf( 'Queued %d entries in batches of %d.', $started['total'], Sync::BATCH ) ); if ( ! isset( $assoc_args['now'] ) ) { \WP_CLI::success( 'Queued. The scheduler will work through it; run with --now to do it here.' ); return; } $queue = new Queue(); $progress = \WP_CLI\Utils\make_progress_bar( 'Sending', (int) ceil( $started['total'] / Sync::BATCH ) ); while ( $queue->has_pending() ) { $ran = $queue->run_now( $sync ); if ( 0 === $ran ) { // Nothing ran but something is pending: a scheduler that will not // give us the work. Stopping beats spinning. break; } $progress->tick( $ran ); } $progress->finish(); $this->report( $sync ); } /** * Shows what this site believes about its workspace. */ public function status(): void { $sync = new Sync( $this->connection ); $stats = $sync->stats(); if ( $this->connection->is_connected() ) { $name = (string) $this->connection->get( 'workspace_name' ); \WP_CLI::log( sprintf( 'Connected to %s.', '' !== $name ? $name : $this->connection->workspace_id() ) ); } else { \WP_CLI::log( 'Not connected.' ); } \WP_CLI\Utils\format_items( 'table', array( array( 'what' => 'In scope', 'count' => $stats['in_scope'], ), array( 'what' => 'Synced', 'count' => $stats['synced'], ), array( 'what' => 'Queued', 'count' => $stats['queued'], ), array( 'what' => 'Failed', 'count' => $stats['failed'], ), ), array( 'what', 'count' ) ); foreach ( $sync->failures() as $failure ) { \WP_CLI::warning( sprintf( '#%d %s — %s', $failure['post_id'], $failure['title'], $failure['message'] ) ); } } /** * Fixes differences between this site and the workspace. * * Re-sends anything the workspace has lost, and deletes anything of this * site's that no post claims any more. */ public function reconcile(): void { $result = ( new Sync( $this->connection ) )->reconcile(); if ( ! $result['ok'] ) { \WP_CLI::error( $result['message'] ); } \WP_CLI::success( sprintf( 'Re-sent %d, deleted %d left over.', $result['resent'], $result['orphans'] ) ); } /** * Prints what a run did. * * @param Sync $sync The sync service. */ private function report( Sync $sync ): void { $run = $sync->run(); \WP_CLI::success( sprintf( '%d sent: %d new, %d updated, %d unchanged, %d removed, %d failed.', (int) ( $run['done'] ?? 0 ), (int) ( $run['created'] ?? 0 ), (int) ( $run['updated'] ?? 0 ), (int) ( $run['unchanged'] ?? 0 ), (int) ( $run['removed'] ?? 0 ), (int) ( $run['failed'] ?? 0 ) ) ); foreach ( $sync->failures() as $failure ) { \WP_CLI::warning( sprintf( '#%d %s — %s', $failure['post_id'], $failure['title'], $failure['message'] ) ); } } }