-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Set editor rendering mode by post type (#62304)
Co-authored-by: TylerB24890 <[email protected]> Co-authored-by: Sidsector9 <[email protected]> Co-authored-by: fabiankaegy <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: dcalhoun <[email protected]> Co-authored-by: ramonjd <[email protected]> Co-authored-by: mcsf <[email protected]> Co-authored-by: jasmussen <[email protected]> Co-authored-by: annezazu <[email protected]> Co-authored-by: jameskoster <[email protected]> Co-authored-by: dinhtungdu <[email protected]>
- Loading branch information
1 parent
cf59c28
commit c43a0c9
Showing
12 changed files
with
243 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/WordPress/wordpress-develop/pull/7129 | ||
|
||
* https://github.com/WordPress/gutenberg/pull/62304 |
61 changes: 61 additions & 0 deletions
61
lib/compat/wordpress-6.8/class-gutenberg-rest-post-types-controller-6-8.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* REST API: Gutenberg_REST_Post_Types_Controller_6_8 class | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Gutenberg_REST_Post_Types_Controller_6_8 class | ||
* | ||
* Add Block Editor default rendering mode to the post type response | ||
* to allow enabling/disabling at the post type level. | ||
*/ | ||
class Gutenberg_REST_Post_Types_Controller_6_8 extends WP_REST_Post_Types_Controller { | ||
/** | ||
* Add Block Editor default rendering mode setting to the response. | ||
* | ||
* @param WP_Post_Type $item Post type object. | ||
* @param WP_REST_Request $request Request object. | ||
* @return WP_REST_Response Response object. | ||
*/ | ||
public function prepare_item_for_response( $item, $request ) { | ||
$response = parent::prepare_item_for_response( $item, $request ); | ||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; | ||
|
||
// Property will only exist if the post type supports the block editor. | ||
if ( 'edit' === $context && property_exists( $item, 'default_rendering_mode' ) ) { | ||
/** | ||
* Filters the block editor rendering mode for a post type. | ||
* | ||
* @since 6.8.0 | ||
* @param string $default_rendering_mode Default rendering mode for the post type. | ||
* @param WP_Post_Type $post_type Post type name. | ||
* @return string Default rendering mode for the post type. | ||
*/ | ||
$rendering_mode = apply_filters( 'post_type_default_rendering_mode', $item->default_rendering_mode, $item ); | ||
|
||
/** | ||
* Filters the block editor rendering mode for a specific post type. | ||
* Applied after the generic `post_type_default_rendering_mode` filter. | ||
* | ||
* The dynamic portion of the hook name, `$item->name`, refers to the post type slug. | ||
* | ||
* @since 6.8.0 | ||
* @param string $default_rendering_mode Default rendering mode for the post type. | ||
* @param WP_Post_Type $post_type Post type object. | ||
* @return string Default rendering mode for the post type. | ||
*/ | ||
$rendering_mode = apply_filters( "post_type_{$item->name}_default_rendering_mode", $rendering_mode, $item ); | ||
|
||
// Validate the filtered rendering mode. | ||
if ( ! in_array( $rendering_mode, gutenberg_post_type_rendering_modes(), true ) ) { | ||
$rendering_mode = 'post-only'; | ||
} | ||
|
||
$response->data['default_rendering_mode'] = $rendering_mode; | ||
} | ||
|
||
return rest_ensure_response( $response ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Temporary compatibility shims for block APIs present in Gutenberg. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Get the available rendering modes for the Block Editor. | ||
* | ||
* post-only: This mode extracts the post blocks from the template and renders only those. | ||
* The idea is to allow the user to edit the post/page in isolation without the wrapping template. | ||
* | ||
* template-locked: This mode renders both the template and the post blocks | ||
* but the template blocks are locked and can't be edited. The post blocks are editable. | ||
* | ||
* @return array Array of available rendering modes. | ||
*/ | ||
function gutenberg_post_type_rendering_modes() { | ||
return array( | ||
'post-only', | ||
'template-locked', | ||
); | ||
} | ||
|
||
/** | ||
* Add the default_rendering_mode property to the WP_Post_Type object. | ||
* This property can be overwritten by using the post_type_default_rendering_mode filter. | ||
* | ||
* @param array $args Array of post type arguments. | ||
* @param string $post_type Post type key. | ||
* @return array Updated array of post type arguments. | ||
*/ | ||
function gutenberg_post_type_default_rendering_mode( $args, $post_type ) { | ||
$rendering_mode = 'page' === $post_type ? 'template-locked' : 'post-only'; | ||
$rendering_modes = gutenberg_post_type_rendering_modes(); | ||
|
||
// Make sure the post type supports the block editor. | ||
if ( | ||
wp_is_block_theme() && | ||
( isset( $args['show_in_rest'] ) && $args['show_in_rest'] ) && | ||
( isset( $args['supports'] ) && in_array( 'editor', $args['supports'], true ) ) | ||
) { | ||
// Validate the supplied rendering mode. | ||
if ( | ||
isset( $args['default_rendering_mode'] ) && | ||
in_array( $args['default_rendering_mode'], $rendering_modes, true ) | ||
) { | ||
$rendering_mode = $args['default_rendering_mode']; | ||
} | ||
|
||
$args['default_rendering_mode'] = $rendering_mode; | ||
} | ||
|
||
return $args; | ||
} | ||
add_filter( 'register_post_type_args', 'gutenberg_post_type_default_rendering_mode', 10, 2 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
/** | ||
* PHP and WordPress configuration compatibility functions for the Gutenberg | ||
* editor plugin changes related to REST API. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
if ( ! defined( 'ABSPATH' ) ) { | ||
die( 'Silence is golden.' ); | ||
} | ||
|
||
if ( ! function_exists( 'gutenberg_add_post_type_rendering_mode' ) ) { | ||
/** | ||
* Add Block Editor default rendering mode to the post type response. | ||
*/ | ||
function gutenberg_add_post_type_rendering_mode() { | ||
$controller = new Gutenberg_REST_Post_Types_Controller_6_8(); | ||
$controller->register_routes(); | ||
} | ||
} | ||
add_action( 'rest_api_init', 'gutenberg_add_post_type_rendering_mode' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.