PluginProbe
WP Reset / 1.50
WP Reset v1.50
trunk 1.0 1.1 1.11 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.77 1.80 1.81 1.82 1.83 1.84 1.85 1.86 1.90 All 42 releases
wp-reset / wp-reset-cli.php

wp-reset-cli.php in WP Reset 1.50, at wp-reset-cli.php

307 lines 11.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP Reset
4 * https://wpreset.com/
5 * (c) WebFactory Ltd, 2017-2019
6 */
7
8
9 // include only file
10 if (!defined('ABSPATH')) {
11 wp_die(__('Do not open this file directly.', 'wp-error'));
12 }
13
14
15 /**
16 * Resets the site to the default values without modifying any files.
17 */
18 class WP_Reset_CLI extends WP_CLI_Command {
19
20 /**
21 * Reset the site database to default values. No files are modified.
22 *
23 * ## OPTIONS
24 *
25 * [--reactivate-theme]
26 * : Reactivate currently active theme after reset.
27 *
28 * [--reactivate-plugins]
29 * : Reactivate all currently active plugins after reset.
30 *
31 * [--deactivate-wp-reset]
32 * : Deactivate WP Reset plugin after reset. By default it will stay active after reset.
33 *
34 * [--yes]
35 * : Answer yes to the confirmation message.
36 *
37 * ## EXAMPLES
38 *
39 * $ wp reset reset --yes
40 * Success: Database has been reset.
41 *
42 * @when after_wp_load
43 */
44 function reset( $args, $assoc_args ) {
45 WP_CLI::confirm( 'Are you sure you want to reset the site? There is NO UNDO!', $assoc_args );
46
47 global $wp_reset;
48 $params = array();
49
50 if ( !empty( $assoc_args['reactivate-theme'] ) ) {
51 $params['reactivate_theme'] = true;
52 }
53 if ( !empty( $assoc_args['disable-wp-reset'] ) ) {
54 $params['reactivate_wpreset'] = false;
55 } else {
56 $params['reactivate_wpreset'] = true;
57 }
58 if ( !empty( $assoc_args['reactivate-plugins'] ) ) {
59 $params['reactivate_plugins'] = true;
60 }
61
62 $result = $wp_reset->do_reinstall( $params );
63 if (is_wp_error($result)) {
64 WP_CLI::error( $result->get_error_message );
65 } else {
66 WP_CLI::success( 'Database has been reset.' );
67 }
68 } // reset
69
70
71 /**
72 * Display WP Reset version.
73 *
74 * @when after_wp_load
75 */
76 function version( $args, $assoc_args ) {
77 global $wp_reset;
78
79 WP_CLI::line( 'WP Reset v' . $wp_reset->version );
80 } // version
81
82
83 /**
84 * Delete selected WordPress objects.
85 *
86 * ## OPTIONS
87 *
88 * <plugins|themes|transients|uploads|custom-tables|htaccess>
89 * : WP objects to delete.
90 *
91 * [--yes]
92 * : Answer yes to the confirmation message.
93 *
94 * [--empty]
95 * : Empty (truncate) custom tables instead of deleting (dropping) them.
96 *
97 * ## EXAMPLES
98 *
99 * $ wp reset delete themes --yes
100 * Success: 3 themes have been deleted.
101 *
102 * $ wp reset delete custom-tables --truncate --yes
103 * Success: 3 custom tables have been emptied.
104 *
105 * $ wp reset delete htaccess --yes
106 * Success: Htaccess file has been deleted.
107 *
108 * @when after_wp_load
109 */
110 function delete( $args, $assoc_args ) {
111 global $wp_reset, $wpdb;
112
113 if ( empty( $args[0] ) ) {
114 WP_CLI::error( 'Please choose a subcommand: plugins, themes, transients, uploads, htaccess or custom-tables.' );
115 return;
116 } elseif ( false == in_array( $args[0], array( 'themes', 'plugins', 'transients', 'uploads', 'htaccess', 'custom-tables' ) ) ) {
117 WP_CLI::error( 'Unknown subcommand. Please choose from: plugins, themes, transients, uploads, htaccess or custom tables.' );
118 } else {
119 $subcommand = $args[0];
120 }
121
122 switch ($subcommand) {
123 case 'themes':
124 WP_CLI::confirm( 'Are you sure you want to delete all themes?', $assoc_args );
125 $cnt = $wp_reset->do_delete_themes( false );
126 WP_CLI::success( $cnt . ' themes have been deleted.' );
127 break;
128 case 'plugins':
129 WP_CLI::confirm( 'Are you sure you want to delete all plugins?', $assoc_args );
130 $cnt = $wp_reset->do_delete_plugins( true, false );
131 WP_CLI::success( $cnt . ' plugins have been deleted.' );
132 break;
133 case 'transients':
134 WP_CLI::confirm( 'Are you sure you want to delete all transients?', $assoc_args );
135 $cnt = $wp_reset->do_delete_transients();
136 WP_CLI::success( $cnt . ' transient database entries have been deleted.' );
137 break;
138 case 'uploads':
139 WP_CLI::confirm( 'Are you sure you want to delete all files & folders in /uploads/ folder?', $assoc_args );
140 $cnt = $wp_reset->do_delete_uploads();
141 WP_CLI::success( $cnt . ' files & folders have been deleted.' );
142 break;
143 case 'custom-tables':
144 if ( !empty( $assoc_args['empty'] ) ) {
145 WP_CLI::confirm( 'Are you sure you want to empty (truncate) all custom tables (prefix: ' . $wpdb->prefix . ')?', $assoc_args );
146 $cnt = $wp_reset->do_truncate_custom_tables();
147 WP_CLI::success( $cnt . ' custom tables have been emptied.' );
148 } else {
149 WP_CLI::confirm( 'Are you sure you want to delete (drop) all custom tables (prefix: ' . $wpdb->prefix . ')?', $assoc_args );
150 $cnt = $wp_reset->do_drop_custom_tables();
151 WP_CLI::success( $cnt . ' custom tables have been deleted.' );
152 }
153 break;
154 case 'htaccess':
155 WP_CLI::confirm( 'Are you sure you want to delete the .htaccess file?', $assoc_args );
156 $tmp = $wp_reset->do_delete_htaccess();
157 if ( !is_wp_error( $tmp ) ) {
158 WP_CLI::success( 'Htaccess file has been deleted.' );
159 } else {
160 WP_CLI::error( 'Htaccess file has not been deleted. ' . $tmp->get_error_message() );
161 }
162 break;
163 default:
164 // should never come to this but can't hurt
165 WP_CLI::error( 'Unknown subcommand. Please choose from: plugins, themes, transients, uploads, htaccess or custom-tables.' );
166 return;
167 }
168 } // delete
169
170
171 /**
172 * List and manipulate DB snapshots.
173 *
174 * ## OPTIONS
175 *
176 * <list|create|restore|export|delete>
177 * : Action to perform with snapshot.
178 *
179 * [--yes]
180 * : Answer yes to the confirmation message.
181 *
182 * [--id=<snapshot-id>]
183 * : Specify snapshot ID when doing restore, export and delete.
184 *
185 * [--name=<snapshot-name>]
186 * : When creating a new snapshot specify an optional name.
187 *
188 * ## EXAMPLES
189 *
190 * wp reset snapshots create --yes
191 * Success: New snapshot with ID 089bea has been created.
192 *
193 * $ wp reset snapshots delete --id=123456
194 * Success: Snapshot has been deleted.
195 *
196 * $ wp reset snapshots export --id=123456
197 * Success: Snapshot has been exported and saved to: https://test.site/wp-content/wp-reset-snapshots-export/wp-reset-snapshot-123456.sql.gz
198 *
199 * @when after_wp_load
200 */
201 function snapshots( $args, $assoc_args ) {
202 global $wp_reset;
203
204 if ( empty( $args[0] ) ) {
205 WP_CLI::error( 'Please choose a subcommand: list, create, restore, export or delete.' );
206 return;
207 } elseif ( false == in_array( $args[0], array( 'list', 'create', 'restore', 'export', 'delete' ) ) ) {
208 WP_CLI::error( 'Unknown subcommand. Please choose from: list, create, restore, export or delete.' );
209 } else {
210 $subcommand = $args[0];
211 }
212
213 switch ($subcommand) {
214 case 'list':
215 if ( $snapshots = $wp_reset->get_snapshots() ) {
216 $table = array();
217 foreach ( $snapshots as $ss ) {
218 $tmp = array();
219 $tmp['id'] = $ss['uid'];
220 if ( !empty( $ss['name'] ) ) {
221 $tmp['name'] = $ss['name'];
222 } else {
223 $tmp['name'] = 'n/a';
224 }
225 $tmp['created'] = date( get_option( 'date_format' ), strtotime( $ss['timestamp'] ) ) . ' @ ' . date( get_option( 'time_format' ), strtotime( $ss['timestamp'] ) );
226 $tmp['info'] = $ss['tbl_core'] . ' standard & ';
227 if ( $ss['tbl_custom'] ) {
228 $tmp['info'] .= $ss['tbl_custom'] . ' custom table' . ( $ss['tbl_custom'] == 1? '': 's' );
229 } else {
230 $tmp['info'] .= 'no custom tables';
231 }
232 $tmp['info'] .= ' totaling ' . $wp_reset->format_size( $ss['tbl_size'] ) . ' in ' . number_format( $ss['tbl_rows'] ) . ' rows';
233
234 $table[] = $tmp;
235 } // foreach
236 WP_CLI\Utils\format_items( 'table', $table, array('id', 'name', 'created', 'info') );
237 } else {
238 WP_CLI::line( 'There are no saved snapshots.' );
239 }
240 break;
241 case 'create':
242 if ( !empty( $assoc_args['name'] ) ) {
243 $name = trim( $assoc_args['name'] );
244 } else {
245 $name = '';
246 }
247
248 WP_CLI::confirm( 'Are you sure you want to create a new snapshot?', $assoc_args );
249 $new = $wp_reset->do_create_snapshot( $name );
250 if ( is_wp_error( $new ) ) {
251 WP_CLI::error( $new->get_error_message() );
252 } else {
253 WP_CLI::success( 'New snapshot with ID ' . $new['uid'] . ' has been created.' );
254 }
255 break;
256 case 'restore':
257 if ( empty( $assoc_args['id'] ) ) {
258 WP_CLI::error( 'Please specify the snapshot ID with the "--id=123456" param. Use "wp reset snapshots list" to get a list of all snapshots.' );
259 break;
260 } else {
261 WP_CLI::confirm( 'Are you sure you want to restore the site to the snapshot with ID ' . $assoc_args['id'] . '?', $assoc_args );
262 $restore = $wp_reset->do_restore_snapshot( $assoc_args['id'] );
263 if ( is_wp_error( $restore ) ) {
264 WP_CLI::error( $restore->get_error_message() );
265 } else {
266 WP_CLI::success( 'Site has been restored to the selected snapshot.' );
267 }
268 }
269 break;
270 case 'export':
271 if ( empty( $assoc_args['id'] ) ) {
272 WP_CLI::error( 'Please specify the snapshot ID with the "--id=123456" param. Use "wp reset snapshots list" to get a list of all snapshots.' );
273 break;
274 } else {
275 $export = $wp_reset->do_export_snapshot( $assoc_args['id'] );
276 if ( is_wp_error( $export ) ) {
277 WP_CLI::error( $export->get_error_message() );
278 } else {
279 $url = content_url() . '/' . $wp_reset->snapshots_folder . '/' . $export;
280 WP_CLI::success( 'Snapshot has been exported and saved to: ' . $url );
281 }
282 }
283 break;
284 case 'delete':
285 if ( empty( $assoc_args['id'] ) ) {
286 WP_CLI::error( 'Please specify the snapshot ID with the "--id=123456" param. Use "wp reset snapshots list" to get a list of all snapshots.' );
287 break;
288 } else {
289 WP_CLI::confirm( 'Are you sure you want to delete the snapshot with ID ' . $assoc_args['id'] . '?', $assoc_args );
290 $del = $wp_reset->do_delete_snapshot( $assoc_args['id'] );
291 if ( is_wp_error( $del ) ) {
292 WP_CLI::error( $del->get_error_message() );
293 } else {
294 WP_CLI::success( 'Snapshot has been deleted.' );
295 }
296 }
297 break;
298 default:
299 // it should never come to this but can't hurt
300 WP_CLI::error( 'Unknown subcommand. Please choose from: list, create, restore, export or delete.' );
301 return;
302 }
303 } // snapshots
304 } // WP_Reset_CLI
305
306 WP_CLI::add_command( 'reset', 'WP_Reset_CLI' );
307