/**
 * check file callback
 */
function jcf_ajax_check_file()
{
    $jcf_read_settings = $_POST['jcf_read_settings'];
    if ($jcf_read_settings == JCF_CONF_SOURCE_FS_THEME or $jcf_read_settings == JCF_CONF_SOURCE_FS_GLOBAL) {
        $file = jcf_get_settings_file_path($jcf_read_settings);
        if ($jcf_read_settings == JCF_CONF_SOURCE_FS_THEME) {
            $msg = __("The settings will be written to your theme folder.\nIn case you have settings there, they will be overwritten.\nPlease confirm that you want to continue.", JCF_TEXTDOMAIN);
        } else {
            $msg = __("The settings will be written to folder wp-conten/jcf-settings.\nIn case you have settings there, they will be overwritten.\nPlease confirm that you want to continue.", JCF_TEXTDOMAIN);
        }
        if (file_exists($file)) {
            $resp = array('status' => '1', 'msg' => $msg);
        } else {
            $resp = array('status' => '1', 'file' => '1');
        }
    } else {
        $resp = array('status' => '1');
    }
    jcf_ajax_reposnse($resp, 'json');
}
/**
 *	Function for update saving method
 *	@return string Return read method from file or database
 */
function jcf_update_read_settings()
{
    $current_value = jcf_get_read_settings();
    $new_value = $_POST['jcf_read_settings'];
    if (MULTISITE && ($_POST['jcf_multisite_setting'] != JCF_CONF_MS_NETWORK && $new_value == JCF_CONF_SOURCE_FS_GLOBAL)) {
        jcf_add_admin_notice('error', __('<strong>Settings storage update FAILED!</strong>. Your MultiSite Settings do not allow to set global storage in FileSystem', JCF_TEXTDOMAIN));
        return $current_value;
    } else {
        if (!empty($current_value)) {
            // if need to copy settings from db to FS
            if (!empty($_POST['jcf_keep_settings'])) {
                if (in_array($new_value, array(JCF_CONF_SOURCE_FS_GLOBAL, JCF_CONF_SOURCE_FS_THEME))) {
                    $file = jcf_get_settings_file_path($new_value);
                    if (jcf_clone_db_settings_to_fs($file, $new_value)) {
                        jcf_add_admin_notice('notice', __('<strong>Database settings has been imported</strong> to file system.', JCF_TEXTDOMAIN));
                        $saved = update_site_option('jcf_read_settings', $new_value);
                    } else {
                        jcf_add_admin_notice('error', __('<strong>Database settings import to file system FAILED!</strong>. Revert settings storage to Database.', JCF_TEXTDOMAIN));
                    }
                }
            } else {
                $saved = update_site_option('jcf_read_settings', $new_value);
            }
        } else {
            $saved = add_site_option('jcf_read_settings', $new_value);
        }
        if ($saved) {
            jcf_add_admin_notice('notice', __('<strong>Settings storage</strong> configurations has been updated.', JCF_TEXTDOMAIN));
        }
        return $saved ? $new_value : $current_value;
    }
}