From 0b65b9c6afd1c0c3ef9d8a317bdcd34bf24d00eb Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Wed, 23 Jan 2019 21:51:17 -0500 Subject: [PATCH] Plugin: Deprecate gutenberg_can_edit_* functions --- .../backward-compatibility/deprecations.md | 2 + gutenberg.php | 2 +- lib/register.php | 60 ++------------- phpunit/class-admin-test.php | 74 ------------------- 4 files changed, 9 insertions(+), 129 deletions(-) diff --git a/docs/designers-developers/developers/backward-compatibility/deprecations.md b/docs/designers-developers/developers/backward-compatibility/deprecations.md index f2ce4c1ee8d2f..97dbd13c1c2f1 100644 --- a/docs/designers-developers/developers/backward-compatibility/deprecations.md +++ b/docs/designers-developers/developers/backward-compatibility/deprecations.md @@ -56,6 +56,8 @@ The Gutenberg project's deprecation policy is intended to support backward compa - The PHP function `gutenberg_meta_box_post_form_hidden_fields` has been removed. Use [`the_block_editor_meta_box_post_form_hidden_fields`](https://developer.wordpress.org/reference/functions/the_block_editor_meta_box_post_form_hidden_fields/) instead. - The PHP function `gutenberg_toggle_custom_fields` has been removed. - The PHP function `gutenberg_collect_meta_box_data` has been removed. Use [`register_and_do_post_meta_boxes`](https://developer.wordpress.org/reference/functions/register_and_do_post_meta_boxes/) instead. +- The PHP function `gutenberg_can_edit_post_type` has been removed. Use [`use_block_editor_for_post_type`](https://developer.wordpress.org/reference/functions/use_block_editor_for_post_type/) instead. +- The PHP function `gutenberg_can_edit_post` has been removed. Use [`use_block_editor_for_post`](https://developer.wordpress.org/reference/functions/use_block_editor_for_post/) instead. ## 4.5.0 - `Dropdown.refresh()` has been deprecated as the contained `Popover` is now automatically refreshed. diff --git a/gutenberg.php b/gutenberg.php index f91df4b918597..4fa3862a2adbb 100644 --- a/gutenberg.php +++ b/gutenberg.php @@ -132,7 +132,7 @@ function is_gutenberg_page() { return false; } - if ( ! gutenberg_can_edit_post( $post ) ) { + if ( ! use_block_editor_for_post( $post ) ) { return false; } diff --git a/lib/register.php b/lib/register.php index ebdc43c7f2e5c..0334c2a1cc563 100644 --- a/lib/register.php +++ b/lib/register.php @@ -25,44 +25,15 @@ function gutenberg_collect_meta_box_data() { * Return whether the post can be edited in Gutenberg and by the current user. * * @since 0.5.0 + * @deprecated 5.0.0 use_block_editor_for_post * * @param int|WP_Post $post Post ID or WP_Post object. * @return bool Whether the post can be edited with Gutenberg. */ function gutenberg_can_edit_post( $post ) { - $post = get_post( $post ); - $can_edit = true; + _deprecated_function( __FUNCTION__, '5.0.0', 'use_block_editor_for_post' ); - if ( ! $post ) { - $can_edit = false; - } - - if ( $can_edit && 'trash' === $post->post_status ) { - $can_edit = false; - } - - if ( $can_edit && ! gutenberg_can_edit_post_type( $post->post_type ) ) { - $can_edit = false; - } - - if ( $can_edit && ! current_user_can( 'edit_post', $post->ID ) ) { - $can_edit = false; - } - - // Disable the editor if on the blog page and there is no content. - if ( $can_edit && absint( get_option( 'page_for_posts' ) ) === $post->ID && empty( $post->post_content ) ) { - $can_edit = false; - } - - /** - * Filter to allow plugins to enable/disable Gutenberg for particular post. - * - * @since 3.5 - * - * @param bool $can_edit Whether the post can be edited or not. - * @param WP_Post $post The post being checked. - */ - return apply_filters( 'gutenberg_can_edit_post', $can_edit, $post ); + return use_block_editor_for_post( $post ); } @@ -73,34 +44,15 @@ function gutenberg_can_edit_post( $post ) { * REST API, then the post cannot be edited in Gutenberg. * * @since 1.5.2 + * @deprecated 5.0.0 use_block_editor_for_post_type * * @param string $post_type The post type. * @return bool Whether the post type can be edited with Gutenberg. */ function gutenberg_can_edit_post_type( $post_type ) { - $can_edit = true; - if ( ! post_type_exists( $post_type ) ) { - $can_edit = false; - } - - if ( ! post_type_supports( $post_type, 'editor' ) ) { - $can_edit = false; - } - - $post_type_object = get_post_type_object( $post_type ); - if ( $post_type_object && ! $post_type_object->show_in_rest ) { - $can_edit = false; - } + _deprecated_function( __FUNCTION__, '5.0.0', 'use_block_editor_for_post_type' ); - /** - * Filter to allow plugins to enable/disable Gutenberg for particular post types. - * - * @since 1.5.2 - * - * @param bool $can_edit Whether the post type can be edited or not. - * @param string $post_type The post type being checked. - */ - return apply_filters( 'gutenberg_can_edit_post_type', $can_edit, $post_type ); + return use_block_editor_for_post_type( $post_type ); } /** diff --git a/phpunit/class-admin-test.php b/phpunit/class-admin-test.php index f801df79e4f16..6d52896eb7b73 100644 --- a/phpunit/class-admin-test.php +++ b/phpunit/class-admin-test.php @@ -10,13 +10,6 @@ */ class Admin_Test extends WP_UnitTestCase { - /** - * Editor user ID. - * - * @var int - */ - protected static $editor_user_id; - /** * ID for a post containing blocks. * @@ -35,11 +28,6 @@ class Admin_Test extends WP_UnitTestCase { * Set up before class. */ public static function wpSetUpBeforeClass() { - self::$editor_user_id = self::factory()->user->create( - array( - 'role' => 'editor', - ) - ); self::$post_with_blocks = self::factory()->post->create( array( 'post_title' => 'Example', @@ -54,68 +42,6 @@ public static function wpSetUpBeforeClass() { ); } - /** - * Tests gutenberg_can_edit_post(). - * - * @covers ::gutenberg_can_edit_post - */ - function test_gutenberg_can_edit_post() { - $this->assertFalse( gutenberg_can_edit_post( -1 ) ); - $bogus_post_id = $this->factory()->post->create( - array( - 'post_type' => 'bogus', - ) - ); - $this->assertFalse( gutenberg_can_edit_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( gutenberg_can_edit_post( $restless_post_id ) ); - - $generic_post_id = $this->factory()->post->create(); - - wp_set_current_user( 0 ); - $this->assertFalse( gutenberg_can_edit_post( $generic_post_id ) ); - - wp_set_current_user( self::$editor_user_id ); - $this->assertTrue( gutenberg_can_edit_post( $generic_post_id ) ); - - $blog_page_without_content = self::factory()->post->create( - array( - 'post_title' => 'Blog', - 'post_content' => '', - ) - ); - update_option( 'page_for_posts', $blog_page_without_content ); - $this->assertFalse( gutenberg_can_edit_post( $blog_page_without_content ) ); - - $blog_page_with_content = self::factory()->post->create( - array( - 'post_title' => 'Blog', - 'post_content' => 'Hello World!', - ) - ); - update_option( 'page_for_posts', $blog_page_with_content ); - $this->assertTrue( gutenberg_can_edit_post( $blog_page_with_content ) ); - - add_filter( 'gutenberg_can_edit_post', '__return_false' ); - $this->assertFalse( gutenberg_can_edit_post( $generic_post_id ) ); - remove_filter( 'gutenberg_can_edit_post', '__return_false' ); - - add_filter( 'gutenberg_can_edit_post', '__return_true' ); - $this->assertTrue( gutenberg_can_edit_post( $restless_post_id ) ); - remove_filter( 'gutenberg_can_edit_post', '__return_true' ); - } - /** * Tests gutenberg_post_has_blocks(). *