Automattic\WooCommerce\Admin\API\Reports

GenericController::add_pagination_headers()publicWC 1.0

Add pagination headers and links.

Метод класса: GenericController{}

Хуков нет.

Возвращает

\WP_REST_Response.

Использование

$GenericController = new GenericController();
$GenericController->add_pagination_headers( $request, $response, $total, $page, $max_pages );
$request(\WP_REST_Request) (обязательный)
Request data.
$response(\WP_REST_Response|массив) (обязательный)
Response data.
$total(int) (обязательный)
Total results.
$page(int) (обязательный)
Current page.
$max_pages(int) (обязательный)
Total amount of pages.

Код GenericController::add_pagination_headers() WC 9.3.3

public function add_pagination_headers( $request, $response, int $total, int $page, int $max_pages ) {
	$response = rest_ensure_response( $response );
	$response->header( 'X-WP-Total', $total );
	$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 ( $page > 1 ) {
		$prev_page = $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 > $page ) {
		$next_page = $page + 1;
		$next_link = add_query_arg( 'page', $next_page, $base );
		$response->link_header( 'next', $next_link );
	}

	return $response;
}