make_before_block_visitor

函数
make_before_block_visitor ( $hooked_blocks, $context )
Access
Private
参数
  • (array) $hooked_blocks An array of blocks hooked to another given block.
    Required:
  • (WP_Block_Template|array) $context A block template, template part, or pattern that the blocks belong to.
    Required:
返回值
  • (callable) A function that returns the serialized markup for the given block, including the markup for any hooked blocks before it.
定义位置
相关方法
make_after_block_visitorbefore_last_barparse_blockswp_use_widgets_block_editorblock_version
引入
6.4.0
弃用
-

返回一个将主题属性注入给定代码区块并在给定代码区块之前挂钩代码块的函数。

返回的函数可用作` traverse_and_serialize_block(s) `的` $pre_callback `参数,它将向所有模板部分区块注入` theme `属性,并分别为在给定区块之前和作为其父区块的` first_child `勾住的任何区块预置标记。

function make_before_block_visitor( $hooked_blocks, $context ) {
	/**
	 * Injects hooked blocks before the given block, injects the `theme` attribute into Template Part blocks, and returns the serialized markup.
	 *
	 * If the current block is a Template Part block, inject the `theme` attribute.
	 * Furthermore, prepend the markup for any blocks hooked `before` the given block and as its parent's
	 * `first_child`, respectively, to the serialized markup for the given block.
	 *
	 * @param array $block        The block to inject the theme attribute into, and hooked blocks before. Passed by reference.
	 * @param array $parent_block The parent block of the given block. Passed by reference. Default null.
	 * @param array $prev         The previous sibling block of the given block. Default null.
	 * @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it.
	 */
	return function ( &$block, &$parent_block = null, $prev = null ) use ( $hooked_blocks, $context ) {
		_inject_theme_attribute_in_template_part_block( $block );

		$markup = '';

		if ( $parent_block && ! $prev ) {
			// Candidate for first-child insertion.
			$relative_position  = 'first_child';
			$anchor_block_type  = $parent_block['blockName'];
			$hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
				? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
				: array();

			/**
			 * Filters the list of hooked block types for a given anchor block type and relative position.
			 *
			 * @since 6.4.0
			 *
			 * @param string[]                $hooked_block_types  The list of hooked block types.
			 * @param string                  $relative_position   The relative position of the hooked blocks.
			 *                                                     Can be one of 'before', 'after', 'first_child', or 'last_child'.
			 * @param string                  $anchor_block_type   The anchor block type.
			 * @param WP_Block_Template|array $context             The block template, template part, or pattern that the anchor block belongs to.
			 */
			$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
			foreach ( $hooked_block_types as $hooked_block_type ) {
				$markup .= get_comment_delimited_block_content( $hooked_block_type, array(), '' );
			}
		}

		$relative_position  = 'before';
		$anchor_block_type  = $block['blockName'];
		$hooked_block_types = isset( $hooked_blocks[ $anchor_block_type ][ $relative_position ] )
			? $hooked_blocks[ $anchor_block_type ][ $relative_position ]
			: array();

		/** This filter is documented in wp-includes/blocks.php */
		$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );
		foreach ( $hooked_block_types as $hooked_block_type ) {
			$markup .= get_comment_delimited_block_content( $hooked_block_type, array(), '' );
		}

		return $markup;
	};
}

常见问题

FAQs
查看更多 >