Gets the font collections available.
Source
public function get_items( $request ) {
$collections_all = WP_Font_Library::get_instance()->get_font_collections();
$page = $request['page'];
$per_page = $request['per_page'];
$total_items = count( $collections_all );
$max_pages = (int) ceil( $total_items / $per_page );
if ( $page > $max_pages && $total_items > 0 ) {
return new WP_Error(
'rest_post_invalid_page_number',
__( 'The page number requested is larger than the number of pages available.' ),
array( 'status' => 400 )
);
}
$collections_page = array_slice( $collections_all, ( $page - 1 ) * $per_page, $per_page );
$items = array();
foreach ( $collections_page as $collection ) {
$item = $this->prepare_item_for_response( $collection, $request );
// If there's an error loading a collection, skip it and continue loading valid collections.
if ( is_wp_error( $item ) ) {
continue;
}
$item = $this->prepare_response_for_collection( $item );
$items[] = $item;
}
$response = rest_ensure_response( $items );
$response->header( 'X-WP-Total', (int) $total_items );
$response->header( 'X-WP-TotalPages', $max_pages );
$request_params = $request->get_query_params();
$collection_url = rest_url( $this->namespace . '/' . $this->rest_base );
$base = add_query_arg( urlencode_deep( $request_params ), $collection_url );
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;
}
Changelog
Version | Description |
---|---|
6.5.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.