-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
/** | ||
* Server-side rendering of the `core/query-pagination-previous` block. | ||
* | ||
|
@@ -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 . '"'; | ||
} | ||
|
@@ -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'] ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 I know realized that we have an This way we wouldn't also need the temp assignment 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 ) ) { | ||
|
There was a problem hiding this comment.
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.