Skip to content

Instantly share code, notes, and snippets.

@deckerweb
Last active November 8, 2019 10:51
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save deckerweb/32db10fcd1735e84023f265861e91eca to your computer and use it in GitHub Desktop.
Adds optional new post states if a Post has Blocks, has the "Classic" Block or no Blocks at all. *** Note: This respects also the popular "Disable Gutenberg" plugin (free, by Jeff Starr) which allows for disabling the "Block Editor" for entire post types.
<?php
/** Do NOT include the opening php tag */
if ( ! function_exists( 'ddw_cf_maybe_add_post_state_blocks' ) ) :
add_filter( 'display_post_states', 'ddw_cf_maybe_add_post_state_blocks', 10, 2 );
/**
* Adds optional new post states if a Post has Blocks, has the "Classic" Block or no Blocks at all.
*
* Note: This respects also the popular "Disable Gutenberg" plugin (free, by Jeff Starr) which allows for disabling the "Block Editor" for entire post types.
*
* @link https://github.com/WordPress/gutenberg/issues/12862
* @link https://www.billerickson.net/access-gutenberg-block-data/
* @link https://florianbrinkmann.com/bestimmte-gutenberg-bloecke-eines-beitrags-im-theme-an-anderer-stelle-ausgeben-6602/
*
* @since 1.0.0
*
* @author David Decker - DECKERWEB
* @link https://gist.github.com/deckerweb/32db10fcd1735e84023f265861e91eca
*
* @uses has_blocks()
* @uses parse_blocks()
* @uses disable_gutenberg_disable_post_type() // from "Disable Gutenberg" plugin!
*
* @param array $post_states Array holding all post states.
* @param object $post Object of the current post type item.
* @return array Modified array of post states.
*/
function ddw_cf_maybe_add_post_state_blocks( $post_states, $post ) {
/** Bail early if not on WP 5.0+ */
if ( ! function_exists( 'parse_blocks' ) || ! function_exists( 'has_blocks' ) ) {
return $post_states;
}
/** Initiate the Block parser */
$blocks = parse_blocks( $post->post_content );
/** 1) Has some kind of block(s) */
if ( $blocks && ! ( function_exists( 'disable_gutenberg_disable_post_type' ) && disable_gutenberg_disable_post_type() ) ) {
if ( has_blocks( $post->post_content ) && NULL !== $blocks[0][ 'blockName' ] ) {
/**
* 1st case: a post item with regular blocks (no "Classic" block!).
*/
$post_states[ 'ddwcf_has_blocks' ] = sprintf(
'<span style="color: %2$s;">%1$s</span>',
__( 'Has Blocks', 'Label for Post State', 'ddw-core-functionality' ),
sanitize_hex_color( '#555' )
);
} elseif ( ! has_blocks( $post->post_content ) && ( NULL === $blocks[0][ 'blockName' ] || ! empty( $blocks[0][ 'blockName' ] ) ) ) {
/**
* 2nd case: a post item containing "Classic" block at the first position - usually a great sign for a post that can be transformed into regular Blocks.
*/
$post_states[ 'ddwcf_has_block_classic' ] = sprintf(
'<span style="color: %2$s;">%1$s</span>',
__( 'Classic Block', 'Label for Post State', 'ddw-core-functionality' ),
sanitize_hex_color( '#00c' )
);
} // end if/elseif
}
/** 2) Has really NO blocks at all */
else {
/**
* 3rd case: a post item with NO regular blocks and NO "Classic" block in it.
*/
$post_states[ 'ddwcf_has_no_blocks' ] = sprintf(
'<span style="color: %2$s;">%1$s</span>',
__( 'No Blocks', 'Label for Post State', 'ddw-core-functionality' ),
sanitize_hex_color( '#c00' )
);
} // end if/else
/** Return modified Post States */
return $post_states;
} // end function
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment