Skip to content

Commit

Permalink
Admin: Show post display state for Gutenberg posts
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Sep 1, 2017
1 parent 65f752e commit 547d038
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,19 @@ function gutenberg_post_has_blocks( $post_id ) {
$post = get_post( $post_id );
return $post && strpos( $post->post_content, '<!-- wp:' ) !== false;
}

/**
* Adds a "Gutenberg" post state for post tables, if the post contains blocks.
*
* @param array $post_states An array of post display states.
* @param WP_Post $post The current post object.
* @return array A filtered array of post display states.
*/
function gutenberg_add_gutenberg_post_state( $post_states, $post ) {
if ( gutenberg_post_has_blocks( $post->ID ) ) {
$post_states[] = 'Gutenberg';
}

return $post_states;
}
add_filter( 'display_post_states', 'gutenberg_add_gutenberg_post_state', 10, 2 );
15 changes: 15 additions & 0 deletions phpunit/class-admin-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,19 @@ function test_gutenberg_post_has_blocks() {
$this->assertTrue( gutenberg_post_has_blocks( self::$post_with_blocks ) );
$this->assertFalse( gutenberg_post_has_blocks( self::$post_without_blocks ) );
}

/**
* Tests gutenberg_add_gutenberg_post_state().
*
* @covers gutenberg_add_gutenberg_post_state
*/
function test_add_gutenberg_post_state() {
// With blocks.
$post_states = apply_filters( 'display_post_states', array(), get_post( self::$post_with_blocks ) );
$this->assertEquals( array( 'Gutenberg' ), $post_states );

// Without blocks.
$post_states = apply_filters( 'display_post_states', array(), get_post( self::$post_without_blocks ) );
$this->assertEquals( array(), $post_states );
}
}

0 comments on commit 547d038

Please sign in to comment.