private function prepareBugReport($bugReportDir, $zipFile) { FileSystem::mkdir($bugReportDir); FileSystem::copyDir(VERSIONPRESS_PLUGIN_DIR . '/log', $bugReportDir . '/log'); $this->savePhpinfo($bugReportDir); $this->saveWordPressSpecificInfo($bugReportDir); Zip::zipDirectory($bugReportDir, $zipFile); }
/** * Clones site to a new folder and database. * * ## OPTIONS * * --name=<name> * : Name of the clone. Used as a directory name, part of the DB prefix * and an argument to the pull & push commands later. * * --siteurl=<url> * : URL of the clone. By default, the original URL is searched for <cwd> * and replaced with the clone name. * * --dbname=<dbname> * : Database name for the clone. * * --dbuser=<dbuser> * : Database user for the clone. * * --dbpass=<dbpass> * : Database user password for the clone. * * --dbhost=<dbhost> * : Database host for the clone. * * --dbprefix=<dbprefix> * : Database table prefix for the clone. * * --dbcharset=<dbcharset> * : Database charset for the clone. * * --dbcollate=<dbcollate> * : Database collation for the clone. * * --force * : Forces cloning even if the target directory or DB tables exists. * Basically provides --yes to all warnings / confirmations. * * --yes * : Another way to force the clone * * ## EXAMPLES * * The main site lives in a directory 'wpsite', uses the 'wp_' database table prefix and is * accessible via 'http://localhost/wpsite'. The command * * wp vp clone --name=myclone * * does the following: * * - Creates new directory 'myclone' next to the current one * - Clones the files there * - Creates new database tables prefixed with 'wp_myclone_' * - Populates database tables with data * - Makes the site accessible as 'http://localhost/myclone' * * @synopsis --name=<name> [--siteurl=<url>] [--dbname=<dbname>] [--dbuser=<dbuser>] [--dbpass=<dbpass>] [--dbhost=<dbhost>] [--dbprefix=<dbprefix>] [--dbcharset=<dbcharset>] [--dbcollate=<dbcollate>] [--force] [--yes] * * @subcommand clone */ public function clone_($args = array(), $assoc_args = array()) { global $table_prefix; if (isset($assoc_args['force'])) { $assoc_args['yes'] = 1; } $name = $assoc_args['name']; if (preg_match('/[^a-zA-Z0-9-_]/', $name)) { WP_CLI::error("Clone name '{$name}' is not valid. It can only contain letters, numbers, hyphens and underscores."); } $currentWpPath = get_home_path(); $cloneDirName = $name; $clonePath = dirname($currentWpPath) . '/' . $cloneDirName; $cloneDbUser = isset($assoc_args['dbuser']) ? $assoc_args['dbuser'] : DB_USER; $cloneDbPassword = isset($assoc_args['dbpass']) ? $assoc_args['dbpass'] : DB_PASSWORD; $cloneDbName = isset($assoc_args['dbname']) ? $assoc_args['dbname'] : DB_NAME; $cloneDbHost = isset($assoc_args['dbhost']) ? $assoc_args['dbhost'] : DB_HOST; $cloneDbPrefix = isset($assoc_args['dbprefix']) ? $assoc_args['dbprefix'] : $table_prefix . $name . '_'; $cloneDbCharset = isset($assoc_args['dbcharset']) ? $assoc_args['dbcharset'] : DB_CHARSET; $cloneDbCollate = isset($assoc_args['dbcollate']) ? $assoc_args['dbcollate'] : DB_COLLATE; // Checking the DB prefix, regex from wp-admin/setup-config.php if (isset($assoc_args['dbprefix']) && preg_match('|[^a-z0-9_]|i', $cloneDbPrefix)) { WP_CLI::error("Table prefix '{$cloneDbPrefix}' is not valid. It can only contain letters, numbers and underscores. Please choose different one."); } $prefixChanged = false; if (Strings::contains($cloneDbPrefix, '-')) { $cloneDbPrefix = str_replace('-', '_', $cloneDbPrefix); $prefixChanged = true; } $currentUrl = get_site_url(); $suggestedUrl = $this->suggestCloneUrl($currentUrl, basename($currentWpPath), $cloneDirName); if (!$suggestedUrl && !isset($assoc_args['siteurl'])) { WP_CLI::error("The command cannot derive default clone URL. Please specify the --siteurl parameter."); } $cloneUrl = isset($assoc_args['siteurl']) ? $assoc_args['siteurl'] : $suggestedUrl; $urlChanged = !isset($assoc_args['siteurl']) && !Strings::contains($cloneUrl, $cloneDirName); if (is_dir($clonePath)) { WP_CLI::confirm("Directory '" . basename($clonePath) . "' already exists, it will be deleted before cloning. Proceed?", $assoc_args); } if ($this->someWpTablesExist($cloneDbUser, $cloneDbPassword, $cloneDbName, $cloneDbHost, $cloneDbPrefix)) { WP_CLI::confirm("Database tables for the clone already exist, they will be dropped and re-created. Proceed?", $assoc_args); } if (is_dir($clonePath)) { try { FileSystem::removeContent($clonePath); } catch (IOException $e) { WP_CLI::error("Could not delete directory '" . basename($clonePath) . "'. Please do it manually."); } } // Clone the site $cloneCommand = sprintf("git clone %s %s", escapeshellarg($currentWpPath), escapeshellarg($clonePath)); $process = VPCommandUtils::exec($cloneCommand, $currentWpPath); if (!$process->isSuccessful()) { WP_CLI::error($process->getConsoleOutput(), false); WP_CLI::error("Cloning Git repo failed"); } else { WP_CLI::success("Site files cloned"); } // Adding the clone as a remote for the convenience of the `vp pull` command - its `--from` // parameter can then be just the name of the clone, not a path to it $addRemoteCommand = sprintf("git remote add %s %s", escapeshellarg($name), escapeshellarg($clonePath)); $process = VPCommandUtils::exec($addRemoteCommand, $currentWpPath); if (!$process->isSuccessful()) { $overwriteRemote = VPCommandUtils::cliQuestion("The Git repo of this site already defines remote '{$name}', overwrite it?", array("y", "n"), $assoc_args); if ($overwriteRemote == "y") { $addRemoteCommand = str_replace(" add ", " set-url ", $addRemoteCommand); $process = VPCommandUtils::exec($addRemoteCommand, $currentWpPath); if (!$process->isSuccessful()) { WP_CLI::error("Could not update remote's URL"); } else { WP_CLI::success("Updated remote configuration"); } } } else { WP_CLI::success("Clone added as a remote"); } // Enable pushing to origin $configCommand = "git config receive.denyCurrentBranch ignore"; $process = VPCommandUtils::exec($configCommand); if ($process->isSuccessful()) { WP_CLI::success("Enabled pushing to the original repository"); } else { WP_CLI::error("Cannot enable pushing to the original repository"); } // Enable pushing to clone $configCommand = "git config receive.denyCurrentBranch ignore"; $process = VPCommandUtils::exec($configCommand, $clonePath); if ($process->isSuccessful()) { WP_CLI::success("Enabled pushing to the clone"); } else { WP_CLI::error("Cannot enable pushing to the clone"); } // Copy & Update wp-config $wpConfigFile = $clonePath . '/wp-config.php'; copy($currentWpPath . '/wp-config.php', $wpConfigFile); $this->updateConfig($wpConfigFile, $cloneDbUser, $cloneDbPassword, $cloneDbName, $cloneDbHost, $cloneDbPrefix, $cloneDbCharset, $cloneDbCollate); // Copy VersionPress FileSystem::copyDir(VERSIONPRESS_PLUGIN_DIR, $clonePath . '/wp-content/plugins/versionpress'); WP_CLI::success("Copied VersionPress"); // Finish the process by doing the standard restore-site $process = VPCommandUtils::runWpCliCommand('vp', 'restore-site', array('siteurl' => $cloneUrl, 'yes' => null, 'require' => __FILE__), $clonePath); WP_CLI::log(trim($process->getConsoleOutput())); if ($process->isSuccessful()) { WP_CLI::success("All done. Clone created here:"); WP_CLI::log(""); WP_CLI::log("Path: {$clonePath}"); WP_CLI::log("URL: {$cloneUrl}"); if ($urlChanged) { WP_CLI::log("Note: Underscores changed to hyphens for URL."); } if ($prefixChanged) { WP_CLI::log("Note: Hyphens changed to underscores for DB prefix."); } } }
/** * Puts WP directory to a default state, as if one manually downloaded the WordPress ZIP * and extracted it there. Removes all old files if necessary. */ private function prepareStandardWpInstallation() { $this->ensureCleanInstallationIsAvailable(); FileSystem::copyDir($this->getCleanInstallationPath(), $this->siteConfig->path); $this->createConfigFile(); }