File tree Expand file tree Collapse file tree 4 files changed +92
-12
lines changed Expand file tree Collapse file tree 4 files changed +92
-12
lines changed Original file line number Diff line number Diff line change 1+ # WordPress Block Parser
2+
3+ Parse WordPress edit-context content to PHP objects easily.
4+
5+ ## Installation
6+ ``` bash
7+ composer require recoded-dev/wordpress-block-parser
8+ ```
9+
10+ ## Examples
11+
12+ ### Parsing and replacing
13+ ``` php
14+ <?php
15+
16+ use Recoded\WordPressBlockParser\BlockParser;
17+ use Recoded\WordPressBlockParser\Blocks\Block;
18+ use Recoded\WordPressBlockParser\BlockReplacer;
19+
20+ $content = <<<HTML
21+ <!-- wp:paragraph -- >
22+ Test
23+ <!-- /wp:paragraph -->
24+ HTML;
25+
26+ $parser = BlockParser::create($content);
27+ $replacer = BlockReplacer::create($content);
28+
29+ foreach ($parser as $block) {
30+ // $block->namespace
31+ // $block->name
32+ // $block->attributes
33+
34+ if ($block instanceof Block) {
35+ // $block->content
36+ }
37+
38+ $replacer->replace($block, 'Your replaced content');
39+ }
40+
41+ echo (string) $replacer; // Your replaced content
42+ ```
43+
44+ ## Contributing
45+ Everyone is welcome to contribute. Feel free to PR your work once you think it's ready.
46+ Or open a draft-PR if you want to get some opinions or further help.
47+
48+ I would like to keep this package relatively small and want to avoid bloat. The package
49+ should remain extensible and unopinionated.
Original file line number Diff line number Diff line change 44
55abstract class AbstractBlock
66{
7+ /**
8+ * Create new AbstractBlock instance.
9+ *
10+ * @param string $namespace
11+ * @param string $name
12+ * @param array<string, mixed> $attributes
13+ * @return void
14+ */
15+ public function __construct (
16+ public readonly string $ namespace ,
17+ public readonly string $ name ,
18+ public readonly array $ attributes ,
19+ ) {
20+ //
21+ }
22+
723 /**
824 * Get the offset on which the block starts.
925 *
Original file line number Diff line number Diff line change 55use Recoded \WordPressBlockParser \Tokens \BlockClosing ;
66use Recoded \WordPressBlockParser \Tokens \BlockOpening ;
77
8+ /**
9+ * @TODO find better name, block is too generic
10+ */
811final class Block extends AbstractBlock
912{
13+ public readonly string $ content ;
14+ public readonly BlockOpening $ opening ;
15+ public readonly BlockClosing $ closing ;
16+
1017 /**
1118 * Create new Block instance.
1219 *
@@ -19,14 +26,18 @@ final class Block extends AbstractBlock
1926 * @return void
2027 */
2128 public function __construct (
22- public readonly string $ namespace ,
23- public readonly string $ name ,
24- public readonly array $ attributes ,
25- public readonly string $ content ,
26- public readonly BlockOpening $ opening ,
27- public readonly BlockClosing $ closing ,
29+ string $ namespace ,
30+ string $ name ,
31+ array $ attributes ,
32+ string $ content ,
33+ BlockOpening $ opening ,
34+ BlockClosing $ closing ,
2835 ) {
29- //
36+ parent ::__construct ($ namespace , $ name , $ attributes );
37+
38+ $ this ->content = $ content ;
39+ $ this ->opening = $ opening ;
40+ $ this ->closing = $ closing ;
3041 }
3142
3243 /**
Original file line number Diff line number Diff line change 66
77final class SelfClosingBlock extends AbstractBlock
88{
9+ public readonly SelfClosingBlockToken $ token ;
10+
911 /**
1012 * Create new SelfClosingBlock instance.
1113 *
@@ -16,12 +18,14 @@ final class SelfClosingBlock extends AbstractBlock
1618 * @return void
1719 */
1820 public function __construct (
19- public readonly string $ namespace ,
20- public readonly string $ name ,
21- public readonly array $ attributes ,
22- public readonly SelfClosingBlockToken $ token ,
21+ string $ namespace ,
22+ string $ name ,
23+ array $ attributes ,
24+ SelfClosingBlockToken $ token ,
2325 ) {
24- //
26+ parent ::__construct ($ namespace , $ name , $ attributes );
27+
28+ $ this ->token = $ token ;
2529 }
2630
2731 /**
You can’t perform that action at this time.
0 commit comments