Skip to content

Commit 8a37b1a

Browse files
committed
Update error messages and check packages on restore command
1 parent 8670105 commit 8a37b1a

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

snapshot-command.php

+1-12
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,4 @@
1010
require_once $wpcli_snapshot_autoloader;
1111
}
1212

13-
$wpcli_snapshot_verify_required_packages = function() {
14-
$utils = new \WP_CLI\Snapshot\Utils();
15-
$utils->available_wp_packages();
16-
};
17-
18-
WP_CLI::add_command(
19-
'snapshot',
20-
'\WP_CLI\Snapshot\SnapshotCommand',
21-
[
22-
'before_invoke' => $wpcli_snapshot_verify_required_packages,
23-
]
24-
);
13+
WP_CLI::add_command( 'snapshot', '\WP_CLI\Snapshot\SnapshotCommand' );

src/SnapshotCommand.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,10 @@ public function __construct() {
9898
throw new \Exception( 'Snapshot command requires ZipArchive.' );
9999
}
100100

101-
$this->db = new SnapshotDB();
102-
$this->storage = new SnapshotStorage();
103-
$this->progress = Utils\make_progress_bar( 'Creating Backup', 5 );
101+
$this->db = new SnapshotDB();
102+
$this->storage = new SnapshotStorage();
103+
$this->snapshot_utils = new WP_CLI\Snapshot\Utils();
104+
$this->progress = Utils\make_progress_bar( 'Creating Backup', 5 );
104105

105106
}
106107

@@ -252,6 +253,7 @@ public function _list( $args, $assoc_args ) {
252253
* @throws WP_CLI\ExitException
253254
*/
254255
public function restore( $args, $assoc_args ) {
256+
$this->snapshot_utils->available_wp_packages(); // Check required packages available or not.
255257
$backup_info = $this->get_backup_info( $args[0] );
256258
$snapshot_files = [];
257259
$extra_snapshot_info = $this->db->get_extra_snapshot_info( $backup_info['id'] );

src/utils.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private function get_packages_list() {
6161
}
6262

6363
foreach ( $packages_raw_list as $package ) {
64-
$this->installed_packages[ $package['name'] ] = $package['name'];
64+
$this->installed_packages[] = $package['name'];
6565
}
6666
}
6767

@@ -72,9 +72,11 @@ private function get_packages_list() {
7272
*/
7373
private function check_missing_packages() {
7474
foreach ( $this->required_packages as $package_name => $installation_command ) {
75-
if ( empty( $this->installed_packages[ $package_name ] ) ) {
76-
$this->missing_packages[ $package_name ] = $installation_command;
75+
if ( in_array( $package_name, $this->installed_packages, true ) ) {
76+
continue;
7777
}
78+
79+
$this->missing_packages[ $package_name ] = $installation_command;
7880
}
7981

8082
if ( empty( $this->missing_packages ) ) {
@@ -91,10 +93,10 @@ private function check_missing_packages() {
9193
*/
9294
private function show_missing_packages_info() {
9395
foreach ( $this->missing_packages as $package_name => $installation_command ) {
94-
WP_CLI::warning( "Missing '$package_name' package. Try '$installation_command'." );
96+
WP_CLI::warning( "Missing '{$package_name}' package. Try '{$installation_command}'." );
9597
}
9698

97-
WP_CLI::error( 'Snapshot command needs to be installed above packages.' );
99+
WP_CLI::error( 'Snapshot command requires above packages to be installed.' );
98100
}
99101

100102
}

0 commit comments

Comments
 (0)