Beispiel #1
0
 /**
  * List users.
  *
  * ## OPTIONS
  *
  * [--role=<role>]
  * : Only display users with a certain role.
  *
  * [--<field>=<value>]
  * : Filter by one or more fields. For accepted fields, see get_users().
  *
  * [--field=<field>]
  * : Prints the value of a single field for each user.
  *
  * [--fields=<fields>]
  * : Limit the output to specific object fields. Defaults to ID,user_login,display_name,user_email,user_registered,roles
  *
  * [--format=<format>]
  * : Accepted values: table, csv, json, count. Default: table
  *
  * ## EXAMPLES
  *
  *     wp user list --field=ID
  *
  *     wp user list --role=administrator --format=csv
  *
  *     wp user list --fields=display_name,user_email --format=json
  *
  * @subcommand list
  */
 public function _list($args, $assoc_args)
 {
     $formatter = $this->get_formatter($assoc_args);
     if ('ids' == $formatter->format) {
         $assoc_args['fields'] = 'ids';
     } else {
         $assoc_args['fields'] = 'all_with_meta';
     }
     $users = get_users($assoc_args);
     $it = WP_CLI\Utils\iterator_map($users, function ($user) {
         if (!is_object($user)) {
             return $user;
         }
         $user->roles = implode(',', $user->roles);
         return $user;
     });
     $formatter->display_items($it);
 }
Beispiel #2
0
 /**
  * Display multiple items according to the output arguments.
  *
  * @param array $items
  */
 public function display_items($items)
 {
     if ($this->args['field']) {
         $this->show_single_field($items, $this->args['field']);
     } else {
         if (in_array($this->args['format'], array('csv', 'json', 'table'))) {
             $item = is_array($items) && !empty($items) ? array_shift($items) : false;
             if ($item && !empty($this->args['fields'])) {
                 foreach ($this->args['fields'] as &$field) {
                     $field = $this->find_item_key($item, $field);
                 }
                 array_unshift($items, $item);
             }
         }
         if (in_array($this->args['format'], array('table', 'csv'))) {
             if (is_object($items) && is_a($items, 'Iterator')) {
                 $items = \WP_CLI\Utils\iterator_map($items, array($this, 'transform_item_values_to_json'));
             } else {
                 $items = array_map(array($this, 'transform_item_values_to_json'), $items);
             }
         }
         $this->format($items);
     }
 }
Beispiel #3
0
 /**
  * List users.
  *
  * ## OPTIONS
  *
  * [--role=<role>]
  * : Only display users with a certain role.
  *
  * [--<field>=<value>]
  * : Control output by one or more arguments of get_users().
  *
  * [--network]
  * : List all users in the network for multisite.
  *
  * [--field=<field>]
  * : Prints the value of a single field for each user.
  *
  * [--fields=<fields>]
  * : Limit the output to specific object fields.
  *
  * [--format=<format>]
  * : Accepted values: table, csv, json, count, yaml. Default: table
  *
  * ## AVAILABLE FIELDS
  *
  * These fields will be displayed by default for each user:
  *
  * * ID
  * * user_login
  * * display_name
  * * user_email
  * * user_registered
  * * roles
  *
  * These fields are optionally available:
  *
  * * user_pass
  * * user_nicename
  * * user_url
  * * user_activation_key
  * * user_status
  * * spam
  * * deleted
  * * caps
  * * cap_key
  * * allcaps
  * * filter
  *
  * ## EXAMPLES
  *
  *     # List user IDs
  *     $ wp user list --field=ID
  *     1
  *
  *     # List users with administrator role
  *     $ wp user list --role=administrator --format=csv
  *     ID,user_login,display_name,user_email,user_registered,roles
  *     1,supervisor,supervisor,supervisor@gmail.com,"2016-06-03 04:37:00",administrator
  *
  *     # List users with only given fields
  *     $ wp user list --fields=display_name,user_email --format=json
  *     [{"display_name":"supervisor","user_email":"*****@*****.**"}]
  *
  * @subcommand list
  */
 public function list_($args, $assoc_args)
 {
     if (\WP_CLI\Utils\get_flag_value($assoc_args, 'network')) {
         if (!is_multisite()) {
             WP_CLI::error('This is not a multisite install.');
         }
         $assoc_args['blog_id'] = 0;
         if (isset($assoc_args['fields'])) {
             $fields = explode(',', $assoc_args['fields']);
             $assoc_args['fields'] = array_diff($fields, array('roles'));
         } else {
             $assoc_args['fields'] = array_diff($this->obj_fields, array('roles'));
         }
     }
     $formatter = $this->get_formatter($assoc_args);
     if (in_array($formatter->format, array('ids', 'count'))) {
         $assoc_args['fields'] = 'ids';
     } else {
         $assoc_args['fields'] = 'all_with_meta';
     }
     $assoc_args['count_total'] = false;
     $users = get_users($assoc_args);
     if ('ids' == $formatter->format) {
         echo implode(' ', $users);
     } else {
         if ('count' === $formatter->format) {
             $formatter->display_items($users);
         } else {
             $it = WP_CLI\Utils\iterator_map($users, function ($user) {
                 if (!is_object($user)) {
                     return $user;
                 }
                 $user->roles = implode(',', $user->roles);
                 return $user;
             });
             $formatter->display_items($it);
         }
     }
 }
Beispiel #4
0
 /**
  * List all sites in a multisite install.
  *
  * ## OPTIONS
  *
  * [--network=<id>]
  * : The network to which the sites belong.
  *
  * [--<field>=<value>]
  * : Filter by one or more fields.
  *
  * [--field=<field>]
  * : Prints the value of a single field for each site.
  *
  * [--fields=<fields>]
  * : Comma-separated list of fields to show.
  *
  * [--format=<format>]
  * : Accepted values: table, csv, json, count. Default: table
  *
  * ## AVAILABLE FIELDS
  *
  * These fields will be displayed by default for each site:
  *
  * * blog_id
  * * url
  * * last_updated
  * * registered
  *
  * These fields are optionally available:
  *
  * * site_id
  * * domain
  * * path
  * * public
  * * archived
  * * mature
  * * spam
  * * deleted
  * * lang_id
  *
  * ## EXAMPLES
  *
  *     # Output a simple list of site URLs
  *     wp site list --field=url
  *
  * @subcommand list
  */
 public function list_($_, $assoc_args)
 {
     if (!is_multisite()) {
         WP_CLI::error('This is not a multisite install.');
     }
     global $wpdb;
     if (isset($assoc_args['fields'])) {
         $assoc_args['fields'] = preg_split('/,[ \\t]*/', $assoc_args['fields']);
     }
     $defaults = array('format' => 'table', 'fields' => array('blog_id', 'url', 'last_updated', 'registered'));
     $assoc_args = array_merge($defaults, $assoc_args);
     $where = array();
     $append = '';
     $site_cols = array('blog_id', 'url', 'last_updated', 'registered', 'site_id', 'domain', 'path', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
     foreach ($site_cols as $col) {
         if (isset($assoc_args[$col])) {
             $where[$col] = $assoc_args[$col];
         }
     }
     if (isset($assoc_args['site__in'])) {
         $where['blog_id'] = explode(',', $assoc_args['site__in']);
         $append = "ORDER BY FIELD( blog_id, " . implode(',', array_map('intval', $where['blog_id'])) . " )";
     }
     if (isset($assoc_args['network'])) {
         $where['site_id'] = $assoc_args['network'];
     }
     $iterator_args = array('table' => $wpdb->blogs, 'where' => $where, 'append' => $append);
     $it = new \WP_CLI\Iterators\Table($iterator_args);
     $it = \WP_CLI\Utils\iterator_map($it, function ($blog) {
         $blog->url = trailingslashit(get_site_url($blog->blog_id));
         return $blog;
     });
     $formatter = new \WP_CLI\Formatter($assoc_args, null, 'site');
     $formatter->display_items($it);
 }
Beispiel #5
0
 /**
  * List all sites in a multisite install.
  *
  * ## OPTIONS
  *
  * [--network=<id>]
  * : The network to which the sites belong.
  *
  * [--field=<field>]
  * : Prints the value of a single field for each site.
  *
  * [--fields=<fields>]
  * : Comma-separated list of fields to show.
  *
  * [--format=<format>]
  * : Accepted values: table, csv, json, count. Default: table
  *
  * ## EXAMPLES
  *
  *     # Output a simple list of site URLs
  *     wp site list --field=url
  *
  * @subcommand list
  */
 function _list($_, $assoc_args)
 {
     if (!is_multisite()) {
         WP_CLI::error('This is not a multisite install.');
     }
     global $wpdb;
     if (isset($assoc_args['fields'])) {
         $assoc_args['fields'] = preg_split('/,[ \\t]*/', $assoc_args['fields']);
     }
     $defaults = array('format' => 'table', 'fields' => array('blog_id', 'url', 'last_updated', 'registered'));
     $assoc_args = array_merge($defaults, $assoc_args);
     $where = array();
     if (isset($assoc_args['network'])) {
         $where['site_id'] = $assoc_args['network'];
     }
     $iterator_args = array('table' => $wpdb->blogs, 'where' => $where);
     $it = new \WP_CLI\Iterators\Table($iterator_args);
     $it = \WP_CLI\Utils\iterator_map($it, function ($blog) {
         $blog->url = $blog->domain . $blog->path;
         return $blog;
     });
     $formatter = new \WP_CLI\Formatter($assoc_args, null, 'site');
     $formatter->display_items($it);
 }
 /**
  * Perform a action on an item on a remote site
  */
 protected function perform_item_action($action, $args, $assoc_args)
 {
     $site_id = $assoc_args['site-id'];
     unset($assoc_args['site-id']);
     $this->set_account();
     // 'format' and 'fields' are present in a variety of requests
     if (isset($assoc_args['format'])) {
         $format = $assoc_args['format'];
         unset($assoc_args['format']);
     } else {
         $format = 'table';
     }
     if (isset($assoc_args['fields'])) {
         $fields = $assoc_args['fields'];
         unset($assoc_args['fields']);
     } else {
         $fields = implode(',', $this->obj_fields);
     }
     switch ($action) {
         case 'list':
             $endpoint = 'site/' . (int) $site_id . '/' . $this->obj_type;
             $method = 'GET';
             $api_args = $assoc_args;
             break;
         case 'get':
             list($obj_id) = $args;
             $endpoint = 'site/' . (int) $site_id . '/' . $this->obj_type . '/' . $obj_id;
             $method = 'GET';
             $api_args = $assoc_args;
             break;
         case 'create':
             $endpoint = 'site/' . (int) $site_id . '/' . $this->obj_type;
             $method = 'POST';
             $api_args = $assoc_args;
             break;
         case 'update':
             list($obj_id) = $args;
             $endpoint = 'site/' . (int) $site_id . '/' . $this->obj_type . '/' . $obj_id;
             $method = 'POST';
             $api_args = $assoc_args;
             break;
         case 'delete':
             list($obj_id) = $args;
             $endpoint = 'site/' . (int) $site_id . '/' . $this->obj_type . '/' . $obj_id;
             $method = 'DELETE';
             $api_args = $assoc_args;
             break;
     }
     $args = array('endpoint' => $endpoint, '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 'list':
             $it = WP_CLI\Utils\iterator_map($response, function ($item) {
                 if (!is_object($item)) {
                     return $item;
                 }
                 if ('user' == $this->obj_type) {
                     $item->roles = implode(',', $item->roles);
                 }
                 return $item;
             });
             WP_CLI\Utils\format_items($format, $it, $fields);
             break;
         case 'get':
             $this->show_multiple_fields($response, array('format' => $format, 'fields' => $fields));
             break;
         case 'create':
             WP_CLI::success("Created {$this->obj_type}.");
             break;
         case 'update':
             WP_CLI::success("Updated {$this->obj_type}.");
             break;
         case 'delete':
             WP_CLI::success("Deleted {$this->obj_type}.");
             break;
     }
 }