Skip to content

Commit

Permalink
Blocks: Add the reusable block post type, wp_block.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jeremyfelt committed Dec 14, 2018
1 parent 93836b4 commit 6a84afa
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/wp-admin/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@
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 ) {
Expand Down
17 changes: 17 additions & 0 deletions src/wp-includes/capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
32 changes: 32 additions & 0 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
102 changes: 102 additions & 0 deletions tests/phpunit/tests/blocks/render-reusable.php
Original file line number Diff line number Diff line change
@@ -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 );
}
}
89 changes: 89 additions & 0 deletions tests/phpunit/tests/user/capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) ),
Expand All @@ -27,6 +29,16 @@ public static function wpSetUpBeforeClass( $factory ) {
);
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() {
Expand All @@ -36,6 +48,11 @@ function setUp() {

}

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
Expand Down Expand Up @@ -2095,4 +2112,76 @@ function test_roles_get_site_id() {

$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;
}
}

0 comments on commit 6a84afa

Please sign in to comment.