GC_Block::render()

最后更新于:2021-11-26 09:38:29

GC_Block::render( array$options=array())

Generates the render output for the block.

参数

$options

(array) (Optional) Optional options object.

  • ‘dynamic’
    (bool) Defaults to ‘true’. Optionally set to false to avoid using the block’s render_callback.

Default value: array()

响应

(string) Rendered block output.

源文件

文件: gc-includes/class-gc-block.php

	public function render( $options = array() ) {
		global $post;
		$options = gc_parse_args(
			$options,
			array(
				'dynamic' => true,
			)
		);

		$is_dynamic    = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic();
		$block_content = '';

		if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) {
			$index = 0;
			foreach ( $this->inner_content as $chunk ) {
				$block_content .= is_string( $chunk ) ?
					$chunk :
					$this->inner_blocks[ $index++ ]->render();
			}
		}

		if ( $is_dynamic ) {
			$global_post = $post;
			$parent      = GC_Block_Supports::$block_to_render;

			GC_Block_Supports::$block_to_render = $this->parsed_block;

			$block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this );

			GC_Block_Supports::$block_to_render = $parent;

			$post = $global_post;
		}

		if ( ! empty( $this->block_type->script ) ) {
			gc_enqueue_script( $this->block_type->script );
		}

		if ( ! empty( $this->block_type->style ) ) {
			gc_enqueue_style( $this->block_type->style );
		}

		/**
		 * Filters the content of a single block.
		 *
		 * @since 5.0.0
		 *
		 * @param string $block_content The block content about to be appended.
		 * @param array  $block         The full block, including name and attributes.
		 */
		$block_content = apply_filters( 'render_block', $block_content, $this->parsed_block );

		/**
		 * Filters the content of a single block.
		 *
		 * The dynamic portion of the hook name, `$name`, refers to
		 * the block name, e.g. "core/paragraph".
		 *
		 * @since 5.7.0
		 *
		 * @param string $block_content The block content about to be appended.
		 * @param array  $block         The full block, including name and attributes.
		 */
		$block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block );

		return $block_content;
	}