Completed
Branch Gutenberg/event-attendees-bloc... (76b12a)
by
unknown
78:26 queued 65:41
created

BlockRegistrationManager::initializeBlocks()   A

Complexity

Conditions 4
Paths 10

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 2
b 0
f 0
nc 10
nop 0
dl 0
loc 16
rs 9.2
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\BlockAssetManagerCollection;
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
/**
26
 * Class BlockRegistrationManager
27
 * Loads BlockInterface classes into the injected BlockCollection,
28
 * which can be used in other classes by retrieving it from EE's Loader.
29
 * After loading, the BlockManager gets each Block to register
30
 * its block type and ensures assets are enqueued at the appropriate time
31
 *
32
 * @package EventEspresso\core\domain\services\editor
33
 * @author  Brent Christensen
34
 * @since   $VID:$
35
 */
36
class BlockRegistrationManager extends BlockManager
37
{
38
39
    /**
40
     * @var BlockAssetManagerCollection $block_asset_manager_collection
41
     */
42
    protected $block_asset_manager_collection;
43
44
45
    /**
46
     * BlockRegistrationManager constructor.
47
     *
48
     * @param BlockAssetManagerCollection $block_asset_manager_collection
49
     * @param BlockCollection             $blocks
50
     * @param RequestInterface            $request
51
     */
52
    public function __construct(
53
        BlockAssetManagerCollection $block_asset_manager_collection,
54
        BlockCollection $blocks,
55
        RequestInterface $request
56
    ) {
57
        $this->block_asset_manager_collection = $block_asset_manager_collection;
58
        parent::__construct($blocks, $request);
59
    }
60
61
62
    /**
63
     *  Returns the name of a hookpoint to be used to call initialize()
64
     *
65
     * @return string
66
     */
67
    public function initHook()
68
    {
69
        return 'AHEE__EE_System__core_loaded_and_ready';
70
    }
71
72
73
    /**
74
     * Perform any early setup required for block editors to functions
75
     *
76
     * @return void
77
     * @throws Exception
78
     */
79
    public function initialize()
80
    {
81
        $this->initializeBlocks();
82
        add_action('AHEE__EE_System__initialize', array($this, 'registerBlocks'));
83
    }
84
85
86
    /**
87
     * @return CollectionInterface|BlockInterface[]
88
     * @throws ReflectionException
89
     * @throws InvalidArgumentException
90
     * @throws EE_Error
91
     * @throws InvalidClassException
92
     * @throws InvalidDataTypeException
93
     * @throws InvalidEntityException
94
     * @throws InvalidFilePathException
95
     * @throws InvalidIdentifierException
96
     * @throws InvalidInterfaceException
97
     */
98
    protected function populateBlockCollection()
99
    {
100
        $loader = new CollectionLoader(
101
            new CollectionDetails(
102
                // collection name
103
                'shortcodes',
104
                // collection interface
105
                'EventEspresso\core\domain\entities\editor\BlockInterface',
106
                // FQCNs for classes to add (all classes within each namespace will be loaded)
107
                apply_filters(
108
                    'FHEE__EventEspresso_core_services_editor_BlockManager__populateBlockCollection__collection_FQCNs',
109
                    array(
110
                        // 'EventEspresso\core\domain\entities\editor\blocks\common',
111
                        // 'EventEspresso\core\domain\entities\editor\blocks\editor',
112
                        'EventEspresso\core\domain\entities\editor\blocks\widgets',
113
                    )
114
                ),
115
                // filepaths to classes to add
116
                array(),
117
                // file mask to use if parsing folder for files to add
118
                '',
119
                // what to use as identifier for collection entities
120
                // using CLASS NAME prevents duplicates (works like a singleton)
121
                CollectionDetails::ID_CLASS_NAME
122
            ),
123
            $this->blocks
124
        );
125
        return $loader->getCollection();
126
    }
127
128
129
    /**
130
     * populates the BlockCollection and calls initialize() on all installed blocks
131
     *
132
     * @return void
133
     * @throws Exception
134
     */
135
    public function initializeBlocks()
136
    {
137
        try {
138
            $this->populateBlockCollection();
139
            // cycle thru block loaders and initialize each loader
140
            foreach ($this->blocks as $block) {
141
                $block->initialize();
142
                if (! $this->block_asset_manager_collection->has($block->assetManager())) {
143
                    $this->block_asset_manager_collection->add($block->assetManager());
144
                    $block->assetManager()->setAssetHandles();
145
                }
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) {
0 ignored issues
show
Bug introduced by
The class WP_Block_Type does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
167
                    throw new InvalidEntityException($block_type, 'WP_Block_Type');
168
                }
169
                do_action(
170
                    'FHEE__EventEspresso_core_services_editor_BlockManager__registerBlocks__block_type_registered',
171
                    $block,
172
                    $block_type
173
                );
174
            }
175
        } catch (Exception $exception) {
176
            new ExceptionStackTraceDisplay($exception);
177
        }
178
    }
179
}
180