Skip to content

Commit 1c5ef3b

Browse files
authored
Merge pull request #60 from Automattic/add/pre-content-filter
Add pre- and post- content filter
2 parents 4c58623 + c547220 commit 1c5ef3b

File tree

6 files changed

+320
-134
lines changed

6 files changed

+320
-134
lines changed

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,99 @@ Direct block HTML can be accessed through `$block['innerHTML']`. This may be use
12181218
12191219
For another example of how this filter can be used to extend block data, we have implemented a default image block filter in [`src/parser/block-additions/core-image.php`][repo-core-image-block-addition]. This filter is automatically called on `core/image` blocks to add `width` and `height` to image attributes.
12201220
1221+
---
1222+
1223+
### `vip_block_data_api__before_parse_post_content`
1224+
1225+
Modify raw post content before it's parsed by the Block Data API. The `$post_content` provided by this filter is directly what is stored in the post database before any processing occurs.
1226+
1227+
```php
1228+
/**
1229+
* Filters content before parsing blocks in a post.
1230+
*
1231+
* @param string $post_content The content of the post being parsed.
1232+
* @param int $post_id Post ID associated with the content.
1233+
*/
1234+
$post_content = apply_filters( 'vip_block_data_api__before_parse_post_content', $post_content, $post_id );
1235+
```
1236+
1237+
For example, this could be used to modify a block's type before parsing. The code below replaces instances of `test/invalid-block` blocks with `core/paragraph`:
1238+
1239+
```php
1240+
add_filter( 'vip_block_data_api__before_parse_post_content', 'replace_invalid_blocks' );
1241+
1242+
function replace_invalid_blocks( $post_content, $post_id ) {
1243+
return str_replace( 'wp:test/invalid-block', 'wp:paragraph', $post_content );
1244+
}
1245+
1246+
$html = '
1247+
<!-- wp:test/invalid-block -->
1248+
<p>Block content!</p>
1249+
<!-- /wp:test/invalid-block -->
1250+
';
1251+
1252+
$content_parser = new ContentParser();
1253+
$result = $content_parser->parse( $html );
1254+
1255+
// Evaluates to true
1256+
assertEquals( [
1257+
[
1258+
'name' => 'core/paragraph',
1259+
'attributes' => [
1260+
'content' => 'Block content!',
1261+
],
1262+
],
1263+
], $result['blocks'] );
1264+
```
1265+
1266+
**Warning**
1267+
1268+
Be careful with content modification before parsing. In the example above, if a block contained the text "wp:test/invalid-block" outside of a block header, this would also be changed to "wp:paragraph". This is likely not the intent of the code.
1269+
1270+
All block markup is sensitive to changes, even changes in whitespace. We've added this filter to make the plugin flexible, but any transforms to `post_content` should be done with extreme care. Strongly consider adding tests to any usage of this filter.
1271+
1272+
---
1273+
1274+
### `vip_block_data_api__after_parse_blocks`
1275+
1276+
Modify the Block Data API REST endpoint response.
1277+
1278+
```php
1279+
/**
1280+
* Filters the API result before returning parsed blocks in a post.
1281+
*
1282+
* @param string $result The successful API result, contains 'blocks' key with an array
1283+
* of block data, and optionally 'warnings' and 'debug' keys.
1284+
* @param int $post_id Post ID associated with the content.
1285+
*/
1286+
$result = apply_filters( 'vip_block_data_api__after_parse_blocks', $result, $post_id );
1287+
```
1288+
1289+
This filter is called directly before returning a result in the REST API. Use this filter to add additional metadata or debug information to the API output.
1290+
1291+
```php
1292+
add_action( 'vip_block_data_api__after_parse_blocks', 'add_block_data_debug_info', 10, 2 );
1293+
1294+
function add_block_data_debug_info( $result, $post_id ) {
1295+
$result['debug']['my-value'] = 123;
1296+
1297+
return $result;
1298+
}
1299+
```
1300+
1301+
This would add `debug.my-value` to all Block Data API REST results:
1302+
1303+
```bash
1304+
> curl https://my.site/wp-json/vip-block-data-api/v1/posts/1/blocks
1305+
1306+
{
1307+
"debug": {
1308+
"my-value": 123
1309+
},
1310+
"blocks": [ /* ... */ ]
1311+
}
1312+
```
1313+
12211314
## Analytics
12221315
12231316
**Please note that, this is for VIP sites only. Analytics are disabled if this plugin is not being run on VIP sites.**

src/parser/content-parser.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ public function parse( $post_content, $post_id = null, $filter_options = [] ) {
128128
$parsing_error = false;
129129

130130
try {
131+
/**
132+
* Filters content before parsing blocks in a post.
133+
*
134+
* @param string $post_content The content of the post being parsed.
135+
* @param int $post_id Post ID associated with the content.
136+
*/
137+
$post_content = apply_filters( 'vip_block_data_api__before_parse_post_content', $post_content, $post_id );
138+
131139
$blocks = parse_blocks( $post_content );
132140
$blocks = array_values( array_filter( $blocks, function ( $block ) {
133141
$is_whitespace_block = ( null === $block['blockName'] && empty( trim( $block['innerHTML'] ) ) );
@@ -168,6 +176,15 @@ public function parse( $post_content, $post_id = null, $filter_options = [] ) {
168176
'details' => $parsing_error->__toString(),
169177
] );
170178
} else {
179+
/**
180+
* Filters the API result before returning parsed blocks in a post.
181+
*
182+
* @param string $result The successful API result, contains 'blocks'
183+
* key with an array of block data, and optionally 'warnings' and 'debug' keys.
184+
* @param int $post_id Post ID associated with the content.
185+
*/
186+
$result = apply_filters( 'vip_block_data_api__after_parse_blocks', $result, $post_id );
187+
171188
return $result;
172189
}
173190
}

0 commit comments

Comments
 (0)