| 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 |
'ai-text', |
| 121 |
'anchor', |
| 122 |
'accordion', |
| 123 |
'advanced-heading', |
| 124 |
'advanced-spacer', |
| 125 |
'banner', |
| 126 |
'button-group', |
| 127 |
'circle-progress-bar', |
| 128 |
'counter', |
| 129 |
'custom-post-type', |
| 130 |
'icon-box', |
| 131 |
'icon', |
| 132 |
'image-box', |
| 133 |
'images-slider', |
| 134 |
'images-stack', |
| 135 |
'instagram', |
| 136 |
'google-map', |
| 137 |
'media-text-slider', |
| 138 |
'person', |
| 139 |
'post-carousel', |
| 140 |
'post-slider', |
| 141 |
'price-box', |
| 142 |
'price-list', |
| 143 |
'progress-bar', |
| 144 |
'recent-posts', |
| 145 |
'section', |
| 146 |
'social-links', |
| 147 |
'tabs', |
| 148 |
'testimonial', |
| 149 |
'toggle', |
| 150 |
'table-of-contents', |
| 151 |
'video-popup', |
| 152 |
'image-hotspot', |
| 153 |
'countdown', |
| 154 |
'template-library', |
| 155 |
'contact-form', |
| 156 |
'mailchimp', |
| 157 |
'content-timeline', |
| 158 |
'table', |
| 159 |
'content-slider' |
| 160 |
); |
| 161 |
|
| 162 |
// load and register main blocks |
| 163 |
foreach ( $block_files as $block_file_name ) { |
| 164 |
$this->require_block($block_file_name); |
| 165 |
} |
| 166 |
|
| 167 |
// fill array of active blocks |
| 168 |
foreach ( $this->blocks as $block ) { |
| 169 |
|
| 170 |
if ( $block->isEnabled() ) { |
| 171 |
$this->enabledBlocks[] = $block; |
| 172 |
} else { |
| 173 |
$this->disabledBlocks[] = $block; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
$template_parts = array( |
| 178 |
'template-parts/post-title', |
| 179 |
'template-parts/post-featured-image', |
| 180 |
'template-parts/post-content', |
| 181 |
'template-parts/post-link', |
| 182 |
'template-parts/post-button', |
| 183 |
'template-parts/post-author', |
| 184 |
'template-parts/post-categories', |
| 185 |
'template-parts/post-comments', |
| 186 |
'template-parts/post-tags', |
| 187 |
'template-parts/post-date', |
| 188 |
'template-parts/post-featured-background-image', |
| 189 |
'template-parts/post-meta', |
| 190 |
'template-parts/post-custom-field', |
| 191 |
'template-parts/post-layout-helper', |
| 192 |
); |
| 193 |
|
| 194 |
// load template-parts blocks |
| 195 |
foreach ( $template_parts as $block_file_name ) { |
| 196 |
$this->require_block($block_file_name); |
| 197 |
} |
| 198 |
|
| 199 |
$template_parts_acf = array( |
| 200 |
'template-parts/acf/background-image', |
| 201 |
'template-parts/acf/image', |
| 202 |
'template-parts/acf/select', |
| 203 |
'template-parts/acf/wysiwyg', |
| 204 |
); |
| 205 |
|
| 206 |
// load template-acf blocks |
| 207 |
foreach ( $template_parts_acf as $block_file_name ) { |
| 208 |
$this->require_block($block_file_name); |
| 209 |
} |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
private function require_block( $block_file_name ) { |
| 214 |
|
| 215 |
$path = getwid_get_plugin_path( '/includes/blocks/' . $block_file_name . '.php' ); |
| 216 |
|
| 217 |
if ( file_exists( $path ) ) { |
| 218 |
require_once( $path ); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
public function addBlock( $block ) { |
| 223 |
|
| 224 |
if ( $block instanceof \Getwid\Blocks\AbstractBlock ) { |
| 225 |
$this->blocks[ $block::getBlockName() ] = $block; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
public function getBlocks() { |
| 230 |
return $this->blocks; |
| 231 |
} |
| 232 |
|
| 233 |
public function getEnabledBlocks() { |
| 234 |
return $this->enabledBlocks; |
| 235 |
} |
| 236 |
|
| 237 |
public function getDisabledBlocks() { |
| 238 |
return $this->disabledBlocks; |
| 239 |
} |
| 240 |
|
| 241 |
public function hasEnabledBlocks() { |
| 242 |
return ( sizeof ($this->enabledBlocks ) > 0 ); |
| 243 |
} |
| 244 |
|
| 245 |
public function hasDisabledBlocks() { |
| 246 |
return ( sizeof ($this->disabledBlocks ) > 0 ); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Determine whether a post or content string has Getwid blocks. |
| 251 |
*/ |
| 252 |
public function hasGetwidBlocks() { |
| 253 |
|
| 254 |
$has_getwid_blocks = false; |
| 255 |
|
| 256 |
if ( has_blocks() ) { |
| 257 |
|
| 258 |
$blocks = $this->getBlocks(); |
| 259 |
foreach ( $blocks as $block ) { |
| 260 |
|
| 261 |
if ( has_block( $block->getBlockName() ) ) { |
| 262 |
$has_getwid_blocks = true; |
| 263 |
break; |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
return apply_filters( 'getwid/blocks/has_getwid_blocks', $has_getwid_blocks); |
| 269 |
} |
| 270 |
|
| 271 |
public function hasGetwidNestedBlocks() { |
| 272 |
|
| 273 |
$has_getwid_nested_blocks = false; |
| 274 |
|
| 275 |
$nestedBlocks = [ |
| 276 |
\Getwid\Blocks\PostCarousel::getBlockName(), |
| 277 |
\Getwid\Blocks\PostSlider::getBlockName(), |
| 278 |
\Getwid\Blocks\CustomPostType::getBlockName(), |
| 279 |
]; |
| 280 |
|
| 281 |
foreach( $nestedBlocks as $block ) { |
| 282 |
if ( has_block($block) ) { |
| 283 |
$has_getwid_nested_blocks = true; |
| 284 |
break; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
return apply_filters( 'getwid/blocks/has_getwid_nested_blocks', $has_getwid_nested_blocks); |
| 289 |
} |
| 290 |
|
| 291 |
} |
| 292 |
|