<?php
require '../../../wp-blog-header.php';
header("HTTP/1.1 200 OK");

global $wpdb;
$wpdb->show_errors();

if (isset($_GET['delete'])) {
	$table = $_GET['target'];
	$table_name = $wpdb->prefix . $table;

	$id = $_GET['id'];
	$wpdb->delete($table_name, array('id' => $id));

	echo "deleted";
}

if (isset($_POST['target'])) {
	$table = $_POST['target'];
	$table_name = $wpdb->prefix . $table;
	$id = $_POST['id'];

	if ($table === 'smfeeds') {
		$wpdb->update(
			$table_name,
			array(
				'url' => $_POST['url'],
				'contentType' => $_POST['contentType'],
				'titlesetId' => $_POST['titlesetId'],
				'ioId' => $_POST['ioId'],
			),
			array('id' => $id)
		);
	} else if ($table === 'smtitlesets') {
		$title = $_POST['title'];
		if (!is_array($title)) {
			$title = [$title];
		}

		$wpdb->update(
			$table_name,
			array(
				'setname' => trim($_POST['setname']),
				'title' => implode('|:|', $title),
			),
			array('id' => $id)
		);
	} else if ($table === 'smintrosoutros') {
		$wpdb->update(
			$table_name,
			array(
				'setname' => trim($_POST['setname']),
				'intro' => trim($_POST['intro']),
				'outro' => trim($_POST['outro']),
			),
			array('id' => $id)
		);
	} else if ($table === 'smcustom') {
		$wpdb->update(
			$table_name,
			array(
				'headline' => trim($_POST['headline']),
				'message' => trim($_POST['message']),
				'url' => trim($_POST['url']),
				'image' => trim($_POST['image']),
			),
			array('id' => $id)
		);
	}

	echo "updated";
}

exit;
