WordPress函数:excerpt_remove_blocks 解析内容字符串中的块,并呈现适合摘录的块

江河/ 2023年07月16日/ WordPress/ 浏览 1597

函数原型:


excerpt_remove_blocks( string $content ): string


解析内容字符串中的块,并呈现适合摘录的块。


由于摘录应该是与完整帖子内容相关的一小串文本,因此此函数将呈现最有可能包含此类文本的块。


参数说明:


$content,要分析的内容。


函数源码:


function excerpt_remove_blocks( $content ) {
	$allowed_inner_blocks = array(
		// Classic blocks have their blockName set to null.
		null,
		'core/freeform',
		'core/heading',
		'core/html',
		'core/list',
		'core/media-text',
		'core/paragraph',
		'core/preformatted',
		'core/pullquote',
		'core/quote',
		'core/table',
		'core/verse',
	);

	$allowed_wrapper_blocks = array(
		'core/columns',
		'core/column',
		'core/group',
	);

	$allowed_wrapper_blocks = apply_filters( 'excerpt_allowed_wrapper_blocks', $allowed_wrapper_blocks );

	$allowed_blocks = array_merge( $allowed_inner_blocks, $allowed_wrapper_blocks );

	$allowed_blocks = apply_filters( 'excerpt_allowed_blocks', $allowed_blocks );
	$blocks         = parse_blocks( $content );
	$output         = '';

	foreach ( $blocks as $block ) {
		if ( in_array( $block['blockName'], $allowed_blocks, true ) ) {
			if ( ! empty( $block['innerBlocks'] ) ) {
				if ( in_array( $block['blockName'], $allowed_wrapper_blocks, true ) ) {
					$output .= _excerpt_render_inner_blocks( $block, $allowed_blocks );
					continue;
				}

				// Skip the block if it has disallowed or nested inner blocks.
				foreach ( $block['innerBlocks'] as $inner_block ) {
					if (
						! in_array( $inner_block['blockName'], $allowed_inner_blocks, true ) ||
						! empty( $inner_block['innerBlocks'] )
					) {
						continue 2;
					}
				}
			}

			$output .= render_block( $block );
		}
	}

	return $output;
}


包含钩子:


apply_filters( 'excerpt_allowed_blocks', string[] $allowed_blocks )

apply_filters( 'excerpt_allowed_wrapper_blocks', string[] $allowed_wrapper_blocks )


发表评论

暂无评论,抢个沙发...

客服 工单