Completed
Branch FET/11400/gutenberg-block-mana... (713a0a)
by
unknown
45:29 queued 32:15
created

EditorBlockManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 120
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B populateEditorBlockCollection() 0 25 1
A loadEditorBlocks() 0 12 3
B registerEditorBlocks() 0 27 5
1
<?php
2
3
namespace EventEspresso\core\services\editor;
4
5
use EE_Error;
6
use EventEspresso\core\domain\EnqueueAssetsInterface;
7
use EventEspresso\core\domain\entities\editor\EditorBlockCollection;
8
use EventEspresso\core\domain\entities\editor\EditorBlockInterface;
9
use EventEspresso\core\exceptions\ExceptionStackTraceDisplay;
10
use EventEspresso\core\exceptions\InvalidClassException;
11
use EventEspresso\core\exceptions\InvalidDataTypeException;
12
use EventEspresso\core\exceptions\InvalidEntityException;
13
use EventEspresso\core\exceptions\InvalidFilePathException;
14
use EventEspresso\core\exceptions\InvalidIdentifierException;
15
use EventEspresso\core\exceptions\InvalidInterfaceException;
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 EditorBlockManager
31
 * Loads EditorBlockInterface classes into the injected EditorBlockCollection,
32
 * which can be used in other classes by retrieving it from EE's Loader.
33
 * After loading, the EditorBlockManager gets each EditorBlock 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 EditorBlockManager
41
{
42
43
    /**
44
     * @var CollectionInterface|EditorBlockInterface[] $blocks
45
     */
46
    private $blocks;
47
48
    /**
49
     * @var RequestInterface $request
50
     */
51
    protected $request;
52
53
54
    /**
55
     * EditorBlockManager constructor.
56
     *
57
     * @param EditorBlockCollection $blocks
58
     * @param RequestInterface         $request
59
     */
60
    public function __construct(EditorBlockCollection $blocks, RequestInterface $request)
61
    {
62
        $this->blocks  = $blocks;
63
        $this->request = $request;
64
        //  populates the EditorBlockCollection and calls initialize() on all installed blocks
65
        add_action('AHEE__EE_System__core_loaded_and_ready', array($this, 'loadEditorBlocks'));
66
        //  call initialize() and load assets for all installed blocks
67
        add_action('AHEE__EE_System__initialize', array($this, 'registerEditorBlocks'));
68
    }
69
70
71
    /**
72
     * @return CollectionInterface|EditorBlockInterface[]
73
     * @throws ReflectionException
74
     * @throws InvalidArgumentException
75
     * @throws EE_Error
76
     * @throws InvalidClassException
77
     * @throws InvalidDataTypeException
78
     * @throws InvalidEntityException
79
     * @throws InvalidFilePathException
80
     * @throws InvalidIdentifierException
81
     * @throws InvalidInterfaceException
82
     */
83
    protected function populateEditorBlockCollection()
84
    {
85
        $loader = new CollectionLoader(
86
            new CollectionDetails(
87
            // collection name
88
                'shortcodes',
89
                // collection interface
90
                'EventEspresso\core\domain\entities\editor\EditorBlockInterface',
91
                // FQCNs for classes to add (all classes within each namespace will be loaded)
92
                apply_filters(
93
                    'FHEE__EventEspresso_core_services_editor_EditorBlockManager__populateEditorBlockCollection__collection_FQCNs',
94
                    array('EventEspresso\core\domain\entities\editor\blocks')
95
                ),
96
                // filepaths to classes to add
97
                array(),
98
                // file mask to use if parsing folder for files to add
99
                '',
100
                // what to use as identifier for collection entities
101
                // using CLASS NAME prevents duplicates (works like a singleton)
102
                CollectionDetails::ID_CLASS_NAME
103
            ),
104
            $this->blocks
105
        );
106
        return $loader->getCollection();
107
    }
108
109
110
    /**
111
     * @return void
112
     * @throws Exception
113
     */
114
    public function loadEditorBlocks()
115
    {
116
        try {
117
            $this->populateEditorBlockCollection();
118
            // cycle thru block loaders and initialize each loader
119
            foreach ($this->blocks as $block) {
120
                $block->initialize();
121
            }
122
        } catch (Exception $exception) {
123
            new ExceptionStackTraceDisplay($exception);
124
        }
125
    }
126
127
128
    /**
129
     * @return void
130
     * @throws Exception
131
     */
132
    public function registerEditorBlocks()
133
    {
134
        try {
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) {
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...
140
                    throw new InvalidEntityException($block_type, 'WP_Block_Type');
141
                }
142
                do_action(
143
                    'FHEE__EventEspresso_core_services_editor_EditorBlockManager__registerEditorBlocks__block_type_registered',
144
                    $block,
145
                    $block_type
146
                );
147
                if ($block instanceof EnqueueAssetsInterface) {
148
                    // the following can be refactored in whatever way works best,
149
                    // but it should be self evident that controlling asset loading from here
150
                    // avoids having to add the following lines of code in every EditorBlock class
151
                    add_action('enqueue_block_assets', array($block, 'registerScripts'));
152
                    add_action('enqueue_block_assets', array($block, 'registerStyles'));
153
                }
154
            }
155
        } catch (Exception $exception) {
156
            new ExceptionStackTraceDisplay($exception);
157
        }
158
    }
159
}
160