WPCOM_JSON_API_Menus_Simplifier

The Jetpack by WordPress.com WPCOM JSON API Menus Simplifier 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_Simplifier extends WPCOM_JSON_API_Menus_Translator { 
  2. protected $filter = 'wpcom_menu_api_translator_simplify'; 
  3.  
  4. protected $filters = array( 
  5. 'whitelist_and_rename_keys',  
  6. 'add_locations',  
  7. 'treeify',  
  8. 'add_widget_locations',  
  9. ); 
  10.  
  11. protected $menu_whitelist = array( 
  12. 'term_id' => array( 'name' => 'id', 'type' => 'int' ),  
  13. 'name' => array( 'name' => 'name', 'type' => 'string' ),  
  14. 'description' => array( 'name' => 'description', 'type' => 'string' ),  
  15. 'items' => array( 'name' => 'items', 'type' => 'array' ),  
  16. ); 
  17.  
  18. protected $menu_item_whitelist = array( 
  19. 'db_id' => array( 'name' => 'id', 'type' => 'int' ),  
  20. 'object_id' => array( 'name' => 'content_id', 'type' => 'int' ),  
  21. 'object' => array( 'name' => 'type', 'type' => 'string' ),  
  22. 'type' => array( 'name' => 'type_family', 'type' => 'string' ),  
  23. 'type_label' => array( 'name' => 'type_label', 'type' => 'string' ),  
  24. 'title' => array( 'name' => 'name', 'type' => 'string' ),  
  25. 'menu_order' => array( 'name' => 'order', 'type' => 'int' ),  
  26. 'menu_item_parent' => array( 'name' => 'parent', 'type' => 'int' ),  
  27. 'url' => array( 'name' => 'url', 'type' => 'string' ),  
  28. 'target' => array( 'name' => 'link_target', 'type' => 'string' ),  
  29. 'attr_title' => array( 'name' => 'link_title', 'type' => 'string' ),  
  30. 'description' => array( 'name' => 'description', 'type' => 'string' ),  
  31. 'classes' => array( 'name' => 'classes', 'type' => 'array' ),  
  32. 'xfn' => array( 'name' => 'xfn', 'type' => 'string' ),  
  33. ); 
  34.  
  35. /************************** 
  36. * Filters methods 
  37. **************************/ 
  38.  
  39. public function treeify( $menus ) { 
  40. return array_map( array( $this, 'treeify_menu' ), $menus ); 
  41.  
  42. // turn the flat item list into a tree of items 
  43. protected function treeify_menu( $menu ) { 
  44. $indexed_nodes = array(); 
  45. $tree = array(); 
  46.  
  47. foreach( $menu['items'] as &$item ) { 
  48. $indexed_nodes[ $item['id'] ] = &$item; 
  49.  
  50. foreach( $menu['items'] as &$item ) { 
  51. if ( $item['parent'] && isset( $indexed_nodes[ $item['parent'] ] ) ) { 
  52. $parent_node = &$indexed_nodes[ $item['parent'] ]; 
  53. if ( !isset( $parent_node['items'] ) ) { 
  54. $parent_node['items'] = array(); 
  55. $parent_node['items'][ $item['order'] ] = &$item; 
  56. } else { 
  57. $tree[ $item['order'] ] = &$item; 
  58. unset( $item['order'] ); 
  59. unset( $item['parent'] ); 
  60.  
  61. $menu['items'] = $tree; 
  62. $this->remove_item_keys( $menu ); 
  63. return $menu; 
  64.  
  65. // recursively ensure item lists are contiguous 
  66. protected function remove_item_keys( &$item ) { 
  67. if ( ! isset( $item['items'] ) || ! is_array( $item['items'] ) ) { 
  68. return; 
  69.  
  70.  
  71. foreach( $item['items'] as &$it ) { 
  72. $this->remove_item_keys( $it ); 
  73.  
  74. $item['items'] = array_values( $item['items'] ); 
  75.  
  76. protected function whitelist_and_rename_keys( $menus ) { 
  77. $transformed_menus = array(); 
  78.  
  79. foreach ( $menus as $menu ) { 
  80. $menu = $this->whitelist_and_rename_with( $menu, $this->menu_whitelist ); 
  81.  
  82. if ( isset( $menu['items'] ) ) { 
  83. foreach ( $menu['items'] as &$item ) { 
  84. $item = $this->whitelist_and_rename_with( $item, $this->menu_item_whitelist ); 
  85.  
  86. $transformed_menus[] = $menu; 
  87.  
  88. return $transformed_menus; 
  89.  
  90. protected function add_locations( $menus ) { 
  91. $menus_with_locations = array(); 
  92.  
  93. foreach( $menus as $menu ) { 
  94. $menu['locations'] = array_keys( get_nav_menu_locations(), $menu['id'] ); 
  95. $menus_with_locations[] = $menu; 
  96.  
  97. return $menus_with_locations; 
  98.  
  99. protected function add_widget_locations( $menus ) { 
  100. $nav_menu_widgets = WPCOM_JSON_API_Menus_Widgets::get(); 
  101.  
  102. if ( ! is_array( $nav_menu_widgets ) ) { 
  103. return $menus; 
  104.  
  105. foreach ( $menus as &$menu ) { 
  106. $widget_locations = array(); 
  107.  
  108. foreach ( $nav_menu_widgets as $key => $widget ) { 
  109. if ( is_array( $widget ) && isset( $widget['nav_menu'] ) && 
  110. $widget['nav_menu'] === $menu['id'] ) { 
  111. $widget_locations[] = 'nav_menu_widget-' . $key; 
  112. $menu['locations'] = array_merge( $menu['locations'], $widget_locations ); 
  113.  
  114. return $menus;