Files
WP-LIT-MCP/src/Integrations/WordPress/Menus/MenuLocations.php
T

47 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace LIT\MCP\Integrations\WordPress\Menus;
/**
* Theme menu locations (theme mod nav_menu_locations).
*/
final class MenuLocations
{
/** @return array<string, string> Location slug => description. */
public static function registered(): array
{
return array_map('strval', (array) get_registered_nav_menus());
}
/** @return array<string, int> Location slug => menu ID. */
public static function assigned(): array
{
$locations = get_nav_menu_locations();
return is_array($locations) ? array_map('intval', $locations) : [];
}
/** @return list<string> */
public static function of(int $menuId): array
{
return array_map('strval', array_keys(array_filter(
self::assigned(),
static fn (int $id): bool => $id === $menuId
)));
}
public static function assign(string $location, int $menuId): void
{
$locations = self::assigned();
if ($menuId > 0) {
$locations[$location] = $menuId;
} else {
unset($locations[$location]);
}
set_theme_mod('nav_menu_locations', $locations);
}
}