BlockModel.php
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\Blocks; |
| 4 | |
| 5 | use Give\Framework\Support\Contracts\Arrayable; |
| 6 | |
| 7 | /** |
| 8 | * @since 3.0.0 |
| 9 | * A structured model for a Gutenberg block. |
| 10 | * Similar to WP_Block_Parser_Block, but without innerHTML or innerContent. |
| 11 | * ... and without the HTML comments as structure. |
| 12 | * ... but now with a Collection for innerBlocks :) |
| 13 | */ |
| 14 | class BlockModel implements Arrayable |
| 15 | { |
| 16 | /** @var string */ |
| 17 | public $name; |
| 18 | |
| 19 | /** @var string */ |
| 20 | public $clientId; |
| 21 | |
| 22 | /** @var bool */ |
| 23 | public $isValid; |
| 24 | |
| 25 | /** @var array */ |
| 26 | protected $attributes; |
| 27 | |
| 28 | /** @var BlockCollection */ |
| 29 | public $innerBlocks; |
| 30 | |
| 31 | /** |
| 32 | * @since 3.1.0 added innerBlocks sanitization |
| 33 | * @since 3.0.0 |
| 34 | * @param string $name |
| 35 | * @param string $clientId |
| 36 | * @param bool $isValid |
| 37 | * @param array $attributes |
| 38 | * @param BlockCollection|null $innerBlocks |
| 39 | */ |
| 40 | public function __construct( |
| 41 | string $name, |
| 42 | string $clientId = null, |
| 43 | bool $isValid = true, |
| 44 | array $attributes = [], |
| 45 | $innerBlocks = null |
| 46 | ) { |
| 47 | $this->name = $name; |
| 48 | $this->clientId = $clientId ?? wp_generate_uuid4(); |
| 49 | $this->isValid = $isValid; |
| 50 | $this->attributes = $attributes; |
| 51 | $this->innerBlocks = $this->sanitizeInnerBlocks($innerBlocks); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @since 3.1.0 |
| 56 | * |
| 57 | * @param array|BlockCollection|null $innerBlocks |
| 58 | */ |
| 59 | public function sanitizeInnerBlocks($innerBlocks): BlockCollection |
| 60 | { |
| 61 | if (empty($innerBlocks)) { |
| 62 | return new BlockCollection([]); |
| 63 | } |
| 64 | |
| 65 | if (is_a($innerBlocks, BlockCollection::class)) { |
| 66 | return $innerBlocks; |
| 67 | } |
| 68 | |
| 69 | return new BlockCollection( |
| 70 | array_map([__CLASS__, 'make'], |
| 71 | $innerBlocks) |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @since 3.0.0 |
| 77 | */ |
| 78 | public function hasAttribute($name): bool |
| 79 | { |
| 80 | return isset($this->attributes[$name]); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @since 3.0.0 |
| 85 | */ |
| 86 | public function getAttribute($name) |
| 87 | { |
| 88 | return $this->hasAttribute($name) ? $this->attributes[$name] : null; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @since 3.0.0 |
| 93 | */ |
| 94 | public function getAttributes(): array |
| 95 | { |
| 96 | return $this->attributes; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @since 3.0.0 |
| 101 | */ |
| 102 | public function setAttribute(string $name, $value): BlockModel |
| 103 | { |
| 104 | $this->attributes[$name] = $value; |
| 105 | |
| 106 | return $this; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns the unqualified, or short name, of the block without the namespace. |
| 111 | * |
| 112 | * @since 3.0.0 |
| 113 | */ |
| 114 | public function getShortName(): string |
| 115 | { |
| 116 | return substr($this->name, strpos($this->name, '/') + 1); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @since 3.1.0 simplified innerBlocks param |
| 121 | * @since 3.0.0 |
| 122 | * |
| 123 | * @param array $blockData |
| 124 | * @return BlockModel |
| 125 | */ |
| 126 | public static function make( array $blockData ): BlockModel |
| 127 | { |
| 128 | return new BlockModel( |
| 129 | $blockData['name'], |
| 130 | !empty($blockData['clientId']) ? $blockData['clientId'] : wp_generate_uuid4(), |
| 131 | !empty($blockData['isValid']) ? $blockData['isValid'] : true, |
| 132 | !empty($blockData['attributes']) ? $blockData['attributes'] : [], |
| 133 | $blockData['innerBlocks'] ?? [] |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @since 3.0.0 |
| 139 | */ |
| 140 | public function toArray(): array |
| 141 | { |
| 142 | return [ |
| 143 | 'name' => $this->name, |
| 144 | 'clientId' => $this->clientId, |
| 145 | 'isValid' => $this->isValid, |
| 146 | 'attributes' => $this->attributes, |
| 147 | 'innerBlocks' => $this->innerBlocks ? $this->innerBlocks->toArray() : [] |
| 148 | ]; |
| 149 | } |
| 150 | } |
| 151 |