<?php defined( 'ABSPATH' ) or die( "No script kiddies please!" );

global $wpdb;
if(isset($_POST['wpfm_save_template_settings'])){
    
foreach($_POST as $key=>$val)
{
if($key == 'custom_template'){
	$$key = $val;
    } else {
		$$key = sanitize_text_field( $val );
	}
}
    /** Sanitizing each form fields for Menu field added */

	$custom_template_array = array();
    $custom_template_array['custom_template'] = array_map( 'sanitize_text_field', $custom_template );
    $custom_template_name = sanitize_text_field($_POST['wpfm-template-title-name']);
    $table_name = $wpdb->prefix . "wp_floating_menu_custom_templates";    
    $insert = $wpdb->query($wpdb->prepare
        ("INSERT INTO ".$table_name."
        	(template_name, template_details)
        	   VALUES(%s, %s)",
                	array(
                    $custom_template_name,                                                            
                    maybe_serialize($custom_template_array)                                        
	           )
            )
       );
    if($insert){
        $_SESSION['wpfmp_message'] = __( 'New Template Added And Saved.', 'wp-floating-menu-pro' );   
        wp_redirect( admin_url() . 'admin.php?page=wpfmp-custom-template');
    }else{
        $_SESSION['wpfmp_message'] = __( 'New Template Couldnot Be Added .', 'wp-floating-menu-pro' );   
        wp_redirect( admin_url() . 'admin.php?page=wpfmp-custom-template');
    }
 }

/** Condition to save current menu display setting values */
else if(isset($_POST['wpfm_save_edited_template_settings'])){
 foreach($_POST as $key=>$val)
{
if ($key == 'custom_template'){
        $$key = $val;
	} else {
		$$key = sanitize_text_field( $val );
	}
}
    $custom_template_array = array();
    $custom_template_array['custom_template'] = array_map( 'sanitize_text_field', $custom_template );
    $custom_template_name = sanitize_text_field($_POST['wpfm-template-title-name']);
    $current_template_id = $_POST['current_template_id'];   
    $table_name = $wpdb->prefix . "wp_floating_menu_custom_templates";    
    $update = $wpdb->update($table_name, 
        array(
            'template_name' =>$custom_template_name,
            'template_details'=>maybe_serialize($custom_template_array)              
            ),
             array( 
                'ID' => $current_template_id            
                ),
                array(
                     '%s',
                     '%s'                     
                      ),
                      array( 
                        '%d' 
                ));
    if($update){
        $_SESSION['wpfmp_message'] = __( 'Template Saved and Updated.', 'wp-floating-menu-pro' );   
        wp_redirect( admin_url() . 'admin.php?page=wpfmp-custom-template&action=wpfmp-edit-template&id='.$current_template_id );
    }else{
        $_SESSION['wpfmp_message'] = __( 'No Changes Made.', 'wp-floating-menu-pro' );   
        wp_redirect( admin_url() . 'admin.php?page=wpfmp-custom-template&action=wpfmp-edit-template&id='.$current_template_id );
    }   
 }