colorize() public static method

Yes, you too can change the color of command line text. For instance, here's how WP_CLI::success() colorizes "Success: " WP_CLI::colorize( "%GSuccess:%n " ) Uses \cli\Colors::colorize() to transform color tokens to display settings. Choose from the following tokens (and note 'reset'): * %y => ['color' => 'yellow'], * %g => ['color' => 'green'], * %b => ['color' => 'blue'], * %r => ['color' => 'red'], * %p => ['color' => 'magenta'], * %m => ['color' => 'magenta'], * %c => ['color' => 'cyan'], * %w => ['color' => 'grey'], * %k => ['color' => 'black'], * %n => ['color' => 'reset'], * %Y => ['color' => 'yellow', 'style' => 'bright'], * %G => ['color' => 'green', 'style' => 'bright'], * %B => ['color' => 'blue', 'style' => 'bright'], * %R => ['color' => 'red', 'style' => 'bright'], * %P => ['color' => 'magenta', 'style' => 'bright'], * %M => ['color' => 'magenta', 'style' => 'bright'], * %C => ['color' => 'cyan', 'style' => 'bright'], * %W => ['color' => 'grey', 'style' => 'bright'], * %K => ['color' => 'black', 'style' => 'bright'], * %N => ['color' => 'reset', 'style' => 'bright'], * %3 => ['background' => 'yellow'], * %2 => ['background' => 'green'], * %4 => ['background' => 'blue'], * %1 => ['background' => 'red'], * %5 => ['background' => 'magenta'], * %6 => ['background' => 'cyan'], * %7 => ['background' => 'grey'], * %0 => ['background' => 'black'], * %F => ['style' => 'blink'], * %U => ['style' => 'underline'], * %8 => ['style' => 'inverse'], * %9 => ['style' => 'bright'], * %_ => ['style' => 'bright')
public static colorize ( string $string ) : string
$string string String to colorize for output, with color tokens.
return string Colorized string.
 /**
  * Migrate meta values to taxonomy terms. Delete meta after import
  *
  * ## OPTIONS
  *
  * <meta-key>
  * : Meta key to convert
  *
  * <taxonomy-slug>
  * : Taxonomy to move values into
  *
  * [--<field>=<value>]
  * : One or more args to pass to WP_Query.
  *
  * ## EXAMPLES
  *
  *     wp mtt migrate meta_key taxonomy_slug
  *     wp mtt migrate meta_key taxonomy_slug --posts_per_page=200 --paged=2
  *
  */
 function migrate($args, $assoc_args)
 {
     list($meta_key, $taxonomy) = $args;
     if (!($taxonomy_object = get_taxonomy($taxonomy))) {
         WP_CLI::error(sprintf("The taxonomy '%s' doesn't exist", $taxonomy));
     }
     $defaults = array('post_type' => $taxonomy_object->object_type, 'posts_per_page' => -1, 'post_status' => 'any');
     $query_args = array_merge($defaults, $assoc_args);
     $query = new WP_Query($query_args);
     // summary
     WP_CLI::log(sprintf("---\nPer page: %d \nPage: %d \nTotal pages: %d\n---", $query_args['posts_per_page'], isset($query_args['paged']) ? $query_args['paged'] : 1, $query->max_num_pages));
     while ($query->have_posts()) {
         $query->the_post();
         $id = get_the_id();
         // get meta
         $metas = get_post_meta($id, $meta_key);
         // create term
         if (!$metas) {
             WP_CLI::log(WP_CLI::colorize("%c[{$id}]%n No meta, skipped"));
         } else {
             if (!is_wp_error(wp_set_object_terms($id, $metas, $taxonomy, true))) {
                 WP_CLI::log(WP_CLI::colorize("%g[{$id}]%n Migrated: " . implode(', ', $metas)));
                 // clean meta
                 delete_post_meta($id, $meta_key);
             } else {
                 WP_CLI::log(WP_CLI::colorize("%r[{$id}]%n Error: Could not set terms for post"));
             }
         }
     }
 }
 private function show_legend($items)
 {
     $statuses = array_unique(wp_list_pluck($items, 'status'));
     $legend_line = array();
     foreach ($statuses as $status) {
         $legend_line[] = sprintf('%s%s = %s%%n', $this->get_color($status), $this->map['short'][$status], $this->map['long'][$status]);
     }
     if (in_array(true, wp_list_pluck($items, 'update'))) {
         $legend_line[] = '%yU = Update Available%n';
     }
     \WP_CLI::line('Legend: ' . \WP_CLI::colorize(implode(', ', $legend_line)));
 }
Example #3
0
File: help.php Project: nb/wp-cli
 private static function show_help($command)
 {
     $out = self::get_initial_markdown($command);
     $longdesc = $command->get_longdesc();
     if ($longdesc) {
         $out .= wordwrap($longdesc, 79) . "\n";
     }
     // section headers
     $out = preg_replace('/^## ([A-Z ]+)/m', WP_CLI::colorize('%9\\1%n'), $out);
     // definition lists
     $out = preg_replace_callback('/([^\\n]+)\\n: (.+?)(\\n\\n|$)/s', array(__CLASS__, 'rewrap_param_desc'), $out);
     $out = str_replace("\t", '  ', $out);
     self::pass_through_pager($out);
 }
Example #4
0
 /**
  * Prints a greeting.
  * 
  * ## OPTIONS
  * 
  * <name>
  * : The name of the person to greet.
  * 
  * ## EXAMPLES
  * 
  *     wp theme-check check twentyfifteen
  *
  * @synopsis <name> [--disable-themeforest]
  */
 function check($args, $assoc_args)
 {
     global $checkcount, $themechecks;
     $theme = $this->fetcher->get_check($args[0]);
     $files = $theme->get_files(null, -1);
     $css = $php = $other = array();
     if ($files) {
         foreach ($files as $key => $filename) {
             if (substr($filename, -4) == '.php') {
                 $php[$filename] = php_strip_whitespace($filename);
             } else {
                 if (substr($filename, -4) == '.css') {
                     $css[$filename] = file_get_contents($filename);
                 } else {
                     $other[$filename] = !is_dir($filename) ? file_get_contents($filename) : '';
                 }
             }
         }
         // run the checks
         $success = run_themechecks($php, $css, $other);
         foreach ($themechecks as $check) {
             if ($check instanceof themecheck) {
                 $errors = $check->getError();
                 if (!empty($error)) {
                     $errors = array_merge($error, $errors);
                 }
                 $errors = array_map('strip_tags', $errors);
                 foreach ($errors as $key => $value) {
                     list($type, $message) = explode(':', $value, 2);
                     switch (trim($type)) {
                         case 'WARNING':
                             WP_CLI::line(WP_CLI::colorize('%rWARNING:%n ' . trim($message)));
                             break;
                         case 'INFO':
                             WP_CLI::line(WP_CLI::colorize('%bINFO:%n ' . trim($message)));
                             break;
                         case 'RECOMMENDED':
                             WP_CLI::line(WP_CLI::colorize('%gRECOMMENDED:%n ' . trim($message)));
                             break;
                         case 'REQUIRED':
                             WP_CLI::line(WP_CLI::colorize('%rREQUIRED:%n ' . trim($message)));
                             break;
                     }
                 }
             }
         }
     }
 }
 /**
  * Show the Redis object cache status and (when possible) client.
  *
  * ## EXAMPLES
  *
  *     wp redis status
  *
  */
 public function status()
 {
     $plugin = $GLOBALS['redisObjectCache'];
     $status = $plugin->get_status();
     $client = $plugin->get_redis_client_name();
     switch ($status) {
         case __('Disabled', 'redis-cache'):
             $status = WP_CLI::colorize("%y{$status}%n");
             break;
         case __('Connected', 'redis-cache'):
             $status = WP_CLI::colorize("%g{$status}%n");
             break;
         case __('Not Connected', 'redis-cache'):
             $status = WP_CLI::colorize("%r{$status}%n");
             break;
     }
     WP_CLI::line('Status: ' . $status);
     if (!is_null($client)) {
         WP_CLI::line('Client: ' . $client);
     }
 }
 /**
  * Index all posts for a site or network wide
  *
  * @synopsis [--setup] [--network-wide] [--posts-per-page] [--no-bulk] [--offset] [--show-bulk-errors]
  * @param array $args
  *
  * @since 0.1.2
  *
  * @param array $assoc_args
  */
 public function index($args, $assoc_args)
 {
     $this->_connect_check();
     if (!empty($assoc_args['posts-per-page'])) {
         $assoc_args['posts-per-page'] = absint($assoc_args['posts-per-page']);
     } else {
         $assoc_args['posts-per-page'] = 350;
     }
     if (!empty($assoc_args['offset'])) {
         $assoc_args['offset'] = absint($assoc_args['offset']);
     } else {
         $assoc_args['offset'] = 0;
     }
     $total_indexed = 0;
     /**
      * Prior to the index command invoking
      * Useful for deregistering filters/actions that occur during a query request
      *
      * @since 1.4.1
      */
     do_action('ep_wp_cli_pre_index', $args, $assoc_args);
     // Deactivate our search integration
     $this->deactivate();
     timer_start();
     // Run setup if flag was passed
     if (isset($assoc_args['setup']) && true === $assoc_args['setup']) {
         // Right now setup is just the put_mapping command, as this also deletes the index(s) first
         $this->put_mapping($args, $assoc_args);
     }
     if (isset($assoc_args['network-wide']) && is_multisite()) {
         if (!is_numeric($assoc_args['network-wide'])) {
             $assoc_args['network-wide'] = 0;
         }
         WP_CLI::log(__('Indexing posts network-wide...', 'elasticpress'));
         $sites = ep_get_sites($assoc_args['network-wide']);
         foreach ($sites as $site) {
             switch_to_blog($site['blog_id']);
             $result = $this->_index_helper(isset($assoc_args['no-bulk']), $assoc_args['posts-per-page'], $assoc_args['offset'], isset($assoc_args['show-bulk-errors']));
             $total_indexed += $result['synced'];
             WP_CLI::log(sprintf(__('Number of posts synced on site %d: %d', 'elasticpress'), $site['blog_id'], $result['synced']));
             if (!empty($result['errors'])) {
                 WP_CLI::error(sprintf(__('Number of post sync errors on site %d: %d', 'elasticpress'), $site['blog_id'], count($result['errors'])));
             }
             restore_current_blog();
         }
         WP_CLI::log(__('Recreating network alias...', 'elasticpress'));
         $this->_create_network_alias();
         WP_CLI::log(sprintf(__('Total number of posts indexed: %d', 'elasticpress'), $total_indexed));
     } else {
         WP_CLI::log(__('Indexing posts...', 'elasticpress'));
         $result = $this->_index_helper(isset($assoc_args['no-bulk']), $assoc_args['posts-per-page'], $assoc_args['offset'], isset($assoc_args['show-bulk-errors']));
         WP_CLI::log(sprintf(__('Number of posts synced on site %d: %d', 'elasticpress'), get_current_blog_id(), $result['synced']));
         if (!empty($result['errors'])) {
             WP_CLI::error(sprintf(__('Number of post sync errors on site %d: %d', 'elasticpress'), get_current_blog_id(), count($result['errors'])));
         }
     }
     WP_CLI::log(WP_CLI::colorize('%Y' . __('Total time elapsed: ', 'elasticpress') . '%N' . timer_stop()));
     // Reactivate our search integration
     $this->activate();
     WP_CLI::success(__('Done!', 'elasticpress'));
 }
 /**
  * Get discount details for on your EDD site
  *
  * ## OPTIONS
  *
  * --id=<discount_id>: A specific discount ID to retrieve
  *
  * ## EXAMPLES
  *
  * wp edd discounts --id=103
  */
 public function discounts($args, $assoc_args)
 {
     $discount_id = isset($assoc_args) && array_key_exists('id', $assoc_args) ? absint($assoc_args['id']) : false;
     $discounts = $this->api->get_discounts($discount_id);
     if (isset($discounts['error'])) {
         WP_CLI::error($discounts['error']);
     }
     if (empty($discounts)) {
         WP_CLI::error(__('No discounts found', 'easy-digital-downloads'));
         return;
     }
     foreach ($discounts['discounts'] as $discount) {
         WP_CLI::line(WP_CLI::colorize('%G' . $discount['ID'] . '%N'));
         WP_CLI::line(sprintf(__('Name: %s', 'easy-digital-downloads'), $discount['name']));
         WP_CLI::line(sprintf(__('Code: %s', 'easy-digital-downloads'), $discount['code']));
         if ($discount['type'] == 'percent') {
             $amount = $discount['amount'] . '%';
         } else {
             $amount = edd_format_amount($discount['amount']) . ' ' . edd_get_currency();
         }
         WP_CLI::line(sprintf(__('Amount: %s', 'easy-digital-downloads'), $amount));
         WP_CLI::line(sprintf(__('Uses: %s', 'easy-digital-downloads'), $discount['uses']));
         WP_CLI::line(sprintf(__('Max Uses: %s', 'easy-digital-downloads'), $discount['max_uses'] == '0' ? __('Unlimited', 'easy-digital-downloads') : $discount['max_uses']));
         WP_CLI::line(sprintf(__('Start Date: %s', 'easy-digital-downloads'), empty($discount['start_date']) ? __('No Start Date', 'easy-digital-downloads') : $discount['start_date']));
         WP_CLI::line(sprintf(__('Expiration Date: %s', 'easy-digital-downloads'), empty($discount['exp_date']) ? __('No Expiration', 'easy-digital-downloads') : $discount['exp_date']));
         WP_CLI::line(sprintf(__('Status: %s', 'easy-digital-downloads'), ucwords($discount['status'])));
         WP_CLI::line('');
         if (array_key_exists(0, $discount['product_requirements'])) {
             WP_CLI::line(__('Product Requirements:', 'easy-digital-downloads'));
             foreach ($discount['product_requirements'] as $req => $req_id) {
                 WP_CLI::line(sprintf(__('  Product: %s', 'easy-digital-downloads'), $req_id));
             }
         }
         WP_CLI::line('');
         WP_CLI::line(sprintf(__('Global Discount: %s', 'easy-digital-downloads'), empty($discount['global_discount']) ? 'False' : 'True'));
         WP_CLI::line(sprintf(__('Single Use: %s', 'easy-digital-downloads'), empty($discount['single_use']) ? 'False' : 'True'));
         WP_CLI::line('');
     }
 }
Example #8
0
 protected function status_single($args)
 {
     $plugin = $this->fetcher->get_check($args[0]);
     $file = $plugin->file;
     $details = $this->get_details($file);
     $status = $this->format_status($this->get_status($file), 'long');
     $version = $details['Version'];
     if ($this->has_update($file)) {
         $version .= ' (%gUpdate available%n)';
     }
     echo WP_CLI::colorize(\WP_CLI\Utils\mustache_render('plugin-status.mustache', array('slug' => Utils\get_plugin_name($file), 'status' => $status, 'version' => $version, 'name' => $details['Name'], 'author' => $details['Author'], 'description' => $details['Description'])));
 }
Example #9
0
 /**
  * Create a bar that spans with width of the console
  *
  * ## OPTIONS
  *
  * [<character>]
  * : The character(s) to make the bar with. Default =
  *
  * [--c=<c>]
  * : Color for bar. Default %p
  *
  *
  * ## EXAMPLES
  *
  *     wp <command> bar
  *
  *     wp <command> bar '-~' --c='%r'
  *
  *     wp <command> bar '+-' --c='%r%3'
  */
 function bar($args = array(), $assoc_args = array())
 {
     $char = isset($args[0]) ? $args[0] : '=';
     $cols = \cli\Shell::columns();
     $line = substr(str_repeat($char, $cols), 0, $cols);
     if (!isset($assoc_args['c'])) {
         $color = '%p';
         // https://github.com/jlogsdon/php-cli-tools/blob/master/lib/cli/Colors.php#L113
     } else {
         $color = $assoc_args['c'];
         $color = '%' . trim($color, '%');
     }
     WP_CLI::line(WP_CLI::colorize($color . $line . '%n'));
 }
 /**
  * Write an error message to STDERR, prefixed with "Error: ".
  *
  * @param string $message Message to write.
  */
 public function error($message)
 {
     fwrite(STDERR, \WP_CLI::colorize("%RError:%n {$message}\n"));
 }
 /**
  *
  * Detects a database prefix mismatch and displays a CLI message about it. Does not interrupt the migration.
  *
  * @param $post_data
  *
  * @return bool
  */
 public function handle_prefix_mismatch($post_data, $profile)
 {
     global $wpdb;
     $local_prefix = $wpdb->base_prefix;
     $remote_prefix = $this->get_remote_prefix($post_data);
     $mismatch = null;
     $message = '';
     if (!empty($local_prefix) && !empty($remote_prefix)) {
         $mismatch = false;
         if ($local_prefix !== $remote_prefix) {
             $mismatch = true;
         }
     }
     $subsite_prefix_mismatch = $this->is_multisite_prefix_mismatch($post_data, $profile, $mismatch);
     if (true === $mismatch) {
         if (isset($post_data['intent'])) {
             $message = "%Y" . __("Database table prefix differs between installations.", 'wp-migrate-db-cli');
             $message .= "%n \n%R";
             if (true === $subsite_prefix_mismatch) {
                 $message .= sprintf(__("We have detected you have table prefix \"%s\" at %s but have \"%s\" here. Multisite Tools currently only supports migrating subsites between sites with the same base table prefix.", 'wp-migrate-db-cli'), $remote_prefix, $post_data['site_details']['remote']['site_url'], $wpdb->base_prefix);
             } else {
                 if ('push' == $post_data['intent']) {
                     $message .= sprintf(__("The remote database uses a prefix of \"%s\". This migration will create new tables in the remote database with a prefix of \"%s\".  \nTo use these new tables, AFTER the migration is complete, you will need to edit the wp-config.php file on the remote server and change the \$table_prefix variable to \"%s\"", 'wp-migrate-db-cli'), $remote_prefix, $wpdb->base_prefix, $wpdb->base_prefix);
                 } else {
                     if ('pull' == $post_data['intent']) {
                         $message .= sprintf(__("The local database uses a prefix of \"%s\". This migration will create new tables in the local database with a prefix of \"%s\".  \nTo use these new tables, AFTER the migration is complete, you will need to edit your wp-config.php file in your local environment and change the \$table_prefix variable to \"%s\"", 'wp-migrate-db-cli'), $wpdb->base_prefix, $remote_prefix, $remote_prefix);
                     }
                 }
             }
             $message .= "%n";
         }
         // Only display the CLI warning if invoked manually.
         if (!defined('DOING_WPMDB_TESTS')) {
             if (false === $subsite_prefix_mismatch) {
                 WP_CLI::warning(WP_CLI::colorize($message));
             } else {
                 WP_CLI::error(WP_CLI::colorize($message));
             }
         }
     }
     return $mismatch;
 }
Example #12
0
 /**
  * Take a walk through the history of a function.
  *
  * ## OPTIONS
  *
  * [--filename=<filename>]
  * : Absolute or ABSPATH-relative path to the file which contains this
  * function. Only necessary when the function you're blaming isn't loaded
  * in the current call.
  *
  * [--no-docblock]
  * : Exclude the docblock from the blame.
  *
  * ## EXAMPLES
  *
  *     wp svn blame get_post_meta
  *
  *     wp svn blame WP_Rewrite::generate_rewrite_rules
  *
  * @subcommand blame
  * @synopsis <function> [--filename=<filename>] [--no-docblock]
  */
 public function blame($args, $assoc_args)
 {
     $assoc_args = array_merge(array('filename' => false, 'docblock' => true), $assoc_args);
     $func = explode('::', $args[0], 2);
     $revision = false;
     $prompt = 'b';
     $do_docblock = false;
     if (2 === count($func)) {
         $function_name = $func[1];
     } else {
         $function_name = $func[0];
     }
     while ('q' != $prompt) {
         switch ($prompt) {
             case 'v':
                 $url = sprintf('https://core.trac.wordpress.org/changeset/%d', $latest);
                 $cmd = Utils\esc_cmd('open %s', $url);
                 WP_CLI::line('Opening changeset in your browser');
                 $text = self::launch_command($cmd);
                 if (is_wp_error($text)) {
                     WP_CLI::error($text);
                 }
                 break;
             case 'g':
                 $go = intval($this->prompt('Enter revision number', ''));
                 if (!$go) {
                     break;
                 } else {
                     $revision = $go;
                     // fall through
                 }
             case 'b':
                 // ----------------------------------
                 if (!$revision && !$assoc_args['filename']) {
                     try {
                         if (2 === count($func)) {
                             $ref = new ReflectionMethod($func[0], $func[1]);
                         } else {
                             $ref = new ReflectionFunction($func[0]);
                         }
                         $filename = $ref->getFileName();
                     } catch (ReflectionException $e) {
                         // @TODO message about using the --filename arg
                         WP_CLI::error($e->getMessage());
                     }
                 } else {
                     if ($assoc_args['filename']) {
                         if (file_exists($assoc_args['filename'])) {
                             $filename = $assoc_args['filename'];
                         } elseif (file_exists(ABSPATH . $assoc_args['filename'])) {
                             $filename = ABSPATH . $assoc_args['filename'];
                         } else {
                             WP_CLI::error(sprintf("File '%s' does not exist", $assoc_args['filename']));
                         }
                     }
                 }
                 if ($revision) {
                     $cmd = Utils\esc_cmd('svn blame %s@%s', $filename, $revision);
                     WP_CLI::line(sprintf('Fetching r%d...', $revision));
                     WP_CLI::line('This can be *very* slow for large files');
                 } else {
                     $cmd = Utils\esc_cmd('svn blame %s', $filename);
                 }
                 $text = self::launch_command($cmd);
                 if (is_wp_error($text)) {
                     WP_CLI::error($text);
                 }
                 $text = str_replace(array("\r\n", "\r"), "\n", $text);
                 $data = explode("\n", "\n" . $text);
                 $lazy_explode = explode(sprintf('function %s', $function_name), $text, 2);
                 $start_line = substr_count($lazy_explode[0], "\n") + 1;
                 $func_onwards = explode("\n", $lazy_explode[1]);
                 $func_length = 0;
                 foreach ($func_onwards as $k => $v) {
                     if (18 === strpos($v, '}')) {
                         $func_length = $k;
                         break;
                     }
                 }
                 if (empty($func_length)) {
                     WP_CLI::error('¯\\_(ツ)_/¯');
                 }
                 $do_docblock = $assoc_args['docblock'] && ' */' === substr($data[$start_line - 1], 18, 3);
                 if ($do_docblock) {
                     while ('/**' !== substr($data[$start_line], 18, 3)) {
                         $start_line--;
                         $func_length++;
                     }
                 }
                 $data = $raw_data = array_slice($data, $start_line, $func_length + 1, true);
                 $revisions = array();
                 $data = array_map(function ($line, $value) use(&$revisions) {
                     if (preg_match('|^[ ]*([0-9]+)|', $value, $matches)) {
                         $revisions[$line] = intval($matches[1]);
                     }
                     return $value;
                 }, array_keys($data), $data);
                 $revision_list = array_unique($revisions);
                 rsort($revision_list);
                 $latest = $revision_list[0];
                 $out = array_map(function ($line, $value) use($revision_list) {
                     $n = str_pad($line, 4, ' ', STR_PAD_LEFT);
                     $return = "{$n} | {$value}";
                     if (preg_match('|^[ ]*([0-9]+)|', $value, $matches)) {
                         if ($matches[1] == $revision_list[0]) {
                             $return = WP_CLI::colorize('%K' . $return . '%n');
                         }
                     }
                     return $return;
                 }, array_keys($raw_data), $data);
                 WP_CLI::line(str_repeat('-', 100));
                 if ($revision) {
                     WP_CLI::line(sprintf('%s() at r%s', $function_name, $revision));
                 } else {
                     WP_CLI::line(sprintf('%s() in your working copy', $function_name));
                 }
                 WP_CLI::line(str_repeat('-', 100));
                 WP_CLI::line('');
                 WP_CLI::line(implode("\n", $out));
                 WP_CLI::line('');
                 WP_CLI::line(str_repeat('-', 100));
                 $revision = $revision_list[1];
                 break;
         }
         WP_CLI::line('');
         WP_CLI::line(sprintf('[v] View changeset in your browser (r%d)', $latest));
         WP_CLI::line(sprintf('[b] Back in history (r%d)', $revision));
         WP_CLI::line('[g] Go directly to a specified revision');
         WP_CLI::line('[q] Quit');
         WP_CLI::line('');
         $prompt = strtolower($this->prompt('What next?', '?'));
     }
     WP_CLI::success('DONE');
 }
Example #13
0
 /**
  * Index all posts for a site or network wide
  *
  * @synopsis [--setup] [--network-wide] [--posts-per-page] [--no-bulk]
  * @param array $args
  *
  * @since 0.1.2
  *
  * @param array $assoc_args
  */
 public function index($args, $assoc_args)
 {
     $this->_connect_check();
     if (!empty($assoc_args['posts-per-page'])) {
         $assoc_args['posts-per-page'] = absint($assoc_args['posts-per-page']);
     } else {
         $assoc_args['posts-per-page'] = 350;
     }
     $total_indexed = 0;
     // Deactivate our search integration
     $this->deactivate();
     timer_start();
     // Run setup if flag was passed
     if (isset($assoc_args['setup']) && true === $assoc_args['setup']) {
         // Right now setup is just the put_mapping command, as this also deletes the index(s) first
         $this->put_mapping($args, $assoc_args);
     }
     if (!empty($assoc_args['network-wide']) && is_multisite()) {
         \WP_CLI::log(__('Indexing posts network-wide...', 'elasticpress'));
         $sites = ep_get_sites();
         foreach ($sites as $site) {
             switch_to_blog($site['blog_id']);
             $result = $this->_index_helper(isset($assoc_args['no-bulk']), $assoc_args['posts-per-page']);
             $total_indexed += $result['synced'];
             \WP_CLI::log(sprintf(__('Number of posts synced on site %d: %d', 'elasticpress'), $site['blog_id'], $result['synced']));
             if (!empty($result['errors'])) {
                 \WP_CLI::error(sprintf(__('Number of post sync errors on site %d: %d', 'elasticpress'), $site['blog_id'], count($result['errors'])));
             }
             restore_current_blog();
         }
         \WP_CLI::log(__('Recreating network alias...', 'elasticpress'));
         $this->_create_network_alias();
         \WP_CLI::log(sprintf(__('Total number of posts indexed: %d', 'elasticpress'), $total_indexed));
     } else {
         \WP_CLI::log(__('Indexing posts...', 'elasticpress'));
         $result = $this->_index_helper(isset($assoc_args['no-bulk']), $assoc_args['posts-per-page']);
         \WP_CLI::log(sprintf(__('Number of posts synced on site %d: %d', 'elasticpress'), get_current_blog_id(), $result['synced']));
         if (!empty($result['errors'])) {
             \WP_CLI::error(sprintf(__('Number of post sync errors on site %d: %d', 'elasticpress'), get_current_blog_id(), count($result['errors'])));
         }
     }
     \WP_CLI::log(\WP_CLI::colorize('%Y' . __('Total time elapsed: ', 'elasticpress') . '%N' . timer_stop()));
     // Reactivate our search integration
     $this->activate();
     \WP_CLI::success(__('Done!', 'elasticpress'));
 }
Example #14
0
 /**
  * Allows us to log if we are using WP_CLI to fire this cron task
  *
  * @see    http://wp-cli.org/docs/internal-api/#output
  *
  * @param  string $type    What kind of log is this
  * @param  string $message message displayed
  *
  * @return void
  */
 public function log($type = 'colorize', $message = '')
 {
     // Log on our Structure
     Tribe__Main::instance()->log()->log_debug($message, 'aggregator');
     // Only go further if we have WP_CLI
     if (!class_exists('WP_CLI')) {
         return false;
     }
     switch ($type) {
         case 'error':
             WP_CLI::error($message);
             break;
         case 'warning':
             WP_CLI::warning($message);
             break;
         case 'success':
             WP_CLI::success($message);
             break;
         case 'debug':
             WP_CLI::debug($message, 'aggregator');
             break;
         case 'colorize':
         default:
             WP_CLI::log(WP_CLI::colorize($message));
             break;
     }
 }
Example #15
0
 /**
  * Create a bar that spans with width of the console
  *
  * @param array $args Only expects a zero-indexed value, the character to build the bar with
  * @param array $assoc_args
  *               string  'c'    Color value. Default %p
  *               integer 'w'    Width percentage, 0-100. Default 100
  *               string  'text' Message to show in bar
  */
 private function bar($args = array(), $assoc_args = array())
 {
     $char = isset($args[0]) ? $args[0] : '=';
     $cols = \cli\Shell::columns();
     if (isset($assoc_args['w'])) {
         $cols = floor($cols * ($assoc_args['w'] / 100));
     }
     $line = substr(str_repeat($char, $cols), 0, $cols);
     if (isset($assoc_args['text'])) {
         $text = "{$char}{$char}{$char} {$assoc_args['text']} ";
         $len = strlen($text);
         $line = $text . substr($line, $len);
     }
     if (!isset($assoc_args['c'])) {
         $color = '%p';
         // https://github.com/jlogsdon/php-cli-tools/blob/master/lib/cli/Colors.php#L113
     } else {
         $color = $assoc_args['c'];
         $color = '%' . trim($color, '%');
     }
     WP_CLI::log(WP_CLI::colorize($color . $line . '%n'));
 }
Example #16
0
 protected function status_single($args)
 {
     $theme = $this->fetcher->get_check($args[0]);
     $status = $this->format_status($this->get_status($theme), 'long');
     $version = $theme->get('Version');
     if ($this->has_update($theme->get_stylesheet())) {
         $version .= ' (%gUpdate available%n)';
     }
     echo WP_CLI::colorize(\WP_CLI\Utils\mustache_render('theme-status.mustache', array('slug' => $theme->get_stylesheet(), 'status' => $status, 'version' => $version, 'name' => $theme->get('Name'), 'author' => $theme->get('Author'))));
 }
Example #17
0
 /**
  * Similar to error( $message ), but outputs $message in a red box
  *
  * @param  array $message Message to write.
  */
 public function error_multi_line($message_lines)
 {
     $message = implode("\n", $message_lines);
     $this->write(STDERR, \WP_CLI::colorize("%RError:%n\n{$message}\n"));
     $this->write(STDERR, \WP_CLI::colorize("%R---------%n\n\n"));
 }