Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ Reuse this design across your site. ([Source](https://github.com/WordPress/guten
- **Supports:** interactivity (clientNavigation), ~~customClassName~~, ~~html~~, ~~inserter~~, ~~renaming~~
- **Attributes:** content, ref

## Breadcrumbs

Display a breadcrumb trail only for Pages, or for hierarchical post types. The block is useful to insert in the Pages template. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/breadcrumbs))

- **Name:** core/breadcrumbs
- **Category:** theme
- **Supports:** color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** separator, showHomeLink

## Button

Prompt visitors to take action with a button-style link. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/button))
Expand Down
3 changes: 2 additions & 1 deletion lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function gutenberg_reregister_core_block_types() {
'archives.php' => 'core/archives',
'avatar.php' => 'core/avatar',
'block.php' => 'core/block',
'breadcrumbs.php' => 'core/breadcrumbs',
'button.php' => 'core/button',
'calendar.php' => 'core/calendar',
'categories.php' => 'core/categories',
Expand Down Expand Up @@ -263,6 +264,7 @@ function gutenberg_register_core_block_assets( $block_name ) {
$stylesheet_path = gutenberg_dir_path() . $style_path . ( is_rtl() ? 'style-rtl.css' : 'style.css' );

if ( file_exists( $stylesheet_path ) ) {

wp_deregister_style( "wp-block-{$block_name}" );
wp_register_style(
"wp-block-{$block_name}",
Expand All @@ -271,7 +273,6 @@ function gutenberg_register_core_block_assets( $block_name ) {
$default_version
);
wp_style_add_data( "wp-block-{$block_name}", 'rtl', 'replace' );

// Add a reference to the stylesheet's path to allow calculations for inlining styles in `wp_head`.
wp_style_add_data( "wp-block-{$block_name}", 'path', $stylesheet_path );
} else {
Expand Down
64 changes: 64 additions & 0 deletions packages/block-library/src/breadcrumbs/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "core/breadcrumbs",
"title": "Breadcrumbs",
"category": "theme",
"description": "Display a breadcrumb trail only for Pages, or for hierarchical post types. The block is useful to insert in the Pages template.",
"textdomain": "default",
"attributes": {
"separator": {
"type": "string",
"default": "/"
},
"showHomeLink": {
"type": "boolean",
"default": true
}
},
"usesContext": [ "postId", "postType" ],
"supports": {
"html": false,
Comment thread
t-hamano marked this conversation as resolved.
Resolved
"spacing": {
"margin": true,
"padding": true
},
"color": {
"gradients": true,
"link": true,
"__experimentalDefaultControls": {
"background": true,
"text": true
}
},
"__experimentalBorder": {
"radius": true,
"color": true,
"width": true,
"style": true,
"__experimentalDefaultControls": {
"radius": false,
"color": true,
"width": true,
"style": true
}
},
"typography": {
"fontSize": true,
"lineHeight": true,
"__experimentalFontFamily": true,
"__experimentalFontWeight": true,
"__experimentalFontStyle": true,
"__experimentalTextTransform": true,
"__experimentalTextDecoration": true,
"__experimentalLetterSpacing": true,
"__experimentalDefaultControls": {
"fontSize": true
}
},
"interactivity": {
"clientNavigation": true
}
},
"style": "wp-block-breadcrumbs"
}
138 changes: 138 additions & 0 deletions packages/block-library/src/breadcrumbs/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import {
ToggleControl,
TextControl,
__experimentalToolsPanel as ToolsPanel,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { RawHTML } from '@wordpress/element';
import { useServerSideRender } from '@wordpress/server-side-render';

/**
* Internal dependencies
*/
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
const separatorDefaultValue = '/';

export default function BreadcrumbEdit( {
attributes,
setAttributes,
context: { postId, postType },
} ) {
const { separator, showHomeLink } = attributes;
const isPostTypeHierarchical = useSelect(
( select ) => {
if ( ! postType ) {
return null;
}
return select( coreStore ).getPostType( postType )?.hierarchical;
},
[ postType ]
);
const blockProps = useBlockProps();
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
const { content } = useServerSideRender( {
attributes,
skipBlockSupportAttributes: true,
block: 'core/breadcrumbs',
urlQueryArgs: { post_id: postId },
} );
let placeholder = null;
// If no post context or the post type is not hierarchical, show placeholder.
// This is fragile because this block is server side rendered and we'll have to
// update the placeholder html if the server side rendering output changes.
if ( ! postId || ! postType || ! isPostTypeHierarchical ) {
const placeholderItems = [
showHomeLink && __( 'Home' ),
__( 'Ancestor' ),
__( 'Parent' ),
].filter( Boolean );
placeholder = (
<nav
style={ {
'--separator': `'${ separator }'`,
} }
inert="true"
>
<ol>
{ placeholderItems.map( ( text, index ) => (
<li key={ index }>
<a href={ `#breadcrumbs-pseudo-link-${ index }` }>
{ text }
</a>
</li>
) ) }
<li>
<span>{ __( 'Current' ) }</span>
</li>
</ol>
</nav>
);
}
return (
<>
<InspectorControls>
<ToolsPanel
label={ __( 'Settings' ) }
resetAll={ () => {
setAttributes( {
separator: separatorDefaultValue,
showHomeLink: true,
} );
} }
dropdownMenuProps={ dropdownMenuProps }
>
<ToolsPanelItem
label={ __( 'Show home link' ) }
isShownByDefault
hasValue={ () => ! showHomeLink }
onDeselect={ () =>
setAttributes( {
showHomeLink: true,
} )
}
>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Show home link' ) }
onChange={ ( value ) =>
setAttributes( { showHomeLink: value } )
}
checked={ showHomeLink }
/>
</ToolsPanelItem>
<ToolsPanelItem
label={ __( 'Separator' ) }
isShownByDefault
hasValue={ () => separator !== separatorDefaultValue }
onDeselect={ () =>
setAttributes( {
separator: separatorDefaultValue,
} )
}
>
<TextControl
__nextHasNoMarginBottom
__next40pxDefaultSize
autoComplete="off"
label={ __( 'Separator' ) }
value={ separator }
onChange={ ( value ) =>
setAttributes( { separator: value } )
}
/>
</ToolsPanelItem>
</ToolsPanel>
</InspectorControls>
<div { ...blockProps }>
{ placeholder || <RawHTML inert="true">{ content }</RawHTML> }
</div>
</>
);
}
22 changes: 22 additions & 0 deletions packages/block-library/src/breadcrumbs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* WordPress dependencies
*/
import { breadcrumbs } from '@wordpress/icons';

/**
* Internal dependencies
*/
import initBlock from '../utils/init-block';
import metadata from './block.json';
import edit from './edit';

const { name } = metadata;

export { metadata, name };

export const settings = {
icon: breadcrumbs,
edit,
};

export const init = () => initBlock( { name, metadata, settings } );
100 changes: 100 additions & 0 deletions packages/block-library/src/breadcrumbs/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Server-side rendering of the `core/breadcrumbs` block.
*
* @package WordPress
*/

/**
* Renders the `core/breadcrumbs` block on the server.
*
* @since 6.9.0
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
*
* @return string Returns the post breadcrumb for hierarchical post types.
*/
function render_block_core_breadcrumbs( $attributes, $content, $block ) {
if ( ! isset( $block->context['postId'] ) || ! isset( $block->context['postType'] ) ) {
return '';
}

$post_id = $block->context['postId'];
$post_type = $block->context['postType'];

// Only render for hierarchical post types.
Comment thread
ntsekouras marked this conversation as resolved.
Resolved
if ( ! is_post_type_hierarchical( $post_type ) ) {
return '';
}

$post = get_post( $post_id );
if ( ! $post ) {
return '';
}

$separator = isset( $attributes['separator'] ) ? $attributes['separator'] : '/';
$show_home_link = isset( $attributes['showHomeLink'] ) ? $attributes['showHomeLink'] : true;
$breadcrumb_items = array();

if ( $show_home_link ) {
$breadcrumb_items[] = sprintf(
'<a href="%s">%s</a>',
esc_url( home_url() ),
esc_html( __( 'Home' ) )
Comment thread
ntsekouras marked this conversation as resolved.
Resolved
);
}

$ancestors = get_post_ancestors( $post_id );
$ancestors = array_reverse( $ancestors );

foreach ( $ancestors as $ancestor_id ) {
$breadcrumb_items[] = sprintf(
'<a href="%s">%s</a>',
esc_url( get_permalink( $ancestor_id ) ),
esc_html( get_the_title( $ancestor_id ) )
Comment thread
ntsekouras marked this conversation as resolved.
Resolved
);
}

// Add current post title (not linked).
$breadcrumb_items[] = sprintf( '<span>%s</span>', esc_html( get_the_title( $post ) ) );
Comment thread
ntsekouras marked this conversation as resolved.
Resolved

$wrapper_attributes = get_block_wrapper_attributes(
array(
'style' => '--separator: "' . esc_attr( $separator ) . '";',
Comment thread
ntsekouras marked this conversation as resolved.
Resolved
'aria-label' => __( 'Breadcrumbs' ),
)
);

$breadcrumb_html = sprintf(
'<nav %s><ol>%s</ol></nav>',
Comment thread
ntsekouras marked this conversation as resolved.
Resolved
$wrapper_attributes,
implode(
'',
array_map(
function ( $item ) {
Comment thread
ntsekouras marked this conversation as resolved.
Resolved
return '<li>' . $item . '</li>';
},
$breadcrumb_items
)
)
);

return $breadcrumb_html;
}

/**
* Registers the `core/breadcrumbs` block on the server.
*
* @since 6.9.0
*/
function register_block_core_breadcrumbs() {
register_block_type_from_metadata(
__DIR__ . '/breadcrumbs',
array(
'render_callback' => 'render_block_core_breadcrumbs',
)
);
}
add_action( 'init', 'register_block_core_breadcrumbs' );
30 changes: 30 additions & 0 deletions packages/block-library/src/breadcrumbs/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.wp-block-breadcrumbs {
// This block has customizable padding, border-box makes that more predictable.
box-sizing: border-box;

ol {
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-wrap: wrap;
align-items: center;
}

li {
margin: 0;
padding: 0;
display: flex;
align-items: center;

&:not(:last-child)::after {
content: var(--separator, "/");
margin: 0 0.5em;
opacity: 0.7;
}
}

span {
color: inherit;
}
}
Loading