PluginProbe
bBlocks – Essential Gutenberg Blocks & Patterns Collection / trunk
bBlocks – Essential Gutenberg Blocks & Patterns Collection vtrunk
2.1.6 2.1.5 2.1.4 2.1.3 2.1.2 2.1.1 2.1.0 2.0.43 2.0.42 2.0.41 2.0.40 2.0.39 2.0.38 trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 106 releases
b-blocks / build / breadcrumb / render.php

render.php in bBlocks – Essential Gutenberg Blocks & Patterns Collection trunk, at build/breadcrumb/render.php

221 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 $id = wp_unique_id( 'bBlocksBreadcrumb-' );
3
4 if ( ! function_exists( 'bBlocks_breadcrumb_build_crumbs' ) ) {
5
6 function bBlocks_breadcrumb_build_crumbs( $show_home, $home_label ) {
7 $crumbs = array();
8
9 if ( $show_home ) {
10 $crumbs[] = array(
11 'label' => $home_label,
12 'url' => home_url( '/' ),
13 );
14 }
15
16 if ( is_front_page() ) {
17 if ( ! empty( $crumbs ) ) {
18 $crumbs[ count( $crumbs ) - 1 ]['url'] = '';
19 }
20 return $crumbs;
21 }
22
23 if ( is_home() ) {
24 $blog_page_id = get_option( 'page_for_posts' );
25 if ( $blog_page_id ) {
26 $crumbs[] = array(
27 'label' => get_the_title( $blog_page_id ),
28 'url' => '',
29 );
30 } else {
31 $crumbs[] = array(
32 'label' => __( 'Blog', 'b-blocks' ),
33 'url' => '',
34 );
35 }
36 return $crumbs;
37 }
38
39 if ( is_404() ) {
40 $crumbs[] = array(
41 'label' => __( '404 Not Found', 'b-blocks' ),
42 'url' => '',
43 );
44 return $crumbs;
45 }
46
47 if ( is_search() ) {
48 $crumbs[] = array(
49 'label' => sprintf( __( 'Search: %s', 'b-blocks' ), get_search_query() ),
50 'url' => '',
51 );
52 return $crumbs;
53 }
54
55 if ( is_author() ) {
56 $crumbs[] = array(
57 'label' => sprintf( __( 'Author: %s', 'b-blocks' ), get_the_author() ),
58 'url' => '',
59 );
60 return $crumbs;
61 }
62
63 if ( is_date() ) {
64 if ( is_year() ) {
65 $crumbs[] = array(
66 'label' => get_the_date( 'Y' ),
67 'url' => '',
68 );
69 } elseif ( is_month() ) {
70 $crumbs[] = array(
71 'label' => get_the_date( 'Y' ),
72 'url' => get_year_link( get_the_date( 'Y' ) ),
73 );
74 $crumbs[] = array(
75 'label' => get_the_date( 'F' ),
76 'url' => '',
77 );
78 } elseif ( is_day() ) {
79 $crumbs[] = array(
80 'label' => get_the_date( 'Y' ),
81 'url' => get_year_link( get_the_date( 'Y' ) ),
82 );
83 $crumbs[] = array(
84 'label' => get_the_date( 'F' ),
85 'url' => get_month_link( get_the_date( 'Y' ), get_the_date( 'm' ) ),
86 );
87 $crumbs[] = array(
88 'label' => get_the_date( 'j' ),
89 'url' => '',
90 );
91 }
92 return $crumbs;
93 }
94
95 if ( is_category() || is_tag() || is_tax() ) {
96 $term = get_queried_object();
97 if ( $term && ! is_wp_error( $term ) ) {
98 // Add parent terms.
99 if ( $term->parent ) {
100 $ancestors = get_ancestors( $term->term_id, $term->taxonomy );
101 $ancestors = array_reverse( $ancestors );
102 foreach ( $ancestors as $ancestor_id ) {
103 $ancestor = get_term( $ancestor_id, $term->taxonomy );
104 if ( $ancestor && ! is_wp_error( $ancestor ) ) {
105 $crumbs[] = array(
106 'label' => $ancestor->name,
107 'url' => get_term_link( $ancestor ),
108 );
109 }
110 }
111 }
112 $crumbs[] = array(
113 'label' => $term->name,
114 'url' => '',
115 );
116 }
117 return $crumbs;
118 }
119
120 if ( is_post_type_archive() ) {
121 $post_type_obj = get_queried_object();
122 if ( $post_type_obj ) {
123 $crumbs[] = array(
124 'label' => $post_type_obj->label,
125 'url' => '',
126 );
127 }
128 return $crumbs;
129 }
130
131 if ( is_singular() ) {
132 $post = get_queried_object();
133
134 if ( 'post' === $post->post_type ) {
135 $categories = get_the_category( $post->ID );
136 if ( ! empty( $categories ) ) {
137 $category = $categories[0];
138 // Add parent categories.
139 if ( $category->parent ) {
140 $ancestors = get_ancestors( $category->term_id, 'category' );
141 $ancestors = array_reverse( $ancestors );
142 foreach ( $ancestors as $ancestor_id ) {
143 $ancestor = get_term( $ancestor_id, 'category' );
144 if ( $ancestor && ! is_wp_error( $ancestor ) ) {
145 $crumbs[] = array(
146 'label' => $ancestor->name,
147 'url' => get_term_link( $ancestor ),
148 );
149 }
150 }
151 }
152 $crumbs[] = array(
153 'label' => $category->name,
154 'url' => get_term_link( $category ),
155 );
156 }
157 } elseif ( 'page' !== $post->post_type ) {
158 // Custom post type: add archive link.
159 $post_type_obj = get_post_type_object( $post->post_type );
160 if ( $post_type_obj && $post_type_obj->has_archive ) {
161 $crumbs[] = array(
162 'label' => $post_type_obj->labels->name,
163 'url' => get_post_type_archive_link( $post->post_type ),
164 );
165 }
166 }
167
168 // Page ancestors.
169 if ( 'page' === $post->post_type && $post->post_parent ) {
170 $ancestors = get_ancestors( $post->ID, 'page' );
171 $ancestors = array_reverse( $ancestors );
172 foreach ( $ancestors as $ancestor_id ) {
173 $crumbs[] = array(
174 'label' => get_the_title( $ancestor_id ),
175 'url' => get_permalink( $ancestor_id ),
176 );
177 }
178 }
179
180 $crumbs[] = array(
181 'label' => get_the_title( $post->ID ),
182 'url' => '',
183 );
184 return $crumbs;
185 }
186
187 return $crumbs;
188 }
189 }
190 $show_home = $attributes['options']['isHomeShow'];
191 $home_label = $attributes['home']['label'];
192
193 $crumbs = bBlocks_breadcrumb_build_crumbs( $show_home, $home_label );
194
195 $schema = null;
196 if ( ! empty( $attributes['options']['isSchema'] ) ) {
197 $schema_items = array();
198 $pos = 1;
199 foreach ( $crumbs as $c ) {
200 $item = array(
201 '@type' => 'ListItem',
202 'position' => $pos++,
203 'name' => $c['label'],
204 );
205 if ( ! empty( $c['url'] ) ) {
206 $item['item'] = $c['url'];
207 }
208 $schema_items[] = $item;
209 }
210
211 $schema = array(
212 '@context' => 'https://schema.org',
213 '@type' => 'BreadcrumbList',
214 'itemListElement' => $schema_items,
215 );
216 }
217
218 ?>
219 <div <?php echo get_block_wrapper_attributes(); ?> id='<?php echo esc_attr( $id ); ?>' data-attributes='<?php echo esc_attr( wp_json_encode( $attributes ) ); ?>' data-crumbs='<?php echo esc_attr( wp_json_encode( $crumbs ) ); ?>' data-schema='<?php echo esc_attr( wp_json_encode( $schema ) ); ?>' ></div>
220
221