<?php
$path = $_SERVER['DOCUMENT_ROOT'];

include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';

if (isset($_POST['submit'])) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'locations';
    $res = $_POST['result'];
    $del = $_POST['deleted'];
    $ind = 0;
    if($del){
        foreach ($del as $delt){
            if($wpdb->get_var('SELECT id FROM '.$table_name.' WHERE id = '.$delt)){
                $wpdb->delete($table_name, array('id' => $delt));
            }
        }
    }
    if($res){
        foreach ($res as $resu) {
            $id = $resu['id'];
            $item = $wpdb->get_var('SELECT id FROM '.$table_name.' WHERE id = '.$id);
            if ($item) {
                $success = $wpdb->update($table_name, $resu, array('id' => $resu['id']));
            } else {
                $success = $wpdb->insert($table_name, array(
                    "id" => $resu['id'],
                    "loc" => $resu['loc'],
                    "url" => $resu['url'],
                ));
            }
        }
    }
    $location = $_SERVER['HTTP_REFERER'];
    wp_safe_redirect($location);
}

