| @@ -11,89 +11,18 @@ | ||
| 11 | 11 | * |
| 12 | 12 | * @see WP_REST_Controller |
| 13 | 13 | */ |
| 14 | 14 | class WP_REST_Menus_Controller extends WP_REST_Terms_Controller { |
| 15 | - | |
| 16 | 15 | /** |
| 17 | - * Whether the controller supports batching. | |
| 16 | + * Constructor. | |
| 18 | 17 | * |
| 19 | - * @since 5.9.0 | |
| 20 | - * @var array | |
| 18 | + * @param string $taxonomy Taxonomy key. | |
| 21 | 19 | */ |
| 22 | - protected $allow_batch = array( 'v1' => true ); | |
| 23 | - | |
| 24 | - /** | |
| 25 | - * Overrides the route registration to support "allow_batch". | |
| 26 | - * | |
| 27 | - * @since 11.5.0 | |
| 28 | - * | |
| 29 | - * @see register_rest_route() | |
| 30 | - */ | |
| 31 | - public function register_routes() { | |
| 32 | - register_rest_route( | |
| 33 | - $this->namespace, | |
| 34 | - '/' . $this->rest_base, | |
| 35 | - array( | |
| 36 | - array( | |
| 37 | - 'methods' => WP_REST_Server::READABLE, | |
| 38 | - 'callback' => array( $this, 'get_items' ), | |
| 39 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), | |
| 40 | - 'args' => $this->get_collection_params(), | |
| 41 | - ), | |
| 42 | - array( | |
| 43 | - 'methods' => WP_REST_Server::CREATABLE, | |
| 44 | - 'callback' => array( $this, 'create_item' ), | |
| 45 | - 'permission_callback' => array( $this, 'create_item_permissions_check' ), | |
| 46 | - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), | |
| 47 | - ), | |
| 48 | - 'allow_batch' => $this->allow_batch, | |
| 49 | - 'schema' => array( $this, 'get_public_item_schema' ), | |
| 50 | - ) | |
| 51 | - ); | |
| 52 | - | |
| 53 | - register_rest_route( | |
| 54 | - $this->namespace, | |
| 55 | - '/' . $this->rest_base . '/(?P<id>[\d]+)', | |
| 56 | - array( | |
| 57 | - 'args' => array( | |
| 58 | - 'id' => array( | |
| 59 | - 'description' => __( 'Unique identifier for the term.', 'default' ), | |
| 60 | - 'type' => 'integer', | |
| 61 | - ), | |
| 62 | - ), | |
| 63 | - array( | |
| 64 | - 'methods' => WP_REST_Server::READABLE, | |
| 65 | - 'callback' => array( $this, 'get_item' ), | |
| 66 | - 'permission_callback' => array( $this, 'get_item_permissions_check' ), | |
| 67 | - 'args' => array( | |
| 68 | - 'context' => $this->get_context_param( array( 'default' => 'view' ) ), | |
| 69 | - ), | |
| 70 | - ), | |
| 71 | - array( | |
| 72 | - 'methods' => WP_REST_Server::EDITABLE, | |
| 73 | - 'callback' => array( $this, 'update_item' ), | |
| 74 | - 'permission_callback' => array( $this, 'update_item_permissions_check' ), | |
| 75 | - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), | |
| 76 | - ), | |
| 77 | - array( | |
| 78 | - 'methods' => WP_REST_Server::DELETABLE, | |
| 79 | - 'callback' => array( $this, 'delete_item' ), | |
| 80 | - 'permission_callback' => array( $this, 'delete_item_permissions_check' ), | |
| 81 | - 'args' => array( | |
| 82 | - 'force' => array( | |
| 83 | - 'type' => 'boolean', | |
| 84 | - 'default' => false, | |
| 85 | - 'description' => __( 'Required to be true, as terms do not support trashing.', 'default' ), | |
| 86 | - ), | |
| 87 | - ), | |
| 88 | - ), | |
| 89 | - 'allow_batch' => $this->allow_batch, | |
| 90 | - 'schema' => array( $this, 'get_public_item_schema' ), | |
| 91 | - ) | |
| 92 | - ); | |
| 20 | + public function __construct( $taxonomy ) { | |
| 21 | + parent::__construct( $taxonomy ); | |
| 22 | + $this->namespace = '__experimental'; | |
| 93 | 23 | } |
| 94 | 24 | |
| 95 | - | |
| 96 | 25 | /** |
| 97 | 26 | * Checks if a request has access to read terms in the specified taxonomy. |
| 98 | 27 | * |
| 99 | 28 | * @param WP_REST_Request $request Full details about the request. |
| @@ -259,13 +188,9 @@ | ||
| 259 | 188 | |
| 260 | 189 | $fields = $this->get_fields_for_response( $request ); |
| 261 | 190 | $data = $response->get_data(); |
| 262 | 191 | |
| 263 | - if ( rest_is_field_included( 'locations', $fields ) ) { | |
| 264 | - $data['locations'] = $this->get_menu_locations( $nav_menu->term_id ); | |
| 265 | - } | |
| 266 | - | |
| 267 | - if ( rest_is_field_included( 'auto_add', $fields ) ) { | |
| 192 | + if ( in_array( 'auto_add', $fields, true ) ) { | |
| 268 | 193 | $auto_add = $this->get_menu_auto_add( $nav_menu->term_id ); |
| 269 | 194 | $data['auto_add'] = $auto_add; |
| 270 | 195 | } |
| 271 | 196 | |
| @@ -289,16 +214,18 @@ | ||
| 289 | 214 | */ |
| 290 | 215 | protected function prepare_links( $term ) { |
| 291 | 216 | $links = parent::prepare_links( $term ); |
| 292 | 217 | |
| 293 | - $locations = $this->get_menu_locations( $term->term_id ); | |
| 218 | + $locations = get_nav_menu_locations(); | |
| 294 | 219 | $rest_base = 'menu-locations'; |
| 295 | - foreach ( $locations as $location ) { | |
| 296 | - $url = rest_url( sprintf( 'wp/v2/%s/%s', $rest_base, $location ) ); | |
| 297 | - $links['https://api.w.org/menu-location'][] = array( | |
| 298 | - 'href' => $url, | |
| 299 | - 'embeddable' => true, | |
| 300 | - ); | |
| 220 | + foreach ( $locations as $menu_name => $menu_id ) { | |
| 221 | + if ( $term->term_id === $menu_id ) { | |
| 222 | + $url = rest_url( sprintf( '__experimental/%s/%s', $rest_base, $menu_name ) ); | |
| 223 | + $links['https://api.w.org/menu-location'][] = array( | |
| 224 | + 'href' => $url, | |
| 225 | + 'embeddable' => true, | |
| 226 | + ); | |
| 227 | + } | |
| 301 | 228 | } |
| 302 | 229 | |
| 303 | 230 | return $links; |
| 304 | 231 | } |
| @@ -350,20 +277,18 @@ | ||
| 350 | 277 | /* |
| 351 | 278 | * If we're going to inform the client that the term already exists, |
| 352 | 279 | * give them the identifier for future use. |
| 353 | 280 | */ |
| 354 | - | |
| 355 | - if ( in_array( 'menu_exists', $term->get_error_codes(), true ) ) { | |
| 356 | - $existing_term = get_term_by( 'name', $prepared_term['menu-name'], $this->taxonomy ); | |
| 357 | - $term->add_data( $existing_term->term_id, 'menu_exists' ); | |
| 281 | + $term_id = $term->get_error_data( 'term_exists' ); | |
| 282 | + if ( $term_id ) { | |
| 283 | + $existing_term = get_term( $term_id, $this->taxonomy ); | |
| 284 | + $term->add_data( $existing_term->term_id, 'term_exists' ); | |
| 358 | 285 | $term->add_data( |
| 359 | 286 | array( |
| 360 | 287 | 'status' => 400, |
| 361 | - 'term_id' => $existing_term->term_id, | |
| 288 | + 'term_id' => $term_id, | |
| 362 | 289 | ) |
| 363 | 290 | ); |
| 364 | - } else { | |
| 365 | - $term->add_data( array( 'status' => 400 ) ); | |
| 366 | 291 | } |
| 367 | 292 | |
| 368 | 293 | return $term; |
| 369 | 294 | } |
| @@ -603,30 +528,8 @@ | ||
| 603 | 528 | /** This action is documented in wp-includes/nav-menu.php */ |
| 604 | 529 | do_action( 'wp_update_nav_menu', $menu_id ); |
| 605 | 530 | |
| 606 | 531 | return $update; |
| 607 | - } | |
| 608 | - | |
| 609 | - /** | |
| 610 | - * Returns names of the locations assigned to the menu. | |
| 611 | - * | |
| 612 | - * @since 5.8.0 | |
| 613 | - * | |
| 614 | - * @param int $menu_id The menu id. | |
| 615 | - * | |
| 616 | - * @return string[] $menu_locations The locations assigned to the menu. | |
| 617 | - */ | |
| 618 | - protected function get_menu_locations( $menu_id ) { | |
| 619 | - $locations = get_nav_menu_locations(); | |
| 620 | - $menu_locations = array(); | |
| 621 | - | |
| 622 | - foreach ( $locations as $location => $assigned_menu_id ) { | |
| 623 | - if ( $menu_id === $assigned_menu_id ) { | |
| 624 | - $menu_locations[] = $location; | |
| 625 | - } | |
| 626 | - } | |
| 627 | - | |
| 628 | - return $menu_locations; | |
| 629 | 532 | } |
| 630 | 533 | |
| 631 | 534 | /** |
| 632 | 535 | * Updates the menu's locations from a REST request. |