From 54990c0f77294e6603cc4e0aa957c0679c1bf242 Mon Sep 17 00:00:00 2001 From: Micah Wood Date: Tue, 31 Jul 2018 13:08:23 -0400 Subject: [PATCH] Respect Posts Page (#8259) * Ensure that Gutenberg is disabled when on the assigned blog page in WordPress (as per similar functionality from edit-form-advanced.php). * Move code into the gutenberg_can_edit_post() function. * Use Yoda conditional. Use strict equality, which requires casting the `page_for_posts` option as an integer. Added an inline comment. * Added unit tests to ensure gutenberg_can_edit_post() returns true when the posts page has content and false when the posts page has no content. --- lib/register.php | 5 +++++ phpunit/class-admin-test.php | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/register.php b/lib/register.php index d52722aba1aa52..058cdd831f2622 100644 --- a/lib/register.php +++ b/lib/register.php @@ -269,6 +269,11 @@ function gutenberg_can_edit_post( $post ) { return false; } + // Disable the editor if on the blog page and there is no content. + if ( absint( get_option( 'page_for_posts' ) ) === $post->ID && empty( $post->post_content ) ) { + return false; + } + if ( ! gutenberg_can_edit_post_type( $post->post_type ) ) { return false; } diff --git a/phpunit/class-admin-test.php b/phpunit/class-admin-test.php index 12b718178dae87..06fb99a0eff795 100644 --- a/phpunit/class-admin-test.php +++ b/phpunit/class-admin-test.php @@ -75,6 +75,20 @@ function test_gutenberg_can_edit_post() { 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 ) ); } /**