PluginProbe
Database Reset / trunk
Database Reset vtrunk
2.3.2 3.0 3.0.1 3.0.2 3.1 3.15 3.16 3.17 3.18 3.19 3.20 3.21 3.22 3.23 3.24 3.25 trunk 1.0 1.2 1.2.1 1.2.2 1.3 1.4 2.0 2.1 All 27 releases
wordpress-database-reset / class-db-reset-command.php

class-db-reset-command.php in Database Reset trunk, at class-db-reset-command.php

106 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Reset the database tables.
5 */
6 class DB_Reset_Command extends WP_CLI_Command {
7
8 private $reactivate;
9 private $reporting;
10
11 public function __construct() {
12 $this->resetter = new DB_Resetter();
13 }
14
15 /**
16 * Reset the database tables.
17 *
18 * ## OPTIONS
19 *
20 * <tables>
21 * The list of tables to reset
22 *
23 * <reactivate>
24 * Reactivate current theme and plugins after reset?
25 *
26 * ## EXAMPLES
27 * wp reset database
28 * wp reset database --tables='users,posts,comments'
29 * wp reset database --no-reactivate
30 */
31 public function database( $args, $assoc_args ) {
32 $this->handle_before_reset();
33 $this->reactivate_data( $assoc_args[ 'reactivate' ] );
34 $this->reset( $this->sanitize_input( $assoc_args[ 'tables' ] ) );
35 $this->handle_after_reset();
36 }
37
38 /**
39 * Lists the database tables.
40 *
41 * @subcommand list
42 */
43 public function _list() {
44 $tables = $this->get_wp_tables();
45
46 foreach( $tables as $key => $value ) {
47 WP_CLI::line( $key );
48 }
49 }
50
51 private function handle_before_reset() {
52 $this->disable_error_reporting();
53 }
54
55 private function disable_error_reporting() {
56 // disable error reporting during reset
57 $this->reporting = error_reporting(); //phpcs:ignore
58 error_reporting( 0 ); //phpcs:ignore
59 }
60
61 private function sanitize_input( $string = '' ) {
62 if ( ! empty ( $string ) ) {
63 return explode( ',', preg_replace( '/\s+/', '', $string ) );
64 }
65
66 return array_keys( $this->get_wp_tables() );
67 }
68
69 private function get_wp_tables() {
70 return $this->resetter->get_wp_tables();
71 }
72
73 private function reactivate_data( $string = '' ) {
74 if ( is_null( $string ) ) {
75 $string = 'true';
76 }
77
78 $this->reactivate = ( $string ) ? 'true' : 'false';
79
80 $this->resetter->set_reactivate( $this->reactivate );
81 }
82
83 private function reset( array $tables ) {
84 foreach ( $tables as $key => $value ) {
85 WP_CLI::success( $value );
86 }
87
88 $this->resetter->reset( $tables );
89 $this->output_successful_notice();
90 }
91
92 private function output_successful_notice() {
93 WP_CLI::line( __( 'The selected tables were reset', 'wordpress-database-reset' ) );
94
95 if ( 'true' === $this->reactivate ) {
96 WP_CLI::line( __( 'The current theme and plugins were reactivated','wordpress-database-reset' ) );
97 }
98 }
99
100 private function handle_after_reset() {
101 error_reporting( $this->reporting ); //phpcs:ignore
102 }
103 }
104
105 WP_CLI::add_command( 'reset', 'DB_Reset_Command' );
106