WC_CLI_REST_Command::list_items
List all items.
Метод класса: WC_CLI_REST_Command{}
Хуков нет.
Возвращает
null. Ничего (null).
Использование
$WC_CLI_REST_Command = new WC_CLI_REST_Command(); $WC_CLI_REST_Command->list_items( $args, $assoc_args );
- $args(массив) (обязательный)
- WP-CLI positional arguments.
- $assoc_args(массив) (обязательный)
- WP-CLI associative arguments.
Код WC_CLI_REST_Command::list_items() WC CLI REST Command::list items WC 10.5.2
public function list_items( $args, $assoc_args ) {
if ( ! empty( $assoc_args['format'] ) && 'count' === $assoc_args['format'] ) {
$method = 'HEAD';
} else {
$method = 'GET';
}
if ( ! isset( $assoc_args['per_page'] ) || empty( $assoc_args['per_page'] ) ) {
$assoc_args['per_page'] = '100';
}
list( $status, $body, $headers ) = $this->do_request( $method, $this->get_filled_route( $args ), $assoc_args );
if ( ! empty( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
$items = array_column( $body, 'id' );
} else {
$items = $body;
}
if ( ! empty( $assoc_args['fields'] ) ) {
foreach ( $items as $key => $item ) {
$items[ $key ] = self::limit_item_to_fields( $item, $assoc_args['fields'] );
}
}
if ( empty( $assoc_args['format'] ) ) {
$assoc_args['format'] = 'table';
}
if ( ! empty( $assoc_args['format'] ) && 'count' === $assoc_args['format'] ) {
if ( isset( $headers['X-WP-Total'] ) ) {
echo (int) $headers['X-WP-Total'];
} else {
WP_CLI::error( 'Count format not implemented yet.' );
}
} elseif ( 'headers' === $assoc_args['format'] ) {
echo wp_json_encode( $headers );
} elseif ( 'body' === $assoc_args['format'] ) {
echo wp_json_encode( $body );
} elseif ( 'envelope' === $assoc_args['format'] ) {
echo wp_json_encode(
array(
'body' => $body,
'headers' => $headers,
'status' => $status,
'api_url' => $this->api_url,
)
);
} else {
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_items( $items );
}
}