75 lines
3.7 KiB
PHP
75 lines
3.7 KiB
PHP
<?php
|
|
namespace AskTamMCP\Tools;
|
|
defined('ABSPATH') || exit;
|
|
|
|
class MenuTools {
|
|
|
|
public static function listMenus(array $args): array {
|
|
$menus = wp_get_nav_menus();
|
|
return ['menus' => array_map(fn($m) => ['id' => $m->term_id, 'name' => $m->name, 'slug' => $m->slug, 'count' => $m->count], $menus)];
|
|
}
|
|
|
|
public static function getMenu(array $args): array {
|
|
$items = wp_get_nav_menu_items($args['menu']);
|
|
if ($items === false) throw new \RuntimeException("Menu not found: {$args['menu']}");
|
|
$items = $items ?: [];
|
|
return ['items' => array_map(fn($i) => ['id' => $i->ID, 'title' => $i->title, 'url' => $i->url, 'parent' => (int) $i->menu_item_parent, 'order' => (int) $i->menu_order, 'type' => $i->type, 'object' => $i->object, 'object_id' => (int) $i->object_id], $items)];
|
|
}
|
|
|
|
public static function getMenuItems(array $args): array {
|
|
return self::getMenu($args);
|
|
}
|
|
|
|
public static function createMenuItem(array $args): array {
|
|
if (empty($args['menu'])) throw new \RuntimeException("menu is required.");
|
|
$data = [
|
|
'menu-item-title' => $args['title'] ?? '',
|
|
'menu-item-url' => $args['url'] ?? '#',
|
|
'menu-item-status' => 'publish',
|
|
'menu-item-type' => $args['type'] ?? 'custom',
|
|
];
|
|
if (!empty($args['parent'])) $data['menu-item-parent-id'] = (int) $args['parent'];
|
|
if (!empty($args['position'])) $data['menu-item-position'] = (int) $args['position'];
|
|
if (!empty($args['object_id'])) {
|
|
$data['menu-item-object-id'] = (int) $args['object_id'];
|
|
$data['menu-item-object'] = $args['object'] ?? 'page';
|
|
$data['menu-item-type'] = $args['type'] ?? 'post_type';
|
|
}
|
|
$id = wp_update_nav_menu_item($args['menu'], 0, $data);
|
|
if (is_wp_error($id)) throw new \RuntimeException($id->get_error_message());
|
|
return ['item_id' => $id, 'id' => $id, 'menu' => $args['menu']];
|
|
}
|
|
|
|
public static function updateMenuItem(array $args): array {
|
|
if (empty($args['menu'])) throw new \RuntimeException("menu is required.");
|
|
$data = [];
|
|
if (isset($args['title'])) $data['menu-item-title'] = $args['title'];
|
|
if (isset($args['url'])) $data['menu-item-url'] = $args['url'];
|
|
if (isset($args['parent'])) $data['menu-item-parent-id']= (int) $args['parent'];
|
|
if (isset($args['position'])) $data['menu-item-position'] = (int) $args['position'];
|
|
if (empty($data)) throw new \RuntimeException("No fields to update.");
|
|
$data['menu-item-status'] = 'publish';
|
|
$item_id = (int) ($args['item_id'] ?? 0);
|
|
$id = wp_update_nav_menu_item($args['menu'], $item_id, $data);
|
|
if (is_wp_error($id)) throw new \RuntimeException($id->get_error_message());
|
|
return ['item_id' => $id, 'id' => $id, 'menu' => (int) $args['menu'], 'updated' => true];
|
|
}
|
|
|
|
public static function deleteMenuItem(array $args): array {
|
|
$item_id = (int) ($args['item_id'] ?? 0);
|
|
if (!$item_id) throw new \RuntimeException("item_id is required.");
|
|
$result = wp_delete_post($item_id, true);
|
|
if (!$result) throw new \RuntimeException("Failed to delete menu item: {$item_id}");
|
|
return ['item_id' => $item_id, 'deleted' => true];
|
|
}
|
|
|
|
public static function reorderMenuItems(array $args): array {
|
|
$ids = $args['menu_item_ids'] ?? [];
|
|
if (empty($ids)) throw new \RuntimeException("menu_item_ids is required.");
|
|
foreach ($ids as $pos => $id) {
|
|
wp_update_post(['ID' => (int) $id, 'menu_order' => $pos + 1]);
|
|
}
|
|
return ['reordered' => true, 'count' => count($ids)];
|
|
}
|
|
}
|