-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefinitionList.php
66 lines (56 loc) · 1.48 KB
/
DefinitionList.php
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
<?php
namespace Kunoichi\BlockLibrary\Blocks;
use Kunoichi\BlockLibrary\Pattern\BlockLibraryBase;
class DefinitionList extends BlockLibraryBase {
protected $block_name = 'definition-list';
protected function init() {
parent::init();
add_action( 'wp_head', [ $this, 'render_json_ld' ], 9999 );
}
/**
* Render JSON ld.
*/
public function render_json_ld() {
if ( ! is_singular() ) {
return;
}
if ( ! has_block( 'kunoichi/definition-list', get_queried_object() ) ) {
return;
}
$blocks = parse_blocks( get_queried_object()->post_content );
foreach ( $blocks as $block ) {
if ( 'kunoichi/definition-list' !== $block['blockName'] ) {
continue;
}
if ( ! ( isset( $block['attrs']['faq'] ) && $block['attrs']['faq'] ) ) {
continue;
}
$faqs = [];
foreach ( $block['innerBlocks'] as $index => $dt ) {
if ( 'kunoichi/dt' !== $dt['blockName'] ) {
continue;
}
if ( ! isset( $block['innerBlocks'][ $index + 1 ] ) || 'kunoichi/dd' !== $block['innerBlocks'][ $index + 1 ]['blockName'] ) {
continue;
}
$dd = $block['innerBlocks'][ $index + 1 ];
$faqs[] = [
'@type' => 'Question',
'name' => trim( strip_tags( $dt['innerHTML'] ) ),
'acceptedAnswer' => [
'@type' => 'Answer',
'text' => trim( strip_tags( $dd['innerHTML'] ) ),
],
];
}
if ( ! $faqs ) {
continue;
}
$json = [
'mainEntity' => $faqs,
];
$this->json_ld( $json, 'FAQPage' );
break;
}
}
}