<?php

require(dirname(__FILE__) . '/wp-load.php');

function isActuallyEmpty($value) {
	return !isset($value) || $value == "";
}

if (
	isActuallyEmpty($_POST["first_name"]) ||
	isActuallyEmpty($_POST["last_name"]) ||
	isActuallyEmpty($_POST["city"]) ||
	isActuallyEmpty($_POST["street"]) ||
	isActuallyEmpty($_POST["postal_code"]) ||
	isActuallyEmpty($_POST["phone"]) ||
	isActuallyEmpty($_POST["start_place"]) ||
	isActuallyEmpty($_POST["end_place"]) ||
	isActuallyEmpty($_POST["start_time"]) ||
	isActuallyEmpty($_POST["end_time"]) ||
	isActuallyEmpty($_POST["service_type"])
) {
	http_response_code(400);
	die();
}

$post_id = wp_insert_post(array('post_type' => 'aoon_order'));
add_post_meta($post_id, 'first_name', $_POST["first_name"], true);
add_post_meta($post_id, 'last_name', $_POST["last_name"], true);
add_post_meta($post_id, 'city', $_POST["city"], true);
add_post_meta($post_id, 'street', $_POST["street"], true);
add_post_meta($post_id, 'postal_code', $_POST["postal_code"], true);
add_post_meta($post_id, 'phone', $_POST["phone"], true);
add_post_meta($post_id, 'start_place', array('0' => $_POST["start_place"]), true);
add_post_meta($post_id, 'end_place', array('0' => $_POST["end_place"]), true);
add_post_meta($post_id, 'start_time', $_POST["start_time"], true);
add_post_meta($post_id, 'end_time', $_POST["end_time"], true);
add_post_meta($post_id, 'service_type', array('0' => $_POST["service_type"]), true);

//$path = wp_guess_url();
//header("Location: ".$path);
http_response_code(200);
die();