Files
hoponhopofftickets/viator-collector/includes/class-vc-menu.php
Amir Bagheri d0c48e0e8e fix: styled city cards on region pages; regions follow All Tours in the menu
- Region page city list uses the design's destination rail (dcard) instead of
  the unstyled pre-reskin card template
- Header menu: structure regions are inserted after the top-level link to the
  tours archive instead of at the end
- Plugin 1.9.2

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 16:27:31 +03:30

106 lines
3.2 KiB
PHP

<?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;
}
$extra = self::items();
if ( ! $extra ) {
return $items;
}
// Regions go right after the top-level link to the tours archive ("All
// Tours"); without one, at the end. The walker renders top-level items
// in array order.
$items = array_values( $items );
$position = count( $items );
$archive = untrailingslashit( (string) get_post_type_archive_link( 'viator_tour' ) );
foreach ( $items as $i => $item ) {
if ( empty( $item->menu_item_parent ) && '' !== $archive && untrailingslashit( (string) $item->url ) === $archive ) {
$position = $i + 1;
break;
}
}
array_splice( $items, $position, 0, $extra );
foreach ( $items as $i => $item ) {
$item->menu_order = $i + 1;
}
return $items;
}
/**
* 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;
}
}