balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

post-excerpt.php (2432B)


      1 <?php
      2 /**
      3  * Server-side rendering of the `core/post-excerpt` block.
      4  *
      5  * @package WordPress
      6  */
      7 
      8 /**
      9  * Renders the `core/post-excerpt` 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 filtered post excerpt for the current post wrapped inside "p" tags.
     15  */
     16 function render_block_core_post_excerpt( $attributes, $content, $block ) {
     17 	if ( ! isset( $block->context['postId'] ) ) {
     18 		return '';
     19 	}
     20 
     21 	$more_text           = ! empty( $attributes['moreText'] ) ? '<a class="wp-block-post-excerpt__more-link" href="' . esc_url( get_the_permalink( $block->context['postId'] ) ) . '">' . $attributes['moreText'] . '</a>' : '';
     22 	$filter_excerpt_more = function( $more ) use ( $more_text ) {
     23 		return empty( $more_text ) ? $more : '';
     24 	};
     25 	/**
     26 	 * Some themes might use `excerpt_more` filter to handle the
     27 	 * `more` link displayed after a trimmed excerpt. Since the
     28 	 * block has a `more text` attribute we have to check and
     29 	 * override if needed the return value from this filter.
     30 	 * So if the block's attribute is not empty override the
     31 	 * `excerpt_more` filter and return nothing. This will
     32 	 * result in showing only one `read more` link at a time.
     33 	 */
     34 	add_filter( 'excerpt_more', $filter_excerpt_more );
     35 	$classes = '';
     36 	if ( isset( $attributes['textAlign'] ) ) {
     37 		$classes .= "has-text-align-{$attributes['textAlign']}";
     38 	}
     39 	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
     40 
     41 	$content               = '<p class="wp-block-post-excerpt__excerpt">' . get_the_excerpt( $block->context['postId'] );
     42 	$show_more_on_new_line = ! isset( $attributes['showMoreOnNewLine'] ) || $attributes['showMoreOnNewLine'];
     43 	if ( $show_more_on_new_line && ! empty( $more_text ) ) {
     44 		$content .= '</p><p class="wp-block-post-excerpt__more-text">' . $more_text . '</p>';
     45 	} else {
     46 		$content .= " $more_text</p>";
     47 	}
     48 	remove_filter( 'excerpt_more', $filter_excerpt_more );
     49 	return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $content );
     50 }
     51 
     52 /**
     53  * Registers the `core/post-excerpt` block on the server.
     54  */
     55 function register_block_core_post_excerpt() {
     56 	register_block_type_from_metadata(
     57 		__DIR__ . '/post-excerpt',
     58 		array(
     59 			'render_callback' => 'render_block_core_post_excerpt',
     60 		)
     61 	);
     62 }
     63 add_action( 'init', 'register_block_core_post_excerpt' );