Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance: Improve pagination logic in core/query-pagination-previous block #68070

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions packages/block-library/src/query-pagination-previous/index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove this extra line.

/**
* Server-side rendering of the `core/query-pagination-previous` block.
*
Expand All @@ -19,14 +20,14 @@
function render_block_core_query_pagination_previous( $attributes, $content, $block ) {
$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
$enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination'];
$max_page = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0;
$page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];

$wrapper_attributes = get_block_wrapper_attributes();
$show_label = isset( $block->context['showLabel'] ) ? (bool) $block->context['showLabel'] : true;
$default_label = __( 'Previous Page' );
$label_text = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? esc_html( $attributes['label'] ) : $default_label;
$label = $show_label ? $label_text : '';
$pagination_arrow = get_query_pagination_arrow( $block, false );
$wrapper_attributes = get_block_wrapper_attributes();
$show_label = isset( $block->context['showLabel'] ) ? (bool) $block->context['showLabel'] : true;
$default_label = __( 'Previous Page' );
$label_text = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? esc_html( $attributes['label'] ) : $default_label;
$label = $show_label ? $label_text : '';
$pagination_arrow = get_query_pagination_arrow( $block, false );
if ( ! $label ) {
$wrapper_attributes .= ' aria-label="' . $label_text . '"';
}
Expand All @@ -44,13 +45,25 @@ function render_block_core_query_pagination_previous( $attributes, $content, $bl
add_filter( 'previous_posts_link_attributes', $filter_link_attributes );
$content = get_previous_posts_link( $label );
remove_filter( 'previous_posts_link_attributes', $filter_link_attributes );
} elseif ( 1 !== $page ) {
$content = sprintf(
'<a href="%1$s" %2$s>%3$s</a>',
esc_url( add_query_arg( $page_key, $page - 1 ) ),
$wrapper_attributes,
$label
);
} elseif ( 1 < $page ) {
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved

$total_pages = $max_page;
if ( ! isset( $block->context['query']['inherit'] ) || ! $block->context['query']['inherit'] ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I suggested to move this block here is that we could avoid this if condition, since it's actually the else from above.

I know realized that we have an elseif ( 1 !== $page ), so I guess we could change the elseif to else, and inside that else block handle the if ( 1 !== $page ).

This way we wouldn't also need the temp assignment $total_pages = $max_page;

I hope I didn't confuse you :)

$block_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) );
$total_pages = $block_query->max_num_pages;
wp_reset_postdata();
}

$total = ! $max_page || $max_page > $total_pages ? $total_pages : $max_page;

if ( $page <= $total ) {
$content = sprintf(
'<a href="%1$s" %2$s>%3$s</a>',
esc_url( add_query_arg( $page_key, $page - 1 ) ),
$wrapper_attributes,
$label
);
}
}

if ( $enhanced_pagination && isset( $content ) ) {
Expand Down
Loading