Skip to content

Commit dfc5d7e

Browse files
committed
Add default rendering mode to post and page post types. Add rendering mode filters to REST controller.
1 parent a799101 commit dfc5d7e

File tree

2 files changed

+88
-34
lines changed

2 files changed

+88
-34
lines changed

src/wp-includes/post.php

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,46 +26,48 @@ function create_initial_post_types() {
2626
'labels' => array(
2727
'name_admin_bar' => _x( 'Post', 'add new from admin bar' ),
2828
),
29-
'public' => true,
30-
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
31-
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
32-
'capability_type' => 'post',
33-
'map_meta_cap' => true,
34-
'menu_position' => 5,
35-
'menu_icon' => 'dashicons-admin-post',
36-
'hierarchical' => false,
37-
'rewrite' => false,
38-
'query_var' => false,
39-
'delete_with_user' => true,
40-
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
41-
'show_in_rest' => true,
42-
'rest_base' => 'posts',
43-
'rest_controller_class' => 'WP_REST_Posts_Controller',
29+
'public' => true,
30+
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
31+
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
32+
'capability_type' => 'post',
33+
'map_meta_cap' => true,
34+
'menu_position' => 5,
35+
'menu_icon' => 'dashicons-admin-post',
36+
'hierarchical' => false,
37+
'rewrite' => false,
38+
'query_var' => false,
39+
'delete_with_user' => true,
40+
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
41+
'show_in_rest' => true,
42+
'rest_base' => 'posts',
43+
'rest_controller_class' => 'WP_REST_Posts_Controller',
44+
'default_rendering_mode' => 'post-only',
4445
)
4546
);
4647

4748
register_post_type(
4849
'page',
4950
array(
50-
'labels' => array(
51+
'labels' => array(
5152
'name_admin_bar' => _x( 'Page', 'add new from admin bar' ),
5253
),
53-
'public' => true,
54-
'publicly_queryable' => false,
55-
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
56-
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
57-
'capability_type' => 'page',
58-
'map_meta_cap' => true,
59-
'menu_position' => 20,
60-
'menu_icon' => 'dashicons-admin-page',
61-
'hierarchical' => true,
62-
'rewrite' => false,
63-
'query_var' => false,
64-
'delete_with_user' => true,
65-
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
66-
'show_in_rest' => true,
67-
'rest_base' => 'pages',
68-
'rest_controller_class' => 'WP_REST_Posts_Controller',
54+
'public' => true,
55+
'publicly_queryable' => false,
56+
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
57+
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
58+
'capability_type' => 'page',
59+
'map_meta_cap' => true,
60+
'menu_position' => 20,
61+
'menu_icon' => 'dashicons-admin-page',
62+
'hierarchical' => true,
63+
'rewrite' => false,
64+
'query_var' => false,
65+
'delete_with_user' => true,
66+
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
67+
'show_in_rest' => true,
68+
'rest_base' => 'pages',
69+
'rest_controller_class' => 'WP_REST_Posts_Controller',
70+
'default_rendering_mode' => 'template-lock',
6971
)
7072
);
7173

@@ -2326,6 +2328,25 @@ function get_post_types_by_support( $feature, $operator = 'and' ) {
23262328
return array_keys( wp_filter_object_list( $_wp_post_type_features, $features, $operator ) );
23272329
}
23282330

2331+
/**
2332+
* Get the available rendering modes for the Block Editor.
2333+
*
2334+
* post-only: This mode extracts the post blocks from the template and renders only those.
2335+
* The idea is to allow the user to edit the post/page in isolation without the wrapping template.
2336+
*
2337+
* template-locked: This mode renders both the template and the post blocks
2338+
* but the template blocks are locked and can't be edited. The post blocks are editable.
2339+
*
2340+
* @return array Array of available rendering modes.
2341+
*/
2342+
function get_post_type_rendering_modes() {
2343+
return array(
2344+
'post-only',
2345+
'template-lock',
2346+
'template-locked',
2347+
);
2348+
}
2349+
23292350
/**
23302351
* Updates the post type for the post ID.
23312352
*

src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,41 @@ public function prepare_item_for_response( $item, $request ) {
255255
}
256256

257257
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
258-
$data = $this->add_additional_fields_to_object( $data, $request );
259-
$data = $this->filter_response_by_context( $data, $context );
258+
259+
// Editor rendering mode.
260+
if ( wp_is_block_theme() && 'edit' === $context && property_exists( $post_type, 'default_rendering_mode' ) ) {
261+
/**
262+
* Filters the block editor rendering mode for a specific post type.
263+
*
264+
* The dynamic portion of the hook name, `$item->name`, refers to the post type slug.
265+
*
266+
* @since 6.7.0
267+
* @param string $default_rendering_mode Default rendering mode for the post type.
268+
* @param WP_Post_Type $post_type Post type object.
269+
* @return string Default rendering mode for the post type.
270+
*/
271+
$rendering_mode = apply_filters( "post_type_{$post_type->name}_default_rendering_mode", $post_type->default_rendering_mode, $post_type );
272+
273+
/**
274+
* Filters the block editor rendering mode for a post type.
275+
*
276+
* @since 6.7.0
277+
* @param string $default_rendering_mode Default rendering mode for the post type.
278+
* @param WP_Post_Type $post_type Post type name.
279+
* @return string Default rendering mode for the post type.
280+
*/
281+
$rendering_mode = apply_filters( 'post_type_default_rendering_mode', $rendering_mode, $post_type );
282+
283+
// Validate the filtered rendering mode.
284+
if ( ! in_array( $rendering_mode, get_post_type_rendering_modes(), true ) ) {
285+
$rendering_mode = 'post-only';
286+
}
287+
288+
$data['default_rendering_mode'] = $rendering_mode;
289+
}
290+
291+
$data = $this->add_additional_fields_to_object( $data, $request );
292+
$data = $this->filter_response_by_context( $data, $context );
260293

261294
// Wrap the data in a response object.
262295
$response = rest_ensure_response( $data );

0 commit comments

Comments
 (0)