-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockify-accordion.php
146 lines (121 loc) · 3.14 KB
/
blockify-accordion.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Plugin Name: Blockify Accordion
* Plugin URI: https://blockifywp.com/blocks/accordion
* Description: Lightweight, customizable accordion block for WordPress.
* Author: Blockify
* Author URI: https://blockifywp.com/
* Version: 0.0.1
* License: GPLv2-or-Later
* Text Domain: blockify
*/
declare( strict_types=1 );
namespace Blockify\Accordion;
const NS = __NAMESPACE__ . '\\';
const DS = DIRECTORY_SEPARATOR;
use DOMElement;
use function add_action;
use function register_block_type;
use function str_contains;
use function defined;
use function libxml_clear_errors;
use function libxml_use_internal_errors;
use function mb_convert_encoding;
use DOMDocument;
add_action( 'init', NS . 'register' );
/**
* Registers the block.
*
* @since 0.0.1
*
* @since 1.0.0
*
* @return void
*/
function register() : void {
register_block_type( __DIR__ . '/build' );
add_filter( 'block_categories_all', fn( array $categories ) : array => array_merge(
$categories,
[
[
'slug' => 'blockify',
'title' => __( 'Blockify', 'blockify' ),
],
]
) );
}
add_filter( 'render_block_blockify/accordion', NS . 'render_accordion_block', 10, 2 );
/**
* Modifies front end HTML output of block.
*
* @since 0.0.3
*
* @param string $content
* @param array $block
*
* @return string
*/
function render_accordion_block( string $content, array $block ): string {
$dom = dom( $content );
/**
* @var $div DOMElement
*/
$div = $dom->getElementsByTagName( 'div' )->item( 0 );
$div->setAttribute( 'class', 'wp-block-blockify-accordion' );
return $dom->saveHTML();
}
add_filter( 'render_block', NS . 'render_accordion_item_block', 10, 2 );
/**
* Modifies front end HTML output of child blocks.
*
* @since 0.0.2
*
* @param string $content
* @param array $block
*
* @return string
*/
function render_accordion_item_block( string $content, array $block ): string {
if ( 'blockify/accordion-item' !== $block['blockName'] ) {
return $content;
}
if ( isset( $block['attrs']['className'] ) && str_contains( $block['attrs']['className'], 'is-style-open' ) ) {
$dom = dom( $content );
/** @var DOMElement $details */
$details = $dom->firstChild;
$details->setAttribute( 'open', '' );
$content = $dom->saveHTML();
}
return $content;
}
/**
* Returns a formatted DOMDocument object from a given string.
*
* @since 0.0.2
*
* @param string $html
*
* @return string
*/
function dom( string $html ): DOMDocument {
$dom = new DOMDocument();
if ( ! $html ) {
return $dom;
}
$libxml_previous_state = libxml_use_internal_errors( true );
$dom->preserveWhiteSpace = true;
if ( defined( 'LIBXML_HTML_NOIMPLIED' ) && defined( 'LIBXML_HTML_NODEFDTD' ) ) {
$options = LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD;
} else if ( defined( 'LIBXML_HTML_NOIMPLIED' ) ) {
$options = LIBXML_HTML_NOIMPLIED;
} else if ( defined( 'LIBXML_HTML_NODEFDTD' ) ) {
$options = LIBXML_HTML_NODEFDTD;
} else {
$options = 0;
}
$dom->loadHTML( mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ), $options );
$dom->formatOutput = true;
libxml_clear_errors();
libxml_use_internal_errors( $libxml_previous_state );
return $dom;
}