-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add Term Name block #72129
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
Add Term Name block #72129
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
75c3aed
Add initial block
mikachan 324f6fc
Add tagName attribute
mikachan c3549bc
Fix phpcs
mikachan dda7d42
Update packages/block-library/src/term-name/block.json
mikachan ffdefaa
Add border-box
mikachan 4ffb901
Remove term from isLink render logic
mikachan 7817e35
Update HTML element selector
mikachan 1319080
Switch to HeadingLevelDropdown
mikachan a4f201c
Remove border radius and margin controls
mikachan e8fb069
Use pseudo link
mikachan 78a1946
Allow block to be used outside of terms query
mikachan f1205da
Remove apply_filters
mikachan af78593
Rename tagName to level
mikachan f7e3c59
Add apply_filters back
mikachan 2bc9f9a
Merge branch 'trunk' into add/term-name-block
mikachan 8cacf01
Add new icon
mikachan 7eccb17
Remove fill from svg
mikachan e59e153
Add term title keyword
mikachan 87e8f77
Remove experimental flag
mikachan cb608e1
Fix spacing
mikachan 3e12e66
Remove apply_filters
mikachan 76f35a8
Add test fixture
mikachan 2644aa2
Add style import
mikachan c334cd5
Update svg
mikachan bce4117
Merge branch 'trunk' into add/term-name-block
mikachan 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
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,68 @@ | ||
| { | ||
| "$schema": "https://schemas.wp.org/trunk/block.json", | ||
| "apiVersion": 3, | ||
| "name": "core/term-name", | ||
| "title": "Term Name", | ||
| "category": "theme", | ||
| "description": "Displays the name of a taxonomy term.", | ||
| "keywords": [ "term title" ], | ||
| "textdomain": "default", | ||
| "usesContext": [ "termId", "taxonomy" ], | ||
| "attributes": { | ||
| "textAlign": { | ||
t-hamano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "type": "string" | ||
| }, | ||
| "level": { | ||
| "type": "number", | ||
| "default": 0 | ||
| }, | ||
| "isLink": { | ||
| "type": "boolean", | ||
| "default": false | ||
| } | ||
| }, | ||
| "supports": { | ||
| "align": [ "wide", "full" ], | ||
| "html": false, | ||
| "color": { | ||
| "gradients": true, | ||
| "link": true, | ||
| "__experimentalDefaultControls": { | ||
| "background": true, | ||
| "text": true, | ||
| "link": true | ||
| } | ||
| }, | ||
| "spacing": { | ||
| "padding": true | ||
t-hamano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| "typography": { | ||
| "fontSize": true, | ||
| "lineHeight": true, | ||
| "__experimentalFontFamily": true, | ||
| "__experimentalFontWeight": true, | ||
| "__experimentalFontStyle": true, | ||
| "__experimentalTextTransform": true, | ||
| "__experimentalTextDecoration": true, | ||
| "__experimentalLetterSpacing": true, | ||
| "__experimentalDefaultControls": { | ||
| "fontSize": true | ||
| } | ||
| }, | ||
| "interactivity": { | ||
| "clientNavigation": true | ||
| }, | ||
| "__experimentalBorder": { | ||
| "radius": true, | ||
| "color": true, | ||
| "width": true, | ||
| "style": true, | ||
| "__experimentalDefaultControls": { | ||
| "color": true, | ||
| "width": true, | ||
| "style": true | ||
| } | ||
| } | ||
| }, | ||
| "style": "wp-block-term-name" | ||
| } | ||
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,108 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import clsx from 'clsx'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { | ||
| useBlockProps, | ||
| BlockControls, | ||
| AlignmentControl, | ||
| InspectorControls, | ||
| HeadingLevelDropdown, | ||
| } from '@wordpress/block-editor'; | ||
| import { | ||
| ToggleControl, | ||
| __experimentalToolsPanel as ToolsPanel, | ||
| __experimentalToolsPanelItem as ToolsPanelItem, | ||
| } from '@wordpress/components'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { useToolsPanelDropdownMenuProps } from '../utils/hooks'; | ||
| import { useTermName } from './use-term-name'; | ||
|
|
||
| export default function TermNameEdit( { | ||
| attributes, | ||
| setAttributes, | ||
| context: { termId, taxonomy }, | ||
| } ) { | ||
| const { textAlign, level = 0, isLink } = attributes; | ||
| const { term } = useTermName( termId, taxonomy ); | ||
|
|
||
| const termName = term?.name || __( 'Term Name' ); | ||
|
|
||
| const blockProps = useBlockProps( { | ||
| className: clsx( { | ||
| [ `has-text-align-${ textAlign }` ]: textAlign, | ||
| } ), | ||
| } ); | ||
|
|
||
| const dropdownMenuProps = useToolsPanelDropdownMenuProps(); | ||
|
|
||
| const TagName = level === 0 ? 'p' : `h${ level }`; | ||
|
|
||
| let termNameDisplay = termName; | ||
| if ( isLink ) { | ||
| termNameDisplay = ( | ||
| <a | ||
| href="#term-name-pseudo-link" | ||
| onClick={ ( e ) => e.preventDefault() } | ||
| > | ||
| { termName } | ||
| </a> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| <BlockControls group="block"> | ||
| <HeadingLevelDropdown | ||
| value={ level } | ||
| options={ [ 0, 1, 2, 3, 4, 5, 6 ] } | ||
| onChange={ ( newLevel ) => { | ||
| setAttributes( { level: newLevel } ); | ||
| } } | ||
| /> | ||
| <AlignmentControl | ||
| value={ textAlign } | ||
| onChange={ ( nextAlign ) => { | ||
| setAttributes( { textAlign: nextAlign } ); | ||
| } } | ||
| /> | ||
| </BlockControls> | ||
| <InspectorControls> | ||
| <ToolsPanel | ||
| label={ __( 'Settings' ) } | ||
| resetAll={ () => { | ||
| setAttributes( { | ||
| isLink: false, | ||
| } ); | ||
| } } | ||
| dropdownMenuProps={ dropdownMenuProps } | ||
| > | ||
| <ToolsPanelItem | ||
| hasValue={ () => !! isLink } | ||
| label={ __( 'Make term name a link' ) } | ||
| onDeselect={ () => setAttributes( { isLink: false } ) } | ||
| isShownByDefault | ||
| > | ||
| <ToggleControl | ||
| __nextHasNoMarginBottom | ||
| label={ __( 'Make term name a link' ) } | ||
| onChange={ () => | ||
| setAttributes( { isLink: ! isLink } ) | ||
| } | ||
| checked={ isLink } | ||
| /> | ||
| </ToolsPanelItem> | ||
| </ToolsPanel> | ||
| </InspectorControls> | ||
| <TagName { ...blockProps }>{ termNameDisplay }</TagName> | ||
| </> | ||
| ); | ||
| } |
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,21 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { termName as icon } 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, | ||
| 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,81 @@ | ||
| <?php | ||
| /** | ||
| * Server-side rendering of the `core/term-name` block. | ||
| * | ||
| * @package WordPress | ||
| */ | ||
|
|
||
| /** | ||
| * Renders the `core/term-name` 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 name of the current taxonomy term wrapped inside a heading tag. | ||
| */ | ||
| function render_block_core_term_name( $attributes, $content, $block ) { | ||
| $term_name = ''; | ||
|
|
||
| // Get term from context or from the current query. | ||
| if ( isset( $block->context['termId'] ) && isset( $block->context['taxonomy'] ) ) { | ||
| $term = get_term( $block->context['termId'], $block->context['taxonomy'] ); | ||
| } else { | ||
| $term = get_queried_object(); | ||
| if ( ! $term instanceof WP_Term ) { | ||
| $term = null; | ||
| } | ||
| } | ||
|
|
||
| if ( ! $term || is_wp_error( $term ) ) { | ||
| return ''; | ||
| } | ||
|
|
||
| $term_name = $term->name; | ||
| $level = isset( $attributes['level'] ) ? $attributes['level'] : 0; | ||
| $tag_name = 0 === $level ? 'p' : 'h' . (int) $level; | ||
|
|
||
| if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) { | ||
| $term_link = get_term_link( $term ); | ||
| if ( ! is_wp_error( $term_link ) ) { | ||
| $term_name = sprintf( | ||
| '<a href="%1$s">%2$s</a>', | ||
| esc_url( $term_link ), | ||
| $term_name | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| $classes = array(); | ||
| if ( isset( $attributes['textAlign'] ) ) { | ||
| $classes[] = 'has-text-align-' . $attributes['textAlign']; | ||
| } | ||
| if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) { | ||
| $classes[] = 'has-link-color'; | ||
| } | ||
| $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); | ||
|
|
||
| return sprintf( | ||
| '<%1$s %2$s>%3$s</%1$s>', | ||
| $tag_name, | ||
| $wrapper_attributes, | ||
| $term_name | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Registers the `core/term-name` block on the server. | ||
| * | ||
| * @since 6.9.0 | ||
| */ | ||
| function register_block_core_term_name() { | ||
| register_block_type_from_metadata( | ||
| __DIR__ . '/term-name', | ||
| array( | ||
| 'render_callback' => 'render_block_core_term_name', | ||
| ) | ||
| ); | ||
| } | ||
| add_action( 'init', 'register_block_core_term_name' ); |
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,4 @@ | ||
| .wp-block-term-name { | ||
t-hamano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // This block has customizable padding, border-box makes that more predictable. | ||
| box-sizing: border-box; | ||
| } | ||
Oops, something went wrong.
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.