blocks-manager.php
6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
namespace Getwid;
/**
* Class BlocksManager
* @package Getwid
*/
class BlocksManager {
private $blocks = array();
private $enabledBlocks = array();
private $disabledBlocks = array();
/**
* BlockManager constructor.
*/
public function __construct() {
if ( version_compare( get_bloginfo( 'version' ), '5.8', '>=' ) ) {
add_filter( 'block_categories_all', array( $this, 'block_categories_all' ), 10, 2 );
} else {
add_filter( 'block_categories', array( $this, 'block_categories' ), 10, 2 );
}
add_action( 'init', [$this, 'includeBlocks'] );
}
public function block_categories_all( $block_categories, $editor_context ) {
//Add Getwid blocks category
$block_categories = array_merge(
$block_categories,
array(
array(
'slug' => 'getwid-blocks',
'title' => __( 'Getwid Blocks', 'getwid' ),
),
)
);
//Add Getwid post-block category (Only on Templates page)
if ( ! empty( $editor_context->post ) && ( $editor_context->post->post_type == getwid()->postTemplatePart()->postType ) ) {
$block_categories = array_merge(
$block_categories,
array(
array(
'slug' => 'getwid-post-blocks',
'title' => __( 'Getwid Post Blocks', 'getwid' ),
),
)
);
}
if ( getwid_acf_is_active() ) {
if ( ! empty( $editor_context->post ) && ( $editor_context->post->post_type == getwid()->postTemplatePart()->postType ) ) {
$block_categories = array_merge(
$block_categories,
array(
array(
'slug' => 'getwid-acf-blocks',
'title' => __( 'Getwid ACF Blocks', 'getwid' ),
),
)
);
}
}
return $block_categories;
}
public function block_categories( $categories, $post ) {
//Add Getwid blocks category
$categories = array_merge(
$categories,
array(
array(
'slug' => 'getwid-blocks',
'title' => __( 'Getwid Blocks', 'getwid' ),
),
)
);
//Add Getwid post-block category (Only on Templates page)
if ( $post && ( $post->post_type == getwid()->postTemplatePart()->postType ) ) {
$categories = array_merge(
$categories,
array(
array(
'slug' => 'getwid-post-blocks',
'title' => __( 'Getwid Post Blocks', 'getwid' ),
),
)
);
}
if ( getwid_acf_is_active() ) {
if ( $post && ( $post->post_type == getwid()->postTemplatePart()->postType ) ) {
$categories = array_merge(
$categories,
array(
array(
'slug' => 'getwid-acf-blocks',
'title' => __( 'Getwid ACF Blocks', 'getwid' ),
),
)
);
}
}
return $categories;
}
public function includeBlocks() {
$block_files = array(
'abstract-block',
'anchor',
'accordion',
'advanced-heading',
'advanced-spacer',
'banner',
'button-group',
'circle-progress-bar',
'counter',
'custom-post-type',
'icon-box',
'icon',
'image-box',
'images-slider',
'images-stack',
'instagram',
'google-map',
'media-text-slider',
'person',
'post-carousel',
'post-slider',
'price-box',
'price-list',
'progress-bar',
'recent-posts',
'section',
'social-links',
'tabs',
'testimonial',
'toggle',
'table-of-contents',
'video-popup',
'image-hotspot',
'countdown',
'template-library',
'contact-form',
'mailchimp',
'content-timeline',
'table',
'content-slider'
);
// load and register main blocks
foreach ( $block_files as $block_file_name ) {
$this->require_block($block_file_name);
}
// fill array of active blocks
foreach ( $this->blocks as $block ) {
if ( $block->isEnabled() ) {
$this->enabledBlocks[] = $block;
} else {
$this->disabledBlocks[] = $block;
}
}
$template_parts = array(
'template-parts/post-title',
'template-parts/post-featured-image',
'template-parts/post-content',
'template-parts/post-link',
'template-parts/post-button',
'template-parts/post-author',
'template-parts/post-categories',
'template-parts/post-comments',
'template-parts/post-tags',
'template-parts/post-date',
'template-parts/post-featured-background-image',
'template-parts/post-meta',
'template-parts/post-custom-field',
'template-parts/post-layout-helper',
);
// load template-parts blocks
foreach ( $template_parts as $block_file_name ) {
$this->require_block($block_file_name);
}
$template_parts_acf = array(
'template-parts/acf/background-image',
'template-parts/acf/image',
'template-parts/acf/select',
'template-parts/acf/wysiwyg',
);
// load template-acf blocks
foreach ( $template_parts_acf as $block_file_name ) {
$this->require_block($block_file_name);
}
}
private function require_block( $block_file_name ) {
$path = getwid_get_plugin_path( '/includes/blocks/' . $block_file_name . '.php' );
if ( file_exists( $path ) ) {
require_once( $path );
}
}
public function addBlock( $block ) {
if ( $block instanceof \Getwid\Blocks\AbstractBlock ) {
$this->blocks[ $block::getBlockName() ] = $block;
}
}
public function getBlocks() {
return $this->blocks;
}
public function getEnabledBlocks() {
return $this->enabledBlocks;
}
public function getDisabledBlocks() {
return $this->disabledBlocks;
}
public function hasEnabledBlocks() {
return ( sizeof ($this->enabledBlocks ) > 0 );
}
public function hasDisabledBlocks() {
return ( sizeof ($this->disabledBlocks ) > 0 );
}
/**
* Determine whether a post or content string has Getwid blocks.
*/
public function hasGetwidBlocks() {
$has_getwid_blocks = false;
if ( has_blocks() ) {
$blocks = $this->getBlocks();
foreach ( $blocks as $block ) {
if ( has_block( $block->getBlockName() ) ) {
$has_getwid_blocks = true;
break;
}
}
}
return apply_filters( 'getwid/blocks/has_getwid_blocks', $has_getwid_blocks);
}
public function hasGetwidNestedBlocks() {
$has_getwid_nested_blocks = false;
$nestedBlocks = [
\Getwid\Blocks\PostCarousel::getBlockName(),
\Getwid\Blocks\PostSlider::getBlockName(),
\Getwid\Blocks\CustomPostType::getBlockName(),
];
foreach( $nestedBlocks as $block ) {
if ( has_block($block) ) {
$has_getwid_nested_blocks = true;
break;
}
}
return apply_filters( 'getwid/blocks/has_getwid_nested_blocks', $has_getwid_nested_blocks);
}
}