-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAnnotatedBlockMarkupProducer.php
More file actions
69 lines (61 loc) · 1.56 KB
/
AnnotatedBlockMarkupProducer.php
File metadata and controls
69 lines (61 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace WordPress\DataLiberation\DataFormatProducer;
use WordPress\DataLiberation\DataFormatConsumer\BlocksWithMetadata;
use WordPress\HTML\WP_HTML_Tag_Processor;
/**
* Turns Block Markup + Metadata into a metadata-annotated Block Markup.
*
* Example:
*
* The following block markup:
*
* <!-- wp:paragraph -->
* <p>Hello <b>world</b>!</p>
* <!-- /wp:paragraph -->
*
* And metadata:
*
* array(
* 'post_title' => array( 'My first post' ),
* )
*
* Becomes:
*
* <meta name="post_title" content="My first post">
* <!-- wp:paragraph -->
* <p>Hello <b>world</b>!</p>
* <!-- /wp:paragraph -->
*/
class AnnotatedBlockMarkupProducer {
/**
* @var BlocksWithMetadata
*/
private $blocks_with_meta;
/**
* @var string
*/
private $result;
public function __construct( BlocksWithMetadata $blocks_with_meta ) {
$this->blocks_with_meta = $blocks_with_meta;
}
public function produce() {
if ( null === $this->result ) {
$this->result = '';
foreach ( $this->blocks_with_meta->get_all_metadata() as $key => $values ) {
foreach ( $values as $value ) {
$p = new WP_HTML_Tag_Processor( '<meta>' );
$p->next_tag();
$p->set_attribute( 'name', $key );
if ( is_array( $value ) || is_object( $value ) ) {
$value = json_encode( $value );
}
$p->set_attribute( 'content', $value );
$p->set_attribute( 'type', gettype( $value ) );
$this->result .= $p->get_updated_html() . "\n";
}
}
$this->result .= "\n" . trim( $this->blocks_with_meta->get_block_markup(), "\n" );
}
return $this->result;
}
}