Completed
Branch EDTR/master (7a3fe8)
by
unknown
10:11 queued 01:18
created

Block::loadGraphQLRelayAutoloader()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\entities\editor;
4
5
use EventEspresso\core\services\assets\BlockAssetManagerInterface;
6
use EventEspresso\core\services\request\RequestInterface;
7
use WP_Block_Type;
8
9
/**
10
 * Class Block
11
 * Registers a Block type with WordPress core
12
 * and executes all logic as necessary
13
 * ALL blocks should be located in
14
 *  \core\domain\entities\editor\blocks\
15
 * under the appropriate namespace root
16
 *
17
 * @package EventEspresso\core\services\editor
18
 * @author  Brent Christensen
19
 * @since   4.9.71.p
20
 */
21
abstract class Block implements BlockInterface
22
{
23
24
    /**
25
     * BlockAssetManager that this editor block uses for asset registration
26
     *
27
     * @var BlockAssetManagerInterface $block_asset_manager
28
     */
29
    protected $block_asset_manager;
30
31
    /**
32
     * @var RequestInterface $request
33
     */
34
    protected $request;
35
36
    /**
37
     * @var array $attributes
38
     */
39
    private $attributes;
40
41
    /**
42
     * If set to true, then the block will render its content client side
43
     * If false, then the block will render its content server side using the renderBlock() method
44
     *
45
     * @var bool $dynamic
46
     */
47
    private $dynamic = false;
48
49
    /**
50
     * @var string $block_type
51
     */
52
    private $block_type;
53
54
    /**
55
     * @var array $supported_routes
56
     */
57
    private $supported_routes;
58
59
60
    /**
61
     * Block constructor.
62
     *
63
     * @param BlockAssetManagerInterface $block_asset_manager
64
     * @param RequestInterface           $request
65
     */
66
    public function __construct(BlockAssetManagerInterface $block_asset_manager, RequestInterface $request)
67
    {
68
        $this->block_asset_manager = $block_asset_manager;
69
        $this->request = $request;
70
    }
71
72
73
    /**
74
     * @return string
75
     */
76
    public function blockType()
77
    {
78
        return $this->block_type;
79
    }
80
81
82
    /**
83
     * @return string
84
     */
85
    public function namespacedBlockType()
86
    {
87
        return Block::NAME_SPACE . '/' . $this->block_type;
88
    }
89
90
91
    /**
92
     * @param string $block_type
93
     */
94
    protected function setBlockType($block_type)
95
    {
96
        $this->block_type = $block_type;
97
    }
98
99
100
    /**
101
     * BlockAssetManager that this editor block uses for asset registration
102
     *
103
     * @return BlockAssetManagerInterface
104
     */
105
    public function assetManager()
106
    {
107
        return $this->block_asset_manager;
108
    }
109
110
111
    /**
112
     * returns an array of fully qualified class names
113
     * for RouteMatchSpecificationInterface objects
114
     * that specify routes that the block should be loaded for.
115
     *
116
     * @return array
117
     */
118
    public function supportedRoutes()
119
    {
120
        return $this->supported_routes;
121
    }
122
123
124
    /**
125
     * @param array $supported_routes
126
     */
127
    protected function setSupportedRoutes(array $supported_routes)
128
    {
129
        $this->supported_routes = $supported_routes;
130
    }
131
132
133
    /**
134
     * @return array
135
     */
136
    public function attributes()
137
    {
138
        return $this->attributes;
139
    }
140
141
142
    /**
143
     * @param array $attributes
144
     */
145
    public function setAttributes(array $attributes)
146
    {
147
        $this->attributes = $attributes;
148
    }
149
150
151
    /**
152
     * @return bool
153
     */
154
    public function isDynamic()
155
    {
156
        return $this->dynamic;
157
    }
158
159
160
    /**
161
     * @param bool $dynamic
162
     */
163
    public function setDynamic($dynamic = true)
164
    {
165
        $this->dynamic = filter_var($dynamic, FILTER_VALIDATE_BOOLEAN);
166
    }
167
168
169
    /**
170
     * Registers the Editor Block with WP core;
171
     * Returns the registered block type on success, or false on failure.
172
     *
173
     * @return WP_Block_Type|false
174
     */
175
    public function registerBlock()
176
    {
177
        $args = array(
178
            'attributes'    => $this->attributes(),
179
            'editor_script' => $this->block_asset_manager->getEditorScriptHandle(),
180
            'script'        => $this->block_asset_manager->getScriptHandle(),
181
        );
182
        if ($this->isDynamic()) {
183
            $args['render_callback'] = array($this, 'renderBlock');
184
        }
185
        return register_block_type(
186
            new WP_Block_Type(
187
                $this->namespacedBlockType(),
188
                $args
189
            )
190
        );
191
    }
192
193
194
    /**
195
     * @return WP_Block_Type|false The registered block type on success, or false on failure.
196
     */
197
    public function unRegisterBlock()
198
    {
199
        return unregister_block_type($this->namespacedBlockType());
200
    }
201
202
203
204
    /**
205
     * @return array
206
     */
207
    public function getEditorContainer()
208
    {
209
        return array(
210
            $this->namespacedBlockType(),
211
            array(),
212
        );
213
    }
214
215
216
    /**
217
     * @since $VID:$
218
     */
219
    protected function loadGraphQLRelayAutoloader()
220
    {
221
        if (! class_exists('GraphQLRelay')) {
222
            $autoloader = EE_THIRD_PARTY . 'wp-graphql/vendor/autoload.php';
223
            if (! file_exists($autoloader)) {
224
                return;
225
            }
226
            require_once $autoloader;
227
        }
228
    }
229
}
230