Skip to content

Commit

Permalink
Navigation Screen: Fix 'menu_exists' response status code (#34888)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedmonkey authored Sep 17, 2021
1 parent 96e1130 commit 4f3d93e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/class-wp-rest-menus-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,16 +352,18 @@ public function create_item( $request ) {
* If we're going to inform the client that the term already exists,
* give them the identifier for future use.
*/
$term_id = $term->get_error_data( 'term_exists' );
if ( $term_id ) {
$existing_term = get_term( $term_id, $this->taxonomy );
$term->add_data( $existing_term->term_id, 'term_exists' );

if ( in_array( 'menu_exists', $term->get_error_codes(), true ) ) {
$existing_term = get_term_by( 'name', $prepared_term['menu-name'], $this->taxonomy );
$term->add_data( $existing_term->term_id, 'menu_exists' );
$term->add_data(
array(
'status' => 400,
'term_id' => $term_id,
'term_id' => $existing_term->term_id,
)
);
} else {
$term->add_data( array( 'status' => 400 ) );
}

return $term;
Expand Down
22 changes: 22 additions & 0 deletions phpunit/class-rest-nav-menus-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,28 @@ public function test_create_item() {
$this->assertEquals( 'my-awesome-menus', $data['slug'] );
}

/**
*
*/
public function test_create_item_same_name() {
wp_set_current_user( self::$admin_id );

wp_update_nav_menu_object(
0,
array(
'description' => 'This menu is so Original',
'menu-name' => 'Original',
)
);

$request = new WP_REST_Request( 'POST', '/__experimental/menus' );
$request->set_param( 'name', 'Original' );
$request->set_param( 'description', 'This menu is so Original' );
$response = rest_get_server()->dispatch( $request );

$this->assertErrorResponse( 'menu_exists', $response, 400 );
}

/**
*
*/
Expand Down

0 comments on commit 4f3d93e

Please sign in to comment.