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 4
4
5
5
abstract class AbstractBlock
6
6
{
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
+
7
23
/**
8
24
* Get the offset on which the block starts.
9
25
*
Original file line number Diff line number Diff line change 5
5
use Recoded \WordPressBlockParser \Tokens \BlockClosing ;
6
6
use Recoded \WordPressBlockParser \Tokens \BlockOpening ;
7
7
8
+ /**
9
+ * @TODO find better name, block is too generic
10
+ */
8
11
final class Block extends AbstractBlock
9
12
{
13
+ public readonly string $ content ;
14
+ public readonly BlockOpening $ opening ;
15
+ public readonly BlockClosing $ closing ;
16
+
10
17
/**
11
18
* Create new Block instance.
12
19
*
@@ -19,14 +26,18 @@ final class Block extends AbstractBlock
19
26
* @return void
20
27
*/
21
28
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 ,
28
35
) {
29
- //
36
+ parent ::__construct ($ namespace , $ name , $ attributes );
37
+
38
+ $ this ->content = $ content ;
39
+ $ this ->opening = $ opening ;
40
+ $ this ->closing = $ closing ;
30
41
}
31
42
32
43
/**
Original file line number Diff line number Diff line change 6
6
7
7
final class SelfClosingBlock extends AbstractBlock
8
8
{
9
+ public readonly SelfClosingBlockToken $ token ;
10
+
9
11
/**
10
12
* Create new SelfClosingBlock instance.
11
13
*
@@ -16,12 +18,14 @@ final class SelfClosingBlock extends AbstractBlock
16
18
* @return void
17
19
*/
18
20
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 ,
23
25
) {
24
- //
26
+ parent ::__construct ($ namespace , $ name , $ attributes );
27
+
28
+ $ this ->token = $ token ;
25
29
}
26
30
27
31
/**
You can’t perform that action at this time.
0 commit comments