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