Use WP_CLI::runcommand() instead, which is easier to use and works better.
To run a command using a new process with the same global parameters,
use WP_CLI::launch_self(). To run a command using a new process with
different global parameters, use WP_CLI::launch().
ob_start();
WP_CLI::run_command( array( 'cli', 'cmd-dump' ) );
$ret = ob_get_clean();
public static run_command ( array $args, array $assoc_args = [] ) | ||
$args | array | Positional arguments including command name. |
$assoc_args | array |
/** * Sets up Developer Plugin * @subcommand install-plugins * @synopsis --type=<type> [--activate] */ function install_plugins($args, $assoc_args) { global $automattic_developer; // wp-cli doesn't fire admin_init since 0.11.2 if (!did_action('admin_init')) { $automattic_developer->admin_init(); } $type = $assoc_args['type']; $activate = isset($assoc_args['activate']) && $assoc_args['activate'] == "true"; $reco_plugins = $automattic_developer->recommended_plugins; $installed_plugins = array_keys(get_plugins()); $types = array_keys($automattic_developer->get_project_types()); if (in_array($type, $types)) { $automattic_developer->save_project_type($type); foreach ($reco_plugins as $slug => $plugin) { $path = $automattic_developer->get_path_for_recommended_plugin($slug); $activate_plugin = $activate && ('all' == $plugin['project_type'] || $type == $plugin['project_type']); // Download the plugin if we don't already have it if (!in_array($path, $installed_plugins)) { WP_CLI::run_command(explode(" ", "plugin install {$slug}")); } // Install the plugin if --activate and it's the right type if (is_plugin_inactive($path) && $activate_plugin) { if (NULL == activate_plugin($path)) { WP_CLI::success("Activated " . $plugin['name']); } } } } else { WP_CLI::error("Specify a valid type to install: <" . implode("|", $types) . ">"); } }
function __construct() { // After updating plugins/themes also update translations by running the `core language update` command. add_action('upgrader_process_complete', function () { remove_action('upgrader_process_complete', array('Language_Pack_Upgrader', 'async_upgrade'), 20); \WP_CLI::run_command(array('core', 'language', 'update'), array('dry-run' => false)); }, 1); }
/** * Run all cron events that are due now. * * ## EXAMPLES * * wp cron event wppaas run */ public function run($args, $assoc_args) { $events = static::get_cron_events_due_now(); if (!is_wp_error($events) && $events) { $hooks = wp_list_pluck($events, 'hook'); WP_CLI::log(sprintf('Running %d cron event(s) due now: %s', count($hooks), implode(', ', $hooks))); WP_CLI::run_command(array_merge(['cron', 'event', 'run'], $hooks), $assoc_args); return; } if (WP_CLI\Utils\get_flag_value($assoc_args, 'quiet')) { return; } WP_CLI::warning('No cron events are due.'); }
/** * Theme activation * * ## OPTIONS * * [<theme>] * : The theme name to activate. Defaults to 'sage'. * * [--show-on-front=<page-type>] * : What to show on the front page. Options are: 'posts', 'page'. Default is 'page'. * * [--permalink-structure=<permalink-string>] * : Permalink structure. Default is '/%postname%/'. * * [--skip-navigation] * : Skip creating default Primary Navigation. * * ## EXAMPLES * * wp theme activation * wp theme activation --show-on-front=page --permalink-structure='/%year%/%postname%/' --skip-navigation * * @subcommand activation * @alias roots-activate */ public function activation($args = [], $options = []) { list($theme) = $args + ['sage']; $defaults = ['permalink-structure' => '/%postname%/', 'show-on-front' => 'page', 'skip-navigation' => false]; $options = wp_parse_args($options, $defaults); $options['skip-navigation'] = $options['skip-navigation'] || !!wp_get_nav_menu_object('Primary Navigation'); \WP_CLI::log('Activating theme and setting options'); $home_page_options = ['post_content' => 'Lorem Ipsum', 'post_status' => 'publish', 'post_title' => 'Home', 'post_type' => 'page']; parent::activate([$theme]); if ($home_page_id = wp_insert_post($home_page_options, false)) { \WP_CLI::run_command(['option', 'update', 'show_on_front', $options['show-on-front']]); \WP_CLI::run_command(['option', 'update', 'page_on_front', $home_page_id]); } if (!$options['skip-navigation'] && ($menu_id = wp_create_nav_menu('Primary Navigation'))) { $home_page_id && wp_update_nav_menu_item($menu_id, 0, ['menu-item-title' => $home_page_options['post_title'], 'menu-item-object' => $home_page_options['post_type'], 'menu-item-object-id' => $home_page_id, 'menu-item-type' => 'post_type', 'menu-item-status' => 'publish']); set_theme_mod('nav_menu_locations', ['primary_navigation' => $menu_id]); \WP_CLI::success('Primary Navigation created.'); } \WP_CLI::run_command(['rewrite', 'structure', $options['permalink-structure']]); \WP_CLI::run_command(['rewrite', 'flush']); \WP_CLI::success('Theme activated'); }
/** * Generate starter code for a plugin. * * ## OPTIONS * * <slug> * : The internal name of the plugin. * * [--dir=<dirname>] * : Put the new plugin in some arbitrary directory path. Plugin directory will be path plus supplied slug. * * [--plugin_name=<title>] * : What to put in the 'Plugin Name:' header * * [--skip-tests] * : Don't generate files for unit testing. * * [--activate] * : Activate the newly generated plugin. * * [--activate-network] * : Network activate the newly generated plugin. */ function plugin($args, $assoc_args) { $plugin_slug = $args[0]; $data = wp_parse_args($assoc_args, array('plugin_slug' => $plugin_slug, 'plugin_name' => ucfirst($plugin_slug))); $data['textdomain'] = $plugin_slug; if (!empty($assoc_args['dir'])) { if (!is_dir($assoc_args['dir'])) { WP_CLI::error("Cannot create plugin in directory that doesn't exist."); } $plugin_dir = $assoc_args['dir'] . "/{$plugin_slug}"; } else { $plugin_dir = WP_PLUGIN_DIR . "/{$plugin_slug}"; $this->maybe_create_plugins_dir(); } $plugin_path = "{$plugin_dir}/{$plugin_slug}.php"; $plugin_readme_path = "{$plugin_dir}/readme.txt"; $this->create_file($plugin_path, Utils\mustache_render('plugin.mustache', $data)); $this->create_file($plugin_readme_path, Utils\mustache_render('plugin-readme.mustache', $data)); $this->create_file("{$plugin_dir}/package.json", Utils\mustache_render('plugin-packages.mustache', $data)); $this->create_file("{$plugin_dir}/Gruntfile.js", Utils\mustache_render('plugin-gruntfile.mustache', $data)); WP_CLI::success("Created {$plugin_dir}"); if (!\WP_CLI\Utils\get_flag_value($assoc_args, 'skip-tests')) { WP_CLI::run_command(array('scaffold', 'plugin-tests', $plugin_slug), array('dir' => $plugin_dir)); } if (\WP_CLI\Utils\get_flag_value($assoc_args, 'activate')) { WP_CLI::run_command(array('plugin', 'activate', $plugin_slug)); } else { if (\WP_CLI\Utils\get_flag_value($assoc_args, 'activate-network')) { WP_CLI::run_command(array('plugin', 'activate', $plugin_slug), array('network' => true)); } } }
/** * Generate starter code for a plugin. * * ## OPTIONS * * <slug> * : The internal name of the plugin. * * [--dir=<dirname>] * : Put the new plugin in some arbitrary directory path. Plugin directory will be path plus supplied slug. * * [--plugin_name=<title>] * : What to put in the 'Plugin Name:' header * * [--plugin_description=<description>] * : What to put in the 'Description:' header. * * [--plugin_author=<author>] * : What to put in the 'Author:' header. * * [--plugin_author_uri=<url>] * : What to put in the 'Author URI:' header. * * [--plugin_uri=<url>] * : What to put in the 'Plugin URI:' header. * * [--skip-tests] * : Don't generate files for unit testing. * * [--activate] * : Activate the newly generated plugin. * * [--activate-network] * : Network activate the newly generated plugin. * * [--force] * : Overwrite files that already exist. * */ function plugin($args, $assoc_args) { $plugin_slug = $args[0]; $plugin_name = ucwords(str_replace('-', ' ', $plugin_slug)); $plugin_package = str_replace(' ', '_', $plugin_name); $data = wp_parse_args($assoc_args, array('plugin_slug' => $plugin_slug, 'plugin_name' => $plugin_name, 'plugin_package' => $plugin_package, 'plugin_description' => 'PLUGIN DESCRIPTION HERE', 'plugin_author' => 'YOUR NAME HERE', 'plugin_author_uri' => 'YOUR SITE HERE', 'plugin_uri' => 'PLUGIN SITE HERE')); $data['textdomain'] = $plugin_slug; if (!empty($assoc_args['dir'])) { if (!is_dir($assoc_args['dir'])) { WP_CLI::error("Cannot create plugin in directory that doesn't exist."); } $plugin_dir = $assoc_args['dir'] . "/{$plugin_slug}"; } else { $plugin_dir = WP_PLUGIN_DIR . "/{$plugin_slug}"; $this->maybe_create_plugins_dir(); } $plugin_path = "{$plugin_dir}/{$plugin_slug}.php"; $plugin_readme_path = "{$plugin_dir}/readme.txt"; $force = \WP_CLI\Utils\get_flag_value($assoc_args, 'force'); $files_written = $this->create_files(array($plugin_path => Utils\mustache_render('plugin.mustache', $data), $plugin_readme_path => Utils\mustache_render('plugin-readme.mustache', $data), "{$plugin_dir}/package.json" => Utils\mustache_render('plugin-packages.mustache', $data), "{$plugin_dir}/Gruntfile.js" => Utils\mustache_render('plugin-gruntfile.mustache', $data), "{$plugin_dir}/.gitignore" => Utils\mustache_render('plugin-gitignore.mustache', $data), "{$plugin_dir}/.editorconfig" => file_get_contents(WP_CLI_ROOT . "/templates/.editorconfig")), $force); $this->log_whether_files_written($files_written, $skip_message = 'All plugin files were skipped.', $success_message = 'Created plugin files.'); if (!\WP_CLI\Utils\get_flag_value($assoc_args, 'skip-tests')) { WP_CLI::run_command(array('scaffold', 'plugin-tests', $plugin_slug), array('dir' => $plugin_dir, 'force' => $force)); } if (\WP_CLI\Utils\get_flag_value($assoc_args, 'activate')) { WP_CLI::run_command(array('plugin', 'activate', $plugin_slug)); } else { if (\WP_CLI\Utils\get_flag_value($assoc_args, 'activate-network')) { WP_CLI::run_command(array('plugin', 'activate', $plugin_slug), array('network' => true)); } } }
protected static function apply($cmd, $args, $assoc_args) { $op = array_shift($args); list($url, $tgz, $zip) = static::tgz($args, $assoc_args); WP_CLI::debug("Installing from {$zip}"); WP_CLI::run_command(array($cmd, $op, $zip), array('force' => 1)); WP_CLI::debug("Removing {$tgz}, {$zip}"); unlink($tgz); unlink($zip); }
$GLOBALS['wp']->parse_request(); $GLOBALS['wp_query']->query($GLOBALS['wp']->query_vars); } } // Set the user if (isset($assoc_args['user'])) { $user = $assoc_args['user']; if (is_numeric($user)) { $user_id = (int) $user; } else { $user_id = (int) username_exists($user); } if (!$user_id || !wp_set_current_user($user_id)) { WP_CLI::error(sprintf('Could not get a user_id for this user: %s', var_export($user, true))); } unset($user); } // Set filesystem method add_filter('filesystem_method', function () { return 'direct'; }, 99); // Handle --completions parameter if (isset($assoc_args['completions'])) { WP_CLI::load_all_commands(); foreach (WP_CLI::$commands as $name => $command) { WP_CLI::line($name . ' ' . implode(' ', WP_CLI_Command::get_subcommands($command))); } exit; } WP_CLI::run_command($arguments, $assoc_args);
/** * View customer orders. * * ## OPTIONS * * <customer> * : The customer ID, email or username. * * [--field=<field>] * : Instead of returning the whole customer fields, returns the value of a single fields. * * [--fields=<fields>] * : Get a specific subset of the customer's fields. * * [--format=<format>] * : Accepted values: table, json, csv. Default: table. * * ## AVAILABLE FIELDS * * For more fields, see: wp wc order list --help * * ## EXAMPLES * * wp wc customer orders 123 * * @since 2.5.0 */ public function orders($args, $assoc_args) { try { WP_CLI::run_command(array('wc', 'order', 'list'), array('customer_id' => $args[0])); } catch (WC_CLI_Exception $e) { WP_CLI::error($e->getMessage()); } }
/** * @subcommand verify-wordpress * */ function verifyWordPress($args, $assoc_args) { WP_CLI::run_command(array('core', 'verify-checksums')); }
/** * Generate starter code for a plugin. * * ## OPTIONS * * <slug> * : The internal name of the plugin. * * [--plugin_name=<title>] * : What to put in the 'Plugin Name:' header * * [--skip-tests] * : Don't generate files for unit testing. * * [--activate] * : Activate the newly generated plugin. */ function plugin($args, $assoc_args) { $plugin_slug = $args[0]; $data = wp_parse_args($assoc_args, array('plugin_name' => ucfirst($plugin_slug))); $data['textdomain'] = $plugin_slug; $plugin_dir = WP_PLUGIN_DIR . "/{$plugin_slug}"; $plugin_path = "{$plugin_dir}/{$plugin_slug}.php"; $this->create_file($plugin_path, Utils\mustache_render('plugin.mustache', $data)); WP_CLI::success("Created {$plugin_dir}"); if (!isset($assoc_args['skip-tests'])) { WP_CLI::run_command(array('scaffold', 'plugin-tests', $plugin_slug)); } if (isset($assoc_args['activate'])) { WP_CLI::run_command(array('plugin', 'activate', $plugin_slug)); } }
/** * @param $args * @param array $assoc_args */ public function run_command($args, $assoc_args = array()) { return \WP_CLI::run_command($args, $assoc_args); }
/** * Implementation of command 'upgrade' */ private function upgrade() { # todo: use wp-cli to download tarfile. # todo: if tarfile is not specified, see if the code already exists and use that instead. if (!$this->getOption('tarfile', false) and !$this->getOption('zipfile', false)) { return WP_CLI::error('Must specify either --tarfile or --zipfile'); } # fixme: throw error if tarfile is not in a valid format. if (!defined('CIVICRM_UPGRADE_ACTIVE')) { define('CIVICRM_UPGRADE_ACTIVE', 1); } $wp_root = ABSPATH; $settings_path = ABSPATH . '/wp-content/plugins/civicrm/civicrm.settings.php'; if (!file_exists($settings_path)) { return WP_CLI::error('Unable to locate settings file at ' . $settings_path); } # nb: we don't want to require civicrm.settings.php here, because .. # # a) this is the old environment we're going to replace # b) upgrade-db needs to bootstrap the new environment, so requiring the file # now will create multiple inclusion problems later on # # however, all we're really after is $civicrm_root and CIVICRM_DSN, so we're going to # pull out the lines we need using a regex and run them - yes, it's pretty silly .. # don't try this at home, kids. $settings = file_get_contents($settings_path); $settings = str_replace("\r", '', $settings); $settings = explode("\n", $settings); if ($civicrm_root_code = reset(preg_grep('/^\\s*\\$civicrm_root\\s*=.*$/', $settings))) { eval($civicrm_root_code); } else { return WP_CLI::error('Unable to read $civicrm_root from civicrm.settings.php'); } if ($civicrm_dsn_code = reset(preg_grep('/^\\s*define.*CIVICRM_DSN.*$/', $settings))) { $civicrm_dsn_code = str_replace('CIVICRM_DSN', 'CIVICRM_OLD_DSN', $civicrm_dsn_code); eval($civicrm_dsn_code); } else { return WP_CLI::error('Unable to read CIVICRM_DSN from civicrm.settings.php'); } if (!defined('CIVICRM_OLD_DSN')) { return WP_CLI::error('Unable to set CIVICRM_OLD_DSN'); } $date = date('YmdHis'); $backup_file = "civicrm"; $basepath = explode('/', $civicrm_root); if (!end($basepath)) { array_pop($basepath); } array_pop($basepath); $project_path = implode('/', $basepath) . '/'; array_pop($basepath); $plugin_path = implode('/', $basepath) . '/'; $backup_dir = $this->getOption('backup-dir', $wp_root . '../backup'); $backup_dir = rtrim($backup_dir, '/'); WP_CLI::line("\nThe upgrade process involves - "); WP_CLI::line(sprintf("1. Backing up current CiviCRM code as => %s", "{$backup_dir}/plugins/{$date}/{$backup_file}")); WP_CLI::line(sprintf("2. Backing up database as => %s", "{$backup_dir}/plugins/{$date}/{$backup_file}.sql")); WP_CLI::line(sprintf("3. Unpacking tarfile to => %s", $plugin_path)); WP_CLI::line("4. Executing civicrm/upgrade?reset=1 just as a browser would.\n"); WP_CLI::confirm('Do you really want to continue?'); # begin upgrade $backup_dir .= '/plugins/' . $date; if (!mkdir($backup_dir, 777, true)) { return WP_CLI::error('Failed creating directory: ' . $backup_dir); } $backup_target = $backup_dir . '/' . $backup_file; if (!rename($project_path, $backup_target)) { return WP_CLI::error(sprintf("Failed to backup CiviCRM project directory %s to %s", $project_path, $backup_target)); } WP_CLI::line(); WP_CLI::success("1. Code backed up."); WP_CLI::run_command(array('civicrm', 'sql-dump'), array('result-file' => $backup_target . '.sql')); WP_CLI::success('2. Database backed up.'); # decompress if ($this->getOption('tarfile', false)) { # should probably never get to here, as looks like Wordpress Civi comes # in a zip file if (!$this->untar($plugin_path)) { return WP_CLI::error("Error extracting tarfile"); } } elseif ($this->getOption('zipfile', false)) { if (!$this->unzip($plugin_path)) { return WP_CLI::error("Error extracting zipfile"); } } else { return WP_CLI::error("No zipfile specified, use --zipfile=path/to/zipfile"); } WP_CLI::success('3. Archive unpacked.'); WP_CLI::line('Copying civicrm.settings.php to ' . $project_path . '..'); define('CIVICRM_SETTINGS_PATH', $project_path . 'civicrm.settings.php'); if (!copy($backup_dir . '/civicrm/civicrm.settings.php', CIVICRM_SETTINGS_PATH)) { return WP_CLI::error('Failed to copy file'); } WP_CLI::success("4. "); WP_CLI::run_command(array('civicrm', 'upgrade-db'), array()); WP_CLI::success("Process completed."); }
private function import_db($file) { WP_CLI::run_command(array('db', 'import', $file), array()); }
/** * Generate starter code for a plugin. * * ## OPTIONS * * <slug> * : The internal name of the plugin. * * [--dir=<dirname>] * : Put the new plugin in some arbitrary directory path. Plugin directory will be path plus supplied slug. * * [--plugin_name=<title>] * : What to put in the 'Plugin Name:' header * * [--skip-tests] * : Don't generate files for unit testing. * * [--activate] * : Activate the newly generated plugin. * * [--activate-network] * : Network activate the newly generated plugin. * * [--force] * : Overwrite files that already exist. * */ function plugin($args, $assoc_args) { $plugin_slug = $args[0]; $data = wp_parse_args($assoc_args, array('plugin_slug' => $plugin_slug, 'plugin_name' => ucfirst($plugin_slug))); $data['textdomain'] = $plugin_slug; if (!empty($assoc_args['dir'])) { if (!is_dir($assoc_args['dir'])) { WP_CLI::error("Cannot create plugin in directory that doesn't exist."); } $plugin_dir = $assoc_args['dir'] . "/{$plugin_slug}"; } else { $plugin_dir = WP_PLUGIN_DIR . "/{$plugin_slug}"; $this->maybe_create_plugins_dir(); } $plugin_path = "{$plugin_dir}/{$plugin_slug}.php"; $plugin_readme_path = "{$plugin_dir}/readme.txt"; $force = \WP_CLI\Utils\get_flag_value($assoc_args, 'force'); $files_written = $this->create_files(array($plugin_path => Utils\mustache_render('plugin.mustache', $data), $plugin_readme_path => Utils\mustache_render('plugin-readme.mustache', $data), "{$plugin_dir}/package.json" => Utils\mustache_render('plugin-packages.mustache', $data), "{$plugin_dir}/Gruntfile.js" => Utils\mustache_render('plugin-gruntfile.mustache', $data)), $force); $this->log_whether_files_written($files_written, $skip_message = 'All plugin files were skipped.', $success_message = 'Created plugin files.'); if (!\WP_CLI\Utils\get_flag_value($assoc_args, 'skip-tests')) { WP_CLI::run_command(array('scaffold', 'plugin-tests', $plugin_slug), array('dir' => $plugin_dir, 'force' => $force)); } if (\WP_CLI\Utils\get_flag_value($assoc_args, 'activate')) { WP_CLI::run_command(array('plugin', 'activate', $plugin_slug)); } else { if (\WP_CLI\Utils\get_flag_value($assoc_args, 'activate-network')) { WP_CLI::run_command(array('plugin', 'activate', $plugin_slug), array('network' => true)); } } }