-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathblock-editor-settings.php
More file actions
89 lines (74 loc) · 2.21 KB
/
block-editor-settings.php
File metadata and controls
89 lines (74 loc) · 2.21 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
<?php
/**
* Adds settings to the block editor.
*
* @package gutenberg
*/
/**
* Finds the first occurrence of a specific block in an array of blocks.
*
* @param string $block_name Name of the block to find.
* @param array $blocks Array of blocks.
* @return array Found block, or empty array if none found.
*/
function gutenberg_find_first_block( $block_name, $blocks ) {
foreach ( $blocks as $block ) {
if ( $block_name === $block['blockName'] ) {
return $block;
}
if ( ! empty( $block['innerBlocks'] ) ) {
$found_block = gutenberg_find_first_block( $block_name, $block['innerBlocks'] );
if ( ! empty( $found_block ) ) {
return $found_block;
}
}
}
return array();
}
/**
* Adds styles and __experimentalFeatures to the block editor settings.
*
* @param array $settings Existing block editor settings.
*
* @return array New block editor settings.
*/
function gutenberg_get_block_editor_settings_experimental( $settings ) {
$is_block_theme = wp_is_block_theme();
global $post_ID;
if ( ! $is_block_theme || ! $post_ID ) {
return $settings;
}
$template_slug = get_page_template_slug( $post_ID );
if ( ! $template_slug ) {
$post_slug = 'singular';
$page_slug = 'singular';
$template_types = get_block_templates();
foreach ( $template_types as $template_type ) {
if ( 'page' === $template_type->slug ) {
$page_slug = 'page';
}
if ( 'single' === $template_type->slug ) {
$post_slug = 'single';
}
}
$what_post_type = get_post_type( $post_ID );
switch ( $what_post_type ) {
case 'page':
$template_slug = $page_slug;
break;
default:
$template_slug = $post_slug;
break;
}
}
$current_template = gutenberg_get_block_templates( array( 'slug__in' => array( $template_slug ) ) );
if ( ! empty( $current_template ) ) {
$template_blocks = parse_blocks( $current_template[0]->content );
$post_content_block = gutenberg_find_first_block( 'core/post-content', $template_blocks );
if ( ! empty( $post_content_block['attrs'] ) ) {
$settings['postContentAttributes'] = $post_content_block['attrs'];
}
}
return $settings;
}
add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings_experimental', PHP_INT_MAX );