Skip to content

Commit f25ae3d

Browse files
committed
Bits: Introduce the ability to store and render bits
1 parent a584c4e commit f25ae3d

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

src/wp-includes/blocks.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,8 @@ function render_block( $parsed_block ) {
17141714
* @return array[] Array of parsed block objects.
17151715
*/
17161716
function parse_blocks( $content ) {
1717+
$content = wp_replace_bits( $content );
1718+
17171719
/**
17181720
* Filter to allow plugins to replace the server-side block parser.
17191721
*
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
function wp_replace_bits( $content ) {
4+
$processor = new class ( $content ) extends WP_HTML_Tag_Processor {
5+
private $deferred_updates = array();
6+
7+
public function replace_token( $new_content ) {
8+
$this->set_bookmark( 'here' );
9+
$here = $this->bookmarks['here'];
10+
11+
$this->deferred_updates[] = new WP_HTML_Text_Replacement(
12+
$here->start,
13+
$here->length,
14+
$new_content
15+
);
16+
}
17+
18+
public function flush_updates() {
19+
foreach ( $this->deferred_updates as $update ) {
20+
$this->lexical_updates[] = $update;
21+
}
22+
}
23+
};
24+
25+
while ( $processor->next_token() ) {
26+
switch ( $processor->get_token_type() ) {
27+
case '#funky-comment':
28+
$processor->replace_token( '<b>bl<em>ar</em>g</b>' );
29+
break;
30+
31+
case '#tag':
32+
foreach ( $processor->get_attribute_names_with_prefix( '' ) ?? [] as $name ) {
33+
$value = $processor->get_attribute( $name );
34+
if ( is_string( $value ) ) {
35+
$new_value = preg_replace_callback(
36+
'~<//wp:([^>]+)>~',
37+
static function ( $bit ) {
38+
return 'blarg';
39+
},
40+
$value
41+
);
42+
43+
if ( $new_value !== $value ) {
44+
$processor->set_attribute( $name, $new_value );
45+
}
46+
}
47+
}
48+
break;
49+
50+
case '#comment':
51+
if ( WP_HTML_Tag_Processor::COMMENT_AS_HTML_COMMENT !== $processor->get_comment_type() ) {
52+
break;
53+
}
54+
55+
$text = $processor->get_modifiable_text();
56+
if ( 1 === preg_match( '~^<//wp:([^>]+)>$~', $text ) ) {
57+
$processor->replace_token( '<b>Bla<em>rg</em>!</b>' );
58+
break;
59+
}
60+
61+
$new_value = preg_replace_callback(
62+
'~<//wp:([^>]+)>~',
63+
static function ( $bit ) {
64+
return 'blarg';
65+
},
66+
$text
67+
);
68+
69+
$processor->replace_token( "<!--{$new_value}-->" );
70+
break;
71+
}
72+
}
73+
$processor->flush_updates();
74+
$content = $processor->get_updated_html();
75+
76+
return $content;
77+
}

src/wp-includes/kses.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ function wp_kses_split( $content, $allowed_html, $allowed_protocols ) {
981981
$pass_allowed_html = $allowed_html;
982982
$pass_allowed_protocols = $allowed_protocols;
983983

984-
return preg_replace_callback( '%(<!--.*?(-->|$))|(<[^>]*(>|$)|>)%', '_wp_kses_split_callback', $content );
984+
return preg_replace_callback( '%((?:<!--.*?(-->|$))|</[^a-z][^>]*>)|(<[^>]*(>|$)|>)%', '_wp_kses_split_callback', $content );
985985
}
986986

987987
/**
@@ -1085,6 +1085,11 @@ function wp_kses_split2( $content, $allowed_html, $allowed_protocols ) {
10851085
return '&gt;';
10861086
}
10871087

1088+
// Allows Bits.
1089+
if ( 1 === preg_match( '~</[^a-z][^>]*>~', $content ) ) {
1090+
return $content;
1091+
}
1092+
10881093
// Allow HTML comments.
10891094
if ( str_starts_with( $content, '<!--' ) ) {
10901095
$content = str_replace( array( '<!--', '-->' ), '', $content );

src/wp-settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@
258258
require ABSPATH . WPINC . '/html-api/class-wp-html-token.php';
259259
require ABSPATH . WPINC . '/html-api/class-wp-html-processor-state.php';
260260
require ABSPATH . WPINC . '/html-api/class-wp-html-processor.php';
261+
require ABSPATH . WPINC . '/html-api/class-wp-bits.php';
261262
require ABSPATH . WPINC . '/class-wp-http.php';
262263
require ABSPATH . WPINC . '/class-wp-http-streams.php';
263264
require ABSPATH . WPINC . '/class-wp-http-curl.php';

0 commit comments

Comments
 (0)