Няма описание

class-wp-block.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * Blocks API: WP_Block class
  4. *
  5. * @package WordPress
  6. * @since 5.5.0
  7. */
  8. /**
  9. * Class representing a parsed instance of a block.
  10. *
  11. * @since 5.5.0
  12. * @property array $attributes
  13. */
  14. class WP_Block {
  15. /**
  16. * Original parsed array representation of block.
  17. *
  18. * @since 5.5.0
  19. * @var array
  20. */
  21. public $parsed_block;
  22. /**
  23. * Name of block.
  24. *
  25. * @example "core/paragraph"
  26. *
  27. * @since 5.5.0
  28. * @var string
  29. */
  30. public $name;
  31. /**
  32. * Block type associated with the instance.
  33. *
  34. * @since 5.5.0
  35. * @var WP_Block_Type
  36. */
  37. public $block_type;
  38. /**
  39. * Block context values.
  40. *
  41. * @since 5.5.0
  42. * @var array
  43. */
  44. public $context = array();
  45. /**
  46. * All available context of the current hierarchy.
  47. *
  48. * @since 5.5.0
  49. * @var array
  50. * @access protected
  51. */
  52. protected $available_context;
  53. /**
  54. * List of inner blocks (of this same class)
  55. *
  56. * @since 5.5.0
  57. * @var WP_Block[]
  58. */
  59. public $inner_blocks = array();
  60. /**
  61. * Resultant HTML from inside block comment delimiters after removing inner
  62. * blocks.
  63. *
  64. * @example "...Just <!-- wp:test /--> testing..." -> "Just testing..."
  65. *
  66. * @since 5.5.0
  67. * @var string
  68. */
  69. public $inner_html = '';
  70. /**
  71. * List of string fragments and null markers where inner blocks were found
  72. *
  73. * @example array(
  74. * 'inner_html' => 'BeforeInnerAfter',
  75. * 'inner_blocks' => array( block, block ),
  76. * 'inner_content' => array( 'Before', null, 'Inner', null, 'After' ),
  77. * )
  78. *
  79. * @since 5.5.0
  80. * @var array
  81. */
  82. public $inner_content = array();
  83. /**
  84. * Constructor.
  85. *
  86. * Populates object properties from the provided block instance argument.
  87. *
  88. * The given array of context values will not necessarily be available on
  89. * the instance itself, but is treated as the full set of values provided by
  90. * the block's ancestry. This is assigned to the private `available_context`
  91. * property. Only values which are configured to consumed by the block via
  92. * its registered type will be assigned to the block's `context` property.
  93. *
  94. * @since 5.5.0
  95. *
  96. * @param array $block Array of parsed block properties.
  97. * @param array $available_context Optional array of ancestry context values.
  98. * @param WP_Block_Type_Registry $registry Optional block type registry.
  99. */
  100. public function __construct( $block, $available_context = array(), $registry = null ) {
  101. $this->parsed_block = $block;
  102. $this->name = $block['blockName'];
  103. if ( is_null( $registry ) ) {
  104. $registry = WP_Block_Type_Registry::get_instance();
  105. }
  106. $this->block_type = $registry->get_registered( $this->name );
  107. $this->available_context = $available_context;
  108. if ( ! empty( $this->block_type->uses_context ) ) {
  109. foreach ( $this->block_type->uses_context as $context_name ) {
  110. if ( array_key_exists( $context_name, $this->available_context ) ) {
  111. $this->context[ $context_name ] = $this->available_context[ $context_name ];
  112. }
  113. }
  114. }
  115. if ( ! empty( $block['innerBlocks'] ) ) {
  116. $child_context = $this->available_context;
  117. if ( ! empty( $this->block_type->provides_context ) ) {
  118. foreach ( $this->block_type->provides_context as $context_name => $attribute_name ) {
  119. if ( array_key_exists( $attribute_name, $this->attributes ) ) {
  120. $child_context[ $context_name ] = $this->attributes[ $attribute_name ];
  121. }
  122. }
  123. }
  124. $this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry );
  125. }
  126. if ( ! empty( $block['innerHTML'] ) ) {
  127. $this->inner_html = $block['innerHTML'];
  128. }
  129. if ( ! empty( $block['innerContent'] ) ) {
  130. $this->inner_content = $block['innerContent'];
  131. }
  132. }
  133. /**
  134. * Returns a value from an inaccessible property.
  135. *
  136. * This is used to lazily initialize the `attributes` property of a block,
  137. * such that it is only prepared with default attributes at the time that
  138. * the property is accessed. For all other inaccessible properties, a `null`
  139. * value is returned.
  140. *
  141. * @since 5.5.0
  142. *
  143. * @param string $name Property name.
  144. * @return array|null Prepared attributes, or null.
  145. */
  146. public function __get( $name ) {
  147. if ( 'attributes' === $name ) {
  148. $this->attributes = isset( $this->parsed_block['attrs'] ) ?
  149. $this->parsed_block['attrs'] :
  150. array();
  151. if ( ! is_null( $this->block_type ) ) {
  152. $this->attributes = $this->block_type->prepare_attributes_for_render( $this->attributes );
  153. }
  154. return $this->attributes;
  155. }
  156. return null;
  157. }
  158. /**
  159. * Generates the render output for the block.
  160. *
  161. * @since 5.5.0
  162. *
  163. * @param array $options {
  164. * Optional options object.
  165. *
  166. * @type bool $dynamic Defaults to 'true'. Optionally set to false to avoid using the block's render_callback.
  167. * }
  168. * @return string Rendered block output.
  169. */
  170. public function render( $options = array() ) {
  171. global $post;
  172. $options = wp_parse_args(
  173. $options,
  174. array(
  175. 'dynamic' => true,
  176. )
  177. );
  178. $is_dynamic = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic();
  179. $block_content = '';
  180. if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) {
  181. $index = 0;
  182. foreach ( $this->inner_content as $chunk ) {
  183. $block_content .= is_string( $chunk ) ?
  184. $chunk :
  185. $this->inner_blocks[ $index++ ]->render();
  186. }
  187. }
  188. if ( $is_dynamic ) {
  189. $global_post = $post;
  190. $parent = WP_Block_Supports::$block_to_render;
  191. WP_Block_Supports::$block_to_render = $this->parsed_block;
  192. $block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this );
  193. WP_Block_Supports::$block_to_render = $parent;
  194. $post = $global_post;
  195. }
  196. if ( ! empty( $this->block_type->script ) ) {
  197. wp_enqueue_script( $this->block_type->script );
  198. }
  199. if ( ! empty( $this->block_type->style ) ) {
  200. wp_enqueue_style( $this->block_type->style );
  201. }
  202. /**
  203. * Filters the content of a single block.
  204. *
  205. * @since 5.0.0
  206. *
  207. * @param string $block_content The block content about to be appended.
  208. * @param array $block The full block, including name and attributes.
  209. */
  210. $block_content = apply_filters( 'render_block', $block_content, $this->parsed_block );
  211. /**
  212. * Filters the content of a single block.
  213. *
  214. * The dynamic portion of the hook name, `$name`, refers to
  215. * the block name, e.g. "core/paragraph".
  216. *
  217. * @since 5.7.0
  218. *
  219. * @param string $block_content The block content about to be appended.
  220. * @param array $block The full block, including name and attributes.
  221. */
  222. $block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block );
  223. return $block_content;
  224. }
  225. }