Skip to content

Commit

Permalink
Plugin: Remove 5.0-merged block registration functions, integrations (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jan 23, 2019
1 parent 60bd2a5 commit b9e7686
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 570 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

The Gutenberg project's deprecation policy is intended to support backward compatibility for releases, when possible. The current deprecations are listed below and are grouped by _the version at which they will be removed completely_. If your plugin depends on these behaviors, you must update to the recommended alternative before the noted version.

## 5.2.0

- The PHP function `gutenberg_parse_blocks` has been removed. Use [`parse_blocks`](https://developer.wordpress.org/reference/functions/parse_blocks/) instead.
- The PHP function `get_dynamic_blocks_regex` has been removed.
- The PHP function `gutenberg_render_block` has been removed. Use [`render_block`](https://developer.wordpress.org/reference/functions/render_block/) instead.
- The PHP function `strip_dynamic_blocks` has been removed. For use in excerpt preparation, consider [`excerpt_remove_blocks`](https://developer.wordpress.org/reference/functions/excerpt_remove_blocks/) instead.
- The PHP function `strip_dynamic_blocks_add_filter` has been removed.
- The PHP function `strip_dynamic_blocks_remove_filter` has been removed.
- The PHP function `gutenberg_post_has_blocks` has been removed. Use [`has_blocks`](https://developer.wordpress.org/reference/functions/has_blocks/) instead.
- The PHP function `gutenberg_content_has_blocks` has been removed. Use [`has_blocks`](https://developer.wordpress.org/reference/functions/has_blocks/) instead.

## 4.5.0
- `Dropdown.refresh()` has been deprecated as the contained `Popover` is now automatically refreshed.
- `wp.editor.PostPublishPanelToggle` has been deprecated in favor of `wp.editor.PostPublishButton`.
Expand Down
165 changes: 18 additions & 147 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,89 +9,20 @@
die( 'Silence is golden.' );
}

if ( ! function_exists( 'register_block_type' ) ) {
/**
* Registers a block type.
*
* @since 0.1.0
* @since 0.6.0 Now also accepts a WP_Block_Type instance as first parameter.
*
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
* complete WP_Block_Type instance. In case a WP_Block_Type
* is provided, the $args parameter will be ignored.
* @param array $args {
* Optional. Array of block type arguments. Any arguments may be defined, however the
* ones described below are supported by default. Default empty array.
*
* @type callable $render_callback Callback used to render blocks of this block type.
* }
* @return WP_Block_Type|false The registered block type on success, or false on failure.
*/
function register_block_type( $name, $args = array() ) {
return WP_Block_Type_Registry::get_instance()->register( $name, $args );
}
}

if ( ! function_exists( 'unregister_block_type' ) ) {
/**
* Unregisters a block type.
*
* @since 0.1.0
* @since 0.6.0 Now also accepts a WP_Block_Type instance as first parameter.
*
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
* complete WP_Block_Type instance.
* @return WP_Block_Type|false The unregistered block type on success, or false on failure.
*/
function unregister_block_type( $name ) {
return WP_Block_Type_Registry::get_instance()->unregister( $name );
}
}

if ( ! function_exists( 'gutenberg_parse_blocks' ) ) {
/**
* Parses blocks out of a content string.
*
* @since 0.5.0
* @deprecated 5.0.0 parse_blocks()
*
* @param string $content Post content.
* @return array Array of parsed block objects.
*/
function gutenberg_parse_blocks( $content ) {
/**
* Filter to allow plugins to replace the server-side block parser
*
* @since 3.8.0
*
* @param string $parser_class Name of block parser class
*/
$parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' );
// Load default block parser for server-side parsing if the default parser class is being used.
if ( 'WP_Block_Parser' === $parser_class ) {
require_once dirname( __FILE__ ) . '/../packages/block-serialization-default-parser/parser.php';
}
$parser = new $parser_class();
return $parser->parse( $content );
}
}

if ( ! function_exists( 'get_dynamic_block_names' ) ) {
/**
* Returns an array of the names of all registered dynamic block types.
*
* @return array Array of dynamic block names.
*/
function get_dynamic_block_names() {
$dynamic_block_names = array();
_deprecated_function( __FUNCTION__, '5.0.0', 'parse_blocks()' );

$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
foreach ( $block_types as $block_type ) {
if ( $block_type->is_dynamic() ) {
$dynamic_block_names[] = $block_type->name;
}
}

return $dynamic_block_names;
return parse_blocks( $content );
}
}

Expand All @@ -100,10 +31,13 @@ function get_dynamic_block_names() {
* Retrieve the dynamic blocks regular expression for searching.
*
* @since 3.6.0
* @deprecated 5.0.0
*
* @return string
*/
function get_dynamic_blocks_regex() {
_deprecated_function( __FUNCTION__, '5.0.0' );

$dynamic_block_names = get_dynamic_block_names();
$dynamic_block_pattern = (
'/<!--\s+wp:(' .
Expand Down Expand Up @@ -135,97 +69,30 @@ function get_dynamic_blocks_regex() {
* @since 1.9.0
* @since 4.4.0 renders full nested tree of blocks before reassembling into HTML string
* @global WP_Post $post The post to edit.
* @deprecated 5.0.0 render_block()
*
* @param array $block A single parsed block object.
* @return string String of rendered HTML.
*/
function gutenberg_render_block( $block ) {
global $post;

$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
$is_dynamic = $block['blockName'] && null !== $block_type && $block_type->is_dynamic();
$inner_content = '';
$index = 0;

foreach ( $block['innerContent'] as $chunk ) {
$inner_content .= is_string( $chunk ) ? $chunk : gutenberg_render_block( $block['innerBlocks'][ $index++ ] );
}

if ( $is_dynamic ) {
$attributes = is_array( $block['attrs'] ) ? (array) $block['attrs'] : array();
$global_post = $post;
$output = $block_type->render( $attributes, $inner_content );
$post = $global_post;
_deprecated_function( __FUNCTION__, '5.0.0', 'render_block()' );

return $output;
}

return $inner_content;
}

if ( ! function_exists( 'do_blocks' ) ) {
/**
* Parses dynamic blocks out of `post_content` and re-renders them.
*
* @since 0.1.0
* @since 4.4.0 performs full parse on input post content
*
* @param string $content Post content.
* @return string Updated post content.
*/
function do_blocks( $content ) {
// If there are blocks in this content, we shouldn't run wpautop() on it later.
$priority = has_filter( 'the_content', 'wpautop' );
if ( false !== $priority && doing_filter( 'the_content' ) && has_blocks( $content ) ) {
remove_filter( 'the_content', 'wpautop', $priority );
add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 );
}

$blocks = gutenberg_parse_blocks( $content );
$output = '';

foreach ( $blocks as $block ) {
$output .= gutenberg_render_block( $block );
}

return $output;
}

add_filter( 'the_content', 'do_blocks', 7 ); // BEFORE do_shortcode() and oembed.
}

if ( ! function_exists( '_restore_wpautop_hook' ) ) {
/**
* If do_blocks() needs to remove wpautop() from the `the_content` filter,
* this re-adds it afterwards, for subsequent `the_content` usage.
*
* @access private
*
* @since 4.6.0
*
* @param string $content The post content running through this filter.
* @return string The unmodified content.
*/
function _restore_wpautop_hook( $content ) {
$current_priority = has_filter( 'the_content', '_restore_wpautop_hook' );

add_filter( 'the_content', 'wpautop', $current_priority - 1 );
remove_filter( 'the_content', '_restore_wpautop_hook', $current_priority );

return $content;
}
return render_block( $block );
}

if ( ! function_exists( 'strip_dynamic_blocks' ) ) {
/**
* Remove all dynamic blocks from the given content.
*
* @since 3.6.0
* @deprecated 5.0.0
*
* @param string $content Content of the current post.
* @return string
*/
function strip_dynamic_blocks( $content ) {
_deprecated_function( __FUNCTION__, '5.0.0' );

return preg_replace( get_dynamic_blocks_regex(), '', $content );
}
}
Expand All @@ -238,16 +105,18 @@ function strip_dynamic_blocks( $content ) {
* can just be called in `wp_trim_excerpt()`.
*
* @since 3.6.0
* @deprecated 5.0.0
*
* @param string $text Excerpt.
* @return string
*/
function strip_dynamic_blocks_add_filter( $text ) {
_deprecated_function( __FUNCTION__, '5.0.0' );

add_filter( 'the_content', 'strip_dynamic_blocks', 6 );

return $text;
}
add_filter( 'get_the_excerpt', 'strip_dynamic_blocks_add_filter', 9 ); // Before wp_trim_excerpt().
}

if ( ! function_exists( 'strip_dynamic_blocks_remove_filter' ) ) {
Expand All @@ -258,14 +127,16 @@ function strip_dynamic_blocks_add_filter( $text ) {
* can just be called in `wp_trim_excerpt()`.
*
* @since 3.6.0
* @deprecated 5.0.0
*
* @param string $text Excerpt.
* @return string
*/
function strip_dynamic_blocks_remove_filter( $text ) {
_deprecated_function( __FUNCTION__, '5.0.0' );

remove_filter( 'the_content', 'strip_dynamic_blocks', 6 );

return $text;
}
add_filter( 'wp_trim_excerpt', 'strip_dynamic_blocks_remove_filter', 0 ); // Before all other.
}
Loading

0 comments on commit b9e7686

Please sign in to comment.