| 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 |
|