I would like to create a WP-CLI command for WordPress Database Reset plugin.
In the main function it uses $_POST for the options but when running the command in cli they will not exist.
How should go about creating the cli command?
I have seen the commands cookbook.
function wp_reset_init() {
global $wpdb, $current_user, $pagenow;
// Grab the WordPress database tables
$this->_wp_tables = $wpdb->tables();
// Check for valid input - goes ahead and drops / resets tables
if ( isset($_POST['wp-random-value'], $_POST['wp-reset-input']) && $_POST['wp-random-value'] == $_POST['wp-reset-input']
&& check_admin_referer('wp-nonce-submit', $this->_nonce) ) {
require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
// No tables were selected
if ( ! isset($_POST['tables']) && empty($_POST['tables']) ) {
wp_redirect( admin_url( $pagenow ) . '?page=wp-reset&reset=no-select' ); exit();
}
// Get current options
$blog_title = get_option('blogname');
$public = get_option('blog_public');
$admin_user = get_user_by('login', 'admin');
$user = ( ! $admin_user || ! user_can($admin_user->ID, 'update_core') ) ? $current_user : $admin_user;
// Get the selected tables
$tables = (isset($_POST['tables'])) ? array_flip($_POST['tables']) : array();
// Compare the selected tables against the ones in the database
$this->_tables = array_diff_key($this->_wp_tables, $tables);
// Preserve the data from the tables that are unique
if ( 0 < count($this->_tables) ) {
$backup_tables = $this->_backup_tables($this->_tables);
}
// Grab the currently active plugins and theme
if ( isset($_POST['wp-reset-check']) && 'true' == $_POST['wp-reset-check'] ) {
$current_data['active-plugins'] = get_option( 'active_plugins' );
$current_data['current-theme'] = get_option( 'current_theme' );
$current_data['template'] = get_option( 'template' );
$current_data['stylesheet'] = get_option( 'stylesheet' );
}
// Run through the database columns, drop all the tables and
// install wp with previous settings
if ( $db_tables = $wpdb->get_col("SHOW TABLES LIKE '{$wpdb->prefix}%'") ) {
foreach ($db_tables as $db_table) {
$wpdb->query("DROP TABLE {$db_table}");
}
$keys = wp_install($blog_title, $user->user_login, $user->user_email, $public);
$this->_wp_update_user($user, $keys);
}
// Delete and replace tables with the backed up table data
if ( ! empty( $backup_tables ) ) {
foreach ($this->_tables as $table) {
$wpdb->query("DELETE FROM " . $table);
}
$this->_backup_tables($backup_tables, 'reset');
}
if ( ! empty( $current_data ) ) {
update_option( 'active_plugins', $current_data['active-plugins'] );
if ( ! empty( $current_data['current-theme'] ) ) {
update_option( 'current_theme', $current_data['current-theme'] );
}
update_option( 'template', $current_data['template'] );
update_option( 'stylesheet', $current_data['stylesheet'] );
wp_redirect( admin_url($pagenow) . '?page=wp-reset&reset=success' ); exit();
}
wp_redirect( admin_url() ); exit();
}
}