Extracts constants necessary for all environments (mostly constants changing directory layout) into tracked file.
Esempio n. 1
0
/**
 * Activates the plugin from a WordPress point of view. Note that for VersionPress
 * to become fully active, the Initializer needs to be run.
 *
 * @see Initializer
 */
function vp_activate()
{
    WpConfigSplitter::split(WordPressMissingFunctions::getWpConfigPath());
    set_transient('vp_just_activated', '1', 10);
}
Esempio n. 2
0
 /**
  * Restores a WP site from Git repo / working directory.
  *
  * ## OPTIONS
  *
  * --siteurl=<url>
  * : The address of the restored site.
  *
  * [--yes]
  * : Answer yes to the confirmation message.
  *
  * ## DESCRIPTION
  *
  * The command will then do the following:
  *
  *    * Drops all tables tracked by VersionPress.
  *    * Recreates and fill them with data from repository.
  *
  * If you just cloned the site from another repository, run `wp core config` first.
  *
  *
  * @subcommand restore-site
  *
  * @when before_wp_load
  */
 public function restoreSite($args, $assoc_args)
 {
     if (file_exists(getcwd() . '/composer.json')) {
         $proc = proc_open("composer install", [1 => ["pipe", "w"], ["pipe", "w"]], $_);
         $result = proc_close($proc);
         if ($result !== 0) {
             WP_CLI::error('Composer dependencies could not be restored.');
         }
     }
     defined('SHORTINIT') or define('SHORTINIT', true);
     require_once __DIR__ . '/../Initialization/WpConfigSplitter.php';
     $wpConfigPath = \WP_CLI\Utils\locate_wp_config();
     $this->requireWpConfig($wpConfigPath, WpConfigSplitter::COMMON_CONFIG_NAME);
     require_once __DIR__ . '/../../bootstrap.php';
     if (!VersionPress::isActive()) {
         WP_CLI::error('Unfortunately, this site was not tracked by VersionPress.
         Therefore, it cannot be restored.');
     }
     // Check if the site is installed
     $process = VPCommandUtils::runWpCliCommand('core', 'is-installed');
     if ($process->isSuccessful()) {
         $this->checkVpRequirements($assoc_args, RequirementsChecker::ENVIRONMENT);
         WP_CLI::confirm("It looks like the site is OK. Do you really want to run the 'restore-site' command?", $assoc_args);
     }
     $url = $assoc_args['siteurl'];
     // Update URLs in wp-config.php
     define('VP_INDEX_DIR', dirname(\WP_CLI\Utils\locate_wp_config()));
     // just for the following method
     $this->setConfigUrl('WP_CONTENT_URL', 'WP_CONTENT_DIR', ABSPATH . 'wp-content', $url);
     $this->setConfigUrl('WP_PLUGIN_URL', 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins', $url);
     $this->setConfigUrl('WP_HOME', 'VP_INDEX_DIR', VP_PROJECT_ROOT, $url);
     defined('WP_PLUGIN_DIR') || define('WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins');
     WpConfigSplitter::ensureCommonConfigInclude($wpConfigPath);
     // Disable VersionPress tracking for a while
     WpdbReplacer::restoreOriginal();
     unlink(VERSIONPRESS_ACTIVATION_FILE);
     // Create or empty database
     $this->prepareDatabase($assoc_args);
     // Create WP tables.
     // The only important thing is site URL, all else will be rewritten later during synchronization.
     $installArgs = ['url' => $url, 'title' => 'x', 'admin_user' => 'x', 'admin_password' => 'x', 'admin_email' => '*****@*****.**'];
     if (version_compare(WP_CLI_VERSION, '0.22.0', '>=')) {
         $installArgs['skip-email'] = null;
     }
     $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" - enables VP
     $resetCmd = 'git reset --hard';
     $process = VPCommandUtils::exec($resetCmd);
     if (!$process->isSuccessful()) {
         WP_CLI::log("Could not clean working directory");
         WP_CLI::error($process->getConsoleOutput());
     }
     // Fail-safe for gitignored WordPress
     if (!WpdbReplacer::isReplaced()) {
         WpdbReplacer::replaceMethods();
     }
     /* We need correct value in the `active_plugins` option before the synchronization run.
      * Without this option VersionPress doesn't know which schema.yml files it should load and consequently which
      * DB entities it should synchronize.
      */
     $activePluginsOption = IniSerializer::deserialize(file_get_contents(VP_VPDB_DIR . '/options/ac/active_plugins.ini'));
     $activePlugins = json_encode(unserialize($activePluginsOption['active_plugins']['option_value']));
     VPCommandUtils::runWpCliCommand('option', 'update', ['active_plugins', $activePlugins, 'autoload' => 'yes', 'format' => 'json', 'skip-plugins' => null]);
     // The next couple of the steps need to be done after WP is fully loaded; we use `finish-restore-site` for that
     // The main reason for this is that we need properly set WP_CONTENT_DIR constant for reading from storages
     $process = $this->runVPInternalCommand('finish-restore-site');
     WP_CLI::log($process->getConsoleOutput());
     if (!$process->isSuccessful()) {
         WP_CLI::error("Could not finish site restore");
     }
 }
Esempio n. 3
0
 private function createCommonConfig()
 {
     $configPath = WordPressMissingFunctions::getWpConfigPath();
     WpConfigSplitter::split($configPath);
 }
 /**
  * @test
  */
 public function runningSplitterMultipleTimesDoesntChangeTheOutput()
 {
     WpConfigSplitter::split($this->wpConfigPath, $this->commonConfigName);
     WpConfigSplitter::split($this->wpConfigPath, $this->commonConfigName);
     WpConfigSplitter::split($this->wpConfigPath, $this->commonConfigName);
     $filteredConfig = file_get_contents($this->wpConfigPath);
     $commonConfig = file_get_contents($this->commonConfigPath);
     $this->assertEquals($this->filteredConfig, $filteredConfig);
     $this->assertEquals($this->commonConfig, $commonConfig);
 }