/**
* Finishes clone operation
*
* @subcommand finish-restore-site
*
* @when before_wp_load
*
*/
public function finishRestore($args, $assoc_args)
{
define('SHORTINIT', true);
$wpConfigPath = \WP_CLI\Utils\locate_wp_config();
require_once $wpConfigPath;
require ABSPATH . WPINC . '/formatting.php';
require ABSPATH . WPINC . '/link-template.php';
require ABSPATH . WPINC . '/shortcodes.php';
require ABSPATH . WPINC . '/taxonomy.php';
wp_plugin_directory_constants();
require_once WP_PLUGIN_DIR . '/versionpress/bootstrap.php';
$versionPressContainer = DIContainer::getConfiguredInstance();
/** @var ActionsDefinitionRepository $actionsDefinitionRepository */
$actionsDefinitionRepository = $versionPressContainer->resolve(VersionPressServices::ACTIONS_DEFINITION_REPOSITORY);
$actionsDefinitionRepository->restoreAllDefinitionFilesFromHistory();
// Truncate tables
/** @var Database $database */
$database = $versionPressContainer->resolve(VersionPressServices::DATABASE);
/** @var DbSchemaInfo $dbSchema */
$dbSchema = $versionPressContainer->resolve(VersionPressServices::DB_SCHEMA);
$tables = array_map(function ($entityName) use($dbSchema) {
return $dbSchema->getPrefixedTableName($entityName);
}, array_merge($dbSchema->getAllEntityNames(), array_map(function ($referenceDetails) {
return $referenceDetails['junction-table'];
}, $dbSchema->getAllMnReferences())));
$tables = array_filter($tables, function ($table) use($database) {
return $table !== $database->options;
});
foreach ($tables as $table) {
$truncateCmd = "TRUNCATE TABLE `{$table}`";
@$database->query($truncateCmd);
// Intentional @ - not existing table is ok for us but TRUNCATE ends with error
}
// Create VersionPress tables
/** @var \VersionPress\Initialization\Initializer $initializer */
$initializer = $versionPressContainer->resolve(VersionPressServices::INITIALIZER);
$initializer->createVersionPressTables();
WP_CLI::success("VersionPress tables created");
// Install Custom merge driver
MergeDriverInstaller::installMergeDriver(VP_PROJECT_ROOT, VERSIONPRESS_PLUGIN_DIR, VP_VPDB_DIR);
WP_CLI::success("Git merge driver added");
// Run synchronization
/** @var SynchronizationProcess $syncProcess */
$syncProcess = $versionPressContainer->resolve(VersionPressServices::SYNCHRONIZATION_PROCESS);
$syncProcess->synchronizeAll();
WP_CLI::success("Database synchronized");
VPCommandUtils::runWpCliCommand('vp-internal', 'flush-regenerable-values', ['require' => __FILE__]);
}