Skip to content

Commit

Permalink
Enhance: Improve pagination logic in core/query-pagination-previous b…
Browse files Browse the repository at this point in the history
…lock (#68070)

Co-authored-by: yogeshbhutkar <yogeshbhutkar@git.wordpress.org>
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
Co-authored-by: michalczaplinski <czapla@git.wordpress.org>
  • Loading branch information
4 people authored Dec 19, 2024
1 parent 79200bf commit a4abb4d
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions packages/block-library/src/query-pagination-previous/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,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 +44,20 @@ 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
);
} else {
$block_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) );
$block_max_pages = $block_query->max_num_pages;
$total = ! $max_page || $max_page > $block_max_pages ? $block_max_pages : $max_page;
wp_reset_postdata();

if ( 1 < $page && $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

1 comment on commit a4abb4d

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in a4abb4d.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12410622721
📝 Reported issues:

Please sign in to comment.