Make WordPress Core


Ignore:
Timestamp:
11/09/2021 06:58:59 PM (3 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Introduce Menu management endpoints.

This commit introduces the /wp/v2/menus, /wp/v2/menu-items and /wp/v2/menu-locations REST API endpoints. These endpoints are fully available to users with the edit_theme_options capability, but can be read by any user who can edit a REST API available post type.

The nav_menu taxonomy and nav_menu_item post type now map their capabilities to the edit_theme_options primitive capability. This allows developers to provide more fine-grained access control. However, if a developer is currently dynamically removing the edit_theme_options capability using map_meta_cap, they should use the user_has_cap filter instead.

The wp_update_nav_menu_item() function has been adjusted to return an error if saving the menu item post or assigning the menu item to a menu generate an error.

Lastly, a new menu item type is introduced, block, that can be used to store a Block as a menu item.

Props andraganescu, antonvlasenko, dingo_d, dlh, isabel_brison, kadamwhite, Mamaduka, NateWr, noisysocks, peterwilsoncc, ryelle, schlessera, soean, Spacedmonkey, talldanwp, TimothyBlynJacobs, tobifjellner, westonruter, wpscholar, zieladam.
Fixes #40878.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/nav-menu.php

    r51301 r52079  
    407407 * Save the properties of a menu item or create a new one.
    408408 *
    409  * The menu-item-title, menu-item-description, and menu-item-attr-title are expected
    410  * to be pre-slashed since they are passed directly into `wp_insert_post()`.
    411  *
    412  * @since 3.0.0
     409 * The menu-item-title, menu-item-description, menu-item-attr-title, and menu-item-content are expected
     410 * to be pre-slashed since they are passed directly to APIs that expect slashed data.
     411 *
     412 * @since 3.0.0
     413 * @since 5.9.0 Added the menu-item-content parameter.
    413414 *
    414415 * @param int   $menu_id         The ID of the menu. Required. If "0", makes the menu item a draft orphan.
     
    449450        'menu-item-target'        => '',
    450451        'menu-item-classes'       => '',
     452        'menu-item-content'       => '',
    451453        'menu-item-xfn'           => '',
    452454        'menu-item-status'        => '',
     
    527529        $post['ID']          = 0;
    528530        $post['post_status'] = 'publish' === $args['menu-item-status'] ? 'publish' : 'draft';
    529         $menu_item_db_id     = wp_insert_post( $post );
     531        $menu_item_db_id     = wp_insert_post( $post, true );
    530532        if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ) {
    531533            return $menu_item_db_id;
     
    549551    // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms().
    550552    if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) {
    551         wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
     553        $update_terms = wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
     554        if ( is_wp_error( $update_terms ) ) {
     555            return $update_terms;
     556        }
    552557    }
    553558
     
    570575    update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
    571576    update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw( $args['menu-item-url'] ) );
     577    update_post_meta( $menu_item_db_id, '_menu_item_content', $args['menu-item-content'] );
    572578
    573579    if ( 0 == $menu_id ) {
     
    581587        $post['ID']          = $menu_item_db_id;
    582588        $post['post_status'] = ( 'draft' === $args['menu-item-status'] ) ? 'draft' : 'publish';
    583         wp_update_post( $post );
     589
     590        $update_post = wp_update_post( $post, true );
     591        if ( is_wp_error( $update_post ) ) {
     592            return $update_post;
     593        }
    584594    }
    585595
     
    904914                $menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title;
    905915
     916            } elseif ( 'block' === $menu_item->type ) {
     917                $menu_item->type_label        = __( 'Block' );
     918                $menu_item->title             = $menu_item->post_title;
     919                $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;
    906920            } else {
    907921                $menu_item->type_label = __( 'Custom Link' );
Note: See TracChangeset for help on using the changeset viewer.