PluginProbe
Breadcrumb Block / trunk
Breadcrumb Block vtrunk
trunk 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1
breadcrumb-block / breadcrumb-block.php

breadcrumb-block.php in Breadcrumb Block trunk, at breadcrumb-block.php

83 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Breadcrumb Block
4 * Description: A simple breadcrumb trail block that supports JSON-LD structured data and is compatible with Woocommerce
5 * Requires at least: 5.9
6 * Requires PHP: 7.0
7 * Version: 1.1.1
8 * Author: Phi Phan
9 * Author URI: https://boldblocks.net
10 * Plugin URI: https://boldblocks.net?utm_source=Breadcrumb+Block&utm_campaign=visit+site&utm_medium=link&utm_content=Plugin+URI
11 * License: GPL-2.0-or-later
12 *
13 * @package BoldBlocks
14 * @copyright Copyright(c) 2022, Phi Phan
15 */
16
17 namespace BreadcrumbBlock;
18
19 // Exit if accessed directly.
20 defined( 'ABSPATH' ) || exit;
21
22 // Load required file.
23 require_once __DIR__ . '/includes/breadcrumbs.php';
24
25 /**
26 * Registers the block using the metadata loaded from the `block.json` file.
27 * Behind the scenes, it registers also all assets so they can be enqueued
28 * through the block editor in the corresponding context.
29 *
30 * @see https://developer.wordpress.org/reference/functions/register_block_type/
31 */
32 function breadcrumb_block_block_init() {
33 register_block_type( __DIR__ . '/build', [ 'render_callback' => __NAMESPACE__ . '\\breadcrumb_block_render_block' ] );
34 }
35 add_action( 'init', __NAMESPACE__ . '\\breadcrumb_block_block_init' );
36
37 /**
38 * Renders the `boldblocks/breadcrumb-block` block on the server.
39 *
40 * @param array $attributes Block attributes.
41 * @param string $content Block default content.
42 * @param WP_Block $block Block instance.
43 * @return string
44 */
45 function breadcrumb_block_render_block( $attributes, $content, $block ) {
46 // Get labels.
47 $labels = $attributes['labels'] ?? [];
48 if ( ! empty( $attributes['homeText'] ) && empty( $labels['home'] ) ) {
49 $labels['home'] = $attributes['homeText'];
50 }
51
52 $content = Breadcrumbs::get_instance()->get_breadcrumb_trail(
53 [
54 'separator' => $attributes['separator'] ?? '',
55 'labels' => $labels,
56 ]
57 );
58
59 $vars = [];
60 if ( isset( $attributes['gap'] ) ) {
61 $vars[] = '--bb--crumb-gap:' . $attributes['gap'] . ';';
62 }
63
64 $style = count( $vars ) > 0 ? \implode( '', $vars ) : '';
65
66 $block_classes = [];
67 if ( $attributes['hideHomePage'] ?? false ) {
68 $block_classes[] = 'hide-home-page';
69 }
70 if ( $attributes['hideCurrentPage'] ?? false ) {
71 $block_classes[] = 'hide-current-page';
72 }
73
74 $wrapper_attributes = get_block_wrapper_attributes(
75 array(
76 'style' => $style,
77 'class' => implode( ' ', $block_classes ),
78 )
79 );
80
81 return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $content );
82 }
83