PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / src / Framework / Blocks / BlockModel.php

BlockModel.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/Framework/Blocks/BlockModel.php

152 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 4.17.0 Declare the nullable parameter explicitly.
33 * @since 3.1.0 added innerBlocks sanitization
34 * @since 3.0.0
35 * @param string $name
36 * @param string $clientId
37 * @param bool $isValid
38 * @param array $attributes
39 * @param BlockCollection|null $innerBlocks
40 */
41 public function __construct(
42 string $name,
43 ?string $clientId = null,
44 bool $isValid = true,
45 array $attributes = [],
46 $innerBlocks = null
47 ) {
48 $this->name = $name;
49 $this->clientId = $clientId ?? wp_generate_uuid4();
50 $this->isValid = $isValid;
51 $this->attributes = $attributes;
52 $this->innerBlocks = $this->sanitizeInnerBlocks($innerBlocks);
53 }
54
55 /**
56 * @since 3.1.0
57 *
58 * @param array|BlockCollection|null $innerBlocks
59 */
60 public function sanitizeInnerBlocks($innerBlocks): BlockCollection
61 {
62 if (empty($innerBlocks)) {
63 return new BlockCollection([]);
64 }
65
66 if (is_a($innerBlocks, BlockCollection::class)) {
67 return $innerBlocks;
68 }
69
70 return new BlockCollection(
71 array_map([__CLASS__, 'make'],
72 $innerBlocks)
73 );
74 }
75
76 /**
77 * @since 3.0.0
78 */
79 public function hasAttribute($name): bool
80 {
81 return isset($this->attributes[$name]);
82 }
83
84 /**
85 * @since 3.0.0
86 */
87 public function getAttribute($name)
88 {
89 return $this->hasAttribute($name) ? $this->attributes[$name] : null;
90 }
91
92 /**
93 * @since 3.0.0
94 */
95 public function getAttributes(): array
96 {
97 return $this->attributes;
98 }
99
100 /**
101 * @since 3.0.0
102 */
103 public function setAttribute(string $name, $value): BlockModel
104 {
105 $this->attributes[$name] = $value;
106
107 return $this;
108 }
109
110 /**
111 * Returns the unqualified, or short name, of the block without the namespace.
112 *
113 * @since 3.0.0
114 */
115 public function getShortName(): string
116 {
117 return substr($this->name, strpos($this->name, '/') + 1);
118 }
119
120 /**
121 * @since 3.1.0 simplified innerBlocks param
122 * @since 3.0.0
123 *
124 * @param array $blockData
125 * @return BlockModel
126 */
127 public static function make( array $blockData ): BlockModel
128 {
129 return new BlockModel(
130 $blockData['name'],
131 !empty($blockData['clientId']) ? $blockData['clientId'] : wp_generate_uuid4(),
132 !empty($blockData['isValid']) ? $blockData['isValid'] : true,
133 !empty($blockData['attributes']) ? $blockData['attributes'] : [],
134 $blockData['innerBlocks'] ?? []
135 );
136 }
137
138 /**
139 * @since 3.0.0
140 */
141 public function toArray(): array
142 {
143 return [
144 'name' => $this->name,
145 'clientId' => $this->clientId,
146 'isValid' => $this->isValid,
147 'attributes' => $this->attributes,
148 'innerBlocks' => $this->innerBlocks ? $this->innerBlocks->toArray() : []
149 ];
150 }
151 }
152