<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[44146] trunk: Blocks: Add the reusable block post type, `wp_block`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/44146">44146</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/44146","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>jeremyfelt</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-12-14 02:14:13 +0000 (Fri, 14 Dec 2018)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Blocks: Add the reusable block post type, `wp_block`.

Merges <a href="https://core.trac.wordpress.org/changeset/43804">[43804]</a> from the 5.0 branch to trunk.

See <a href="https://core.trac.wordpress.org/ticket/45098">#45098</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadmineditphp">trunk/src/wp-admin/edit.php</a></li>
<li><a href="#trunksrcwpincludescapabilitiesphp">trunk/src/wp-includes/capabilities.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunktestsphpunittestsusercapabilitiesphp">trunk/tests/phpunit/tests/user/capabilities.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsblocksrenderreusablephp">trunk/tests/phpunit/tests/blocks/render-reusable.php</a></li>
</ul>

<h3>Property Changed</h3>
<ul>
<li><a href="#trunk">trunk/</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<span class="cx" style="display: block; padding: 0 10px">Index: trunk
</span><span class="cx" style="display: block; padding: 0 10px">===================================================================
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">--- trunk        2018-12-14 02:12:04 UTC (rev 44145)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+++ trunk 2018-12-14 02:14:13 UTC (rev 44146)
</ins><a id="trunk"></a>
<div class="propset"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Property changes: trunk</h4>
<pre class="diff"><span>
</span></pre></div>
<a id="svnmergeinfo"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: svn:mergeinfo</h4></div>
<span class="cx" style="display: block; padding: 0 10px"> /branches/3.3:20543
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/3.4:21757
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/4.9:43557,43622
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/branches/5.0:43681-43682,43684-43688,43719-43720,43723,43726-43727,43729-43731,43734-43744,43751-43754,43758,43761-43765,43767-43770,43772,43774-43781,43783,43790-43798,43802
</del><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/branches/5.0:43681-43682,43684-43688,43719-43720,43723,43726-43727,43729-43731,43734-43744,43751-43754,43758,43761-43765,43767-43770,43772,43774-43781,43783,43790-43798,43802,43804
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunksrcwpadmineditphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/edit.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/edit.php       2018-12-14 02:12:04 UTC (rev 44145)
+++ trunk/src/wp-admin/edit.php 2018-12-14 02:14:13 UTC (rev 44146)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -200,6 +200,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> wp_enqueue_script( 'inline-edit-post' );
</span><span class="cx" style="display: block; padding: 0 10px"> wp_enqueue_script( 'heartbeat' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+if ( 'wp_block' === $post_type ) {
+       wp_enqueue_script( 'wp-list-reusable-blocks' );
+       wp_enqueue_style( 'wp-list-reusable-blocks' );
+}
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $title = $post_type_object->labels->name;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> if ( 'post' == $post_type ) {
</span></span></pre></div>
<a id="trunksrcwpincludescapabilitiesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/capabilities.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/capabilities.php    2018-12-14 02:12:04 UTC (rev 44145)
+++ trunk/src/wp-includes/capabilities.php      2018-12-14 02:14:13 UTC (rev 44146)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -574,6 +574,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                return call_user_func_array( 'map_meta_cap', $args );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // 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 );
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         // If no meta caps match, return the original cap.
</span><span class="cx" style="display: block; padding: 0 10px">                        $caps[] = $cap;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span></span></pre></div>
<a id="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php    2018-12-14 02:12:04 UTC (rev 44145)
+++ trunk/src/wp-includes/post.php      2018-12-14 02:14:13 UTC (rev 44146)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -252,6 +252,38 @@
</span><span class="cx" style="display: block; padding: 0 10px">                )
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        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',
+                       ),
+               )
+       );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         register_post_status(
</span><span class="cx" style="display: block; padding: 0 10px">                'publish',
</span><span class="cx" style="display: block; padding: 0 10px">                array(
</span></span></pre></div>
<a id="trunktestsphpunittestsblocksrenderreusablephpfromrev43804branches50testsphpunittestsblocksrenderreusablephp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/tests/phpunit/tests/blocks/render-reusable.php (from rev 43804, branches/5.0/tests/phpunit/tests/blocks/render-reusable.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/render-reusable.php                              (rev 0)
+++ trunk/tests/phpunit/tests/blocks/render-reusable.php        2018-12-14 02:14:13 UTC (rev 44146)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,102 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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 );
+       }
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsusercapabilitiesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/user/capabilities.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/user/capabilities.php   2018-12-14 02:12:04 UTC (rev 44145)
+++ trunk/tests/phpunit/tests/user/capabilities.php     2018-12-14 02:14:13 UTC (rev 44146)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -17,6 +17,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><span class="cx" style="display: block; padding: 0 10px">        protected static $super_admin = null;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        protected static $block_id;
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public static function wpSetUpBeforeClass( $factory ) {
</span><span class="cx" style="display: block; padding: 0 10px">                self::$users       = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'administrator' => $factory->user->create_and_get( array( 'role' => 'administrator' ) ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -27,6 +29,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">                self::$super_admin = $factory->user->create_and_get( array( 'role' => 'contributor' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                grant_super_admin( self::$super_admin->ID );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               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 -->',
+                       )
+               );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        function setUp() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -36,6 +48,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        public static function wpTearDownAfterClass() {
+               wp_delete_post( self::$block_id, true );
+       }
+
+
</ins><span class="cx" style="display: block; padding: 0 10px">         function _flush_roles() {
</span><span class="cx" style="display: block; padding: 0 10px">                // we want to make sure we're testing against the db, not just in-memory data
</span><span class="cx" style="display: block; padding: 0 10px">                // this will flush everything and reload it from the db
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2095,4 +2112,76 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( 333, $roles->get_site_id() );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @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;
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>