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

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