<?php
header('Access-Control-Allow-Origin: *');

// Include Wordpress stuff
define( 'SHORTINIT', true );
require( 'wp-load.php' );

// Just a basic botcheck to get rid of crawlers
if( isset($_REQUEST['botcheck']) && $_REQUEST['botcheck'] == "6rupjf3FinzDGRJ3fBBaru4nAsXC" ){
	$year = 0;
	if( isset( $_REQUEST['year_id'] ) ) {
		$year = $_REQUEST['year_id'];
	}

	$make = 0;
	if( isset( $_REQUEST['make'] ) ) {
		$make = $_REQUEST['make'];
	}

	$model = 0;
	if( isset( $_REQUEST['model'] ) ) {
		$model = $_REQUEST['model'];
	}

	$engine = 0;
	if( isset( $_REQUEST['engine'] ) ) {
		$engine = $_REQUEST['engine'];
	}

	$category_id = '';
	if( isset($_REQUEST['category']) ) {
		$category_id = $_REQUEST['category'];
	}

	// Validate requested IDs
	$_error = false;

	if( ! $_error ) {
		if( $engine > 0 ) {
			$term_engine = get_term( $engine, 'product_ymm' );
			if( $term_engine && $term_engine->parent != $model ) {
				$_error = true;
			}
		}
	}

	if( ! $_error ) {
		if( $model > 0 ) {
			$term_model = get_term( $model, 'product_ymm' );
			if( $term_model && $term_model->parent != $make ) {
				$_error = true;
			}
		}
	}

	if( ! $_error ) {
		if( $make > 0 ) {
			$term_make = get_term( $make, 'product_ymm' );
			if( $term_make && $term_make->parent != $year ) {
				$_error = true;
			}
		}
	}

	$term_id = '';
	if( $_error ) {
		$term_id = -1;
	} else {
		if( $engine > 0 ) {
			$term_id = $engine;
		} else if( $model > 0 ) {
			$term_id = $model;
		} else if( $make > 0 ) {
			$term_id = $make;
		} else if( $year > 0 ) {
			$term_id = $year;
		}
	}


	vpf_ymm_set_cookie ( 'mark_search', array (
		'year_id'		=> $year,
		'make'			=> $make,
		'model'			=> $model,
		'engine'		=> $engine,
		'term_id'		=> $term_id,
		'category_id'	=> $category_id
	) );
}else{
	echo "Go away";
	exit();
}




/**
 * Set Cookie [copied from woo-vpf-ymm-cookies.php for use in this file]
 */
function vpf_ymm_set_cookie ( $identifier = '', $item = '', $type = ''/*, $sub_identifier = ''*/ ) { // $type is for array $item only
	if ( empty ( $identifier ) ) {
		return;
	}

	$expiry = 0;

	//setcookie ( 'vpf_ymm', '', time() - 3600, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN ); exit;

	$vpf_ymm = array ();

	if ( isset ( $_COOKIE['vpf_ymm'] ) ) {
		$vpf_ymm = vpf_ymm_get_cookie ();

		if ( empty ( $vpf_ymm ) ) {
			$vpf_ymm = array ();
		}
	}

	if ( $type != '' ) { // append or delete
		if ( ! isset ( $vpf_ymm[ $identifier ] ) || empty ( $vpf_ymm[ $identifier ] ) ) {
			$vpf_ymm[ $identifier ] = array ();
		}

		// Delete Item for Reset Latest Item on Top
		if ( ! empty ( $vpf_ymm[ $identifier ] ) && in_array ( $item, $vpf_ymm[ $identifier ] ) ) {
			$item_key = array_search ( $item, $vpf_ymm[ $identifier ] );

			if ( $item_key !== false ) {
				unset ( $vpf_ymm[ $identifier ][ $item_key ] );
			}
		}

		if ( $type == 'append' && ! empty ( $item ) ) {
			$vpf_ymm[ $identifier ][] = $item;
		}

		// Reset Array Keys from Zero
		if ( ! empty ( $vpf_ymm[ $identifier ] ) ) {
			$vpf_ymm[ $identifier ] = array_values ( $vpf_ymm[ $identifier ] );
		}
	} else {
		if ( empty ( $item ) ) {
			$expiry	= time() - 3600;
		}

		$vpf_ymm[ $identifier ] = $item;
	}

	$vpf_ymm = maybe_serialize ( $vpf_ymm );
	setcookie ( 'vpf_ymm', $vpf_ymm, $expiry, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN );
	$_COOKIE['vpf_ymm'] = $vpf_ymm;
}