<?php


if (class_exists('TC')) { // Tickera plugin activated

	// Show Cart Menu option in Tickera does not work properly, fixing it
	global $wp_filter;

	if ( isset( $wp_filter['wp_nav_menu_objects'] ) ) {
		foreach ( $wp_filter['wp_nav_menu_objects'] as $k => $v ) {
			foreach ( $v as $k2 => $v2 ) {
				if ( isset( $v2['function'] ) && is_array( $v2['function'] ) && isset( $v2['function'][0] ) && is_object( $v2['function'][0] ) ) {
					if ( get_class( $v2['function'][0] ) == 'TC' && $v2['function'][1] == 'main_navigation_links' ) {
						remove_filter( 'wp_nav_menu_objects', $k2 );
						add_filter( 'wp_nav_menu_objects', 'eventerra_tc_main_navigation_links', 10, 2 );
						break 2;
					}
				}
			}
		}
	}

	if ( isset( $wp_filter['wp_page_menu'] ) ) {
		foreach ( $wp_filter['wp_page_menu'] as $k => $v ) {
			foreach ( $v as $k2 => $v2 ) {
				if ( isset( $v2['function'] ) && is_array( $v2['function'] ) && isset( $v2['function'][0] ) && is_object( $v2['function'][0] ) ) {
					if ( get_class( $v2['function'][0] ) == 'TC' && $v2['function'][1] == 'main_navigation_links_fallback' ) {
						remove_filter( 'wp_page_menu', $k2, 20 );
						break 2;
					}
				}
			}
		}
	}

	function eventerra_tc_main_navigation_links( $sorted_menu_items, $args ) {
		global $tc;
		if ( ! is_admin() && is_object( $tc ) ) {

			if ( $args->theme_location == 'primary-menu' ) {//put extra menu items only in primary menu
				$cart_link = new stdClass;

				$cart_link->title            = apply_filters( 'tc_cart_page_link_title', esc_html__( 'Cart', 'eventerra' ) );
				$cart_link->menu_item_parent = 0;
				$cart_link->ID               = 'tc_cart';
				$cart_link->db_id            = '';
				$cart_link->url              = $tc->get_cart_slug( true );

				if ( tc_current_url() == $cart_link->url ) {
					$cart_link->classes[] = 'current_page_item';
				}

				$sorted_menu_items[] = $cart_link;
			}
		}

		return $sorted_menu_items;
	}
}