Skip to content

Commit

Permalink
REST API: Simplify 'default_rendering_mode' field registration for po…
Browse files Browse the repository at this point in the history
…st types
  • Loading branch information
Mamaduka committed Dec 12, 2024
1 parent 795e9c2 commit 8479b4d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,36 @@
*/

/**
* 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 );
function gutenberg_editor_rendering_mode_field() {
register_rest_field( 'type', 'default_rendering_mode', array(

Check failure on line 16 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

Opening parenthesis of a multi-line function call must be the last content on the line

Check failure on line 16 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

Only one argument is allowed per line in a multi-line function call

Check failure on line 16 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

Only one argument is allowed per line in a multi-line function call
'get_callback' => static function( $object ) {

Check failure on line 17 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

Expected 1 space after FUNCTION keyword; 0 found

Check warning on line 17 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'] );

/**
* 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 );
// Property will only exist if the post type supports the block editor.
if ( ! $post_type_object || ! isset( $post_type_object->default_rendering_mode ) ) {
return '';
}

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

$response->data['default_rendering_mode'] = $rendering_mode;
}

return rest_ensure_response( $response );
}
return $post_type_object->default_rendering_mode;
},
'schema' => array(

Check warning on line 32 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

Array double arrow not aligned correctly; expected 7 space(s) between "'schema'" and double arrow, but found 1.
'description' => __( 'The rendering mode for the editor.', 'gutenberg' ),
'type' => 'string',
'enum' => array( 'post-only', 'template-locked' ),
'context' => array( 'edit' ),
'readonly' => true,
),
) );

Check failure on line 39 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

Closing parenthesis of a multi-line function call must be on a line by itself
}
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

0 comments on commit 8479b4d

Please sign in to comment.