Пример #1
0
 /**
  * List all metadata associated with an object.
  *
  * <id>
  * : ID for the object.
  *
  * [--keys=<keys>]
  * : Limit output to metadata of specific keys.
  *
  * [--fields=<fields>]
  * : Limit the output to specific row fields. Defaults to id,meta_key,meta_value.
  *
  * [--format=<format>]
  * : Accepted values: table, csv, json, count. Default: table
  *
  * @subcommand list
  */
 public function list_($args, $assoc_args)
 {
     list($object_id) = $args;
     $keys = !empty($assoc_args['keys']) ? explode(',', $assoc_args['keys']) : array();
     $object_id = $this->check_object_id($object_id);
     $metadata = get_metadata($this->meta_type, $object_id);
     if (!$metadata) {
         $metadata = array();
     }
     $items = array();
     foreach ($metadata as $key => $values) {
         // Skip if not requested
         if (!empty($keys) && !in_array($key, $keys)) {
             continue;
         }
         foreach ($values as $item_value) {
             $item_value = maybe_unserialize($item_value);
             $items[] = (object) array("{$this->meta_type}_id" => $object_id, 'meta_key' => $key, 'meta_value' => $item_value);
         }
     }
     if (!empty($assoc_args['fields'])) {
         $fields = explode(',', $assoc_args['fields']);
     } else {
         $fields = $this->get_fields();
     }
     $formatter = new \WP_CLI\Formatter($assoc_args, $fields, $this->meta_type);
     $formatter->display_items($items);
 }
 /**
  * Display a list of mappings
  *
  * @param Network_Mapping[] $mappings Mapping objects to show
  * @param array $options
  */
 protected function display($mappings, $options)
 {
     $defaults = array('format' => 'table', 'fields' => array('id', 'domain', 'network', 'active'));
     $options = wp_parse_args($options, $defaults);
     $mapper = function (Network_Mapping $mapping) {
         $data = array('id' => (int) $mapping->get_id(), 'domain' => $mapping->get_domain(), 'network' => (int) $mapping->get_network_id(), 'active' => $mapping->is_active() ? __('Active', 'mercator') : __('Inactive', 'mercator'));
         return apply_filters('mercator.cli.mapping.fields', $data, $mapping);
     };
     $display_items = Utils\iterator_map($mappings, $mapper);
     $formatter = new Formatter($options);
     $formatter->display_items($display_items);
 }
 /**
  * List the defined variables from the environment file
  *
  * [--format=<format>]
  * : Accepted values: table, csv, json, count. Default: table
  *
  * [--file=<path-to-dotenv>]
  * : Path to the environment file.  Default: '.env'
  *
  * @subcommand list
  * @when before_wp_load
  *
  * @param $_
  * @param $assoc_args
  */
 public function _list($_, $assoc_args)
 {
     $dotenv = get_dotenv_for_read_or_fail($assoc_args);
     $keys = \WP_CLI\Utils\get_flag_value($assoc_args, 'keys');
     $keys = is_string($keys) ? explode(',', $keys) : $keys;
     $items = [];
     foreach ($dotenv->get_pairs() as $key => $value) {
         // Skip if not requested
         if (!empty($keys) && !in_array($key, $keys)) {
             continue;
         }
         $items[] = (object) compact('key', 'value');
     }
     $fields = \WP_CLI\Utils\get_flag_value($assoc_args, 'fields', ['key', 'value']);
     $fields = is_string($fields) ? explode(',', $fields) : $fields;
     $formatter = new Formatter($assoc_args, $fields);
     $formatter->display_items($items);
 }
Пример #4
0
 /**
  * View IP address rules.
  */
 public function status($args, $assoc_args)
 {
     $items = [];
     $labels = [esc_html__('IP Address', 'wprestcop'), esc_html__('Action', 'wprestcop'), esc_html__('Source', 'wprestcop')];
     $option = get_option('wprestcop_allowed_ips', []);
     $action_l10n = esc_html('ALLOW', 'wprestcop');
     foreach (wprestcop()->get_ip_rules()->get_allowed() as $ip) {
         $source = 'code';
         if (in_array($ip, $option)) {
             $source = 'option';
         }
         $items[] = array_combine($labels, [$ip, $action_l10n, $source]);
     }
     $option = get_option('wprestcop_denied_ips', []);
     $action_l10n = esc_html__('DENY', 'wprestcop');
     foreach (wprestcop()->get_ip_rules()->get_denied() as $ip) {
         $source = 'code';
         if (in_array($ip, $option)) {
             $source = 'option';
         }
         $items[] = array_combine($labels, [$ip, $action_l10n, $source]);
     }
     $format_args = [];
     $formatter = new WP_CLI\Formatter($format_args, $labels);
     $formatter->display_items($items);
 }
Пример #5
0
/**
 * Render a collection of items as an ASCII table, JSON, CSV, YAML, list of ids, or count.
 *
 * Given a collection of items with a consistent data structure:
 *
 * ```
 * $items = array(
 *     array(
 *         'key'   => 'foo',
 *         'value'  => 'bar',
 *     )
 * );
 * ```
 *
 * Render `$items` as an ASCII table:
 *
 * ```
 * WP_CLI\Utils\format_items( 'table', $items, array( 'key', 'value' ) );
 *
 * # +-----+-------+
 * # | key | value |
 * # +-----+-------+
 * # | foo | bar   |
 * # +-----+-------+
 * ```
 *
 * Or render `$items` as YAML:
 *
 * ```
 * WP_CLI\Utils\format_items( 'yaml', $items, array( 'key', 'value' ) );
 *
 * # ---
 * # -
 * #   key: foo
 * #   value: bar
 * ```
 *
 * @access public
 * @category Output
 *
 * @param string        $format     Format to use: 'table', 'json', 'csv', 'yaml', 'ids', 'count'
 * @param array         $items      An array of items to output.
 * @param array|string  $fields     Named fields for each item of data. Can be array or comma-separated list.
 * @return null
 */
function format_items($format, $items, $fields)
{
    $assoc_args = compact('format', 'fields');
    $formatter = new \WP_CLI\Formatter($assoc_args);
    $formatter->display_items($items);
}
 /**
  * List the defined variables from the environment file
  *
  * [<pattern>...]
  * : Key names or glob-style patterns to match.
  *
  * [--format=<format>]
  * : Accepted values: table, csv, json, count. Default: table
  *
  * [--file=<path-to-dotenv>]
  * : Path to the environment file.  Default: '.env'
  *
  * @subcommand list
  * @when       before_wp_load
  *
  * @param array $patterns   Glob-style patterns to match against env keys
  * @param array $assoc_args
  */
 public function _list($patterns, $assoc_args)
 {
     $this->init_args(func_get_args());
     $env = $this->get_env_for_read_or_fail();
     $items = $env->dictionaryWithKeysMatching($patterns)->map(function ($value, $key) {
         return (object) compact('key', 'value');
     });
     $args = $this->args->toArray();
     // var required - passed by reference
     $formatter = new Formatter($args, $this->fields());
     $formatter->display_items($items->all());
 }
Пример #7
0
 /**
  * List all capabilities and show roles that have capability.
  *
  * ## OPTIONS
  *
  * [--fields=<fields>]
  * : Limit the output to specific object fields.
  *
  * [--format=<format>]
  * : Accepted values: table, csv, json, count. Default: table
  *
  * ## AVAILABLE FIELDS
  *
  * These fields will be displayed by default for each role:
  *
  * * capabilities
  * * [any of the roles]
  *
  * There are no optional fields.
  *
  * ## EXAMPLES
  *
  *     wp rights list-cap --fields=role --format=csv
  *
  * @subcommand list-cap
  */
 public function list_cap($args, $assoc_args)
 {
     global $wp_roles;
     $output_matrix = [];
     $roles = array_keys($wp_roles->role_names);
     $fields = array_merge(['capabilities'], $roles);
     $row_keys = array_fill_keys($roles, null);
     foreach ($wp_roles->roles as $key => $role) {
         foreach ($role['capabilities'] as $capability => $has_it) {
             if (!isset($output_matrix[$capability])) {
                 $output_matrix[$capability] = array_merge(['capabilities' => $capability], $row_keys);
             }
             $output_matrix[$capability][$key] = 'Y';
         }
     }
     $formatter = new Formatter($assoc_args, $fields);
     $formatter->display_items($output_matrix);
 }