Skip to content

Commit

Permalink
Blocks: Move more properties to the server for dynamic blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Mar 29, 2018
1 parent 0571275 commit 271404c
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 37 deletions.
4 changes: 0 additions & 4 deletions blocks/library/block/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,6 @@ const EnhancedReusableBlockEdit = compose( [
export const name = 'core/block';

export const settings = {
title: __( 'Shared Block' ),

isPrivate: true,

edit: EnhancedReusableBlockEdit,

save: () => null,
Expand Down
2 changes: 2 additions & 0 deletions blocks/library/block/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ function gutenberg_render_block_core_reusable_block( $attributes ) {
}

register_block_type( 'core/block', array(
'title' => __( 'Shared Block', 'gutenberg' ),
'category' => 'shared',
'supports' => array(
'customClassName' => false,
'html' => false,
'insertable' => false,
),
'attributes' => array(
'ref' => array(
Expand Down
11 changes: 0 additions & 11 deletions blocks/library/categories/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
Expand All @@ -13,12 +8,6 @@ import CategoriesBlock from './block';
export const name = 'core/categories';

export const settings = {
title: __( 'Categories' ),

description: __( 'Shows a list of your site\'s categories.' ),

icon: 'list-view',

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'full' === align ) {
Expand Down
3 changes: 3 additions & 0 deletions blocks/library/categories/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ function onCatChange() {
*/
function register_block_core_categories() {
register_block_type( 'core/categories', array(
'title' => __( 'Categories', 'gutenberg' ),
'description' => __( 'Shows a list of your site\'s categories.', 'gutenberg' ),
'icon' => 'list-view',
'category' => 'widgets',
'supports' => array(
'html' => false,
Expand Down
13 changes: 0 additions & 13 deletions blocks/library/latest-posts/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
Expand All @@ -13,14 +8,6 @@ import LatestPostsBlock from './block';
export const name = 'core/latest-posts';

export const settings = {
title: __( 'Latest Posts' ),

description: __( 'Shows a list of your site\'s most recent posts.' ),

icon: 'list-view',

keywords: [ __( 'recent posts' ) ],

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align || 'full' === align ) {
Expand Down
6 changes: 6 additions & 0 deletions blocks/library/latest-posts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ function render_block_core_latest_posts( $attributes ) {
*/
function register_block_core_latest_posts() {
register_block_type( 'core/latest-posts', array(
'title' => __( 'Latest Posts', 'gutenberg' ),
'description' => __( 'Shows a list of your site\'s most recent posts.', 'gutenberg' ),
'icon' => 'list-view',
'keywords' => array(
__( 'recent posts', 'gutenberg' ),
),
'category' => 'widgets',
'supports' => array(
'html' => false,
Expand Down
2 changes: 1 addition & 1 deletion blocks/test/server-registered.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"core\/block":{"category":"shared","supports":{"customClassName":false,"html":false},"attributes":{"ref":{"type":"number"}}},"core\/categories":{"category":"widgets","supports":{"html":false},"attributes":{"showPostCounts":{"type":"boolean","default":false},"displayAsDropdown":{"type":"boolean","default":false},"showHierarchy":{"type":"boolean","default":false},"align":{"type":"string"}}},"core\/latest-posts":{"category":"widgets","supports":{"html":false},"attributes":{"categories":{"type":"string"},"postsToShow":{"type":"number","default":5},"displayPostDate":{"type":"boolean","default":false},"postLayout":{"type":"string","default":"list"},"columns":{"type":"number","default":3},"align":{"type":"string","default":"center"},"order":{"type":"string","default":"desc"},"orderBy":{"type":"string","default":"date"}}}}
{"core\/block":{"title":"Shared Block","category":"shared","supports":{"customClassName":false,"html":false,"insertable":false},"attributes":{"ref":{"type":"number"}}},"core\/categories":{"title":"Categories","description":"Shows a list of your site's categories.","category":"widgets","icon":"list-view","supports":{"html":false},"attributes":{"showPostCounts":{"type":"boolean","default":false},"displayAsDropdown":{"type":"boolean","default":false},"showHierarchy":{"type":"boolean","default":false},"align":{"type":"string"}}},"core\/latest-posts":{"title":"Latest Posts","description":"Shows a list of your site's most recent posts.","category":"widgets","icon":"list-view","keywords":["recent posts"],"supports":{"html":false},"attributes":{"categories":{"type":"string"},"postsToShow":{"type":"number","default":5},"displayPostDate":{"type":"boolean","default":false},"postLayout":{"type":"string","default":"list"},"columns":{"type":"number","default":3},"align":{"type":"string","default":"center"},"order":{"type":"string","default":"desc"},"orderBy":{"type":"string","default":"date"}}}}
7 changes: 7 additions & 0 deletions docs/block-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ className: false,
html: false,
```

- `insertable` (default `true`): By default, Gutenberg will show all blocks in the inserter. To disable this behavior, set `insertable` to `false`.

```js
// Hide from the inserter.
insertable: false,
```

## Edit and Save

The `edit` and `save` functions define the editor interface with which a user would interact, and the markup to be serialized back when a post is saved. They are the heart of how a block operates, so they are [covered separately](https://wordpress.org/gutenberg/handbook/block-edit-save/).
4 changes: 2 additions & 2 deletions editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import createSelector from 'rememo';
/**
* WordPress dependencies
*/
import { serialize, getBlockType, getBlockTypes } from '@wordpress/blocks';
import { serialize, getBlockType, getBlockTypes, hasBlockSupport } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
import { moment } from '@wordpress/date';
Expand Down Expand Up @@ -1183,7 +1183,7 @@ function buildInserterItemFromBlockType( state, enabledBlockTypes, blockType ) {
return null;
}

if ( blockType.isPrivate ) {
if ( ! hasBlockSupport( blockType, 'insertable', true ) ) {
return null;
}

Expand Down
10 changes: 8 additions & 2 deletions editor/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import { filter, property, union } from 'lodash';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { registerBlockType, unregisterBlockType, registerCoreBlocks, getBlockTypes } from '@wordpress/blocks';
import {
getBlockTypes,
hasBlockSupport,
registerBlockType,
registerCoreBlocks,
unregisterBlockType,
} from '@wordpress/blocks';
import { moment } from '@wordpress/date';

/**
Expand Down Expand Up @@ -2433,7 +2439,7 @@ describe( 'selectors', () => {
},
};

const blockTypes = getBlockTypes().filter( blockType => ! blockType.isPrivate );
const blockTypes = getBlockTypes().filter( blockType => hasBlockSupport( blockType, 'insertable', true ) );
expect( getInserterItems( state ) ).toHaveLength( blockTypes.length );
} );

Expand Down
42 changes: 39 additions & 3 deletions lib/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,46 @@ class WP_Block_Type {
*/
public $name;

/**
* Block type title.
*
* @since 2.6.0
* @var string
*/
public $title;

/**
* Block type description.
*
* @since 2.6.0
* @var string
*/
public $description;

/**
* Block type category.
*
* @since 2.5.0
* @since 2.6.0
* @var string
*/
public $category;

/**
* Block type icon.
*
* @since 2.6.0
* @var string
*/
public $icon;

/**
* Block type keywords.
*
* @since 2.6.0
* @var array
*/
public $keywords;

/**
* Block type render callback.
*
Expand All @@ -41,7 +73,7 @@ class WP_Block_Type {
/**
* Block type supports property schemas.
*
* @since 2.5.0
* @since 2.6.0
* @var array
*/
public $supports;
Expand Down Expand Up @@ -189,15 +221,19 @@ public function set_props( $args ) {
/**
* Filters settings for the block just before it gets registered.
*
* @since 2.5.0
* @since 2.6.0
*/
public function filter_settings() {
if ( ! has_filter( 'register_block_type' ) ) {
return;
}

$this->set_props( apply_filters( 'register_block_type', array(
'name' => $this->name,
'description' => $this->description,
'category' => $this->category,
'icon' => $this->icon,
'keywords' => $this->keywords,
'supports' => $this->supports,
'attributes' => $this->attributes,
'render_callback' => $this->render_callback,
Expand Down
2 changes: 1 addition & 1 deletion lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ function gutenberg_get_post_to_edit( $post_id ) {
function gutenberg_prepare_blocks_for_js() {
$block_registry = WP_Block_Type_Registry::get_instance();
$blocks = array();
$keys_to_pick = array( 'title', 'icon', 'category', 'keywords', 'supports', 'attributes' );
$keys_to_pick = array( 'title', 'description', 'category', 'icon', 'keywords', 'supports', 'attributes' );

foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
foreach ( $keys_to_pick as $key ) {
Expand Down

0 comments on commit 271404c

Please sign in to comment.