Make WordPress Core

Changeset 44122 for trunk


Ignore:
Timestamp:
12/13/2018 08:22:06 PM (8 years ago)
Author:
desrosj
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-admin/includes/post.php

    r43582 r44122  
    20242024        return $clean_terms;
    20252025}
     2026
     2027/**
     2028 * Return whether the post can be edited in the block editor.
     2029 *
     2030 * @since 5.0.0
     2031 *
     2032 * @param int|WP_Post $post Post ID or WP_Post object.
     2033 * @return bool Whether the post can be edited in the block editor.
     2034 */
     2035function use_block_editor_for_post( $post ) {
     2036        $post = get_post( $post );
     2037
     2038        if ( ! $post ) {
     2039                return false;
     2040        }
     2041
     2042        $use_block_editor = use_block_editor_for_post_type( $post->post_type );
     2043
     2044        /**
     2045         * Filter whether a post is able to be edited in the block editor.
     2046         *
     2047         * @since 5.0.0
     2048         *
     2049         * @param bool    $use_block_editor Whether the post can be edited or not.
     2050         * @param WP_Post $post             The post being checked.
     2051         */
     2052        return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post );
     2053}
     2054
     2055/**
     2056 * Return whether a post type is compatible with the block editor.
     2057 *
     2058 * The block editor depends on the REST API, and if the post type is not shown in the
     2059 * REST API, then it won't work with the block editor.
     2060 *
     2061 * @since 5.0.0
     2062 *
     2063 * @param string $post_type The post type.
     2064 * @return bool Whether the post type can be edited with the block editor.
     2065 */
     2066function use_block_editor_for_post_type( $post_type ) {
     2067        if ( ! post_type_exists( $post_type ) ) {
     2068                return false;
     2069        }
     2070
     2071        if ( ! post_type_supports( $post_type, 'editor' ) ) {
     2072                return false;
     2073        }
     2074
     2075        $post_type_object = get_post_type_object( $post_type );
     2076        if ( $post_type_object && ! $post_type_object->show_in_rest ) {
     2077                return false;
     2078        }
     2079
     2080        /**
     2081         * Filter whether a post is able to be edited in the block editor.
     2082         *
     2083         * @since 5.0.0
     2084         *
     2085         * @param bool   $use_block_editor  Whether the post type can be edited or not. Default true.
     2086         * @param string $post_type         The post type being checked.
     2087         */
     2088        return apply_filters( 'use_block_editor_for_post_type', true, $post_type );
     2089}
     2090
     2091/**
     2092 * Returns all the block categories that will be shown in the block editor.
     2093 *
     2094 * @since 5.0.0
     2095 *
     2096 * @param WP_Post $post Post object.
     2097 * @return array Array of block categories.
     2098 */
     2099function get_block_categories( $post ) {
     2100        $default_categories = array(
     2101                array(
     2102                        'slug'  => 'common',
     2103                        'title' => __( 'Common Blocks', 'gutenberg' ),
     2104                ),
     2105                array(
     2106                        'slug'  => 'formatting',
     2107                        'title' => __( 'Formatting', 'gutenberg' ),
     2108                ),
     2109                array(
     2110                        'slug'  => 'layout',
     2111                        'title' => __( 'Layout Elements', 'gutenberg' ),
     2112                ),
     2113                array(
     2114                        'slug'  => 'widgets',
     2115                        'title' => __( 'Widgets', 'gutenberg' ),
     2116                ),
     2117                array(
     2118                        'slug'  => 'embed',
     2119                        'title' => __( 'Embeds', 'gutenberg' ),
     2120                ),
     2121                array(
     2122                        'slug'  => 'reusable',
     2123                        'title' => __( 'Reusable Blocks', 'gutenberg' ),
     2124                ),
     2125        );
     2126
     2127        /**
     2128         * Filter the default array of block categories.
     2129         *
     2130         * @since 5.0.0
     2131         *
     2132         * @param array   $default_categories Array of block categories.
     2133         * @param WP_Post $post               Post being loaded.
     2134         */
     2135        return apply_filters( 'block_categories', $default_categories, $post );
     2136}
     2137
     2138/**
     2139 * Prepares server-registered blocks for the block editor.
     2140 *
     2141 * Returns an associative array of registered block data keyed by block name. Data includes properties
     2142 * of a block relevant for client registration.
     2143 *
     2144 * @since 5.0.0
     2145 *
     2146 * @return array An associative array of registered block data.
     2147 */
     2148function get_block_editor_server_block_settings() {
     2149        $block_registry = WP_Block_Type_Registry::get_instance();
     2150        $blocks         = array();
     2151        $keys_to_pick   = array( 'title', 'description', 'icon', 'category', 'keywords', 'supports', 'attributes' );
     2152
     2153        foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
     2154                foreach ( $keys_to_pick as $key ) {
     2155                        if ( ! isset( $block_type->{ $key } ) ) {
     2156                                continue;
     2157                        }
     2158
     2159                        if ( ! isset( $blocks[ $block_name ] ) ) {
     2160                                $blocks[ $block_name ] = array();
     2161                        }
     2162
     2163                        $blocks[ $block_name ][ $key ] = $block_type->{ $key };
     2164                }
     2165        }
     2166
     2167        return $blocks;
     2168}
  • trunk/tests/phpunit/tests/admin/includesPost.php

    r43571 r44122  
    751751        }
    752752
     753        function test_use_block_editor_for_post() {
     754                $this->assertFalse( use_block_editor_for_post( -1 ) );
     755                $bogus_post_id = $this->factory()->post->create(
     756                        array(
     757                                'post_type' => 'bogus',
     758                        )
     759                );
     760                $this->assertFalse( use_block_editor_for_post( $bogus_post_id ) );
     761
     762                register_post_type(
     763                        'restless',
     764                        array(
     765                                'show_in_rest' => false,
     766                        )
     767                );
     768                $restless_post_id = $this->factory()->post->create(
     769                        array(
     770                                'post_type' => 'restless',
     771                        )
     772                );
     773                $this->assertFalse( use_block_editor_for_post( $restless_post_id ) );
     774
     775                $generic_post_id = $this->factory()->post->create();
     776
     777                add_filter( 'use_block_editor_for_post', '__return_false' );
     778                $this->assertFalse( use_block_editor_for_post( $generic_post_id ) );
     779                remove_filter( 'use_block_editor_for_post', '__return_false' );
     780
     781                add_filter( 'use_block_editor_for_post', '__return_true' );
     782                $this->assertTrue( use_block_editor_for_post( $restless_post_id ) );
     783                remove_filter( 'use_block_editor_for_post', '__return_true' );
     784        }
     785
     786        function test_get_block_editor_server_block_settings() {
     787                $name     = 'core/test';
     788                $settings = array(
     789                        'icon'            => 'text',
     790                        'render_callback' => 'foo',
     791                );
     792
     793                register_block_type( $name, $settings );
     794
     795                $blocks = get_block_editor_server_block_settings();
     796
     797                unregister_block_type( $name );
     798
     799                $this->assertArrayHasKey( $name, $blocks );
     800                $this->assertSame( array( 'icon' => 'text' ), $blocks[ $name ] );
     801        }
    753802}
Note: See TracChangeset for help on using the changeset viewer.