-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
query-pagination-next and previous: Changing the markup on the first and last pages of a pagination #36681
query-pagination-next and previous: Changing the markup on the first and last pages of a pagination #36681
Changes from all commits
e7fb180
f362d1f
3a89020
9218edc
e1e6e7a
d786609
5296c32
2c89ae6
6084c04
02f3c2b
dde160d
cadb2a6
a7c4481
64fe5b9
760873d
844d381
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 |
---|---|---|
|
@@ -17,31 +17,63 @@ | |
function render_block_core_query_pagination_previous( $attributes, $content, $block ) { | ||
$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; | ||
$page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; | ||
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; | ||
$max_page = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0; | ||
|
||
$wrapper_attributes = get_block_wrapper_attributes(); | ||
$hidden_wrapper_attributes = get_block_wrapper_attributes( array( 'aria-hidden' => 'true' ) ); | ||
$default_label = __( 'Previous Page' ); | ||
$label = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? $attributes['label'] : $default_label; | ||
$pagination_arrow = get_query_pagination_arrow( $block, false ); | ||
$content = ''; | ||
|
||
$wrapper_attributes = get_block_wrapper_attributes(); | ||
$default_label = __( 'Previous Page' ); | ||
$label = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? $attributes['label'] : $default_label; | ||
$pagination_arrow = get_query_pagination_arrow( $block, false ); | ||
if ( $pagination_arrow ) { | ||
$label = $pagination_arrow . $label; | ||
} | ||
$content = ''; | ||
// Check if the pagination is for Query that inherits the global context | ||
// and handle appropriately. | ||
if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) { | ||
global $wp_query; | ||
$max_page = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page; | ||
$filter_link_attributes = function() use ( $wrapper_attributes ) { | ||
return $wrapper_attributes; | ||
}; | ||
|
||
add_filter( 'previous_posts_link_attributes', $filter_link_attributes ); | ||
$content = get_previous_posts_link( $label ); | ||
|
||
// If there are pages to paginate... | ||
if ( 1 < $max_page ) { | ||
if ( 1 !== $paged ) { // ... and we are NOT in the first one. | ||
$content = get_previous_posts_link( $label ); | ||
} else { // ... and we are in the first one. | ||
$content = sprintf( | ||
'<span %1$s>%2$s</span>', | ||
$hidden_wrapper_attributes, | ||
$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 ( ! $max_page || $max_page > $page ) { | ||
$custom_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) ); | ||
$max_num_pages = $custom_query->max_num_pages ? $custom_query->max_num_pages : 1; | ||
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. Why do we need this 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. This is necessary because if the query has no results 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. I'm sorry but I still don't get it. In 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. Nope, we don't care only about if we are on the first page or not. We need to know if there are pages to paginate. If there are not pages to paginate should not render anything. |
||
// If there are pages to paginate... | ||
if ( 1 < $max_num_pages ) { | ||
if ( 1 !== $page ) { // ... and we are NOT in the first one. | ||
$content = sprintf( | ||
'<a href="%1$s" %2$s>%3$s</a>', | ||
esc_url( add_query_arg( $page_key, $page - 1 ) ), | ||
$wrapper_attributes, | ||
$label | ||
); | ||
} else { // ... and we are in the first one. | ||
$content = sprintf( | ||
'<span %1$s>%2$s</span>', | ||
$hidden_wrapper_attributes, | ||
$label | ||
); | ||
} | ||
} | ||
} | ||
return $content; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,17 @@ $pagination-margin: 0.5em; | |
} | ||
} | ||
|
||
&.aligncenter { | ||
justify-content: center; | ||
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. This shouldn't be removed. It was added here: #34739 as a fix for 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. 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. I know, but we also support the 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. @ntsekouras could you provide more context about backward compatibility related to this, please? 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. Sure. Even if the Even if we removed the Makes sense? Please tell me to clarify anything from the above if you want. |
||
// Non-clickeable previous and next elements are not visible | ||
>span.wp-block-query-pagination-next, | ||
>span.wp-block-query-pagination-previous { | ||
visibility: hidden; | ||
} | ||
|
||
&.wp-justify-left, | ||
&.wp-justify-right { | ||
> [aria-hidden="true"] { | ||
display: none; | ||
} | ||
} | ||
|
||
} |
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.
Why we added this to the
layout
abstraction? Was something specific to pagination blocks?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.
Yep, we needed to identify with a class the justification of the block, without this change that was not possible.
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.
There shouldn't be any block logic leaked into the
layout
abstraction. I'll check a bit the PR better to try to understand. That was the case recently withNavigation
block as well, but we should find another way to handle specific block needs.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.
Ok, I see, let me submit a PR with this change you propose.
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.
@ntsekouras a PR moving the functionality from Layout to Query Pagination code:
#37113