module = $module; } /** * Imports SEO data left by another plugin. Nothing of the other plugin is changed or deleted. * * ## OPTIONS * * * : yoast, rankmath, aioseo or seopress. * * [--dry-run] * : Only count and show sample changes; write nothing. * * [--overwrite] * : Replace values that ElevenRank already has. Without it they are kept. * * [--batch=] * : Items per batch. * --- * default: 50 * --- * * ## EXAMPLES * * wp elevenrank import yoast --dry-run * wp elevenrank import rankmath * * @param array $args Positional arguments. * @param array $assoc_args Flags. * @return void */ public function __invoke( $args, $assoc_args ): void { $sources = $this->module->sources(); $source = $sources[ (string) $args[0] ] ?? null; if ( null === $source ) { WP_CLI::error( 'Unknown source. Use one of: ' . implode( ', ', array_keys( $sources ) ) . '.' ); return; } if ( ! $source->detected() ) { WP_CLI::error( $source->label() . ' has left no data on this site.' ); return; } $dry = isset( $assoc_args['dry-run'] ); $over = isset( $assoc_args['overwrite'] ); $batch = max( 1, min( 200, (int) ( $assoc_args['batch'] ?? 50 ) ) ); $run = $this->module->importer(); WP_CLI::log( ( $dry ? 'Dry run: ' : '' ) . 'Importing from ' . $source->label() . '.' ); foreach ( Importer::STEPS as $step ) { $cursor = 0; $total = array( 'processed' => 0, 'changed' => 0, 'unchanged' => 0, 'skipped' => 0, ); $errors = array(); $notes = array(); do { $result = $run->run( $source, $step, $cursor, $dry, $over, $batch ); if ( $result instanceof WP_Error ) { WP_CLI::warning( $step . ': ' . $result->get_error_message() ); break; } foreach ( array_keys( $total ) as $key ) { $total[ $key ] += (int) $result[ $key ]; } $errors = array_merge( $errors, $result['errors'] ); $notes = array_merge( $notes, $result['notes'] ); $cursor = (int) $result['cursor']; } while ( ! $result['done'] ); WP_CLI::log( sprintf( '%-10s processed %d, changed %d, unchanged %d, skipped %d', $step . ':', $total['processed'], $total['changed'], $total['unchanged'], $total['skipped'] ) ); foreach ( array_unique( $notes ) as $note ) { WP_CLI::log( ' not converted: ' . $note ); } foreach ( $errors as $error ) { WP_CLI::warning( $step . ' #' . $error['id'] . ': ' . $error['message'] ); } } WP_CLI::success( $dry ? 'Dry run finished; nothing was written.' : 'Import finished.' ); } }