Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API: Simplify 'default_rendering_mode' field registration for post types #67867

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backport-changelog/6.8/7129.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ https://github.com/WordPress/wordpress-develop/pull/7129

* https://github.com/WordPress/gutenberg/pull/62304
* https://github.com/WordPress/gutenberg/pull/67879
* https://github.com/WordPress/gutenberg/pull/67867
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,40 @@
*/

/**
* Gutenberg_REST_Post_Types_Controller_6_8 class
* Register a REST field for the default rendering mode of a post type.
*
* Add Block Editor default rendering mode to the post type response
* to allow enabling/disabling at the post type level.
* Note: Logic will become part of the `prepare_item_for_response` method when backporting to the core.
*
* @return void
*/
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 );
function gutenberg_editor_rendering_mode_field() {
register_rest_field(
'type',
'default_rendering_mode',
array(
'get_callback' => static function ( $object ) {

Check warning on line 20 in lib/compat/wordpress-6.8/class-gutenberg-rest-post-types-controller-6-8.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

It is recommended not to use reserved keyword "object" as function parameter name. Found: $object
$post_type_object = get_post_type_object( $object['slug'] );

// Validate the filtered rendering mode.
if ( ! in_array( $rendering_mode, gutenberg_post_type_rendering_modes(), true ) ) {
$rendering_mode = 'post-only';
}
// Property will only exist if the post type supports the block editor.
if ( ! $post_type_object || ! isset( $post_type_object->default_rendering_mode ) ) {
return 'post-only';
}

$response->data['default_rendering_mode'] = $rendering_mode;
}
// Validate the filtered rendering mode.
if ( ! in_array( $post_type_object->default_rendering_mode, gutenberg_post_type_rendering_modes(), true ) ) {
return 'post-only';
}

return rest_ensure_response( $response );
}
return $post_type_object->default_rendering_mode;
},
'schema' => array(
'description' => __( 'The rendering mode for the editor.', 'gutenberg' ),
'type' => 'string',
'enum' => array( 'post-only', 'template-locked' ),
'context' => array( 'edit' ),
'readonly' => true,
),
)
);
}
add_action( 'rest_api_init', 'gutenberg_editor_rendering_mode_field' );
11 changes: 0 additions & 11 deletions lib/compat/wordpress-6.8/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@
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' );

// When querying terms for a given taxonomy in the REST API, respect the default
// query arguments set for that taxonomy upon registration.
function gutenberg_respect_taxonomy_default_args_in_rest_api( $args ) {
Expand Down
18 changes: 10 additions & 8 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,25 @@ export const ExperimentalEditorProvider = withRegistryProvider(
getRenderingMode,
__unstableIsEditorReady,
} = select( editorStore );
const { getEntitiesConfig } = select( coreStore );
const {
getEntitiesConfig,
getPostType,
hasFinishedResolution,
} = select( coreStore );

const postTypeObject = select( coreStore ).getPostType(
post.type
const postTypeObject = getPostType( post.type );
const _hasLoadedPostObject = hasFinishedResolution(
'getPostType',
[ post.type ]
);

const _hasLoadedPostObject = select(
coreStore
).hasFinishedResolution( 'getPostType', [ post.type ] );

return {
hasLoadedPostObject: _hasLoadedPostObject,
editorSettings: getEditorSettings(),
isReady: __unstableIsEditorReady(),
mode: getRenderingMode(),
defaultMode:
postTypeObject?.default_rendering_mode ?? 'post-only',
postTypeObject?.default_rendering_mode || 'post-only',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just extra hardening to provide correct fallback on falsy values like empty string

selection: getEditorSelection(),
postTypeEntities:
post.type === 'wp_template'
Expand Down
Loading