Display a value, in various formats
public static print_value ( mixed $value, array $assoc_args = [] ) | ||
$value | mixed | Value to display. |
$assoc_args | array | Arguments passed to the command, determining format. |
/** * Verifies the API keys entered will work for writing and deleting from S3. * * @subcommand verify */ public function verify_api_keys() { S3_Uploads::get_instance(); // Boot $upload_dir = wp_upload_dir(); // The upload file location on the local filesystem $s3_path = $upload_dir['basedir'] . '/' . mt_rand() . '.jpg'; // Attempt to copy the file to S3 WP_CLI::print_value('Attempting to upload file ' . $s3_path); // Copy canola from the test dir, upto S3 $copy = copy(dirname(dirname(__FILE__)) . '/tests/data/canola.jpg', $s3_path); // Check that copy worked if (!$copy) { WP_CLI::error('Failed to copy / write to S3 - check your policy?'); return; } WP_CLI::print_value('File uploaded to S3 successfully'); // Delete off S3 WP_CLI::print_value('Attempting to delete file ' . $s3_path); $delete = unlink($s3_path); // Check that delete worked if (!$delete) { WP_CLI::error('Failed to delete ' . $s3_path); return; } WP_CLI::print_value('File deleted from S3 successfully'); WP_CLI::success('Looks like your configuration is correct.'); }
/** * Perform an option action on a remote site */ private function perform_option_action($action, $args, $assoc_args) { $site_id = $assoc_args['site-id']; unset($assoc_args['site-id']); list($option_name) = $args; $this->set_account(); $method = strtoupper($action); if ('update' == $action) { $method = 'POST'; $api_args = array('option_value' => WP_CLI::read_value($args[1], $assoc_args)); } else { $api_args = array(); } $args = array('endpoint' => 'site/' . (int) $site_id . '/option/' . $option_name, 'method' => $method, 'body' => $api_args); $response = $this->api_request($args); if (is_wp_error($response)) { WP_CLI::error($response->get_error_message()); } switch ($action) { case 'get': if (empty($response)) { die(1); } WP_CLI::print_value($response, $assoc_args); break; case 'update': WP_CLI::success("Updated '{$option_name}' option."); break; case 'delete': WP_CLI::success("Deleted '{$option_name}' option."); break; } }
/** * Verifies the API keys entered will work for writing and deleting from S3. * * @subcommand verify */ public function verify_api_keys() { // Verify first that we have the necessary access keys to connect to S3. if (!$this->verify_s3_access_constants()) { return; } // Get S3 Upload instance. $instance = S3_Uploads::get_instance(); // Create a path in the base directory, with a random file name to avoid potentially overwriting existing data. $upload_dir = wp_upload_dir(); $s3_path = $upload_dir['basedir'] . '/' . mt_rand() . '.jpg'; // Attempt to copy the local Canola test file to the generated path on S3. WP_CLI::print_value('Attempting to upload file ' . $s3_path); $copy = copy(dirname(dirname(__FILE__)) . '/tests/data/canola.jpg', $s3_path); // Check that the copy worked. if (!$copy) { WP_CLI::error('Failed to copy / write to S3 - check your policy?'); return; } WP_CLI::print_value('File uploaded to S3 successfully.'); // Delete the file off S3. WP_CLI::print_value('Attempting to delete file. ' . $s3_path); $delete = unlink($s3_path); // Check that the delete worked. if (!$delete) { WP_CLI::error('Failed to delete ' . $s3_path); return; } WP_CLI::print_value('File deleted from S3 successfully.'); WP_CLI::success('Looks like your configuration is correct.'); }
/** * Emit the message in specified format * * @params $format string optional - options are "raw","json" */ public static function emit($format = 'raw') { $messenger = self::instance(); switch ($format) { case 'pantheon': case 'json': $formatted = array(); foreach ($messenger->messages as $message) { $formatted[$message['name']] = $message; } \WP_CLI::print_value($formatted, array('format' => 'json')); break; case 'raw': case 'default': foreach ($messenger->messages as $message) { // colorize if ($message['score'] == 2) { $color = "%G"; } elseif ($message['score'] == 0) { $color = "%C"; } else { $color = "%R"; } // @todo might be a better way to do this echo \cli\Colors::colorize(sprintf(str_repeat('-', 80) . PHP_EOL . "%s: (%s) \n%s\nResult:%s %s\nRecommendation: %s\n\n" . PHP_EOL, strtoupper($message['label']), $message['description'], str_repeat('-', 80), $color, $message['result'] . '%n', $message['action'])); } break; } }
/** * Debug object cache hit / miss ratio for any page URL. * * ## OPTIONS * * [--url=<url>] * : Execute a request against a specified URL. Defaults to home_url( '/' ). * * [--format=<format>] * : Render the results in a particular format. * * @when before_wp_load */ public function debug($_, $assoc_args) { global $wp_object_cache; $this->load_wordpress_with_template(); $data = array('cache_hits' => $wp_object_cache->cache_hits, 'cache_misses' => $wp_object_cache->cache_misses, 'redis_calls' => $wp_object_cache->redis_calls); WP_CLI::print_value($data, $assoc_args); }
/** * Get an option. * * @synopsis <key> [--format=<format>] */ public function get($args, $assoc_args) { list($key) = $args; $value = get_option($key); if (false === $value) { die(1); } WP_CLI::print_value($value, $assoc_args); }
/** * Get meta field value. * * @synopsis <id> <key> [--format=<format>] */ public function get($args, $assoc_args) { list($object_id, $meta_key) = $args; $value = \get_metadata($this->meta_type, $object_id, $meta_key, true); if ('' === $value) { die(1); } \WP_CLI::print_value($value, $assoc_args); }
/** * Get a transient value. * * @synopsis <key> [--json] */ public function get($args, $assoc_args) { list($key) = $args; $value = get_transient($key); if (false === $value) { WP_CLI::warning('Transient with key "' . $key . '" is not set.'); exit; } WP_CLI::print_value($value, $assoc_args); }
/** * Get meta field value * * @param array $args * @param array $assoc_args **/ public function get($args, $assoc_args) { $object_id = WP_CLI::get_numeric_arg($args, 0, "{$this->meta_type} ID"); $meta_key = self::get_arg_or_error($args, 1, "meta_key"); $value = get_metadata($this->meta_type, $object_id, $meta_key, true); if ('' === $value) { die(1); } WP_CLI::print_value($value, $assoc_args); }
/** * Get a transient value. * * ## OPTIONS * * <key> * : Key for the transient. * * [--format=<format>] * : Render output in a particular format. * --- * default: table * options: * - table * - csv * - json * - yaml * --- * * [--network] * : Get the value of the network transient, instead of the single site. * * ## EXAMPLES * * $ wp transient get sample_key * test data * * $ wp transient get random_key * Warning: Transient with key "random_key" is not set. */ public function get($args, $assoc_args) { list($key) = $args; $func = \WP_CLI\Utils\get_flag_value($assoc_args, 'network') ? 'get_site_transient' : 'get_transient'; $value = $func($key); if (false === $value) { WP_CLI::warning('Transient with key "' . $key . '" is not set.'); exit; } WP_CLI::print_value($value, $assoc_args); }
public function add_posts($args, $assoc_args) { $i = 0; $wp_cat_id = null; if (empty($this->mt_data)) { $this->init($args[0], $args[1]); } foreach ($this->mt_data->entry as $mt_entry) { $mt_entry = (array) $mt_entry; $mt_entry_atts = $mt_entry['@attributes']; if (!empty($mt_entry_atts['title']) && $i < $this->import_limit) { $date_stamp = strtotime($mt_entry_atts['authored_on']); $postdate = date("Y-m-d H:i:s", $date_stamp); $args = array('post_title' => $mt_entry_atts['title'], 'post_author' => $this->author_id, 'post_date' => $postdate, 'post_status' => 'publish', 'post_type' => 'property', 'post_excerpt' => !empty($mt_entry['excerpt']) ? $mt_entry['excerpt'] : '', 'post_content' => !empty($mt_entry['text']) ? $mt_entry['text'] : ''); $mt_entry_cats = $this->get_mt_categories($mt_entry_atts['id']); if (!empty($mt_entry_cats) && in_array(5, $mt_entry_cats)) { $args['post_type'] = 'post'; WP_CLI::print_value($mt_entry); WP_CLI::print_value($args); } // $post_id = $this->create_post( $args ); // $this->add_post_to_category( $post_id, $mt_entry_cats ); // if ( ! in_array( 5, $mt_entry_cats ) ) { // $post_meta = array( // 'name' => ! empty( $mt_entry_atts['field.property-name'] ) ? $mt_entry_atts['field.property-name'] : '', // 'architect' => ! empty( $mt_entry_atts['field.property-architect'] ) ? $mt_entry_atts['field.property-architect'] : '', // 'address' => ! empty( $mt_entry['field.property-address'] ) ? $mt_entry['field.property-address'] : '', // 'price' => ! empty( $mt_entry_atts['field.property-price'] ) ? $mt_entry_atts['field.property-price'] : '', // 'status' => ! empty( $mt_entry_atts['field.property-status'] ) ? $mt_entry_atts['field.property-status'] : '', // 'listing-number' => ! empty( $mt_entry_atts['field.property-listing-number'] ) ? $mt_entry_atts['field.property-listing-number'] : '', // 'listing-provided-by' => ! empty( $mt_entry_atts['field.property-listing-provided-by'] ) ? $mt_entry_atts['field.property-listing-provided-by'] : '', // 'type' => ! empty( $mt_entry_atts['field.property-type'] ) ? $mt_entry_atts['field.property-type'] : '', // 'bedrooms' => ! empty( $mt_entry_atts['field.property-bedrooms'] ) ? $mt_entry_atts['field.property-bedrooms'] : '', // 'bathrooms' => ! empty( $mt_entry_atts['field.property-bathrooms'] ) ? $mt_entry_atts['field.property-bathrooms'] : '', // 'square-footage' => ! empty( $mt_entry_atts['field.property-square-footage'] ) ? $mt_entry_atts['field.property-square-footage'] : '', // 'lot-size' => ! empty( $mt_entry_atts['field.property-lot-size'] ) ? $mt_entry_atts['field.property-lot-size'] : '', // 'year-built' => ! empty( $mt_entry_atts['field.property-year-built'] ) ? $mt_entry_atts['field.property-year-built'] : '', // 'parking' => ! empty( $mt_entry_atts['field.property-parking'] ) ? $mt_entry_atts['field.property-parking'] : '', // 'fireplaces' => ! empty( $mt_entry_atts['field.property-fireplaces'] ) ? $mt_entry_atts['field.property-fireplaces'] : '', // 'amenities' => ! empty( $mt_entry['field.property-amenities'] ) ? $mt_entry['field.property-amenities'] : '', // 'appliances' => ! empty( $mt_entry['field.property-appliances'] ) ? $mt_entry['field.property-appliances'] : '', // 'features' => ! empty( $mt_entry['field.property-featured'] ) ? $mt_entry['field.property-features'] : '' // ); // $this->set_post_meta( $post_id, $post_meta ); // $this->add_images( $post_id, $mt_entry_atts['id'] ); // } else { // WP_CLI::line( 'blog post' ); // } } $i++; } }
/** * Get an option * * @param array $args **/ public function get($args, $assoc_args) { if (empty($args)) { WP_CLI::line("usage: wp option get <option-name>"); exit; } list($key) = $args; $value = get_option($key); if (false === $value) { die(1); } WP_CLI::print_value($value, $assoc_args); }
/** * Increment a value in the object cache. * * @synopsis <key> [<offset>] [<group>] */ public function incr($args, $assoc_args) { $key = $args[0]; $offset = isset($args[1]) ? $args[1] : 1; $group = isset($args[2]) ? $args[2] : ''; $value = wp_cache_incr($key, $offset, $group); if (false === $value) { WP_CLI::error('The value was not incremented.'); } WP_CLI::print_value($value, $assoc_args); }
/** * Show multiple fields of an object. * * @param object|array Data to display * @param string Format to display the data in */ private function show_multiple_fields($data, $format) { $true_fields = array(); foreach ($this->args['fields'] as $field) { $true_fields[] = $this->find_item_key($data, $field); } foreach ($data as $key => $value) { if (!in_array($key, $true_fields)) { if (is_array($data)) { unset($data[$key]); } else { if (is_object($data)) { unset($data->{$key}); } } } } switch ($format) { case 'table': case 'csv': $rows = $this->assoc_array_to_rows($data); $fields = array('Field', 'Value'); if ('table' == $format) { self::show_table($rows, $fields); } else { if ('csv' == $format) { \WP_CLI\Utils\write_csv(STDOUT, $rows, $fields); } } break; case 'yaml': case 'json': \WP_CLI::print_value($data, array('format' => $format)); break; default: \WP_CLI::error("Invalid format: " . $format); break; } }
/** * Print to WP_CLI if in CLI environment or * write to debug.log if WP_DEBUG is enabled * @source http://www.stumiller.me/sending-output-to-the-wordpress-debug-log/ * * @param mixed $msg * @param string $write */ public static function write_log($msg, $write = 'line') { if (defined('WP_CLI') && WP_CLI) { if (is_array($msg) || is_object($msg)) { WP_CLI::print_value($msg); } else { WP_CLI::$write($msg); } } elseif (true === WP_DEBUG) { if (is_array($msg) || is_object($msg)) { error_log(print_r($msg, true)); } else { error_log($msg); } } }
/** * List available WP-CLI aliases. * * Aliases are shorthand references to WordPress installs. For instance, * `@dev` could refer to a development install and `@prod` could refer to * a production install. This command gives you visibility in what * registered aliases you have available. * * ## OPTIONS * * [--format=<format>] * : Render output in a particular format. * --- * default: yaml * options: * - yaml * - json * --- * * ## EXAMPLES * * # List all available aliases. * $ wp cli alias * --- * @all: Run command against every registered alias. * @prod: * ssh: runcommand@runcommand.io~/webapps/production * @dev: * ssh: vagrant@192.168.50.10/srv/www/runcommand.dev * @both: * - @prod * - @dev * * @alias aliases */ public function alias($_, $assoc_args) { WP_CLI::print_value(WP_CLI::get_runner()->aliases, $assoc_args); }
/** * Increment a value in the object cache. * * Errors if the value can't be incremented. * * ## OPTIONS * * <key> * : Cache key. * * [<offset>] * : The amount by which to increment the item's value. Default is 1. * * [<group>] * : Method for grouping data within the cache which allows the same key to be used across groups. * * ## EXAMPLES * * # Increase cache value. * $ wp cache incr my_key 2 my_group * 50 */ public function incr($args, $assoc_args) { $key = $args[0]; $offset = \WP_CLI\Utils\get_flag_value($args, 1, 1); $group = \WP_CLI\Utils\get_flag_value($args, 2, ''); $value = wp_cache_incr($key, $offset, $group); if (false === $value) { WP_CLI::error('The value was not incremented.'); } WP_CLI::print_value($value, $assoc_args); }
protected function show_single_field($obj, $field) { $value = null; foreach (array($field, $this->obj_type . '_' . $field) as $key) { if (isset($obj->{$key})) { $value = $obj->{$key}; break; } } if (null === $value) { \WP_CLI::error("Invalid {$this->obj_type} field: {$field}."); } else { \WP_CLI::print_value($value); } }
/** * Attempts to determine which object cache is being used. * * Note that the guesses made by this function are based on the WP_Object_Cache classes * that define the 3rd party object cache extension. Changes to those classes could render * problems with this function's ability to determine which object cache is being used. * * @uses $_wp_using_ext_object_cache * @uses $wp_object_cache * * @param array $args Function arguments. * @param array $assoc_args Function arguments with parameter key. * @return void */ public function type($args, $assoc_args) { global $_wp_using_ext_object_cache, $wp_object_cache; if (false !== $_wp_using_ext_object_cache) { // Test for Memcached PECL extension memcached object cache (https://github.com/tollmanz/wordpress-memcached-backend) if (isset($wp_object_cache->m) && is_a($wp_object_cache->m, 'Memcached')) { $message = 'Memcached'; // Test for Memcache PECL extension memcached object cache (http://wordpress.org/extend/plugins/memcached/) } elseif (isset($wp_object_cache->mc)) { $is_memcache = true; foreach ($wp_object_cache->mc as $bucket) { if (!is_a($bucket, 'Memcache')) { $is_memcache = false; } } if ($is_memcache) { $message = 'Memcache'; } // Test for Xcache object cache (http://plugins.svn.wordpress.org/xcache/trunk/object-cache.php) } elseif (is_a($wp_object_cache, 'XCache_Object_Cache')) { $message = 'Xcache'; // Test for WinCache object cache (http://wordpress.org/extend/plugins/wincache-object-cache-backend/) } elseif (class_exists('WinCache_Object_Cache')) { $message = 'WinCache'; // Test for APC object cache (http://wordpress.org/extend/plugins/apc/) } elseif (class_exists('APC_Object_Cache')) { $message = 'APC'; } else { $message = 'Unknown'; } } else { $message = 'Default'; } WP_CLI::print_value($message); }
/** * Show multiple fields of an object. * * @param object|array Data to display * @param string Format to display the data in */ private static function show_multiple_fields($data, $format) { switch ($format) { case 'table': self::assoc_array_to_table($data); break; case 'json': \WP_CLI::print_value($data, array('format' => $format)); break; default: \WP_CLI::error("Invalid format: " . $format); break; } }