diff --git a/bin/get-server-block-attributes.php b/bin/get-server-block-attributes.php deleted file mode 100755 index b0dcc034f73d7e..00000000000000 --- a/bin/get-server-block-attributes.php +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env php - { } ); setUnknownTypeHandlerName( undefined ); setDefaultBlockName( undefined ); - window._wpBlocksAttributes = {}; + window._wpBlocks = {}; console.error = error; } ); @@ -163,8 +163,8 @@ describe( 'blocks', () => { it( 'should default to browser-initialized global attributes', () => { const attributes = { ok: { type: 'boolean' } }; - window._wpBlocksAttributes = { - 'core/test-block-with-attributes': attributes, + window._wpBlocks = { + 'core/test-block-with-attributes': { attributes }, }; const blockType = { settingName: 'settingValue', save: noop, category: 'common', title: 'block title' }; diff --git a/blocks/test/fixtures/core__latest-posts.json b/blocks/test/fixtures/core__latest-posts.json index 528d4ee3a614b3..0ef356b6788d5f 100644 --- a/blocks/test/fixtures/core__latest-posts.json +++ b/blocks/test/fixtures/core__latest-posts.json @@ -8,7 +8,9 @@ "displayPostDate": false, "layout": "list", "columns": 3, - "align": "center" + "align": "center", + "order": "desc", + "orderBy": "date" }, "originalContent": "" } diff --git a/blocks/test/fixtures/core__latest-posts__displayPostDate.json b/blocks/test/fixtures/core__latest-posts__displayPostDate.json index 0b727f17c14f62..c0e38f52cf8285 100644 --- a/blocks/test/fixtures/core__latest-posts__displayPostDate.json +++ b/blocks/test/fixtures/core__latest-posts__displayPostDate.json @@ -8,7 +8,9 @@ "displayPostDate": true, "layout": "list", "columns": 3, - "align": "center" + "align": "center", + "order": "desc", + "orderBy": "date" }, "originalContent": "" } diff --git a/blocks/test/full-content.js b/blocks/test/full-content.js index 4601439d9b3608..2f480c64380f8e 100644 --- a/blocks/test/full-content.js +++ b/blocks/test/full-content.js @@ -89,7 +89,7 @@ function normalizeParsedBlocks( blocks ) { describe( 'full post content fixture', () => { beforeAll( () => { - window._wpBlocksAttributes = require( './server-attributes.json' ); + window._wpBlocks = require( './server-registered.json' ); // Register all blocks. require( 'blocks' ); diff --git a/blocks/test/server-attributes.json b/blocks/test/server-attributes.json deleted file mode 100644 index 86c1b6198d1f29..00000000000000 --- a/blocks/test/server-attributes.json +++ /dev/null @@ -1 +0,0 @@ -{"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"}}} \ No newline at end of file diff --git a/blocks/test/server-registered.json b/blocks/test/server-registered.json new file mode 100644 index 00000000000000..9e6294f3238b0b --- /dev/null +++ b/blocks/test/server-registered.json @@ -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"}}}} \ No newline at end of file diff --git a/lib/client-assets.php b/lib/client-assets.php index 0840ebbf49b7ee..738b04aaba1d46 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -531,6 +531,35 @@ function gutenberg_get_post_to_edit( $post_id ) { return rest_get_server()->response_to_data( $response, false ); } +/** + * Prepares server-registered blocks for JavaScript, returning an associative + * array of registered block data keyed by block name. Data includes properties + * of a block relevant for client registration. + * + * @return array An associative array of registered block data. + */ +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' ); + + foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { + foreach ( $keys_to_pick as $key ) { + if ( ! isset( $block_type->{ $key } ) ) { + continue; + } + + if ( ! isset( $blocks[ $block_name ] ) ) { + $blocks[ $block_name ] = array(); + } + + $blocks[ $block_name ][ $key ] = $block_type->{ $key }; + } + } + + return $blocks; +} + /** * Handles the enqueueing of block scripts and styles that are common to both * the editor and the front-end. @@ -750,16 +779,7 @@ function gutenberg_editor_scripts_and_styles( $hook ) { ); // Preload server-registered block schemas. - $block_registry = WP_Block_Type_Registry::get_instance(); - $schemas = array(); - - foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { - if ( isset( $block_type->attributes ) ) { - $schemas[ $block_name ] = $block_type->attributes; - } - } - - wp_localize_script( 'wp-blocks', '_wpBlocksAttributes', $schemas ); + wp_localize_script( 'wp-blocks', '_wpBlocks', gutenberg_prepare_blocks_for_js() ); // Get admin url for handling meta boxes. $meta_box_url = admin_url( 'post.php' ); diff --git a/package.json b/package.json index a092813f17aeb9..58eb251a480e8d 100644 --- a/package.json +++ b/package.json @@ -139,8 +139,8 @@ "test": "npm run lint && npm run test-unit", "ci": "concurrently \"npm run lint && npm run build\" \"npm run test-unit:coverage-ci\"", "fixtures:clean": "rimraf \"blocks/test/fixtures/*.+(json|serialized.html)\"", - "fixtures:server-attributes": "./bin/get-server-block-attributes.php > blocks/test/server-attributes.json", - "fixtures:generate": "npm run fixtures:server-attributes && cross-env GENERATE_MISSING_FIXTURES=y npm run test-unit", + "fixtures:server-registered": "./bin/get-server-blocks.php > blocks/test/server-registered.json", + "fixtures:generate": "npm run fixtures:server-registered && cross-env GENERATE_MISSING_FIXTURES=y npm run test-unit", "fixtures:regenerate": "npm run fixtures:clean && npm run fixtures:generate", "package-plugin": "./bin/build-plugin-zip.sh", "test-unit": "jest", diff --git a/phpunit/class-prepare-for-js-test.php b/phpunit/class-prepare-for-js-test.php new file mode 100644 index 00000000000000..ec7258f504debe --- /dev/null +++ b/phpunit/class-prepare-for-js-test.php @@ -0,0 +1,48 @@ +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 ); + } +} diff --git a/test/unit/setup-globals.js b/test/unit/setup-globals.js index 188224b27a0e2f..f10bf435639b2f 100644 --- a/test/unit/setup-globals.js +++ b/test/unit/setup-globals.js @@ -54,3 +54,6 @@ global.window.userSettings = { uid: 1 }; // Mock jQuery global.window.jQuery = { holdReady() {} }; + +// Bootstrap server-registered blocks +global.window._wpBlocks = require( 'blocks/test/server-registered.json' );