function wpv_create_view_callback()
{
    global $wpdb;
    if (!wp_verify_nonce($_POST["wpnonce"], 'wp_nonce_create_view')) {
        die("Security check");
    }
    if (!isset($_POST["title"]) || $_POST["title"] == '') {
        $_POST["title"] = __('Unnamed View', 'wp-views');
    }
    // Check for already existing Views with that title
    $postid = $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE post_title = '" . $_POST["title"] . "' AND post_type='view'");
    if (!empty($postid)) {
        $result['error'] = 'error';
        $result['error_message'] = __('A View with that name already exists. Please use another name.', 'wpv-views');
        echo json_encode($result);
        die;
    }
    $post = array('post_type' => 'view', 'post_title' => $_POST["title"], 'post_status' => 'publish', 'post_content' => "[wpv-filter-meta-html]\n[wpv-layout-meta-html]");
    $id = wp_insert_post($post);
    // If we could create the View, populate it with settings
    if (0 != $id) {
        if (!isset($_POST["kind"])) {
            $_POST["kind"] = 'normal';
        }
        // TODO check if kind is needed anymore, see below
        if (!isset($_POST["purpose"])) {
            $_POST["purpose"] = 'full';
        }
        switch ($_POST["kind"]) {
            case 'archive':
                // TODO maybe not needed anymore, we have a specific callback for this
                $view_normal_defaults = wpv_wordpress_archives_defaults('view_settings');
                $view_normal_layout_defaults = wpv_wordpress_archives_defaults('view_layout_settings');
                update_post_meta($id, '_wpv_settings', $view_normal_defaults);
                update_post_meta($id, '_wpv_layout_settings', $view_normal_layout_defaults);
                break;
            default:
                $view_normal_defaults = wpv_view_defaults('view_settings', $_POST["purpose"]);
                $view_normal_layout_defaults = wpv_view_defaults('view_layout_settings', $_POST["purpose"]);
                if ($_POST["purpose"] == 'slider') {
                    // if the purpose is slider, create a CT for every slide, add it to the View layout and to the list of connected CT
                    $temp = wpv_create_new_ct_for_slider_view($id, $_POST["title"]);
                    $ct_post = get_post($temp);
                    $view_normal_layout_defaults['layout_meta_html'] = str_replace('<wpv-loop>', '<wpv-loop>[wpv-post-body view_template="' . $ct_post->post_title . '"]', $view_normal_layout_defaults['layout_meta_html']);
                    $view_normal_layout_defaults['included_ct_ids'] = $temp;
                    update_post_meta($id, '_wpv_first_time_load', 'on');
                }
                update_post_meta($id, '_wpv_settings', $view_normal_defaults);
                update_post_meta($id, '_wpv_layout_settings', $view_normal_layout_defaults);
                break;
        }
        echo $id;
    } else {
        echo 'error';
    }
    die;
}
/**
* wpv_create_view
*
* API function to create a new View
*
* @param $args (array) set of arguments for the new View
*    'title' (string) (semi-mandatory) Title for the View
*    'settings' (array) (optional) Array compatible with the View settings to override the defaults
*    'layout_settings' (array) (optional) Array compatible with the View layout settings to override the defaults
*
* @return (array) response of the operation, one of the following
*    $return['success] = View ID
*    $return['error'] = 'Error message'
*
* @since 1.6.0
*
* @note overriding default Views settings and layout settings must provide complete data when the element is an array, because it overrides them all.
*    For example, $args['settings']['pagination'] can not override just the "postsper page" options: it must provide a complete pagination implementation.
*    This might change and be corrected in the future, keeping backwards compatibility.
*
* @todo once we create a default layout for a View, we need to make sure that:
* - the _view_loop_template postmeat is created and updated - DONE
* - the fields added to that loop Template are stored in the layout settings - PENDING
* - check how Layouts can apply this all to their Views, to create a Bootstrap loop by default - PENDING
*/
function wpv_create_view($args)
{
    global $wpdb;
    $return = array();
    // First, set the title
    if (!isset($args["title"]) || $args["title"] == '') {
        $args["title"] = __('Unnamed View', 'wp-views');
    }
    // Check for already existing Views with that title
    $existing = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} \n\t\t\tWHERE ( post_title = %s OR post_name = %s ) \n\t\t\tAND post_type = 'view' \n\t\t\tLIMIT 1", $args["title"], $args["title"]));
    if ($existing) {
        $return['error'] = __('A View with that name already exists. Please use another name.', 'wpv-views');
        return $return;
    }
    // Compose the $post to be created
    $post = array('post_type' => 'view', 'post_title' => $args["title"], 'post_status' => 'publish', 'post_content' => "[wpv-filter-meta-html]\n[wpv-layout-meta-html]");
    $id = wp_insert_post($post);
    if (0 != $id) {
        if (!isset($args['settings']) || !is_array($args['settings'])) {
            $args['settings'] = array();
        }
        if (!isset($args['layout_settings']) || !is_array($args['layout_settings'])) {
            $args['layout_settings'] = array();
        }
        if (!isset($args['settings']["view-query-mode"])) {
            $args['settings']["view-query-mode"] = 'normal';
            // TODO check if view-query-mode is needed anymore, see below
        }
        if (!isset($args['settings']["view_purpose"])) {
            $args['settings']["view_purpose"] = 'full';
        }
        $create_loop_template = false;
        $create_loop_template_suffix = '';
        $create_loop_template_content = '';
        $create_loop_template_layout = '';
        $add_archive_pagination = false;
        switch ($args['settings']["view-query-mode"]) {
            case 'archive':
                $view_normal_defaults = wpv_wordpress_archives_defaults('view_settings');
                $view_normal_layout_defaults = wpv_wordpress_archives_defaults('view_layout_settings');
                break;
            case 'layouts-loop':
                $view_normal_defaults = wpv_wordpress_archives_defaults('view_settings');
                $view_normal_layout_defaults = wpv_wordpress_archives_defaults('view_layout_settings');
                $create_loop_template = true;
                $create_loop_template_suffix = __('loop item', 'wpv-views');
                $create_loop_template_content = "<h1>[wpv-post-title]</h1>\n[wpv-post-body view_template=\"None\"]\n[wpv-post-featured-image]\n" . sprintf(__('Posted by %s on %s', 'wpv-views'), '[wpv-post-author]', '[wpv-post-date]');
                $add_archive_pagination = true;
                break;
            default:
                $view_normal_defaults = wpv_view_defaults('view_settings', $args['settings']["view_purpose"]);
                $view_normal_layout_defaults = wpv_view_defaults('view_layout_settings', $args['settings']["view_purpose"]);
                if ($args['settings']["view_purpose"] == 'slider') {
                    $create_loop_template = true;
                    $create_loop_template_suffix = __('slide', 'wpv-views');
                    $create_loop_template_content = '[wpv-post-link]';
                } else {
                    if ($args['settings']["view_purpose"] == 'bootstrap-grid') {
                        // Deprecated in Views 1.7, keep for backwards compatibility
                        $args['settings']["view_purpose"] = 'full';
                    }
                }
                break;
        }
        if ($create_loop_template) {
            // @todo review
            // This creates the Template, but it does not adjust the Layout Wizard settings to use it, in case someone touches it
            $template = wpv_create_content_template($args["title"], $create_loop_template_suffix, true, $create_loop_template_content);
            if (isset($template['success'])) {
                $template_id = $template['success'];
                if (isset($template['title'])) {
                    $template_title = $template['title'];
                } else {
                    $template_object = get_post($template_id);
                    $template_title = $template_object->post_title;
                }
                // @todo here we should create the layout acordingly to the $create_loop_template_layout value
                $view_normal_layout_defaults['layout_meta_html'] = str_replace("<wpv-loop>", "<wpv-loop>\n\t\t\t[wpv-post-body view_template=\"" . $template_title . "\"]", $view_normal_layout_defaults['layout_meta_html']);
                $view_normal_layout_defaults['included_ct_ids'] = $template_id;
                update_post_meta($id, '_view_loop_template', $template_id);
                update_post_meta($template_id, '_view_loop_id', $id);
                // @todo
                // I really hate this solution
                update_post_meta($id, '_wpv_first_time_load', 'on');
            }
        }
        if ($add_archive_pagination) {
            $view_normal_layout_defaults['layout_meta_html'] = str_replace("[/wpv-items-found]", "[wpv-archive-pager-prev-page]\n\t\t[wpml-string context=\"wpv-views\"]Older posts[/wpml-string]\n\t[/wpv-archive-pager-prev-page]\n\t[wpv-archive-pager-next-page]\n\t\t[wpml-string context=\"wpv-views\"]Newer posts[/wpml-string]\n\t[/wpv-archive-pager-next-page]\n\t[/wpv-items-found]", $view_normal_layout_defaults['layout_meta_html']);
        }
        // Override the settings with our own
        foreach ($args['settings'] as $key => $value) {
            $view_normal_defaults[$key] = $args['settings'][$key];
        }
        // Override the layout settings with our own
        foreach ($args['layout_settings'] as $key => $value) {
            $view_normal_layout_defaults[$key] = $args['layout_settings'][$key];
        }
        // Set the whole View settings
        update_post_meta($id, '_wpv_settings', $view_normal_defaults);
        update_post_meta($id, '_wpv_layout_settings', $view_normal_layout_defaults);
        $return['success'] = $id;
    } else {
        $return['error'] = __('The View could not be created.', 'wpv-views');
        return $return;
    }
    return $return;
}