-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Block API: Support and bootstrap server-registered block attribute schemas #2529
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
/** | ||
* Generates server-registered block attributes, writing to standard output. | ||
* | ||
* @package gutenberg-build | ||
*/ | ||
|
||
$attributes = array(); | ||
|
||
/** | ||
* Register a block type. Substitute for core API in lieu of loading full | ||
* WordPress context. | ||
* | ||
* @param string $name Block type name including namespace. | ||
* @param array $args { | ||
* Optional. Array of block type arguments. Any arguments may be defined, however the | ||
* ones described below are supported by default. Default empty array. | ||
* | ||
* @type callable $render_callback Callback used to render blocks of this block type. | ||
* @type array $attributes Block attributes mapping, property name to schema. | ||
* } | ||
*/ | ||
function register_block_type( $name, $args = array() ) { | ||
if ( ! isset( $args['attributes'] ) ) { | ||
return; | ||
} | ||
|
||
global $attributes; | ||
$attributes[ $name ] = $args['attributes']; | ||
} | ||
|
||
// Register server-side code for individual blocks. | ||
foreach ( glob( dirname( dirname( __FILE__ ) ) . '/blocks/library/*/index.php' ) as $block_logic ) { | ||
require_once $block_logic; | ||
} | ||
|
||
echo json_encode( $attributes, JSON_PRETTY_PRINT ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ | |
"postsToShow": 5, | ||
"displayPostDate": false, | ||
"layout": "list", | ||
"columns": 3 | ||
"columns": 3, | ||
"align": "center" | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ | |
"postsToShow": 5, | ||
"displayPostDate": true, | ||
"layout": "list", | ||
"columns": 3 | ||
"columns": 3, | ||
"align": "center" | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"core\/latest-posts": { | ||
"postsToShow": { | ||
"type": "number", | ||
"default": 5 | ||
}, | ||
"displayPostDate": { | ||
"type": "boolean", | ||
"default": false | ||
}, | ||
"layout": { | ||
"type": "string", | ||
"default": "list" | ||
}, | ||
"columns": { | ||
"type": "number", | ||
"default": 3 | ||
}, | ||
"align": { | ||
"type": "string", | ||
"default": "center" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been thinking about this a bit more. This kind of feels odd to me—that opening the block JS file has no trace of attributes. (We would also need to handle the categories block.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could have the block implementer include attributes here, but it's redundant information that could easily fall out of sync.
A single source of truth would be ideal, but if we want to support server validation of block attributes, this would mean that single source must live on the server (or in some way compatible*). In some ways, this could even be seen as a good thing, but it also fragments the implementation of a block (some in the server, some in the client) and hinders the ease of implementing a block which can live exclusively in the client.
* Some previous discussion of compatible formats such as block manifest (JSON) file but... that doesn't really eliminate fragmentation as much as it introduces more 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, if we move all config to a generic json file, and separate edit / save into their own exported
edit.js
save.js
(or a commonindex.js
file), it might be ok.The source of truth of a block seems like it should be independent from server/client distinction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related: #1905 (comment)
Recreating matchers in an environment-agnostic setting is a challenge. This isn't as much of a problem for server-rendered blocks because we assume we don't need matchers (unclear yet if this is a fair assumption).