Blocks: Add the reusable block post type, wp_block.

See #45098.



git-svn-id: https://develop.svn.wordpress.org/branches/5.0@43804 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2018-10-23 06:52:03 +00:00
parent 4bdfa3d5ea
commit 2b18dcdecd
5 changed files with 246 additions and 0 deletions

View File

@ -182,6 +182,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 ) {

View File

@ -555,6 +555,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;
}

View File

@ -225,6 +225,39 @@ function create_initial_post_types() {
'supports' => array(),
) );
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(
'label' => _x( 'Published', 'post status' ),
'public' => true,

View File

@ -0,0 +1,102 @@
<?php
/**
* Reusable block rendering tests.
*
* @package WordPress
* @subpackage Blocks
* @since 5.0.0
*/
/**
* Tests for reusable block rendering.
*
* @since 5.0.0
*
* @group blocks
*/
class WP_Test_Render_Reusable_Blocks extends WP_UnitTestCase {
/**
* Fake user ID.
*
* @var int
*/
protected static $user_id;
/**
* Fake block ID.
*
* @var int
*/
protected static $block_id;
/**
* Fake post ID.
*
* @var int
*/
protected static $post_id;
/**
* Create fake data before tests run.
*
* @since 5.0.0
*
* @param WP_UnitTest_Factory $factory Helper that creates fake data.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$user_id = $factory->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' => '<p>Hello world!</p>',
)
);
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' => '<!-- wp:core/paragraph --><p>Hello world!</p><!-- /wp:core/paragraph -->',
)
);
}
/**
* 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( '<p>Hello world!</p>', $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 );
}
}

View File

@ -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' => '<!-- wp:core/paragraph --><p>Hello world!</p><!-- /wp:core/paragraph -->',
)
);
}
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
@ -2016,4 +2033,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;
}
}