| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions related to editor blocks for the Gutenberg editor plugin. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
die( 'Silence is golden.' ); |
| 10 |
} |
| 11 |
|
| 12 |
if ( ! function_exists( 'gutenberg_parse_blocks' ) ) { |
| 13 |
/** |
| 14 |
* Parses blocks out of a content string. |
| 15 |
* |
| 16 |
* @since 0.5.0 |
| 17 |
* @deprecated 5.0.0 parse_blocks() |
| 18 |
* |
| 19 |
* @param string $content Post content. |
| 20 |
* @return array Array of parsed block objects. |
| 21 |
*/ |
| 22 |
function gutenberg_parse_blocks( $content ) { |
| 23 |
_deprecated_function( __FUNCTION__, '5.0.0', 'parse_blocks()' ); |
| 24 |
|
| 25 |
return parse_blocks( $content ); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
if ( ! function_exists( 'get_dynamic_blocks_regex' ) ) { |
| 30 |
/** |
| 31 |
* Retrieve the dynamic blocks regular expression for searching. |
| 32 |
* |
| 33 |
* @since 3.6.0 |
| 34 |
* @deprecated 5.0.0 |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
function get_dynamic_blocks_regex() { |
| 39 |
_deprecated_function( __FUNCTION__, '5.0.0' ); |
| 40 |
|
| 41 |
$dynamic_block_names = get_dynamic_block_names(); |
| 42 |
$dynamic_block_pattern = ( |
| 43 |
'/<!--\s+wp:(' . |
| 44 |
str_replace( |
| 45 |
'/', |
| 46 |
'\/', // Escape namespace, not handled by preg_quote. |
| 47 |
str_replace( |
| 48 |
'core/', |
| 49 |
'(?:core/)?', // Allow implicit core namespace, but don't capture. |
| 50 |
implode( |
| 51 |
'|', // Join block names into capture group alternation. |
| 52 |
array_map( |
| 53 |
'preg_quote', // Escape block name for regular expression. |
| 54 |
$dynamic_block_names |
| 55 |
) |
| 56 |
) |
| 57 |
) |
| 58 |
) . |
| 59 |
')(\s+(\{.*?\}))?\s+(\/)?-->/' |
| 60 |
); |
| 61 |
|
| 62 |
return $dynamic_block_pattern; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Renders a single block into a HTML string. |
| 68 |
* |
| 69 |
* @since 1.9.0 |
| 70 |
* @since 4.4.0 renders full nested tree of blocks before reassembling into HTML string |
| 71 |
* @global WP_Post $post The post to edit. |
| 72 |
* @deprecated 5.0.0 render_block() |
| 73 |
* |
| 74 |
* @param array $block A single parsed block object. |
| 75 |
* @return string String of rendered HTML. |
| 76 |
*/ |
| 77 |
function gutenberg_render_block( $block ) { |
| 78 |
_deprecated_function( __FUNCTION__, '5.0.0', 'render_block()' ); |
| 79 |
|
| 80 |
return render_block( $block ); |
| 81 |
} |
| 82 |
|
| 83 |
if ( ! function_exists( 'strip_dynamic_blocks' ) ) { |
| 84 |
/** |
| 85 |
* Remove all dynamic blocks from the given content. |
| 86 |
* |
| 87 |
* @since 3.6.0 |
| 88 |
* @deprecated 5.0.0 |
| 89 |
* |
| 90 |
* @param string $content Content of the current post. |
| 91 |
* @return string |
| 92 |
*/ |
| 93 |
function strip_dynamic_blocks( $content ) { |
| 94 |
_deprecated_function( __FUNCTION__, '5.0.0' ); |
| 95 |
|
| 96 |
return preg_replace( get_dynamic_blocks_regex(), '', $content ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if ( ! function_exists( 'strip_dynamic_blocks_add_filter' ) ) { |
| 101 |
/** |
| 102 |
* Adds the content filter to strip dynamic blocks from excerpts. |
| 103 |
* |
| 104 |
* It's a bit hacky for now, but once this gets merged into core the function |
| 105 |
* can just be called in `wp_trim_excerpt()`. |
| 106 |
* |
| 107 |
* @since 3.6.0 |
| 108 |
* @deprecated 5.0.0 |
| 109 |
* |
| 110 |
* @param string $text Excerpt. |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
function strip_dynamic_blocks_add_filter( $text ) { |
| 114 |
_deprecated_function( __FUNCTION__, '5.0.0' ); |
| 115 |
|
| 116 |
add_filter( 'the_content', 'strip_dynamic_blocks', 6 ); |
| 117 |
|
| 118 |
return $text; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
if ( ! function_exists( 'strip_dynamic_blocks_remove_filter' ) ) { |
| 123 |
/** |
| 124 |
* Removes the content filter to strip dynamic blocks from excerpts. |
| 125 |
* |
| 126 |
* It's a bit hacky for now, but once this gets merged into core the function |
| 127 |
* can just be called in `wp_trim_excerpt()`. |
| 128 |
* |
| 129 |
* @since 3.6.0 |
| 130 |
* @deprecated 5.0.0 |
| 131 |
* |
| 132 |
* @param string $text Excerpt. |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
function strip_dynamic_blocks_remove_filter( $text ) { |
| 136 |
_deprecated_function( __FUNCTION__, '5.0.0' ); |
| 137 |
|
| 138 |
remove_filter( 'the_content', 'strip_dynamic_blocks', 6 ); |
| 139 |
|
| 140 |
return $text; |
| 141 |
} |
| 142 |
} |
| 143 |
|