Skip to content

Commit

Permalink
prep build 3/7
Browse files Browse the repository at this point in the history
  • Loading branch information
bph committed Mar 7, 2025
2 parents bb4d50c + acb176e commit e50fa2d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import HierarchicalTermSelector from '../post-taxonomies/hierarchical-term-selec
import { store as editorStore } from '../../store';

function MaybeCategoryPanel() {
const hasNoCategory = useSelect( ( select ) => {
const { hasNoCategory, hasSiteCategories } = useSelect( ( select ) => {
const postType = select( editorStore ).getCurrentPostType();
const { canUser, getEntityRecord } = select( coreStore );
const categoriesTaxonomy = getEntityRecord(
Expand All @@ -39,19 +39,30 @@ function MaybeCategoryPanel() {
select( editorStore ).getEditedPostAttribute(
categoriesTaxonomy.rest_base
);
const siteCategories = postTypeSupportsCategories
? !! select( coreStore ).getEntityRecords( 'taxonomy', 'category', {
exclude: [ defaultCategoryId ],
per_page: 1,
} )?.length
: false;

// This boolean should return true if everything is loaded
// ( categoriesTaxonomy, defaultCategory )
// and the post has not been assigned a category different than "uncategorized".
return (
const noCategory =
!! categoriesTaxonomy &&
!! defaultCategory &&
postTypeSupportsCategories &&
( categories?.length === 0 ||
( categories?.length === 1 &&
defaultCategory?.id === categories[ 0 ] ) )
);
defaultCategory?.id === categories[ 0 ] ) );

return {
hasNoCategory: noCategory,
hasSiteCategories: siteCategories,
};
}, [] );

const [ shouldShowPanel, setShouldShowPanel ] = useState( false );
useEffect( () => {
// We use state to avoid hiding the panel if the user edits the categories
Expand All @@ -61,7 +72,11 @@ function MaybeCategoryPanel() {
}
}, [ hasNoCategory ] );

if ( ! shouldShowPanel ) {
// We only want to show the category panel:
// if the post type supports categories,
// if the site has categories other than the default category,
// and if the post has no other categories than the default category.
if ( ! shouldShowPanel || ! hasSiteCategories ) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,55 @@ const TagsPanel = () => {
};

const MaybeTagsPanel = () => {
const { hasTags, isPostTypeSupported } = useSelect( ( select ) => {
const postType = select( editorStore ).getCurrentPostType();
const tagsTaxonomy = select( coreStore ).getEntityRecord(
'root',
'taxonomy',
'post_tag'
);
const _isPostTypeSupported = tagsTaxonomy?.types?.includes( postType );
const areTagsFetched = tagsTaxonomy !== undefined;
const tags =
tagsTaxonomy &&
select( editorStore ).getEditedPostAttribute(
tagsTaxonomy.rest_base
const { postHasTags, siteHasTags, isPostTypeSupported } = useSelect(
( select ) => {
const postType = select( editorStore ).getCurrentPostType();
const tagsTaxonomy = select( coreStore ).getEntityRecord(
'root',
'taxonomy',
'post_tag'
);
return {
hasTags: !! tags?.length,
isPostTypeSupported: areTagsFetched && _isPostTypeSupported,
};
}, [] );
const [ hadTagsWhenOpeningThePanel ] = useState( hasTags );
const _isPostTypeSupported =
tagsTaxonomy?.types?.includes( postType );
const areTagsFetched = tagsTaxonomy !== undefined;
const tags =
tagsTaxonomy &&
select( editorStore ).getEditedPostAttribute(
tagsTaxonomy.rest_base
);
const siteTags = _isPostTypeSupported
? !! select( coreStore ).getEntityRecords(
'taxonomy',
'post_tag',
{ per_page: 1 }
)?.length
: false;

if ( ! isPostTypeSupported ) {
return {
postHasTags: !! tags?.length,
siteHasTags: siteTags,
isPostTypeSupported: areTagsFetched && _isPostTypeSupported,
};
},
[]
);
const [ hadTagsWhenOpeningThePanel ] = useState( postHasTags );

/**
* We only want to show the tag panel if the post type supports
* tags and the site has tags.
*/
if ( ! isPostTypeSupported || ! siteHasTags ) {
return null;
}

/*
* We only want to show the tag panel if the post didn't have
* any tags when the user hit the Publish button.
*
* We can't use the prop.hasTags because it'll change to true
* We can't use the prop.postHasTags because it'll change to true
* if the user adds a new tag within the pre-publish panel.
* This would force a re-render and a new prop.hasTags check,
* This would force a re-render and a new prop.postHasTags check,
* hiding this panel and keeping the user from adding
* more than one tag.
*/
Expand Down
17 changes: 16 additions & 1 deletion packages/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ This script uses [webpack](https://webpack.js.org/) behind the scenes. It’ll l
This script generates a PHP file containing block metadata from all
`block.json` files in the project. This is useful for enhancing performance
when registering multiple block types, as it allows you to use
`wp_register_block_metadata_collection()` in WordPress.
`wp_register_block_metadata_collection()` and
`wp_register_block_types_from_metadata_collection()` in WordPress.

Usage: `wp-scripts build-blocks-manifest [options]`

Expand Down Expand Up @@ -139,6 +140,20 @@ Using this approach can improve performance when registering multiple block
types, especially for plugins with several custom blocks. Note that this
feature is only available in WordPress 6.7 and later versions.

Alternatively, you can use `wp_register_block_types_from_metadata_collection()`
to have all block types from your plugin automatically registered in the same
function call. This way you no longer need to call `register_block_type()` for
every block type in your plugin.

```php
wp_register_block_types_from_metadata_collection(
plugin_dir_path( __FILE__ ) . 'dist',
plugin_dir_path( __FILE__ ) . 'dist/blocks-manifest.php'
);
```

Note that this feature is only available in WordPress 6.8 and later versions.

### `check-engines`

Checks if the current `node`, `npm` (or `yarn`) versions match the given [semantic version](https://semver.org/) ranges. If the given version is not satisfied, information about installing the needed version is printed and the program exits with an error code.
Expand Down

0 comments on commit e50fa2d

Please sign in to comment.