PluginProbe
Getwid – Gutenberg Blocks / 3.0.2
Getwid – Gutenberg Blocks v3.0.2
3.0.2 3.0.1 3.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 trunk 1.8.7 1.9.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
getwid / includes / blocks-manager.php

blocks-manager.php in Getwid – Gutenberg Blocks 3.0.2, at includes/blocks-manager.php

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