Skip to content

Commit

Permalink
Respect Posts Page (#8259)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
wpscholar authored Jul 31, 2018
1 parent 176bb82 commit 54990c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 14 additions & 0 deletions phpunit/class-admin-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}

/**
Expand Down

0 comments on commit 54990c0

Please sign in to comment.