From 2bc1329494d53bd97e98eefa19ef184e4e46f5c8 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Fri, 15 Dec 2017 17:13:19 +1100 Subject: [PATCH] Refactor Reusable Blocks controller to use WP_REST_Posts_Controller We can remove a lot of code by subclassing WP_REST_Posts_Controller, which gives us methods for creating, reading, updating and deleting posts (in this case, blocks) for free. This also adds support for DELETEing a block to the API, and renames the 'name' field to 'title'. --- blocks/api/factory.js | 2 +- blocks/api/test/factory.js | 2 +- blocks/library/block/edit-panel/index.js | 14 +- blocks/library/block/edit-panel/style.scss | 2 +- blocks/library/block/index.js | 20 +- editor/components/inserter/menu.js | 2 +- editor/store/effects.js | 14 +- editor/store/test/effects.js | 24 +- lib/class-wp-rest-blocks-controller.php | 116 +++++++ ...ass-wp-rest-reusable-blocks-controller.php | 306 ------------------ lib/client-assets.php | 2 +- lib/load.php | 2 +- lib/register.php | 22 +- phpunit/class-rest-blocks-controller-test.php | 206 ++++++++++++ ...s-rest-reusable-blocks-controller-test.php | 270 ---------------- 15 files changed, 373 insertions(+), 631 deletions(-) create mode 100644 lib/class-wp-rest-blocks-controller.php delete mode 100644 lib/class-wp-rest-reusable-blocks-controller.php create mode 100644 phpunit/class-rest-blocks-controller-test.php delete mode 100644 phpunit/class-rest-reusable-blocks-controller-test.php diff --git a/blocks/api/factory.js b/blocks/api/factory.js index 60dfc8ce1bf7f3..f2cff80082af76 100644 --- a/blocks/api/factory.js +++ b/blocks/api/factory.js @@ -224,7 +224,7 @@ export function createReusableBlock( type, attributes ) { return { id: +uniqueId(), // Temorary id replaced when the block is saved server side isTemporary: true, - name: __( 'Untitled block' ), + title: __( 'Untitled block' ), type, attributes, }; diff --git a/blocks/api/test/factory.js b/blocks/api/test/factory.js index 0bbb4837c69105..9932007d88f82b 100644 --- a/blocks/api/test/factory.js +++ b/blocks/api/test/factory.js @@ -624,7 +624,7 @@ describe( 'block factory', () => { expect( createReusableBlock( type, attributes ) ).toMatchObject( { id: expect.any( Number ), - name: 'Untitled block', + title: 'Untitled block', type, attributes, } ); diff --git a/blocks/library/block/edit-panel/index.js b/blocks/library/block/edit-panel/index.js index 793ef1f932ca0e..357c9dd683aa82 100644 --- a/blocks/library/block/edit-panel/index.js +++ b/blocks/library/block/edit-panel/index.js @@ -10,13 +10,13 @@ import { __ } from '@wordpress/i18n'; import './style.scss'; function ReusableBlockEditPanel( props ) { - const { isEditing, name, isSaving, onEdit, onDetach, onChangeName, onSave, onCancel } = props; + const { isEditing, title, isSaving, onEdit, onDetach, onChangeTitle, onSave, onCancel } = props; return (
{ ! isEditing && ! isSaving && [ - { name } + { title } ,