-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender-template.php
108 lines (90 loc) · 4.05 KB
/
render-template.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?
// Create id attribute allowing for custom "anchor" value.
if( !empty($block['anchor']) ) {
$block['id'] = $block['anchor'];
}
// Create class attribute allowing for custom "className" and "align" values.
$className = 'slick-table-contents';
if( !empty($block['className']) ) {
$className .= ' ' . $block['className'];
}
$bgColor = (get_field('background_color')) ? 'background-color: ' . get_field('background_color') . ';' : '';
$textColor = (get_field('text_color')) ? 'color: ' . get_field('text_color') . ';' : '';
$padding = (get_field('background_color')) ? 'padding: 2rem;' : '';
$style = implode(" ", array($bgColor, $textColor, $padding));
$tag = ( get_field('list_style') == 'number' ) ? array('<ol>','</ol>') : array('<ul>','</ul>');
$headingList = implode(", ", get_field('headings') );
$post = get_post();
?>
<style>
.editor-styles-wrapper .wp-block .slick-table-contents a, .slick-table-contents a {
color: inherit;
}
</style>
<div id="<?= $block['id']; ?>" class="<?= $className; ?>" style="<?= $style; ?>">
<?php if ( get_field('title') ): ?>
<h2><?php the_field('title'); ?></h2>
<?php endif; ?>
<?php
if (!$is_preview) {
// ul or ol
echo $tag[0];
if ( has_blocks( $post->post_content ) ) {
$blocks = parse_blocks( $post->post_content );
$i = 0;
foreach( $blocks as $block ) {
if ( $blocks[$i]['blockName'] === 'core/heading' ) {
if ( !isset($blocks[$i]['attrs']['level']) ) {
$blocks[$i]['attrs']['level'] = 2;
}
$headingLevel = 'h'.$blocks[$i]['attrs']['level'];
if ( in_array($headingLevel, get_field('headings')) && strpos($blocks[$i]['attrs']['className'], 'ignore') === false ) {
$fullstring = $blocks[$i]['innerHTML'];
$parsed = get_string_between($fullstring, '>', '</h');
echo "<li><a href='#" . toSafeID($parsed) . "'>" . $parsed . "</a></li>";
}
}
$i++;
}
}
echo $tag[1];
}
else {
echo $tag[0]; ?>
<li><a href="#">This placeholder table of contents is only visible in the editor</a></li>
<li>
<a href="#">Check the frontend for the real table of contents</a>
<?php echo $tag[0]; ?>
<li><a href="#">This is a subheading</a></li>
<li><a href="#">This is another subheading</a></li>
<?php echo $tag[1]; ?>
</li>
<li>
<a href="">This is a another heading</a>
<?php echo $tag[0]; ?>
<li><a href="#">This is a subheading</a></li>
<li><a href="#">This is another subheading</a></li>
<?php echo $tag[1]; ?>
</li>
<?php echo $tag[1];
}
echo "</div>";
?>
<?php if (!$is_preview): ?>
<script>
document.addEventListener("DOMContentLoaded", function() {
var headings = content.querySelectorAll('<?= $headingList; ?>');
var headingMap = {};
Array.prototype.forEach.call(headings, function (heading) {
var id = heading.id ? heading.id : heading.textContent.trim().toLowerCase()
.split(' ').join('-').replace(/[!@#$%^&*():]/ig, '');
headingMap[id] = !isNaN(headingMap[id]) ? ++headingMap[id] : 0;
if (headingMap[id]) {
heading.id = id + '-' + headingMap[id];
} else {
heading.id = id;
}
});
});
</script>
<?php endif; ?>