Make WordPress Core

Changeset 53559


Ignore:
Timestamp:
06/23/2022 06:46:18 PM (4 years ago)
Author:
jorbin
Message:

Editor: Universalize functions for checking block editor status.

use_block_editor_for_post_type and use_block_editor_for_post can be very useful in more contexts than wp-admin, especially when a site is in transition. For example, you may want to do things on init that are different.

Neither function depends on other functions that are available only in wp-admin (other than use_block_editor_for_post() relying on use_block_editor_for_post_type() and an admin-referrer check that's historically gated by a query variable and now also gated by is_admin), therefore moving them to wp-includes seems both feasible and beneficial

Props ethitter, jorbin.
Fixes #51819.

Location:
trunk
Files:
4 edited

Legend:

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

    r53455 r53559  
    21492149
    21502150/**
    2151  * Returns whether the post can be edited in the block editor.
    2152  *
    2153  * @since 5.0.0
    2154  *
    2155  * @param int|WP_Post $post Post ID or WP_Post object.
    2156  * @return bool Whether the post can be edited in the block editor.
    2157  */
    2158 function use_block_editor_for_post( $post ) {
    2159         $post = get_post( $post );
    2160 
    2161         if ( ! $post ) {
    2162                 return false;
    2163         }
    2164 
    2165         // We're in the meta box loader, so don't use the block editor.
    2166         if ( isset( $_GET['meta-box-loader'] ) ) {
    2167                 check_admin_referer( 'meta-box-loader', 'meta-box-loader-nonce' );
    2168                 return false;
    2169         }
    2170 
    2171         $use_block_editor = use_block_editor_for_post_type( $post->post_type );
    2172 
    2173         /**
    2174          * Filters whether a post is able to be edited in the block editor.
    2175          *
    2176          * @since 5.0.0
    2177          *
    2178          * @param bool    $use_block_editor Whether the post can be edited or not.
    2179          * @param WP_Post $post             The post being checked.
    2180          */
    2181         return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post );
    2182 }
    2183 
    2184 /**
    2185  * Returns whether a post type is compatible with the block editor.
    2186  *
    2187  * The block editor depends on the REST API, and if the post type is not shown in the
    2188  * REST API, then it won't work with the block editor.
    2189  *
    2190  * @since 5.0.0
    2191  *
    2192  * @param string $post_type The post type.
    2193  * @return bool Whether the post type can be edited with the block editor.
    2194  */
    2195 function use_block_editor_for_post_type( $post_type ) {
    2196         if ( ! post_type_exists( $post_type ) ) {
    2197                 return false;
    2198         }
    2199 
    2200         if ( ! post_type_supports( $post_type, 'editor' ) ) {
    2201                 return false;
    2202         }
    2203 
    2204         $post_type_object = get_post_type_object( $post_type );
    2205         if ( $post_type_object && ! $post_type_object->show_in_rest ) {
    2206                 return false;
    2207         }
    2208 
    2209         /**
    2210          * Filters whether a post is able to be edited in the block editor.
    2211          *
    2212          * @since 5.0.0
    2213          *
    2214          * @param bool   $use_block_editor  Whether the post type can be edited or not. Default true.
    2215          * @param string $post_type         The post type being checked.
    2216          */
    2217         return apply_filters( 'use_block_editor_for_post_type', true, $post_type );
    2218 }
    2219 
    2220 /**
    22212151 * Prepares server-registered blocks for the block editor.
    22222152 *
  • trunk/src/wp-includes/post.php

    r53547 r53559  
    81078107        return $previous_status;
    81088108}
     8109
     8110/**
     8111 * Returns whether the post can be edited in the block editor.
     8112 *
     8113 * @since 5.0.0
     8114 * @since 6.1.0 Moved to wp-includes from wp-admin.
     8115 *
     8116 * @param int|WP_Post $post Post ID or WP_Post object.
     8117 * @return bool Whether the post can be edited in the block editor.
     8118 */
     8119function use_block_editor_for_post( $post ) {
     8120        $post = get_post( $post );
     8121
     8122        if ( ! $post ) {
     8123                return false;
     8124        }
     8125
     8126        // We're in the meta box loader, so don't use the block editor.
     8127        if ( is_admin() && isset( $_GET['meta-box-loader'] ) ) {
     8128                check_admin_referer( 'meta-box-loader', 'meta-box-loader-nonce' );
     8129                return false;
     8130        }
     8131
     8132        $use_block_editor = use_block_editor_for_post_type( $post->post_type );
     8133
     8134        /**
     8135         * Filters whether a post is able to be edited in the block editor.
     8136         *
     8137         * @since 5.0.0
     8138         *
     8139         * @param bool    $use_block_editor Whether the post can be edited or not.
     8140         * @param WP_Post $post             The post being checked.
     8141         */
     8142        return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post );
     8143}
     8144
     8145/**
     8146 * Returns whether a post type is compatible with the block editor.
     8147 *
     8148 * The block editor depends on the REST API, and if the post type is not shown in the
     8149 * REST API, then it won't work with the block editor.
     8150 *
     8151 * @since 5.0.0
     8152 * @since 6.1.0 Moved to wp-includes from wp-admin.
     8153 *
     8154 * @param string $post_type The post type.
     8155 * @return bool Whether the post type can be edited with the block editor.
     8156 */
     8157function use_block_editor_for_post_type( $post_type ) {
     8158        if ( ! post_type_exists( $post_type ) ) {
     8159                return false;
     8160        }
     8161
     8162        if ( ! post_type_supports( $post_type, 'editor' ) ) {
     8163                return false;
     8164        }
     8165
     8166        $post_type_object = get_post_type_object( $post_type );
     8167        if ( $post_type_object && ! $post_type_object->show_in_rest ) {
     8168                return false;
     8169        }
     8170
     8171        /**
     8172         * Filters whether a post is able to be edited in the block editor.
     8173         *
     8174         * @since 5.0.0
     8175         *
     8176         * @param bool   $use_block_editor  Whether the post type can be edited or not. Default true.
     8177         * @param string $post_type         The post type being checked.
     8178         */
     8179        return apply_filters( 'use_block_editor_for_post_type', true, $post_type );
     8180}
  • trunk/tests/phpunit/tests/admin/includesPost.php

    r53268 r53559  
    788788
    789789                $this->assertSame( $p, post_exists( $title, $content, $date ) );
    790         }
    791 
    792         public function test_use_block_editor_for_post() {
    793                 $this->assertFalse( use_block_editor_for_post( -1 ) );
    794                 $bogus_post_id = $this->factory()->post->create(
    795                         array(
    796                                 'post_type' => 'bogus',
    797                         )
    798                 );
    799                 $this->assertFalse( use_block_editor_for_post( $bogus_post_id ) );
    800 
    801                 register_post_type(
    802                         'restless',
    803                         array(
    804                                 'show_in_rest' => false,
    805                         )
    806                 );
    807                 $restless_post_id = $this->factory()->post->create(
    808                         array(
    809                                 'post_type' => 'restless',
    810                         )
    811                 );
    812                 $this->assertFalse( use_block_editor_for_post( $restless_post_id ) );
    813 
    814                 $generic_post_id = $this->factory()->post->create();
    815 
    816                 add_filter( 'use_block_editor_for_post', '__return_false' );
    817                 $this->assertFalse( use_block_editor_for_post( $generic_post_id ) );
    818                 remove_filter( 'use_block_editor_for_post', '__return_false' );
    819 
    820                 add_filter( 'use_block_editor_for_post', '__return_true' );
    821                 $this->assertTrue( use_block_editor_for_post( $restless_post_id ) );
    822                 remove_filter( 'use_block_editor_for_post', '__return_true' );
    823790        }
    824791
  • trunk/tests/phpunit/tests/post.php

    r53238 r53559  
    18451845                $this->assertSameSets( array( 1, 2, 2 ), get_option( 'sticky_posts' ) );
    18461846        }
     1847
     1848        /**
     1849         * Check if post supports block editor.
     1850         *
     1851         * @ticket 51819
     1852         * @covers ::use_block_editor_for_post
     1853         */
     1854        public function test_use_block_editor_for_post() {
     1855                $this->assertFalse( use_block_editor_for_post( -1 ) );
     1856                $bogus_post_id = $this->factory()->post->create(
     1857                        array(
     1858                                'post_type' => 'bogus',
     1859                        )
     1860                );
     1861                $this->assertFalse( use_block_editor_for_post( $bogus_post_id ) );
     1862
     1863                register_post_type(
     1864                        'restless',
     1865                        array(
     1866                                'show_in_rest' => false,
     1867                        )
     1868                );
     1869                $restless_post_id = $this->factory()->post->create(
     1870                        array(
     1871                                'post_type' => 'restless',
     1872                        )
     1873                );
     1874                $this->assertFalse( use_block_editor_for_post( $restless_post_id ) );
     1875
     1876                $generic_post_id = $this->factory()->post->create();
     1877
     1878                add_filter( 'use_block_editor_for_post', '__return_false' );
     1879                $this->assertFalse( use_block_editor_for_post( $generic_post_id ) );
     1880                remove_filter( 'use_block_editor_for_post', '__return_false' );
     1881
     1882                add_filter( 'use_block_editor_for_post', '__return_true' );
     1883                $this->assertTrue( use_block_editor_for_post( $restless_post_id ) );
     1884                remove_filter( 'use_block_editor_for_post', '__return_true' );
     1885        }
    18471886}
Note: See TracChangeset for help on using the changeset viewer.