launch() public static method

# wp core download falls back to the tar binary when PharData isn't available if ( ! class_exists( 'PharData' ) ) { $cmd = "tar xz --strip-components=1 --directory=%s -f $tarball"; WP_CLI::launch( Utils\esc_cmd( $cmd, $dest ) ); return; }
public static launch ( string $command, boolean $exit_on_error = true, boolean $return_detailed = false ) : integer | ProcessRun
$command string External process to launch.
$exit_on_error boolean Whether to exit if the command returns an elevated return code.
$return_detailed boolean Whether to return an exit status (default) or detailed execution results.
return integer | ProcessRun The command exit status, or a ProcessRun object for full details.
Example #1
0
 /**
  * Download the core files from wordpress.org
  */
 public function download($args, $assoc_args)
 {
     if (is_readable(WP_ROOT . 'wp-load.php')) {
         WP_CLI::error('WordPress files seem to already be present here.');
     }
     if (isset($assoc_args['path'])) {
         $docroot = $assoc_args['path'];
     } else {
         $docroot = './';
     }
     if (isset($assoc_args['locale'])) {
         exec('curl -s ' . escapeshellarg('https://api.wordpress.org/core/version-check/1.5/?locale=' . $assoc_args['locale']), $lines, $r);
         if ($r) {
             exit($r);
         }
         $download_url = str_replace('.zip', '.tar.gz', $lines[2]);
         WP_CLI::line(sprintf('Downloading WordPress %s (%s)...', $lines[3], $lines[4]));
     } elseif (isset($assoc_args['version'])) {
         $download_url = 'https://wordpress.org/wordpress-' . $assoc_args['version'] . '.tar.gz';
         WP_CLI::line(sprintf('Downloading WordPress %s (%s)...', $assoc_args['version'], 'en_US'));
     } else {
         $download_url = 'https://wordpress.org/latest.tar.gz';
         WP_CLI::line(sprintf('Downloading latest WordPress (%s)...', 'en_US'));
     }
     WP_CLI::launch('curl -f' . (WP_CLI_QUIET ? ' --silent ' : ' ') . escapeshellarg($download_url) . ' | tar xz');
     WP_CLI::launch('mv wordpress/* . && rm -rf wordpress');
     WP_CLI::success('WordPress downloaded.');
 }
Example #2
0
 private static function maybe_load_man_page($args)
 {
     $man_file = \WP_CLI\Man\get_path($args);
     if (is_readable($man_file)) {
         exit(WP_CLI::launch("man {$man_file}"));
     }
 }
 public function uploads($command = array(), $args = array())
 {
     if (count($command) == 1 && reset($command) == 'help') {
         return $this->uploads_help();
     }
     $dir = wp_upload_dir();
     $defaults = array('ssh_host' => defined('IMPORT_UPLOADS_SSH_HOST') ? IMPORT_UPLOADS_SSH_HOST : '', 'ssh_user' => defined('IMPORT_UPLOADS_SSH_USER') ? IMPORT_UPLOADS_SSH_USER : '', 'remote_path' => defined('IMPORT_UPLOADS_REMOTE_PATH') ? IMPORT_UPLOADS_REMOTE_PATH : '', 'uploads_dir' => '', 'local_path' => $dir['basedir']);
     $args = wp_parse_args($args, $defaults);
     if ($args['uploads_dir']) {
         $args['remote_path'] = $args['remote_path'] ? trailingslashit($args['remote_path']) . trailingslashit(ltrim($args['uploads_dir'], '/')) : '';
         $args['local_path'] = trailingslashit($args['local_path']) . untrailingslashit(ltrim($args['uploads_dir'], '/'));
     } else {
         $args['remote_path'] = $args['remote_path'] ? trailingslashit($args['remote_path']) : '';
         $args['local_path'] = untrailingslashit($args['local_path']);
     }
     if (empty($args['remote_path'])) {
         WP_CLI::error('You must specify a remote path. Use --remote_path=~/foo/bar');
         return;
     }
     if (empty($args['ssh_host'])) {
         WP_CLI::error('You must specify a ssh host. Use --ssh_host=example.com');
         return;
     }
     if (empty($args['ssh_user'])) {
         WP_CLI::error('You must specify a ssh user. Use --ssh_user=root');
         return;
     }
     WP_CLI::line(sprintf('Running rsync from %s:%s to %s', $args['ssh_host'], $args['remote_path'], $args['local_path']));
     WP_CLI::launch(sprintf("rsync -avz -e ssh %s@%s:%s %s --exclude 'cache' --exclude '*backup*'", $args['ssh_user'], $args['ssh_host'], $args['remote_path'], $args['local_path']));
 }
Example #4
0
 private static function maybe_load_man_page($args)
 {
     $man_file = \WP_CLI\Man\get_file_name($args);
     foreach (\WP_CLI::get_man_dirs() as $dest_dir => $_) {
         $man_path = $dest_dir . $man_file;
         if (is_readable($man_path)) {
             exit(WP_CLI::launch("man {$man_path}"));
         }
     }
 }
 /**
  * Run PHP Documentor
  *
  * ## OPTIONS
  *
  * <folder>
  * : Folder to generate
  *
  * ## EXAMPLES
  *
  * wp phpdoc run folder/
  *
  * @synopsis <folder>
  *
  * @since 0.1.0
  */
 public function run($args = null, $assoc_args = null)
 {
     #print_r($assoc_args);
     if (null === $args[0]) {
         WP_CLI::error('Usage: wp phpdoc run <folder>');
     } else {
         $cmd = 'phpdoc.php list';
         WP_CLI::launch($cmd);
     }
 }
 private function _do_plugin($slug, $tests)
 {
     WP_CLI::line('Doing Plugin ' . $slug);
     $plugin_dir = WP_PLUGIN_DIR . '/' . $slug;
     if (is_dir($plugin_dir)) {
         WP_CLI::launch('export WP_TESTS_DIR=' . $tests . '; cd ' . $plugin_dir . '; phpunit -c phpunit.xml ');
     } else {
         WP_CLI::error('Can\'t find plugin folder: ' . $plugin_dir);
     }
 }
 /**
  * Execute WP-CLI against configuration for a given environment
  *
  * <core>
  *
  * <download>
  *
  *
  * @when before_wp_load
  */
 public function __invoke($args, $assoc_args)
 {
     global $argv;
     try {
         $environment = new \ViewOne\WPCLIEnvironment\Environment();
         $environment->run($argv[1]);
     } catch (Exception $e) {
         \WP_CLI::error($e->getMessage());
     }
     $command = \ViewOne\WPCLIEnvironment\Command::getCommand($args, $assoc_args);
     WP_CLI::launch($command);
 }
 private function _run_phpdcd($slug, $flags)
 {
     $plugin_path = WP_PLUGIN_DIR . '/' . $slug;
     $theme_path = WP_CONTENT_DIR . '/themes/' . $slug;
     if (is_dir($theme_path) && false === is_dir($plugin_path)) {
         WP_CLI::launch('phpdcd ' . $flags . $theme_path);
     } elseif (is_dir($plugin_path) && false === is_dir($theme_path)) {
         WP_CLI::launch('phpdcd ' . $flags . $plugin_path);
     } else {
         WP_CLI::error('Plugin/theme not found');
     }
 }
 /**
  * Verify plugin or theme path, run phpcs when found
  *
  * @since 0.2.1
  */
 private function _run_phpcs($slug, $flags)
 {
     $plugin_path = WP_PLUGIN_DIR . '/' . $slug;
     $theme_path = WP_CONTENT_DIR . '/themes/' . $slug;
     if (is_dir($theme_path) && false === is_dir($plugin_path)) {
         WP_CLI::launch('phpcs ' . $flags . $theme_path);
     } elseif (is_dir($plugin_path) && false === is_dir($theme_path)) {
         WP_CLI::launch('phpcs ' . $flags . $plugin_path);
     } else {
         WP_CLI::error('No theme or plugin with that slug.');
     }
 }
Example #10
0
 private static function maybe_load_man_page($args)
 {
     $man_dir = WP_CLI_ROOT . "../../../man/";
     if (!is_dir($man_dir)) {
         WP_CLI::warning("man pages do not seem to be installed.");
     } else {
         $man_file = $man_dir . implode('-', $args) . '.1';
         if (is_readable($man_file)) {
             exit(WP_CLI::launch("man {$man_file}"));
         }
     }
 }
Example #11
0
 private static function _extract($tarball, $dest)
 {
     if (!class_exists('PharData')) {
         $cmd = "tar xz --strip-components=1 --directory=%s -f {$tarball}";
         WP_CLI::launch(Utils\esc_cmd($cmd, $dest));
         return;
     }
     $phar = new PharData($tarball);
     $tempdir = implode(DIRECTORY_SEPARATOR, array(dirname($tarball), basename($tarball, '.tar.gz'), $phar->getFileName()));
     $phar->extractTo(dirname($tempdir), null, true);
     self::_copy_overwrite_files($tempdir, $dest);
     self::_rmdir(dirname($tempdir));
 }
Example #12
0
 protected function _execute_commands($commands)
 {
     if ($this->flags['dry-run']) {
         WP_CLI::line('DRY RUN....');
     }
     foreach ($commands as $command_info) {
         list($command, $exit_on_error) = $command_info;
         WP_CLI::line($command);
         if (!$this->flags['dry-run']) {
             WP_CLI::launch($command, $exit_on_error);
         }
     }
 }
Example #13
0
 private function test_for_phpunit()
 {
     if (!@(require_once 'PHPUnit/Autoload.php')) {
         WP_CLI::line('%RPHPUnit not found%n, you need to install PHPUnit to use the test command, see https://github.com/humanmade/hm-dev');
         WP_CLI::line('Attempting to auto install PHPUnit...');
         WP_CLI::launch('pear config-set auto_discover 1');
         WP_CLI::launch('sudo pear install pear.phpunit.de/PHPUnit');
         if (file_exists(trailingslashit(substr(get_include_path(), 2)) . 'PHPUnit/Autoload.php')) {
             WP_CLI::line('%GPHPUnit was auto installed%n, You\'ll need to run the command again.');
         }
         return false;
     }
     return true;
 }
 private function _do_commands($slug, $path)
 {
     WP_CLI::success('PHP Copy/Paste Detector: ' . $args[0]);
     WP_CLI::launch('wp phpcpd ' . $slug);
     WP_CLI::success('PHP CodeSniffer: ' . $args[0]);
     WP_CLI::launch('wp phpcs ' . $slug);
     WP_CLI::success('PHP Dead Code Detector: ' . $args[0]);
     WP_CLI::launch('wp phpdcd ' . $slug);
     WP_CLI::success('PHP Lines of Code: ' . $args[0]);
     WP_CLI::launch('wp phploc ' . $slug);
     WP_CLI::success('PHP Mess Detector: ' . $args[0]);
     WP_CLI::launch('wp phpmd ' . $slug);
     WP_CLI::success('PHPunit: ' . $args[0]);
     WP_CLI::launch('wp phpunit --plugin=' . $slug);
 }
Example #15
0
 /**
  * Generate connections for a specific connection type.
  *
  * @subcommand generate-connections
  * @synopsis <connection-type> [--items]
  */
 function generate_connections($args, $assoc_args)
 {
     list($connection_type) = $args;
     $ctype = p2p_type($connection_type);
     if (!$ctype) {
         WP_CLI::error("'{$connection_type}' is not a registered connection type.");
     }
     if (isset($assoc_args['items'])) {
         foreach (_p2p_extract_post_types($ctype->side) as $ptype) {
             $assoc_args = array('post_type' => $ptype);
             WP_CLI::launch('wp post generate' . \WP_CLI\Utils\assoc_args_to_str($assoc_args));
         }
     }
     $count = $this->_generate_c($ctype);
     WP_CLI::success("Created {$count} connections.");
 }
Example #16
0
 /**
  * Launch redis-cli using Redis configuration for WordPress
  */
 public function cli()
 {
     global $redis_server;
     if (empty($redis_server)) {
         # Attempt to automatically load Pantheon's Redis config from the env.
         if (isset($_SERVER['CACHE_HOST'])) {
             $redis_server = array('host' => $_SERVER['CACHE_HOST'], 'port' => $_SERVER['CACHE_PORT'], 'auth' => $_SERVER['CACHE_PASSWORD']);
         } else {
             $redis_server = array('host' => '127.0.0.1', 'port' => 6379);
         }
     }
     if (!isset($redis_server['database'])) {
         $redis_server['database'] = 0;
     }
     $cmd = WP_CLI\Utils\esc_cmd('redis-cli -h %s -p %s -a %s -n %d', $redis_server['host'], $redis_server['port'], $redis_server['auth'], $redis_server['database']);
     WP_CLI::launch($cmd);
 }
Example #17
0
 /**
  * @synopsis [--loggly] [--skip-dns] [--cgi-output]
  * @subcommand healthcheck
  *
  */
 function healthcheck($args, $assoc_args)
 {
     global $wpdb;
     $networkstatus = is_multisite() ? "yes" : "no";
     $blog_url = get_bloginfo('url');
     $blog_version = get_bloginfo('version');
     $domain_name = str_replace('http://', '', str_replace('https://', '', untrailingslashit($blog_url)));
     $count = self::synth_get_plugin_count();
     // Memory Footprint
     if (false === ($memusage = get_transient('synthesis_memory_check'))) {
         $memusage = 0;
     }
     // Transients
     $orphan = "SELECT substring(option_name,1,6) as x,count(*) FROM {$wpdb->options} group by x HAVING x = '_trans' order by count(*) desc";
     $result = $wpdb->get_var($orphan, 1, 0);
     // Autoloaded Options
     $autoloaded_count = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->options} WHERE autoload = 'yes';");
     $autoloaded_size = round($wpdb->get_var("SELECT SUM(LENGTH(option_value)) / POWER(1024,2) FROM {$wpdb->options} WHERE autoload = 'yes';"), 2);
     // WordPress Cron
     $domain = explode("/", preg_replace("/^www\\./", "", $domain_name));
     $subsite = str_replace($domain[0], "", preg_replace("/^www\\./", "", $domain_name));
     $wpcron = "-";
     if (file_exists("/var/log/nginx/" . $domain[0] . ".access.log")) {
         $wpcron = sizeof(preg_grep("/" . preg_replace("/\\//", "\\/", $subsite) . "\\/wp-cron\\.php/", file("/var/log/nginx/" . $domain[0] . ".access.log")));
     }
     if (defined("DISABLE_WP_CRON")) {
         if (constant("DISABLE_WP_CRON")) {
             $wpcron .= " (probably running via crontab)";
         }
     }
     // Accesses
     $accesses = array();
     if (file_exists("/var/log/nginx/" . $domain[0] . ".access.log")) {
         $accesses['total'] = sizeof(preg_grep("/-/", file("/var/log/nginx/" . $domain[0] . ".access.log")));
         $accesses['wplogin'] = sizeof(preg_grep("/" . preg_replace("/\\//", "\\/", $subsite) . "\\/wp-login\\.php/", file("/var/log/nginx/" . $domain[0] . ".access.log")));
         $accesses['xmlrpc'] = sizeof(preg_grep("/" . preg_replace("/\\//", "\\/", $subsite) . "\\/xmlrpc\\.php/", file("/var/log/nginx/" . $domain[0] . ".access.log")));
     }
     // Live Site
     $live = null;
     if (!isset($assoc_args['skip-dns'])) {
         WP_CLI::launch("ifconfig | grep inet | cut -d':' -f2 | cut -d' ' -f1 | egrep -v \"^\$|127.0.0.1\" > /tmp/server_ips");
         $file = fopen("/tmp/server_ips", "r");
         while (!feof($file)) {
             $value = fgets($file);
             $ip = preg_replace("/[^0-9\\.]/", "", $value);
             $ip = preg_replace("/\n/", "", $ip);
             if ($ip == "127.0.0.1" || $ip == "::2") {
                 continue;
             }
             $ips[] = $ip;
         }
         fclose($file);
         $dns = dns_get_record($domain[0], DNS_A);
         $live_ip = null;
         $live = false;
         foreach ($dns as $record) {
             foreach ($ips as $ip) {
                 if ($record['ip'] == $ip) {
                     $live = true;
                     $live_ip = $ip;
                     break 2;
                 }
             }
         }
         if (!$live) {
             $live_ip = $dns[0]['ip'];
         }
         WP_CLI::launch("rm /tmp/server_ips");
     }
     // Caching Method
     $cache = "";
     if (defined('W3TC')) {
         $cache .= "w3-total-cache; ";
     }
     if (preg_match("/\\.spaccel\\.net\$/", @gethostbyaddr($live_ip))) {
         $cache .= "studiopress-accelerator (" . gethostbyaddr($live_ip) . "); ";
     }
     if (preg_match("/cache(\\d{1,})?\\.wsynth\\.net\$/", @gethostbyaddr($live_ip))) {
         $cache .= "cache server (" . gethostbyaddr($live_ip) . "); ";
     }
     $nginx_file = "/etc/nginx/sites-enabled/" . $domain[0];
     if (file_exists($nginx_file) && is_readable($nginx_file)) {
         if (preg_grep("/proxy_vhost\\.inc/", file($nginx_file)) && preg_grep("/fastcgi_cache/", file($nginx_file))) {
             $cache .= "fastcgi_cache; ";
         }
     }
     $cache = empty($cache) ? "-" : substr($cache, 0, -2);
     // Loggly
     $healthdata = array('body' => array('domain' => $domain_name, 'wpversion' => $blog_version, 'memusage' => $memusage, 'transients' => $result, 'plugin_count' => $count, 'autoloaded_options' => array('count' => $autoloaded_count, 'size' => $autoloaded_count), 'wpcron' => $wpcron, 'is_multisite' => $networkstatus, 'is_genesis' => defined('GENESIS_ADMIN_DIR') ? "yes" : "no"), 'headers' => array('Content-Type' => 'application/json'));
     if (sizeof($accesses) > 0) {
         $healthdata['body']['accesses'] = $accesses;
     }
     $healthdata['body'] = json_encode($healthdata['body']);
     // Output
     if (isset($assoc_args['cgi-output'])) {
         echo $healthdata['body'];
     } else {
         if (empty($assoc_args['loggly'])) {
             echo "Memory Usage: {$memusage}";
             echo "\nWordPress Version: {$blog_version}";
             echo "\nTransient Variables: {$result}";
             echo "\nActive Plugins: {$count}";
             echo "\nAutoloaded Options Count: {$autoloaded_count}";
             echo "\nAutoloaded Options Size: {$autoloaded_size} Mb";
             echo "\nWordPress Cron Calls: {$wpcron}";
             echo "\nIs Multisite? {$networkstatus}\n";
             echo "Is Genesis? " . (defined('GENESIS_ADMIN_DIR') ? "yes" : "no") . "\n";
             echo "--\n";
             echo "Web Server: " . (file_exists("/var/run/apache2.pid") || file_exists("/run/apache2/apache2.pid") ? "apache+nginx" : "nginx+php5-fpm") . "\n";
             if (!preg_match("/.rmkr.net\$/", gethostname())) {
                 echo "Caching Method: " . $cache . "\n";
             }
             if (!is_null($live)) {
                 if ($live) {
                     echo "DNS Status: site is live ( {$live_ip} )\n";
                 } else {
                     echo "DNS Status: site not live OR behind a cache/proxy server ";
                     if (filter_var($live_ip, FILTER_VALIDATE_IP)) {
                         echo "( " . gethostbyaddr($live_ip) . " )";
                     }
                     echo "\n";
                 }
             }
             if (sizeof($accesses) > 0) {
                 echo "--";
                 echo "\nToday's Accesses: " . $accesses['total'];
                 echo "\n * wp-login.php: " . $accesses['wplogin'];
                 echo "\n * xmlrpc.php: " . $accesses['xmlrpc'] . "\n";
             }
         } else {
             wp_remote_post('https://logs-01.loggly.com/inputs/11bdfe58-13d0-479c-a81b-b5fa8ee1806b/tag/synthesis-inventory/', $healthdata);
             WP_CLI::success("Data sent to Loggly");
         }
     }
 }
 /**
  * Download a remote site.
  * 
  * @subcommand download
  * @synopsis --site-id=<site-id> [--type=<type>]
  */
 public function download($args, $assoc_args)
 {
     $site_id = $assoc_args['site-id'];
     $this->set_account();
     if (empty($assoc_args['type'])) {
         $assoc_args['type'] = 'complete';
     }
     WP_CLI::line(sprintf("Initiating %s archive.", $assoc_args['type']));
     $args = array('endpoint' => '/site/' . $site_id . '/download', 'method' => 'POST', 'body' => array('type' => $assoc_args['type']));
     $response = $this->api_request($args);
     if (is_wp_error($response)) {
         WP_CLI::error($response->get_error_message());
     }
     do {
         $args = array('endpoint' => '/site/' . $site_id . '/download', 'method' => 'GET');
         $response = $this->api_request($args);
         if (!is_wp_error($response) && !empty($response->status) && $response->status == 'error-status') {
             WP_Cli::line('Backup status: ' . strip_tags($response->backup_progress));
             sleep(15);
         }
     } while (!is_wp_error($response) && $response->status != 'backup-complete');
     if (is_wp_error($response)) {
         WP_CLI::error($response->get_error_message());
     }
     WP_CLI::launch(sprintf("wget '%s'", $response->url));
     WP_CLI::success("Site downloaded.");
 }
Example #19
0
 /**
  * Download core WordPress files.
  *
  * Downloads and extracts WordPress core files to the specified path. Uses
  * an archive file stored in cache if WordPress has been previously
  * downloaded.
  *
  * ## OPTIONS
  *
  * [--path=<path>]
  * : Specify the path in which to install WordPress.
  *
  * [--locale=<locale>]
  * : Select which language you want to download.
  *
  * [--version=<version>]
  * : Select which version you want to download. Accepts a version number, 'latest' or 'nightly'
  *
  * [--force]
  * : Overwrites existing files, if present.
  *
  * ## EXAMPLES
  *
  *     $ wp core download --locale=nl_NL
  *     Downloading WordPress 4.5.2 (nl_NL)...
  *     md5 hash verified: c5366d05b521831dd0b29dfc386e56a5
  *     Success: WordPress downloaded.
  *
  * @when before_wp_load
  */
 public function download($args, $assoc_args)
 {
     $download_dir = !empty($assoc_args['path']) ? $assoc_args['path'] : ABSPATH;
     $wordpress_present = is_readable($download_dir . 'wp-load.php');
     if (!\WP_CLI\Utils\get_flag_value($assoc_args, 'force') && $wordpress_present) {
         WP_CLI::error('WordPress files seem to already be present here.');
     }
     if (!is_dir($download_dir)) {
         if (!is_writable(dirname($download_dir))) {
             WP_CLI::error(sprintf("Insufficient permission to create directory '%s'.", $download_dir));
         }
         WP_CLI::log(sprintf("Creating directory '%s'.", $download_dir));
         $mkdir = \WP_CLI\Utils\is_windows() ? 'mkdir %s' : 'mkdir -p %s';
         WP_CLI::launch(Utils\esc_cmd($mkdir, $download_dir));
     }
     if (!is_writable($download_dir)) {
         WP_CLI::error(sprintf("'%s' is not writable by current user.", $download_dir));
     }
     $locale = \WP_CLI\Utils\get_flag_value($assoc_args, 'locale', 'en_US');
     if (isset($assoc_args['version']) && 'latest' !== $assoc_args['version']) {
         $version = $assoc_args['version'];
         $version = in_array(strtolower($version), array('trunk', 'nightly')) ? 'nightly' : $version;
         //nightly builds are only available in .zip format
         $ext = 'nightly' === $version ? 'zip' : 'tar.gz';
         $download_url = $this->get_download_url($version, $locale, $ext);
     } else {
         $offer = $this->get_download_offer($locale);
         if (!$offer) {
             WP_CLI::error("The requested locale ({$locale}) was not found.");
         }
         $version = $offer['current'];
         $download_url = str_replace('.zip', '.tar.gz', $offer['download']);
     }
     if ('nightly' === $version && 'en_US' !== $locale) {
         WP_CLI::error('Nightly builds are only available for the en_US locale.');
     }
     $from_version = '';
     if (file_exists($download_dir . 'wp-includes/version.php')) {
         global $wp_version;
         require_once $download_dir . 'wp-includes/version.php';
         $from_version = $wp_version;
     }
     WP_CLI::log(sprintf('Downloading WordPress %s (%s)...', $version, $locale));
     $path_parts = pathinfo($download_url);
     $extension = 'tar.gz';
     if ('zip' === $path_parts['extension']) {
         $extension = 'zip';
         if (!class_exists('ZipArchive')) {
             WP_CLI::error('Extracting a zip file requires ZipArchive.');
         }
     }
     $cache = WP_CLI::get_cache();
     $cache_key = "core/wordpress-{$version}-{$locale}.{$extension}";
     $cache_file = $cache->has($cache_key);
     $bad_cache = false;
     if ($cache_file) {
         WP_CLI::log("Using cached file '{$cache_file}'...");
         try {
             Extractor::extract($cache_file, $download_dir);
         } catch (Exception $e) {
             WP_CLI::warning("Extraction failed, downloading a new copy...");
             $bad_cache = true;
         }
     }
     if (!$cache_file || $bad_cache) {
         // We need to use a temporary file because piping from cURL to tar is flaky
         // on MinGW (and probably in other environments too).
         $temp = \WP_CLI\Utils\get_temp_dir() . uniqid('wp_') . ".{$extension}";
         $headers = array('Accept' => 'application/json');
         $options = array('timeout' => 600, 'filename' => $temp);
         $response = Utils\http_request('GET', $download_url, null, $headers, $options);
         if (404 == $response->status_code) {
             WP_CLI::error("Release not found. Double-check locale or version.");
         } else {
             if (20 != substr($response->status_code, 0, 2)) {
                 WP_CLI::error("Couldn't access download URL (HTTP code {$response->status_code}).");
             }
         }
         if ('nightly' !== $version) {
             $md5_response = Utils\http_request('GET', $download_url . '.md5');
             if (20 != substr($md5_response->status_code, 0, 2)) {
                 WP_CLI::error("Couldn't access md5 hash for release (HTTP code {$response->status_code}).");
             }
             $md5_file = md5_file($temp);
             if ($md5_file === $md5_response->body) {
                 WP_CLI::log('md5 hash verified: ' . $md5_file);
             } else {
                 WP_CLI::error("md5 hash for download ({$md5_file}) is different than the release hash ({$md5_response->body}).");
             }
         } else {
             WP_CLI::warning('md5 hash checks are not available for nightly downloads.');
         }
         try {
             Extractor::extract($temp, $download_dir);
         } catch (Exception $e) {
             WP_CLI::error("Couldn't extract WordPress archive. " . $e->getMessage());
         }
         if ('nightly' !== $version) {
             $cache->import($cache_key, $temp);
         }
         unlink($temp);
     }
     if ($wordpress_present) {
         $this->cleanup_extra_files($from_version, $version, $locale);
     }
     WP_CLI::success('WordPress downloaded.');
 }
Example #20
0
 /**
  * Import database from a file.
  *
  * @synopsis [<file>]
  */
 function import($args, $assoc_args)
 {
     $result_file = $this->get_file_name($args);
     $command = self::create_cmd('mysql %s --user=%s --password=%s --host=%s < %s', DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, $result_file);
     WP_CLI::launch($command);
     WP_CLI::success(sprintf('Imported from %s', $result_file));
 }
 /**
  * Download and install theme unit test datafile
  * 
  * @param string $option  --data value
  * @since 0.2
  */
 private function import_test_data($option = NULL)
 {
     if ('skip' === $option) {
         return;
     }
     $option = NULL === $option ? 'unit-test' : $option;
     $datafiles = array('unit-test' => 'https://wpcom-themes.svn.automattic.com/demo/theme-unit-test-data.xml', 'wpcom-theme' => 'https://wpcom-themes.svn.automattic.com/demo/wordpress-com-theme-test.xml', 'wpcom-demo' => 'https://wpcom-themes.svn.automattic.com/demo/demo-data.xml', 'wptest' => 'https://raw.github.com/manovotny/wptest/master/wptest.xml');
     $keys = array_values(array_keys($datafiles));
     if (in_array($option, $keys)) {
         $download_url = $datafiles[$option];
     } elseif (false != $option) {
         $download_url = $option;
     } else {
         WP_CLI::error('Missing WXR path/URL.');
     }
     WP_CLI::line('WXR data URL: ' . $download_url);
     $silent = WP_CLI::get_config('quiet') ? '--silent ' : '';
     $cmdline = "curl -f {$silent} {$download_url} -o /tmp/wp-cli-test-data.xml";
     WP_CLI::launch($cmdline);
     WP_CLI::launch('wp import /tmp/wp-cli-test-data.xml --authors=skip');
 }
Example #22
0
 /**
  * Extract a zip archive
  * @param  $destinationPath - the path to extract to
  * @param  $option          - command line option to get zip filename from, defaults to 'zipfile'
  * @return bool
  */
 private function unzip($destinationPath, $option = 'zipfile')
 {
     if ($zipfile = $this->getOption($option, false)) {
         WP_CLI::line('Extracting zip archive ...');
         WP_CLI::launch("unzip -q " . $zipfile . " -d " . $destinationPath);
         return true;
     } else {
         return false;
     }
 }
Example #23
0
 protected static function launch_command($cmd)
 {
     $launch = WP_CLI::launch($cmd, false, true);
     if (!empty($launch->stderr)) {
         return new WP_Error('command_error', $launch->stderr);
     } else {
         return $launch->stdout;
     }
 }
Example #24
0
 /**
  * Delete plugin files.
  *
  * @synopsis <plugin>
  */
 function delete($args, $assoc_args = array(), $exit_on_error = true)
 {
     list($file, $name) = $this->parse_name($args);
     $plugin_dir = dirname($file);
     if ('.' == $plugin_dir) {
         $plugin_dir = $file;
     }
     $command = 'rm -rf ' . path_join(WP_PLUGIN_DIR, $plugin_dir);
     return WP_CLI::launch($command, $exit_on_error);
 }
Example #25
0
 /**
  * @param string     $command
  * @param bool|true  $exit_on_error
  * @param bool|false $return_detailed
  *
  * @return int|\ProcessRun
  */
 public function launch($command, $exit_on_error = true, $return_detailed = false)
 {
     return \WP_CLI::launch($command, $exit_on_error, $return_detailed);
 }
Example #26
0
 private function _delete($plugin)
 {
     $plugin_dir = dirname($plugin->file);
     if ('.' == $plugin_dir) {
         $plugin_dir = $plugin->file;
     }
     $path = path_join(WP_PLUGIN_DIR, $plugin_dir);
     if (\WP_CLI\Utils\is_windows()) {
         // Handles plugins that are not in own folders
         // e.g. Hello Dolly -> plugins/hello.php
         if (is_file($path)) {
             $command = 'del /f /q ';
         } else {
             $command = 'rd /s /q ';
         }
         $path = str_replace("/", "\\", $path);
     } else {
         $command = 'rm -rf ';
     }
     return !WP_CLI::launch($command . $path);
 }
Example #27
0
/**
 * Launch system's $EDITOR to edit text
 *
 * @param  str  $content  Text to edit (eg post content)
 * @return str|bool       Edited text, if file is saved from editor
 *                        False, if no change to file
 */
function launch_editor_for_input($input, $title = 'WP-CLI')
{
    $tmpfile = wp_tempnam($title);
    if (!$tmpfile) {
        \WP_CLI::error('Error creating temporary file.');
    }
    $output = '';
    file_put_contents($tmpfile, $input);
    $editor = getenv('EDITOR');
    if (!$editor) {
        if (isset($_SERVER['OS']) && false !== strpos($_SERVER['OS'], 'indows')) {
            $editor = 'notepad';
        } else {
            $editor = 'vi';
        }
    }
    \WP_CLI::launch("{$editor} " . escapeshellarg($tmpfile));
    $output = file_get_contents($tmpfile);
    unlink($tmpfile);
    if ($output === $input) {
        return false;
    }
    return $output;
}
 /**
  * Download a given backup for a given site
  * 
  * @subcommand download
  * @synopsis <backup-id> --site-id=<site-id>
  */
 public function download($args, $assoc_args)
 {
     $site_id = $assoc_args['site-id'];
     list($backup_id) = $args;
     $this->set_account();
     $args = array('endpoint' => '/site/' . (int) $site_id . '/backup/' . (int) $backup_id, 'method' => 'GET');
     $backup = $this->api_request($args);
     if (is_wp_error($backup)) {
         WP_CLI::error($backup->get_error_message());
     }
     WP_CLI::launch(sprintf("wget '%s'", $backup->url));
     WP_CLI::success("Backup downloaded.");
 }
 private function _delete($plugin)
 {
     $plugin_dir = dirname($plugin->file);
     if ('.' == $plugin_dir) {
         $plugin_dir = $plugin->file;
     }
     $path = path_join(WP_PLUGIN_DIR, $plugin_dir);
     if (\WP_CLI\Utils\is_windows()) {
         $command = 'rd /s /q ';
         $path = str_replace("/", "\\", $path);
     } else {
         $command = 'rm -rf ';
     }
     return !WP_CLI::launch($command . $path);
 }