WordPress at Your Fingertips
function is not described

WC_REST_Product_Reviews_Controller::get_items() public WC 1.0

Get all reviews.

{} It's a method of the class: WC_REST_Product_Reviews_Controller{}

Return

Array|WP_Error.

Usage

$WC_REST_Product_Reviews_Controller = new WC_REST_Product_Reviews_Controller();
$WC_REST_Product_Reviews_Controller->get_items( $request );
$request(WP_REST_Request) (required)
Full details about the request.

Code of WC_REST_Product_Reviews_Controller::get_items() WC 5.6.0

public function get_items( $request ) {
	// Retrieve the list of registered collection query parameters.
	$registered = $this->get_collection_params();

	/*
	 * This array defines mappings between public API query parameters whose
	 * values are accepted as-passed, and their internal WP_Query parameter
	 * name equivalents (some are the same). Only values which are also
	 * present in $registered will be set.
	 */
	$parameter_mappings = array(
		'reviewer'         => 'author__in',
		'reviewer_email'   => 'author_email',
		'reviewer_exclude' => 'author__not_in',
		'exclude'          => 'comment__not_in',
		'include'          => 'comment__in',
		'offset'           => 'offset',
		'order'            => 'order',
		'per_page'         => 'number',
		'product'          => 'post__in',
		'search'           => 'search',
		'status'           => 'status',
	);

	$prepared_args = array();

	/*
	 * For each known parameter which is both registered and present in the request,
	 * set the parameter's value on the query $prepared_args.
	 */
	foreach ( $parameter_mappings as $api_param => $wp_param ) {
		if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
			$prepared_args[ $wp_param ] = $request[ $api_param ];
		}
	}

	// Ensure certain parameter values default to empty strings.
	foreach ( array( 'author_email', 'search' ) as $param ) {
		if ( ! isset( $prepared_args[ $param ] ) ) {
			$prepared_args[ $param ] = '';
		}
	}

	if ( isset( $registered['orderby'] ) ) {
		$prepared_args['orderby'] = $this->normalize_query_param( $request['orderby'] );
	}

	if ( isset( $prepared_args['status'] ) ) {
		$prepared_args['status'] = 'approved' === $prepared_args['status'] ? 'approve' : $prepared_args['status'];
	}

	$prepared_args['no_found_rows'] = false;
	$prepared_args['date_query']    = array();

	// Set before into date query. Date query must be specified as an array of an array.
	if ( isset( $registered['before'], $request['before'] ) ) {
		$prepared_args['date_query'][0]['before'] = $request['before'];
	}

	// Set after into date query. Date query must be specified as an array of an array.
	if ( isset( $registered['after'], $request['after'] ) ) {
		$prepared_args['date_query'][0]['after'] = $request['after'];
	}

	if ( isset( $registered['page'] ) && empty( $request['offset'] ) ) {
		$prepared_args['offset'] = $prepared_args['number'] * ( absint( $request['page'] ) - 1 );
	}

	/**
	 * Filters arguments, before passing to WP_Comment_Query, when querying reviews via the REST API.
	 *
	 * @since 3.5.0
	 * @link https://developer.wordpress.org/reference/classes/wp_comment_query/
	 * @param array           $prepared_args Array of arguments for WP_Comment_Query.
	 * @param WP_REST_Request $request       The current request.
	 */
	$prepared_args = apply_filters( 'woocommerce_rest_product_review_query', $prepared_args, $request );

	// Make sure that returns only reviews.
	$prepared_args['type'] = 'review';

	// Query reviews.
	$query        = new WP_Comment_Query();
	$query_result = $query->query( $prepared_args );
	$reviews      = array();

	foreach ( $query_result as $review ) {
		if ( ! wc_rest_check_product_reviews_permissions( 'read', $review->comment_ID ) ) {
			continue;
		}

		$data      = $this->prepare_item_for_response( $review, $request );
		$reviews[] = $this->prepare_response_for_collection( $data );
	}

	$total_reviews = (int) $query->found_comments;
	$max_pages     = (int) $query->max_num_pages;

	if ( $total_reviews < 1 ) {
		// Out-of-bounds, run the query again without LIMIT for total count.
		unset( $prepared_args['number'], $prepared_args['offset'] );

		$query                  = new WP_Comment_Query();
		$prepared_args['count'] = true;

		$total_reviews = $query->query( $prepared_args );
		$max_pages     = ceil( $total_reviews / $request['per_page'] );
	}

	$response = rest_ensure_response( $reviews );
	$response->header( 'X-WP-Total', $total_reviews );
	$response->header( 'X-WP-TotalPages', $max_pages );

	$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );

	if ( $request['page'] > 1 ) {
		$prev_page = $request['page'] - 1;

		if ( $prev_page > $max_pages ) {
			$prev_page = $max_pages;
		}

		$prev_link = add_query_arg( 'page', $prev_page, $base );
		$response->link_header( 'prev', $prev_link );
	}

	if ( $max_pages > $request['page'] ) {
		$next_page = $request['page'] + 1;
		$next_link = add_query_arg( 'page', $next_page, $base );

		$response->link_header( 'next', $next_link );
	}

	return $response;
}