Skip to content

Commit

Permalink
Add server side pagination for the main query (WordPress#26557)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Nov 9, 2020
1 parent d0f883b commit 42b8f6b
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packages/block-library/src/query-pagination/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@
* @return string Returns the pagination for the query.
*/
function render_block_core_query_pagination( $attributes, $content, $block ) {
if ( isset( $block->context['queryId'] ) ) {
$html = render_block_core_query_pagination_selected_query( $attributes, $content, $block );
} else {
$html = render_block_core_query_pagination_main_query( $attributes, $content, $block );
}
return $html;
}

/**
* Renders the `core/query-pagination` block on the server for the selected query.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
*
* @return string Returns the pagination for the query.
*/
function render_block_core_query_pagination_selected_query( $attributes, $content, $block ) {

$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
$page = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT );

Expand All @@ -36,6 +55,22 @@ function render_block_core_query_pagination( $attributes, $content, $block ) {
return $content;
}

/**
* Renders the `core/query-pagination` block on the server for the main query.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
*
* @return string Returns the pagination for the query.
*/
function render_block_core_query_pagination_main_query( $attributes, $content, $block ) {
$html = '<div class="wp-block-query-pagination">';
$html .= paginate_links( [ 'type' => 'list'] );
$html .= "</div>";
return $html;
}

/**
* Registers the `core/query-pagination` block on the server.
*/
Expand Down

0 comments on commit 42b8f6b

Please sign in to comment.