Skip to content

Instantly share code, notes, and snippets.

@gadiener
Last active May 13, 2016 07:27
Show Gist options
  • Save gadiener/1c2e32f918c11ec8f163b661c892d233 to your computer and use it in GitHub Desktop.
Save gadiener/1c2e32f918c11ec8f163b661c892d233 to your computer and use it in GitHub Desktop.
Helper for build custom menu in wordpress
<?php
class Menu {
protected $elements = [],
$args = [],
$menu_location = "primary",
$setted = false;
/**
* Return array with all menu items.
* @param string $menu_location Menu location
* @param array $args Additional parameters for the elements of the menu
* @see var_dump((new AeriaMenu($location))->items);
*
* @return (false|array) Array of menu items, otherwise false
*/
public function __construct( $menu_location = false, $args = [] ) {
if ( !empty( $menu_location ) ) $this->menu_location = $menu_location;
if ( !empty( $args ) ) $this->args = $args;
$menu_items = $this->items();
$this->elements = empty($menu_items) ? false : array_reduce( $this->trasform($menu_items), function( $tree, $e ) {
$parent_id = $e['parent_id'];
$tree[ $parent_id ]['children'][] = $e;
$tree[$e['id']] =& $tree[ $parent_id ]['children'][ count( $tree[ $parent_id ]['children']) - 1 ];
return $tree;
}, [ 0 => [ 'children' => [] ] ] )[0]['children'];
}
public static function location() {
return get_nav_menu_locations();
}
public function items() {
return wp_get_nav_menu_items( static::location()[ $this->menu_location ] );
}
public function elements() {
return $this->elements;
}
protected function trasform( $menu ) {
return array_map( function($item) {
return [
'id' => $item->ID,
'parent_id' => $item->menu_item_parent,
'title' => $item->title,
'url' => $item->url,
'object_id' => $item->object_id,
] + $this->resultArgs($item) + [
'children' => []
];
}, $menu );
}
protected function resultArgs($menu) {
if ($this->setted) return $this->args;
$this->args = array_map( function( $item ) use ($menu) {
return $menu->{$item};
}, $this->args );
$this->setted = true;
return $this->args;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment