REST API: Remove experimental block menu item types.

The menu items REST API controller was added in [52079]. This included functionality to add a "block" menu item type. This functionality is experimental and not currently used in WordPress core, so should be removed. 

Props noisysocks.
See #40878.



git-svn-id: https://develop.svn.wordpress.org/trunk@52184 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris 2021-11-16 17:07:43 +00:00
parent e411a1536a
commit f4e75ee8b7
4 changed files with 6 additions and 270 deletions

View File

@ -406,11 +406,10 @@ function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
/**
* Save the properties of a menu item or create a new one.
*
* The menu-item-title, menu-item-description, menu-item-attr-title, and menu-item-content are expected
* The menu-item-title, menu-item-description and menu-item-attr-title are expected
* to be pre-slashed since they are passed directly to APIs that expect slashed data.
*
* @since 3.0.0
* @since 5.9.0 Added the menu-item-content parameter.
*
* @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan.
* @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
@ -449,7 +448,6 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
'menu-item-attr-title' => '',
'menu-item-target' => '',
'menu-item-classes' => '',
'menu-item-content' => '',
'menu-item-xfn' => '',
'menu-item-status' => '',
'menu-item-post-date' => '',
@ -574,7 +572,6 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] );
update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw( $args['menu-item-url'] ) );
update_post_meta( $menu_item_db_id, '_menu_item_content', $args['menu-item-content'] );
if ( 0 == $menu_id ) {
update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() );
@ -913,10 +910,6 @@ function wp_setup_nav_menu_item( $menu_item ) {
$menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title;
} elseif ( 'block' === $menu_item->type ) {
$menu_item->type_label = __( 'Block' );
$menu_item->title = $menu_item->post_title;
$menu_item->menu_item_content = ! isset( $menu_item->menu_item_content ) ? get_post_meta( $menu_item->ID, '_menu_item_content', true ) : $menu_item->menu_item_content;
} else {
$menu_item->type_label = __( 'Custom Link' );
$menu_item->title = $menu_item->post_title;

View File

@ -339,7 +339,6 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
'menu-item-title' => $menu_item_obj->title,
'menu-item-url' => $menu_item_obj->url,
'menu-item-description' => $menu_item_obj->description,
'menu-item-content' => $menu_item_obj->menu_item_content,
'menu-item-attr-title' => $menu_item_obj->attr_title,
'menu-item-target' => $menu_item_obj->target,
'menu-item-classes' => $menu_item_obj->classes,
@ -360,7 +359,6 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
'menu-item-title' => '',
'menu-item-url' => '',
'menu-item-description' => '',
'menu-item-content' => '',
'menu-item-attr-title' => '',
'menu-item-target' => '',
'menu-item-classes' => array(),
@ -409,15 +407,6 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
}
}
// Nav menu content.
if ( ! empty( $schema['properties']['content'] ) && isset( $request['content'] ) ) {
if ( is_string( $request['content'] ) ) {
$prepared_nav_item['menu-item-content'] = $request['content'];
} elseif ( isset( $request['content']['raw'] ) ) {
$prepared_nav_item['menu-item-content'] = $request['content']['raw'];
}
}
$error = new WP_Error();
// Check if object id exists before saving.
@ -460,11 +449,6 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
}
}
// If menu item is type block, then content is required.
if ( 'block' === $prepared_nav_item['menu-item-type'] && empty( $prepared_nav_item['menu-item-content'] ) ) {
$error->add( 'rest_content_required', __( 'The content is required when using a block menu item type.' ), array( 'status' => 400 ) );
}
if ( $error->has_errors() ) {
return $error;
}
@ -566,23 +550,6 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
$data['object_id'] = absint( $menu_item->object_id );
}
if ( rest_is_field_included( 'content', $fields ) ) {
$data['content'] = array();
}
if ( rest_is_field_included( 'content.raw', $fields ) ) {
$data['content']['raw'] = $menu_item->menu_item_content;
}
if ( rest_is_field_included( 'content.rendered', $fields ) ) {
/** This filter is documented in wp-includes/post-template.php */
$data['content']['rendered'] = apply_filters( 'the_content', $menu_item->menu_item_content );
}
if ( rest_is_field_included( 'content.block_version', $fields ) ) {
$data['content']['block_version'] = block_version( $menu_item->menu_item_content );
}
if ( rest_is_field_included( 'parent', $fields ) ) {
// Same as post_parent, exposed as an integer.
$data['parent'] = (int) $menu_item->menu_item_parent;
@ -781,7 +748,7 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
$schema['properties']['type'] = array(
'description' => __( 'The family of objects originally represented, such as "post_type" or "taxonomy".' ),
'type' => 'string',
'enum' => array( 'taxonomy', 'post_type', 'post_type_archive', 'custom', 'block' ),
'enum' => array( 'taxonomy', 'post_type', 'post_type_archive', 'custom' ),
'context' => array( 'view', 'edit', 'embed' ),
'default' => 'custom',
);
@ -859,31 +826,6 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
'default' => 0,
);
$schema['properties']['content'] = array(
'description' => __( 'HTML content to display for this block menu item.' ),
'context' => array( 'view', 'edit', 'embed' ),
'type' => array( 'string', 'object' ),
'properties' => array(
'raw' => array(
'description' => __( 'HTML content, as it exists in the database.' ),
'type' => 'string',
'context' => array( 'edit' ),
),
'rendered' => array(
'description' => __( 'HTML content, transformed for display.' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'readonly' => true,
),
'block_version' => array(
'description' => __( 'Version of the block format used in the HTML content.' ),
'type' => 'integer',
'context' => array( 'edit' ),
'readonly' => true,
),
),
);
$schema['properties']['target'] = array(
'description' => __( 'The target attribute of the link element for this menu item.' ),
'type' => 'string',

View File

@ -607,48 +607,6 @@ class Tests_REST_WpRestMenuItemsController extends WP_Test_REST_Post_Type_Contro
$this->assertSame( 'rest_invalid_url', $response->get_data()['data']['details']['url']['code'] );
}
/**
* Tests that a block menu item can be created.
*
* @ticket 40878
* @covers ::create_item
*/
public function test_create_item_block() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
$params = $this->set_menu_item_data(
array(
'type' => 'block',
'content' => '<!-- wp:paragraph --><p>Block content</p><!-- /wp:paragraph -->',
)
);
$request->set_body_params( $params );
$response = rest_get_server()->dispatch( $request );
$this->check_create_menu_item_response( $response );
}
/**
* Tests that a block menu item can be created.
*
* @ticket 40878
* @covers ::create_item
*/
public function test_create_item_invalid_block_content() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );
$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
$params = $this->set_menu_item_data(
array(
'type' => 'block',
)
);
$request->set_body_params( $params );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_content_required', $response, 400 );
}
/**
* @ticket 40878
* @covers ::update_item
@ -679,43 +637,6 @@ class Tests_REST_WpRestMenuItemsController extends WP_Test_REST_Post_Type_Contro
$this->assertSame( $params['xfn'], explode( ' ', $menu_item->xfn ) );
}
/**
* @ticket 40878
* @covers ::update_item
*/
public function test_update_item_preserves_type() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );
$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
$params = array(
'status' => 'draft',
'type' => 'block',
'title' => 'TEST',
'content' => '<!-- wp:paragraph --><p>Block content</p><!-- /wp:paragraph -->',
);
$request->set_body_params( $params );
$response = rest_get_server()->dispatch( $request );
$this->check_update_menu_item_response( $response );
$new_data = $response->get_data();
$this->assertSame( 'block', $new_data['type'] );
$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );
$request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
$params = array(
'status' => 'draft',
'title' => 'TEST2',
'content' => '<!-- wp:paragraph --><p>Block content</p><!-- /wp:paragraph -->',
);
$request->set_body_params( $params );
$response = rest_get_server()->dispatch( $request );
$this->check_update_menu_item_response( $response );
$new_data = $response->get_data();
// The type shouldn't change just because it was missing from request args.
$this->assertSame( 'block', $new_data['type'] );
}
/**
* @ticket 40878
* @covers ::update_item
@ -825,12 +746,11 @@ class Tests_REST_WpRestMenuItemsController extends WP_Test_REST_Post_Type_Contro
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertSame( 19, count( $properties ) );
$this->assertSame( 18, count( $properties ) );
$this->assertArrayHasKey( 'type_label', $properties );
$this->assertArrayHasKey( 'attr_title', $properties );
$this->assertArrayHasKey( 'classes', $properties );
$this->assertArrayHasKey( 'description', $properties );
$this->assertArrayHasKey( 'content', $properties );
$this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'url', $properties );
$this->assertArrayHasKey( 'meta', $properties );
@ -957,20 +877,6 @@ class Tests_REST_WpRestMenuItemsController extends WP_Test_REST_Post_Type_Contro
$this->assertFalse( isset( $data['title'] ) );
}
// Check content.
if ( 'block' === $data['type'] ) {
$menu_item_content = get_post_meta( $post->ID, '_menu_item_content', true );
$this->assertSame( apply_filters( 'the_content', $menu_item_content ), $data['content']['rendered'] );
if ( 'edit' === $context ) {
$this->assertSame( $menu_item_content, $data['content']['raw'] );
} else {
$this->assertFalse( isset( $data['title']['raw'] ) );
}
$this->assertSame( 1, $data['content']['block_version'] );
} else {
$this->assertEmpty( $data['content']['rendered'] );
}
// post_parent.
$this->assertArrayHasKey( 'parent', $data );
if ( $post->post_parent ) {

View File

@ -3705,8 +3705,7 @@ mockedApiResponse.Schema = {
"taxonomy",
"post_type",
"post_type_archive",
"custom",
"block"
"custom"
],
"required": false
},
@ -3767,40 +3766,6 @@ mockedApiResponse.Schema = {
"minimum": 0,
"required": false
},
"content": {
"description": "HTML content to display for this block menu item.",
"type": [
"string",
"object"
],
"properties": {
"raw": {
"description": "HTML content, as it exists in the database.",
"type": "string",
"context": [
"edit"
]
},
"rendered": {
"description": "HTML content, transformed for display.",
"type": "string",
"context": [
"view",
"edit"
],
"readonly": true
},
"block_version": {
"description": "Version of the block format used in the HTML content.",
"type": "integer",
"context": [
"edit"
],
"readonly": true
}
},
"required": false
},
"target": {
"description": "The target attribute of the link element for this menu item.",
"type": "string",
@ -3931,8 +3896,7 @@ mockedApiResponse.Schema = {
"taxonomy",
"post_type",
"post_type_archive",
"custom",
"block"
"custom"
],
"required": false
},
@ -3989,40 +3953,6 @@ mockedApiResponse.Schema = {
"minimum": 0,
"required": false
},
"content": {
"description": "HTML content to display for this block menu item.",
"type": [
"string",
"object"
],
"properties": {
"raw": {
"description": "HTML content, as it exists in the database.",
"type": "string",
"context": [
"edit"
]
},
"rendered": {
"description": "HTML content, transformed for display.",
"type": "string",
"context": [
"view",
"edit"
],
"readonly": true
},
"block_version": {
"description": "Version of the block format used in the HTML content.",
"type": "integer",
"context": [
"edit"
],
"readonly": true
}
},
"required": false
},
"target": {
"description": "The target attribute of the link element for this menu item.",
"type": "string",
@ -4157,8 +4087,7 @@ mockedApiResponse.Schema = {
"taxonomy",
"post_type",
"post_type_archive",
"custom",
"block"
"custom"
],
"required": false
},
@ -4209,40 +4138,6 @@ mockedApiResponse.Schema = {
"minimum": 0,
"required": false
},
"content": {
"description": "HTML content to display for this block menu item.",
"type": [
"string",
"object"
],
"properties": {
"raw": {
"description": "HTML content, as it exists in the database.",
"type": "string",
"context": [
"edit"
]
},
"rendered": {
"description": "HTML content, transformed for display.",
"type": "string",
"context": [
"view",
"edit"
],
"readonly": true
},
"block_version": {
"description": "Version of the block format used in the HTML content.",
"type": "integer",
"context": [
"edit"
],
"readonly": true
}
},
"required": false
},
"target": {
"description": "The target attribute of the link element for this menu item.",
"type": "string",