renders a default menu when wordpress api fails

This commit is contained in:
buttle 2022-03-25 12:07:14 +01:00
parent 9d3976c1cb
commit 5b2d677d17
1 changed files with 52 additions and 28 deletions

View File

@ -10,35 +10,60 @@ $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) {
$client = new Client($wordpress_endpoint); try {
$client->setOptions(array("timeout"=>1)); $client = new Client($wordpress_endpoint);
$response = $client->send(); $client->setOptions(array("timeout"=>1));
$response_data = json_decode($response->getBody()); $response = $client->send();
}
if (!$response->isClientError() && $response_data) { catch (exception $e) {
foreach ($response_data as $menu_item) { $response = null;
$lowered_title = strtolower($menu_item->title); }
$item_url = $menu_item->url; if ($response) {
if ($lowered_title == 'login') { // manipulate login url $response_data = json_decode($response->getBody());
$item_url = "/login"; 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;
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')) { if (!function_exists('render_menu')) {
@ -55,8 +80,7 @@ if (!function_exists('render_menu')) {
echo '</ul>' . PHP_EOL; echo '</ul>' . PHP_EOL;
} }
} }
if (count($items[0]) > 0) {
render_menu($items, 0); render_menu($items, 0);
}
?> ?>