WordPress.org

Make WordPress Core

Changeset 43762


Ignore:
Timestamp:
10/19/2018 08:43:25 AM (13 months ago)
Author:
pento
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.

See #45110.

Location:
branches/5.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-admin/includes/post.php

    r43064 r43762  
    18751875    exit;
    18761876}
     1877
     1878/**
     1879 * Return whether the post can be edited in the block editor.
     1880 *
     1881 * @since 5.0.0
     1882 *
     1883 * @param int|WP_Post $post Post ID or WP_Post object.
     1884 * @return bool Whether the post can be edited in the block editor.
     1885 */
     1886function use_block_editor_for_post( $post ) {
     1887    $post = get_post( $post );
     1888
     1889    if ( ! $post ) {
     1890        return false;
     1891    }
     1892
     1893    $use_block_editor = use_block_editor_for_post_type( $post->post_type );
     1894
     1895    /**
     1896     * Filter whether a post is able to be edited in the block editor.
     1897     *
     1898     * @since 5.0.0
     1899     *
     1900     * @param bool    $use_block_editor Whether the post can be edited or not.
     1901     * @param WP_Post $post             The post being checked.
     1902     */
     1903    return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post );
     1904}
     1905
     1906/**
     1907 * Return whether a post type is compatible with the block editor.
     1908 *
     1909 * The block editor depends on the REST API, and if the post type is not shown in the
     1910 * REST API, then it won't work with the block editor.
     1911 *
     1912 * @since 5.0.0
     1913 *
     1914 * @param string $post_type The post type.
     1915 * @return bool Whether the post type can be edited with the block editor.
     1916 */
     1917function use_block_editor_for_post_type( $post_type ) {
     1918    if ( ! post_type_exists( $post_type ) ) {
     1919        return false;
     1920    }
     1921
     1922    if ( ! post_type_supports( $post_type, 'editor' ) ) {
     1923        return false;
     1924    }
     1925
     1926    $post_type_object = get_post_type_object( $post_type );
     1927    if ( $post_type_object && ! $post_type_object->show_in_rest ) {
     1928        return false;
     1929    }
     1930
     1931    /**
     1932     * Filter whether a post is able to be edited in the block editor.
     1933     *
     1934     * @since 5.0.0
     1935     *
     1936     * @param bool   $use_block_editor  Whether the post type can be edited or not. Default true.
     1937     * @param string $post_type         The post type being checked.
     1938     */
     1939    return apply_filters( 'use_block_editor_for_post_type', true, $post_type );
     1940}
     1941
     1942/**
     1943 * Returns all the block categories that will be shown in the block editor.
     1944 *
     1945 * @since 5.0.0
     1946 *
     1947 * @param WP_Post $post Post object.
     1948 * @return array Array of block categories.
     1949 */
     1950function get_block_categories( $post ) {
     1951    $default_categories = array(
     1952        array(
     1953            'slug'  => 'common',
     1954            'title' => __( 'Common Blocks', 'gutenberg' ),
     1955        ),
     1956        array(
     1957            'slug'  => 'formatting',
     1958            'title' => __( 'Formatting', 'gutenberg' ),
     1959        ),
     1960        array(
     1961            'slug'  => 'layout',
     1962            'title' => __( 'Layout Elements', 'gutenberg' ),
     1963        ),
     1964        array(
     1965            'slug'  => 'widgets',
     1966            'title' => __( 'Widgets', 'gutenberg' ),
     1967        ),
     1968        array(
     1969            'slug'  => 'embed',
     1970            'title' => __( 'Embeds', 'gutenberg' ),
     1971        ),
     1972        array(
     1973            'slug'  => 'reusable',
     1974            'title' => __( 'Reusable Blocks', 'gutenberg' ),
     1975        ),
     1976    );
     1977
     1978    /**
     1979     * Filter the default array of block categories.
     1980     *
     1981     * @since 5.0.0
     1982     *
     1983     * @param array   $default_categories Array of block categories.
     1984     * @param WP_Post $post               Post being loaded.
     1985     */
     1986    return apply_filters( 'block_categories', $default_categories, $post );
     1987}
     1988
     1989/**
     1990 * Prepares server-registered blocks for the block editor.
     1991 *
     1992 * Returns an associative array of registered block data keyed by block name. Data includes properties
     1993 * of a block relevant for client registration.
     1994 *
     1995 * @since 5.0.0
     1996 *
     1997 * @return array An associative array of registered block data.
     1998 */
     1999function get_block_editor_server_block_settings() {
     2000    $block_registry = WP_Block_Type_Registry::get_instance();
     2001    $blocks         = array();
     2002    $keys_to_pick   = array( 'title', 'description', 'icon', 'category', 'keywords', 'supports', 'attributes' );
     2003
     2004    foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
     2005        foreach ( $keys_to_pick as $key ) {
     2006            if ( ! isset( $block_type->{ $key } ) ) {
     2007                continue;
     2008            }
     2009
     2010            if ( ! isset( $blocks[ $block_name ] ) ) {
     2011                $blocks[ $block_name ] = array();
     2012            }
     2013
     2014            $blocks[ $block_name ][ $key ] = $block_type->{ $key };
     2015        }
     2016    }
     2017
     2018    return $blocks;
     2019}
  • branches/5.0/tests/phpunit/tests/admin/includesPost.php

    r41187 r43762  
    659659    }
    660660
     661    function test_use_block_editor_for_post() {
     662        $this->assertFalse( use_block_editor_for_post( -1 ) );
     663        $bogus_post_id = $this->factory()->post->create(
     664            array(
     665                'post_type' => 'bogus',
     666            )
     667        );
     668        $this->assertFalse( use_block_editor_for_post( $bogus_post_id ) );
     669
     670        register_post_type(
     671            'restless',
     672            array(
     673                'show_in_rest' => false,
     674            )
     675        );
     676        $restless_post_id = $this->factory()->post->create(
     677            array(
     678                'post_type' => 'restless',
     679            )
     680        );
     681        $this->assertFalse( use_block_editor_for_post( $restless_post_id ) );
     682
     683        $generic_post_id = $this->factory()->post->create();
     684
     685        add_filter( 'use_block_editor_for_post', '__return_false' );
     686        $this->assertFalse( use_block_editor_for_post( $generic_post_id ) );
     687        remove_filter( 'use_block_editor_for_post', '__return_false' );
     688
     689        add_filter( 'use_block_editor_for_post', '__return_true' );
     690        $this->assertTrue( use_block_editor_for_post( $restless_post_id ) );
     691        remove_filter( 'use_block_editor_for_post', '__return_true' );
     692    }
     693
     694    function test_get_block_editor_server_block_settings() {
     695        $name     = 'core/test';
     696        $settings = array(
     697            'icon'            => 'text',
     698            'render_callback' => 'foo',
     699        );
     700
     701        register_block_type( $name, $settings );
     702
     703        $blocks = get_block_editor_server_block_settings();
     704
     705        unregister_block_type( $name );
     706
     707        $this->assertArrayHasKey( $name, $blocks );
     708        $this->assertSame( array( 'icon' => 'text' ), $blocks[ $name ] );
     709    }
    661710}
Note: See TracChangeset for help on using the changeset viewer.