PluginProbe
WP Reset / 1.60
WP Reset v1.60
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.60, at wp-reset-cli.php

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