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