diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index a4dd6a0519..51af5bb2f2 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -2147,76 +2147,6 @@ function taxonomy_meta_box_sanitize_cb_input( $taxonomy, $terms ) { return $clean_terms; } -/** - * Returns whether the post can be edited in the block editor. - * - * @since 5.0.0 - * - * @param int|WP_Post $post Post ID or WP_Post object. - * @return bool Whether the post can be edited in the block editor. - */ -function use_block_editor_for_post( $post ) { - $post = get_post( $post ); - - if ( ! $post ) { - return false; - } - - // We're in the meta box loader, so don't use the block editor. - if ( isset( $_GET['meta-box-loader'] ) ) { - check_admin_referer( 'meta-box-loader', 'meta-box-loader-nonce' ); - return false; - } - - $use_block_editor = use_block_editor_for_post_type( $post->post_type ); - - /** - * Filters whether a post is able to be edited in the block editor. - * - * @since 5.0.0 - * - * @param bool $use_block_editor Whether the post can be edited or not. - * @param WP_Post $post The post being checked. - */ - return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post ); -} - -/** - * Returns whether a post type is compatible with the block editor. - * - * The block editor depends on the REST API, and if the post type is not shown in the - * REST API, then it won't work with the block editor. - * - * @since 5.0.0 - * - * @param string $post_type The post type. - * @return bool Whether the post type can be edited with the block editor. - */ -function use_block_editor_for_post_type( $post_type ) { - if ( ! post_type_exists( $post_type ) ) { - return false; - } - - if ( ! post_type_supports( $post_type, 'editor' ) ) { - return false; - } - - $post_type_object = get_post_type_object( $post_type ); - if ( $post_type_object && ! $post_type_object->show_in_rest ) { - return false; - } - - /** - * Filters whether a post is able to be edited in the block editor. - * - * @since 5.0.0 - * - * @param bool $use_block_editor Whether the post type can be edited or not. Default true. - * @param string $post_type The post type being checked. - */ - return apply_filters( 'use_block_editor_for_post_type', true, $post_type ); -} - /** * Prepares server-registered blocks for the block editor. * diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 23719feadd..05b97d699f 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -8106,3 +8106,75 @@ function wp_get_original_image_url( $attachment_id ) { function wp_untrash_post_set_previous_status( $new_status, $post_id, $previous_status ) { return $previous_status; } + +/** + * Returns whether the post can be edited in the block editor. + * + * @since 5.0.0 + * @since 6.1.0 Moved to wp-includes from wp-admin. + * + * @param int|WP_Post $post Post ID or WP_Post object. + * @return bool Whether the post can be edited in the block editor. + */ +function use_block_editor_for_post( $post ) { + $post = get_post( $post ); + + if ( ! $post ) { + return false; + } + + // We're in the meta box loader, so don't use the block editor. + if ( is_admin() && isset( $_GET['meta-box-loader'] ) ) { + check_admin_referer( 'meta-box-loader', 'meta-box-loader-nonce' ); + return false; + } + + $use_block_editor = use_block_editor_for_post_type( $post->post_type ); + + /** + * Filters whether a post is able to be edited in the block editor. + * + * @since 5.0.0 + * + * @param bool $use_block_editor Whether the post can be edited or not. + * @param WP_Post $post The post being checked. + */ + return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post ); +} + +/** + * Returns whether a post type is compatible with the block editor. + * + * The block editor depends on the REST API, and if the post type is not shown in the + * REST API, then it won't work with the block editor. + * + * @since 5.0.0 + * @since 6.1.0 Moved to wp-includes from wp-admin. + * + * @param string $post_type The post type. + * @return bool Whether the post type can be edited with the block editor. + */ +function use_block_editor_for_post_type( $post_type ) { + if ( ! post_type_exists( $post_type ) ) { + return false; + } + + if ( ! post_type_supports( $post_type, 'editor' ) ) { + return false; + } + + $post_type_object = get_post_type_object( $post_type ); + if ( $post_type_object && ! $post_type_object->show_in_rest ) { + return false; + } + + /** + * Filters whether a post is able to be edited in the block editor. + * + * @since 5.0.0 + * + * @param bool $use_block_editor Whether the post type can be edited or not. Default true. + * @param string $post_type The post type being checked. + */ + return apply_filters( 'use_block_editor_for_post_type', true, $post_type ); +} diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index d27f68255e..d6ae09a302 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -789,39 +789,6 @@ class Tests_Admin_IncludesPost extends WP_UnitTestCase { $this->assertSame( $p, post_exists( $title, $content, $date ) ); } - public function test_use_block_editor_for_post() { - $this->assertFalse( use_block_editor_for_post( -1 ) ); - $bogus_post_id = $this->factory()->post->create( - array( - 'post_type' => 'bogus', - ) - ); - $this->assertFalse( use_block_editor_for_post( $bogus_post_id ) ); - - register_post_type( - 'restless', - array( - 'show_in_rest' => false, - ) - ); - $restless_post_id = $this->factory()->post->create( - array( - 'post_type' => 'restless', - ) - ); - $this->assertFalse( use_block_editor_for_post( $restless_post_id ) ); - - $generic_post_id = $this->factory()->post->create(); - - add_filter( 'use_block_editor_for_post', '__return_false' ); - $this->assertFalse( use_block_editor_for_post( $generic_post_id ) ); - remove_filter( 'use_block_editor_for_post', '__return_false' ); - - add_filter( 'use_block_editor_for_post', '__return_true' ); - $this->assertTrue( use_block_editor_for_post( $restless_post_id ) ); - remove_filter( 'use_block_editor_for_post', '__return_true' ); - } - public function test_get_block_editor_server_block_settings() { $name = 'core/test'; $settings = array( diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 8180a867dc..108e6ef4a6 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -1844,4 +1844,43 @@ class Tests_Post extends WP_UnitTestCase { unstick_post( 3 ); $this->assertSameSets( array( 1, 2, 2 ), get_option( 'sticky_posts' ) ); } + + /** + * Check if post supports block editor. + * + * @ticket 51819 + * @covers ::use_block_editor_for_post + */ + public function test_use_block_editor_for_post() { + $this->assertFalse( use_block_editor_for_post( -1 ) ); + $bogus_post_id = $this->factory()->post->create( + array( + 'post_type' => 'bogus', + ) + ); + $this->assertFalse( use_block_editor_for_post( $bogus_post_id ) ); + + register_post_type( + 'restless', + array( + 'show_in_rest' => false, + ) + ); + $restless_post_id = $this->factory()->post->create( + array( + 'post_type' => 'restless', + ) + ); + $this->assertFalse( use_block_editor_for_post( $restless_post_id ) ); + + $generic_post_id = $this->factory()->post->create(); + + add_filter( 'use_block_editor_for_post', '__return_false' ); + $this->assertFalse( use_block_editor_for_post( $generic_post_id ) ); + remove_filter( 'use_block_editor_for_post', '__return_false' ); + + add_filter( 'use_block_editor_for_post', '__return_true' ); + $this->assertTrue( use_block_editor_for_post( $restless_post_id ) ); + remove_filter( 'use_block_editor_for_post', '__return_true' ); + } }