No Description

class-wp-block-type.php 10KB

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