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

Blocks: Add a way to manage block categories #7606

Merged
merged 3 commits into from Jun 28, 2018
Merged

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented Jun 28, 2018

Description

Supersedes #1732.

With this PR, It is possible to filter the list of default block categories using the block_categories filter. You can do it on the server by implementing a function which returns a list of categories. It is going to be used during blocks registration and to group blocks in the inserter. You can also use the second provided param $post to generate a different list depending on the post's content.

add_filter( 'block_categories', function( $categories, $post ) {
	if ( $post->post_type !== 'post' ) {
		return $categories;
	}
	return array_merge(
		$categories,
		array(
			array(
				'slug' => 'my-category',
				'title' => __( 'My category', 'my-plugin' ),
			),
		)
	);
}, 10, 2 );

How has this been tested?

Renamed the category of one of the blocks to my-category and included the code shared above to activate this category.

I also tried what happens when some existing categories get removed. You see warnings (expected non-destructive errors triggered by our code) on the console informing that blocks couldn't be registered because a given category doesn't exist. Which is more than expected.

Screenshots

screen shot 2018-06-28 at 15 34 33

Types of changes

New feature (non-breaking change which adds functionality)

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@gziolo gziolo added [Feature] Blocks Overall functionality of blocks [Feature] Extensibility The ability to extend blocks or the editing experience labels Jun 28, 2018
@gziolo gziolo added this to the 3.2 milestone Jun 28, 2018
@gziolo gziolo self-assigned this Jun 28, 2018
@gziolo gziolo added this to To do in Extensibility via automation Jun 28, 2018
@gziolo gziolo requested review from mcsf, mtias and a team June 28, 2018 14:08
@gziolo gziolo moved this from To do to In progress in Extensibility Jun 28, 2018
@gziolo gziolo force-pushed the add/set-categories-filter branch 2 times, most recently from 73b600b to 979c13a Compare June 28, 2018 14:40
@gziolo gziolo force-pushed the add/set-categories-filter branch from 979c13a to 5dd18a5 Compare June 28, 2018 14:42
Copy link
Contributor

@mcsf mcsf left a comment

Choose a reason for hiding this comment

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

LGTM!

It is possible to filter the list of default block categories using the `block_categories` filter. You can do it on the server by implementing a function which returns a list of categories. It is going to be used during blocks registration and to group blocks in the inserter. You can also use the second provided param `$post` to generate a different list depending on the post's content.

```php
add_filter( 'block_categories', function( $categories, $post ) {
Copy link
Member

Choose a reason for hiding this comment

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

FYI this example is not PHP 5.2.x compliant (with use of closure, vs. named function).

Copy link
Member Author

@gziolo gziolo Jun 28, 2018

Choose a reason for hiding this comment

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

Yes, phpcs complained when I pasted it into codebase. I will split it into 5.2.x style before merging.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in e75c4f3.

@@ -80,10 +80,8 @@ export const fallbackBlockName = createBlockNameSetterReducer( 'SET_FALLBACK_BLO
* @return {Object} Updated state.
*/
export function categories( state = DEFAULT_CATEGORIES, action ) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we want to keep the DEFAULT_CATEGORIES in the client?

Copy link
Contributor

Choose a reason for hiding this comment

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

That question will come up with the blocks as well. I think we need a way to generate scripts bundled as separate modules in case someone wants to use Gutenberg fully client-side.

Copy link
Member

Choose a reason for hiding this comment

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

I think it seems fine to have a default set in the client. From purely a maintainability perspective, it would be nice if we could avoid maintaining two separate sets of the same values though (e.g. somehow inject into the client JS from the server default value, or vice-versa).

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I thought about that, too. It would be nice to have it in one place, but it is tricky because of translations. I opted for duplication to move on. I open a follow-up issue where we can discuss how to tackle the standalone case.

Copy link
Contributor

Choose a reason for hiding this comment

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

There's already some previous work done by @aduth bin/get-server-blocks.php that can achieve something close to what we need.

@gziolo gziolo merged commit 2e29bb3 into master Jun 28, 2018
Extensibility automation moved this from In progress to Done Jun 28, 2018
@gziolo gziolo deleted the add/set-categories-filter branch June 28, 2018 17:53
@mtias mtias mentioned this pull request Jul 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Blocks Overall functionality of blocks [Feature] Extensibility The ability to extend blocks or the editing experience
Projects
No open projects
Extensibility
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants