wp/wp-includes/blocks/read-more.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/read-more` block.
       
     4  *
       
     5  * @package WordPress
       
     6  */
       
     7 
       
     8 /**
       
     9  * Renders the `core/read-more` 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  * @return string  Returns the post link.
       
    15  */
       
    16 function render_block_core_read_more( $attributes, $content, $block ) {
       
    17 	if ( ! isset( $block->context['postId'] ) ) {
       
    18 		return '';
       
    19 	}
       
    20 
       
    21 	$post_ID            = $block->context['postId'];
       
    22 	$justify_class_name = empty( $attributes['justifyContent'] ) ? '' : "is-justified-{$attributes['justifyContent']}";
       
    23 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $justify_class_name ) );
       
    24 	$more_text          = ! empty( $attributes['content'] ) ? wp_kses_post( $attributes['content'] ) : __( 'Read more' );
       
    25 	return sprintf(
       
    26 		'<a %1s href="%2s" target="%3s">%4s</a>',
       
    27 		$wrapper_attributes,
       
    28 		get_the_permalink( $post_ID ),
       
    29 		esc_attr( $attributes['linkTarget'] ),
       
    30 		$more_text
       
    31 	);
       
    32 }
       
    33 
       
    34 /**
       
    35  * Registers the `core/read-more` block on the server.
       
    36  */
       
    37 function register_block_core_read_more() {
       
    38 	register_block_type_from_metadata(
       
    39 		__DIR__ . '/read-more',
       
    40 		array(
       
    41 			'render_callback' => 'render_block_core_read_more',
       
    42 		)
       
    43 	);
       
    44 }
       
    45 add_action( 'init', 'register_block_core_read_more' );