Block Markup | +Query | +Block Data API | +
+
+```html
+
+
+ ++ +``` + + |
++ +```graphQL +query NewQuery { + post(id: "1", idType: DATABASE_ID) { + blocksData { + blocks { + id + name + attributes { + name + value + } + innerBlocks { + name + parentId + id + attributes { + name + value + } + } + } + } + } +} +``` + + | ++ +```json +{ + "data": { + "post": { + "blocksData": { + "blocks": [ + { + "attributes": [ + { + "name": "ordered", + "value": "" + }, + { + "name": "values", + "value": "" + } + ], + "id": "1", + "name": "core/list", + "innerBlocks": [ + { + "id": "2", + "name": "core/list-item", + "parentId": "1", + "attributes": [ + { + "name": "content", + "value": "This is item 1 in the list" + } + ] + }, + { + "id": "3", + "name": "core/list-item", + "parentId": "1", + "attributes": [ + { + "name": "content", + "value": "This is item 2 in the list" + } + ] + } + ] + }, + { + "attributes": [ + { + "name": "value", + "value": "" + }, + { + "name": "citation", + "value": "" + } + ], + "id": "4", + "name": "core/quote", + "innerBlocks": [ + { + "id": "5", + "name": "core/paragraph", + "parentId": "4", + "attributes": [ + { + "name": "content", + "value": "This is a paragraph within a quote" + }, + { + "name": "dropCap", + "value": "" + } + ] + } + ] + } + ] + } + } + } +} +``` + + | +
Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
+ + + +++ + '; + + $expected_blocks = [ + 'blocks' => [ + [ + 'name' => 'core/paragraph', + 'attributes' => [ + array( + 'name' => 'content', + 'value' => 'Welcome to WordPress. This is your first post. Edit or delete it, then start writing!', + ), + array( + 'name' => 'dropCap', + 'value' => '', + ), + ], + 'id' => '1', + ], + [ + 'name' => 'core/quote', + 'attributes' => [ + array( + 'name' => 'value', + 'value' => '', + ), + array( + 'name' => 'citation', + 'value' => '', + ), + ], + 'innerBlocks' => [ + [ + 'name' => 'core/paragraph', + 'attributes' => [ + array( + 'name' => 'content', + 'value' => 'This is a heading inside a quote', + ), + array( + 'name' => 'dropCap', + 'value' => '', + ), + ], + 'id' => '3', + ], + [ + 'name' => 'core/quote', + 'attributes' => [ + array( + 'name' => 'value', + 'value' => '', + ), + array( + 'name' => 'citation', + 'value' => '', + ), + ], + 'innerBlocks' => [ + [ + 'name' => 'core/heading', + 'attributes' => [ + array( + 'name' => 'content', + 'value' => 'This is a heading', + ), + array( + 'name' => 'level', + 'value' => '2', + ), + ], + 'id' => '5', + ], + ], + 'id' => '4', + ], + ], + 'id' => '2', + ], + ], + ]; + + $post = $this->factory()->post->create_and_get( [ + 'post_content' => $html, + ] ); + + $blocks_data = GraphQLApi::get_blocks_data( $post ); + + $this->assertEquals( $expected_blocks, $blocks_data ); + } + + public function test_flatten_inner_blocks() { + $inner_blocks = [ + [ + 'name' => 'core/paragraph', + 'attributes' => [ + array( + 'name' => 'content', + 'value' => 'Welcome to WordPress. This is your first post. Edit or delete it, then start writing!', + ), + array( + 'name' => 'dropCap', + 'value' => '', + ), + ], + 'id' => '2', + ], + [ + 'name' => 'core/quote', + 'attributes' => [ + array( + 'name' => 'value', + 'value' => '', + ), + array( + 'name' => 'citation', + 'value' => '', + ), + ], + 'innerBlocks' => [ + [ + 'name' => 'core/paragraph', + 'attributes' => [ + array( + 'name' => 'content', + 'value' => 'This is a heading inside a quote', + ), + array( + 'name' => 'dropCap', + 'value' => '', + ), + ], + 'id' => '4', + ], + [ + 'name' => 'core/quote', + 'attributes' => [ + array( + 'name' => 'value', + 'value' => '', + ), + array( + 'name' => 'citation', + 'value' => '', + ), + ], + 'innerBlocks' => [ + [ + 'name' => 'core/heading', + 'attributes' => [ + array( + 'name' => 'content', + 'value' => 'This is a heading', + ), + array( + 'name' => 'level', + 'value' => '2', + ), + ], + 'id' => '6', + ], + ], + 'id' => '5', + ], + ], + 'id' => '3', + ], + ]; + + $expected_blocks = [ + [ + 'name' => 'core/paragraph', + 'attributes' => [ + array( + 'name' => 'content', + 'value' => 'Welcome to WordPress. This is your first post. Edit or delete it, then start writing!', + ), + array( + 'name' => 'dropCap', + 'value' => '', + ), + ], + 'parentId' => '1', + 'id' => '2', + ], + [ + 'name' => 'core/quote', + 'attributes' => [ + array( + 'name' => 'value', + 'value' => '', + ), + array( + 'name' => 'citation', + 'value' => '', + ), + ], + 'id' => '3', + 'parentId' => '1', + ], + [ + 'name' => 'core/paragraph', + 'attributes' => [ + array( + 'name' => 'content', + 'value' => 'This is a heading inside a quote', + ), + array( + 'name' => 'dropCap', + 'value' => '', + ), + ], + 'id' => '4', + 'parentId' => '3', + ], + [ + 'name' => 'core/quote', + 'attributes' => [ + array( + 'name' => 'value', + 'value' => '', + ), + array( + 'name' => 'citation', + 'value' => '', + ), + ], + 'id' => '5', + 'parentId' => '3', + ], + [ + 'name' => 'core/heading', + 'attributes' => [ + array( + 'name' => 'content', + 'value' => 'This is a heading', + ), + array( + 'name' => 'level', + 'value' => '2', + ), + ], + 'id' => '6', + 'parentId' => '5', + ], + ]; + + $flattened_blocks = GraphQLApi::flatten_inner_blocks( $inner_blocks, '1' ); + + $this->assertEquals( $expected_blocks, $flattened_blocks ); + } +} diff --git a/tests/mocks/graphql-relay-mock.php b/tests/mocks/graphql-relay-mock.php new file mode 100644 index 00000000..d0b6eabb --- /dev/null +++ b/tests/mocks/graphql-relay-mock.php @@ -0,0 +1,24 @@ +register(true); - $filesToLoad = \Composer\Autoload\ComposerStaticInit5a91735b735b03b013d5e95913d80f8f::$files; + $filesToLoad = \Composer\Autoload\ComposerStaticInit0e98fdf7ca952c8f4cb3c82e525d431a::$files; $requireFile = \Closure::bind(static function ($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 480f01b3..f66e5e9d 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInit5a91735b735b03b013d5e95913d80f8f +class ComposerStaticInit0e98fdf7ca952c8f4cb3c82e525d431a { public static $files = array ( 'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php', @@ -67,9 +67,9 @@ class ComposerStaticInit5a91735b735b03b013d5e95913d80f8f public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInit5a91735b735b03b013d5e95913d80f8f::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInit5a91735b735b03b013d5e95913d80f8f::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInit5a91735b735b03b013d5e95913d80f8f::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInit0e98fdf7ca952c8f4cb3c82e525d431a::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInit0e98fdf7ca952c8f4cb3c82e525d431a::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInit0e98fdf7ca952c8f4cb3c82e525d431a::$classMap; }, null, ClassLoader::class); } diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index a5c18f11..87d59b63 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -3,7 +3,7 @@ 'name' => 'automattic/vip-block-data-api', 'pretty_version' => 'dev-trunk', 'version' => 'dev-trunk', - 'reference' => '3b1bb5bd0dc9edf8960335e8d0e06ba83930868d', + 'reference' => 'd98425acb0b1c58f3584f1fbc160a4756e0c5c79', 'type' => 'wordpress-plugin', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), @@ -13,7 +13,7 @@ 'automattic/vip-block-data-api' => array( 'pretty_version' => 'dev-trunk', 'version' => 'dev-trunk', - 'reference' => '3b1bb5bd0dc9edf8960335e8d0e06ba83930868d', + 'reference' => 'd98425acb0b1c58f3584f1fbc160a4756e0c5c79', 'type' => 'wordpress-plugin', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), diff --git a/vip-block-data-api.php b/vip-block-data-api.php index 94ea06f2..630a25c9 100644 --- a/vip-block-data-api.php +++ b/vip-block-data-api.php @@ -5,9 +5,9 @@ * Description: Access Gutenberg block data in JSON via the REST API. * Author: WordPress VIP * Text Domain: vip-block-data-api - * Version: 1.0.3 - * Requires at least: 5.6.0 - * Tested up to: 6.3.0 + * Version: 1.1.0 + * Requires at least: 5.9 + * Tested up to: 6.3 * Requires PHP: 7.4 * License: GPL-3 * License URI: https://www.gnu.org/licenses/gpl-3.0.html @@ -20,7 +20,7 @@ if ( ! defined( 'VIP_BLOCK_DATA_API_LOADED' ) ) { define( 'VIP_BLOCK_DATA_API_LOADED', true ); - define( 'WPCOMVIP__BLOCK_DATA_API__PLUGIN_VERSION', '1.0.3' ); + define( 'WPCOMVIP__BLOCK_DATA_API__PLUGIN_VERSION', '1.1.0' ); define( 'WPCOMVIP__BLOCK_DATA_API__REST_ROUTE', 'vip-block-data-api/v1' ); // Analytics related configs. @@ -31,6 +31,9 @@ // Composer dependencies. require_once __DIR__ . '/vendor/autoload.php'; + // GraphQL API. + require_once __DIR__ . '/src/graphql/graphql-api.php'; + // /wp-json/ API. require_once __DIR__ . '/src/rest/rest-api.php';This is a heading inside a quote
+ + + +++This is a heading
+