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

[Block Library - Query Pagination] Simplify pagination blocks #37125

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
86 changes: 44 additions & 42 deletions packages/block-library/src/query-pagination-next/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,65 @@
function render_block_core_query_pagination_next( $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 = __( 'Next Page' );
$label = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? $attributes['label'] : $default_label;
$pagination_arrow = get_query_pagination_arrow( $block, true );
$content = '';
$wrapper_attributes = get_block_wrapper_attributes();
$default_label = __( 'Next Page' );
$label = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? $attributes['label'] : $default_label;
$pagination_arrow = get_query_pagination_arrow( $block, true );
$content = '';

if ( $pagination_arrow ) {
$label .= $pagination_arrow;
}

// TODO: udpate below comment..
// If we are in query's first page, render a hidden placeholder for...(design) purposes??
$placholder_attributes = get_block_wrapper_attributes(
array(
'aria-hidden' => 'true',
'style' => 'visibility:hidden;',
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see why an inline style written in PHP is better than a CSS rule. I'm wondering why do you prefer this?

)
);
$placeholder = sprintf(
'<span %1$s>%2$s</span>',
$placholder_attributes,
$label
);

// Check if the pagination is for Query that inherits the global context.
if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
global $wp_query;
// Take into account if we have set a bigger `max page`
// than what the query has.
$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( 'next_posts_link_attributes', $filter_link_attributes );

// If there are pages to paginate.
if ( 1 < $max_page ) {
if ( (int) $max_page !== $paged ) { // If we are NOT in the last one.
$content = get_next_posts_link( $label, $max_page );
} else { // If we are in the last one.
$content = sprintf(
'<span %1$s>%2$s</span>',
$hidden_wrapper_attributes,
$label
);
}
// Take into account if we have set a bigger `max page`
// than what the query has.
global $wp_query;
if ( $max_page > $wp_query->max_num_pages ) {
$max_page = $wp_query->max_num_pages;
}
$content = get_next_posts_link( $label, $max_page );
if ( empty( $content ) ) {
// Return the placeholder content as we are in query's last page.
$content = $placeholder;
}
remove_filter( 'next_posts_link_attributes', $filter_link_attributes );
} 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;
// If there are pages to paginate.
if ( 1 < $max_num_pages ) {
if ( (int) $max_num_pages !== $page ) { // If we are NOT in the last 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 { // If we are in the last one.
$content = sprintf(
'<span %1$s>%2$s</span>',
$hidden_wrapper_attributes,
$label
);
}
return $content;
}
// We have a custom query here.
if ( ! $max_page || $max_page > $page ) {
$custom_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) );
if ( (int) $custom_query->max_num_pages !== $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 {
// Return the placeholder content as we are in query's last page.
$content = $placeholder;
}
wp_reset_postdata(); // Restore original Post Data.
}
Expand Down
77 changes: 34 additions & 43 deletions packages/block-library/src/query-pagination-previous/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,56 @@
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;
}
// TODO: udpate below comment..
// If we are in query's first page, render a hidden placeholder for...(design) purposes??
$placholder_attributes = get_block_wrapper_attributes(
array(
'aria-hidden' => 'true',
'style' => 'visibility:hidden;',
Copy link
Contributor

Choose a reason for hiding this comment

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

Adding PHP logic to add styles doesn't seem to be the best option. I think it adds complexity and makes the elements harder to style with themes.

)
);
$placeholder = sprintf(
'<span %1$s>%2$s</span>',
$placholder_attributes,
$label
);

// 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 );

// 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
);
}
$content = get_previous_posts_link( $label );
if ( empty( $content ) ) {
// Return the placeholder content as we are in query's first page.
$content = $placeholder;
}
remove_filter( 'previous_posts_link_attributes', $filter_link_attributes );
} 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;
// 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;
}
// We have a custom query here.
if ( 1 !== $page ) {
return sprintf(
'<a href="%1$s" %2$s>%3$s</a>',
esc_url( add_query_arg( $page_key, $page - 1 ) ),
$wrapper_attributes,
$label
);
}
return $content;
// Return the placeholder content as we are in query's first page.
return $placeholder;
}

/**
Expand Down
7 changes: 2 additions & 5 deletions packages/block-library/src/query-pagination/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ $pagination-margin: 0.5em;
transform: scaleX(1) #{"/*rtl:scaleX(-1);*/"}; // This points the arrow right for LTR and left for RTL.
}
}

// Non-clickeable previous and next elements are not visible
>span.wp-block-query-pagination-next,
>span.wp-block-query-pagination-previous {
visibility: hidden;
&.aligncenter {
justify-content: center;
}

&.wp-justify-left,
Expand Down