PluginProbe
Getwid – Gutenberg Blocks / 1.8.7
Getwid – Gutenberg Blocks v1.8.7
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 1.8.7, at includes/blocks-manager.php

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