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

[WIP] Blocks: Explore how we could divide server and client block properties #5652

Closed
wants to merge 4 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
39 changes: 0 additions & 39 deletions bin/get-server-blocks.php

This file was deleted.

21 changes: 13 additions & 8 deletions blocks/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@ const POST_FORMAT_BLOCK_MAP = {
* otherwise `undefined`.
*/
export function registerBlockType( name, settings ) {
settings = {
name,
...get( window._wpBlocks, name ),
...settings,
};

if ( typeof name !== 'string' ) {
console.error(
'Block names must be strings.'
Expand All @@ -112,10 +106,21 @@ export function registerBlockType( name, settings ) {
);
return;
}
if ( typeof settings !== 'object' ) {
console.error(
'Block settings must be objects.'
);
return;
}

settings = applyFilters( 'blocks.registerBlockType', settings, name );
settings = {
name,
...applyFilters( 'blocks.registerBlockType', settings, name ),
// the values set server-side can't be updated on the client
...get( window._wpBlocks, name, {} ),
};

if ( ! settings || ! isFunction( settings.save ) ) {
if ( ! isFunction( settings.save ) ) {
console.error(
'The "save" property must be specified and must be a valid function.'
);
Expand Down
16 changes: 3 additions & 13 deletions blocks/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe( 'blocks', () => {

it( 'should reject blocks without a save function', () => {
const block = registerBlockType( 'my-plugin/fancy-block-5' );
expect( console ).toHaveErroredWith( 'The "save" property must be specified and must be a valid function.' );
expect( console ).toHaveErroredWith( 'Block settings must be objects.' );
expect( block ).toBeUndefined();
} );

Expand Down Expand Up @@ -161,7 +161,7 @@ describe( 'blocks', () => {
expect( block ).toBeUndefined();
} );

it( 'should default to browser-initialized global attributes', () => {
it( 'should not update server-initialized attributes', () => {
const attributes = { ok: { type: 'boolean' } };
window._wpBlocks = {
'core/test-block-with-attributes': { attributes },
Expand All @@ -176,17 +176,7 @@ describe( 'blocks', () => {
category: 'common',
title: 'block title',
icon: 'block-default',
attributes: {
ok: {
type: 'boolean',
},
className: {
type: 'string',
},
layout: {
type: 'string',
},
},
attributes,
} );
} );

Expand Down
16 changes: 2 additions & 14 deletions blocks/library/block/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,12 @@ const ConnectedReusableBlockEdit = connect(
} )
)( ReusableBlockEdit );

export const name = 'core/block';
export { name } from './settings.json';

export const settings = {
title: __( 'Shared Block' ),
category: 'shared',
isPrivate: true,

attributes: {
ref: {
type: 'number',
},
},

supports: {
customClassName: false,
html: false,
},

edit: ConnectedReusableBlockEdit,

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

return gutenberg_render_block( $block );
}

register_block_type( 'core/block', array(
'attributes' => array(
'ref' => array(
'type' => 'number',
),
),

'render_callback' => 'gutenberg_render_block_core_reusable_block',
) );
15 changes: 15 additions & 0 deletions blocks/library/block/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "core/block",
"category": "shared",
"isPrivate": true,
"supports": {
"customClassName": false,
"html": false
},
"attributes": {
"ref": {
"type": "number"
}
},
"render_callback": "gutenberg_render_block_core_reusable_block"
}
26 changes: 1 addition & 25 deletions blocks/library/categories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import './editor.scss';
import './style.scss';
import CategoriesBlock from './block';

export const name = 'core/categories';
export { name } from './settings.json';

export const settings = {
title: __( 'Categories' ),
Expand All @@ -19,30 +19,6 @@ export const settings = {

icon: 'list-view',

category: 'widgets',

attributes: {
showPostCounts: {
type: 'boolean',
default: false,
},
displayAsDropdown: {
type: 'boolean',
default: false,
},
showHierarchy: {
type: 'boolean',
default: false,
},
align: {
type: 'string',
},
},

supports: {
html: false,
},

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'full' === align ) {
Expand Down
11 changes: 0 additions & 11 deletions blocks/library/categories/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,3 @@ function onCatChange() {
<?php
return ob_get_clean();
}

/**
* Registers the `core/categories` block on server.
*/
function register_block_core_categories() {
register_block_type( 'core/categories', array(
'render_callback' => 'render_block_core_categories',
) );
}

add_action( 'init', 'register_block_core_categories' );
25 changes: 25 additions & 0 deletions blocks/library/categories/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "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"
}
},
"render_callback": "render_block_core_categories"
}
27 changes: 2 additions & 25 deletions blocks/library/embed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,16 @@ import BlockAlignmentToolbar from '../../block-alignment-toolbar';
// These embeds do not work in sandboxes
const HOSTS_NO_PREVIEWS = [ 'facebook.com' ];

function getEmbedBlockSettings( { title, icon, category = 'embed', transforms, keywords = [] } ) {
function getEmbedBlockSettings( { title, icon, transforms, keywords = [] } ) {
return {
title,

description: __( 'The Embed block allows you to easily add videos, images, tweets, audio, and other content to your post or page.' ),

icon,

category,

keywords,

attributes: {
url: {
type: 'string',
},
caption: {
type: 'array',
source: 'children',
selector: 'figcaption',
default: [],
},
align: {
type: 'string',
},
type: {
type: 'string',
},
providerNameSlug: {
type: 'string',
},
},

transforms,

getEditWrapperProps( attributes ) {
Expand Down Expand Up @@ -255,7 +232,7 @@ function getEmbedBlockSettings( { title, icon, category = 'embed', transforms, k
};
}

export const name = 'core/embed';
export { name } from './settings.json';

export const settings = getEmbedBlockSettings( {
title: __( 'Embed' ),
Expand Down
59 changes: 59 additions & 0 deletions blocks/library/embed/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": "core/embed",
"category": "embed",
"attributes": {
"url": {
"type": "string"
},
"caption": {
"type": "array",
"source": "children",
"selector": "figcaption",
"default": []
},
"align": {
"type": "string"
},
"type": {
"type": "string"
},
"providerNameSlug": {
"type": "string"
}
},
"variations": [
"core-embed/twitter",
"core-embed/youtube",
"core-embed/facebook",
"core-embed/instagram",
"core-embed/wordpress",
"core-embed/soundcloud",
"core-embed/spotify",
"core-embed/flickr",
"core-embed/vimeo",
"core-embed/animoto",
"core-embed/cloudup",
"core-embed/collegehumor",
"core-embed/dailymotion",
"core-embed/funnyordie",
"core-embed/hulu",
"core-embed/imgur",
"core-embed/issuu",
"core-embed/kickstarter",
"core-embed/meetup-com",
"core-embed/mixcloud",
"core-embed/photobucket",
"core-embed/polldaddy",
"core-embed/reddit",
"core-embed/reverbnation",
"core-embed/screencast",
"core-embed/scribd",
"core-embed/slideshare",
"core-embed/smugmug",
"core-embed/speaker",
"core-embed/ted",
"core-embed/tumblr",
"core-embed/videopress",
"core-embed/wordpress-tv"
]
}
8 changes: 1 addition & 7 deletions blocks/library/latest-posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import './editor.scss';
import './style.scss';
import LatestPostsBlock from './block';

export const name = 'core/latest-posts';
export { name } from './settings.json';

export const settings = {
title: __( 'Latest Posts' ),
Expand All @@ -19,14 +19,8 @@ export const settings = {

icon: 'list-view',

category: 'widgets',

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

supports: {
html: false,
},

getEditWrapperProps( attributes ) {
const { align } = attributes;
if ( 'left' === align || 'right' === align || 'wide' === align || 'full' === align ) {
Expand Down
Loading