From 10f62ef1d1d79d90aecbfcb7f2a3aa13ab4fc345 Mon Sep 17 00:00:00 2001 From: stoyan-g <67575788+stoyan-g@users.noreply.github.com> Date: Thu, 14 Apr 2022 11:42:52 +0300 Subject: [PATCH 1/5] Check if file exists and bail if not --- src/Import_Command.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Import_Command.php b/src/Import_Command.php index 03a3178ad..5ea8fde45 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -71,8 +71,13 @@ public function __invoke( $args, $assoc_args ) { } } } + $args = $new_args; + if ( empty( $args ) ) { + WP_CLI::error( "Import file doesn't exists." ); + } + foreach ( $args as $file ) { if ( ! is_readable( $file ) ) { WP_CLI::warning( "Can't read '$file' file." ); From 88d3c46e4f4aff79b8d477529f2128b92d64733c Mon Sep 17 00:00:00 2001 From: stoyan-g <67575788+stoyan-g@users.noreply.github.com> Date: Thu, 14 Apr 2022 11:50:46 +0300 Subject: [PATCH 2/5] Check if file exists --- src/Import_Command.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Import_Command.php b/src/Import_Command.php index 5ea8fde45..3bc9419e7 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -71,7 +71,6 @@ public function __invoke( $args, $assoc_args ) { } } } - $args = $new_args; if ( empty( $args ) ) { From ed5da5746000d181e0164bfe29b1069419a72561 Mon Sep 17 00:00:00 2001 From: stoyan-g <67575788+stoyan-g@users.noreply.github.com> Date: Thu, 14 Apr 2022 14:21:38 +0300 Subject: [PATCH 3/5] Improved error handling and warning messages --- src/Import_Command.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Import_Command.php b/src/Import_Command.php index 3bc9419e7..c438a1583 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -65,22 +65,32 @@ public function __invoke( $args, $assoc_args ) { if ( ! empty( $files ) ) { $new_args = array_merge( $new_args, $files ); } + + if ( empty( $files ) ) { + WP_CLI::warning( "No import files found in '$arg'." ); + } } else { - if ( file_exists( $arg ) ) { + if ( ! file_exists( $arg ) ) { + WP_CLI::warning( " '$arg' doesn't exist." ); + continue; + } + + if ( is_readable( $arg ) ) { $new_args[] = $arg; + continue; } + + WP_CLI::warning( "Can't read '$file' file." ); } } - $args = $new_args; - if ( empty( $args ) ) { - WP_CLI::error( "Import file doesn't exists." ); + if ( empty( $new_args ) ) { + WP_CLI::error( "Import failed due to missing or unreadable file/s." ); } + $args = $new_args; + foreach ( $args as $file ) { - if ( ! is_readable( $file ) ) { - WP_CLI::warning( "Can't read '$file' file." ); - } $ret = $this->import_wxr( $file, $assoc_args ); From 7d25e4d27dfcc882f410be30f943aff4ddf9a1ae Mon Sep 17 00:00:00 2001 From: stoyan-g <67575788+stoyan-g@users.noreply.github.com> Date: Thu, 14 Apr 2022 14:33:34 +0300 Subject: [PATCH 4/5] Fix typo --- src/Import_Command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Import_Command.php b/src/Import_Command.php index c438a1583..6bbd9ea69 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -71,7 +71,7 @@ public function __invoke( $args, $assoc_args ) { } } else { if ( ! file_exists( $arg ) ) { - WP_CLI::warning( " '$arg' doesn't exist." ); + WP_CLI::warning( "'$arg' doesn't exist." ); continue; } @@ -80,7 +80,7 @@ public function __invoke( $args, $assoc_args ) { continue; } - WP_CLI::warning( "Can't read '$file' file." ); + WP_CLI::warning( "Can't read '$arg' file." ); } } From f4ffe718d5e1c80f6a4725eefdb8fab83544c087 Mon Sep 17 00:00:00 2001 From: stoyan-g <67575788+stoyan-g@users.noreply.github.com> Date: Thu, 14 Apr 2022 14:55:48 +0300 Subject: [PATCH 5/5] Improve warning message --- src/Import_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Import_Command.php b/src/Import_Command.php index 6bbd9ea69..495591a20 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -67,7 +67,7 @@ public function __invoke( $args, $assoc_args ) { } if ( empty( $files ) ) { - WP_CLI::warning( "No import files found in '$arg'." ); + WP_CLI::warning( "No files found in the import dir '$arg'." ); } } else { if ( ! file_exists( $arg ) ) {