feat(menu): list regions and cities in the header menu

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-29 15:24:15 +03:30
parent f6153376f9
commit c73189f2db
3 changed files with 97 additions and 0 deletions

View File

@@ -0,0 +1,88 @@
<?php
defined( 'ABSPATH' ) || exit;
/**
* Appends the Viator Structure regions (each with a dropdown of its cities)
* to the theme's header menu, so the menu follows the structure without
* hand-editing Appearance → Menus.
*/
class VC_Menu {
const LOCATION = 'hoho_primary';
public static function init() {
add_filter( 'wp_nav_menu_objects', array( __CLASS__, 'append' ), 10, 2 );
}
public static function append( $items, $args ) {
if ( empty( $args->theme_location ) || self::LOCATION !== $args->theme_location ) {
return $items;
}
$order = count( $items );
$extra = self::items();
foreach ( $extra as $item ) {
$item->menu_order = ++$order;
}
return array_merge( $items, $extra );
}
/**
* Menu-item objects for the walker. The theme walker turns a parent with
* children into a toggle button (no link), so a region's dropdown starts
* with an "All of {region}" link to the region page itself.
*
* @return object[]
*/
public static function items() {
$items = array();
$n = 0;
foreach ( VC_Structure::get() as $region ) {
$term = $region['term_id'] ? get_term( (int) $region['term_id'], 'viator_destination' ) : null;
if ( ! $term instanceof WP_Term ) {
continue;
}
$region_url = get_term_link( $term );
if ( is_wp_error( $region_url ) ) {
continue;
}
$parent = self::item( ++$n, 0, $term->name, $region_url );
$items[] = $parent;
if ( ! $region['cities'] ) {
continue;
}
/* translators: %s: region name. */
$items[] = self::item( ++$n, $parent->db_id, sprintf( __( 'All of %s', 'viator-collector' ), $term->name ), $region_url );
foreach ( $region['cities'] as $city ) {
$items[] = self::item( ++$n, $parent->db_id, $city['name'], home_url( '/' . $city['slug'] . '/' ) );
}
}
return $items;
}
private static function item( $n, $parent_id, $title, $url ) {
$id = -1000 - $n;
$item = new stdClass();
$item->ID = $id;
$item->db_id = $id;
$item->menu_item_parent = (string) $parent_id;
$item->object_id = (string) $id;
$item->object = 'custom';
$item->type = 'custom';
$item->type_label = '';
$item->title = $title;
$item->url = $url;
$item->target = '';
$item->attr_title = '';
$item->description = '';
$item->classes = array( 'menu-item', 'vc-structure-item' );
$item->xfn = '';
$item->status = 'publish';
$item->post_parent = 0;
$item->menu_order = $n;
$item->current = false;
$item->current_item_ancestor = false;
$item->current_item_parent = false;
return $item;
}
}