wp/wp-includes/blocks/comments-pagination-numbers.php
changeset 19 3d72ae0968f4
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
       
     1 <?php
       
     2 /**
       
     3  * Server-side rendering of the `core/comments-pagination-numbers` block.
       
     4  *
       
     5  * @package WordPress
       
     6  */
       
     7 
       
     8 /**
       
     9  * Renders the `core/comments-pagination-numbers` block on the server.
       
    10  *
       
    11  * @param array    $attributes Block attributes.
       
    12  * @param string   $content    Block default content.
       
    13  * @param WP_Block $block      Block instance.
       
    14  *
       
    15  * @return string Returns the pagination numbers for the comments.
       
    16  */
       
    17 function render_block_core_comments_pagination_numbers( $attributes, $content, $block ) {
       
    18 	// Bail out early if the post ID is not set for some reason.
       
    19 	if ( empty( $block->context['postId'] ) ) {
       
    20 		return '';
       
    21 	}
       
    22 
       
    23 	$comment_vars = build_comment_query_vars_from_block( $block );
       
    24 
       
    25 	$total   = ( new WP_Comment_Query( $comment_vars ) )->max_num_pages;
       
    26 	$current = ! empty( $comment_vars['paged'] ) ? $comment_vars['paged'] : null;
       
    27 
       
    28 	// Render links.
       
    29 	$content = paginate_comments_links(
       
    30 		array(
       
    31 			'total'     => $total,
       
    32 			'current'   => $current,
       
    33 			'prev_next' => false,
       
    34 			'echo'      => false,
       
    35 		)
       
    36 	);
       
    37 
       
    38 	if ( empty( $content ) ) {
       
    39 		return '';
       
    40 	}
       
    41 
       
    42 	$wrapper_attributes = get_block_wrapper_attributes();
       
    43 
       
    44 	return sprintf(
       
    45 		'<div %1$s>%2$s</div>',
       
    46 		$wrapper_attributes,
       
    47 		$content
       
    48 	);
       
    49 }
       
    50 
       
    51 /**
       
    52  * Registers the `core/comments-pagination-numbers` block on the server.
       
    53  */
       
    54 function register_block_core_comments_pagination_numbers() {
       
    55 	register_block_type_from_metadata(
       
    56 		__DIR__ . '/comments-pagination-numbers',
       
    57 		array(
       
    58 			'render_callback' => 'render_block_core_comments_pagination_numbers',
       
    59 		)
       
    60 	);
       
    61 }
       
    62 add_action( 'init', 'register_block_core_comments_pagination_numbers' );