@@ -33,228 +33,228 @@ |
||
33 | 33 | class BlockRegistrationManager extends BlockManager |
34 | 34 | { |
35 | 35 | |
36 | - /** |
|
37 | - * @var BlockAssetManagerCollection $block_asset_manager_collection |
|
38 | - */ |
|
39 | - protected $block_asset_manager_collection; |
|
36 | + /** |
|
37 | + * @var BlockAssetManagerCollection $block_asset_manager_collection |
|
38 | + */ |
|
39 | + protected $block_asset_manager_collection; |
|
40 | 40 | |
41 | - /** |
|
42 | - * @var RouteMatchSpecificationManager $route_manager |
|
43 | - */ |
|
44 | - protected $route_manager; |
|
41 | + /** |
|
42 | + * @var RouteMatchSpecificationManager $route_manager |
|
43 | + */ |
|
44 | + protected $route_manager; |
|
45 | 45 | |
46 | - /** |
|
47 | - * array for tracking asset managers required by blocks for the current route |
|
48 | - * |
|
49 | - * @var array $block_asset_managers |
|
50 | - */ |
|
51 | - protected $block_asset_managers = array(); |
|
46 | + /** |
|
47 | + * array for tracking asset managers required by blocks for the current route |
|
48 | + * |
|
49 | + * @var array $block_asset_managers |
|
50 | + */ |
|
51 | + protected $block_asset_managers = array(); |
|
52 | 52 | |
53 | 53 | |
54 | - /** |
|
55 | - * BlockRegistrationManager constructor. |
|
56 | - * |
|
57 | - * @param BlockAssetManagerCollection $block_asset_manager_collection |
|
58 | - * @param BlockCollection $blocks |
|
59 | - * @param RouteMatchSpecificationManager $route_manager |
|
60 | - * @param RequestInterface $request |
|
61 | - */ |
|
62 | - public function __construct( |
|
63 | - BlockAssetManagerCollection $block_asset_manager_collection, |
|
64 | - BlockCollection $blocks, |
|
65 | - RouteMatchSpecificationManager $route_manager, |
|
66 | - RequestInterface $request |
|
67 | - ) { |
|
68 | - $this->block_asset_manager_collection = $block_asset_manager_collection; |
|
69 | - $this->route_manager = $route_manager; |
|
70 | - parent::__construct($blocks, $request); |
|
71 | - } |
|
54 | + /** |
|
55 | + * BlockRegistrationManager constructor. |
|
56 | + * |
|
57 | + * @param BlockAssetManagerCollection $block_asset_manager_collection |
|
58 | + * @param BlockCollection $blocks |
|
59 | + * @param RouteMatchSpecificationManager $route_manager |
|
60 | + * @param RequestInterface $request |
|
61 | + */ |
|
62 | + public function __construct( |
|
63 | + BlockAssetManagerCollection $block_asset_manager_collection, |
|
64 | + BlockCollection $blocks, |
|
65 | + RouteMatchSpecificationManager $route_manager, |
|
66 | + RequestInterface $request |
|
67 | + ) { |
|
68 | + $this->block_asset_manager_collection = $block_asset_manager_collection; |
|
69 | + $this->route_manager = $route_manager; |
|
70 | + parent::__construct($blocks, $request); |
|
71 | + } |
|
72 | 72 | |
73 | 73 | |
74 | - /** |
|
75 | - * Returns the name of a hook point to be used to call initialize() |
|
76 | - * |
|
77 | - * @return string |
|
78 | - */ |
|
79 | - public function initHook() |
|
80 | - { |
|
81 | - return 'AHEE__EE_System__initialize'; |
|
82 | - } |
|
74 | + /** |
|
75 | + * Returns the name of a hook point to be used to call initialize() |
|
76 | + * |
|
77 | + * @return string |
|
78 | + */ |
|
79 | + public function initHook() |
|
80 | + { |
|
81 | + return 'AHEE__EE_System__initialize'; |
|
82 | + } |
|
83 | 83 | |
84 | 84 | |
85 | - /** |
|
86 | - * Perform any early setup required for block editors to functions |
|
87 | - * |
|
88 | - * @return void |
|
89 | - * @throws Exception |
|
90 | - */ |
|
91 | - public function initialize() |
|
92 | - { |
|
93 | - $this->initializeBlocks(); |
|
94 | - add_action('AHEE__EE_System__initialize_last', array($this, 'registerBlocks')); |
|
95 | - add_action('wp_loaded', array($this, 'unloadAssets')); |
|
96 | - add_filter('block_categories_all', array($this, 'addEspressoBlockCategories'), 10, 2); |
|
97 | - } |
|
85 | + /** |
|
86 | + * Perform any early setup required for block editors to functions |
|
87 | + * |
|
88 | + * @return void |
|
89 | + * @throws Exception |
|
90 | + */ |
|
91 | + public function initialize() |
|
92 | + { |
|
93 | + $this->initializeBlocks(); |
|
94 | + add_action('AHEE__EE_System__initialize_last', array($this, 'registerBlocks')); |
|
95 | + add_action('wp_loaded', array($this, 'unloadAssets')); |
|
96 | + add_filter('block_categories_all', array($this, 'addEspressoBlockCategories'), 10, 2); |
|
97 | + } |
|
98 | 98 | |
99 | 99 | |
100 | - /** |
|
101 | - * @param array $categories |
|
102 | - * @since 4.9.71.p |
|
103 | - * @return array |
|
104 | - */ |
|
105 | - public function addEspressoBlockCategories(array $categories, $block_editor_context): array |
|
106 | - { |
|
107 | - // $block_editor_context can be either an object or a string |
|
108 | - // so checking it here, thus no type hinting in function params |
|
109 | - if (! $block_editor_context instanceof WP_Block_Editor_Context) { |
|
110 | - return $categories; |
|
111 | - } |
|
112 | - return array_merge( |
|
113 | - $categories, |
|
114 | - [ |
|
115 | - [ |
|
116 | - 'slug' => 'event-espresso', |
|
117 | - 'title' => esc_html__('Event Espresso', 'event_espresso'), |
|
118 | - ] |
|
119 | - ] |
|
120 | - ); |
|
121 | - } |
|
100 | + /** |
|
101 | + * @param array $categories |
|
102 | + * @since 4.9.71.p |
|
103 | + * @return array |
|
104 | + */ |
|
105 | + public function addEspressoBlockCategories(array $categories, $block_editor_context): array |
|
106 | + { |
|
107 | + // $block_editor_context can be either an object or a string |
|
108 | + // so checking it here, thus no type hinting in function params |
|
109 | + if (! $block_editor_context instanceof WP_Block_Editor_Context) { |
|
110 | + return $categories; |
|
111 | + } |
|
112 | + return array_merge( |
|
113 | + $categories, |
|
114 | + [ |
|
115 | + [ |
|
116 | + 'slug' => 'event-espresso', |
|
117 | + 'title' => esc_html__('Event Espresso', 'event_espresso'), |
|
118 | + ] |
|
119 | + ] |
|
120 | + ); |
|
121 | + } |
|
122 | 122 | |
123 | 123 | |
124 | - /** |
|
125 | - * @return CollectionInterface|BlockInterface[] |
|
126 | - * @throws CollectionLoaderException |
|
127 | - * @throws CollectionDetailsException |
|
128 | - */ |
|
129 | - protected function populateBlockCollection() |
|
130 | - { |
|
131 | - $loader = new CollectionLoader( |
|
132 | - new CollectionDetails( |
|
133 | - // collection name |
|
134 | - 'editor_blocks', |
|
135 | - // collection interface |
|
136 | - 'EventEspresso\core\domain\entities\editor\BlockInterface', |
|
137 | - // FQCNs for classes to add (all classes within each namespace will be loaded) |
|
138 | - apply_filters( |
|
139 | - 'FHEE__EventEspresso_core_services_editor_BlockManager__populateBlockCollection__collection_FQCNs', |
|
140 | - array('EventEspresso\core\domain\entities\editor\blocks') |
|
141 | - ), |
|
142 | - // file paths to classes to add |
|
143 | - array(), |
|
144 | - // file mask to use if parsing folder for files to add |
|
145 | - '', |
|
146 | - // what to use as identifier for collection entities |
|
147 | - // using CLASS NAME prevents duplicates (works like a singleton) |
|
148 | - CollectionDetails::ID_CLASS_NAME |
|
149 | - ), |
|
150 | - $this->blocks |
|
151 | - ); |
|
152 | - return $loader->getCollection(); |
|
153 | - } |
|
124 | + /** |
|
125 | + * @return CollectionInterface|BlockInterface[] |
|
126 | + * @throws CollectionLoaderException |
|
127 | + * @throws CollectionDetailsException |
|
128 | + */ |
|
129 | + protected function populateBlockCollection() |
|
130 | + { |
|
131 | + $loader = new CollectionLoader( |
|
132 | + new CollectionDetails( |
|
133 | + // collection name |
|
134 | + 'editor_blocks', |
|
135 | + // collection interface |
|
136 | + 'EventEspresso\core\domain\entities\editor\BlockInterface', |
|
137 | + // FQCNs for classes to add (all classes within each namespace will be loaded) |
|
138 | + apply_filters( |
|
139 | + 'FHEE__EventEspresso_core_services_editor_BlockManager__populateBlockCollection__collection_FQCNs', |
|
140 | + array('EventEspresso\core\domain\entities\editor\blocks') |
|
141 | + ), |
|
142 | + // file paths to classes to add |
|
143 | + array(), |
|
144 | + // file mask to use if parsing folder for files to add |
|
145 | + '', |
|
146 | + // what to use as identifier for collection entities |
|
147 | + // using CLASS NAME prevents duplicates (works like a singleton) |
|
148 | + CollectionDetails::ID_CLASS_NAME |
|
149 | + ), |
|
150 | + $this->blocks |
|
151 | + ); |
|
152 | + return $loader->getCollection(); |
|
153 | + } |
|
154 | 154 | |
155 | 155 | |
156 | - /** |
|
157 | - * populates the BlockCollection and calls initialize() on all installed blocks |
|
158 | - * |
|
159 | - * @return void |
|
160 | - * @throws Exception |
|
161 | - */ |
|
162 | - public function initializeBlocks() |
|
163 | - { |
|
164 | - try { |
|
165 | - $this->populateBlockCollection(); |
|
166 | - // cycle thru block loaders and initialize each loader |
|
167 | - foreach ($this->blocks as $block) { |
|
168 | - $block->initialize(); |
|
169 | - $this->trackAssetManagersForBlocks($block); |
|
170 | - if (! $this->block_asset_manager_collection->has($block->assetManager())) { |
|
171 | - $this->block_asset_manager_collection->add($block->assetManager()); |
|
172 | - $block->assetManager()->setAssetHandles(); |
|
173 | - } |
|
174 | - } |
|
175 | - } catch (Exception $exception) { |
|
176 | - new ExceptionStackTraceDisplay($exception); |
|
177 | - } |
|
178 | - } |
|
156 | + /** |
|
157 | + * populates the BlockCollection and calls initialize() on all installed blocks |
|
158 | + * |
|
159 | + * @return void |
|
160 | + * @throws Exception |
|
161 | + */ |
|
162 | + public function initializeBlocks() |
|
163 | + { |
|
164 | + try { |
|
165 | + $this->populateBlockCollection(); |
|
166 | + // cycle thru block loaders and initialize each loader |
|
167 | + foreach ($this->blocks as $block) { |
|
168 | + $block->initialize(); |
|
169 | + $this->trackAssetManagersForBlocks($block); |
|
170 | + if (! $this->block_asset_manager_collection->has($block->assetManager())) { |
|
171 | + $this->block_asset_manager_collection->add($block->assetManager()); |
|
172 | + $block->assetManager()->setAssetHandles(); |
|
173 | + } |
|
174 | + } |
|
175 | + } catch (Exception $exception) { |
|
176 | + new ExceptionStackTraceDisplay($exception); |
|
177 | + } |
|
178 | + } |
|
179 | 179 | |
180 | 180 | |
181 | - /** |
|
182 | - * track blocks with routes that match the current request |
|
183 | - * |
|
184 | - * @param BlockInterface $block |
|
185 | - * @throws InvalidClassException |
|
186 | - */ |
|
187 | - private function trackAssetManagersForBlocks(BlockInterface $block) |
|
188 | - { |
|
189 | - $supported_routes = $block->supportedRoutes(); |
|
190 | - foreach ($supported_routes as $supported_route) { |
|
191 | - if ($this->route_manager->routeMatchesCurrentRequest($supported_route)) { |
|
192 | - $this->block_asset_managers[ $block->blockType() ] = $block->assetManager()->assetNamespace(); |
|
193 | - } |
|
194 | - } |
|
195 | - } |
|
181 | + /** |
|
182 | + * track blocks with routes that match the current request |
|
183 | + * |
|
184 | + * @param BlockInterface $block |
|
185 | + * @throws InvalidClassException |
|
186 | + */ |
|
187 | + private function trackAssetManagersForBlocks(BlockInterface $block) |
|
188 | + { |
|
189 | + $supported_routes = $block->supportedRoutes(); |
|
190 | + foreach ($supported_routes as $supported_route) { |
|
191 | + if ($this->route_manager->routeMatchesCurrentRequest($supported_route)) { |
|
192 | + $this->block_asset_managers[ $block->blockType() ] = $block->assetManager()->assetNamespace(); |
|
193 | + } |
|
194 | + } |
|
195 | + } |
|
196 | 196 | |
197 | 197 | |
198 | - /** |
|
199 | - * returns true if the block should be registered for the current request |
|
200 | - * else removes block from block_routes array and returns false |
|
201 | - * |
|
202 | - * @param BlockInterface $block |
|
203 | - * @return boolean |
|
204 | - * @throws InvalidClassException |
|
205 | - */ |
|
206 | - public function matchesRoute(BlockInterface $block) |
|
207 | - { |
|
208 | - if (isset($this->block_asset_managers[ $block->blockType() ])) { |
|
209 | - return true; |
|
210 | - } |
|
211 | - unset($this->block_asset_managers[ $block->blockType() ]); |
|
212 | - return false; |
|
213 | - } |
|
198 | + /** |
|
199 | + * returns true if the block should be registered for the current request |
|
200 | + * else removes block from block_routes array and returns false |
|
201 | + * |
|
202 | + * @param BlockInterface $block |
|
203 | + * @return boolean |
|
204 | + * @throws InvalidClassException |
|
205 | + */ |
|
206 | + public function matchesRoute(BlockInterface $block) |
|
207 | + { |
|
208 | + if (isset($this->block_asset_managers[ $block->blockType() ])) { |
|
209 | + return true; |
|
210 | + } |
|
211 | + unset($this->block_asset_managers[ $block->blockType() ]); |
|
212 | + return false; |
|
213 | + } |
|
214 | 214 | |
215 | 215 | |
216 | - /** |
|
217 | - * calls registerBlock() and load assets for all installed blocks |
|
218 | - * |
|
219 | - * @return void |
|
220 | - * @throws Exception |
|
221 | - */ |
|
222 | - public function registerBlocks() |
|
223 | - { |
|
224 | - try { |
|
225 | - // cycle thru block loader folders |
|
226 | - foreach ($this->blocks as $block) { |
|
227 | - if (! $this->matchesRoute($block)) { |
|
228 | - continue; |
|
229 | - } |
|
230 | - // perform any setup required for the block |
|
231 | - $block_type = $block->registerBlock(); |
|
232 | - if (! $block_type instanceof WP_Block_Type) { |
|
233 | - throw new InvalidEntityException($block_type, 'WP_Block_Type'); |
|
234 | - } |
|
235 | - do_action( |
|
236 | - 'FHEE__EventEspresso_core_services_editor_BlockManager__registerBlocks__block_type_registered', |
|
237 | - $block, |
|
238 | - $block_type |
|
239 | - ); |
|
240 | - } |
|
241 | - } catch (Exception $exception) { |
|
242 | - new ExceptionStackTraceDisplay($exception); |
|
243 | - } |
|
244 | - } |
|
216 | + /** |
|
217 | + * calls registerBlock() and load assets for all installed blocks |
|
218 | + * |
|
219 | + * @return void |
|
220 | + * @throws Exception |
|
221 | + */ |
|
222 | + public function registerBlocks() |
|
223 | + { |
|
224 | + try { |
|
225 | + // cycle thru block loader folders |
|
226 | + foreach ($this->blocks as $block) { |
|
227 | + if (! $this->matchesRoute($block)) { |
|
228 | + continue; |
|
229 | + } |
|
230 | + // perform any setup required for the block |
|
231 | + $block_type = $block->registerBlock(); |
|
232 | + if (! $block_type instanceof WP_Block_Type) { |
|
233 | + throw new InvalidEntityException($block_type, 'WP_Block_Type'); |
|
234 | + } |
|
235 | + do_action( |
|
236 | + 'FHEE__EventEspresso_core_services_editor_BlockManager__registerBlocks__block_type_registered', |
|
237 | + $block, |
|
238 | + $block_type |
|
239 | + ); |
|
240 | + } |
|
241 | + } catch (Exception $exception) { |
|
242 | + new ExceptionStackTraceDisplay($exception); |
|
243 | + } |
|
244 | + } |
|
245 | 245 | |
246 | - public function unloadAssets() |
|
247 | - { |
|
248 | - $assets = array_flip($this->block_asset_managers); |
|
249 | - foreach ($this->block_asset_manager_collection as $asset_manager) { |
|
250 | - // if there are no longer any blocks that require these assets, |
|
251 | - if (! isset($assets[ $asset_manager->assetNamespace() ])) { |
|
252 | - // then unset asset enqueueing and bail |
|
253 | - remove_action('wp_enqueue_scripts', array($asset_manager, 'addManifestFile'), 0); |
|
254 | - remove_action('admin_enqueue_scripts', array($asset_manager, 'addManifestFile'), 0); |
|
255 | - remove_action('wp_enqueue_scripts', array($asset_manager, 'addAssets'), 2); |
|
256 | - remove_action('admin_enqueue_scripts', array($asset_manager, 'addAssets'), 2); |
|
257 | - } |
|
258 | - } |
|
259 | - } |
|
246 | + public function unloadAssets() |
|
247 | + { |
|
248 | + $assets = array_flip($this->block_asset_managers); |
|
249 | + foreach ($this->block_asset_manager_collection as $asset_manager) { |
|
250 | + // if there are no longer any blocks that require these assets, |
|
251 | + if (! isset($assets[ $asset_manager->assetNamespace() ])) { |
|
252 | + // then unset asset enqueueing and bail |
|
253 | + remove_action('wp_enqueue_scripts', array($asset_manager, 'addManifestFile'), 0); |
|
254 | + remove_action('admin_enqueue_scripts', array($asset_manager, 'addManifestFile'), 0); |
|
255 | + remove_action('wp_enqueue_scripts', array($asset_manager, 'addAssets'), 2); |
|
256 | + remove_action('admin_enqueue_scripts', array($asset_manager, 'addAssets'), 2); |
|
257 | + } |
|
258 | + } |
|
259 | + } |
|
260 | 260 | } |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | { |
107 | 107 | // $block_editor_context can be either an object or a string |
108 | 108 | // so checking it here, thus no type hinting in function params |
109 | - if (! $block_editor_context instanceof WP_Block_Editor_Context) { |
|
109 | + if ( ! $block_editor_context instanceof WP_Block_Editor_Context) { |
|
110 | 110 | return $categories; |
111 | 111 | } |
112 | 112 | return array_merge( |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | foreach ($this->blocks as $block) { |
168 | 168 | $block->initialize(); |
169 | 169 | $this->trackAssetManagersForBlocks($block); |
170 | - if (! $this->block_asset_manager_collection->has($block->assetManager())) { |
|
170 | + if ( ! $this->block_asset_manager_collection->has($block->assetManager())) { |
|
171 | 171 | $this->block_asset_manager_collection->add($block->assetManager()); |
172 | 172 | $block->assetManager()->setAssetHandles(); |
173 | 173 | } |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | $supported_routes = $block->supportedRoutes(); |
190 | 190 | foreach ($supported_routes as $supported_route) { |
191 | 191 | if ($this->route_manager->routeMatchesCurrentRequest($supported_route)) { |
192 | - $this->block_asset_managers[ $block->blockType() ] = $block->assetManager()->assetNamespace(); |
|
192 | + $this->block_asset_managers[$block->blockType()] = $block->assetManager()->assetNamespace(); |
|
193 | 193 | } |
194 | 194 | } |
195 | 195 | } |
@@ -205,10 +205,10 @@ discard block |
||
205 | 205 | */ |
206 | 206 | public function matchesRoute(BlockInterface $block) |
207 | 207 | { |
208 | - if (isset($this->block_asset_managers[ $block->blockType() ])) { |
|
208 | + if (isset($this->block_asset_managers[$block->blockType()])) { |
|
209 | 209 | return true; |
210 | 210 | } |
211 | - unset($this->block_asset_managers[ $block->blockType() ]); |
|
211 | + unset($this->block_asset_managers[$block->blockType()]); |
|
212 | 212 | return false; |
213 | 213 | } |
214 | 214 | |
@@ -224,12 +224,12 @@ discard block |
||
224 | 224 | try { |
225 | 225 | // cycle thru block loader folders |
226 | 226 | foreach ($this->blocks as $block) { |
227 | - if (! $this->matchesRoute($block)) { |
|
227 | + if ( ! $this->matchesRoute($block)) { |
|
228 | 228 | continue; |
229 | 229 | } |
230 | 230 | // perform any setup required for the block |
231 | 231 | $block_type = $block->registerBlock(); |
232 | - if (! $block_type instanceof WP_Block_Type) { |
|
232 | + if ( ! $block_type instanceof WP_Block_Type) { |
|
233 | 233 | throw new InvalidEntityException($block_type, 'WP_Block_Type'); |
234 | 234 | } |
235 | 235 | do_action( |
@@ -248,7 +248,7 @@ discard block |
||
248 | 248 | $assets = array_flip($this->block_asset_managers); |
249 | 249 | foreach ($this->block_asset_manager_collection as $asset_manager) { |
250 | 250 | // if there are no longer any blocks that require these assets, |
251 | - if (! isset($assets[ $asset_manager->assetNamespace() ])) { |
|
251 | + if ( ! isset($assets[$asset_manager->assetNamespace()])) { |
|
252 | 252 | // then unset asset enqueueing and bail |
253 | 253 | remove_action('wp_enqueue_scripts', array($asset_manager, 'addManifestFile'), 0); |
254 | 254 | remove_action('admin_enqueue_scripts', array($asset_manager, 'addManifestFile'), 0); |