-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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: Initial explorations to migrate to server-registered blocks #3962
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
/** | ||
* Generates server-registered blocks, writing to standard output. By default | ||
* assumes plugin exists in a standard install `wp-content/plugins` directory. | ||
* Define ABSPATH environment variable pointing to WordPress install otherwise. | ||
* | ||
* @package gutenberg-build | ||
*/ | ||
|
||
// Disable error reporting which would otherwise be displayed in stdout along | ||
// with the JSON output. | ||
error_reporting( 0 ); | ||
|
||
$abspath = getenv( 'ABSPATH' ); | ||
define( 'ABSPATH', $abspath ? $abspath : dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ) . '/' ); | ||
define( 'WPINC', 'wp-includes' ); | ||
define( 'WP_SETUP_CONFIG', true ); | ||
define( 'WP_USE_THEMES', false ); | ||
require_once( ABSPATH . WPINC . '/load.php' ); | ||
require_once( ABSPATH . WPINC . '/default-constants.php' ); | ||
wp_fix_server_vars(); | ||
wp_initial_constants(); | ||
require_once( ABSPATH . WPINC . '/functions.php' ); | ||
wp_load_translations_early(); | ||
wp_set_lang_dir(); | ||
require_once( dirname( dirname( __FILE__ ) ) . '/lib/blocks.php' ); | ||
require_once( dirname( dirname( __FILE__ ) ) . '/lib/class-wp-block-type-registry.php' ); | ||
require_once( dirname( dirname( __FILE__ ) ) . '/lib/class-wp-block-type.php' ); | ||
require_once( dirname( dirname( __FILE__ ) ) . '/lib/client-assets.php' ); | ||
|
||
// Register server-side code for individual blocks. | ||
foreach ( glob( dirname( dirname( __FILE__ ) ) . '/blocks/library/*/index.php' ) as $block_logic ) { | ||
require_once $block_logic; | ||
} | ||
|
||
do_action( 'init' ); | ||
|
||
echo json_encode( gutenberg_prepare_blocks_for_js() ); | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,12 @@ let defaultBlockName; | |
* registered; otherwise `undefined`. | ||
*/ | ||
export function registerBlockType( name, settings ) { | ||
settings = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we should update our documentation to favor SSR registration if we go all server side. At that time we could decide to add a "warning" here if the corresponding registration is not found (for a smooth migration) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Agree.
Customizer controls are bootstrapped in a very similar way, and I find it interesting there's no inclination for developers to go straight to using the JavaScript APIs. I guess one difference here is that we'd probably assume there is a JavaScript counter-part for defining the UI behaviors. Part of my thinking with #2756 is that by moving the script handle to being a property of the block register API, it would require some unconventional hoop-jumping to register the JS block any other way. Maybe to your earlier work on feeding the editor components with the registered blocks, it would make sense to warn here, since then the editor would still be generic enough to work with client-only blocks in non-WordPress contexts. |
||
name, | ||
...get( window._wpBlocks, name ), | ||
...settings, | ||
}; | ||
|
||
if ( typeof name !== 'string' ) { | ||
console.error( | ||
'Block names must be strings.' | ||
|
@@ -113,12 +119,6 @@ export function registerBlockType( name, settings ) { | |
settings.icon = 'block-default'; | ||
} | ||
|
||
settings = { | ||
name, | ||
attributes: get( window._wpBlocksAttributes, name, {} ), | ||
...settings, | ||
}; | ||
|
||
settings = applyFilters( 'blocks.registerBlockType', settings, name ); | ||
|
||
return blocks[ name ] = settings; | ||
|
@@ -223,7 +223,7 @@ export function hasBlockSupport( nameOrType, feature, defaultSupports ) { | |
* Determines whether or not the given block is a reusable block. This is a | ||
* special block type that is used to point to a global block stored via the | ||
* API. | ||
* | ||
* | ||
* @param {Object} blockOrType Block or Block Type to test | ||
* @return {Boolean} Whether the given block is a reusable block | ||
*/ | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"core\/block":{"attributes":{"ref":{"type":"number"}}},"core\/latest-posts":{"attributes":{"categories":{"type":"string"},"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"},"order":{"type":"string","default":"desc"},"orderBy":{"type":"string","default":"date"}}}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Block types registration Tests | ||
* | ||
* @package Gutenberg | ||
*/ | ||
|
||
/** | ||
* Test gutenberg_prepare_blocks_for_js() | ||
*/ | ||
class Prepare_For_JS_Test extends WP_UnitTestCase { | ||
|
||
function setUp() { | ||
parent::setUp(); | ||
|
||
$this->reset(); | ||
} | ||
|
||
function tearDown() { | ||
parent::tearDown(); | ||
|
||
$this->reset(); | ||
} | ||
|
||
function reset() { | ||
foreach ( WP_Block_Type_Registry::get_instance()->get_all_registered() as $name => $block_type ) { | ||
WP_Block_Type_Registry::get_instance()->unregister( $name ); | ||
} | ||
} | ||
|
||
function test_gutenberg_prepare_blocks_for_js() { | ||
$name = 'core/paragraph'; | ||
$settings = array( | ||
'icon' => 'text', | ||
'render_callback' => 'foo', | ||
); | ||
|
||
register_block_type( $name, $settings ); | ||
|
||
$blocks = gutenberg_prepare_blocks_for_js(); | ||
|
||
$this->assertEquals( array( | ||
'core/paragraph' => array( | ||
'icon' => 'text', | ||
), | ||
), $blocks ); | ||
} | ||
} |
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.
So why do we need these fixtures? For test purpose?
Maybe the unit tests should not rely on the fixtures (on the default blocks), we'll probably lose some coverage but I think the unit tests should be "isolated" tests.
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.
Right, without these, the block content fixture tests fail too, since otherwise the Node process has no way to know what are the attributes of a paragraph 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.
Not for this PR, but I think we should avoid registering the default blocks in the tests completely and just provide "dummy blocks" in tests.