<?php 

    // prevent direct file access
	if( !defined( 'ABSPATH' ) ) exit;

    // save custom color
    add_action('wp_ajax_pixe_save_colors', 'pixe_save_colors');
    function pixe_save_colors(){
        
        // check nonce
        if( !check_ajax_referer('pixe-editor', 'security', false) ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('nonce-head'),
                'message'=> pixe_get_error_message('nonce-message')
            )));
        }

        // check permission
        if( !current_user_can('manage_options') ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('permission-head'),
                'message'=> pixe_get_error_message('permission-message'),
            )));
        }
        
        // save custom colors to database
        $site = empty($_POST['site'])? '': sanitize_text_field(wp_unslash($_POST['site']));
        $autoload = empty($site)? true: false;
        if( !empty($_POST['colors']) ){
            $colors = map_deep(wp_unslash($_POST['colors']), 'sanitize_text_field');
            update_option($site . 'pixe_colors', $colors, $autoload);
        }

        die(json_encode(array(
            'status' => 'success',
            'head' => esc_html__('Color Saved', 'pixebuilder')
        )));

    }
    function pixe_get_default_colors(){
        return array(
            'active-preset' => 'default',
            'preset' => array('default' => array( 'core' => 1, 'label' => 'Default Set' )),
            'presetOrder' => array('default')
        );
    }
    function pixe_get_colors(){
        global $pixe_site;
        if( empty($pixe_site) ){
            return get_option('pixe_colors', array());
        }else{
            return get_option($pixe_site . 'pixe_colors', array());
        }
    }

    // save custom variables
    add_action('wp_ajax_pixe_save_variables', 'pixe_save_variables');
    function pixe_save_variables(){

        // check nonce
        if( !check_ajax_referer('pixe-editor', 'security', false) ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('nonce-head'),
                'message'=> pixe_get_error_message('nonce-message')
            )));
        }

        // check permission
        if( !current_user_can('manage_options') ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('permission-head'),
                'message'=> pixe_get_error_message('permission-message'),
            )));
        }

        // save to database
        if( !empty($_POST['variables']) ){
            $variables = map_deep(json_decode(wp_unslash($_POST['variables']), true), 'sanitize_text_field'); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized by map_deep direclty after json_decode
            update_option('pixe_variables', $variables, true);
        }else{
            delete_option('pixe_variables');
        }

        die(json_encode(array(
            'status' => 'success',
            'head' => esc_html__('Variable Saved', 'pixebuilder')
        )));

    }

    function pixe_get_variables(){
        return get_option('pixe_variables', array());
    }

    // save custom icon
    add_action('wp_ajax_pixe_save_icons', 'pixe_save_icons');
    function pixe_save_icons(){
        
        // check nonce
        if( !check_ajax_referer('pixe-editor', 'security', false) ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('nonce-head'),
                'message'=> pixe_get_error_message('nonce-message')
            )));
        }

        // check permission
        if( !current_user_can('manage_options') ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('permission-head'),
                'message'=> pixe_get_error_message('permission-message'),
            )));
        }
        
        // save custom icon to database
        if( !empty($_POST['icons']) ){
            $icons = wp_unslash($_POST['icons']); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized below using array_walk_recursive
            if( !is_array($icons) ){ $icons = array(); }
            array_walk_recursive($icons, 'pixe_sanitize_builder_data');
            update_option('pixe_icons', $icons, false);
        }

        die(json_encode(array(
            'status' => 'success',
            'head' => esc_html__('Icon Saved', 'pixebuilder')
        )));

    }

    function pixe_get_icons(){
        return get_option('pixe_icons', array());
    }

    // save button preset
    add_action('wp_ajax_pixe_save_button_preset', 'pixe_save_button_preset');
    function pixe_save_button_preset(){
        
        // check nonce
        if( !check_ajax_referer('pixe-editor', 'security', false) ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('nonce-head'),
                'message'=> pixe_get_error_message('nonce-message')
            )));
        }

        // check permission
        if( !current_user_can('manage_options') ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('permission-head'),
                'message'=> pixe_get_error_message('permission-message'),
            )));
        }
        
        // save custom button preset to database
        if( !empty($_POST['buttons']) ){
            $buttons = wp_unslash($_POST['buttons']); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized below using array_walk_recursive
            if( !is_array($buttons) ){ $buttons = array(); }
            array_walk_recursive($buttons, 'pixe_sanitize_builder_data');
            update_option('pixe_buttons', $buttons, false);
        }

        die(json_encode(array(
            'status' => 'success',
            'head' => esc_html__('Button Saved', 'pixebuilder')
        )));

    }

    function pixe_get_button_preset(){
        return get_option('pixe_buttons', array());
    }

    // save control preset
    add_action('wp_ajax_pixe_save_controls', 'pixe_save_controls');
    function pixe_save_controls(){
        
        // check nonce
        if( !check_ajax_referer('pixe-editor', 'security', false) ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('nonce-head'),
                'message'=> pixe_get_error_message('nonce-message')
            )));
        }

        // check permission
        if( !current_user_can('manage_options') ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('permission-head'),
                'message'=> pixe_get_error_message('permission-message'),
            )));
        }
        
        // save control to database
        $site = empty($_POST['site'])? '': sanitize_text_field(wp_unslash($_POST['site']));
        $autoload = empty($site)? true: false;
        if( !empty($_POST['controls']) ){
            $controls = wp_unslash($_POST['controls']); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized below using array_walk_recursive
            if( !is_array($controls) ){ $controls = array(); }
            array_walk_recursive($controls, 'pixe_sanitize_builder_data');
            update_option($site . 'pixe_controls', $controls, $autoload);
        }

        die(json_encode(array(
            'status' => 'success',
            'head' => esc_html__('Preset Saved', 'pixebuilder')
        )));

    }

    function pixe_get_controls($type = ''){
        global $pixe_site;
        if( empty($pixe_site) ){
            $controls = get_option('pixe_controls', array());
        }else{
            $controls = get_option($pixe_site . 'pixe_controls', array());

            if( empty($controls) ){
                $controls = file_get_contents(PIXE_LOCAL_URL . '/admin/data/control-preset.json');
                $controls = json_decode($controls, true);
            }
        }

        if( empty($type) ) return $controls;
        return empty($controls[$type])? '': $controls[$type];
    }

    // save font & custom preset
    add_action('wp_ajax_pixe_save_typo', 'pixe_save_typo');
    function pixe_save_typo(){
                
        // check nonce
        if( !check_ajax_referer('pixe-editor', 'security', false) ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('nonce-head'),
                'message'=> pixe_get_error_message('nonce-message')
            )));
        }

        // check permission
        if( !current_user_can('manage_options') ){
            die(json_encode(array(
                'status' => 'failed',
                'head' => pixe_get_error_message('permission-head'),
                'message'=> pixe_get_error_message('permission-message'),
            )));
        }

        // save typo to database
        $site = empty($_POST['site'])? '': sanitize_text_field(wp_unslash($_POST['site']));
        $autoload = empty($site)? true: false;
        if( !empty($_POST['typo']) ){
            $typo = map_deep(wp_unslash($_POST['typo']), 'sanitize_text_field');
            update_option($site . 'pixe_typo', $typo, $autoload);
        }

        die(json_encode(array(
            'status' => 'success',
            'head' => esc_html__('Typography Saved', 'pixebuilder')
        )));
    }

    function pixe_get_typo(){
        global $pixe_site;
        if( empty($pixe_site) ){
            return get_option('pixe_typo', array());
        }else{
            return get_option($pixe_site . 'pixe_typo', array());
        }
    }
