-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add Term Count block #72218
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 Count block #72218
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
4ac1e41
term count block based on term name block work by @mikachan
cr0ybot 215d460
register term count block
cr0ybot 11f02ab
remove tagName attribute, count alone doesn't make sense as a heading
cr0ybot 991eb68
remove parens inspector controls in favor of block controls, add icons
cr0ybot 754d8d6
refactor based on query-total
cr0ybot d468fad
remove vestigial tag name
cr0ybot ed2e860
remove alignment features and vestigial link styles
cr0ybot 07f23c7
refactor to simplify
cr0ybot ebd2e61
refactor for range of bracket types
cr0ybot 338afdb
sentence case
cr0ybot a670413
sentence case
cr0ybot 7b67d5a
tweak description
cr0ybot 2d91285
remove unnecessary component
cr0ybot 1d66187
cleanup
cr0ybot 4e7aa0e
add icons to BRACKET_TYPES
cr0ybot 32e8c9c
import term count block styles
cr0ybot 64ce3ed
remove experimental flag
cr0ybot d63dc3c
remove redundant svg attributes
cr0ybot 06fcd32
emove unused file
cr0ybot 4e1a786
add fixtures
cr0ybot 83c524f
fix file extension
cr0ybot 0f002ab
try term count icon
cr0ybot 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
Some comments aren't visible on the classic Files Changed page.
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,58 @@ | ||
| { | ||
| "$schema": "https://schemas.wp.org/trunk/block.json", | ||
| "apiVersion": 3, | ||
| "name": "core/term-count", | ||
| "title": "Term Count", | ||
| "category": "theme", | ||
| "description": "Displays the post count of a taxonomy term.", | ||
| "textdomain": "default", | ||
| "usesContext": [ "termId", "taxonomy" ], | ||
| "attributes": { | ||
| "bracketType": { | ||
| "type": "string", | ||
| "enum": [ "none", "round", "square", "curly", "angle" ], | ||
| "default": "round" | ||
| } | ||
| }, | ||
| "supports": { | ||
| "html": false, | ||
| "color": { | ||
| "gradients": true, | ||
| "__experimentalDefaultControls": { | ||
| "background": true, | ||
| "text": true | ||
| } | ||
| }, | ||
| "spacing": { | ||
| "padding": true | ||
| }, | ||
| "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-count" | ||
| } |
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,91 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { useBlockProps, BlockControls } from '@wordpress/block-editor'; | ||
| import { ToolbarDropdownMenu } from '@wordpress/components'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { | ||
| bareNumber, | ||
| numberInParenthesis, | ||
| numberInSquareBrackets, | ||
| numberInCurlyBrackets, | ||
| numberInAngleBrackets, | ||
| } from './icons'; | ||
| import { useTermCount } from './use-term-count'; | ||
|
|
||
| const BRACKET_TYPES = { | ||
| none: { label: __( 'No brackets' ), icon: bareNumber }, | ||
| round: { | ||
| label: __( 'Round brackets' ), | ||
| icon: numberInParenthesis, | ||
| before: '(', | ||
| after: ')', | ||
| }, | ||
| square: { | ||
| label: __( 'Square brackets' ), | ||
| icon: numberInSquareBrackets, | ||
| before: '[', | ||
| after: ']', | ||
| }, | ||
| curly: { | ||
| label: __( 'Curly brackets' ), | ||
| icon: numberInCurlyBrackets, | ||
| before: '{', | ||
| after: '}', | ||
| }, | ||
| angle: { | ||
| label: __( 'Angle brackets' ), | ||
| icon: numberInAngleBrackets, | ||
| before: '<', | ||
| after: '>', | ||
| }, | ||
| }; | ||
|
|
||
| export default function TermCountEdit( { | ||
| attributes, | ||
| setAttributes, | ||
| context: { termId, taxonomy }, | ||
| } ) { | ||
| const { bracketType } = attributes; | ||
| const term = useTermCount( termId, taxonomy ); | ||
|
|
||
| const termCount = term?.termCount || 0; | ||
|
|
||
| const blockProps = useBlockProps(); | ||
|
|
||
| const bracketTypeControls = Object.entries( BRACKET_TYPES ).map( | ||
| ( [ type, { label, icon } ] ) => ( { | ||
| role: 'menuitemradio', | ||
| title: label, | ||
| isActive: bracketType === type, | ||
| icon, | ||
| onClick: () => { | ||
| setAttributes( { bracketType: type } ); | ||
| }, | ||
| } ) | ||
| ); | ||
|
|
||
| const formatTermCount = ( count, type ) => { | ||
| const { before = '', after = '' } = BRACKET_TYPES[ type ] || {}; | ||
| return `${ before }${ count }${ after }`; | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <BlockControls group="block"> | ||
| <ToolbarDropdownMenu | ||
| icon={ BRACKET_TYPES[ bracketType ]?.icon ?? bareNumber } | ||
| label={ __( 'Change bracket type' ) } | ||
| controls={ bracketTypeControls } | ||
| /> | ||
| </BlockControls> | ||
| <div { ...blockProps }> | ||
| { formatTermCount( termCount, bracketType ) } | ||
| </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,34 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { SVG, Path } from '@wordpress/components'; | ||
|
|
||
| export const bareNumber = ( | ||
| <SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> | ||
| <Path d="M 10 6 L 9.609375 9 L 7 9 L 7 10.5 L 9.4121094 10.5 L 9.0878906 13 L 7 13 L 7 14.5 L 8.890625 14.5 L 8.5 17.5 L 10 17.5 L 10.390625 14.5 L 12.890625 14.5 L 12.5 17.5 L 14 17.5 L 14.390625 14.5 L 17 14.5 L 17 13 L 14.587891 13 L 14.912109 10.5 L 17 10.5 L 17 9 L 15.109375 9 L 15.5 6 L 14 6 L 13.609375 9 L 11.109375 9 L 11.5 6 L 10 6 z M 10.912109 10.5 L 13.412109 10.5 L 13.087891 13 L 10.587891 13 L 10.912109 10.5 z" /> | ||
| </SVG> | ||
| ); | ||
|
|
||
| export const numberInParenthesis = ( | ||
| <SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> | ||
| <Path d="M 10,6 9.609375,9 H 7 v 1.5 H 9.4121094 L 9.0878906,13 H 7 v 1.5 H 8.890625 L 8.5,17.5 H 10 l 0.390625,-3 h 2.5 L 12.5,17.5 H 14 l 0.390625,-3 H 17 V 13 h -2.412109 l 0.324218,-2.5 H 17 V 9 H 15.109375 L 15.5,6 H 14 l -0.390625,3 h -2.5 L 11.5,6 Z m 0.912109,4.5 h 2.5 L 13.087891,13 h -2.5 z M 18.5,3 c 0,0 1.5,4.004036 1.5,9 0,4.995964 -1.5,9 -1.5,9 H 20 c 0,0 1.5,-4.004036 1.5,-9 C 21.5,7.004036 20,3 20,3 Z M 5.5,21 C 5.5,21 4,16.995964 4,12 4,7.0040356 5.5,3 5.5,3 H 4 c 0,0 -1.5,4.004036 -1.5,9 0,4.995964 1.5,9 1.5,9 z" /> | ||
| </SVG> | ||
| ); | ||
|
|
||
| export const numberInSquareBrackets = ( | ||
| <SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> | ||
| <Path d="M 21.5,21 V 3 H 18 v 1.5 h 2 v 15 H 18 V 21 Z M 2.5,3 V 21 H 6 V 19.5 H 4 V 4.5 H 6 V 3 Z M 10,6 9.609375,9 H 7 v 1.5 H 9.4121094 L 9.0878906,13 H 7 v 1.5 H 8.890625 L 8.5,17.5 H 10 l 0.390625,-3 h 2.5 L 12.5,17.5 H 14 l 0.390625,-3 H 17 V 13 h -2.412109 l 0.324218,-2.5 H 17 V 9 H 15.109375 L 15.5,6 H 14 l -0.390625,3 h -2.5 L 11.5,6 Z m 0.912109,4.5 h 2.5 L 13.087891,13 h -2.5 z" /> | ||
| </SVG> | ||
| ); | ||
|
|
||
| export const numberInCurlyBrackets = ( | ||
| <SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> | ||
| <Path d="M 10,6 9.609375,9 H 7 v 1.5 H 9.4121094 L 9.0878906,13 H 7 v 1.5 H 8.890625 L 8.5,17.5 H 10 l 0.390625,-3 h 2.5 L 12.5,17.5 H 14 l 0.390625,-3 H 17 V 13 h -2.412109 l 0.324218,-2.5 H 17 V 9 H 15.109375 L 15.5,6 H 14 l -0.390625,3 h -2.5 L 11.5,6 Z m 0.912109,4.5 h 2.5 L 13.087891,13 h -2.5 z M 18.5,21 c 1.104567,0 2,-0.895433 2,-2 v -4 c 0,-1.104567 0.895433,-2 2,-2 v -2 c -1.104567,0 -2,-0.895433 -2,-2 V 5 c 0,-1.104567 -0.895433,-2 -2,-2 H 17 v 1.5 h 1.5 A 0.5,0.5 0 0 1 19,5 v 5 c 0,1.104567 0.895433,2 2,2 -1.104567,0 -2,0.895433 -2,2 v 5 c 0,0.276142 -0.223858,0.5 -0.5,0.5 H 17 V 21 Z M 5.5,3 c -1.1045668,0 -2,0.8954327 -2,2 v 4 c 0,1.104567 -0.8954332,2 -2,2 v 2 c 1.1045668,0 2,0.895433 2,2 v 4 c 0,1.104567 0.8954332,2 2,2 H 7 V 19.5 H 5.5 A 0.5,0.5 0 0 1 5,19 V 14 C 5,12.895433 4.1045668,12 3,12 4.1045668,12 5,11.104567 5,10 V 5 C 5,4.7238579 5.2238579,4.5 5.5,4.5 H 7 V 3 Z" /> | ||
| </SVG> | ||
| ); | ||
|
|
||
| export const numberInAngleBrackets = ( | ||
| <SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> | ||
| <Path d="M 18.970703,16.53125 23.5,12 18.970703,7.46875 17.910156,8.53125 21.378906,12 17.910156,15.46875 Z M 5.0292969,7.46875 0.5,12 5.0292969,16.53125 6.0898438,15.46875 2.6210938,12 6.0898438,8.53125 Z M 10,6 9.609375,9 H 7 v 1.5 H 9.4121094 L 9.0878906,13 H 7 v 1.5 H 8.890625 L 8.5,17.5 H 10 l 0.390625,-3 h 2.5 L 12.5,17.5 H 14 l 0.390625,-3 H 17 V 13 h -2.412109 l 0.324218,-2.5 H 17 V 9 H 15.109375 L 15.5,6 H 14 l -0.390625,3 h -2.5 L 11.5,6 Z m 0.912109,4.5 h 2.5 L 13.087891,13 h -2.5 z" /> | ||
| </SVG> | ||
| ); |
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 { termCount 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,80 @@ | ||
| <?php | ||
| /** | ||
| * Server-side rendering of the `core/term-count` block. | ||
| * | ||
| * @package WordPress | ||
| */ | ||
|
|
||
| /** | ||
| * Renders the `core/term-count` 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 count of the current taxonomy term wrapped inside a heading tag. | ||
| */ | ||
| function render_block_core_term_count( $attributes, $content, $block ) { | ||
| // 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_count = $term->count; | ||
|
|
||
| // Format the term count based on bracket type. | ||
| switch ( $attributes['bracketType'] ) { | ||
| case 'none': | ||
| // No formatting needed. | ||
| break; | ||
| case 'round': | ||
| $term_count = "({$term_count})"; | ||
| break; | ||
| case 'square': | ||
| $term_count = "[{$term_count}]"; | ||
| break; | ||
| case 'curly': | ||
| $term_count = "{{$term_count}}"; | ||
| break; | ||
| case 'angle': | ||
| $term_count = "<{$term_count}>"; | ||
| break; | ||
| default: | ||
| // Default to no formatting for unknown types. | ||
| break; | ||
| } | ||
|
|
||
| $wrapper_attributes = get_block_wrapper_attributes(); | ||
|
|
||
| return sprintf( | ||
| '<div %1$s>%2$s</div>', | ||
| $wrapper_attributes, | ||
| $term_count | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Registers the `core/term-count` block on the server. | ||
| * | ||
| * @since 6.9.0 | ||
| */ | ||
| function register_block_core_term_count() { | ||
| register_block_type_from_metadata( | ||
| __DIR__ . '/term-count', | ||
| array( | ||
| 'render_callback' => 'render_block_core_term_count', | ||
| ) | ||
| ); | ||
| } | ||
| add_action( 'init', 'register_block_core_term_count' ); |
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,6 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { init } from './'; | ||
|
|
||
| export default init(); |
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-count { | ||
| // 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.