-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclass-breadcrumbs.php
More file actions
257 lines (231 loc) · 6.79 KB
/
class-breadcrumbs.php
File metadata and controls
257 lines (231 loc) · 6.79 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/**
* Breadcrumbs module.
*
* @package WebberZone\Knowledge_Base
*/
namespace WebberZone\Knowledge_Base\Frontend;
use WebberZone\Knowledge_Base\Util\Helpers;
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Breadcrumbs Class.
*
* @since 2.3.0
*/
class Breadcrumbs {
/**
* Converts Unicode sequence to character.
*
* @since 2.3.0
*
* @param string $unicode Unicode sequence (e.g., '\2192' for →).
* @return string Converted character.
*/
private static function unicode_to_char( $unicode ) {
// Remove backslash if present.
$unicode = ltrim( $unicode, '\\' );
// Convert Unicode sequence to character.
return html_entity_decode( '&#x' . $unicode . ';', ENT_COMPAT, 'UTF-8' );
}
/**
* Get the product term for the current context.
*
* @return \WP_Term|false
*/
private static function get_product_for_context() {
if ( is_tax( 'wzkb_product' ) ) {
$product = get_queried_object();
if ( $product ) {
return $product;
}
}
if ( is_tax( 'wzkb_category' ) || is_tax( 'wzkb_tag' ) ) {
$tax = get_queried_object();
if ( $tax instanceof \WP_Term ) {
$product = wzkb_get_section_product( $tax );
if ( $product ) {
return $product;
}
}
}
if ( is_singular( 'wz_knowledgebase' ) ) {
$post = get_queried_object();
$terms = get_the_terms( $post, 'wzkb_category' );
if ( is_array( $terms ) && ! empty( $terms ) ) {
$primary_term = $terms[0];
$ancestor = $primary_term;
while ( $ancestor->parent ) {
$ancestor = get_term( $ancestor->parent, $ancestor->taxonomy );
}
$product = wzkb_get_section_product( $ancestor );
if ( $product ) {
return $product;
}
}
}
return false;
}
/**
* Check if current page is displaying Knowledge Base content via shortcode or block.
*
* @since 2.3.0
*
* @return bool True if KB content is displayed via shortcode or block.
*/
private static function is_kb_context() {
// Check if current page content contains knowledgebase shortcode or block.
if ( is_front_page() || is_home() || is_page() ) {
$post = get_post();
if ( $post ) {
// Check for shortcode.
if ( has_shortcode( $post->post_content, 'knowledgebase' ) ) {
return true;
}
// Check for block in Gutenberg content.
if ( has_block( 'knowledgebase/knowledgebase', $post ) ) {
return true;
}
}
}
return false;
}
/**
* Creates the breadcrumb.
*
* @since 2.3.0
*
* @param array $args Parameters array.
* @return string|bool Formatted HTML output. False if not a WZKB post type archive or post.
*/
public static function get_breadcrumb( $args = array() ) {
$defaults = array(
'separator' => '»',
);
$args = wp_parse_args( $args, $defaults );
$args = Helpers::sanitize_args( $args );
if ( strpos( $args['separator'], '\\' ) === 0 ) {
$args['separator'] = self::unicode_to_char( $args['separator'] );
}
if ( ( ! is_admin() && ! wp_is_json_request() ) &&
! is_post_type_archive( 'wz_knowledgebase' ) &&
! is_singular( 'wz_knowledgebase' ) &&
! is_tax( 'wzkb_category' ) &&
! is_tax( 'wzkb_tag' ) &&
! is_tax( 'wzkb_product' ) &&
! self::is_kb_context()
) {
return '';
}
$items = array();
$items[] = array(
'url' => home_url(),
'label' => esc_html__( 'Home', 'knowledgebase' ),
'position' => 1,
'current' => false,
);
$items[] = array(
'url' => wzkb_get_kb_url(),
'label' => esc_html( wzkb_get_option( 'kb_title' ) ),
'position' => 2,
'current' => false,
);
$position = 3;
if ( wzkb_get_option( 'multi_product' ) && ! is_tax( 'wzkb_product' ) ) {
$product = self::get_product_for_context();
if ( $product ) {
$items[] = array(
'url' => get_term_link( $product ),
'label' => esc_html( $product->name ),
'position' => $position,
'current' => false,
);
++$position;
}
}
if ( is_tax( 'wzkb_category' ) || is_tax( 'wzkb_tag' ) ) {
$tax = get_queried_object();
$trail = self::get_hierarchical_term_trail_array( $tax, $args, $position );
foreach ( $trail as $item ) {
$items[] = $item;
++$position;
}
}
if ( is_tax( 'wzkb_product' ) ) {
$product = get_queried_object();
$items[] = array(
'url' => get_term_link( $product ),
'label' => esc_html( $product->name ),
'position' => $position,
'current' => true,
);
++$position;
}
if ( is_singular( 'wz_knowledgebase' ) ) {
$post = get_queried_object();
$terms = get_the_terms( $post, 'wzkb_category' );
if ( is_array( $terms ) && ! empty( $terms ) ) {
$tax = $terms[0];
$trail = self::get_hierarchical_term_trail_array( $tax, $args, $position );
foreach ( $trail as $item ) {
$items[] = $item;
++$position;
}
}
$items[] = array(
'url' => get_permalink( $post ),
'label' => esc_html( $post->post_title ),
'position' => $position,
'current' => true,
);
}
$items = apply_filters( 'wzkb_breadcrumb_items', $items, $args );
$output = '<nav class="wzkb_breadcrumb" aria-label="' . esc_attr__( 'Breadcrumb', 'knowledgebase' ) . '">';
$output .= '<ol class="wzkb_breadcrumb-list" itemscope itemtype="https://schema.org/BreadcrumbList">';
$sep = esc_attr( $args['separator'] );
foreach ( $items as $item ) {
$output .= '<li class="wzkb_breadcrumb-item" data-separator="' . $sep . '" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">';
$output .= '<a href="' . esc_url( $item['url'] ) . '" itemprop="item"' . ( ! empty( $item['current'] ) ? ' aria-current="page"' : '' ) . '>';
$output .= '<span itemprop="name">' . $item['label'] . '</span>';
$output .= '</a>';
$output .= '<meta itemprop="position" content="' . intval( $item['position'] ) . '" />';
$output .= '</li>';
}
$output .= '</ol>';
$output .= '</nav>';
return apply_filters( 'wzkb_get_breadcrumb', $output, $args );
}
/**
* Returns the hierarchical term trail as an array of breadcrumb items.
*
* @param \WP_Term $taxonomy Taxonomy object.
* @param array $args Parameters array.
* @param int $position Current position in breadcrumb.
* @return array Array of breadcrumb items.
*/
private static function get_hierarchical_term_trail_array( \WP_Term $taxonomy, $args = array(), $position = 2 ) {
$defaults = array(
'separator' => '»',
);
$args = wp_parse_args( $args, $defaults );
$trail = array();
if ( ! empty( $taxonomy->parent ) ) {
$trail = array_merge(
self::get_hierarchical_term_trail_array(
get_term( $taxonomy->parent, $taxonomy->taxonomy ),
$args,
$position
),
);
$position += count( $trail );
}
$trail[] = array(
'url' => get_term_link( $taxonomy ),
'label' => esc_html( $taxonomy->name ),
'position' => $position,
'current' => false,
);
return $trail;
}
}