-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockify-breadcrumbs.php
171 lines (139 loc) · 3.85 KB
/
blockify-breadcrumbs.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* Plugin Name: Blockify Breadcrumbs
* Plugin URI: https://blockifywp.com/blocks/breadcrumbs
* Description: Lightweight, customizable breadcrumbs 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\Breadcrumbs;
use DOMElement;
use function apply_filters;
use function function_exists;
use function get_option;
use function get_permalink;
use function get_post;
use function get_the_title;
use function home_url;
use function is_front_page;
use function is_home;
use function str_contains;
use function str_replace;
use function trailingslashit;
use function wp_get_post_parent_id;
use function register_block_type;
use function add_action;
const NS = __NAMESPACE__ . '\\';
const DS = DIRECTORY_SEPARATOR;
add_action( 'init', NS . 'register' );
/**
* Registers the block.
*
* @since 0.0.1
*
* @since 1.0.0
*
* @return void
*/
function register() {
register_block_type( __DIR__ . '/build' );
}
/**
* Renders the breadcrumbs block.
*
* @since 0.0.2
*
* @param array $attributes
* @param string $content
*
* @return string
*/
function breadcrumbs_block_render_callback( array $attributes, string $content ): string {
if ( ! str_contains( $content, 'wp-block-blockify-breadcrumbs' ) ) {
return $content;
}
if ( function_exists( 'yoast_breadcrumb' ) ) {
return yoast_breadcrumb( '<p id=breadcrumbs', '</p>', false );
}
$html = null;
$post = get_post();
if ( $post ) {
$html = '<span>' . $post->post_title . '</span>';
}
$id = wp_get_post_parent_id();
$separator = ' / ';
while ( $id ) {
$url = get_permalink( $id );
$title = get_the_title( $id );
$html = "<a href=\"$url\" >$title</a>" . $separator . $html;
$id = wp_get_post_parent_id( $id );
}
if ( 'page' === get_option( 'show_on_front' ) ) {
$url = get_permalink( get_option( 'page_on_front' ) );
} else {
$url = trailingslashit( home_url() );
}
$default = apply_filters( 'blockify_breadcrumbs_home', __( 'Home', 'blockify' ) );
$home = ( is_home() && is_front_page() ) ? $default : "<a href=\"$url\" >$default</a>";
$dom = dom( str_replace(
'</div>',
$home . $separator . $html,
$content
) );
/**
* @var $div DOMElement
*/
$div = $dom->firstChild;
$style = ';';
if ( isset( $attributes['layout']['type'] ) ) {
$style .= 'display:' . $attributes['layout']['type'] . ';';
}
if ( isset( $attributes['layout']['justifyContent'] ) ) {
$style .= 'justify-content:' . $attributes['layout']['justifyContent'] . ';';
}
if ( isset( $attributes['style']['spacing']['blockGap'] ) ) {
$style .= 'gap:' . $attributes['style']['spacing']['blockGap'] . ';';
}
$div->setAttribute( 'style', $div->getAttribute( 'style' ) . $style );
return $dom->saveHTML();
}
use function defined;
use function libxml_clear_errors;
use function libxml_use_internal_errors;
use function mb_convert_encoding;
use DOMDocument;
/**
* 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;
}