wp/wp-includes/class-wp-block.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    56 	 * @access protected
    56 	 * @access protected
    57 	 */
    57 	 */
    58 	protected $available_context;
    58 	protected $available_context;
    59 
    59 
    60 	/**
    60 	/**
       
    61 	 * Block type registry.
       
    62 	 *
       
    63 	 * @since 5.9.0
       
    64 	 * @var WP_Block_Type_Registry
       
    65 	 * @access protected
       
    66 	 */
       
    67 	protected $registry;
       
    68 
       
    69 	/**
    61 	 * List of inner blocks (of this same class)
    70 	 * List of inner blocks (of this same class)
    62 	 *
    71 	 *
    63 	 * @since 5.5.0
    72 	 * @since 5.5.0
    64 	 * @var WP_Block[]
    73 	 * @var WP_Block_List
    65 	 */
    74 	 */
    66 	public $inner_blocks = array();
    75 	public $inner_blocks = array();
    67 
    76 
    68 	/**
    77 	/**
    69 	 * Resultant HTML from inside block comment delimiters after removing inner
    78 	 * Resultant HTML from inside block comment delimiters after removing inner
   112 		$this->name         = $block['blockName'];
   121 		$this->name         = $block['blockName'];
   113 
   122 
   114 		if ( is_null( $registry ) ) {
   123 		if ( is_null( $registry ) ) {
   115 			$registry = WP_Block_Type_Registry::get_instance();
   124 			$registry = WP_Block_Type_Registry::get_instance();
   116 		}
   125 		}
       
   126 
       
   127 		$this->registry = $registry;
   117 
   128 
   118 		$this->block_type = $registry->get_registered( $this->name );
   129 		$this->block_type = $registry->get_registered( $this->name );
   119 
   130 
   120 		$this->available_context = $available_context;
   131 		$this->available_context = $available_context;
   121 
   132 
   183 	 * Generates the render output for the block.
   194 	 * Generates the render output for the block.
   184 	 *
   195 	 *
   185 	 * @since 5.5.0
   196 	 * @since 5.5.0
   186 	 *
   197 	 *
   187 	 * @param array $options {
   198 	 * @param array $options {
   188 	 *   Optional options object.
   199 	 *     Optional options object.
   189 	 *
   200 	 *
   190 	 *   @type bool $dynamic Defaults to 'true'. Optionally set to false to avoid using the block's render_callback.
   201 	 *     @type bool $dynamic Defaults to 'true'. Optionally set to false to avoid using the block's render_callback.
   191 	 * }
   202 	 * }
   192 	 * @return string Rendered block output.
   203 	 * @return string Rendered block output.
   193 	 */
   204 	 */
   194 	public function render( $options = array() ) {
   205 	public function render( $options = array() ) {
   195 		global $post;
   206 		global $post;
   203 		$is_dynamic    = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic();
   214 		$is_dynamic    = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic();
   204 		$block_content = '';
   215 		$block_content = '';
   205 
   216 
   206 		if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) {
   217 		if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) {
   207 			$index = 0;
   218 			$index = 0;
       
   219 
   208 			foreach ( $this->inner_content as $chunk ) {
   220 			foreach ( $this->inner_content as $chunk ) {
   209 				$block_content .= is_string( $chunk ) ?
   221 				if ( is_string( $chunk ) ) {
   210 					$chunk :
   222 					$block_content .= $chunk;
   211 					$this->inner_blocks[ $index++ ]->render();
   223 				} else {
       
   224 					$inner_block  = $this->inner_blocks[ $index ];
       
   225 					$parent_block = $this;
       
   226 
       
   227 					/** This filter is documented in wp-includes/blocks.php */
       
   228 					$pre_render = apply_filters( 'pre_render_block', null, $inner_block->parsed_block, $parent_block );
       
   229 
       
   230 					if ( ! is_null( $pre_render ) ) {
       
   231 						$block_content .= $pre_render;
       
   232 					} else {
       
   233 						$source_block = $inner_block->parsed_block;
       
   234 
       
   235 						/** This filter is documented in wp-includes/blocks.php */
       
   236 						$inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block );
       
   237 
       
   238 						/** This filter is documented in wp-includes/blocks.php */
       
   239 						$inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block );
       
   240 
       
   241 						$block_content .= $inner_block->render();
       
   242 					}
       
   243 
       
   244 					$index++;
       
   245 				}
   212 			}
   246 			}
   213 		}
   247 		}
   214 
   248 
   215 		if ( $is_dynamic ) {
   249 		if ( $is_dynamic ) {
   216 			$global_post = $post;
   250 			$global_post = $post;
   227 
   261 
   228 		if ( ! empty( $this->block_type->script ) ) {
   262 		if ( ! empty( $this->block_type->script ) ) {
   229 			wp_enqueue_script( $this->block_type->script );
   263 			wp_enqueue_script( $this->block_type->script );
   230 		}
   264 		}
   231 
   265 
       
   266 		if ( ! empty( $this->block_type->view_script ) && empty( $this->block_type->render_callback ) ) {
       
   267 			wp_enqueue_script( $this->block_type->view_script );
       
   268 		}
       
   269 
   232 		if ( ! empty( $this->block_type->style ) ) {
   270 		if ( ! empty( $this->block_type->style ) ) {
   233 			wp_enqueue_style( $this->block_type->style );
   271 			wp_enqueue_style( $this->block_type->style );
   234 		}
   272 		}
   235 
   273 
   236 		/**
   274 		/**
   237 		 * Filters the content of a single block.
   275 		 * Filters the content of a single block.
   238 		 *
   276 		 *
   239 		 * @since 5.0.0
   277 		 * @since 5.0.0
   240 		 *
   278 		 * @since 5.9.0 The `$instance` parameter was added.
   241 		 * @param string $block_content The block content about to be appended.
   279 		 *
   242 		 * @param array  $block         The full block, including name and attributes.
   280 		 * @param string   $block_content The block content about to be appended.
       
   281 		 * @param array    $block         The full block, including name and attributes.
       
   282 		 * @param WP_Block $instance      The block instance.
   243 		 */
   283 		 */
   244 		$block_content = apply_filters( 'render_block', $block_content, $this->parsed_block );
   284 		$block_content = apply_filters( 'render_block', $block_content, $this->parsed_block, $this );
   245 
   285 
   246 		/**
   286 		/**
   247 		 * Filters the content of a single block.
   287 		 * Filters the content of a single block.
   248 		 *
   288 		 *
   249 		 * The dynamic portion of the hook name, `$name`, refers to
   289 		 * The dynamic portion of the hook name, `$name`, refers to
   250 		 * the block name, e.g. "core/paragraph".
   290 		 * the block name, e.g. "core/paragraph".
   251 		 *
   291 		 *
   252 		 * @since 5.7.0
   292 		 * @since 5.7.0
   253 		 *
   293 		 * @since 5.9.0 The `$instance` parameter was added.
   254 		 * @param string $block_content The block content about to be appended.
   294 		 *
   255 		 * @param array  $block         The full block, including name and attributes.
   295 		 * @param string   $block_content The block content about to be appended.
       
   296 		 * @param array    $block         The full block, including name and attributes.
       
   297 		 * @param WP_Block $instance      The block instance.
   256 		 */
   298 		 */
   257 		$block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block );
   299 		$block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block, $this );
   258 
   300 
   259 		return $block_content;
   301 		return $block_content;
   260 	}
   302 	}
   261 
   303 
   262 }
   304 }