Make WordPress Core


Ignore:
Timestamp:
06/23/2022 06:46:18 PM (3 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.

File:
1 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 *
Note: See TracChangeset for help on using the changeset viewer.