diff --git a/lib/block-editor.php b/lib/block-editor.php new file mode 100644 index 0000000000000..81603276d27b1 --- /dev/null +++ b/lib/block-editor.php @@ -0,0 +1,293 @@ + 'text', + 'title' => _x( 'Text', 'block category' ), + 'icon' => null, + ), + array( + 'slug' => 'media', + 'title' => _x( 'Media', 'block category' ), + 'icon' => null, + ), + array( + 'slug' => 'design', + 'title' => _x( 'Design', 'block category' ), + 'icon' => null, + ), + array( + 'slug' => 'widgets', + 'title' => _x( 'Widgets', 'block category' ), + 'icon' => null, + ), + array( + 'slug' => 'theme', + 'title' => _x( 'Theme', 'block category' ), + 'icon' => null, + ), + array( + 'slug' => 'embed', + 'title' => _x( 'Embeds', 'block category' ), + 'icon' => null, + ), + array( + 'slug' => 'reusable', + 'title' => _x( 'Reusable Blocks', 'block category' ), + 'icon' => null, + ), + ); +} + +/** + * Returns all the categories for block types that will be shown in the block editor. + * + * This is a temporary solution until the Gutenberg plugin sets + * the required WordPress version to 5.8. + * + * @see https://core.trac.wordpress.org/ticket/52920 + * + * @since 10.5.0 + * + * @param string|WP_Post $editor_name_or_post The name of the editor (e.g. 'post-editor') + * or the post object. + * + * @return array[] Array of categories for block types. + */ +function gutenberg_get_block_categories( $editor_name_or_post ) { + // Assume the post editor when the WP_Post object passed. + $editor_name = is_object( $editor_name_or_post ) ? 'post-editor' : $editor_name_or_post; + $default_categories = get_default_block_categories(); + + /** + * Filters the default array of categories for block types. + * + * @since 5.8.0 + * + * @param array[] $default_categories Array of categories for block types. + */ + $block_categories = apply_filters( "block_categories_{$editor_name}", $default_categories ); + if ( 'post-editor' === $editor_name ) { + $post = is_object( $editor_name_or_post ) ? $editor_name_or_post : get_post(); + + /** + * Filters the default array of categories for block types. + * + * @since 5.0.0 + * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain the $post instance. + * + * @param array[] $block_categories Array of categories for block types. + * @param WP_Post $post Post being loaded. + */ + $block_categories = apply_filters_deprecated( 'block_categories', array( $block_categories, $post ), '5.8.0', "block_categories_{$editor_name}" ); + } + + return $block_categories; +} + +/** + * Gets the list of allowed block types to use in the block editor. + * + * This is a temporary solution until the Gutenberg plugin sets + * the required WordPress version to 5.8. + * + * @see https://core.trac.wordpress.org/ticket/52920 + * + * @since 10.5.0 + * + * @param string $editor_name The name of the editor (e.g. 'post-editor'). + * + * @return bool|array Array of block type slugs, or boolean to enable/disable all. + */ +function gutenberg_get_allowed_block_types( $editor_name ) { + $allowed_block_types = true; + + /** + * Filters the allowed block types for the given editor, defaulting to true (all + * registered block types supported). + * + * @since 5.8.0 + * + * @param bool|array $allowed_block_types Array of block type slugs, or + * boolean to enable/disable all. + */ + $allowed_block_types = apply_filters( "allowed_block_types_{$editor_name}", $allowed_block_types ); + if ( 'post-editor' === $editor_name ) { + $post = get_post(); + + /** + * Filters the allowed block types for the editor, defaulting to true (all + * block types supported). + * + * @since 5.0.0 + * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain $post instance. + * + * @param bool|array $allowed_block_types Array of block type slugs, or + * boolean to enable/disable all. + * @param WP_Post $post The post resource data. + */ + $allowed_block_types = apply_filters_deprecated( 'allowed_block_types', array( $allowed_block_types, $post ), '5.8.0', "allowed_block_types_{$editor_name}" ); + } + + return $allowed_block_types; +} + +/** + * Returns the default block editor settings. + * + * This is a temporary solution until the Gutenberg plugin sets + * the required WordPress version to 5.8. + * + * @see https://core.trac.wordpress.org/ticket/52920 + * + * @since 10.5.0 + * + * @return array The default block editor settings. + */ +function gutenberg_get_default_block_editor_settings() { + // Media settings. + $max_upload_size = wp_max_upload_size(); + if ( ! $max_upload_size ) { + $max_upload_size = 0; + } + + /** This filter is documented in wp-admin/includes/media.php */ + $image_size_names = apply_filters( + 'image_size_names_choose', + array( + 'thumbnail' => __( 'Thumbnail' ), + 'medium' => __( 'Medium' ), + 'large' => __( 'Large' ), + 'full' => __( 'Full Size' ), + ) + ); + + $available_image_sizes = array(); + foreach ( $image_size_names as $image_size_slug => $image_size_name ) { + $available_image_sizes[] = array( + 'slug' => $image_size_slug, + 'name' => $image_size_name, + ); + } + + $default_size = get_option( 'image_default_size', 'large' ); + $image_default_size = in_array( $default_size, array_keys( $image_size_names ), true ) ? $image_default_size : 'large'; + + $image_dimensions = array(); + $all_sizes = wp_get_registered_image_subsizes(); + foreach ( $available_image_sizes as $size ) { + $key = $size['slug']; + if ( isset( $all_sizes[ $key ] ) ) { + $image_dimensions[ $key ] = $all_sizes[ $key ]; + } + } + + $editor_settings = array( + '__unstableEnableFullSiteEditingBlocks' => gutenberg_supports_block_templates(), + 'alignWide' => get_theme_support( 'align-wide' ), + 'allowedBlockTypes' => true, + 'allowedMimeTypes' => get_allowed_mime_types(), + 'blockCategories' => gutenberg_get_default_block_categories(), + 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), + 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), + 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), + 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ), + 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ), + 'enableCustomUnits' => get_theme_support( 'custom-units' ), + 'isRTL' => is_rtl(), + 'imageDefaultSize' => $image_default_size, + 'imageDimensions' => $image_dimensions, + 'imageEditing' => true, + 'imageSizes' => $available_image_sizes, + 'maxUploadFileSize' => $max_upload_size, + ); + + // Theme settings. + $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); + if ( false !== $color_palette ) { + $editor_settings['colors'] = $color_palette; + } + + $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); + if ( false !== $font_sizes ) { + $editor_settings['fontSizes'] = $font_sizes; + } + + $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) ); + if ( false !== $gradient_presets ) { + $editor_settings['gradients'] = $gradient_presets; + } + + return $editor_settings; +} + +/** + * Returns the contextualized block editor settings settings for a selected editor type. + * + * This is a temporary solution until the Gutenberg plugin sets + * the required WordPress version to 5.8. + * + * @see https://core.trac.wordpress.org/ticket/52920 + * + * @since 10.5.0 + * + * @param string $editor_name The name of the editor (e.g. 'post-editor'). + * @param array $custom_settings Optional custom settings to use with the editor type. + * + * @return array The contextualized block editor settings. + */ +function gutenberg_get_block_editor_settings( $editor_name, $custom_settings = array() ) { + $editor_settings = array_merge( + gutenberg_get_default_block_editor_settings( $editor_name ), + array( + 'allowedBlockTypes' => gutenberg_get_allowed_block_types( $editor_name ), + 'blockCategories' => gutenberg_get_block_categories( $editor_name ), + ), + $custom_settings + ); + + /** + * Filters the settings to pass to the block editor for a given editor type. + * + * @since 5.8.0 + * + * @param array $editor_settings Default editor settings. + */ + $editor_settings = apply_filters( "block_editor_settings_{$editor_name}", $editor_settings ); + if ( 'post-editor' === $editor_name ) { + $post = get_post(); + + /** + * Filters the settings to pass to the block editor. + * + * @since 5.0.0 + * @deprecated 5.8.0 The hook transitioned to support also screens that don't contain $post instance. + * + * @param array $editor_settings Default editor settings. + * @param WP_Post $post Post being edited. + */ + $editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', "block_editor_settings_{$editor_name}" ); + } + + return $editor_settings; +} diff --git a/lib/editor-settings.php b/lib/editor-settings.php index 73912d63e028b..0c8ed73d29c38 100644 --- a/lib/editor-settings.php +++ b/lib/editor-settings.php @@ -5,71 +5,6 @@ * @package gutenberg */ -/** - * Returns editor settings that are common to all editors: - * post, site, widgets, and navigation. - * - * All these settings are already part of core, - * see edit-form-blocks.php - * - * @return array Common editor settings. - */ -function gutenberg_get_common_block_editor_settings() { - $max_upload_size = wp_max_upload_size(); - if ( ! $max_upload_size ) { - $max_upload_size = 0; - } - - $available_image_sizes = array(); - // This filter is documented in wp-admin/includes/media.php. - $image_size_names = apply_filters( - 'image_size_names_choose', - array( - 'thumbnail' => __( 'Thumbnail', 'gutenberg' ), - 'medium' => __( 'Medium', 'gutenberg' ), - 'large' => __( 'Large', 'gutenberg' ), - 'full' => __( 'Full Size', 'gutenberg' ), - ) - ); - foreach ( $image_size_names as $image_size_slug => $image_size_name ) { - $available_image_sizes[] = array( - 'slug' => $image_size_slug, - 'name' => $image_size_name, - ); - } - - $settings = array( - '__unstableEnableFullSiteEditingBlocks' => gutenberg_supports_block_templates(), - 'disableCustomColors' => get_theme_support( 'disable-custom-colors' ), - 'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ), - 'disableCustomGradients' => get_theme_support( 'disable-custom-gradients' ), - 'enableCustomLineHeight' => get_theme_support( 'custom-line-height' ), - 'enableCustomUnits' => get_theme_support( 'custom-units' ), - 'enableCustomSpacing' => get_theme_support( 'custom-spacing' ), - 'imageSizes' => $available_image_sizes, - 'isRTL' => is_rtl(), - 'maxUploadFileSize' => $max_upload_size, - 'allowedMimeTypes' => get_allowed_mime_types(), - ); - - $color_palette = current( (array) get_theme_support( 'editor-color-palette' ) ); - if ( false !== $color_palette ) { - $settings['colors'] = $color_palette; - } - - $font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) ); - if ( false !== $font_sizes ) { - $settings['fontSizes'] = $font_sizes; - } - - $gradient_presets = current( (array) get_theme_support( 'editor-gradient-presets' ) ); - if ( false !== $gradient_presets ) { - $settings['gradients'] = $gradient_presets; - } - - return $settings; -} - /** * Extends the block editor with settings that are only in the plugin. * @@ -78,10 +13,6 @@ function gutenberg_get_common_block_editor_settings() { * @return array Filtered settings. */ function gutenberg_extend_post_editor_settings( $settings ) { - $image_default_size = get_option( 'image_default_size', 'large' ); - $image_sizes = wp_list_pluck( $settings['imageSizes'], 'slug' ); - - $settings['imageDefaultSize'] = in_array( $image_default_size, $image_sizes, true ) ? $image_default_size : 'large'; $settings['__unstableEnableFullSiteEditingBlocks'] = gutenberg_supports_block_templates(); return $settings; diff --git a/lib/full-site-editing/edit-site-page.php b/lib/full-site-editing/edit-site-page.php index 783491faa5e74..8d9f09878cce8 100644 --- a/lib/full-site-editing/edit-site-page.php +++ b/lib/full-site-editing/edit-site-page.php @@ -90,9 +90,8 @@ function gutenberg_edit_site_init( $hook ) { $current_screen->is_block_editor( true ); $settings = array_merge( - gutenberg_get_common_block_editor_settings(), + gutenberg_get_default_block_editor_settings(), array( - 'alignWide' => get_theme_support( 'align-wide' ), 'siteUrl' => site_url(), 'postsPerPage' => get_option( 'posts_per_page' ), 'styles' => gutenberg_get_editor_styles(), diff --git a/lib/global-styles.php b/lib/global-styles.php index 4bef8e7b93751..5cef2e7835dd6 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -63,7 +63,7 @@ function gutenberg_experimental_global_styles_enqueue_assets() { return; } - $settings = gutenberg_get_common_block_editor_settings(); + $settings = gutenberg_get_default_block_editor_settings(); $theme_support_data = WP_Theme_JSON::get_from_editor_settings( $settings ); $all = WP_Theme_JSON_Resolver::get_merged_data( $theme_support_data ); diff --git a/lib/load.php b/lib/load.php index 2deda54b6646b..3c817423d71c8 100644 --- a/lib/load.php +++ b/lib/load.php @@ -106,6 +106,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/full-site-editing/edit-site-export.php'; require __DIR__ . '/blocks.php'; +require __DIR__ . '/block-editor.php'; require __DIR__ . '/block-patterns.php'; require __DIR__ . '/client-assets.php'; require __DIR__ . '/demo.php'; diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 1837245544c53..795e7d93baf2a 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -33,7 +33,7 @@ function gutenberg_navigation_init( $hook ) { } $settings = array_merge( - gutenberg_get_common_block_editor_settings(), + gutenberg_get_default_block_editor_settings(), array( 'blockNavMenus' => get_theme_support( 'block-nav-menus' ), ) diff --git a/lib/widgets-page.php b/lib/widgets-page.php index 5e95295a7d04a..891153bcd72b2 100644 --- a/lib/widgets-page.php +++ b/lib/widgets-page.php @@ -42,7 +42,7 @@ function gutenberg_widgets_init( $hook ) { } $settings = array_merge( - gutenberg_get_common_block_editor_settings(), + gutenberg_get_default_block_editor_settings(), gutenberg_get_legacy_widget_settings() ); diff --git a/phpunit/block-editor.php b/phpunit/block-editor.php new file mode 100644 index 0000000000000..cf30a961f0fbc --- /dev/null +++ b/phpunit/block-editor.php @@ -0,0 +1,315 @@ + 'Example', + ); + + $post = $this->factory()->post->create_and_get( $args ); + } + + function filter_set_block_categories_post( $block_categories, $post ) { + if ( empty( $post ) ) { + return $block_categories; + } + + return array( + array( + 'slug' => 'filtered-category', + 'title' => 'Filtered Category', + 'icon' => null, + ), + ); + } + + function filter_set_allowed_block_types_post( $allowed_block_types, $post ) { + if ( empty( $post ) ) { + return $allowed_block_types; + } + + return array( 'test/filtered-block' ); + } + + function filter_set_block_editor_settings_post( $editor_settings, $post ) { + if ( empty( $post ) ) { + return $editor_settings; + } + + return array( + 'filter' => 'deprecated', + ); + } + + /** + * @ticket 52920 + * @expectedDeprecated block_categories + */ + function test_get_block_categories_deprecated_filter_post_object() { + add_filter( 'block_categories', array( $this, 'filter_set_block_categories_post' ), 10, 2 ); + + $block_categories = gutenberg_get_block_categories( get_post() ); + + remove_filter( 'block_categories', array( $this, 'filter_set_block_categories_post' ) ); + + $this->assertSameSets( + array( + array( + 'slug' => 'filtered-category', + 'title' => 'Filtered Category', + 'icon' => null, + ), + ), + $block_categories + ); + } + + /** + * @ticket 52920 + * @expectedDeprecated block_categories + */ + function test_get_block_categories_deprecated_filter_post_editor() { + add_filter( 'block_categories', array( $this, 'filter_set_block_categories_post' ), 10, 2 ); + + $block_categories = gutenberg_get_block_categories( 'post-editor' ); + + remove_filter( 'block_categories', array( $this, 'filter_set_block_categories_post' ) ); + + $this->assertSameSets( + array( + array( + 'slug' => 'filtered-category', + 'title' => 'Filtered Category', + 'icon' => null, + ), + ), + $block_categories + ); + } + + /** + * @ticket 52920 + */ + function test_get_allowed_block_types_default() { + $allowed_block_types = gutenberg_get_allowed_block_types( 'post-editor' ); + + $this->assertTrue( $allowed_block_types ); + } + + /** + * @ticket 52920 + * @expectedDeprecated allowed_block_types + */ + function test_get_allowed_block_types_deprecated_filter_post_editor() { + add_filter( 'allowed_block_types', array( $this, 'filter_set_allowed_block_types_post' ), 10, 2 ); + + $allowed_block_types = gutenberg_get_allowed_block_types( 'post-editor' ); + + remove_filter( 'allowed_block_types', array( $this, 'filter_set_allowed_block_types_post' ) ); + + $this->assertSameSets( array( 'test/filtered-block' ), $allowed_block_types ); + } + + /** + * @ticket 52920 + */ + function test_get_default_block_editor_settings() { + $settings = gutenberg_get_default_block_editor_settings(); + + $this->assertCount( 16, $settings ); + $this->assertFalse( $settings['alignWide'] ); + $this->assertInternalType( 'array', $settings['allowedMimeTypes'] ); + $this->assertTrue( $settings['allowedBlockTypes'] ); + $this->assertSameSets( + array( + array( + 'slug' => 'text', + 'title' => 'Text', + 'icon' => null, + ), + array( + 'slug' => 'media', + 'title' => 'Media', + 'icon' => null, + ), + array( + 'slug' => 'design', + 'title' => 'Design', + 'icon' => null, + ), + array( + 'slug' => 'widgets', + 'title' => 'Widgets', + 'icon' => null, + ), + array( + 'slug' => 'theme', + 'title' => 'Theme', + 'icon' => null, + ), + array( + 'slug' => 'embed', + 'title' => 'Embeds', + 'icon' => null, + ), + array( + 'slug' => 'reusable', + 'title' => 'Reusable Blocks', + 'icon' => null, + ), + ), + $settings['blockCategories'] + ); + $this->assertFalse( $settings['disableCustomColors'] ); + $this->assertFalse( $settings['disableCustomFontSizes'] ); + $this->assertFalse( $settings['disableCustomGradients'] ); + $this->assertFalse( $settings['enableCustomLineHeight'] ); + $this->assertFalse( $settings['enableCustomSpacing'] ); + $this->assertFalse( $settings['enableCustomUnits'] ); + $this->assertFalse( $settings['isRTL'] ); + $this->assertSame( 'large', $settings['imageDefaultSize'] ); + $this->assertSameSets( + array( + array( + 'width' => 150, + 'height' => 150, + 'crop' => true, + ), + array( + 'width' => 300, + 'height' => 300, + 'crop' => false, + ), + array( + 'width' => 1024, + 'height' => 1024, + 'crop' => false, + ), + ), + $settings['imageDimensions'] + ); + $this->assertTrue( $settings['imageEditing'] ); + $this->assertSameSets( + array( + array( + 'slug' => 'full', + 'name' => 'Full Size', + ), + array( + 'slug' => 'large', + 'name' => 'Large', + ), + array( + 'slug' => 'medium', + 'name' => 'Medium', + ), + array( + 'slug' => 'thumbnail', + 'name' => 'Thumbnail', + ), + ), + $settings['imageSizes'] + ); + $this->assertInternalType( 'int', $settings['maxUploadFileSize'] ); + } + + /** + * @ticket 52920 + */ + function test_get_block_editor_settings_returns_default_settings() { + $this->assertSameSets( + gutenberg_get_block_editor_settings( 'post-editor' ), + gutenberg_get_default_block_editor_settings() + ); + } + + /** + * @ticket 52920 + */ + function test_get_block_editor_settings_overrides_default_settings_my_editor() { + function filter_allowed_block_types_my_editor() { + return array( 'test/filtered-my-block' ); + } + function filter_block_categories_my_editor() { + return array( + array( + 'slug' => 'filtered-my-category', + 'title' => 'Filtered My Category', + 'icon' => null, + ), + ); + } + function filter_block_editor_settings_my_editor( $editor_settings ) { + $editor_settings['maxUploadFileSize'] = 12345; + + return $editor_settings; + } + + add_filter( 'allowed_block_types_my-editor', 'filter_allowed_block_types_my_editor', 10, 1 ); + add_filter( 'block_categories_my-editor', 'filter_block_categories_my_editor', 10, 1 ); + add_filter( 'block_editor_settings_my-editor', 'filter_block_editor_settings_my_editor', 10, 1 ); + + $settings = get_block_editor_settings( 'my-editor' ); + + remove_filter( 'allowed_block_types_my-editor', 'filter_allowed_block_types_my_editor' ); + remove_filter( 'block_categories_my-editor', 'filter_block_categories_my_editor' ); + remove_filter( 'block_editor_settings_my-editor', 'filter_block_editor_settings_my_editor' ); + + $this->assertSameSets( array( 'test/filtered-my-block' ), $settings['allowedBlockTypes'] ); + $this->assertSameSets( + array( + array( + 'slug' => 'filtered-my-category', + 'title' => 'Filtered My Category', + 'icon' => null, + ), + ), + $settings['blockCategories'] + ); + $this->assertSame( 12345, $settings['maxUploadFileSize'] ); + } + + /** + * @ticket 52920 + * @expectedDeprecated block_editor_settings + */ + function test_get_block_editor_settings_deprecated_filter_post_editor() { + add_filter( 'block_editor_settings', array( $this, 'filter_set_block_editor_settings_post' ), 10, 2 ); + + $settings = get_block_editor_settings( 'post-editor' ); + + remove_filter( 'block_editor_settings', array( $this, 'filter_set_block_editor_settings_post' ) ); + + $this->assertSameSets( + array( + 'filter' => 'deprecated', + ), + $settings + ); + } +} diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index 8721bbeafc956..71324b61cfeeb 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -959,7 +959,7 @@ function test_get_editor_settings_blank() { function test_get_editor_settings_custom_units_can_be_disabled() { add_theme_support( 'custom-units', array() ); - $input = gutenberg_get_common_block_editor_settings(); + $input = gutenberg_get_default_block_editor_settings(); $expected = array( 'units' => array( array() ), @@ -973,7 +973,7 @@ function test_get_editor_settings_custom_units_can_be_disabled() { function test_get_editor_settings_custom_units_can_be_enabled() { add_theme_support( 'custom-units' ); - $input = gutenberg_get_common_block_editor_settings(); + $input = gutenberg_get_default_block_editor_settings(); $expected = array( 'units' => array( 'px', 'em', 'rem', 'vh', 'vw' ), @@ -987,7 +987,7 @@ function test_get_editor_settings_custom_units_can_be_enabled() { function test_get_editor_settings_custom_units_can_be_filtered() { add_theme_support( 'custom-units', 'rem', 'em' ); - $input = gutenberg_get_common_block_editor_settings(); + $input = gutenberg_get_default_block_editor_settings(); $expected = array( 'units' => array( 'rem', 'em' ),