class-wp-block-type.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Blocks API: WP_Block_Type class
  4. *
  5. * @package WordPress
  6. * @subpackage Blocks
  7. * @since 5.0.0
  8. */
  9. /**
  10. * Core class representing a block type.
  11. *
  12. * @since 5.0.0
  13. *
  14. * @see register_block_type()
  15. */
  16. class WP_Block_Type {
  17. /**
  18. * Block type key.
  19. *
  20. * @since 5.0.0
  21. * @var string
  22. */
  23. public $name;
  24. /**
  25. * Block type render callback.
  26. *
  27. * @since 5.0.0
  28. * @var callable
  29. */
  30. public $render_callback;
  31. /**
  32. * Block type attributes property schemas.
  33. *
  34. * @since 5.0.0
  35. * @var array
  36. */
  37. public $attributes;
  38. /**
  39. * Block type editor script handle.
  40. *
  41. * @since 5.0.0
  42. * @var string
  43. */
  44. public $editor_script;
  45. /**
  46. * Block type front end script handle.
  47. *
  48. * @since 5.0.0
  49. * @var string
  50. */
  51. public $script;
  52. /**
  53. * Block type editor style handle.
  54. *
  55. * @since 5.0.0
  56. * @var string
  57. */
  58. public $editor_style;
  59. /**
  60. * Block type front end style handle.
  61. *
  62. * @since 5.0.0
  63. * @var string
  64. */
  65. public $style;
  66. /**
  67. * Constructor.
  68. *
  69. * Will populate object properties from the provided arguments.
  70. *
  71. * @since 5.0.0
  72. *
  73. * @see register_block_type()
  74. *
  75. * @param string $block_type Block type name including namespace.
  76. * @param array|string $args Optional. Array or string of arguments for registering a block type.
  77. * Default empty array.
  78. */
  79. public function __construct( $block_type, $args = array() ) {
  80. $this->name = $block_type;
  81. $this->set_props( $args );
  82. }
  83. /**
  84. * Renders the block type output for given attributes.
  85. *
  86. * @since 5.0.0
  87. *
  88. * @param array $attributes Optional. Block attributes. Default empty array.
  89. * @param string $content Optional. Block content. Default empty string.
  90. * @return string Rendered block type output.
  91. */
  92. public function render( $attributes = array(), $content = '' ) {
  93. if ( ! $this->is_dynamic() ) {
  94. return '';
  95. }
  96. $attributes = $this->prepare_attributes_for_render( $attributes );
  97. return (string) call_user_func( $this->render_callback, $attributes, $content );
  98. }
  99. /**
  100. * Returns true if the block type is dynamic, or false otherwise. A dynamic
  101. * block is one which defers its rendering to occur on-demand at runtime.
  102. *
  103. * @since 5.0.0
  104. *
  105. * @return boolean Whether block type is dynamic.
  106. */
  107. public function is_dynamic() {
  108. return is_callable( $this->render_callback );
  109. }
  110. /**
  111. * Validates attributes against the current block schema, populating
  112. * defaulted and missing values.
  113. *
  114. * @since 5.0.0
  115. *
  116. * @param array $attributes Original block attributes.
  117. * @return array Prepared block attributes.
  118. */
  119. public function prepare_attributes_for_render( $attributes ) {
  120. // If there are no attribute definitions for the block type, skip
  121. // processing and return vebatim.
  122. if ( ! isset( $this->attributes ) ) {
  123. return $attributes;
  124. }
  125. foreach ( $attributes as $attribute_name => $value ) {
  126. // If the attribute is not defined by the block type, it cannot be
  127. // validated.
  128. if ( ! isset( $this->attributes[ $attribute_name ] ) ) {
  129. continue;
  130. }
  131. $schema = $this->attributes[ $attribute_name ];
  132. // Validate value by JSON schema. An invalid value should revert to
  133. // its default, if one exists. This occurs by virtue of the missing
  134. // attributes loop immediately following. If there is not a default
  135. // assigned, the attribute value should remain unset.
  136. $is_valid = rest_validate_value_from_schema( $value, $schema );
  137. if ( is_wp_error( $is_valid ) ) {
  138. unset( $attributes[ $attribute_name ] );
  139. }
  140. }
  141. // Populate values of any missing attributes for which the block type
  142. // defines a default.
  143. $missing_schema_attributes = array_diff_key( $this->attributes, $attributes );
  144. foreach ( $missing_schema_attributes as $attribute_name => $schema ) {
  145. if ( isset( $schema['default'] ) ) {
  146. $attributes[ $attribute_name ] = $schema['default'];
  147. }
  148. }
  149. return $attributes;
  150. }
  151. /**
  152. * Sets block type properties.
  153. *
  154. * @since 5.0.0
  155. *
  156. * @param array|string $args Array or string of arguments for registering a block type.
  157. */
  158. public function set_props( $args ) {
  159. $args = wp_parse_args(
  160. $args,
  161. array(
  162. 'render_callback' => null,
  163. )
  164. );
  165. $args['name'] = $this->name;
  166. foreach ( $args as $property_name => $property_value ) {
  167. $this->$property_name = $property_value;
  168. }
  169. }
  170. /**
  171. * Get all available block attributes including possible layout attribute from Columns block.
  172. *
  173. * @since 5.0.0
  174. *
  175. * @return array Array of attributes.
  176. */
  177. public function get_attributes() {
  178. return is_array( $this->attributes ) ?
  179. array_merge(
  180. $this->attributes,
  181. array(
  182. 'layout' => array(
  183. 'type' => 'string',
  184. ),
  185. )
  186. ) :
  187. array(
  188. 'layout' => array(
  189. 'type' => 'string',
  190. ),
  191. );
  192. }
  193. }