| @@ -1,0 +1,753 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\Blocks\Breadcrumb; | |
| 3 | + | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | + exit; | |
| 6 | +} | |
| 7 | + | |
| 8 | +use ABlocks\Classes\BlockBaseAbstract; | |
| 9 | +use ABlocks\Classes\CssGeneratorV2; | |
| 10 | +use ABlocks\Helper; | |
| 11 | +use ABlocks\Controls\Color; | |
| 12 | +use ABlocks\Controls\Typography; | |
| 13 | +use ABlocks\Controls\Alignment; | |
| 14 | +use ABlocks\Controls\Dimensions; | |
| 15 | + | |
| 16 | +class Block extends BlockBaseAbstract { | |
| 17 | + protected $block_name = 'breadcrumb'; | |
| 18 | + | |
| 19 | + public function build_css( $attributes ) { | |
| 20 | + $css_generator = new CssGeneratorV2( $attributes, $this->block_name ); | |
| 21 | + | |
| 22 | + $css_generator->add_class_styles( | |
| 23 | + '{{WRAPPER}} .ablocks-breadcrumbs', | |
| 24 | + $this->get_wrapper_css( $attributes ), | |
| 25 | + $this->get_wrapper_css( $attributes, 'Tablet' ), | |
| 26 | + $this->get_wrapper_css( $attributes, 'Mobile' ), | |
| 27 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_wrapper_css( $attributes, $device ); } ) | |
| 28 | + ); | |
| 29 | + | |
| 30 | + // Normal link color | |
| 31 | + $css_generator->add_class_styles( | |
| 32 | + '{{WRAPPER}} div.ablocks-breadcrumbs span a', | |
| 33 | + $this->get_breadcrumb_normal_title_css( $attributes ), | |
| 34 | + $this->get_breadcrumb_normal_title_css( $attributes, 'Tablet' ), | |
| 35 | + $this->get_breadcrumb_normal_title_css( $attributes, 'Mobile' ), | |
| 36 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_breadcrumb_normal_title_css( $attributes, $device ); } ) | |
| 37 | + ); | |
| 38 | + | |
| 39 | + // Hover link color | |
| 40 | + $css_generator->add_class_styles( | |
| 41 | + '{{WRAPPER}} div.ablocks-breadcrumbs span a:hover', | |
| 42 | + $this->get_breadcrumb_hover_title_css( $attributes ), | |
| 43 | + $this->get_breadcrumb_hover_title_css( $attributes, 'Tablet' ), | |
| 44 | + $this->get_breadcrumb_hover_title_css( $attributes, 'Mobile' ), | |
| 45 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_breadcrumb_hover_title_css( $attributes, $device ); } ) | |
| 46 | + ); | |
| 47 | + | |
| 48 | + // Title (typography + color + direction) | |
| 49 | + $css_generator->add_class_styles( | |
| 50 | + '{{WRAPPER}} .ablocks-breadcrumbs__item', | |
| 51 | + $this->get_breadcrumb_title_css( $attributes ), | |
| 52 | + $this->get_breadcrumb_title_css( $attributes, 'Tablet' ), | |
| 53 | + $this->get_breadcrumb_title_css( $attributes, 'Mobile' ), | |
| 54 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_breadcrumb_title_css( $attributes, $device ); } ) | |
| 55 | + ); | |
| 56 | + | |
| 57 | + // Separator (color + font-size) | |
| 58 | + $css_generator->add_class_styles( | |
| 59 | + '{{WRAPPER}} .ablocks-breadcrumbs__separator', | |
| 60 | + $this->get_breadcrumb_separator_css( $attributes ), | |
| 61 | + $this->get_breadcrumb_separator_css( $attributes, 'Tablet' ), | |
| 62 | + $this->get_breadcrumb_separator_css( $attributes, 'Mobile' ), | |
| 63 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_breadcrumb_separator_css( $attributes, $device ); } ) | |
| 64 | + ); | |
| 65 | + $css_generator->add_class_styles( | |
| 66 | + '{{WRAPPER}} .ablocks-breadcrumbs___before-image , {{WRAPPER}} .ablocks-breadcrumbs__before-text', | |
| 67 | + $this->get_breadcrumb_before_text_image_css( $attributes ), | |
| 68 | + $this->get_breadcrumb_before_text_image_css( $attributes, 'Tablet' ), | |
| 69 | + $this->get_breadcrumb_before_text_image_css( $attributes, 'Mobile' ), | |
| 70 | + $css_generator->custom_device_map( function ( $device ) use ( $attributes ) { return $this->get_breadcrumb_before_text_image_css( $attributes, $device ); } ) | |
| 71 | + ); | |
| 72 | + | |
| 73 | + return $css_generator->generate_css(); | |
| 74 | + | |
| 75 | + } | |
| 76 | + | |
| 77 | + | |
| 78 | + public function get_wrapper_css( $attributes, $device = '' ) { | |
| 79 | + $css = []; | |
| 80 | + | |
| 81 | + if ( ! empty( $attributes['breadcrumbSpaceBetween'] ) ) { | |
| 82 | + $css['gap'] = $attributes['breadcrumbSpaceBetween'] . 'px'; | |
| 83 | + } | |
| 84 | + | |
| 85 | + if ( isset( $attributes['positionBreadcrumb'][ 'value' . $device ] ) ) { | |
| 86 | + $css['justify-content'] = $attributes['positionBreadcrumb'][ 'value' . $device ]; | |
| 87 | + } | |
| 88 | + return array_merge( | |
| 89 | + $css, | |
| 90 | + ); | |
| 91 | + | |
| 92 | + } | |
| 93 | + | |
| 94 | + public function get_breadcrumb_title_css( $attributes, $device = '' ) { | |
| 95 | + $css = []; | |
| 96 | + | |
| 97 | + if ( ! empty( $attributes['breadcrumbTitlecolor'] ) ) { | |
| 98 | + $css['color'] = Color::get_css( $attributes['breadcrumbTitlecolor'] ); | |
| 99 | + } | |
| 100 | + | |
| 101 | + $dir = isset( $attributes['taxonomyTitleDirection'] ) ? $attributes['taxonomyTitleDirection'] : null; | |
| 102 | + $dirCss = ! is_null( $dir ) ? get_alignment_css( $dir, 'justify-content', $device ) : []; | |
| 103 | + if ( ! empty( $attributes['breadcrumbItemBackground'] ) ) { | |
| 104 | + $css['background-color'] = Color::get_css( $attributes['breadcrumbItemBackground'] ); | |
| 105 | + } | |
| 106 | + $typographyValueGlobal = ! empty( $attributes['breadcrumbTitleTypographyGlobal'] ) ? $attributes['breadcrumbTitleTypographyGlobal'] : ''; | |
| 107 | + return array_merge( | |
| 108 | + $css, (array) $dirCss, | |
| 109 | + Typography::get_css( $attributes['breadcrumbTitleTypography'] ?? [], '', $device, $typographyValueGlobal ), | |
| 110 | + Dimensions::get_css( $attributes['breadcrumbItemPadding'] ?? [], 'padding', $device ), | |
| 111 | + Dimensions::get_css( $attributes['BreadcrumbBorderRadius'] ?? [], 'border-radius', $device ), | |
| 112 | + ); | |
| 113 | + } | |
| 114 | + | |
| 115 | + public function get_breadcrumb_normal_title_css( $attributes, $device = '' ) { | |
| 116 | + $css = []; | |
| 117 | + | |
| 118 | + if ( ! empty( $attributes['breadcrumbLinkcolor'] ) ) { | |
| 119 | + $css['color'] = Color::get_css( $attributes['breadcrumbLinkcolor'] ); | |
| 120 | + } | |
| 121 | + | |
| 122 | + return $css; | |
| 123 | + } | |
| 124 | + | |
| 125 | + public function get_breadcrumb_hover_title_css( $attributes, $device = '' ) { | |
| 126 | + $css = []; | |
| 127 | + | |
| 128 | + if ( ! empty( $attributes['breadcrumbHoverLinkcolor'] ) ) { | |
| 129 | + $css['color'] = Color::get_css( $attributes['breadcrumbHoverLinkcolor'] ); | |
| 130 | + } | |
| 131 | + | |
| 132 | + return $css; | |
| 133 | + } | |
| 134 | + | |
| 135 | + public function get_breadcrumb_separator_css( $attributes, $device = '' ) { | |
| 136 | + $css = []; | |
| 137 | + | |
| 138 | + if ( ! empty( $attributes['breadcrumbseparatorcolor'] ) ) { | |
| 139 | + $css['color'] = Color::get_css( $attributes['breadcrumbseparatorcolor'] ); | |
| 140 | + } | |
| 141 | + | |
| 142 | + if ( ! empty( $attributes['breadcrumbseparsize'] ) ) { | |
| 143 | + // IMPORTANT: CSS property is kebab-case | |
| 144 | + $css['font-size'] = $attributes['breadcrumbseparsize'] . 'px'; | |
| 145 | + } | |
| 146 | + | |
| 147 | + return $css; | |
| 148 | + } | |
| 149 | + public function get_breadcrumb_before_text_image_css( $attributes, $device = '' ) { | |
| 150 | + $css = []; | |
| 151 | + | |
| 152 | + if ( ! empty( $attributes['beforeBreadcrumbBackgroundcolor'] ) ) { | |
| 153 | + $css['background-color'] = Color::get_css( $attributes['beforeBreadcrumbBackgroundcolor'] ); | |
| 154 | + } | |
| 155 | + | |
| 156 | + return array_merge( | |
| 157 | + $css, | |
| 158 | + Dimensions::get_css( $attributes['beforeBreadcrumbPaddingcolor'] ?? [], 'padding', $device ), | |
| 159 | + Dimensions::get_css( $attributes['beforeBreadcrumbBorderRadius'] ?? [], 'border-radius', $device ), | |
| 160 | + ); | |
| 161 | + } | |
| 162 | + | |
| 163 | + | |
| 164 | + public function render( $attributes = [] ) { | |
| 165 | + $breadcrumb_normalize = $this->normalize( $attributes ); | |
| 166 | + $breadcrumb_items = $this->build_items( $breadcrumb_normalize ); | |
| 167 | + if ( empty( $breadcrumb_items ) ) { | |
| 168 | + return; | |
| 169 | + } | |
| 170 | + $breadcrumb_items = $this->limit_items( $breadcrumb_items, (int) $breadcrumb_normalize['max'] ); | |
| 171 | + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 172 | + echo $this->render_html_breadcrumbs( $breadcrumb_items, $breadcrumb_normalize ); | |
| 173 | + } | |
| 174 | + | |
| 175 | + /** | |
| 176 | + * Defaults | |
| 177 | + */ | |
| 178 | + private function normalize( $attributes ) { | |
| 179 | + $separator = isset( $attributes['SeparatorChange'] ) && $attributes['SeparatorChange'] !== '' | |
| 180 | + ? (string) $attributes['SeparatorChange'] | |
| 181 | + : '>'; | |
| 182 | + | |
| 183 | + $home_text = ( isset( $attributes['homeBreadcrumbs'] ) && $attributes['homeBreadcrumbs'] !== '' ) | |
| 184 | + ? (string) $attributes['homeBreadcrumbs'] | |
| 185 | + : __( 'Home', 'ablocks' ); | |
| 186 | + | |
| 187 | + $beforeBreadcrumbImage = ( isset( $attributes['beforeBreadcrumbImage'] ) && $attributes['beforeBreadcrumbImage'] !== '' ) | |
| 188 | + ? (string) $attributes['beforeBreadcrumbImage'] | |
| 189 | + : ''; // __( '', 'ablocks' ); | |
| 190 | + | |
| 191 | + $beforeBreadcrumbText = ( isset( $attributes['beforeBreadcrumbText'] ) && $attributes['beforeBreadcrumbText'] !== '' ) | |
| 192 | + ? (string) $attributes['beforeBreadcrumbText'] | |
| 193 | + : __( 'Before Text', 'ablocks' ); | |
| 194 | + | |
| 195 | + $beforeBreadcrumbTextImage = ( isset( $attributes['beforeBreadcrumbTextImage'] ) && $attributes['beforeBreadcrumbTextImage'] !== '' ) | |
| 196 | + ? (string) $attributes['beforeBreadcrumbTextImage'] | |
| 197 | + : ''; // __( '', 'ablocks' ); | |
| 198 | + | |
| 199 | + $beforeTextImage = ( isset( $attributes['beforeTextImage'] ) && $attributes['beforeTextImage'] !== '' ) | |
| 200 | + ? (string) $attributes['beforeTextImage'] | |
| 201 | + : ''; // __( '', 'ablocks' ); | |
| 202 | + | |
| 203 | + $beforeSeparator = ( isset( $attributes['beforeSeparator'] ) && $attributes['beforeSeparator'] !== '' ) | |
| 204 | + ? (string) $attributes['beforeSeparator'] | |
| 205 | + : ''; // __( '', 'ablocks' ); | |
| 206 | + | |
| 207 | + $enable_faq_schema = false; | |
| 208 | + if ( isset( $attributes['enableFaqSchema'] ) ) { | |
| 209 | + $parsed_enable_faq_schema = filter_var( $attributes['enableFaqSchema'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ); | |
| 210 | + $enable_faq_schema = ( null === $parsed_enable_faq_schema ) ? false : $parsed_enable_faq_schema; | |
| 211 | + } | |
| 212 | + | |
| 213 | + return wp_parse_args( (array) $attributes, [ | |
| 214 | + 'separator' => $separator, | |
| 215 | + 'home_text' => $home_text, | |
| 216 | + 'before_breadcrumb_image' => $beforeBreadcrumbImage, | |
| 217 | + 'before_breadcrumb_text' => $beforeBreadcrumbText, | |
| 218 | + 'before_breadcrumb_text_image' => $beforeBreadcrumbTextImage, | |
| 219 | + 'before_text_image' => $beforeTextImage, | |
| 220 | + 'before_separator' => $beforeSeparator, | |
| 221 | + 'enableFaqSchema' => $enable_faq_schema, | |
| 222 | + 'include_current' => true, | |
| 223 | + 'tax' => '', | |
| 224 | + 'max' => 0, | |
| 225 | + ] ); | |
| 226 | + } | |
| 227 | + | |
| 228 | + /** | |
| 229 | + * Detect editor (post/site editor, AJAX, REST block render) | |
| 230 | + */ | |
| 231 | + private function in_editor() : bool { | |
| 232 | + // More precise REST check: block renderer routes | |
| 233 | + if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { | |
| 234 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 235 | + $uri = $_SERVER['REQUEST_URI'] ?? ''; | |
| 236 | + if ( strpos( $uri, '/wp/v2/block-renderer' ) !== false | |
| 237 | + || strpos( $uri, '/wp-block-editor/v1/block-renderer' ) !== false ) { | |
| 238 | + return true; | |
| 239 | + } | |
| 240 | + } | |
| 241 | + | |
| 242 | + if ( is_admin() ) { | |
| 243 | + if ( function_exists( 'get_current_screen' ) ) { | |
| 244 | + $scr = get_current_screen(); | |
| 245 | + if ( $scr ) { | |
| 246 | + if ( ! empty( $scr->is_block_editor ) ) { | |
| 247 | + return true; | |
| 248 | + } | |
| 249 | + if ( isset( $scr->base ) && $scr->base === 'site-editor' ) { | |
| 250 | + return true; | |
| 251 | + } | |
| 252 | + } | |
| 253 | + } | |
| 254 | + if ( function_exists( 'wp_doing_ajax' ) && wp_doing_ajax() ) { | |
| 255 | + return true; | |
| 256 | + } | |
| 257 | + } | |
| 258 | + return false; | |
| 259 | + } | |
| 260 | + | |
| 261 | + /** | |
| 262 | + * Build breadcrumb items (editor-aware) | |
| 263 | + * Each: ['label' => string, 'url' => string|false] | |
| 264 | + */ | |
| 265 | + private function build_items( array $breadcrumb_normalize ) { | |
| 266 | + if ( $this->in_editor() ) { | |
| 267 | + return $this->build_items_editor( $breadcrumb_normalize ); | |
| 268 | + } | |
| 269 | + $breadcrumb_items = []; | |
| 270 | + | |
| 271 | + // Front page | |
| 272 | + if ( is_front_page() ) { | |
| 273 | + $this->add_item( $breadcrumb_items, $breadcrumb_normalize['home_text'] ); | |
| 274 | + return $breadcrumb_items; | |
| 275 | + } | |
| 276 | + | |
| 277 | + // Home | |
| 278 | + $this->add_item( $breadcrumb_items, $breadcrumb_normalize['home_text'], home_url( '/' ) ); | |
| 279 | + | |
| 280 | + // Posts page (static front) | |
| 281 | + if ( is_home() && ! is_front_page() ) { | |
| 282 | + $pid = (int) get_option( 'page_for_posts' ); | |
| 283 | + $this->add_item( $breadcrumb_items, $pid ? get_the_title( $pid ) : __( 'Blog', 'ablocks' ) ); | |
| 284 | + return $breadcrumb_items; | |
| 285 | + } | |
| 286 | + | |
| 287 | + // Singular | |
| 288 | + if ( is_singular() ) { | |
| 289 | + global $post; | |
| 290 | + | |
| 291 | + if ( ! $post ) { | |
| 292 | + if ( ! empty( $breadcrumb_normalize['include_current'] ) ) { | |
| 293 | + $this->add_item( $breadcrumb_items, $this->current_title_safe() ); | |
| 294 | + } | |
| 295 | + return $breadcrumb_items; | |
| 296 | + } | |
| 297 | + | |
| 298 | + $breadcrumb_pt = get_post_type( $post ); | |
| 299 | + $breadcrumb_pto = $breadcrumb_pt ? get_post_type_object( $breadcrumb_pt ) : null; | |
| 300 | + | |
| 301 | + // WooCommerce: prepend Shop for product | |
| 302 | + $is_wc_product = ( 'product' === $breadcrumb_pt ) && function_exists( 'wc_get_page_id' ) && is_singular( 'product' ); | |
| 303 | + if ( $is_wc_product ) { | |
| 304 | + $shop_id = wc_get_page_id( 'shop' ); | |
| 305 | + if ( $shop_id && 'publish' === get_post_status( $shop_id ) ) { | |
| 306 | + $this->add_item( $breadcrumb_items, get_the_title( $shop_id ), get_permalink( $shop_id ) ); | |
| 307 | + } | |
| 308 | + } | |
| 309 | + | |
| 310 | + // Post type label/archive (avoid for product since Shop already added) | |
| 311 | + if ( ! $is_wc_product ) { | |
| 312 | + $this->add_post_type_item( $breadcrumb_items, $breadcrumb_pt, $breadcrumb_pto, [ 'post', 'page' ] ); | |
| 313 | + } | |
| 314 | + | |
| 315 | + // Hierarchical (page / hierarchical CPT) | |
| 316 | + if ( is_page() || ( $breadcrumb_pto && is_post_type_hierarchical( $breadcrumb_pt ) ) ) { | |
| 317 | + $this->add_post_ancestors( $breadcrumb_items, $post ); | |
| 318 | + | |
| 319 | + // Also include taxonomy trail (categories/tags) on single pages if available. | |
| 320 | + $this->append_taxonomy_trail( $breadcrumb_items, $post->ID, $breadcrumb_pt, $breadcrumb_normalize['tax'] ); | |
| 321 | + } else { | |
| 322 | + // Non-hierarchical: taxonomy trail (if any). | |
| 323 | + $this->append_taxonomy_trail( $breadcrumb_items, $post->ID, $breadcrumb_pt, $breadcrumb_normalize['tax'] ); | |
| 324 | + }//end if | |
| 325 | + | |
| 326 | + if ( ! empty( $breadcrumb_normalize['include_current'] ) ) { | |
| 327 | + $this->add_item( $breadcrumb_items, $this->current_title_safe( $post ) ); | |
| 328 | + } | |
| 329 | + return $breadcrumb_items; | |
| 330 | + }//end if | |
| 331 | + | |
| 332 | + // Tax archives | |
| 333 | + if ( is_category() || is_tag() || is_tax() ) { | |
| 334 | + $term = get_queried_object(); | |
| 335 | + return ( $term && ! is_wp_error( $term ) ) | |
| 336 | + ? $this->build_taxonomy_archive_items( $breadcrumb_items, $term ) | |
| 337 | + : $breadcrumb_items; | |
| 338 | + }//end if | |
| 339 | + | |
| 340 | + // Post type archive | |
| 341 | + if ( is_post_type_archive() ) { | |
| 342 | + $breadcrumb_pt = get_query_var( 'post_type' ); | |
| 343 | + if ( is_array( $breadcrumb_pt ) ) { | |
| 344 | + $breadcrumb_pt = reset( $breadcrumb_pt ); } | |
| 345 | + $po = $breadcrumb_pt ? get_post_type_object( $breadcrumb_pt ) : null; | |
| 346 | + if ( $po ) { | |
| 347 | + $this->add_item( $breadcrumb_items, $po->labels->name ); | |
| 348 | + return $breadcrumb_items; | |
| 349 | + } | |
| 350 | + } | |
| 351 | + | |
| 352 | + // Author | |
| 353 | + if ( is_author() ) { | |
| 354 | + $breadcrumb_u = get_queried_object(); | |
| 355 | + if ( $breadcrumb_u ) { | |
| 356 | + $this->add_item( $breadcrumb_items, sprintf( __( 'Author: %s', 'ablocks' ), $breadcrumb_u->display_name ) ); | |
| 357 | + return $breadcrumb_items; | |
| 358 | + } | |
| 359 | + } | |
| 360 | + | |
| 361 | + // Date | |
| 362 | + if ( is_year() || is_month() || is_day() ) { | |
| 363 | + $this->add_item( $breadcrumb_items, get_the_date( is_year() ? 'Y' : ( is_month() ? 'F Y' : 'F j, Y' ) ) ); | |
| 364 | + return $breadcrumb_items; | |
| 365 | + } | |
| 366 | + | |
| 367 | + // Search | |
| 368 | + if ( is_search() ) { | |
| 369 | + $this->add_item( $breadcrumb_items, sprintf( __( 'Search: %s', 'ablocks' ), get_search_query() ) ); | |
| 370 | + return $breadcrumb_items; | |
| 371 | + } | |
| 372 | + | |
| 373 | + // 404 | |
| 374 | + if ( is_404() ) { | |
| 375 | + $this->add_item( $breadcrumb_items, __( '404 Not Found', 'ablocks' ) ); | |
| 376 | + return $breadcrumb_items; | |
| 377 | + } | |
| 378 | + | |
| 379 | + return $breadcrumb_items; | |
| 380 | + } | |
| 381 | + | |
| 382 | + /** | |
| 383 | + * Editor builder (mirrors frontend; robust current title) | |
| 384 | + */ | |
| 385 | + private function build_items_editor( array $breadcrumb_normalize ) : array { | |
| 386 | + $breadcrumb_items = []; | |
| 387 | + $home_label = (string) $breadcrumb_normalize['home_text']; // attributes → fallback "Home" | |
| 388 | + $this->add_item( $breadcrumb_items, $home_label, home_url( '/' ) ); | |
| 389 | + | |
| 390 | + global $post; | |
| 391 | + if ( $post instanceof \WP_Post ) { | |
| 392 | + $breadcrumb_pt = get_post_type( $post ); | |
| 393 | + $breadcrumb_pto = $breadcrumb_pt ? get_post_type_object( $breadcrumb_pt ) : null; | |
| 394 | + | |
| 395 | + if ( is_post_type_hierarchical( $breadcrumb_pt ) ) { | |
| 396 | + $this->add_post_ancestors( $breadcrumb_items, $post ); | |
| 397 | + | |
| 398 | + // Also include taxonomy trail (categories/tags) on single pages if available. | |
| 399 | + $this->append_taxonomy_trail( $breadcrumb_items, $post->ID, $breadcrumb_pt, $breadcrumb_normalize['tax'] ); | |
| 400 | + } else { | |
| 401 | + $this->add_post_type_item( $breadcrumb_items, $breadcrumb_pt, $breadcrumb_pto, [ 'post', 'page' ] ); | |
| 402 | + $this->append_taxonomy_trail( $breadcrumb_items, $post->ID, $breadcrumb_pt, $breadcrumb_normalize['tax'] ); | |
| 403 | + }//end if | |
| 404 | + | |
| 405 | + if ( ! empty( $breadcrumb_normalize['include_current'] ) ) { | |
| 406 | + $this->add_item( $breadcrumb_items, $this->current_title_safe( $post ) ); | |
| 407 | + } | |
| 408 | + return $breadcrumb_items; | |
| 409 | + }//end if | |
| 410 | + | |
| 411 | + // No $post in editor (e.g., site editor template) → sample trail | |
| 412 | + $this->add_item( $breadcrumb_items, __( 'Template', 'ablocks' ), home_url( '/Template/' ) ); | |
| 413 | + if ( ! empty( $breadcrumb_normalize['include_current'] ) ) { | |
| 414 | + $this->add_item( $breadcrumb_items, __( 'Currents Page', 'ablocks' ) ); | |
| 415 | + } | |
| 416 | + return $breadcrumb_items; | |
| 417 | + } | |
| 418 | + | |
| 419 | + /** | |
| 420 | + * Safe current title (fallbacks when get_the_title() empty in editor) | |
| 421 | + */ | |
| 422 | + private function current_title_safe( $p = null ) : string { | |
| 423 | + if ( $p instanceof \WP_Post ) { | |
| 424 | + $breadcrumb_t = get_the_title( $p ); | |
| 425 | + if ( $breadcrumb_t === '' ) { | |
| 426 | + $breadcrumb_t = $p->post_title; | |
| 427 | + } | |
| 428 | + if ( $breadcrumb_t === '' ) { | |
| 429 | + $breadcrumb_t = __( '(no title)', 'ablocks' ); | |
| 430 | + } | |
| 431 | + return $breadcrumb_t; | |
| 432 | + } | |
| 433 | + $breadcrumb_t = single_post_title( '', false ); | |
| 434 | + if ( $breadcrumb_t === '' ) { | |
| 435 | + $breadcrumb_t = __( '(no title)', 'ablocks' ); | |
| 436 | + } | |
| 437 | + return $breadcrumb_t; | |
| 438 | + } | |
| 439 | + | |
| 440 | + /** | |
| 441 | + * Add a breadcrumb item. | |
| 442 | + */ | |
| 443 | + private function add_item( array &$breadcrumb_items, $label, $url = false ) : void { | |
| 444 | + $breadcrumb_items[] = [ | |
| 445 | + 'label' => (string) $label, | |
| 446 | + 'url' => $url | |
| 447 | + ]; | |
| 448 | + } | |
| 449 | + | |
| 450 | + /** | |
| 451 | + * Add a term breadcrumb item (linked). | |
| 452 | + */ | |
| 453 | + private function add_term_item( array &$breadcrumb_items, $term ) : void { | |
| 454 | + if ( ! $term || is_wp_error( $term ) ) { | |
| 455 | + return; | |
| 456 | + } | |
| 457 | + $link = get_term_link( $term ); | |
| 458 | + $this->add_item( $breadcrumb_items, $term->name, is_wp_error( $link ) ? false : $link ); | |
| 459 | + } | |
| 460 | + | |
| 461 | + /** | |
| 462 | + * Add post ancestors (linked). | |
| 463 | + */ | |
| 464 | + private function add_post_ancestors( array &$breadcrumb_items, $post ) : void { | |
| 465 | + foreach ( array_reverse( get_post_ancestors( $post ) ) as $aid ) { | |
| 466 | + if ( (int) $aid === (int) get_option( 'page_on_front' ) ) { | |
| 467 | + continue; | |
| 468 | + } | |
| 469 | + $this->add_item( $breadcrumb_items, get_the_title( $aid ), get_permalink( $aid ) ); | |
| 470 | + } | |
| 471 | + } | |
| 472 | + | |
| 473 | + /** | |
| 474 | + * Add a post type item. | |
| 475 | + */ | |
| 476 | + private function add_post_type_item( array &$breadcrumb_items, $post_type, $post_type_obj = null, array $skip_types = [] ) : bool { | |
| 477 | + if ( $post_type && in_array( $post_type, $skip_types, true ) ) { | |
| 478 | + return false; | |
| 479 | + } | |
| 480 | + $breadcrumb_pto = $post_type_obj ?: ( $post_type ? get_post_type_object( $post_type ) : null ); | |
| 481 | + if ( ! $breadcrumb_pto ) { | |
| 482 | + return false; | |
| 483 | + } | |
| 484 | + $url = ! empty( $breadcrumb_pto->has_archive ) ? get_post_type_archive_link( $post_type ) : false; | |
| 485 | + $this->add_item( $breadcrumb_items, $breadcrumb_pto->labels->name, $url ); | |
| 486 | + return true; | |
| 487 | + } | |
| 488 | + | |
| 489 | + /** | |
| 490 | + * Add term ancestors (linked). | |
| 491 | + */ | |
| 492 | + private function add_term_ancestors( array &$breadcrumb_items, $term ) : void { | |
| 493 | + foreach ( array_reverse( get_ancestors( $term->term_id, $term->taxonomy, 'taxonomy' ) ) as $id ) { | |
| 494 | + $this->add_term_item( $breadcrumb_items, get_term( $id, $term->taxonomy ) ); | |
| 495 | + } | |
| 496 | + } | |
| 497 | + | |
| 498 | + /** | |
| 499 | + * Append taxonomy trail (category + tag when available) to breadcrumb items. | |
| 500 | + * Returns true if any taxonomy items were appended. | |
| 501 | + */ | |
| 502 | + private function append_taxonomy_trail( array &$breadcrumb_items, int $post_id, string $post_type, string $preferred_tax = '' ) : bool { | |
| 503 | + $breadcrumb_tax = $this->preferred_tax( $post_type, $preferred_tax ); | |
| 504 | + if ( ! $breadcrumb_tax ) { | |
| 505 | + return false; | |
| 506 | + } | |
| 507 | + | |
| 508 | + $secondary = ( $preferred_tax === '' ) ? $this->secondary_tax( $post_type, $breadcrumb_tax ) : ''; | |
| 509 | + $taxes = array_filter( array_unique( [ $breadcrumb_tax, $secondary ] ) ); | |
| 510 | + $seen_terms = []; | |
| 511 | + foreach ( $taxes as $taxonomy ) { | |
| 512 | + $primary = $this->primary_term( $post_id, $taxonomy ); | |
| 513 | + if ( ! $primary ) { | |
| 514 | + continue; | |
| 515 | + } | |
| 516 | + | |
| 517 | + foreach ( array_reverse( get_ancestors( $primary->term_id, $primary->taxonomy, 'taxonomy' ) ) as $id ) { | |
| 518 | + $breadcrumb_t = get_term( $id, $primary->taxonomy ); | |
| 519 | + $key = $breadcrumb_t ? $breadcrumb_t->taxonomy . ':' . $breadcrumb_t->term_id : ''; | |
| 520 | + if ( ! $breadcrumb_t || is_wp_error( $breadcrumb_t ) || isset( $seen_terms[ $key ] ) ) { | |
| 521 | + continue; | |
| 522 | + } | |
| 523 | + $seen_terms[ $key ] = true; | |
| 524 | + $this->add_term_item( $breadcrumb_items, $breadcrumb_t ); | |
| 525 | + } | |
| 526 | + $key = $primary->taxonomy . ':' . $primary->term_id; | |
| 527 | + if ( ! isset( $seen_terms[ $key ] ) ) { | |
| 528 | + $seen_terms[ $key ] = true; | |
| 529 | + $this->add_term_item( $breadcrumb_items, $primary ); | |
| 530 | + } | |
| 531 | + }//end foreach | |
| 532 | + return ! empty( $seen_terms ); | |
| 533 | + } | |
| 534 | + | |
| 535 | + /** | |
| 536 | + * Preferred taxonomy | |
| 537 | + */ | |
| 538 | + private function preferred_tax( $post_type, $preferred = '' ) { | |
| 539 | + if ( $preferred && taxonomy_exists( $preferred ) ) { | |
| 540 | + return $preferred; | |
| 541 | + } | |
| 542 | + $taxes = get_object_taxonomies( $post_type, 'objects' ); | |
| 543 | + foreach ( $taxes as $breadcrumb_t ) { | |
| 544 | + if ( $breadcrumb_t->public && $breadcrumb_t->hierarchical ) { | |
| 545 | + return $breadcrumb_t->name; | |
| 546 | + } | |
| 547 | + } | |
| 548 | + foreach ( $taxes as $breadcrumb_t ) { | |
| 549 | + if ( $breadcrumb_t->public ) { | |
| 550 | + return $breadcrumb_t->name; | |
| 551 | + } | |
| 552 | + } | |
| 553 | + return false; | |
| 554 | + } | |
| 555 | + | |
| 556 | + /** | |
| 557 | + * Secondary taxonomy (first public non-hierarchical) when primary is hierarchical. | |
| 558 | + */ | |
| 559 | + private function secondary_tax( $post_type, $primary_tax = '' ) { | |
| 560 | + $primary = $primary_tax ? get_taxonomy( $primary_tax ) : null; | |
| 561 | + if ( $primary && empty( $primary->hierarchical ) ) { | |
| 562 | + return false; | |
| 563 | + } | |
| 564 | + | |
| 565 | + foreach ( get_object_taxonomies( $post_type, 'objects' ) as $breadcrumb_t ) { | |
| 566 | + if ( $breadcrumb_t->public && ! $breadcrumb_t->hierarchical && $breadcrumb_t->name !== $primary_tax ) { | |
| 567 | + return $breadcrumb_t->name; | |
| 568 | + } | |
| 569 | + } | |
| 570 | + return false; | |
| 571 | + } | |
| 572 | + | |
| 573 | + /** | |
| 574 | + * Get a representative post type for a taxonomy. | |
| 575 | + */ | |
| 576 | + private function taxonomy_post_type( $taxonomy ) { | |
| 577 | + $tax = $taxonomy ? get_taxonomy( $taxonomy ) : null; | |
| 578 | + if ( ! $tax || empty( $tax->object_type ) ) { | |
| 579 | + return false; | |
| 580 | + } | |
| 581 | + $object_types = (array) $tax->object_type; | |
| 582 | + if ( in_array( 'listing', $object_types, true ) ) { | |
| 583 | + return 'listing'; | |
| 584 | + } | |
| 585 | + return reset( $object_types ); | |
| 586 | + } | |
| 587 | + | |
| 588 | + /** | |
| 589 | + * Build taxonomy archive items: Post Type > Term ancestors > Term (linked). | |
| 590 | + */ | |
| 591 | + private function build_taxonomy_archive_items( array $breadcrumb_items, $term ) : array { | |
| 592 | + $breadcrumb_pt = $this->taxonomy_post_type( $term->taxonomy ); | |
| 593 | + $breadcrumb_pto = $breadcrumb_pt ? get_post_type_object( $breadcrumb_pt ) : null; | |
| 594 | + $this->add_post_type_item( $breadcrumb_items, $breadcrumb_pt, $breadcrumb_pto ); | |
| 595 | + | |
| 596 | + $this->add_term_ancestors( $breadcrumb_items, $term ); | |
| 597 | + $this->add_term_item( $breadcrumb_items, $term ); | |
| 598 | + | |
| 599 | + return $breadcrumb_items; | |
| 600 | + } | |
| 601 | + | |
| 602 | + /** | |
| 603 | + * Primary term (Yoast) or first term | |
| 604 | + */ | |
| 605 | + private function primary_term( $post_id, $taxonomy ) { | |
| 606 | + if ( class_exists( 'WPSEO_Primary_Term' ) ) { | |
| 607 | + $yoast = new \WPSEO_Primary_Term( $taxonomy, $post_id ); | |
| 608 | + $id = (int) $yoast->get_primary_term(); | |
| 609 | + if ( $id ) { | |
| 610 | + $breadcrumb_t = get_term( $id, $taxonomy ); | |
| 611 | + if ( $breadcrumb_t && ! is_wp_error( $breadcrumb_t ) ) { | |
| 612 | + return $breadcrumb_t; | |
| 613 | + } | |
| 614 | + } | |
| 615 | + } | |
| 616 | + $terms = get_the_terms( $post_id, $taxonomy ); | |
| 617 | + return ( $terms && ! is_wp_error( $terms ) ) ? array_shift( $terms ) : null; | |
| 618 | + } | |
| 619 | + | |
| 620 | + /** | |
| 621 | + * Tail limit | |
| 622 | + */ | |
| 623 | + private function limit_items( array $breadcrumb_items, int $max ) { | |
| 624 | + if ( $max > 0 && count( $breadcrumb_items ) > $max ) { | |
| 625 | + return array_slice( $breadcrumb_items, - $max ); | |
| 626 | + } | |
| 627 | + return $breadcrumb_items; | |
| 628 | + } | |
| 629 | + | |
| 630 | + /** | |
| 631 | + * Best-effort current request URL for schema item fallback. | |
| 632 | + */ | |
| 633 | + private function current_request_url() : string { | |
| 634 | + if ( ! empty( $_SERVER['REQUEST_URI'] ) ) { | |
| 635 | + $request_uri = wp_unslash( $_SERVER['REQUEST_URI'] ); | |
| 636 | + $request_uri = esc_url_raw( $request_uri ); | |
| 637 | + | |
| 638 | + return home_url( $request_uri ); | |
| 639 | + } | |
| 640 | + | |
| 641 | + $permalink = get_permalink(); | |
| 642 | + if ( ! empty( $permalink ) ) { | |
| 643 | + return $permalink; | |
| 644 | + } | |
| 645 | + | |
| 646 | + return home_url( '/' ); | |
| 647 | + } | |
| 648 | + | |
| 649 | + /** | |
| 650 | + * Breadcrumbs-style HTML (matches Breadcrumbs classes; includes microdata) | |
| 651 | + */ | |
| 652 | + private function render_html_breadcrumbs( array $breadcrumb_items, array $breadcrumb_normalize ) { | |
| 653 | + $enable_schema = ! empty( $breadcrumb_normalize['enableFaqSchema'] ); | |
| 654 | + ob_start(); | |
| 655 | + ?> | |
| 656 | + <div aria-label="<?php esc_attr_e( 'breadcrumb', 'ablocks' ); ?>" | |
| 657 | + class="ablocks-breadcrumbs" | |
| 658 | + role="navigation" | |
| 659 | + <?php if ( $enable_schema ) : ?> | |
| 660 | + itemscope | |
| 661 | + itemtype="https://schema.org/BreadcrumbList" | |
| 662 | + <?php endif; ?>> | |
| 663 | + | |
| 664 | + <?php | |
| 665 | + | |
| 666 | + if ( $breadcrumb_normalize['beforeTextImage'] == 'true' ) : ?> | |
| 667 | + <?php if ( $breadcrumb_normalize['beforeBreadcrumbTextImage'] == 'text' ) : ?> | |
| 668 | + <span class="ablocks-breadcrumbs__before-text ablocks-breadcrumbs__item"> | |
| 669 | + <?php echo esc_html( $breadcrumb_normalize['beforeBreadcrumbText'] ); ?> | |
| 670 | + </span> | |
| 671 | + <?php if ( $breadcrumb_normalize['beforeSeparator'] == 'true' ) : ?> | |
| 672 | + <span class="ablocks-breadcrumbs__separator"><?php echo esc_html( $breadcrumb_normalize['separator'] ); ?></span> | |
| 673 | + <?php endif; ?> | |
| 674 | + <?php elseif ( $breadcrumb_normalize['beforeBreadcrumbTextImage'] == 'image' ) : ?> | |
| 675 | + <span class="ablocks-breadcrumbs___before-image"><img src="<?php echo esc_url( $breadcrumb_normalize['before_breadcrumb_image'] ); ?>" alt=""> | |
| 676 | + </span> | |
| 677 | + <?php if ( $breadcrumb_normalize['beforeSeparator'] == 'true' ) : ?> | |
| 678 | + <span class="ablocks-breadcrumbs__separator"><?php echo esc_html( $breadcrumb_normalize['separator'] ); ?></span> | |
| 679 | + <?php endif; ?> | |
| 680 | + <?php endif; ?> | |
| 681 | + <?php endif; ?> | |
| 682 | + | |
| 683 | + <?php foreach ( $breadcrumb_items as $i => $breadcrumb_it ) : ?> | |
| 684 | + <span class="ablocks-breadcrumbs__item" | |
| 685 | + <?php if ( $enable_schema ) : ?> | |
| 686 | + itemprop="itemListElement" | |
| 687 | + itemscope | |
| 688 | + itemtype="https://schema.org/ListItem" | |
| 689 | + <?php endif; ?>> | |
| 690 | + | |
| 691 | + <?php if ( ! empty( $breadcrumb_it['url'] ) ) : ?> | |
| 692 | + <a <?php if ( $enable_schema ) : ?>itemprop="item"<?php endif; ?> href="<?php echo esc_url( $breadcrumb_it['url'] ); ?>"> | |
| 693 | + <span <?php if ( $enable_schema ) : ?>itemprop="name"<?php endif; ?>><?php echo esc_html( $breadcrumb_it['label'] ); ?></span> | |
| 694 | + </a> | |
| 695 | + <?php else : ?> | |
| 696 | + <span class="ablocks-breadcrumbs__item-current" <?php if ( $enable_schema ) : ?>itemprop="name"<?php endif; ?>><?php echo esc_html( $breadcrumb_it['label'] ); ?></span> | |
| 697 | + <?php $current_item_url = $this->current_request_url(); ?> | |
| 698 | + <?php if ( $enable_schema && ! empty( $current_item_url ) ) : ?> | |
| 699 | + <meta itemprop="item" content="<?php echo esc_url( $current_item_url ); ?>" /> | |
| 700 | + <?php endif; ?> | |
| 701 | + <?php endif; ?> | |
| 702 | + <?php if ( $enable_schema ) : ?> | |
| 703 | + <meta itemprop="position" content="<?php echo (int) ( $i + 1 ); ?>" /> | |
| 704 | + <?php endif; ?> | |
| 705 | + </span> | |
| 706 | + <?php if ( $i < count( $breadcrumb_items ) - 1 ) : ?> | |
| 707 | + <span class="ablocks-breadcrumbs__separator"><?php echo esc_html( $breadcrumb_normalize['separator'] ); ?></span> | |
| 708 | + <?php endif; ?> | |
| 709 | + <?php endforeach; ?> | |
| 710 | + </div> | |
| 711 | + <?php | |
| 712 | + return trim( ob_get_clean() ); | |
| 713 | + } | |
| 714 | + | |
| 715 | + | |
| 716 | + /** | |
| 717 | + * Block → PHP SSR entry (use in render_callback) | |
| 718 | + */ | |
| 719 | + public function render_block_content( $attributes, $content = '', $block_instance = null ) { | |
| 720 | + $breadcrumb_normalize = $this->normalize( $attributes ); | |
| 721 | + | |
| 722 | + // --- Ensure correct $post in editor/preview (REST) --- | |
| 723 | + $restore_post = null; | |
| 724 | + | |
| 725 | + if ( $block_instance && ! empty( $block_instance->context['postId'] ) ) { | |
| 726 | + $maybe = get_post( (int) $block_instance->context['postId'] ); | |
| 727 | + | |
| 728 | + if ( $maybe instanceof \WP_Post ) { | |
| 729 | + global $post; | |
| 730 | + $restore_post = $post ?? null; | |
| 731 | + $post = $maybe; | |
| 732 | + setup_postdata( $post ); | |
| 733 | + } | |
| 734 | + } | |
| 735 | + | |
| 736 | + $breadcrumb_items = $this->build_items( $breadcrumb_normalize ); | |
| 737 | + | |
| 738 | + if ( $restore_post !== null ) { | |
| 739 | + wp_reset_postdata(); | |
| 740 | + global $post; | |
| 741 | + $post = $restore_post; | |
| 742 | + } | |
| 743 | + | |
| 744 | + if ( empty( $breadcrumb_items ) ) { | |
| 745 | + return ''; | |
| 746 | + } | |
| 747 | + $breadcrumb_items = $this->limit_items( $breadcrumb_items, (int) $breadcrumb_normalize['max'] ); | |
| 748 | + | |
| 749 | + return $this->render_html_breadcrumbs( $breadcrumb_items, $breadcrumb_normalize ); | |
| 750 | + } | |
| 751 | + | |
| 752 | + | |
| 753 | +} | |