Last active
May 29, 2020 12:51
-
-
Save ville6000/5be2188d62f9d1412f0d509c24372594 to your computer and use it in GitHub Desktop.
blocks.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function allowed_block_types( $allowed_blocks, $post ) { | |
$blocks = [ | |
'core/block' => [], | |
'core/template' => [], | |
'core/heading' => [], | |
'core/paragraph' => [], | |
'core/image' => [ | |
'post_types' => [ | |
PostType\Post::SLUG, | |
], | |
], | |
'core/table' => [ | |
'post_types' => [ | |
PostType\Page::SLUG, | |
], | |
], | |
'core/quote' => [ | |
'post_types' => [ | |
PostType\Page::SLUG, | |
], | |
], | |
'core/list' => [ | |
'templates' => [ | |
'models/page-ui-demo.php', | |
], | |
], | |
'core-embed/youtube' => [], | |
'core-embed/instagram' => [], | |
]; | |
$post_type = \get_post_type( $post ); | |
$page_template = \get_page_template_slug( $post->ID ); | |
$allowed_blocks = []; | |
foreach ( $blocks as $block => $rules ) { | |
if ( empty( $rules['post_types'] ) && empty( $rules['templates'] ) ) { | |
$allowed_blocks[] = $block; | |
} | |
$allowed_post_type = false; | |
if ( isset( $rules['post_types'] ) ) { | |
$allowed_post_type = in_array( $post_type, $rules['post_types'], true ); | |
} | |
$allowed_template = false; | |
if ( isset( $rules['templates'] ) ) { | |
$allowed_template = in_array( $page_template, $rules['templates'], true ); | |
} | |
if ( $allowed_post_type || $allowed_template ) { | |
$allowed_blocks[] = $block; | |
} | |
} | |
return $allowed_blocks; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment