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 * @since 6.0.0
12 *
13 * @param array $attributes Block attributes.
14 * @param string $content Block default content.
15 * @param WP_Block $block Block instance.
16 * @return string Returns the post link.
17 */
18function render_block_core_read_more( $attributes, $content, $block ) {
19 if ( ! isset( $block->context['postId'] ) ) {
20 return '';
21 }
22
23 $post_ID = $block->context['postId'];
24 $post_title = get_the_title( $post_ID );
25 if ( '' === $post_title ) {
26 $post_title = sprintf(
27 /* translators: %s is post ID to describe the link for screen readers. */
28 __( 'untitled post %s' ),
29 $post_ID
30 );
31 }
32 $screen_reader_text = sprintf(
33 /* translators: %s is either the post title or post ID to describe the link for screen readers. */
34 __( ': %s' ),
35 $post_title
36 );
37 $justify_class_name = empty( $attributes['justifyContent'] ) ? '' : "is-justified-{$attributes['justifyContent']}";
38 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $justify_class_name ) );
39 $more_text = ! empty( $attributes['content'] ) ? wp_kses_post( $attributes['content'] ) : __( 'Read more' );
40 return sprintf(
41 '<a %1s href="%2s" target="%3s">%4s<span class="screen-reader-text">%5s</span></a>',
42 $wrapper_attributes,
43 get_the_permalink( $post_ID ),
44 esc_attr( $attributes['linkTarget'] ),
45 $more_text,
46 $screen_reader_text
47 );
48}
49
50/**
51 * Registers the `core/read-more` block on the server.
52 *
53 * @since 6.0.0
54 */
55function register_block_core_read_more() {
56 register_block_type_from_metadata(
57 __DIR__ . '/read-more',
58 array(
59 'render_callback' => 'render_block_core_read_more',
60 )
61 );
62}
63add_action( 'init', 'register_block_core_read_more' );
64