Completed
Branch FET/Gutenberg/11400/block-mana... (e5b352)
by
unknown
56:04 queued 43:23
created

EditorBlock   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 235
rs 10
c 0
b 0
f 0
wmc 17
lcom 2
cbo 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A editorBlockType() 0 4 1
A namespacedEditorBlockType() 0 4 1
A setEditorBlockType() 0 4 1
A setWpBlockType() 0 4 1
A setSupportedPostTypes() 0 4 1
A attributes() 0 4 1
A setAttributes() 0 4 1
A isDynamic() 0 4 1
A setDynamic() 0 4 1
A registerBlock() 0 23 2
A unRegisterBlock() 0 4 1
A appliesToPostType() 0 4 1
A getEditorContainer() 0 7 1
A registerScripts() 0 14 1
A registerStyles() 0 9 1
1
<?php
2
3
namespace EventEspresso\core\domain\entities\editor;
4
5
use EventEspresso\core\domain\DomainInterface;
6
use EventEspresso\core\services\loaders\LoaderInterface;
7
use WP_Block_Type;
8
9
defined('EVENT_ESPRESSO_VERSION') || exit;
10
11
12
13
/**
14
 * Class EditorBlock
15
 * Registers a Editor block type with WordPress core,
16
 * specifies all assets required for the block,
17
 * and executes all logic as necessary
18
 * ALL blocks should be located in
19
 *  \core\domain\entities\editor\blocks\
20
 * under the appropriate namespace root
21
 *
22
 * @package EventEspresso\core\services\editor
23
 * @author  Brent Christensen
24
 * @since   $VID:$
25
 */
26
abstract class EditorBlock implements EditorBlockInterface
27
{
28
29
    const NS = 'event-espresso/';
30
31
    /**
32
     * @var DomainInterface $domain
33
     */
34
    protected $domain;
35
36
    /**
37
     * @var LoaderInterface $loader
38
     */
39
    protected $loader;
40
41
    /**
42
     * @var string $editor_block_type
43
     */
44
    private $editor_block_type;
45
46
    /**
47
     * @var WP_Block_Type $wp_block_type
48
     */
49
    private $wp_block_type;
50
51
    /**
52
     * @var array $supported_post_types
53
     */
54
    private $supported_post_types;
55
56
    /**
57
     * @var array $attributes
58
     */
59
    private $attributes;
60
61
    /**
62
     * If set to true, then the block will render its content client side
63
     * If false, then the block will render its content server side using the renderBlock() method
64
     *
65
     * @var bool $dynamic
66
     */
67
    private $dynamic = false;
68
69
70
    /**
71
     * EditorBlockLoader constructor.
72
     *
73
     * @param DomainInterface $domain
74
     * @param LoaderInterface $loader
75
     */
76
    public function __construct(DomainInterface $domain, LoaderInterface $loader)
77
    {
78
        $this->domain = $domain;
79
        $this->loader = $loader;
80
    }
81
82
83
    /**
84
     * @return string
85
     */
86
    public function editorBlockType()
87
    {
88
        return $this->editor_block_type;
89
    }
90
91
92
    /**
93
     * @return string
94
     */
95
    public function namespacedEditorBlockType()
96
    {
97
        return EditorBlock::NS . $this->editor_block_type;
98
    }
99
100
101
    /**
102
     * @param string $editor_block_type
103
     */
104
    protected function setEditorBlockType($editor_block_type)
105
    {
106
        $this->editor_block_type = $editor_block_type;
107
    }
108
109
110
    /**
111
     * @param WP_Block_Type $wp_block_type
112
     */
113
    protected function setWpBlockType($wp_block_type)
114
    {
115
        $this->wp_block_type = $wp_block_type;
116
    }
117
118
119
    /**
120
     * @param array $supported_post_types
121
     */
122
    protected function setSupportedPostTypes(array $supported_post_types)
123
    {
124
        $this->supported_post_types = $supported_post_types;
125
    }
126
127
128
    /**
129
     * @return array
130
     */
131
    public function attributes()
132
    {
133
        return $this->attributes;
134
    }
135
136
137
    /**
138
     * @param array $attributes
139
     */
140
    public function setAttributes(array $attributes)
141
    {
142
        $this->attributes = $attributes;
143
    }
144
145
146
    /**
147
     * @return bool
148
     */
149
    public function isDynamic()
150
    {
151
        return $this->dynamic;
152
    }
153
154
155
    /**
156
     * @param bool $dynamic
157
     */
158
    public function setDynamic($dynamic = true)
159
    {
160
        $this->dynamic = filter_var($dynamic, FILTER_VALIDATE_BOOLEAN);
161
    }
162
163
164
    /**
165
     * Registers the Editor Block with WP core;
166
     * Returns the registered block type on success, or false on failure.
167
     *
168
     * @return WP_Block_Type|false
169
     */
170
    public function registerBlock()
171
    {
172
        $context ='core';
173
        // todo add route detection (ie inject route) and change context based on route
174
        $args          = array(
175
            'attributes'      => $this->attributes(),
176
            'editor_script'   => "ee-{$context}-blocks",
177
            'editor_style'    => "ee-{$context}-blocks",
178
            'script'          => "ee-{$context}-blocks",
179
            'style'           => "ee-{$context}-blocks",
180
        );
181
        if(! $this->isDynamic()) {
182
            $args['render_callback'] = $this->renderBlock();
183
        }
184
        $wp_block_type = register_block_type(
185
            new WP_Block_Type(
186
                $this->namespacedEditorBlockType(),
187
                $args
188
            )
189
        );
190
        $this->setWpBlockType($wp_block_type);
191
        return $wp_block_type;
192
    }
193
194
195
    /**
196
     * @return WP_Block_Type|false The registered block type on success, or false on failure.
197
     */
198
    public function unRegisterBlock()
199
    {
200
        return unregister_block_type($this->namespacedEditorBlockType());
201
    }
202
203
204
    /**
205
     * returns true if the block type applies for the supplied post type
206
     * and should be added to that post type's editor
207
     *
208
     * @param string $post_type
209
     * @return boolean
210
     */
211
    public function appliesToPostType($post_type)
212
    {
213
        return in_array($post_type, $this->supported_post_types, true);
214
    }
215
216
217
    /**
218
     * @return array
219
     */
220
    public function getEditorContainer()
221
    {
222
        return array(
223
            $this->namespacedEditorBlockType(),
224
            array()
225
        );
226
    }
227
228
229
    /**
230
     * @return  void
231
     */
232
    public function registerScripts()
233
    {
234
        // wp_register_script(
235
        //     'core-blocks',
236
        //     $this->domain->distributionAssetsUrl() . 'ee-core-blocks.dist.js',
237
        //     array(
238
        //         'wp-blocks',    // Provides useful functions and components for extending the editor
239
        //         'wp-i18n',      // Provides localization functions
240
        //         'wp-element',   // Provides React.Component
241
        //         'wp-components' // Provides many prebuilt components and controls
242
        //     ),
243
        //     filemtime($this->domain->distributionAssetsPath() . 'ee-core-blocks.dist.js')
244
        // );
245
    }
246
247
248
    /**
249
     * @return void
250
     */
251
    public function registerStyles()
252
    {
253
        // wp_register_style(
254
        //     'ee-block-styles',
255
        //     $this->domain->distributionAssetsUrl() . 'style.css',
256
        //     array(),
257
        //     filemtime($this->domain->distributionAssetsPath() . 'style.css')
258
        // );
259
    }
260
}
261