Skip to content

Commit

Permalink
Fixing elements rendered on a few edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenedetto committed Nov 29, 2021
1 parent cadb2a6 commit a7c4481
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 43 deletions.
58 changes: 36 additions & 22 deletions packages/block-library/src/query-pagination-next/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,56 @@ function render_block_core_query_pagination_next( $attributes, $content, $block
$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;
}

$content = sprintf(
'<span %1$s>%2$s</span>',
$hidden_wrapper_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 );
// 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;
}
if ( (int) $wp_query->max_num_pages !== $paged ) {
$content = get_next_posts_link( $label, $max_page );

// If there are pages to paginate.
var_dump( $wp_query->max_num_pages );
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
);
}
}
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 ) );
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
);
$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
);
}
}
wp_reset_postdata(); // Restore original Post Data.
}
Expand Down
54 changes: 33 additions & 21 deletions packages/block-library/src/query-pagination-previous/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function render_block_core_query_pagination_previous( $attributes, $content, $bl
$default_label = __( 'Previous Page' );
$label = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? $attributes['label'] : $default_label;
$pagination_arrow = get_query_pagination_arrow( $block, false );
$content = '';

if ( $pagination_arrow ) {
$label = $pagination_arrow . $label;
Expand All @@ -39,29 +40,40 @@ function render_block_core_query_pagination_previous( $attributes, $content, $bl
};

add_filter( 'previous_posts_link_attributes', $filter_link_attributes );
if ( 1 !== $paged ) {
$content = get_previous_posts_link( $label );
} else {
// Prints the link only if there are pages to paginate.
$content = $max_page > 1 ? sprintf(
'<span %1$s>%2$s</span>',
$hidden_wrapper_attributes,
$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 );
} else {
$content = ( 1 !== $page ) ? sprintf(
'<a href="%1$s" %2$s>%3$s</a>',
esc_url( add_query_arg( $page_key, $page - 1 ) ),
$wrapper_attributes,
$label
) : sprintf(
'<span %1$s>%2$s</span>',
$hidden_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;
// 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;
}
Expand Down

0 comments on commit a7c4481

Please sign in to comment.