PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.1
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.1
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / CLI / RestoreCommand.php

RestoreCommand.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.1, at classes/CLI/RestoreCommand.php

151 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare(strict_types=1);
3
4 namespace Imagify\CLI;
5
6 use Imagify\Bulk\Bulk;
7
8 /**
9 * Command class for the bulk restore.
10 *
11 * Restores all optimized media back to their original state.
12 *
13 * ## EXAMPLES
14 *
15 * # Restore all optimized images (library + custom folders).
16 * $ wp imagify restore
17 *
18 * # Restore only WordPress media library images.
19 * $ wp imagify restore library
20 *
21 * # Restore only custom folders images.
22 * $ wp imagify restore custom-folders
23 *
24 * # Restore both contexts explicitly.
25 * $ wp imagify restore library custom-folders
26 *
27 * @since 2.3
28 */
29 class RestoreCommand extends AbstractCommand {
30
31 /**
32 * Map of user-friendly context names to internal context identifiers.
33 *
34 * @var array<string, string>
35 */
36 private const CONTEXT_MAP = [
37 'library' => 'wp',
38 'custom-folders' => 'custom-folders',
39 ];
40
41 /**
42 * Executes the command.
43 *
44 * @param array $arguments Positional argument.
45 * @param array $options Optional arguments.
46 */
47 public function __invoke( $arguments, $options ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
48 if ( empty( $arguments ) ) {
49 $arguments = array_keys( self::CONTEXT_MAP );
50 }
51
52 $contexts = [];
53
54 foreach ( $arguments as $arg ) {
55 if ( ! isset( self::CONTEXT_MAP[ $arg ] ) ) {
56 \WP_CLI::error(
57 sprintf(
58 'Invalid context: "%s". Valid values are: %s',
59 $arg,
60 implode( ', ', array_keys( self::CONTEXT_MAP ) )
61 )
62 );
63 return;
64 }
65
66 $contexts[] = self::CONTEXT_MAP[ $arg ];
67 }
68
69 $total_restored = 0;
70 $total_errors = 0;
71
72 foreach ( $contexts as $index => $context ) {
73 $label = $arguments[ $index ];
74
75 \WP_CLI::log( sprintf( 'Restoring optimized media for: %s', $label ) );
76
77 $result = Bulk::get_instance()->run_restore( $context );
78
79 if ( ! $result['success'] ) {
80 \WP_CLI::warning( sprintf( 'No optimized media to restore for: %s', $label ) );
81 continue;
82 }
83
84 $total_restored += $result['restored'];
85 $total_errors += $result['errors'];
86
87 \WP_CLI::log(
88 sprintf(
89 '%s: %d restored, %d errors out of %d total.',
90 ucfirst( $label ),
91 $result['restored'],
92 $result['errors'],
93 $result['total']
94 )
95 );
96 }
97
98 if ( 0 === $total_restored && 0 === $total_errors ) {
99 \WP_CLI::warning( 'No optimized media found to restore.' );
100 return;
101 }
102
103 if ( $total_errors > 0 ) {
104 \WP_CLI::warning(
105 sprintf(
106 'Restore completed with errors: %d restored, %d failed.',
107 $total_restored,
108 $total_errors
109 )
110 );
111 return;
112 }
113
114 \WP_CLI::success(
115 sprintf(
116 'Restore completed: %d media restored successfully.',
117 $total_restored
118 )
119 );
120 }
121
122 /**
123 * {@inheritdoc}
124 */
125 protected function get_command_name(): string {
126 return 'restore';
127 }
128
129 /**
130 * {@inheritdoc}
131 */
132 public function get_description(): string {
133 return 'Restore all optimized media to their original state';
134 }
135
136 /**
137 * {@inheritdoc}
138 */
139 public function get_synopsis(): array {
140 return [
141 [
142 'type' => 'positional',
143 'name' => 'contexts',
144 'description' => 'The context(s) to restore. Possible values are library and custom-folders. Defaults to all if omitted.',
145 'optional' => true,
146 'repeating' => true,
147 ],
148 ];
149 }
150 }
151