/**
* Restores a WP site from Git repo / working directory.
*
* ## OPTIONS
*
* --siteurl=<url>
* : The address of the restored site. Default: 'http://localhost/<cwd>'
*
* --dbname=<dbname>
* : Set the database name.
*
* --dbuser=<dbuser>
* : Set the database user.
*
* --dbpass=<dbpass>
* : Set the database user password.
*
* --dbhost=<dbhost>
* : Set the database host. Default: 'localhost'
*
* --dbprefix=<dbprefix>
* : Set the database table prefix. Default: 'wp_'
*
* --dbcharset=<dbcharset>
* : Set the database charset. Default: 'utf8'
*
* --dbcollate=<dbcollate>
* : Set the database collation. Default: ''
*
* --yes
* : Answer yes to the confirmation message.
*
* ## DESCRIPTION
*
* The simplest possible example just executes `site-restore` without any parameters.
* The assumptions are:
*
* * The current directory must be reachable from the webserver as http://localhost/<cwd>
* * Credentials for the MySQL server are in the wp-config.php
*
* The command will then do the following:
*
* * Create a db <dirname>, e.g., 'vp01'
* * Optionally configure WordPress to connect to this DB
* * Create WordPress tables in it and preconfigure it with site_url and home options
* * Run VP synchronizers on the database
*
* All DB credentials and site URL are configurable.
*
* @synopsis [--siteurl=<url>] [--dbname=<dbname>] [--dbuser=<dbuser>] [--dbpass=<dbpass>] [--dbhost=<dbhost>] [--dbprefix=<dbprefix>] [--dbcharset=<dbcharset>] [--dbcollate=<dbcollate>] [--yes]
*
* @subcommand restore-site
*
* @when before_wp_load
*/
public function restoreSite($args, $assoc_args)
{
// Load VersionPress' bootstrap (WP_CONTENT_DIR needs to be defined)
if (!defined('WP_CONTENT_DIR')) {
define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
}
require_once __DIR__ . '/../../bootstrap.php';
// Check if the site is installed
$process = VPCommandUtils::runWpCliCommand('core', 'is-installed');
if ($process->isSuccessful()) {
WP_CLI::confirm("It looks like the site is OK. Do you really want to run the 'restore-site' command?", $assoc_args);
$defaultUrl = trim(VPCommandUtils::runWpCliCommand('option', 'get', array('siteurl'))->getConsoleOutput());
} else {
$defaultUrl = 'http://localhost/' . basename(getcwd());
}
$url = @$assoc_args['siteurl'] ?: $defaultUrl;
// Confirm automatically chosen site URL
if (!isset($assoc_args['siteurl'])) {
WP_CLI::confirm("The site URL will be set to '{$url}'. Proceed?", $assoc_args);
}
// Updating wp-config.php
if (file_exists(ABSPATH . 'wp-config.php')) {
if ($this->issetConfigOption($assoc_args)) {
WP_CLI::error("Site settings was loaded from wp-config.php. If you want to reconfigure the site, please delete the wp-config.php file");
}
} else {
$this->configSite($assoc_args);
}
// Create or empty database
$this->prepareDatabase($assoc_args);
// Disable VersionPress tracking
if (!defined('WPINC')) {
define('WPINC', 'wp-includes');
}
WpdbReplacer::restoreOriginal();
unlink(VERSIONPRESS_ACTIVATION_FILE);
// Create WP tables. The only important thing is site URL, all else will be rewritten later during synchronization.
$installArgs = array('url' => $url, 'title' => 'x', 'admin_user' => 'x', 'admin_password' => 'x', 'admin_email' => '*****@*****.**');
$process = VPCommandUtils::runWpCliCommand('core', 'install', $installArgs);
if (!$process->isSuccessful()) {
WP_CLI::log("Failed creating database tables");
WP_CLI::error($process->getConsoleOutput());
} else {
WP_CLI::success("Database tables created");
}
// Restores "wp-db.php", "wp-db.php.original" and ".active"
$resetCmd = 'git reset --hard';
$process = VPCommandUtils::exec($resetCmd);
if (!$process->isSuccessful()) {
WP_CLI::log("Could not clean working directory");
WP_CLI::error($process->getConsoleOutput());
}
// The next couple of the steps need to be done after the WP is fully loaded; we use `finish-init-clone` for that
// The main reason for this is that we need properly set WP_CONTENT_DIR constant for reading from storages
$process = VPCommandUtils::runWpCliCommand('vp-internal', 'finish-init-clone', array('require' => $this->getVPInternalCommandPath()));
WP_CLI::log($process->getConsoleOutput());
if (!$process->isSuccessful()) {
WP_CLI::error("Could not finish site restore");
}
}