fonts-api
2 years ago
interactivity-api
2 years ago
block-editor-settings-mobile.php
2 years ago
block-editor-settings.php
2 years ago
blocks.php
2 years ago
class-gutenberg-rest-global-styles-revisions-controller.php
2 years ago
class-wp-classic-to-block-menu-converter.php
2 years ago
class-wp-navigation-fallback-gutenberg.php
2 years ago
class-wp-rest-block-editor-settings-controller.php
2 years ago
class-wp-rest-customizer-nonces.php
2 years ago
class-wp-rest-navigation-fallback-controller.php
2 years ago
editor-settings.php
2 years ago
kses.php
2 years ago
l10n.php
2 years ago
navigation-fallback.php
2 years ago
navigation-theme-opt-in.php
2 years ago
rest-api.php
2 years ago
block-editor-settings.php
90 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Adds settings to the block editor. |
| 4 | * |
| 5 | * @package gutenberg |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Finds the first occurrence of a specific block in an array of blocks. |
| 10 | * |
| 11 | * @param string $block_name Name of the block to find. |
| 12 | * @param array $blocks Array of blocks. |
| 13 | * @return array Found block, or empty array if none found. |
| 14 | */ |
| 15 | function gutenberg_find_first_block( $block_name, $blocks ) { |
| 16 | foreach ( $blocks as $block ) { |
| 17 | if ( $block_name === $block['blockName'] ) { |
| 18 | return $block; |
| 19 | } |
| 20 | if ( ! empty( $block['innerBlocks'] ) ) { |
| 21 | $found_block = gutenberg_find_first_block( $block_name, $block['innerBlocks'] ); |
| 22 | |
| 23 | if ( ! empty( $found_block ) ) { |
| 24 | return $found_block; |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | return array(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Adds styles and __experimentalFeatures to the block editor settings. |
| 34 | * |
| 35 | * @param array $settings Existing block editor settings. |
| 36 | * |
| 37 | * @return array New block editor settings. |
| 38 | */ |
| 39 | function gutenberg_get_block_editor_settings_experimental( $settings ) { |
| 40 | $is_block_theme = wp_is_block_theme(); |
| 41 | |
| 42 | global $post_ID; |
| 43 | |
| 44 | if ( ! $is_block_theme || ! $post_ID ) { |
| 45 | return $settings; |
| 46 | } |
| 47 | |
| 48 | $template_slug = get_page_template_slug( $post_ID ); |
| 49 | |
| 50 | if ( ! $template_slug ) { |
| 51 | $post_slug = 'singular'; |
| 52 | $page_slug = 'singular'; |
| 53 | $template_types = get_block_templates(); |
| 54 | |
| 55 | foreach ( $template_types as $template_type ) { |
| 56 | if ( 'page' === $template_type->slug ) { |
| 57 | $page_slug = 'page'; |
| 58 | } |
| 59 | if ( 'single' === $template_type->slug ) { |
| 60 | $post_slug = 'single'; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | $what_post_type = get_post_type( $post_ID ); |
| 65 | switch ( $what_post_type ) { |
| 66 | case 'page': |
| 67 | $template_slug = $page_slug; |
| 68 | break; |
| 69 | default: |
| 70 | $template_slug = $post_slug; |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | $current_template = get_block_templates( array( 'slug__in' => array( $template_slug ) ) ); |
| 76 | |
| 77 | if ( ! empty( $current_template ) ) { |
| 78 | $template_blocks = parse_blocks( $current_template[0]->content ); |
| 79 | $post_content_block = gutenberg_find_first_block( 'core/post-content', $template_blocks ); |
| 80 | |
| 81 | if ( ! empty( $post_content_block['attrs'] ) ) { |
| 82 | $settings['postContentAttributes'] = $post_content_block['attrs']; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return $settings; |
| 87 | } |
| 88 | |
| 89 | add_filter( 'block_editor_settings_all', 'gutenberg_get_block_editor_settings_experimental', PHP_INT_MAX ); |
| 90 |