<?php
if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

// Permalink filter
function mobile_api_vehicle_permalink( $permalink, $post ) {
	if ( $post->post_type !== 'mobile-api-vehicle' )
		return $permalink;

	$categories = array(
		// Category: Car
		'Cabrio' => __( 'Cabrio', 'ad-integration-for-mobile-de-api' ),
		'EstateCar' => __( 'Estate Car', 'ad-integration-for-mobile-de-api' ),
		'Limousine' => __( 'Limousine', 'ad-integration-for-mobile-de-api' ),
		'OffRoad' => __( 'Offroad', 'ad-integration-for-mobile-de-api' ),
		'SmallCar' => __( 'Small Car', 'ad-integration-for-mobile-de-api' ),
		'SportsCar' => __( 'Sports Car', 'ad-integration-for-mobile-de-api' ),
		'Van' => __( 'Van', 'ad-integration-for-mobile-de-api' ),
		'OtherCar' => __( 'Other Car', 'ad-integration-for-mobile-de-api' ),

		// Category: Motorbike
		'ChopperAndCruiser' => __( 'Chopper / Cruiser', 'ad-integration-for-mobile-de-api' ),
		'CombinationAndSidecar' => __( 'Combination / Sidecar', 'ad-integration-for-mobile-de-api' ),
		'DirtBike' => __( 'Dirt Bike', 'ad-integration-for-mobile-de-api' ),
		'EnduroAndTouringEnduro' => __( 'Enduro / Touring Enduro', 'ad-integration-for-mobile-de-api' ),
		'LightweightMotorcycleAndMotorbike' => __( 'Lightweight Motorcycle / Motorbike', 'ad-integration-for-mobile-de-api' ),
		'MotorAssistedBicycleAndSmallMoped' => __( 'Motor Assisted Bicycle / Small Moped', 'ad-integration-for-mobile-de-api' ),
		'Motorcycle' => __( 'Motorcycle', 'ad-integration-for-mobile-de-api' ),
		'NakedBike' => __( 'Naked Bike', 'ad-integration-for-mobile-de-api' ),
		'Pocketbike' => __( 'Pocket Bike', 'ad-integration-for-mobile-de-api' ),
		'Quad' => __( 'Quad', 'ad-integration-for-mobile-de-api' ),
		'Racing' => __( 'Racing', 'ad-integration-for-mobile-de-api' ),
		'RallyAndCross' => __( 'Rallye / Cross', 'ad-integration-for-mobile-de-api' ),
		'Roadster' => __( 'Roadster', 'ad-integration-for-mobile-de-api' ),
		'Scooter' => __( 'Scooter', 'ad-integration-for-mobile-de-api' ),
		'SportTouringMotorcycle' => __( 'Sport Touring Motorcycle', 'ad-integration-for-mobile-de-api' ),
		'SportsAndSuperSportsBike' => __( 'Sports / Super Sports Bike', 'ad-integration-for-mobile-de-api' ),
		'Streetfighter' => __( 'Streetfighter', 'ad-integration-for-mobile-de-api' ),
		'SuperMoto' => __( 'Super Moto', 'ad-integration-for-mobile-de-api' ),
		'Tourer' => __( 'Tourer', 'ad-integration-for-mobile-de-api' ),
		'Trike' => __( 'Trike', 'ad-integration-for-mobile-de-api' ),
		'OtherMotorbike' => __( 'Other Motorbike', 'ad-integration-for-mobile-de-api' ),

		// Category: Motorhome
		'Alcoves' => __( 'Alcoves', 'ad-integration-for-mobile-de-api' ),
		'CabinMotorhome' => __( 'Cabin Motorhome', 'ad-integration-for-mobile-de-api' ),
		'Caravan' => __( 'Caravan', 'ad-integration-for-mobile-de-api' ),
		'Integrated' => __( 'Integrated Motorhome', 'ad-integration-for-mobile-de-api' ),
		'MobileHome' => __( 'Mobile Home', 'ad-integration-for-mobile-de-api' ),
		'MotorHomesAndPickup' => __( 'Motorhome Pickup', 'ad-integration-for-mobile-de-api' ),
		'PartlyIntegrated' => __( 'Partly Integrated Motorhome', 'ad-integration-for-mobile-de-api' ),
		'VanMotorhome' => __( 'Van', 'ad-integration-for-mobile-de-api' ),
		'OtherMotorhome' => __( 'Other Motorhome', 'ad-integration-for-mobile-de-api' ),
		'MotorHomesAndOther' => __( 'Other Motorhome', 'ad-integration-for-mobile-de-api' ),
	);

	$terms = get_the_terms( $post->ID, 'mobile_api_vehicle_category' );

	if ( $terms && !is_wp_error( $terms ) ) {
		foreach ( $terms AS $term ) {
			if ( in_array( $term->name, $categories ) ) 
				return str_replace( '%mobile_api_vehicle_category%', $term->slug, $permalink );
		}

		/* $slug = array_shift( $terms )->slug; // first category only
		return str_replace( '%mobile_api_vehicle_category%', $slug, $permalink ); */
	} else // If category not set remove placeholder
		return str_replace( '/%mobile_api_vehicle_category%', '', $permalink );
}
add_filter( 'post_type_link', 'mobile_api_vehicle_permalink', 10, 2 );

function mobile_api_vehicle_rewrite_rules() {
	$custom_slug = get_option( 'mobile_api_custom_slug', 'vehicle/%mobile_api_vehicle_category%' );

	$parts = explode( '/', $custom_slug );
	$base  = reset( $parts );

	// Single view with category
	add_rewrite_rule(
		$base.'/([^/]+)/([^/]+)/?$',
		'index.php?mobile-api-vehicle=$matches[2]',
		'top'
	);

	// Single view without category
	add_rewrite_rule(
		$base.'/([^/]+)/?$',
		'index.php?mobile-api-vehicle=$matches[1]',
		'top'
	);
}
add_action( 'init', 'mobile_api_vehicle_rewrite_rules' );

add_action( 'update_option_mobile_api_custom_slug', function( $old, $new ) {
	if ( $old !== $new ) {
		$plugin = new mobile_api();
		$plugin->register_vehicle_taxonomy();
		$plugin->register_vehicle_post_type();

		flush_rewrite_rules();
	}
}, 10, 2 );