forked from brainstormforce/json-ld-breadcrumbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-json-ld-breadcrumbs.php
394 lines (348 loc) · 10 KB
/
class-json-ld-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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
* Class JSON LD Breadcrumb.
*
* @package JSON_LD_Breadcrumbs
*/
// Exit if the file is called directy by URL.
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'JSON_LD_Breadcrumbs' ) ) {
/**
* Class JSON LD Breadcrumb.
*
* @since 1.0.0
*/
class JSON_LD_Breadcrumbs {
/**
* Instance of JSON_LD_Breadcrumbs
*
* @since v1.0.0
* @var Object JSON_LD_Breadcrumbs
*/
private static $instance = null;
/**
* Crumb position. Increases everytime a new crumb is added.
*
* @since 1.0.0
* @var integer
*/
private $crumb_position = 0;
/**
* Crunbs Array
*
* @since 1.0.0
* @var array
*/
private $crumbs = array();
/**
* Initiate the class JSON_LD_Breadcrumbs
*
* @since 1.0.0
* @return (Object) Instance of JSON_LD_Breadcrumbs
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*
* @since 1.0.0
*/
private function __construct() {
$this->post = ( isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null );
$this->show_on_front = get_option( 'show_on_front' );
$this->page_for_posts = get_option( 'page_for_posts' );
add_action( 'wp_head', array( $this, 'set_crumbs' ) );
}
/**
* Initialize the Schema for the breadcrumbs markup.
*
* @since 1.0.0
*
* @param (Array) $breadcrumb Breadcrumbs array.
*
* @return (Array) $breadcrumb Breadcrumbs array.
*/
private function initialize_breadcrumb_schema( $breadcrumb ) {
$breadcrumb['@context'] = 'http://schema.org';
$breadcrumb['@type'] = 'BreadcrumbList';
return $breadcrumb;
}
/**
* Adds homepage to the breadcrumb.
*
* @since 1.0.0
*/
private function maybe_add_home_crumb() {
// TODO: Add option in the admin panel to enable or disable home page in breadcrumb.
// TODO: Add option in the admin panel to choose the text for home page.
$this->add_crumb(
'Home',
get_site_url()
);
}
/**
* Conditionally adds blog page to the breadcrumb.
*
* @since 1.0.0
*/
private function maybe_add_blog_crumb() {
if ( ( 'page' === $this->show_on_front && 'post' === get_post_type() ) && ( ! is_home() && ! is_search() ) ) {
if ( $this->page_for_posts ) {
$this->add_crumb( get_the_title( $this->page_for_posts ), get_permalink( $this->page_for_posts ) );
}
}
}
/**
* Add crumb to the breadcrumbs array.
*
* @since 1.0.0
*
* @param String $name Name of the Breadcrumb element.
* @param string $url URL of the Breadcrumb element.
* @param string $image Image URL of the Breadcrumb element.
*/
private function add_crumb( $name, $url = '', $image = '' ) {
$this->crumb_position = $this->crumb_position + 1;
if ( '' === $image ) {
$this->crumbs[] = array(
'@type' => 'ListItem',
'position' => $this->crumb_position,
'item' => array(
'@id' => $url,
'name' => $name,
),
);
} else {
$this->crumbs[] = array(
'@type' => 'ListItem',
'position' => $this->crumb_position,
'item' => array(
'@id' => $url,
'name' => $name,
'image' => $image,
),
);
}
}
/**
* Post type archive title.
*
* @since 1.0.0
*
* @param string $pt The name of a registered post type.
*
* @return String Title of the post type.
*/
private function post_type_archive_title( $pt ) {
$archive_title = '';
$post_type_obj = get_post_type_object( $pt );
if ( is_object( $post_type_obj ) ) {
if ( isset( $post_type_obj->label ) && '' !== $post_type_obj->label ) {
$archive_title = $post_type_obj->label;
} elseif ( isset( $post_type_obj->labels->menu_name ) && '' !== $post_type_obj->labels->menu_name ) {
$archive_title = $post_type_obj->labels->menu_name;
} else {
$archive_title = $post_type_obj->name;
}
}
return $archive_title;
}
/**
* Conditionally adds the post type archive to the breadcrumb.
*
* @since 1.0.0
*/
private function maybe_add_pt_archive_crumb_for_post() {
if ( 'post' === $this->post->post_type ) {
return;
}
if ( isset( $this->post->post_type ) && get_post_type_archive_link( $this->post->post_type ) ) {
$this->add_crumb( $this->post_type_archive_title( $this->post->post_type ), get_post_type_archive_link( $this->post->post_type ) );
}
}
/**
* Conditionally adds taxanomy titles to the breadcrumb.
*
* @since 1.0.0
*/
private function maybe_add_taxonomy_crumbs_for_post() {
// TODO: Add an option in admin panel to choose taxanomy base in the breadcrumb.
}
/**
* Adds post ancestor to the breadcrumb.
*
* @since 1.0.0
*/
private function add_post_ancestor_crumbs() {
$ancestors = $this->get_post_ancestors();
if ( is_array( $ancestors ) && array() !== $ancestors ) {
foreach ( $ancestors as $ancestor ) {
$this->add_crumb( get_the_title( $ancestor ), get_permalink( $ancestor ) );
}
}
}
/**
* Finds the post ancestors.
*
* @since 1.0.0
* @return Array Ancestors for the current page.
*/
private function get_post_ancestors() {
$ancestors = array();
if ( isset( $this->post->ancestors ) ) {
if ( is_array( $this->post->ancestors ) ) {
$ancestors = array_values( $this->post->ancestors );
} else {
$ancestors = array( $this->post->ancestors );
}
} elseif ( isset( $this->post->post_parent ) ) {
$ancestors = array( $this->post->post_parent );
}
// Reverse the order so it's oldest to newest.
$ancestors = array_reverse( $ancestors );
return $ancestors;
}
/**
* Add Taxanomies to breadcrumb.
*
* @since 1.0.0
*/
private function add_crumbs_for_taxonomy() {
$term = $GLOBALS['wp_query']->get_queried_object();
$this->add_crumb( $term->name, get_term_link( $term ) );
}
/**
* Add month to the breadcrumb.
*
* @since 1.0.0
*/
private function add_month_crumb() {
$this->add_crumb(
'Archives for ' . esc_html( single_month_title( ' ', false ) ),
get_month_link( get_query_var( 'y' ), get_query_var( 'monthnum' ) )
);
}
/**
* Add Month and year to breadcrumb for date archive.
*
* @since 1.0.0
*/
private function add_linked_month_year_crumb() {
$this->add_crumb(
$GLOBALS['wp_locale']->get_month( get_query_var( 'monthnum' ) ) . ' ' . get_query_var( 'year' ),
get_month_link( get_query_var( 'year' ), get_query_var( 'monthnum' ) )
);
}
/**
* Add date to the breadcrumb.
*
* @since 1.0.0
*/
private function add_date_crumb() {
$this->add_crumb(
'Archives for ' . esc_html( single_month_title( ' ', false ) ),
get_day_link( get_query_var( 'year' ), get_query_var( 'monthnum' ), get_query_var( 'day' ) )
);
}
/**
* Add year to the breadcrumb.
*
* @since 1.0.0
*/
private function add_year_crumb() {
$this->add_crumb(
'Archives for ' . esc_html( get_query_var( 'year' ) ),
get_year_link( get_query_var( 'year' ) )
);
}
/**
* Conditionally add individual crumbs to the breadcrumb.
*
* @since 1.0.0
*/
private function add_breadcrumb_crumbs() {
global $wp_query;
$this->maybe_add_home_crumb();
$this->maybe_add_blog_crumb();
if ( ( 'page' === $this->show_on_front && is_front_page() ) || ( 'posts' === $this->show_on_front && is_home() ) ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
// Do nothing.
} elseif ( 'page' === $this->show_on_front && is_home() ) {
$this->add_crumb( get_the_title( $this->page_for_posts ), get_permalink( $this->page_for_posts ) );
} elseif ( is_singular() ) {
$this->maybe_add_pt_archive_crumb_for_post();
if ( isset( $this->post->post_parent ) && 0 === $this->post->post_parent ) {
$this->maybe_add_taxonomy_crumbs_for_post();
} else {
$this->add_post_ancestor_crumbs();
}
if ( isset( $this->post->ID ) ) {
$this->add_crumb( get_the_title( $this->post->ID ), get_permalink( $this->post->ID ) );
}
} else {
if ( is_post_type_archive() ) {
$post_type = $wp_query->get( 'post_type' );
if ( $post_type && is_string( $post_type ) ) {
$this->add_crumb( $this->post_type_archive_title( $post_type ), get_post_type_archive_link( $post_type ) );
}
} elseif ( is_tax() || is_tag() || is_category() ) {
$this->add_crumbs_for_taxonomy();
} elseif ( is_date() ) {
if ( is_day() ) {
$this->add_linked_month_year_crumb();
$this->add_date_crumb();
} elseif ( is_month() ) {
$this->add_month_crumb();
} elseif ( is_year() ) {
$this->add_year_crumb();
}
} elseif ( is_author() ) {
$user = $wp_query->get_queried_object();
$this->add_crumb(
'Archives for ' . $user->display_name,
get_author_posts_url( $user->ID, $user->nicename )
);
} elseif ( is_search() ) {
$this->add_crumb(
'Search results for ' . esc_html( get_search_query() ),
get_search_link( get_query_var( 's' ) )
);
} elseif ( is_404() ) {
$this->add_crumb(
'Error 404: Page not found',
null
);
}
}
return apply_filters( 'json_ld_breadcrumb_itemlist_array', $this->crumbs );
}
/**
* Initialize the breadcrumbs.
*
* @since 1.0.0
*/
public function set_crumbs() {
$breadcrumb = array();
$breadcrumb = $this->initialize_breadcrumb_schema( $breadcrumb );
$breadcrumb['itemListElement'] = $this->add_breadcrumb_crumbs();
$this->json_schema( apply_filters( 'json_ld_breadcrumb_array', $breadcrumb ) );
}
/**
* Output the ld+json schema markup.
*
* @since 1.0.0
*
* @param Array $schema Array to be converted to json markup.
*/
private function json_schema( $schema ) {
$schema_output = null;
if ( ! empty( $schema ) && is_array( $schema ) ) {
$schema_output .= '<script type="application/ld+json">' . wp_json_encode( $schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) . '</script>';
}
echo $schema_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
}