Make WordPress Core


Ignore:
Timestamp:
06/23/2022 06:46:18 PM (2 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-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}
Note: See TracChangeset for help on using the changeset viewer.