| 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( 'register_block_type' ) ) { |
| 13 |
/** |
| 14 |
* Registers a block type. |
| 15 |
* |
| 16 |
* @since 0.1.0 |
| 17 |
* @since 0.6.0 Now also accepts a WP_Block_Type instance as first parameter. |
| 18 |
* |
| 19 |
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively a |
| 20 |
* complete WP_Block_Type instance. In case a WP_Block_Type |
| 21 |
* is provided, the $args parameter will be ignored. |
| 22 |
* @param array $args { |
| 23 |
* Optional. Array of block type arguments. Any arguments may be defined, however the |
| 24 |
* ones described below are supported by default. Default empty array. |
| 25 |
* |
| 26 |
* @type callable $render_callback Callback used to render blocks of this block type. |
| 27 |
* } |
| 28 |
* @return WP_Block_Type|false The registered block type on success, or false on failure. |
| 29 |
*/ |
| 30 |
function register_block_type( $name, $args = array() ) { |
| 31 |
return WP_Block_Type_Registry::get_instance()->register( $name, $args ); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
if ( ! function_exists( 'unregister_block_type' ) ) { |
| 36 |
/** |
| 37 |
* Unregisters a block type. |
| 38 |
* |
| 39 |
* @since 0.1.0 |
| 40 |
* @since 0.6.0 Now also accepts a WP_Block_Type instance as first parameter. |
| 41 |
* |
| 42 |
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively a |
| 43 |
* complete WP_Block_Type instance. |
| 44 |
* @return WP_Block_Type|false The unregistered block type on success, or false on failure. |
| 45 |
*/ |
| 46 |
function unregister_block_type( $name ) { |
| 47 |
return WP_Block_Type_Registry::get_instance()->unregister( $name ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! function_exists( 'gutenberg_parse_blocks' ) ) { |
| 52 |
/** |
| 53 |
* Parses blocks out of a content string. |
| 54 |
* |
| 55 |
* @since 0.5.0 |
| 56 |
* |
| 57 |
* @param string $content Post content. |
| 58 |
* @return array Array of parsed block objects. |
| 59 |
*/ |
| 60 |
function gutenberg_parse_blocks( $content ) { |
| 61 |
/** |
| 62 |
* Filter to allow plugins to replace the server-side block parser |
| 63 |
* |
| 64 |
* @since 3.8.0 |
| 65 |
* |
| 66 |
* @param string $parser_class Name of block parser class |
| 67 |
*/ |
| 68 |
$parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' ); |
| 69 |
// Load default block parser for server-side parsing if the default parser class is being used. |
| 70 |
if ( 'WP_Block_Parser' === $parser_class ) { |
| 71 |
require_once dirname( __FILE__ ) . '/../packages/block-serialization-default-parser/parser.php'; |
| 72 |
} |
| 73 |
$parser = new $parser_class(); |
| 74 |
return $parser->parse( $content ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if ( ! function_exists( 'get_dynamic_block_names' ) ) { |
| 79 |
/** |
| 80 |
* Returns an array of the names of all registered dynamic block types. |
| 81 |
* |
| 82 |
* @return array Array of dynamic block names. |
| 83 |
*/ |
| 84 |
function get_dynamic_block_names() { |
| 85 |
$dynamic_block_names = array(); |
| 86 |
|
| 87 |
$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered(); |
| 88 |
foreach ( $block_types as $block_type ) { |
| 89 |
if ( $block_type->is_dynamic() ) { |
| 90 |
$dynamic_block_names[] = $block_type->name; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return $dynamic_block_names; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! function_exists( 'get_dynamic_blocks_regex' ) ) { |
| 99 |
/** |
| 100 |
* Retrieve the dynamic blocks regular expression for searching. |
| 101 |
* |
| 102 |
* @since 3.6.0 |
| 103 |
* |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
function get_dynamic_blocks_regex() { |
| 107 |
$dynamic_block_names = get_dynamic_block_names(); |
| 108 |
$dynamic_block_pattern = ( |
| 109 |
'/<!--\s+wp:(' . |
| 110 |
str_replace( |
| 111 |
'/', |
| 112 |
'\/', // Escape namespace, not handled by preg_quote. |
| 113 |
str_replace( |
| 114 |
'core/', |
| 115 |
'(?:core/)?', // Allow implicit core namespace, but don't capture. |
| 116 |
implode( |
| 117 |
'|', // Join block names into capture group alternation. |
| 118 |
array_map( |
| 119 |
'preg_quote', // Escape block name for regular expression. |
| 120 |
$dynamic_block_names |
| 121 |
) |
| 122 |
) |
| 123 |
) |
| 124 |
) . |
| 125 |
')(\s+(\{.*?\}))?\s+(\/)?-->/' |
| 126 |
); |
| 127 |
|
| 128 |
return $dynamic_block_pattern; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Renders a single block into a HTML string. |
| 134 |
* |
| 135 |
* @since 1.9.0 |
| 136 |
* @since 4.4.0 renders full nested tree of blocks before reassembling into HTML string |
| 137 |
* @global WP_Post $post The post to edit. |
| 138 |
* |
| 139 |
* @param array $block A single parsed block object. |
| 140 |
* @return string String of rendered HTML. |
| 141 |
*/ |
| 142 |
function gutenberg_render_block( $block ) { |
| 143 |
global $post; |
| 144 |
|
| 145 |
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
| 146 |
$is_dynamic = $block['blockName'] && null !== $block_type && $block_type->is_dynamic(); |
| 147 |
$inner_content = ''; |
| 148 |
$index = 0; |
| 149 |
|
| 150 |
foreach ( $block['innerContent'] as $chunk ) { |
| 151 |
$inner_content .= is_string( $chunk ) ? $chunk : gutenberg_render_block( $block['innerBlocks'][ $index++ ] ); |
| 152 |
} |
| 153 |
|
| 154 |
if ( $is_dynamic ) { |
| 155 |
$attributes = is_array( $block['attrs'] ) ? (array) $block['attrs'] : array(); |
| 156 |
$global_post = $post; |
| 157 |
$output = $block_type->render( $attributes, $inner_content ); |
| 158 |
$post = $global_post; |
| 159 |
|
| 160 |
return $output; |
| 161 |
} |
| 162 |
|
| 163 |
return $inner_content; |
| 164 |
} |
| 165 |
|
| 166 |
if ( ! function_exists( 'do_blocks' ) ) { |
| 167 |
/** |
| 168 |
* Parses dynamic blocks out of `post_content` and re-renders them. |
| 169 |
* |
| 170 |
* @since 0.1.0 |
| 171 |
* @since 4.4.0 performs full parse on input post content |
| 172 |
* |
| 173 |
* @param string $content Post content. |
| 174 |
* @return string Updated post content. |
| 175 |
*/ |
| 176 |
function do_blocks( $content ) { |
| 177 |
$blocks = gutenberg_parse_blocks( $content ); |
| 178 |
$output = ''; |
| 179 |
|
| 180 |
foreach ( $blocks as $block ) { |
| 181 |
$output .= gutenberg_render_block( $block ); |
| 182 |
} |
| 183 |
|
| 184 |
return $output; |
| 185 |
} |
| 186 |
|
| 187 |
add_filter( 'the_content', 'do_blocks', 7 ); // BEFORE do_shortcode() and oembed. |
| 188 |
} |
| 189 |
|
| 190 |
if ( ! function_exists( 'strip_dynamic_blocks' ) ) { |
| 191 |
/** |
| 192 |
* Remove all dynamic blocks from the given content. |
| 193 |
* |
| 194 |
* @since 3.6.0 |
| 195 |
* |
| 196 |
* @param string $content Content of the current post. |
| 197 |
* @return string |
| 198 |
*/ |
| 199 |
function strip_dynamic_blocks( $content ) { |
| 200 |
return preg_replace( get_dynamic_blocks_regex(), '', $content ); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
if ( ! function_exists( 'strip_dynamic_blocks_add_filter' ) ) { |
| 205 |
/** |
| 206 |
* Adds the content filter to strip dynamic blocks from excerpts. |
| 207 |
* |
| 208 |
* It's a bit hacky for now, but once this gets merged into core the function |
| 209 |
* can just be called in `wp_trim_excerpt()`. |
| 210 |
* |
| 211 |
* @since 3.6.0 |
| 212 |
* |
| 213 |
* @param string $text Excerpt. |
| 214 |
* @return string |
| 215 |
*/ |
| 216 |
function strip_dynamic_blocks_add_filter( $text ) { |
| 217 |
add_filter( 'the_content', 'strip_dynamic_blocks', 6 ); |
| 218 |
|
| 219 |
return $text; |
| 220 |
} |
| 221 |
add_filter( 'get_the_excerpt', 'strip_dynamic_blocks_add_filter', 9 ); // Before wp_trim_excerpt(). |
| 222 |
} |
| 223 |
|
| 224 |
if ( ! function_exists( 'strip_dynamic_blocks_remove_filter' ) ) { |
| 225 |
/** |
| 226 |
* Removes the content filter to strip dynamic blocks from excerpts. |
| 227 |
* |
| 228 |
* It's a bit hacky for now, but once this gets merged into core the function |
| 229 |
* can just be called in `wp_trim_excerpt()`. |
| 230 |
* |
| 231 |
* @since 3.6.0 |
| 232 |
* |
| 233 |
* @param string $text Excerpt. |
| 234 |
* @return string |
| 235 |
*/ |
| 236 |
function strip_dynamic_blocks_remove_filter( $text ) { |
| 237 |
remove_filter( 'the_content', 'strip_dynamic_blocks', 6 ); |
| 238 |
|
| 239 |
return $text; |
| 240 |
} |
| 241 |
add_filter( 'wp_trim_excerpt', 'strip_dynamic_blocks_remove_filter', 0 ); // Before all other. |
| 242 |
} |
| 243 |
|