WC_CLI_REST_Command::list_items()publicWC 1.0

List all items.

Method of the class: WC_CLI_REST_Command{}

No Hooks.

Return

null. Nothing (null).

Usage

$WC_CLI_REST_Command = new WC_CLI_REST_Command();
$WC_CLI_REST_Command->list_items( $args, $assoc_args );
$args(array) (required)
WP-CLI positional arguments.
$assoc_args(array) (required)
WP-CLI associative arguments.

WC_CLI_REST_Command::list_items() code WC 8.6.1

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'] ) {
		echo (int) $headers['X-WP-Total'];
	} 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 );
	}
}