From 6a84afa33c8f8d76cc2413e14d12a782ad94a8a2 Mon Sep 17 00:00:00 2001 From: Jeremy Felt Date: Fri, 14 Dec 2018 02:14:13 +0000 Subject: [PATCH] Blocks: Add the reusable block post type, `wp_block`. Merges [43804] from the 5.0 branch to trunk. See #45098. git-svn-id: https://develop.svn.wordpress.org/trunk@44146 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/edit.php | 5 + src/wp-includes/capabilities.php | 17 +++ src/wp-includes/post.php | 32 ++++++ .../phpunit/tests/blocks/render-reusable.php | 102 ++++++++++++++++++ tests/phpunit/tests/user/capabilities.php | 89 +++++++++++++++ 5 files changed, 245 insertions(+) create mode 100644 tests/phpunit/tests/blocks/render-reusable.php diff --git a/src/wp-admin/edit.php b/src/wp-admin/edit.php index 24a02d05b1..3f542be87b 100644 --- a/src/wp-admin/edit.php +++ b/src/wp-admin/edit.php @@ -200,6 +200,11 @@ $wp_list_table->prepare_items(); wp_enqueue_script( 'inline-edit-post' ); wp_enqueue_script( 'heartbeat' ); +if ( 'wp_block' === $post_type ) { + wp_enqueue_script( 'wp-list-reusable-blocks' ); + wp_enqueue_style( 'wp-list-reusable-blocks' ); +} + $title = $post_type_object->labels->name; if ( 'post' == $post_type ) { diff --git a/src/wp-includes/capabilities.php b/src/wp-includes/capabilities.php index 375648e768..c192639608 100644 --- a/src/wp-includes/capabilities.php +++ b/src/wp-includes/capabilities.php @@ -574,6 +574,23 @@ function map_meta_cap( $cap, $user_id ) { return call_user_func_array( 'map_meta_cap', $args ); } + // Block capabilities map to their post equivalent. + $block_caps = array( + 'edit_blocks', + 'edit_others_blocks', + 'publish_blocks', + 'read_private_blocks', + 'delete_blocks', + 'delete_private_blocks', + 'delete_published_blocks', + 'delete_others_blocks', + 'edit_private_blocks', + 'edit_published_blocks', + ); + if ( in_array( $cap, $block_caps, true ) ) { + $cap = str_replace( '_blocks', '_posts', $cap ); + } + // If no meta caps match, return the original cap. $caps[] = $cap; } diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index de8b83143e..d8ccf05ffb 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -252,6 +252,38 @@ function create_initial_post_types() { ) ); + register_post_type( + 'wp_block', + array( + 'labels' => array( + 'name' => __( 'Blocks' ), + 'singular_name' => __( 'Block' ), + 'search_items' => __( 'Search Blocks' ), + ), + 'public' => false, + '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ + 'show_ui' => true, + 'show_in_menu' => false, + 'rewrite' => false, + 'capability_type' => 'block', + 'capabilities' => array( + // You need to be able to edit posts, in order to read blocks in their raw form. + 'read' => 'edit_posts', + // You need to be able to publish posts, in order to create blocks. + 'create_posts' => 'publish_posts', + 'edit_published_posts' => 'edit_published_posts', + 'delete_published_posts' => 'delete_published_posts', + 'edit_others_posts' => 'edit_others_posts', + 'delete_others_posts' => 'delete_others_posts', + ), + 'map_meta_cap' => true, + 'supports' => array( + 'title', + 'editor', + ), + ) + ); + register_post_status( 'publish', array( diff --git a/tests/phpunit/tests/blocks/render-reusable.php b/tests/phpunit/tests/blocks/render-reusable.php new file mode 100644 index 0000000000..7188e3a897 --- /dev/null +++ b/tests/phpunit/tests/blocks/render-reusable.php @@ -0,0 +1,102 @@ +user->create( + array( + 'role' => 'editor', + ) + ); + + self::$post_id = $factory->post->create( + array( + 'post_author' => self::$user_id, + 'post_type' => 'post', + 'post_status' => 'publish', + 'post_title' => 'Test Post', + 'post_content' => '

Hello world!

', + ) + ); + + self::$block_id = $factory->post->create( + array( + 'post_author' => self::$user_id, + 'post_type' => 'wp_block', + 'post_status' => 'publish', + 'post_title' => 'Test Block', + 'post_content' => '

Hello world!

', + ) + ); + } + + /** + * Delete fake data after tests run. + * + * @since 5.0.0 + */ + public static function wpTearDownAfterClass() { + wp_delete_post( self::$block_id, true ); + wp_delete_post( self::$post_id, true ); + self::delete_user( self::$user_id ); + } + + public function test_render() { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/block' ); + $output = $block_type->render( array( 'ref' => self::$block_id ) ); + $this->assertSame( '

Hello world!

', $output ); + } + + public function test_ref_empty() { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/block' ); + $output = $block_type->render( array() ); + $this->assertSame( '', $output ); + } + + public function test_ref_wrong_post_type() { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/block' ); + $output = $block_type->render( array( 'ref' => self::$post_id ) ); + $this->assertSame( '', $output ); + } +} diff --git a/tests/phpunit/tests/user/capabilities.php b/tests/phpunit/tests/user/capabilities.php index 4019d0cd46..3bc5264d52 100644 --- a/tests/phpunit/tests/user/capabilities.php +++ b/tests/phpunit/tests/user/capabilities.php @@ -17,6 +17,8 @@ class Tests_User_Capabilities extends WP_UnitTestCase { ); protected static $super_admin = null; + protected static $block_id; + public static function wpSetUpBeforeClass( $factory ) { self::$users = array( 'administrator' => $factory->user->create_and_get( array( 'role' => 'administrator' ) ), @@ -27,6 +29,16 @@ class Tests_User_Capabilities extends WP_UnitTestCase { ); self::$super_admin = $factory->user->create_and_get( array( 'role' => 'contributor' ) ); grant_super_admin( self::$super_admin->ID ); + + self::$block_id = $factory->post->create( + array( + 'post_author' => self::$users['administrator']->ID, + 'post_type' => 'wp_block', + 'post_status' => 'publish', + 'post_title' => 'Test Block', + 'post_content' => '

Hello world!

', + ) + ); } function setUp() { @@ -36,6 +48,11 @@ class Tests_User_Capabilities extends WP_UnitTestCase { } + public static function wpTearDownAfterClass() { + wp_delete_post( self::$block_id, true ); + } + + function _flush_roles() { // we want to make sure we're testing against the db, not just in-memory data // this will flush everything and reload it from the db @@ -2095,4 +2112,76 @@ class Tests_User_Capabilities extends WP_UnitTestCase { $this->assertSame( 333, $roles->get_site_id() ); } + + /** + * @dataProvider data_block_caps + */ + function test_block_caps( $role, $cap, $use_post, $expected ) { + if ( $use_post ) { + $this->assertEquals( $expected, self::$users[ $role ]->has_cap( $cap, self::$block_id ) ); + } else { + $this->assertEquals( $expected, self::$users[ $role ]->has_cap( $cap ) ); + } + } + + function data_block_caps() { + $post_caps = array( + 'edit_block', + 'read_block', + 'delete_block', + ); + + $all_caps = array( + 'edit_block', + 'read_block', + 'delete_block', + 'edit_blocks', + 'edit_others_blocks', + 'publish_blocks', + 'read_private_blocks', + 'delete_blocks', + 'delete_private_blocks', + 'delete_published_blocks', + 'delete_others_blocks', + 'edit_private_blocks', + 'edit_published_blocks', + ); + + $roles = array( + 'administrator' => $all_caps, + 'editor' => $all_caps, + 'author' => array( + 'read_block', + 'edit_blocks', + 'publish_blocks', + 'delete_blocks', + 'delete_published_blocks', + 'edit_published_blocks', + ), + 'contributor' => array( + 'read_block', + 'edit_blocks', + 'delete_blocks', + ), + 'subscriber' => array(), + ); + + $data = array(); + + foreach ( $roles as $role => $caps ) { + foreach ( $caps as $cap ) { + $use_post = in_array( $cap, $post_caps, true ); + $data[] = array( $role, $cap, $use_post, true ); + } + + foreach ( $all_caps as $cap ) { + if ( ! in_array( $cap, $caps, true ) ) { + $use_post = in_array( $cap, $post_caps, true ); + $data[] = array( $role, $cap, $use_post, false ); + } + } + } + + return $data; + } }