Block Editor: Add helper functions for displaying the editor.

`use_block_editor_for_post()` and `use_block_editor_for_post_type()` determine if the block editor should be loaded.

`get_block_categories()` and `get_block_editor_server_block_settings()` provide data be included while the block editor is loading.

Props pento.

Merges [43762] to trunk.

See #45110.
Built from https://develop.svn.wordpress.org/trunk@44122


git-svn-id: http://core.svn.wordpress.org/trunk@43952 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
desrosj 2018-12-13 22:25:11 +00:00
parent cf6e69ee9f
commit 13226b445e
2 changed files with 144 additions and 1 deletions

View File

@ -2023,3 +2023,146 @@ function taxonomy_meta_box_sanitize_cb_input( $taxonomy, $terms ) {
return $clean_terms;
}
/**
* Return whether the post can be edited in the block editor.
*
* @since 5.0.0
*
* @param int|WP_Post $post Post ID or WP_Post object.
* @return bool Whether the post can be edited in the block editor.
*/
function use_block_editor_for_post( $post ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$use_block_editor = use_block_editor_for_post_type( $post->post_type );
/**
* Filter whether a post is able to be edited in the block editor.
*
* @since 5.0.0
*
* @param bool $use_block_editor Whether the post can be edited or not.
* @param WP_Post $post The post being checked.
*/
return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post );
}
/**
* Return whether a post type is compatible with the block editor.
*
* The block editor depends on the REST API, and if the post type is not shown in the
* REST API, then it won't work with the block editor.
*
* @since 5.0.0
*
* @param string $post_type The post type.
* @return bool Whether the post type can be edited with the block editor.
*/
function use_block_editor_for_post_type( $post_type ) {
if ( ! post_type_exists( $post_type ) ) {
return false;
}
if ( ! post_type_supports( $post_type, 'editor' ) ) {
return false;
}
$post_type_object = get_post_type_object( $post_type );
if ( $post_type_object && ! $post_type_object->show_in_rest ) {
return false;
}
/**
* Filter whether a post is able to be edited in the block editor.
*
* @since 5.0.0
*
* @param bool $use_block_editor Whether the post type can be edited or not. Default true.
* @param string $post_type The post type being checked.
*/
return apply_filters( 'use_block_editor_for_post_type', true, $post_type );
}
/**
* Returns all the block categories that will be shown in the block editor.
*
* @since 5.0.0
*
* @param WP_Post $post Post object.
* @return array Array of block categories.
*/
function get_block_categories( $post ) {
$default_categories = array(
array(
'slug' => 'common',
'title' => __( 'Common Blocks', 'gutenberg' ),
),
array(
'slug' => 'formatting',
'title' => __( 'Formatting', 'gutenberg' ),
),
array(
'slug' => 'layout',
'title' => __( 'Layout Elements', 'gutenberg' ),
),
array(
'slug' => 'widgets',
'title' => __( 'Widgets', 'gutenberg' ),
),
array(
'slug' => 'embed',
'title' => __( 'Embeds', 'gutenberg' ),
),
array(
'slug' => 'reusable',
'title' => __( 'Reusable Blocks', 'gutenberg' ),
),
);
/**
* Filter the default array of block categories.
*
* @since 5.0.0
*
* @param array $default_categories Array of block categories.
* @param WP_Post $post Post being loaded.
*/
return apply_filters( 'block_categories', $default_categories, $post );
}
/**
* Prepares server-registered blocks for the block editor.
*
* Returns an associative array of registered block data keyed by block name. Data includes properties
* of a block relevant for client registration.
*
* @since 5.0.0
*
* @return array An associative array of registered block data.
*/
function get_block_editor_server_block_settings() {
$block_registry = WP_Block_Type_Registry::get_instance();
$blocks = array();
$keys_to_pick = array( 'title', 'description', 'icon', 'category', 'keywords', 'supports', 'attributes' );
foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
foreach ( $keys_to_pick as $key ) {
if ( ! isset( $block_type->{ $key } ) ) {
continue;
}
if ( ! isset( $blocks[ $block_name ] ) ) {
$blocks[ $block_name ] = array();
}
$blocks[ $block_name ][ $key ] = $block_type->{ $key };
}
}
return $blocks;
}

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.1-alpha-44121';
$wp_version = '5.1-alpha-44122';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.