wp/wp-includes/blocks/query-pagination-previous.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
     6  */
     6  */
     7 
     7 
     8 /**
     8 /**
     9  * Renders the `core/query-pagination-previous` block on the server.
     9  * Renders the `core/query-pagination-previous` block on the server.
    10  *
    10  *
       
    11  * @since 5.8.0
       
    12  *
    11  * @param array    $attributes Block attributes.
    13  * @param array    $attributes Block attributes.
    12  * @param string   $content    Block default content.
    14  * @param string   $content    Block default content.
    13  * @param WP_Block $block      Block instance.
    15  * @param WP_Block $block      Block instance.
    14  *
    16  *
    15  * @return string Returns the previous posts link for the query.
    17  * @return string Returns the previous posts link for the query.
    16  */
    18  */
    17 function render_block_core_query_pagination_previous( $attributes, $content, $block ) {
    19 function render_block_core_query_pagination_previous( $attributes, $content, $block ) {
    18 	$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';
    19 	$page     = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
    21 	$enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination'];
       
    22 	$page                = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
    20 
    23 
    21 	$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;
    22 	$default_label      = __( 'Previous Page' );
    26 	$default_label      = __( 'Previous Page' );
    23 	$label              = 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 : '';
    24 	$pagination_arrow   = get_query_pagination_arrow( $block, false );
    29 	$pagination_arrow   = get_query_pagination_arrow( $block, false );
       
    30 	if ( ! $label ) {
       
    31 		$wrapper_attributes .= ' aria-label="' . $label_text . '"';
       
    32 	}
    25 	if ( $pagination_arrow ) {
    33 	if ( $pagination_arrow ) {
    26 		$label = $pagination_arrow . $label;
    34 		$label = $pagination_arrow . $label;
    27 	}
    35 	}
    28 	$content = '';
    36 	$content = '';
    29 	// Check if the pagination is for Query that inherits the global context
    37 	// Check if the pagination is for Query that inherits the global context
    30 	// and handle appropriately.
    38 	// and handle appropriately.
    31 	if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
    39 	if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
    32 		$filter_link_attributes = function() use ( $wrapper_attributes ) {
    40 		$filter_link_attributes = static function () use ( $wrapper_attributes ) {
    33 			return $wrapper_attributes;
    41 			return $wrapper_attributes;
    34 		};
    42 		};
    35 
    43 
    36 		add_filter( 'previous_posts_link_attributes', $filter_link_attributes );
    44 		add_filter( 'previous_posts_link_attributes', $filter_link_attributes );
    37 		$content = get_previous_posts_link( $label );
    45 		$content = get_previous_posts_link( $label );
    42 			esc_url( add_query_arg( $page_key, $page - 1 ) ),
    50 			esc_url( add_query_arg( $page_key, $page - 1 ) ),
    43 			$wrapper_attributes,
    51 			$wrapper_attributes,
    44 			$label
    52 			$label
    45 		);
    53 		);
    46 	}
    54 	}
       
    55 
       
    56 	if ( $enhanced_pagination && isset( $content ) ) {
       
    57 		$p = new WP_HTML_Tag_Processor( $content );
       
    58 		if ( $p->next_tag(
       
    59 			array(
       
    60 				'tag_name'   => 'a',
       
    61 				'class_name' => 'wp-block-query-pagination-previous',
       
    62 			)
       
    63 		) ) {
       
    64 			$p->set_attribute( 'data-wp-key', 'query-pagination-previous' );
       
    65 			$p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' );
       
    66 			$p->set_attribute( 'data-wp-on-async--mouseenter', 'core/query::actions.prefetch' );
       
    67 			$p->set_attribute( 'data-wp-watch', 'core/query::callbacks.prefetch' );
       
    68 			$content = $p->get_updated_html();
       
    69 		}
       
    70 	}
       
    71 
    47 	return $content;
    72 	return $content;
    48 }
    73 }
    49 
    74 
    50 /**
    75 /**
    51  * Registers the `core/query-pagination-previous` block on the server.
    76  * Registers the `core/query-pagination-previous` block on the server.
       
    77  *
       
    78  * @since 5.8.0
    52  */
    79  */
    53 function register_block_core_query_pagination_previous() {
    80 function register_block_core_query_pagination_previous() {
    54 	register_block_type_from_metadata(
    81 	register_block_type_from_metadata(
    55 		__DIR__ . '/query-pagination-previous',
    82 		__DIR__ . '/query-pagination-previous',
    56 		array(
    83 		array(