2022-03-25 13:17:33 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
# Config
|
|
|
|
$wordpress_site="https://dev.arc-hive.zone";
|
|
|
|
$wordpress_endpoint="https://dev.arc-hive.zone/wp-json/menus/v1/menus/4";
|
|
|
|
|
|
|
|
use Zend\Http\Client;
|
|
|
|
|
|
|
|
$items = new ArrayObject;
|
|
|
|
$items[0] = new ArrayObject(); // top level menu items go here
|
|
|
|
|
|
|
|
if ($wordpress_endpoint) {
|
|
|
|
try {
|
|
|
|
$client = new Client($wordpress_endpoint);
|
2022-04-28 18:15:44 +02:00
|
|
|
$client->setOptions(array("timeout"=>2));
|
2022-03-25 13:17:33 +01:00
|
|
|
$response = $client->send();
|
|
|
|
}
|
|
|
|
catch (exception $e) {
|
|
|
|
$response = null;
|
|
|
|
}
|
|
|
|
if ($response) {
|
|
|
|
$response_data = json_decode($response->getBody());
|
|
|
|
if (!$response->isClientError() && $response_data) {
|
|
|
|
foreach ($response_data as $menu_item) {
|
|
|
|
$lowered_title = strtolower($menu_item->title);
|
|
|
|
$item_url = $menu_item->url;
|
|
|
|
if ($lowered_title == 'login') {
|
|
|
|
// manipulate login url
|
|
|
|
$item_url = "/login";
|
|
|
|
}
|
|
|
|
$item_title = $menu_item->title;
|
2022-04-28 17:23:07 +02:00
|
|
|
/*
|
2022-03-25 13:17:33 +01:00
|
|
|
if ($lowered_title == 'collections') {
|
|
|
|
if ($wordpress_site) {
|
|
|
|
// change Collections item
|
|
|
|
$item_title = "Project";
|
|
|
|
$item_url = $wordpress_site;
|
|
|
|
} else {
|
|
|
|
continue; // do not render the Collections item
|
|
|
|
}
|
|
|
|
}
|
2022-04-28 17:23:07 +02:00
|
|
|
*/
|
2022-03-25 13:17:33 +01:00
|
|
|
$item = [
|
|
|
|
"id" => $menu_item->ID,
|
|
|
|
"title" => $item_title,
|
|
|
|
"url" => $item_url
|
|
|
|
];
|
|
|
|
if (!isset($items[$menu_item->menu_item_parent])) {
|
|
|
|
$items[$menu_item->menu_item_parent] = new ArrayObject();
|
|
|
|
}
|
|
|
|
$items[$menu_item->menu_item_parent][$menu_item->ID] =$item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($items[0]) == 0) {
|
|
|
|
// we did not retrieve a menu from the wordpress site
|
|
|
|
// let's create a default menu
|
|
|
|
if ($wordpress_site) {
|
2022-04-28 19:13:23 +02:00
|
|
|
$collections_item = [
|
2022-03-25 13:17:33 +01:00
|
|
|
"id" => 1,
|
2022-04-28 19:13:23 +02:00
|
|
|
"title" => "Collections",
|
|
|
|
"url" => "/"
|
2022-03-25 13:17:33 +01:00
|
|
|
];
|
2022-04-28 19:13:23 +02:00
|
|
|
$items[0][1] = $collections_item;
|
2022-03-25 13:17:33 +01:00
|
|
|
}
|
|
|
|
$login_item = [
|
|
|
|
"id" => 2,
|
|
|
|
"title" => "Login",
|
|
|
|
"url" => "/login"
|
|
|
|
];
|
|
|
|
$items[0][2] = $login_item;
|
|
|
|
}
|
|
|
|
//print_r($items);
|
|
|
|
|
|
|
|
if (!function_exists('render_menu')) {
|
|
|
|
function render_menu($items, $level_id) {
|
|
|
|
if ($level_id == 0) {
|
|
|
|
echo '<ul id="" class="dropdown menu" data-dropdown-menu="dropdown-menu" role="menubar">'.
|
|
|
|
PHP_EOL;
|
|
|
|
} else {
|
|
|
|
echo '<ul class="dropdown menu vertical submenu is-dropdown-submenu first-sub" data-toggle="" data-submenu="" role="">'.
|
|
|
|
PHP_EOL;
|
|
|
|
}
|
|
|
|
foreach ($items[$level_id] as $menu_item) {
|
|
|
|
if ($level_id == 0) {
|
|
|
|
echo '<li id="" class="" role="">'.PHP_EOL;
|
|
|
|
} else {
|
|
|
|
echo '<li id="" class="is-submenu-item is-dropdown-submenu-item" role="">'.PHP_EOL;
|
|
|
|
}
|
|
|
|
echo '<a role="" href="'.$menu_item['url'].'">'.$menu_item['title'].'</a>'.PHP_EOL;
|
|
|
|
if (isset($items[$menu_item['id']])) {
|
|
|
|
render_menu($items, $menu_item['id']); // render sub menu
|
|
|
|
}
|
|
|
|
echo '</li>' . PHP_EOL;
|
|
|
|
}
|
|
|
|
echo '</ul>' . PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render_menu($items, 0);
|
|
|
|
|
|
|
|
?>
|