/**
  * Add new layout
  *
  * @since 1.0.0
  */
 public function add_layout()
 {
     // Make sure Satan isn't lurking
     check_ajax_referer('themeblvd_new_builder', 'security');
     // Handle form data
     parse_str($_POST['data'], $config);
     // Setup arguments for new 'layout' post
     $args = array('post_type' => 'tb_layout', 'post_title' => $config['tb_new_layout']['layout_name'], 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed');
     // Create new post
     $post_id = wp_insert_post($args);
     // Setup meta
     if (!empty($config['tb_new_layout']['layout_start'])) {
         if ($config['tb_new_layout']['layout_start'] == 'layout') {
             // Configure meta for pre-existing layout
             $layout_id = $config['tb_new_layout']['layout_existing'];
             $elements = get_post_meta($layout_id, 'elements', true);
             $settings = get_post_meta($layout_id, 'settings', true);
         } else {
             if ($config['tb_new_layout']['layout_start'] == 'sample') {
                 // Configure meta for sample layout
                 $samples = themeblvd_get_sample_layouts();
                 $current_sample = $samples[$config['tb_new_layout']['layout_sample']];
                 $elements = array('featured' => $current_sample['featured'], 'primary' => $current_sample['primary'], 'featured_below' => $current_sample['featured_below']);
                 $settings = array('sidebar_layout' => $current_sample['sidebar_layout']);
             } else {
                 // Configure meta for blank layout
                 $elements = array();
                 $settings = array('sidebar_layout' => $config['tb_new_layout']['layout_sidebar']);
             }
         }
     }
     // Update even if they're empty
     update_post_meta($post_id, 'elements', $elements);
     update_post_meta($post_id, 'settings', $settings);
     // Adjust response depending on where the creation
     // of the layout happenned.
     if (!isset($config['action']) || $config['action'] != 'editpost') {
         // If this coming from the Builder, send back Post
         // ID and edit layout interface.
         echo $post_id . '[(=>)]';
         $this->admin_page->edit_layout($post_id);
     } else {
         // If this is coming from the Edit Page meta box,
         // send back post slug (i.e. layout ID).
         $post = get_post($post_id);
         echo $post->post_name;
     }
     die;
 }
/**
 * Sample layout previews when selecting one.
 *
 * @since 1.0.0
 *
 * @return string $output HTML to display
 */
function themeblvd_builder_sample_previews()
{
    // Get sample layouts
    $samples = themeblvd_get_sample_layouts();
    // Construct output
    $output = '<div class="sample-layouts">';
    foreach ($samples as $sample) {
        $output .= '<div id="sample-' . $sample['id'] . '">';
        $output .= '<img src="' . $sample['preview'] . '" />';
        if (isset($sample['credit'])) {
            $output .= '<p class="note">' . $sample['credit'] . '</p>';
        }
        $output .= '</div>';
    }
    $output .= '</div><!-- .sample-layouts (end) -->';
    return $output;
}
    /**
     * Generates the the interface to add a new layout.
     *
     * @since 1.0.0
     */
    public function add_layout()
    {
        // Setup sidebar layouts
        $layouts = themeblvd_sidebar_layouts();
        $sidebar_layouts = array('default' => __('Default Sidebar Layout', 'themeblvd_builder'));
        foreach ($layouts as $layout) {
            $sidebar_layouts[$layout['id']] = $layout['name'];
        }
        // Setup sample layouts
        $samples = themeblvd_get_sample_layouts();
        $sample_layouts = array();
        if ($samples) {
            foreach ($samples as $sample) {
                $sample_layouts[$sample['id']] = $sample['name'];
            }
        }
        // Setup existing layouts
        $layouts = get_posts('post_type=tb_layout&numberposts=-1');
        $custom_layouts = array();
        if ($layouts) {
            foreach ($layouts as $layout) {
                $custom_layouts[$layout->ID] = $layout->post_title;
            }
        }
        // Setup options array to display form
        $options = array();
        // Layout Name
        $options[] = array('name' => __('Layout Name', 'themeblvd_builder'), 'desc' => __('Enter a user-friendly name for your layout. You will not be able to change this after you\'ve created the layout.<br><br><em>Example: My Layout</em>', 'themeblvd_builder'), 'id' => 'layout_name', 'type' => 'text');
        // Start subgroup for starting point
        $options[] = array('type' => 'subgroup_start');
        // Starting point
        $options[] = array('name' => __('Starting Point', 'themeblvd_builder'), 'desc' => __('Select if you\'d like to start building your layout from scratch, from a pre-existing layout, or from a sample layout.', 'themeblvd_builder'), 'id' => 'layout_start', 'type' => 'select', 'options' => array('scratch' => __('Start From Scratch', 'themeblvd_builder'), 'layout' => __('Start From Existing Layout', 'themeblvd_builder'), 'sample' => __('Start From Sample Layout', 'themeblvd_builder')));
        if (!$sample_layouts) {
            unset($options[2]['options']['sample']);
        }
        if (!$custom_layouts) {
            unset($options[2]['options']['layout']);
        }
        // Existing Layout
        if ($custom_layouts) {
            $options[] = array('name' => __('Custom Layouts', 'themeblvd_builder'), 'desc' => __('Select one of the layouts you created previously to start this new one.', 'themeblvd_builder'), 'id' => 'layout_existing', 'type' => 'select', 'options' => $custom_layouts);
        }
        // Sample Layouts (only show if there are sample layouts)
        if ($sample_layouts) {
            $options[] = array('name' => __('Sample Layout', 'themeblvd_builder'), 'desc' => __('Select a sample layout to start from.', 'themeblvd_builder'), 'id' => 'layout_sample', 'type' => 'select', 'options' => $sample_layouts, 'class' => 'builder_samples');
        }
        // End subgroup for starting point
        $options[] = array('type' => 'subgroup_end');
        // Sidebar Layout
        $options[] = array('name' => __('Sidebar Layout', 'themeblvd_builder'), 'desc' => __('Select your sidebar layout for this page.<br><br><em>Note: You can change this later when editing your layout.</em>', 'themeblvd_builder'), 'id' => 'layout_sidebar', 'type' => 'select', 'options' => $sidebar_layouts);
        $options = apply_filters('themeblvd_add_layout', $options);
        // Build form
        $form = themeblvd_option_fields('tb_new_layout', $options, null, false);
        ?>
		<div class="metabox-holder">
			<div class="postbox">
				<h3><?php 
        _e('Add New Layout', 'themeblvd_builder');
        ?>
</h3>
				<div class="inner-group">
					<?php 
        echo $form[0];
        ?>
				</div><!-- .group (end) -->
				<div id="optionsframework-submit">
					<input type="submit" class="button-primary" name="update" value="<?php 
        _e('Add New Layout', 'themeblvd_builder');
        ?>
">
					<img src="<?php 
        echo esc_url(admin_url('images/wpspin_light.gif'));
        ?>
" class="ajax-loading" id="ajax-loading">
		            <div class="clear"></div>
				</div>
			</div><!-- .postbox (end) -->
		</div><!-- .metabox-holder (end) -->
		<?php 
    }