`. * * @package ElevenRank * @since 1.0.0 */ namespace ElevenRank\CLI; use ElevenRank\Database; use ElevenRank\Modules\Redirects\Path_Normalizer; use ElevenRank\Modules\Redirects\Redirect_Csv; use ElevenRank\Modules\Redirects\Redirect_Repository; use ElevenRank\Modules\Redirects\Rule_Validator; use ElevenRank\Settings; use WP_CLI; use WP_Error; defined( 'ABSPATH' ) || exit; // WP-CLI hands every command both argument lists, whether it needs them or not. // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed /** * Redirects as CSV files, with the same checks as the admin screen. * * @since 1.0.0 */ final class Redirects_Command { /** * Settings store. * * @since 1.0.0 * * @var Settings */ private $settings; /** * Constructor. * * @since 1.0.0 * * @param Settings $settings Settings store. */ public function __construct( Settings $settings ) { $this->settings = $settings; } /** * Imports redirects from a CSV file with the columns source, destination, type, match and status. * * Every row is checked; rows with problems are skipped and listed. * * ## OPTIONS * * * : Path of the CSV file (at most 1 MB). * * ## EXAMPLES * * wp elevenrank redirects import redirects.csv * * @param array $args Positional arguments. * @param array $assoc_args Flags. * @return void */ public function import( $args, $assoc_args ): void { if ( ! $this->ready() ) { return; } if ( ! is_readable( $args[0] ) ) { WP_CLI::error( 'The file cannot be read.' ); return; } $result = $this->csv()->import( (string) file_get_contents( $args[0] ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- A local file named on the command line. if ( $result instanceof WP_Error ) { WP_CLI::error( $result->get_error_message() ); return; } foreach ( $result['errors'] as $error ) { WP_CLI::warning( 'Line ' . $error['line'] . ': ' . $error['message'] ); } WP_CLI::success( sprintf( '%d imported, %d skipped.', $result['imported'], $result['skipped'] ) ); } /** * Exports all redirects to a CSV file. * * ## OPTIONS * * * : Path of the file to write. * * ## EXAMPLES * * wp elevenrank redirects export redirects.csv * * @param array $args Positional arguments. * @param array $assoc_args Flags. * @return void */ public function export( $args, $assoc_args ): void { if ( ! $this->ready() ) { return; } if ( false === file_put_contents( $args[0], $this->csv()->export() ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- A local file named on the command line. WP_CLI::error( 'The file cannot be written.' ); return; } WP_CLI::success( 'Redirects written to ' . $args[0] . '.' ); } /** * Whether the tables are in place; says so when they are not. * * @since 1.0.0 * * @return bool */ private function ready(): bool { if ( Database::is_ready() ) { return true; } WP_CLI::error( 'The redirects table is not ready. Open the WordPress admin once, then try again.' ); return false; } /** * The CSV handler. * * @since 1.0.0 * * @return Redirect_Csv */ private function csv(): Redirect_Csv { $repository = new Redirect_Repository(); return new Redirect_Csv( new Rule_Validator( $repository, Path_Normalizer::from_settings( $this->settings ) ), $repository ); } }