Skip to content
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

Try: Filter Pattern Categories by Editor Type #47275

Closed
wants to merge 6 commits into from
Closed
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
14 changes: 11 additions & 3 deletions lib/compat/wordpress-6.2/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
* Registers the block pattern categories.
*/
function gutenberg_register_core_block_patterns_categories() {
// Register universal block pattern categories
// Available in post and site editor.
register_block_pattern_category(
'banner',
array(
'label' => _x( 'Banners', 'Block pattern category', 'gutenberg' ),
'label' => _x( 'Banners', 'Block pattern category', 'gutenberg' ),
'description' => __( 'An element that helps structure or contrast the contents of a page.', 'gutenberg' ),
)
);
register_block_pattern_category(
Expand Down Expand Up @@ -51,7 +54,8 @@ function gutenberg_register_core_block_patterns_categories() {
)
);

// Register new core block pattern categories.
// Register new universal core block pattern categories.
// Available in post and site editor.
register_block_pattern_category(
'call-to-action',
array(
Expand Down Expand Up @@ -122,19 +126,23 @@ function gutenberg_register_core_block_patterns_categories() {
'description' => __( 'Display your latest posts in lists, grids or other layouts.', 'gutenberg' ),
)
);
// Site building pattern categories.

// Register site building pattern categories.
// Available in site editor.
register_block_pattern_category(
'footer',
array(
'label' => _x( 'Footers', 'Block pattern category', 'gutenberg' ),
'description' => __( 'A variety of footer designs displaying information and site navigation.', 'gutenberg' ),
'editors' => array( 'core/site' ),
)
);
register_block_pattern_category(
'header',
array(
'label' => _x( 'Headers', 'Block pattern category', 'gutenberg' ),
'description' => __( 'A variety of header designs displaying your site title and navigation.', 'gutenberg' ),
'editors' => array( 'core/site' ),
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Gutenberg_REST_Block_Pattern_Categories_Controller extends WP_REST_Block_P
*/
public function prepare_item_for_response( $item, $request ) {
$fields = $this->get_fields_for_response( $request );
$keys = array( 'name', 'label', 'description' );
$keys = array( 'name', 'label', 'description', 'editors' );
$data = array();
foreach ( $keys as $key ) {
if ( isset( $item[ $key ] ) && rest_is_field_included( $key, $fields ) ) {
Expand Down Expand Up @@ -70,6 +70,12 @@ public function get_item_schema() {
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
'editors' => array(
'description' => __( 'The editors this category is allowed to be shown on.', 'gutenberg' ),
'type' => 'array',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,17 @@ function useBlockEditorSettings( settings, hasTemplate ) {
[
...( settingsBlockPatternCategories || [] ),
...( restBlockPatternCategories || [] ),
].filter(
( x, index, arr ) =>
index === arr.findIndex( ( y ) => x.name === y.name )
),
]
.filter(
( x, index, arr ) =>
index === arr.findIndex( ( y ) => x.name === y.name )
)
.filter( ( cat ) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With only a check like that, any pattern that belongs to such categories(ex footers) will still be present in post editor, but under uncategorized.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, thanks for highlighting!

if ( ! cat?.editors?.length ) {
return true;
}
return cat.editors.includes( 'core/post' );
} ),
[ settingsBlockPatternCategories, restBlockPatternCategories ]
);

Expand Down