/wp-includes/class-wp-block-type.php

https://gitlab.com/campus-academy/krowkaramel · PHP · 373 lines · 79 code · 40 blank · 254 comment · 5 complexity · 040d270a45ce02611b84d3c73ee47c08 MD5 · raw file

  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 API version.
  19. *
  20. * @since 5.6.0
  21. * @var int
  22. */
  23. public $api_version = 1;
  24. /**
  25. * Block type key.
  26. *
  27. * @since 5.0.0
  28. * @var string
  29. */
  30. public $name;
  31. /**
  32. * Human-readable block type label.
  33. *
  34. * @since 5.5.0
  35. * @var string
  36. */
  37. public $title = '';
  38. /**
  39. * Block type category classification, used in search interfaces
  40. * to arrange block types by category.
  41. *
  42. * @since 5.5.0
  43. * @var string|null
  44. */
  45. public $category = null;
  46. /**
  47. * Setting parent lets a block require that it is only available
  48. * when nested within the specified blocks.
  49. *
  50. * @since 5.5.0
  51. * @var array|null
  52. */
  53. public $parent = null;
  54. /**
  55. * Block type icon.
  56. *
  57. * @since 5.5.0
  58. * @var string|null
  59. */
  60. public $icon = null;
  61. /**
  62. * A detailed block type description.
  63. *
  64. * @since 5.5.0
  65. * @var string
  66. */
  67. public $description = '';
  68. /**
  69. * Additional keywords to produce block type as result
  70. * in search interfaces.
  71. *
  72. * @since 5.5.0
  73. * @var array
  74. */
  75. public $keywords = array();
  76. /**
  77. * The translation textdomain.
  78. *
  79. * @since 5.5.0
  80. * @var string|null
  81. */
  82. public $textdomain = null;
  83. /**
  84. * Alternative block styles.
  85. *
  86. * @since 5.5.0
  87. * @var array
  88. */
  89. public $styles = array();
  90. /**
  91. * Block variations.
  92. *
  93. * @since 5.8.0
  94. * @var array
  95. */
  96. public $variations = array();
  97. /**
  98. * Supported features.
  99. *
  100. * @since 5.5.0
  101. * @var array|null
  102. */
  103. public $supports = null;
  104. /**
  105. * Structured data for the block preview.
  106. *
  107. * @since 5.5.0
  108. * @var array|null
  109. */
  110. public $example = null;
  111. /**
  112. * Block type render callback.
  113. *
  114. * @since 5.0.0
  115. * @var callable
  116. */
  117. public $render_callback = null;
  118. /**
  119. * Block type attributes property schemas.
  120. *
  121. * @since 5.0.0
  122. * @var array|null
  123. */
  124. public $attributes = null;
  125. /**
  126. * Context values inherited by blocks of this type.
  127. *
  128. * @since 5.5.0
  129. * @var array
  130. */
  131. public $uses_context = array();
  132. /**
  133. * Context provided by blocks of this type.
  134. *
  135. * @since 5.5.0
  136. * @var array|null
  137. */
  138. public $provides_context = null;
  139. /**
  140. * Block type editor only script handle.
  141. *
  142. * @since 5.0.0
  143. * @var string|null
  144. */
  145. public $editor_script = null;
  146. /**
  147. * Block type front end and editor script handle.
  148. *
  149. * @since 5.0.0
  150. * @var string|null
  151. */
  152. public $script = null;
  153. /**
  154. * Block type front end only script handle.
  155. *
  156. * @since 5.9.0
  157. * @var string|null
  158. */
  159. public $view_script = null;
  160. /**
  161. * Block type editor only style handle.
  162. *
  163. * @since 5.0.0
  164. * @var string|null
  165. */
  166. public $editor_style = null;
  167. /**
  168. * Block type front end and editor style handle.
  169. *
  170. * @since 5.0.0
  171. * @var string|null
  172. */
  173. public $style = null;
  174. /**
  175. * Constructor.
  176. *
  177. * Will populate object properties from the provided arguments.
  178. *
  179. * @since 5.0.0
  180. * @since 5.5.0 Added the `title`, `category`, `parent`, `icon`, `description`,
  181. * `keywords`, `textdomain`, `styles`, `supports`, `example`,
  182. * `uses_context`, and `provides_context` properties.
  183. * @since 5.6.0 Added the `api_version` property.
  184. * @since 5.8.0 Added the `variations` property.
  185. * @since 5.9.0 Added the `view_script` property.
  186. *
  187. * @see register_block_type()
  188. *
  189. * @param string $block_type Block type name including namespace.
  190. * @param array|string $args {
  191. * Optional. Array or string of arguments for registering a block type. Any arguments may be defined,
  192. * however the ones described below are supported by default. Default empty array.
  193. *
  194. * @type string $api_version Block API version.
  195. * @type string $title Human-readable block type label.
  196. * @type string|null $category Block type category classification, used in
  197. * search interfaces to arrange block types by category.
  198. * @type array|null $parent Setting parent lets a block require that it is only
  199. * available when nested within the specified blocks.
  200. * @type string|null $icon Block type icon.
  201. * @type string $description A detailed block type description.
  202. * @type array $keywords Additional keywords to produce block type as
  203. * result in search interfaces.
  204. * @type string|null $textdomain The translation textdomain.
  205. * @type array $styles Alternative block styles.
  206. * @type array $variations Block variations.
  207. * @type array|null $supports Supported features.
  208. * @type array|null $example Structured data for the block preview.
  209. * @type callable|null $render_callback Block type render callback.
  210. * @type array|null $attributes Block type attributes property schemas.
  211. * @type array $uses_context Context values inherited by blocks of this type.
  212. * @type array|null $provides_context Context provided by blocks of this type.
  213. * @type string|null $editor_script Block type editor only script handle.
  214. * @type string|null $script Block type front end and editor script handle.
  215. * @type string|null $view_script Block type front end only script handle.
  216. * @type string|null $editor_style Block type editor only style handle.
  217. * @type string|null $style Block type front end and editor style handle.
  218. * }
  219. */
  220. public function __construct( $block_type, $args = array() ) {
  221. $this->name = $block_type;
  222. $this->set_props( $args );
  223. }
  224. /**
  225. * Renders the block type output for given attributes.
  226. *
  227. * @since 5.0.0
  228. *
  229. * @param array $attributes Optional. Block attributes. Default empty array.
  230. * @param string $content Optional. Block content. Default empty string.
  231. * @return string Rendered block type output.
  232. */
  233. public function render( $attributes = array(), $content = '' ) {
  234. if ( ! $this->is_dynamic() ) {
  235. return '';
  236. }
  237. $attributes = $this->prepare_attributes_for_render( $attributes );
  238. return (string) call_user_func( $this->render_callback, $attributes, $content );
  239. }
  240. /**
  241. * Returns true if the block type is dynamic, or false otherwise. A dynamic
  242. * block is one which defers its rendering to occur on-demand at runtime.
  243. *
  244. * @since 5.0.0
  245. *
  246. * @return bool Whether block type is dynamic.
  247. */
  248. public function is_dynamic() {
  249. return is_callable( $this->render_callback );
  250. }
  251. /**
  252. * Validates attributes against the current block schema, populating
  253. * defaulted and missing values.
  254. *
  255. * @since 5.0.0
  256. *
  257. * @param array $attributes Original block attributes.
  258. * @return array Prepared block attributes.
  259. */
  260. public function prepare_attributes_for_render( $attributes ) {
  261. // If there are no attribute definitions for the block type, skip
  262. // processing and return verbatim.
  263. if ( ! isset( $this->attributes ) ) {
  264. return $attributes;
  265. }
  266. foreach ( $attributes as $attribute_name => $value ) {
  267. // If the attribute is not defined by the block type, it cannot be
  268. // validated.
  269. if ( ! isset( $this->attributes[ $attribute_name ] ) ) {
  270. continue;
  271. }
  272. $schema = $this->attributes[ $attribute_name ];
  273. // Validate value by JSON schema. An invalid value should revert to
  274. // its default, if one exists. This occurs by virtue of the missing
  275. // attributes loop immediately following. If there is not a default
  276. // assigned, the attribute value should remain unset.
  277. $is_valid = rest_validate_value_from_schema( $value, $schema, $attribute_name );
  278. if ( is_wp_error( $is_valid ) ) {
  279. unset( $attributes[ $attribute_name ] );
  280. }
  281. }
  282. // Populate values of any missing attributes for which the block type
  283. // defines a default.
  284. $missing_schema_attributes = array_diff_key( $this->attributes, $attributes );
  285. foreach ( $missing_schema_attributes as $attribute_name => $schema ) {
  286. if ( isset( $schema['default'] ) ) {
  287. $attributes[ $attribute_name ] = $schema['default'];
  288. }
  289. }
  290. return $attributes;
  291. }
  292. /**
  293. * Sets block type properties.
  294. *
  295. * @since 5.0.0
  296. *
  297. * @param array|string $args Array or string of arguments for registering a block type.
  298. * See WP_Block_Type::__construct() for information on accepted arguments.
  299. */
  300. public function set_props( $args ) {
  301. $args = wp_parse_args(
  302. $args,
  303. array(
  304. 'render_callback' => null,
  305. )
  306. );
  307. $args['name'] = $this->name;
  308. /**
  309. * Filters the arguments for registering a block type.
  310. *
  311. * @since 5.5.0
  312. *
  313. * @param array $args Array of arguments for registering a block type.
  314. * @param string $block_type Block type name including namespace.
  315. */
  316. $args = apply_filters( 'register_block_type_args', $args, $this->name );
  317. foreach ( $args as $property_name => $property_value ) {
  318. $this->$property_name = $property_value;
  319. }
  320. }
  321. /**
  322. * Get all available block attributes including possible layout attribute from Columns block.
  323. *
  324. * @since 5.0.0
  325. *
  326. * @return array Array of attributes.
  327. */
  328. public function get_attributes() {
  329. return is_array( $this->attributes ) ?
  330. $this->attributes :
  331. array();
  332. }
  333. }