Compare commits

..

No commits in common. "5b2d677d173c125a7712aad131e47a03cadb4403" and "f9d2d659313d50e25ed587df7f5305345456fe92" have entirely different histories.

1 changed files with 37 additions and 61 deletions

View File

@ -10,77 +10,53 @@ $items = new ArrayObject;
$items[0] = new ArrayObject(); // top level menu items go here $items[0] = new ArrayObject(); // top level menu items go here
if ($wordpress_endpoint) { if ($wordpress_endpoint) {
try { $client = new Client($wordpress_endpoint);
$client = new Client($wordpress_endpoint); $client->setOptions(array("timeout"=>1));
$client->setOptions(array("timeout"=>1)); $response = $client->send();
$response = $client->send(); $response_data = json_decode($response->getBody());
}
catch (exception $e) { if (!$response->isClientError() && $response_data) {
$response = null; foreach ($response_data as $menu_item) {
} $lowered_title = strtolower($menu_item->title);
if ($response) { $item_url = $menu_item->url;
$response_data = json_decode($response->getBody()); if ($lowered_title == 'login') { // manipulate login url
if (!$response->isClientError() && $response_data) { $item_url = "/login";
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;
if ($lowered_title == 'collections') { // change Collections item
$item_title = "Project";
$item_url = $wordpress_site;
}
$item = [
"id" => $menu_item->ID,
"title" => $item_title,
"url" => $item_url
];
if (!array_key_exists($menu_item->menu_item_parent, $items)) {
$items[$menu_item->menu_item_parent] = new ArrayObject();
}
$items[$menu_item->menu_item_parent][$menu_item->ID] =$item;
} }
$item_title = $menu_item->title;
if ($lowered_title == 'collections') { // change Collections item
$item_title = "Project";
$item_url = $wordpress_site;
}
$item = [
"id" => $menu_item->ID,
"title" => $item_title,
"url" => $item_url
];
if (!array_key_exists($menu_item->menu_item_parent, $items)) {
$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) {
$project_item = [
"id" => 2,
"title" => "Project",
"url" => $wordpress_site
];
$items[0][2] = $project_item;
}
$login_item = [
"id" => 1,
"title" => "Login",
"url" => "/login"
];
$items[0][1] = $login_item;
}
//print_r($items); //print_r($items);
if (!function_exists('render_menu')) { function render_menu($items, $level_id) {
function render_menu($items, $level_id) { echo '<ul>' . PHP_EOL;
echo '<ul>' . PHP_EOL; foreach ($items[$level_id] as $menu_item) {
foreach ($items[$level_id] as $menu_item) { echo '<li>' . PHP_EOL;
echo '<li>' . PHP_EOL; echo '<a href="'.$menu_item['url'].'">'.$menu_item['title'].'</a>' . PHP_EOL;
echo '<a href="'.$menu_item['url'].'">'.$menu_item['title'].'</a>' . PHP_EOL; if (array_key_exists($menu_item['id'], $items)) {
if (array_key_exists($menu_item['id'], $items)) { render_menu($items, $menu_item['id']); // render sub menu
render_menu($items, $menu_item['id']); // render sub menu
}
echo '</li>' . PHP_EOL;
} }
echo '</ul>' . PHP_EOL; echo '</li>' . PHP_EOL;
} }
echo '</ul>' . PHP_EOL;
} }
render_menu($items, 0); if (count($items[0]) > 0) {
render_menu($items, 0);
}
?> ?>