Repository navigation
Blocks: Add Breadcrumbs block #71793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d6f7f65
Block Library: Add breadcrumb block
youknowriad a12b89f
add show home page link and fix css
ntsekouras 72430d6
rename block to post breadcrumbs
ntsekouras b82bc99
fix lint php
ntsekouras d1cb5c0
feedback part 1
ntsekouras eb832b2
feedback
ntsekouras f0f1b86
Apply suggestions from code review
ntsekouras d625a21
rename to post breadcrumbs
ntsekouras a8653e1
add aria-label and inert
ntsekouras 1a94486
php lint
ntsekouras e02e921
add terms breadcrumbs variation
ntsekouras 4ecd95a
fix what taxonomy to use
ntsekouras 18bdf8b
feedback
ntsekouras 2283177
Revert "add terms breadcrumbs variation"
ntsekouras 920f743
tiny var rename
ntsekouras fb2d9f0
add breadcrumbs icon with the new way
ntsekouras e977d63
rename to breadcrumbs
ntsekouras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| "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" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | ||
| </> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
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' ) ) | ||
|
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 ) ) | ||
|
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 ) ) ); | ||
|
ntsekouras marked this conversation as resolved.
Resolved
|
||
|
|
||
| $wrapper_attributes = get_block_wrapper_attributes( | ||
| array( | ||
| 'style' => '--separator: "' . esc_attr( $separator ) . '";', | ||
|
ntsekouras marked this conversation as resolved.
Resolved
|
||
| 'aria-label' => __( 'Breadcrumbs' ), | ||
| ) | ||
| ); | ||
|
|
||
| $breadcrumb_html = sprintf( | ||
| '<nav %s><ol>%s</ol></nav>', | ||
|
ntsekouras marked this conversation as resolved.
Resolved
|
||
| $wrapper_attributes, | ||
| implode( | ||
| '', | ||
| array_map( | ||
| function ( $item ) { | ||
|
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' ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.