WPCOM_JSON_API_Menus_Update_Menu_Endpoint

The Jetpack by WordPress.com WPCOM JSON API Menus Update Menu Endpoint class.

Defined (1)

The class is defined in the following location(s).

/json-endpoints/class.wpcom-json-api-menus-v1-1-endpoint.php  
  1. class WPCOM_JSON_API_Menus_Update_Menu_Endpoint extends WPCOM_JSON_API_Menus_Abstract_Endpoint { 
  2. function callback( $path = '', $site = 0, $menu_id = 0 ) { 
  3. $site_id = $this->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site ) ); 
  4.  
  5. if ( is_wp_error( $site_id ) ) { 
  6. return $site_id; 
  7.  
  8. if ( $menu_id <= 0 ) { 
  9. return new WP_Error( 'menu-id', 'Menu ID must be greater than 0.', 400 ); 
  10.  
  11. $data = $this->input( true, false ); 
  12. $data = $this->complexify( array( $data ) ); 
  13. if ( is_wp_error( $data ) ) { 
  14. return $data; 
  15. $data = $data[0]; 
  16.  
  17. // Avoid special-case handling of an unset 'items' field in empty menus 
  18. $data['items'] = isset( $data['items'] ) ? $data['items'] : array(); 
  19.  
  20. $data = $this->create_new_items( $data, $menu_id ); 
  21.  
  22. $result = wp_update_nav_menu_object( $menu_id, array( 'menu-name' => $data['menu-name'] ) ); 
  23.  
  24. if ( is_wp_error( $result ) ) { 
  25. return $result; 
  26.  
  27. $delete_status = $this->delete_items_not_present( $menu_id, $data['items'] ); 
  28. if( is_wp_error( $delete_status ) ) { 
  29. return $delete_status; 
  30.  
  31. foreach ( $data['items'] as $item ) { 
  32. $item_id = isset( $item['menu-item-db-id'] ) ? $item['menu-item-db-id'] : 0; 
  33. $result = wp_update_nav_menu_item( $menu_id, $item_id, $item ); 
  34. if ( is_wp_error( $result ) ) { 
  35. return $result; 
  36.  
  37. $items = wp_get_nav_menu_items( $menu_id, array( 'update_post_term_cache' => false ) ); 
  38.  
  39. if ( is_wp_error( $items ) ) { 
  40. return $items; 
  41.  
  42. $menu = wp_get_nav_menu_object( $menu_id ); 
  43. $menu->items = $items; 
  44.  
  45. return array( 'menu' => $this->simplify( $menu ) ); 
  46.  
  47. /** 
  48. * New items can have a 'tmp_id', allowing them to 
  49. * be used as parent items before they have been created. 
  50. * This function will create items that have a 'tmp_id' set, and 
  51. * update any items with a 'tmp_parent' to use the 
  52. * newly created item as a parent. 
  53. */ 
  54. function create_new_items( $data, $menu_id ) { 
  55. $tmp_to_actual_ids = array(); 
  56. foreach ( $data['items'] as &$item ) { 
  57. if ( isset( $item['tmp_id'] ) ) { 
  58. $actual_id = wp_update_nav_menu_item( $menu_id, 0, $item ); 
  59. $tmp_to_actual_ids[ $item['tmp_id'] ] = $actual_id; 
  60. unset( $item['tmp_id'] ); 
  61. $item['menu-item-db-id'] = $actual_id; 
  62.  
  63. foreach ( $data['items'] as &$item ) { 
  64. if ( isset( $item['tmp_parent'] ) ) { 
  65. $item['menu-item-parent-id'] = $tmp_to_actual_ids[ $item['tmp_parent'] ]; 
  66. unset( $item['tmp_parent'] ); 
  67.  
  68. return $data; 
  69.  
  70. /** 
  71. * remove any existing menu items not present in the supplied array. 
  72. * returns wp_error if an item cannot be deleted. 
  73. */ 
  74. function delete_items_not_present( $menu_id, $menu_items ) { 
  75.  
  76. $existing_items = wp_get_nav_menu_items( $menu_id, array( 'update_post_term_cache' => false ) ); 
  77. if ( ! is_array( $existing_items ) ) { 
  78. return true; 
  79.  
  80. $existing_ids = wp_list_pluck( $existing_items, 'db_id' ); 
  81. $ids_to_keep = wp_list_pluck( $menu_items, 'menu-item-db-id' ); 
  82. $ids_to_remove = array_diff( $existing_ids, $ids_to_keep ); 
  83.  
  84. foreach ( $ids_to_remove as $id ) { 
  85. if ( false === wp_delete_post( $id, true ) ) { 
  86. return new WP_Error( 'menu-item',  
  87. sprintf( 'Failed to delete menu item with id: %d.', $id ), 400 ); 
  88.  
  89. return true;