Completed
Branch Gutenberg/master (a5e5d1)
by
unknown
147:31 queued 134:10
created

Block::setAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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