Make WordPress Core

Changeset 44146


Ignore:
Timestamp:
12/14/2018 02:14:13 AM (8 years ago)
Author:
jeremyfelt
Message:

Blocks: Add the reusable block post type, wp_block.

Merges [43804] from the 5.0 branch to trunk.

See #45098.

Location:
trunk
Files:
5 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-admin/edit.php

    r43571 r44146  
    200200wp_enqueue_script( 'inline-edit-post' );
    201201wp_enqueue_script( 'heartbeat' );
     202
     203if ( 'wp_block' === $post_type ) {
     204        wp_enqueue_script( 'wp-list-reusable-blocks' );
     205        wp_enqueue_style( 'wp-list-reusable-blocks' );
     206}
    202207
    203208$title = $post_type_object->labels->name;
  • trunk/src/wp-includes/capabilities.php

    r43520 r44146  
    575575                        }
    576576
     577                        // Block capabilities map to their post equivalent.
     578                        $block_caps = array(
     579                                'edit_blocks',
     580                                'edit_others_blocks',
     581                                'publish_blocks',
     582                                'read_private_blocks',
     583                                'delete_blocks',
     584                                'delete_private_blocks',
     585                                'delete_published_blocks',
     586                                'delete_others_blocks',
     587                                'edit_private_blocks',
     588                                'edit_published_blocks',
     589                        );
     590                        if ( in_array( $cap, $block_caps, true ) ) {
     591                                $cap = str_replace( '_blocks', '_posts', $cap );
     592                        }
     593
    577594                        // If no meta caps match, return the original cap.
    578595                        $caps[] = $cap;
  • trunk/src/wp-includes/post.php

    r44110 r44146  
    250250                        'delete_with_user' => false,
    251251                        'supports'         => array(),
     252                )
     253        );
     254
     255        register_post_type(
     256                'wp_block',
     257                array(
     258                        'labels'          => array(
     259                                'name'          => __( 'Blocks' ),
     260                                'singular_name' => __( 'Block' ),
     261                                'search_items'  => __( 'Search Blocks' ),
     262                        ),
     263                        'public'          => false,
     264                        '_builtin'        => true, /* internal use only. don't use this when registering your own post type. */
     265                        'show_ui'         => true,
     266                        'show_in_menu'    => false,
     267                        'rewrite'         => false,
     268                        'capability_type' => 'block',
     269                        'capabilities'    => array(
     270                                // You need to be able to edit posts, in order to read blocks in their raw form.
     271                                'read'                   => 'edit_posts',
     272                                // You need to be able to publish posts, in order to create blocks.
     273                                'create_posts'           => 'publish_posts',
     274                                'edit_published_posts'   => 'edit_published_posts',
     275                                'delete_published_posts' => 'delete_published_posts',
     276                                'edit_others_posts'      => 'edit_others_posts',
     277                                'delete_others_posts'    => 'delete_others_posts',
     278                        ),
     279                        'map_meta_cap'    => true,
     280                        'supports'        => array(
     281                                'title',
     282                                'editor',
     283                        ),
    252284                )
    253285        );
  • trunk/tests/phpunit/tests/user/capabilities.php

    r43571 r44146  
    1818        protected static $super_admin = null;
    1919
     20        protected static $block_id;
     21
    2022        public static function wpSetUpBeforeClass( $factory ) {
    2123                self::$users       = array(
    … …  
    2830                self::$super_admin = $factory->user->create_and_get( array( 'role' => 'contributor' ) );
    2931                grant_super_admin( self::$super_admin->ID );
     32
     33                self::$block_id = $factory->post->create(
     34                        array(
     35                                'post_author'  => self::$users['administrator']->ID,
     36                                'post_type'    => 'wp_block',
     37                                'post_status'  => 'publish',
     38                                'post_title'   => 'Test Block',
     39                                'post_content' => '<!-- wp:core/paragraph --><p>Hello world!</p><!-- /wp:core/paragraph -->',
     40                        )
     41                );
    3042        }
    3143
    … …  
    3648
    3749        }
     50
     51        public static function wpTearDownAfterClass() {
     52                wp_delete_post( self::$block_id, true );
     53        }
     54
    3855
    3956        function _flush_roles() {
    … …  
    20962113                $this->assertSame( 333, $roles->get_site_id() );
    20972114        }
     2115
     2116        /**
     2117         * @dataProvider data_block_caps
     2118         */
     2119        function test_block_caps( $role, $cap, $use_post, $expected ) {
     2120                if ( $use_post ) {
     2121                        $this->assertEquals( $expected, self::$users[ $role ]->has_cap( $cap, self::$block_id ) );
     2122                } else {
     2123                        $this->assertEquals( $expected, self::$users[ $role ]->has_cap( $cap ) );
     2124                }
     2125        }
     2126
     2127        function data_block_caps() {
     2128                $post_caps = array(
     2129                        'edit_block',
     2130                        'read_block',
     2131                        'delete_block',
     2132                );
     2133
     2134                $all_caps = array(
     2135                        'edit_block',
     2136                        'read_block',
     2137                        'delete_block',
     2138                        'edit_blocks',
     2139                        'edit_others_blocks',
     2140                        'publish_blocks',
     2141                        'read_private_blocks',
     2142                        'delete_blocks',
     2143                        'delete_private_blocks',
     2144                        'delete_published_blocks',
     2145                        'delete_others_blocks',
     2146                        'edit_private_blocks',
     2147                        'edit_published_blocks',
     2148                );
     2149
     2150                $roles = array(
     2151                        'administrator' => $all_caps,
     2152                        'editor'        => $all_caps,
     2153                        'author'        => array(
     2154                                'read_block',
     2155                                'edit_blocks',
     2156                                'publish_blocks',
     2157                                'delete_blocks',
     2158                                'delete_published_blocks',
     2159                                'edit_published_blocks',
     2160                        ),
     2161                        'contributor'   => array(
     2162                                'read_block',
     2163                                'edit_blocks',
     2164                                'delete_blocks',
     2165                        ),
     2166                        'subscriber'    => array(),
     2167                );
     2168
     2169                $data = array();
     2170
     2171                foreach ( $roles as $role => $caps ) {
     2172                        foreach ( $caps as $cap ) {
     2173                                $use_post = in_array( $cap, $post_caps, true );
     2174                                $data[]   = array( $role, $cap, $use_post, true );
     2175                        }
     2176
     2177                        foreach ( $all_caps as $cap ) {
     2178                                if ( ! in_array( $cap, $caps, true ) ) {
     2179                                        $use_post = in_array( $cap, $post_caps, true );
     2180                                        $data[]   = array( $role, $cap, $use_post, false );
     2181                                }
     2182                        }
     2183                }
     2184
     2185                return $data;
     2186        }
    20982187}
Note: See TracChangeset for help on using the changeset viewer.