1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\editor; |
4
|
|
|
|
5
|
|
|
use EE_Error; |
6
|
|
|
use EventEspresso\core\domain\entities\editor\BlockCollection; |
7
|
|
|
use EventEspresso\core\domain\entities\editor\BlockInterface; |
8
|
|
|
use EventEspresso\core\exceptions\ExceptionStackTraceDisplay; |
9
|
|
|
use EventEspresso\core\exceptions\InvalidClassException; |
10
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
11
|
|
|
use EventEspresso\core\exceptions\InvalidEntityException; |
12
|
|
|
use EventEspresso\core\exceptions\InvalidFilePathException; |
13
|
|
|
use EventEspresso\core\exceptions\InvalidIdentifierException; |
14
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
15
|
|
|
use EventEspresso\core\services\assets\AssetRegisterCollection; |
16
|
|
|
use EventEspresso\core\services\collections\CollectionDetails; |
17
|
|
|
use EventEspresso\core\services\collections\CollectionInterface; |
18
|
|
|
use EventEspresso\core\services\collections\CollectionLoader; |
19
|
|
|
use EventEspresso\core\services\request\RequestInterface; |
20
|
|
|
use Exception; |
21
|
|
|
use InvalidArgumentException; |
22
|
|
|
use ReflectionException; |
23
|
|
|
use WP_Block_Type; |
24
|
|
|
|
25
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class BlockRegistrationManager |
31
|
|
|
* Loads BlockInterface classes into the injected BlockCollection, |
32
|
|
|
* which can be used in other classes by retrieving it from EE's Loader. |
33
|
|
|
* After loading, the BlockManager gets each Block to register |
34
|
|
|
* its block type and ensures assets are enqueued at the appropriate time |
35
|
|
|
* |
36
|
|
|
* @package EventEspresso\core\domain\services\editor |
37
|
|
|
* @author Brent Christensen |
38
|
|
|
* @since $VID:$ |
39
|
|
|
*/ |
40
|
|
|
class BlockRegistrationManager extends BlockManager |
41
|
|
|
{ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var AssetRegisterCollection $asset_register_collection |
45
|
|
|
*/ |
46
|
|
|
protected $asset_register_collection; |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* BlockRegistrationManager constructor. |
51
|
|
|
* |
52
|
|
|
* @param AssetRegisterCollection $asset_register_collection |
53
|
|
|
* @param BlockCollection $blocks |
54
|
|
|
* @param RequestInterface $request |
55
|
|
|
*/ |
56
|
|
|
public function __construct( |
57
|
|
|
AssetRegisterCollection $asset_register_collection, |
58
|
|
|
BlockCollection $blocks, |
59
|
|
|
RequestInterface $request |
60
|
|
|
) { |
61
|
|
|
$this->asset_register_collection = $asset_register_collection; |
62
|
|
|
parent::__construct($blocks, $request); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the name of a hookpoint to be used to call initialize() |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function init_hook() |
72
|
|
|
{ |
73
|
|
|
return 'AHEE__EE_System__set_hooks_for_core'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Perform any early setup required for block editors to functions |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
* @throws Exception |
82
|
|
|
*/ |
83
|
|
|
public function initialize() |
84
|
|
|
{ |
85
|
|
|
$this->loadBlocks(); |
86
|
|
|
add_action('AHEE__EE_System__initialize', array($this, 'registerBlocks')); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return CollectionInterface|BlockInterface[] |
92
|
|
|
* @throws ReflectionException |
93
|
|
|
* @throws InvalidArgumentException |
94
|
|
|
* @throws EE_Error |
95
|
|
|
* @throws InvalidClassException |
96
|
|
|
* @throws InvalidDataTypeException |
97
|
|
|
* @throws InvalidEntityException |
98
|
|
|
* @throws InvalidFilePathException |
99
|
|
|
* @throws InvalidIdentifierException |
100
|
|
|
* @throws InvalidInterfaceException |
101
|
|
|
*/ |
102
|
|
|
protected function populateBlockCollection() |
103
|
|
|
{ |
104
|
|
|
$loader = new CollectionLoader( |
105
|
|
|
new CollectionDetails( |
106
|
|
|
// collection name |
107
|
|
|
'shortcodes', |
108
|
|
|
// collection interface |
109
|
|
|
'EventEspresso\core\domain\entities\editor\BlockInterface', |
110
|
|
|
// FQCNs for classes to add (all classes within each namespace will be loaded) |
111
|
|
|
apply_filters( |
112
|
|
|
'FHEE__EventEspresso_core_services_editor_BlockManager__populateBlockCollection__collection_FQCNs', |
113
|
|
|
array( |
114
|
|
|
// 'EventEspresso\core\domain\entities\editor\blocks\common', |
115
|
|
|
// 'EventEspresso\core\domain\entities\editor\blocks\editor', |
116
|
|
|
'EventEspresso\core\domain\entities\editor\blocks\widgets', |
117
|
|
|
) |
118
|
|
|
), |
119
|
|
|
// filepaths to classes to add |
120
|
|
|
array(), |
121
|
|
|
// file mask to use if parsing folder for files to add |
122
|
|
|
'', |
123
|
|
|
// what to use as identifier for collection entities |
124
|
|
|
// using CLASS NAME prevents duplicates (works like a singleton) |
125
|
|
|
CollectionDetails::ID_CLASS_NAME |
126
|
|
|
), |
127
|
|
|
$this->blocks |
128
|
|
|
); |
129
|
|
|
return $loader->getCollection(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* populates the BlockCollection and calls initialize() on all installed blocks |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
* @throws Exception |
138
|
|
|
*/ |
139
|
|
|
public function loadBlocks() |
140
|
|
|
{ |
141
|
|
|
try { |
142
|
|
|
$this->populateBlockCollection(); |
143
|
|
|
// cycle thru block loaders and initialize each loader |
144
|
|
|
foreach ($this->blocks as $block) { |
145
|
|
|
$block->initialize(); |
146
|
|
|
} |
147
|
|
|
} catch (Exception $exception) { |
148
|
|
|
new ExceptionStackTraceDisplay($exception); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* calls registerBlock() and load assets for all installed blocks |
155
|
|
|
* |
156
|
|
|
* @return void |
157
|
|
|
* @throws Exception |
158
|
|
|
*/ |
159
|
|
|
public function registerBlocks() |
160
|
|
|
{ |
161
|
|
|
try { |
162
|
|
|
// cycle thru block loader folders |
163
|
|
|
foreach ($this->blocks as $block) { |
164
|
|
|
// perform any setup required for the block |
165
|
|
|
$block_type = $block->registerBlock(); |
166
|
|
|
if (! $block_type instanceof WP_Block_Type) { |
|
|
|
|
167
|
|
|
throw new InvalidEntityException($block_type, 'WP_Block_Type'); |
168
|
|
|
} |
169
|
|
|
if (! $this->asset_register_collection->has($block->assetRegister())) { |
170
|
|
|
$this->asset_register_collection->add($block->assetRegister()); |
171
|
|
|
} |
172
|
|
|
do_action( |
173
|
|
|
'FHEE__EventEspresso_core_services_editor_BlockManager__registerBlocks__block_type_registered', |
174
|
|
|
$block, |
175
|
|
|
$block_type |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
if ($this->asset_register_collection->hasObjects()) { |
179
|
|
|
$this->asset_register_collection->registerManifestFile(); |
180
|
|
|
// register primary assets |
181
|
|
|
add_action('enqueue_block_assets', array($this, 'registerAssets')); |
182
|
|
|
} |
183
|
|
|
} catch (Exception $exception) { |
184
|
|
|
new ExceptionStackTraceDisplay($exception); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Registers assets for all classes in the AssetRegisterCollection |
191
|
|
|
*/ |
192
|
|
|
public function registerAssets() |
193
|
|
|
{ |
194
|
|
|
$this->asset_register_collection->registerScripts(); |
195
|
|
|
$this->asset_register_collection->registerStyles(); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|