17 * @return string Returns the previous posts link for the query. |
17 * @return string Returns the previous posts link for the query. |
18 */ |
18 */ |
19 function render_block_core_query_pagination_previous( $attributes, $content, $block ) { |
19 function render_block_core_query_pagination_previous( $attributes, $content, $block ) { |
20 $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; |
20 $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; |
21 $enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination']; |
21 $enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination']; |
|
22 $max_page = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0; |
22 $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; |
23 $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; |
23 |
24 $wrapper_attributes = get_block_wrapper_attributes(); |
24 $wrapper_attributes = get_block_wrapper_attributes(); |
25 $show_label = isset( $block->context['showLabel'] ) ? (bool) $block->context['showLabel'] : true; |
25 $show_label = isset( $block->context['showLabel'] ) ? (bool) $block->context['showLabel'] : true; |
26 $default_label = __( 'Previous Page' ); |
26 $default_label = __( 'Previous Page' ); |
27 $label_text = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? esc_html( $attributes['label'] ) : $default_label; |
27 $label_text = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? esc_html( $attributes['label'] ) : $default_label; |
28 $label = $show_label ? $label_text : ''; |
28 $label = $show_label ? $label_text : ''; |
29 $pagination_arrow = get_query_pagination_arrow( $block, false ); |
29 $pagination_arrow = get_query_pagination_arrow( $block, false ); |
|
30 if ( ! $label ) { |
30 if ( ! $label ) { |
31 $wrapper_attributes .= ' aria-label="' . $label_text . '"'; |
31 $wrapper_attributes .= ' aria-label="' . $label_text . '"'; |
32 } |
32 } |
33 if ( $pagination_arrow ) { |
33 if ( $pagination_arrow ) { |
34 $label = $pagination_arrow . $label; |
34 $label = $pagination_arrow . $label; |
42 }; |
42 }; |
43 |
43 |
44 add_filter( 'previous_posts_link_attributes', $filter_link_attributes ); |
44 add_filter( 'previous_posts_link_attributes', $filter_link_attributes ); |
45 $content = get_previous_posts_link( $label ); |
45 $content = get_previous_posts_link( $label ); |
46 remove_filter( 'previous_posts_link_attributes', $filter_link_attributes ); |
46 remove_filter( 'previous_posts_link_attributes', $filter_link_attributes ); |
47 } elseif ( 1 !== $page ) { |
47 } else { |
48 $content = sprintf( |
48 $block_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) ); |
49 '<a href="%1$s" %2$s>%3$s</a>', |
49 $block_max_pages = $block_query->max_num_pages; |
50 esc_url( add_query_arg( $page_key, $page - 1 ) ), |
50 $total = ! $max_page || $max_page > $block_max_pages ? $block_max_pages : $max_page; |
51 $wrapper_attributes, |
51 wp_reset_postdata(); |
52 $label |
52 |
53 ); |
53 if ( 1 < $page && $page <= $total ) { |
|
54 $content = sprintf( |
|
55 '<a href="%1$s" %2$s>%3$s</a>', |
|
56 esc_url( add_query_arg( $page_key, $page - 1 ) ), |
|
57 $wrapper_attributes, |
|
58 $label |
|
59 ); |
|
60 } |
54 } |
61 } |
55 |
62 |
56 if ( $enhanced_pagination && isset( $content ) ) { |
63 if ( $enhanced_pagination && isset( $content ) ) { |
57 $p = new WP_HTML_Tag_Processor( $content ); |
64 $p = new WP_HTML_Tag_Processor( $content ); |
58 if ( $p->next_tag( |
65 if ( $p->next_tag( |