| 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->resetter->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 |
$this->reporting = error_reporting(); |
| 57 |
error_reporting( 0 ); |
| 58 |
} |
| 59 |
|
| 60 |
private function sanitize_input( $string = '' ) { |
| 61 |
if ( ! empty ( $string ) ) { |
| 62 |
return explode( ',', preg_replace( '/\s+/', '', $string ) ); |
| 63 |
} |
| 64 |
|
| 65 |
return array_keys( $this->get_wp_tables() ); |
| 66 |
} |
| 67 |
|
| 68 |
private function get_wp_tables() { |
| 69 |
return $this->resetter->get_wp_tables(); |
| 70 |
} |
| 71 |
|
| 72 |
private function reactivate_data( $string = '' ) { |
| 73 |
if ( is_null( $string ) ) { |
| 74 |
$string = 'true'; |
| 75 |
} |
| 76 |
|
| 77 |
$this->reactivate = ( $string ) ? 'true' : 'false'; |
| 78 |
|
| 79 |
$this->resetter->set_reactivate( $this->reactivate ); |
| 80 |
} |
| 81 |
|
| 82 |
private function reset( array $tables ) { |
| 83 |
foreach ( $tables as $key => $value ) { |
| 84 |
WP_CLI::success( $value ); |
| 85 |
} |
| 86 |
|
| 87 |
$this->resetter->reset( $tables ); |
| 88 |
$this->output_successful_notice(); |
| 89 |
} |
| 90 |
|
| 91 |
private function output_successful_notice() { |
| 92 |
WP_CLI::line( __( 'The selected tables were reset', 'wordpress-database-reset' ) ); |
| 93 |
|
| 94 |
if ( 'true' === $this->reactivate ) { |
| 95 |
WP_CLI::line( __( 'The current theme and plugins were reactivated','wordpress-database-reset' ) ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
private function handle_after_reset() { |
| 100 |
error_reporting( $this->reporting ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
WP_CLI::add_command( 'reset', 'DB_Reset_Command' ); |
| 105 |
|