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; } }