<?php
/**
 *
 * Custom types form
 *
 *
 */

/**
 * Add/edit form
 */
function wpcf_admin_custom_types_form()
{
    global $wpcf;

    include_once dirname(__FILE__).'/common-functions.php';
    include_once dirname(__FILE__).'/fields.php';

    $ct = array();
    $id = false;
    $update = false;

    if ( isset( $_GET['wpcf-post-type'] ) ) {
        $id = sanitize_text_field( $_GET['wpcf-post-type'] );
    } elseif ( isset( $_POST['wpcf-post-type'] ) ) {
        $id = sanitize_text_field( $_POST['wpcf-post-type'] );
    }

    if ( $id ) {
        $custom_types = get_option( WPCF_OPTION_NAME_CUSTOM_TYPES, array() );
        if ( isset( $custom_types[$id] ) ) {
            $ct = $custom_types[$id];
            $update = true;
            // Set rewrite if needed
            if ( isset( $_GET['wpcf-rewrite'] ) ) {
                flush_rewrite_rules();
            }
        } else {
            $buildin_post_types = wpcf_get_builtin_in_post_types();
            if ( isset($buildin_post_types[$id]) ) {
                $ct = get_object_vars(get_post_type_object($id));
                $ct['labels'] = get_object_vars($ct['labels']);
                $ct['slug'] = esc_attr($id);
                $ct['_builtin'] = true;
            } else {
                wpcf_admin_message( __( 'Wrong custom post type specified', 'wpcf' ), 'error' );
                return false;
            }
        }
    } else {
        $ct = wpcf_custom_types_default();
    }

    $current_user_can_edit = WPCF_Roles::user_can_edit('custom-post-type', $ct);

    /**
     * sanitize _builtin
     */
    if ( !isset($ct['_builtin']) ) {
        $ct['_builtin'] = false;
    }

    /**
     * fix taxonomies assigment for builitin post types
     */
    if ( $ct['_builtin']) {
        $taxonomies = get_taxonomies( '', 'objects' );
        foreach( $taxonomies as $slug => $tax ) {
            foreach( $tax->object_type as $post_slug ) {
                if ( $ct['slug'] == $post_slug) {
                    $ct['taxonomies'][$slug] = 1;
                }
            }
        }
    }

    $form = array();
    /**
     * postbox-controll
     */
    $markup = wp_nonce_field('closedpostboxes', 'closedpostboxesnonce', false, false );
    $markup.= wp_nonce_field('meta-box-order', 'meta-box-order-nonce', false, false );
    $form['postbox-controll'] = array(
        '#type' => 'markup',
        '#markup' => $markup,
        '_builtin' => true,
    );

    /**
     * form setup
     */
    $form['#form']['callback'] = 'wpcf_admin_custom_types_form_submit';
    $form['#form']['redirection'] = false;

    if ( $current_user_can_edit && $update ) {
        $form['id'] = array(
            '#type' => 'hidden',
            '#value' => $id,
            '#name' => 'ct[wpcf-post-type]',
            '_builtin' => true,
        );
        /**
         * update taxonomy too
         */
        $custom_taxonomies = get_option( WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array() );
        foreach( $custom_taxonomies as $slug => $data ) {
            if ( !array_key_exists('supports', $data)) {
                continue;
            }
            if ( !array_key_exists($id, $data['supports']) ) {
                continue;
            }
            if (
                array_key_exists('taxonomies', $ct)
                && array_key_exists($slug, $ct['taxonomies'])
            ) {
                continue;
            }
            unset($custom_taxonomies[$slug]['supports'][$id]);
        }
        update_option( WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies);
    }

    /**
     * WP control for meta boxes
     */
    include_once ABSPATH.'/wp-admin/includes/meta-boxes.php';
    wp_enqueue_script( 'post' );

    $form['form-open'] = array(
        '#type' => 'markup',
        '#markup' => sprintf(
            '<div id="poststuff" class="%s">',
            $current_user_can_edit? '':'wpcf-types-read-only'
        ),
        '_builtin' => true,
    );

    $form['form-metabox-holder-columns-2-open'] = array(
        '#type' => 'markup',
        '#markup' => '<div id="post-body" class="metabox-holder columns-2">',
        '_builtin' => true,
    );

    $form['post-body-content-open'] = array(
        '#type' => 'markup',
        '#markup' => '<div id="post-body-content">',
        '_builtin' => true,
    );

    $form['table-1-open'] = array(
        '#type' => 'markup',
        '#markup' => '<table id="wpcf-types-form-name-table" class="wpcf-types-form-table widefat js-wpcf-slugize-container"><thead><tr><th colspan="2">' . __( 'Name and description', 'wpcf' ) . '</th></tr></thead><tbody>',
        '_builtin' => true,
    );
    $table_row = '<tr><td><LABEL></td><td><ERROR><ELEMENT></td></tr>';
    $form['name'] = array(
        '#type' => 'textfield',
        '#name' => 'ct[labels][name]',
        '#title' => __( 'Custom post type name plural', 'wpcf' ) . ' (<strong>' . __( 'required', 'wpcf' ) . '</strong>)',
        '#description' => '<strong>' . __( 'Enter in plural!', 'wpcf' )
        . '.',
        '#value' => isset( $ct['labels']['name'] ) ? $ct['labels']['name'] : '',
        '#validate' => array(
            'required' => array('value' => 'true'),
        ),
        '#pattern' => $table_row,
        '#inline' => true,
        '#id' => 'name-plural',
        '#attributes' => array(
            'data-wpcf_warning_same_as_slug' => $wpcf->post_types->message( 'warning_singular_plural_match' ),
            'data-wpcf_warning_same_as_slug_ignore' => $wpcf->post_types->message( 'warning_singular_plural_match_ignore' ),
            'placeholder' => __('Enter post type name plural', 'wpcf' ),
        ),
        '_builtin' => true,
    );
    $form['name-singular'] = array(
        '#type' => 'textfield',
        '#name' => 'ct[labels][singular_name]',
        '#title' => __( 'Custom post type name singular', 'wpcf' ) . ' (<strong>' . __( 'required', 'wpcf' ) . '</strong>)',
        '#description' => '<strong>' . __( 'Enter in singular!', 'wpcf' )
        . '</strong><br />'
        . '.',
        '#value' => isset( $ct['labels']['singular_name'] ) ? $ct['labels']['singular_name'] : '',
        '#validate' => array(
            'required' => array('value' => 'true'),
        ),
        '#pattern' => $table_row,
        '#inline' => true,
        '#id' => 'name-singular',
        '#attributes' => array(
            'placeholder' => __('Enter post type name singular', 'wpcf' ),
            'class' => 'js-wpcf-slugize-source',
        ),
        '_builtin' => true,
    );

    /**
     * IF isset $_POST['slug'] it means form is not submitted
     */
    $attributes = array();
    if ( !empty( $_POST['ct']['slug'] ) ) {
        $reserved = wpcf_is_reserved_name( sanitize_text_field( $_POST['ct']['slug'] ), 'post_type' );
        if ( is_wp_error( $reserved ) ) {
            $attributes = array(
                'class' => 'wpcf-form-error',
                'onclick' => 'jQuery(this).removeClass(\'wpcf-form-error\');'
            );
        }
    }

    $form['slug'] = array(
        '#type' => 'textfield',
        '#name' => 'ct[slug]',
        '#title' => __( 'Slug', 'wpcf' ) . ' (<strong>' . __( 'required', 'wpcf' ) . '</strong>)',
        '#value' => isset( $ct['slug'] ) ? $ct['slug'] : '',
        '#pattern' => $table_row,
        '#inline' => true,
        '#validate' => array(
            'required' => array('value' => 'true'),
            'nospecialchars' => array('value' => 'true'),
            'maxlength' => array('value' => '20'),
        ),
        '#attributes' => $attributes + array(
            'maxlength' => '20',
            'placeholder' => __('Enter post type slug', 'wpcf' ),
            'class' => 'js-wpcf-slugize',
        ),
        '#id' => 'slug',
        '_builtin' => true,
    );
    $form['description'] = array(
        '#type' => 'textarea',
        '#name' => 'ct[description]',
        '#title' => __( 'Description', 'wpcf' ),
        '#value' => isset( $ct['description'] ) ? $ct['description'] : '',
        '#attributes' => array(
            'rows' => 4,
            'cols' => 60,
            'placeholder' => __('Enter post type description', 'wpcf' ),
        ),
        '#pattern' => $table_row,
        '#inline' => true,
    );
    /**
     * icons only for version 3.8 up
     */
    global $wp_version;
    if ( version_compare( '3.8', $wp_version ) < 1 ) {
        $form['icon'] = array(
            '#type' => 'select',
            '#name' => 'ct[icon]',
            '#title' => __( 'Icon', 'wpcf' ),
            '#default_value' => isset( $ct['icon'] ) ? $ct['icon'] : 'admin-post',
            '#pattern' => $table_row,
            '#inline' => true,
            '#id' => 'wpcf-types-icon',
            '#options' => array(
                'admin appearance' => 'admin-appearance',
                'admin collapse' => 'admin-collapse',
                'admin comments' => 'admin-comments',
                'admin generic' => 'admin-generic',
                'admin home' => 'admin-home',
                'admin links' => 'admin-links',
                'admin media' => 'admin-media',
                'admin network' => 'admin-network',
                'admin page' => 'admin-page',
                'admin plugins' => 'admin-plugins',
                'admin post' => 'admin-post',
                'admin settings' => 'admin-settings',
                'admin site' => 'admin-site',
                'admin tools' => 'admin-tools',
                'admin users' => 'admin-users',
                'album' => 'album',
                'align center' => 'align-center',
                'align left' => 'align-left',
                'align none' => 'align-none',
                'align right' => 'align-right',
                'analytics' => 'analytics',
                'archive' => 'archive',
                'arrow down alt2' => 'arrow-down-alt2',
                'arrow down alt' => 'arrow-down-alt',
                'arrow down' => 'arrow-down',
                'arrow left alt2' => 'arrow-left-alt2',
                'arrow left alt' => 'arrow-left-alt',
                'arrow left' => 'arrow-left',
                'arrow right alt2' => 'arrow-right-alt2',
                'arrow right alt' => 'arrow-right-alt',
                'arrow right' => 'arrow-right',
                'arrow up alt2' => 'arrow-up-alt2',
                'arrow up alt' => 'arrow-up-alt',
                'arrow up' => 'arrow-up',
                'art' => 'art',
                'awards' => 'awards',
                'backup' => 'backup',
                'book alt' => 'book-alt',
                'book' => 'book',
                'building' => 'building',
                'businessman' => 'businessman',
                'calendar alt' => 'calendar-alt',
                'calendar' => 'calendar',
                'camera' => 'camera',
                'carrot' => 'carrot',
                'cart' => 'cart',
                'category' => 'category',
                'chart area' => 'chart-area',
                'chart bar' => 'chart-bar',
                'chart line' => 'chart-line',
                'chart pie' => 'chart-pie',
                'clipboard' => 'clipboard',
                'clock' => 'clock',
                'cloud' => 'cloud',
                'controls back' => 'controls-back',
                'controls forward' => 'controls-forward',
                'controls pause' => 'controls-pause',
                'controls play' => 'controls-play',
                'controls repeat' => 'controls-repeat',
                'controls skipback' => 'controls-skipback',
                'controls skipforward' => 'controls-skipforward',
                'controls volumeoff' => 'controls-volumeoff',
                'controls volumeon' => 'controls-volumeon',
                'dashboard' => 'dashboard',
                'desktop' => 'desktop',
                'dismiss' => 'dismiss',
                'download' => 'download',
                'editor aligncenter' => 'editor-aligncenter',
                'editor alignleft' => 'editor-alignleft',
                'editor alignright' => 'editor-alignright',
                'editor bold' => 'editor-bold',
                'editor break' => 'editor-break',
                'editor code' => 'editor-code',
                'editor contract' => 'editor-contract',
                'editor customchar' => 'editor-customchar',
                'editor distractionfree' => 'editor-distractionfree',
                'editor expand' => 'editor-expand',
                'editor help' => 'editor-help',
                'editor indent' => 'editor-indent',
                'editor insertmore' => 'editor-insertmore',
                'editor italic' => 'editor-italic',
                'editor justify' => 'editor-justify',
                'editor kitchensink' => 'editor-kitchensink',
                'editor ol' => 'editor-ol',
                'editor outdent' => 'editor-outdent',
                'editor paragraph' => 'editor-paragraph',
                'editor paste text' => 'editor-paste-text',
                'editor paste word' => 'editor-paste-word',
                'editor quote' => 'editor-quote',
                'editor removeformatting' => 'editor-removeformatting',
                'editor rtl' => 'editor-rtl',
                'editor spellcheck' => 'editor-spellcheck',
                'editor strikethrough' => 'editor-strikethrough',
                'editor textcolor' => 'editor-textcolor',
                'editor ul' => 'editor-ul',
                'editor underline' => 'editor-underline',
                'editor unlink' => 'editor-unlink',
                'editor video' => 'editor-video',
                'edit' => 'edit',
                'email alt' => 'email-alt',
                'email' => 'email',
                'excerpt view' => 'excerpt-view',
                'exerpt view' => 'exerpt-view',
                'external' => 'external',
                'facebook alt' => 'facebook-alt',
                'facebook' => 'facebook',
                'feedback' => 'feedback',
                'flag' => 'flag',
                'format aside' => 'format-aside',
                'format audio' => 'format-audio',
                'format chat' => 'format-chat',
                'format gallery' => 'format-gallery',
                'format image' => 'format-image',
                'format links' => 'format-links',
                'format quote' => 'format-quote',
                'format standard' => 'format-standard',
                'format status' => 'format-status',
                'format video' => 'format-video',
                'forms' => 'forms',
                'googleplus' => 'googleplus',
                'grid view' => 'grid-view',
                'groups' => 'groups',
                'hammer' => 'hammer',
                'heart' => 'heart',
                'id alt' => 'id-alt',
                'id' => 'id',
                'images alt2' => 'images-alt2',
                'images alt' => 'images-alt',
                'image crop' => 'image-crop',
                'image flip horizontal' => 'image-flip-horizontal',
                'image flip vertical' => 'image-flip-vertical',
                'image rotate left' => 'image-rotate-left',
                'image rotate right' => 'image-rotate-right',
                'index card' => 'index-card',
                'info' => 'info',
                'leftright' => 'leftright',
                'lightbulb' => 'lightbulb',
                'list view' => 'list-view',
                'location alt' => 'location-alt',
                'location' => 'location',
                'lock' => 'lock',
                'marker' => 'marker',
                'media archive' => 'media-archive',
                'media audio' => 'media-audio',
                'media code' => 'media-code',
                'media default' => 'media-default',
                'media document' => 'media-document',
                'media interactive' => 'media-interactive',
                'media spreadsheet' => 'media-spreadsheet',
                'media text' => 'media-text',
                'media video' => 'media-video',
                'megaphone' => 'megaphone',
                'menu' => 'menu',
                'microphone' => 'microphone',
                'migrate' => 'migrate',
                'minus' => 'minus',
                'money' => 'money',
                'nametag' => 'nametag',
                'networking' => 'networking',
                'no alt' => 'no-alt',
                'no' => 'no',
                'palmtree' => 'palmtree',
                'performance' => 'performance',
                'phone' => 'phone',
                'playlist audio' => 'playlist-audio',
                'playlist video' => 'playlist-video',
                'plus alt' => 'plus-alt',
                'plus' => 'plus',
                'portfolio' => 'portfolio',
                'post status' => 'post-status',
                'post trash' => 'post-trash',
                'pressthis' => 'pressthis',
                'products' => 'products',
                'randomize' => 'randomize',
                'redo' => 'redo',
                'rss' => 'rss',
                'schedule' => 'schedule',
                'screenoptions' => 'screenoptions',
                'search' => 'search',
                'share1' => 'share1',
                'share alt2' => 'share-alt2',
                'share alt' => 'share-alt',
                'share' => 'share',
                'shield alt' => 'shield-alt',
                'shield' => 'shield',
                'slides' => 'slides',
                'smartphone' => 'smartphone',
                'smiley' => 'smiley',
                'sort' => 'sort',
                'sos' => 'sos',
                'star empty' => 'star-empty',
                'star filled' => 'star-filled',
                'star half' => 'star-half',
                'store' => 'store',
                'tablet' => 'tablet',
                'tagcloud' => 'tagcloud',
                'tag' => 'tag',
                'testimonial' => 'testimonial',
                'text' => 'text',
                'tickets alt' => 'tickets-alt',
                'tickets' => 'tickets',
                'translation' => 'translation',
                'trash' => 'trash',
                'twitter' => 'twitter',
                'undo' => 'undo',
                'universal access alt' => 'universal-access-alt',
                'universal access' => 'universal-access',
                'update' => 'update',
                'upload' => 'upload',
                'vault' => 'vault',
                'video alt2' => 'video-alt2',
                'video alt3' => 'video-alt3',
                'video alt' => 'video-alt',
                'visibility' => 'visibility',
                'welcome add page' => 'welcome-add-page',
                'welcome comments' => 'welcome-comments',
                'welcome edit page' => 'welcome-edit-page',
                'welcome learn more' => 'welcome-learn-more',
                'welcome view site' => 'welcome-view-site',
                'welcome widgets menus' => 'welcome-widgets-menus',
                'welcome write blog' => 'welcome-write-blog',
                'wordpress alt' => 'wordpress-alt',
                'wordpress' => 'wordpress',
                'yes' => 'yes',
            ),
        );
    }

    $form['table-1-close'] = array(
        '#type' => 'markup',
        '#markup' => '</tbody></table>',
        '_builtin' => true,
    );

    global $sitepress;
    if (
        $update && isset( $sitepress )
        && defined('ICL_SITEPRESS_VERSION')
        && version_compare( ICL_SITEPRESS_VERSION, '2.6.2', '>=' )
        && function_exists( 'wpml_custom_post_translation_options' )
    ) {
        $form['table-sitepress'] = array(
            '#type' => 'markup',
            '#markup' => wpml_custom_post_translation_options( $ct['slug'] ),
            '_builtin' => true,
        );
    }

    $form['post-body-content-close'] = array(
        '#type' => 'markup',
        '#markup' => '</div>',
        '_builtin' => true,
    );

    /**
     * get box order
     */
    $meta_box_order_defaults = apply_filters(
        'wpcf_meta_box_order_defaults',
        array(
            'side' => array('submitdiv', 'wpcf_visibility', 'taxonomies'),
            'normal' => array('labels', 'display_sections', 'options'),
        ),
        'post_type'
    );
    $screen = get_current_screen();
    if ( false == ( $meta_box_order = get_user_option( 'meta-box-order_'.$screen->id) )) {
        $meta_box_order = $meta_box_order_defaults;
    } else {
        if ( isset($meta_box_order[0]) && !isset($meta_box_order['normal']) ) {
            $meta_box_order['normal'] = $meta_box_order[0];
        }
    }

    $meta_boxes = array();
    foreach( $meta_box_order_defaults as $key => $value ) {
        foreach($value as $meta_box_key) {
            $meta_boxes[$meta_box_key] = $ct;
        }
    }

    if ( !$current_user_can_edit ) {
        $meta_boxes[ 'submitdiv'] = false;
    }

    foreach ( $meta_box_order as $key => $value ) {
        if ( is_array($value) ) {
            continue;
        }
        $meta_box_order[$key] = explode(',', $value);
    }

    /**
     * postbox-container-1
     */

    $form['postbox-container-1-open'] = array(
        '#type' => 'markup',
        '#markup' => '<div id="postbox-container-1" class="postbox-container"><div class="meta-box-sortables ui-sortable" id="side-sortables">',
        '_builtin' => true,
    );
    foreach( $meta_box_order['side'] as $key ) {
        $function = sprintf('wpcf_admin_metabox_%s', $key);
        if ( is_callable($function) && isset($meta_boxes[$key])) {
            $form += $function($meta_boxes[$key]);
            unset($meta_boxes[$key]);
        }
    }
    /* close side container */
    $form['postbox-container-1-close'] = array(
        '#type' => 'markup',
        '#markup' => '</div></div>',
        '_builtin' => true,
    );

    /**
     * normal container
     */

    $form['postbox-container-2-open'] = array(
        '#type' => 'markup',
        '#markup' => '<div id="postbox-container-2" class="postbox-container"><div class="meta-box-sortables ui-sortable">',
        '_builtin' => true,
    );
    foreach( $meta_box_order['normal'] as $key ) {
        $function = sprintf('wpcf_admin_metabox_%s', $key);
        if ( is_callable($function) && isset($meta_boxes[$key])) {
            $form += $function($meta_boxes[$key]);
            unset($meta_boxes[$key]);
        }
    }
    /**
     * grab missing meta-boxes
     */
    foreach( array_keys($meta_boxes) as $key ) {
        $function = sprintf('wpcf_admin_metabox_%s', $key);
        if ( is_callable($function) ) {
            $form += $function($meta_boxes[$key]);
        }
    }

    /**
     * filter wpcf_post_type_form
     */

    $form = $form + apply_filters( 'wpcf_post_type_form', array(), $ct );

    /**
     * container-2 close
     */
    $form['postbox-container-2-close'] = array(
        '#type' => 'markup',
        '#markup' => '</div></div>',
        '_builtin' => true,
    );

    $form['form-close'] = array(
        '#type' => 'markup',
        '#markup' => '</div></div>',
        '_builtin' => true,
    );

    /**
     * handle _builtin post types
     */
    if ( $ct['_builtin'] ) {

        foreach( $form as $key => $data ) {
            if ( !isset($data['#type'] ) ) {
                continue;
            }
            if ( isset($data['_builtin']) ) {
                switch( $data['#type'] ) {
                case 'textfield':
                case 'textarea':
                    $form[$key]['#attributes']['readonly'] = 'readonly';
                    break;
                default:
                }
                continue;
            }
            unset($form[$key]);
        }
    }

    /**
     * return form if current_user_can edit
     */
    if ( $current_user_can_edit) {
        return $form;
    }

    return wpcf_admin_common_only_show($form);
}

/**
 * Adds JS validation script.
 */
function wpcf_admin_types_form_js_validation()
{
    wpcf_form_render_js_validation();
}

/**
 * Submit function
 *
 * @global object $wpdb
 *
 */
function wpcf_admin_custom_types_form_submit($form)
{
    global $wpcf;

    if ( !isset( $_POST['ct'] ) ) {
        return false;
    }
    $data = $_POST['ct'];
    $update = false;

    // Sanitize data
    if ( isset( $data['wpcf-post-type'] ) ) {
        $update = true;
        $data['wpcf-post-type'] = sanitize_title( $data['wpcf-post-type'] );
    } else {
        $data['wpcf-post-type'] = null;
    }
    if ( isset( $data['slug'] ) ) {
        $data['slug'] = sanitize_title( $data['slug'] );
    } else {
        $data['slug'] = null;
    }
    if ( isset( $data['rewrite']['slug'] ) ) {
        $data['rewrite']['slug'] = remove_accents( $data['rewrite']['slug'] );
        $data['rewrite']['slug'] = strtolower( $data['rewrite']['slug'] );
        $data['rewrite']['slug'] = trim( $data['rewrite']['slug'] );
    }
    $data['_builtin'] = false;


    // Set post type name
    $post_type = null;
    if ( !empty( $data['slug'] ) ) {
        $post_type = $data['slug'];
    } elseif ( !empty( $data['wpcf-post-type'] ) ) {
        $post_type = $data['wpcf-post-type'];
    } elseif ( !empty( $data['labels']['singular_name'] ) ) {
        $post_type = sanitize_title( $data['labels']['singular_name'] );
    }

    if ( empty( $post_type ) ) {
        wpcf_admin_message( __( 'Please set post type name', 'wpcf' ), 'error' );
        return false;
    }

    $data['slug'] = $post_type;
    $custom_types = get_option( WPCF_OPTION_NAME_CUSTOM_TYPES, array() );
    $protected_data_check = array();

    if ( wpcf_is_builtin_post_types($data['slug']) ) {
        $data['_builtin'] = true;
    } else {
        // Check reserved name
        $reserved = wpcf_is_reserved_name( $post_type, 'post_type' );
        if ( is_wp_error( $reserved ) ) {
            wpcf_admin_message( $reserved->get_error_message(), 'error' );
            return false;
        }

        // Check overwriting
        if ( ( !array_key_exists( 'wpcf-post-type', $data ) || $data['wpcf-post-type'] != $post_type ) && array_key_exists( $post_type, $custom_types ) ) {
            wpcf_admin_message( __( 'Custom post type already exists', 'wpcf' ), 'error' );
            return false;
        }

        /*
         * Since Types 1.2
         * We do not allow plural and singular names to be same.
         */
        if ( $wpcf->post_types->check_singular_plural_match( $data ) ) {
            wpcf_admin_message( $wpcf->post_types->message( 'warning_singular_plural_match' ), 'error' );
            return false;
        }

        // Check if renaming then rename all post entries and delete old type
        if ( !empty( $data['wpcf-post-type'] )
            && $data['wpcf-post-type'] != $post_type ) {
                global $wpdb;
                $wpdb->update( $wpdb->posts, array('post_type' => $post_type),
                    array('post_type' => $data['wpcf-post-type']), array('%s'),
                    array('%s')
                );

                /**
                 * update post meta "_wp_types_group_post_types"
                 */
                $sql = $wpdb->prepare(
                    sprintf(
                        'select meta_id, meta_value from %s where meta_key = %%s',
                        $wpdb->postmeta
                    ),
                    '_wp_types_group_post_types'
                );
                $all_meta = $wpdb->get_results($sql, OBJECT_K);
                $re = sprintf( '/,%s,/', $data['wpcf-post-type'] );
                foreach( $all_meta as $meta ) {
                    if ( !preg_match( $re, $meta->meta_value ) ) {
                        continue;
                    }
                    $wpdb->update(
                        $wpdb->postmeta,
                        array(
                            'meta_value' => preg_replace( $re, ','.$post_type.',', $meta->meta_value ),
                        ),
                        array(
                            'meta_id' => $meta->meta_id,
                        ),
                        array( '%s' ),
                        array( '%d' )
                    );
                }

                /**
                 * update _wpcf_belongs_{$data['wpcf-post-type']}_id
                 */
                $wpdb->update(
                    $wpdb->postmeta,
                    array(
                        'meta_key' => sprintf( '_wpcf_belongs_%s_id', $post_type ),
                    ),
                    array(
                        'meta_key' => sprintf( '_wpcf_belongs_%s_id', $data['wpcf-post-type'] ),
                    ),
                    array( '%s' ),
                    array( '%s' )
                );

                /**
                 * update options "wpv_options"
                 */
                $wpv_options = get_option( 'wpv_options', true );
                if ( is_array( $wpv_options ) ) {
                    $re = sprintf( '/(views_template_(archive_)?for_)%s/', $data['wpcf-post-type'] );
                    foreach( $wpv_options as $key => $value ) {
                        if ( !preg_match( $re, $key ) ) {
                            continue;
                        }
                        unset($wpv_options[$key]);
                        $key = preg_replace( $re, "$1".$post_type, $key );
                        $wpv_options[$key] = $value;
                    }
                    update_option( 'wpv_options', $wpv_options );
                }

                /**
                 * update option "wpcf-custom-taxonomies"
                 */
                $wpcf_custom_taxonomies = get_option( WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, true );
                if ( is_array( $wpcf_custom_taxonomies ) ) {
                    $update_wpcf_custom_taxonomies = false;
                    foreach( $wpcf_custom_taxonomies as $key => $value ) {
                        if ( array_key_exists( 'supports', $value ) && array_key_exists( $data['wpcf-post-type'], $value['supports'] ) ) {
                            unset( $wpcf_custom_taxonomies[$key]['supports'][$data['wpcf-post-type']] );
                            $update_wpcf_custom_taxonomies = true;
                        }
                    }
                    if ( $update_wpcf_custom_taxonomies ) {
                        update_option( WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $wpcf_custom_taxonomies );
                    }
                }

                // Sync action
                do_action( 'wpcf_post_type_renamed', $post_type, $data['wpcf-post-type'] );

                // Set protected data
                $protected_data_check = $custom_types[$data['wpcf-post-type']];
                // Delete old type
                unset( $custom_types[$data['wpcf-post-type']] );
                $data['wpcf-post-type'] = $post_type;
            } else {
                // Set protected data
                $protected_data_check = !empty( $custom_types[$post_type] ) ? $custom_types[$post_type] : array();
            }

        // Check if active
        if ( isset( $custom_types[$post_type]['disabled'] ) ) {
            $data['disabled'] = $custom_types[$post_type]['disabled'];
        }
    }

    // Sync taxes with custom taxes
    if ( !empty( $data['taxonomies'] ) ) {
        $taxes = get_option( WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array() );
        foreach ( $taxes as $id => $tax ) {
            if ( array_key_exists( $id, $data['taxonomies'] ) ) {
                $taxes[$id]['supports'][$data['slug']] = 1;
            } else {
                unset( $taxes[$id]['supports'][$data['slug']] );
            }
        }
        update_option( WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $taxes );
    }

    // Preserve protected data
    foreach ( $protected_data_check as $key => $value ) {
        if ( strpos( $key, '_' ) !== 0 ) {
            unset( $protected_data_check[$key] );
        }
    }

    /**
     * set last edit time
     */
    $data[TOOLSET_EDIT_LAST] = time();

    /**
     * set last edit author
     */

    $data[WPCF_AUTHOR] = get_current_user_id();

    /**
     * add builid in
     */
    if ( $data['_builtin'] && !isset( $protected_data_check[$data['slug']])) {
        $protected_data_check[$data['slug']] = array();
    }

    // Merging protected data
    $custom_types[$post_type] = array_merge( $protected_data_check, $data );

    update_option( WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types );

    // WPML register strings
    if ( !$data['_builtin'] ) {
        wpcf_custom_types_register_translation( $post_type, $data );
    }

    /**
     * success message
     */
    $message = __( 'Custom post type saved', 'wpcf' );
    if ( $data['_builtin'] ) {
        $message = __( 'Post type saved', 'wpcf' );
    }
    wpcf_admin_message_store( apply_filters( 'types_message_custom_post_type_saved',$message, $data, $update ), 'custom');

    if ( !$data['_builtin'] ) {
        // Flush rewrite rules
        flush_rewrite_rules();

        do_action( 'wpcf_custom_types_save', $data );
    }

    // Redirect
    wp_safe_redirect(
        esc_url_raw(
            add_query_arg(
                array(
                    'page' => 'wpcf-edit-type',
                    'wpcf-post-type' => $post_type,
                    'wpcf-rewrite' => 1,
                    'wpcf-message' => 'view',
                ),
                admin_url( 'admin.php' )
            )
        )
    );
    die();
}

/**
 * components
 */

/**
 * save button
 */
function wpcf_admin_metabox_submitdiv($ct)
{
    $button_text = __( 'Save Custom Post Type', 'wpcf' );
    return wpcf_admin_common_metabox_save($ct, $button_text);
}

/**
 * Visibility
 */
function wpcf_admin_metabox_wpcf_visibility($ct)
{
    $form = array();
    $form['table-2-open'] = wpcf_admin_metabox_begin(__( 'Visibility', 'wpcf' ), 'wpcf_visibility', 'wpcf-types-form-visibility-table', false);
    $form['public'] = array(
        '#type' => 'radios',
        '#name' => 'ct[public]',
        '#options' => array(
            __( 'Make this type public (will appear in the WordPress Admin menu)', 'wpcf' ) => 'public',
            __( 'Hidden - users cannot directly edit data in this type', 'wpcf' ) => 'hidden',
        ),
        '#default_value' => (isset( $ct['public'] ) && strval( $ct['public'] ) == 'hidden') ? 'hidden' : 'public',
        '#inline' => true,
    );
    $hidden = (isset( $ct['public'] ) && strval( $ct['public'] ) == 'hidden') ? ' class="hidden"' : '';
    $form['menu_position'] = array(
        '#type' => 'textfield',
        '#name' => 'ct[menu_position]',
        '#title' => __( 'Menu position', 'wpcf' ),
        '#value' => isset( $ct['menu_position'] ) ? $ct['menu_position'] : '',
        '#validate' => array('number' => array('value' => true)),
        '#inline' => true,
        '#pattern' => '<BEFORE><p><LABEL><ELEMENT><ERROR></p><AFTER>',
        '#before' => '<div' . $hidden . ' id="wpcf-types-form-visiblity-toggle">',
    );
    $form['menu_icon'] = array(
        '#type' => 'textfield',
        '#name' => 'ct[menu_icon]',
        '#title' => __( 'Menu icon', 'wpcf' ),
        '#description' => __( 'The url to the icon to be used for this menu. Default: null - defaults to the posts icon.', 'wpcf' ),
        '#value' => isset( $ct['menu_icon'] ) ? $ct['menu_icon'] : '',
        '#inline' => true,
        '#pattern' => '<BEFORE><p><LABEL><ELEMENT><ERROR></p><AFTER>',
        '#after' => '</div>',
    );
    /**
     * dashboard glance option to show counters on admin dashbord widget
     */
    $form['dashboard_glance'] = array(
        '#type' => 'checkbox',
        '#before' => sprintf('<h4>%s</h4>', __( 'Show on "At a Glance"', 'wpcf' )),
        '#name' => 'ct[dashboard_glance]',
        '#title' => __( 'Show number of entries on "At a Glance" admin widget.', 'wpcf' ),
        '#default_value' => !empty( $ct['dashboard_glance'] ),
    );
    $form['table-2-close'] = wpcf_admin_metabox_end();
    return $form;
}

    /**
     * Taxonomies
     */
function wpcf_admin_metabox_taxonomies($ct)
{
    $form = array();
    $taxonomies = get_taxonomies( '', 'objects' );
    $options = array();

    foreach ( $taxonomies as $category_slug => $category ) {
        if ( $category_slug == 'nav_menu' || $category_slug == 'link_category'
                || $category_slug == 'post_format' ) {
            continue;
        }
        $options[$category_slug]['#name'] = 'ct[taxonomies][' . $category_slug . ']';
        $options[$category_slug]['#title'] = $category->labels->name;
        $options[$category_slug]['#default_value'] = !empty( $ct['taxonomies'][$category_slug] );
        $options[$category_slug]['#inline'] = true;
        $options[$category_slug]['#before'] = '<li>';
        $options[$category_slug]['#after'] = '</li>';
        if ( is_rtl() ) {
            $options[$category_slug]['#before'] = '<div style="float:right;margin-left:10px;">';
            $options[$category_slug]['#after'] .= '</div>';
        }
        $options[$category_slug]['_builtin'] = $category->_builtin;
        if ( $ct['_builtin'] && $category->_builtin ) {
            $options[$category_slug]['#attributes'] = array(
                 'disabled' => 'disabled',
            );
        }
    }

    $form['table-3-open'] = wpcf_admin_metabox_begin(__( 'Select Taxonomies', 'wpcf' ), 'taxonomies', 'wpcf-types-form-taxonomies-table', false, true, '_builtin');
    $form['taxonomies'] = array(
        '#type' => 'checkboxes',
        '#options' => $options,
        '#description' => __( 'Registered taxonomies that will be used with this post type.', 'wpcf' ),
        '#name' => 'ct[taxonomies]',
        '#inline' => true,
        '#before' => '<ul>',
        '#after' => '</ul>',
        '_builtin' => true,
    );
    $form['table-3-close'] = wpcf_admin_metabox_end(false, '_builtin');
    return $form;
}

    /**
     * Labels
     */
function wpcf_admin_metabox_labels($ct)
{
    $form = array();
    $form['table-4-open'] = wpcf_admin_metabox_begin(__( 'Labels', 'wpcf' ), 'labels', 'wpcf-types-form-table');
    $labels = array(
        'add_new' => array(
            'title' => __( 'Add New', 'wpcf' ),
            'description' => __( 'The add new text. The default is Add New for both hierarchical and non-hierarchical types.', 'wpcf' ),
            'label' => __('Add New', 'wpcf'),
        ),
        'add_new_item' => array(
            'title' => __( 'Add New %s', 'wpcf' ),
            'description' => __( 'The add new item text. Default is Add New Post/Add New Page.', 'wpcf' ),
            'label' => __('Add New Item', 'wpcf'),
        ),
        'edit_item' => array(
            'title' => __( 'Edit %s', 'wpcf' ),
            'description' => __( 'The edit item text. Default is Edit Post/Edit Page.', 'wpcf' ),
            'label' => __('Edit Item', 'wpcf'),
        ),
        'new_item' => array(
            'title' => __( 'New %s', 'wpcf' ),
            'description' => __( 'The new item text. Default is New Post/New Page.', 'wpcf' ),
            'label' => __('New Item', 'wpcf'),
        ),
        'view_item' => array(
            'title' => __( 'View %s', 'wpcf' ),
            'description' => __( 'The view item text. Default is View Post/View Page.', 'wpcf' ),
            'label' => __('View Item', 'wpcf'),
        ),
        'search_items' => array(
            'title' => __( 'Search %s', 'wpcf' ),
            'description' => __( 'The search items text. Default is Search Posts/Search Pages.', 'wpcf' ),
            'label' => __('Search Items', 'wpcf'),
        ),
        'not_found' => array(
            'title' => __( 'No %s found', 'wpcf' ),
            'description' => __( 'The not found text. Default is No posts found/No pages found.', 'wpcf' ),
            'label' => __('Not Found', 'wpcf'),
        ),
        'not_found_in_trash' => array(
            'title' => __( 'No %s found in Trash', 'wpcf' ),
            'description' => __( 'The not found in trash text. Default is No posts found in Trash/No pages found in Trash.', 'wpcf' ),
            'label' => __('Not Found In Trash', 'wpcf'),
        ),
        'parent_item_colon' => array(
            'title' => __( 'Parent text', 'wpcf' ),
            'description' => __( "The parent text. This string isn't used on non-hierarchical types. In hierarchical ones the default is Parent Page.", 'wpcf' ),
            'label' => __('Parent Description', 'wpcf'),
        ),
        'all_items' => array(
            'title' => __( 'All items', 'wpcf' ),
            'description' => __( 'The all items text used in the menu. Default is the Name label.', 'wpcf' ),
            'label' => __('All Items', 'wpcf'),
        ),
    );
    foreach ( $labels as $name => $data ) {
        $form['labels-' . $name] = array(
            '#type' => 'textfield',
            '#name' => 'ct[labels][' . $name . ']',
            '#title' => $data['label'],
            '#description' => $data['description'],
            '#value' => empty($ct['slug'])? $data['title']:(isset( $ct['labels'][$name] ) ? $ct['labels'][$name] : ''),
            '#inline' => true,
            '#pattern' => '<tr><td><LABEL></td><td><ELEMENT></td><td><DESCRIPTION></td>',
        );
    }
    $form['table-4-close'] = wpcf_admin_metabox_end();
    return $form;
}

    /**
     * custom post type properites
     */
function wpcf_admin_metabox_display_sections($ct)
{
    $form = array();
    $form['table-5-open'] = wpcf_admin_metabox_begin(__( 'Custom Post Properites', 'wpcf' ), 'display_sections', 'wpcf-types-form-supports-table');
    $options = array(
        'title' => array(
            '#name' => 'ct[supports][title]',
            '#default_value' => !empty( $ct['supports']['title'] ),
            '#title' => __( 'Title', 'wpcf' ),
            '#description' => __( 'Text input field to create a post title.', 'wpcf' ),
            '#inline' => true,
            '#id' => 'wpcf-supports-title',
        ),
        'editor' => array(
            '#name' => 'ct[supports][editor]',
            '#default_value' => !empty( $ct['supports']['editor'] ),
            '#title' => __( 'Editor', 'wpcf' ),
            '#description' => __( 'Content input box for writing.', 'wpcf' ),
            '#inline' => true,
            '#id' => 'wpcf-supports-editor',
        ),
        'comments' => array(
            '#name' => 'ct[supports][comments]',
            '#default_value' => !empty( $ct['supports']['comments'] ),
            '#title' => __( 'Comments', 'wpcf' ),
            '#description' => __( 'Ability to turn comments on/off.', 'wpcf' ),
            '#inline' => true,
        ),
        'trackbacks' => array(
            '#name' => 'ct[supports][trackbacks]',
            '#default_value' => !empty( $ct['supports']['trackbacks'] ),
            '#title' => __( 'Trackbacks', 'wpcf' ),
            '#description' => __( 'Ability to turn trackbacks and pingbacks on/off.', 'wpcf' ),
            '#inline' => true,
        ),
        'revisions' => array(
            '#name' => 'ct[supports][revisions]',
            '#default_value' => !empty( $ct['supports']['revisions'] ),
            '#title' => __( 'Revisions', 'wpcf' ),
            '#description' => __( 'Allows revisions to be made of your post.', 'wpcf' ),
            '#inline' => true,
        ),
        'author' => array(
            '#name' => 'ct[supports][author]',
            '#default_value' => !empty( $ct['supports']['author'] ),
            '#title' => __( 'Author', 'wpcf' ),
            '#description' => __( 'Displays a dropdown menu for changing the post author.', 'wpcf' ),
            '#inline' => true,
        ),
        'excerpt' => array(
            '#name' => 'ct[supports][excerpt]',
            '#default_value' => !empty( $ct['supports']['excerpt'] ),
            '#title' => __( 'Excerpt', 'wpcf' ),
            '#description' => __( 'A text area for writing a custom excerpt.', 'wpcf' ),
            '#inline' => true,
        ),
        'thumbnail' => array(
            '#name' => 'ct[supports][thumbnail]',
            '#default_value' => !empty( $ct['supports']['thumbnail'] ),
            '#title' => __( 'Thumbnail', 'wpcf' ),
            '#description' => __( 'Add a box for uploading a featured image.', 'wpcf' ),
            '#inline' => true,
        ),
        'custom-fields' => array(
            '#name' => 'ct[supports][custom-fields]',
            '#default_value' => !empty( $ct['supports']['custom-fields'] ),
            '#title' => __( 'custom-fields', 'wpcf' ),
            '#description' => __( 'Custom fields input area.', 'wpcf' ),
            '#inline' => true,
        ),
        'page-attributes' => array(
            '#name' => 'ct[supports][page-attributes]',
            '#default_value' => !empty( $ct['supports']['page-attributes'] ),
            '#title' => __( 'page-attributes', 'wpcf' ),
            '#description' => __( 'Menu order, hierarchical must be true to show Parent option', 'wpcf' ),
            '#inline' => true,
        ),
        'post-formats' => array(
            '#name' => 'ct[supports][post-formats]',
            '#default_value' => !empty( $ct['supports']['post-formats'] ),
            '#title' => __( 'post-formats', 'wpcf' ),
            '#description' => sprintf( __( 'Add post formats, see %sPost Formats%s', 'wpcf' ),
                    '<a href="http://codex.wordpress.org/Post_Formats" title="Post Formats" target="_blank">',
                    '</a>' ),
            '#inline' => true,
        ),
    );
    $form['supports'] = array(
        '#type' => 'checkboxes',
        '#options' => $options,
        '#name' => 'ct[supports]',
        '#inline' => true,
    );
    $form['table-5-close'] = wpcf_admin_metabox_end();
    return $form;
}

    /**
     * Options
     */
function wpcf_admin_metabox_options($ct)
{
    $form = array();
    $form['table-6-open'] = wpcf_admin_metabox_begin( __( 'Options', 'wpcf' ), 'options', 'wpcf-types-form-options-table');
    $form['rewrite-enabled'] = array(
        '#type' => 'checkbox',
        '#title' => __( 'Rewrite', 'wpcf' ),
        '#name' => 'ct[rewrite][enabled]',
        '#description' => __( 'Rewrite permalinks with this format. False to prevent rewrite. Default: true and use post type as slug.', 'wpcf' ),
        '#default_value' => !empty( $ct['rewrite']['enabled'] ),
        '#inline' => true,
    );
    $form['rewrite-custom'] = array(
        '#type' => 'radios',
        '#name' => 'ct[rewrite][custom]',
        '#options' => array(
            __( 'Use the normal WordPress URL logic', 'wpcf' ) => 'normal',
            __( 'Use a custom URL format', 'wpcf' ) => 'custom',
        ),
        '#default_value' => empty( $ct['rewrite']['custom'] ) || $ct['rewrite']['custom'] != 'custom' ? 'normal' : 'custom',
        '#inline' => true,
        '#after' => '<br />',
    );
    $hidden = empty( $ct['rewrite']['custom'] ) || $ct['rewrite']['custom'] != 'custom' ? ' class="hidden"' : '';
    $form['rewrite-slug'] = array(
        '#type' => 'textfield',
        '#name' => 'ct[rewrite][slug]',
        '#description' => __( 'Optional.', 'wpcf' ) . ' ' . __( "Prepend posts with this slug - defaults to post type's name.", 'wpcf' ),
        '#value' => isset( $ct['rewrite']['slug'] ) ? $ct['rewrite']['slug'] : '',
        '#inline' => true,
        '#before' => '<div id="wpcf-types-form-rewrite-toggle"' . $hidden . '>',
        '#after' => '</div>',
        '#validate' => array('rewriteslug' => array('value' => 'true')),
    );
    $form['rewrite-with_front'] = array(
        '#type' => 'checkbox',
        '#title' => __( 'Allow permalinks to be prepended with front base', 'wpcf' ),
        '#name' => 'ct[rewrite][with_front]',
        '#description' => __( 'Example: if your permalink structure is /blog/, then your links will be: false->/news/, true->/blog/news/.', 'wpcf' ) . ' ' . __( 'Defaults to true.', 'wpcf' ),
        '#default_value' => !empty( $ct['rewrite']['with_front'] ),
        '#inline' => true,
    );
    $form['rewrite-feeds'] = array(
        '#type' => 'checkbox',
        '#name' => 'ct[rewrite][feeds]',
        '#title' => __( 'Feeds', 'wpcf' ),
        '#description' => __( 'Defaults to has_archive value.', 'wpcf' ),
        '#default_value' => !empty( $ct['rewrite']['feeds'] ),
        '#value' => 1,
        '#inline' => true,
    );
    $form['rewrite-pages'] = array(
        '#type' => 'checkbox',
        '#name' => 'ct[rewrite][pages]',
        '#title' => __( 'Pages', 'wpcf' ),
        '#description' => __( 'Defaults to true.', 'wpcf' ),
        '#default_value' => !empty( $ct['rewrite']['pages'] ),
        '#value' => 1,
        '#inline' => true,
    );
    $show_in_menu_page = isset( $ct['show_in_menu_page'] ) ? $ct['show_in_menu_page'] : '';
    $hidden = !empty( $ct['show_in_menu'] ) ? '' : ' class="hidden"';

    $has_archive_slug = isset( $ct['has_archive_slug'] ) ? $ct['has_archive_slug'] : '';
    $has_archive_slug_show = empty( $ct['has_archive'] )? ' class="hidden"':'';

    $form['vars'] = array(
        '#type' => 'checkboxes',
        '#name' => 'ct[vars]',
        '#inline' => true,
        '#options' => array(
            'has_archive' => array(
                '#name' => 'ct[has_archive]',
                '#default_value' => !empty( $ct['has_archive'] ),
                '#title' => __( 'has_archive', 'wpcf' ),
                '#description' => __( 'Allow to have custom archive slug for CPT.', 'wpcf' ) . '<br />' . __( 'Default: not set.', 'wpcf' ),
                '#inline' => true,
                '#after' => '<div id="wpcf-types-form-has_archive-toggle"' . $has_archive_slug_show . '><input type="text" name="ct[has_archive_slug]" style="width:50%;" value="' . $has_archive_slug . '" /><div class="description wpcf-form-description wpcf-form-description-checkbox description-checkbox">' . __( 'Optional.', 'wpcf' ) . ' ' . __( 'Default is value of rewrite or CPT slug.', 'wpcf' ) . '</div></div>',
            ),
            'show_in_menu' => array(
                '#name' => 'ct[show_in_menu]',
                '#default_value' => !empty( $ct['show_in_menu'] ),
                '#title' => __( 'show_in_menu', 'wpcf' ),
                '#description' => __( 'Whether to show the post type in the admin menu and where to show that menu. Note that show_ui must be true.', 'wpcf' ) . '<br />' . __( 'Default: null.', 'wpcf' ),
                '#after' => '<div id="wpcf-types-form-showinmenu-toggle"' . $hidden . '><input type="text" name="ct[show_in_menu_page]" style="width:50%;" value="' . $show_in_menu_page . '" /><div class="description wpcf-form-description wpcf-form-description-checkbox description-checkbox">' . __( 'Optional.', 'wpcf' ) . ' ' . __( "Top level page like 'tools.php' or 'edit.php?post_type=page'", 'wpcf' ) . '</div></div>',
                '#inline' => true,
            ),
            'show_ui' => array(
                '#name' => 'ct[show_ui]',
                '#default_value' => !empty( $ct['show_ui'] ),
                '#title' => __( 'show_ui', 'wpcf' ),
                '#description' => __( 'Generate a default UI for managing this post type.', 'wpcf' ) . '<br />' . __( 'Default: value of public argument.', 'wpcf' ),
                '#inline' => true,
            ),
            'publicly_queryable' => array(
                '#name' => 'ct[publicly_queryable]',
                '#default_value' => !empty( $ct['publicly_queryable'] ),
                '#title' => __( 'publicly_queryable', 'wpcf' ),
                '#description' => __( 'Whether post_type queries can be performed from the front end.', 'wpcf' ) . '<br />' . __( 'Default: value of public argument.', 'wpcf' ),
                '#inline' => true,
            ),
            'exclude_from_search' => array(
                '#name' => 'ct[exclude_from_search]',
                '#default_value' => !empty( $ct['exclude_from_search'] ),
                '#title' => __( 'exclude_from_search', 'wpcf' ),
                '#description' => __( 'Whether to exclude posts with this post type from search results.', 'wpcf' ) . '<br />' . __( 'Default: value of the opposite of the public argument.', 'wpcf' ),
                '#inline' => true,
            ),
            'hierarchical' => array(
                '#name' => 'ct[hierarchical]',
                '#default_value' => !empty( $ct['hierarchical'] ),
                '#title' => __( 'hierarchical', 'wpcf' ),
                '#description' => __( 'Whether the post type is hierarchical. Allows Parent to be specified.', 'wpcf' ) . '<br />' . __( 'Default: false.', 'wpcf' ),
                '#inline' => true,
            ),
            'can_export' => array(
                '#name' => 'ct[can_export]',
                '#default_value' => !empty( $ct['can_export'] ),
                '#title' => __( 'can_export', 'wpcf' ),
                '#description' => __( 'Can this post_type be exported.', 'wpcf' ) . '<br />' . __( 'Default: true.', 'wpcf' ),
                '#inline' => true,
            ),
            'show_in_nav_menus' => array(
                '#name' => 'ct[show_in_nav_menus]',
                '#default_value' => !empty( $ct['show_in_nav_menus'] ),
                '#title' => __( 'show_in_nav_menus', 'wpcf' ),
                '#description' => __( 'Whether post_type is available for selection in navigation menus.', 'wpcf' ) . '<br />' . __( 'Default: value of public argument.', 'wpcf' ),
                '#inline' => true,
            ),
        ),
    );
    $query_var = isset( $ct['query_var'] ) ? $ct['query_var'] : '';
    $hidden = !empty( $ct['query_var_enabled'] ) ? '' : ' class="hidden"';
    $form['query_var'] = array(
        '#type' => 'checkbox',
        '#name' => 'ct[query_var_enabled]',
        '#title' => 'query_var',
        '#description' => __( 'False to prevent queries, or string value of the query var to use for this post type.', 'wpcf' ) . '<br />' . __( 'Default: true - set to $post_type.', 'wpcf' ),
        '#default_value' => !empty( $ct['query_var_enabled'] ),
        '#after' => '<div id="wpcf-types-form-queryvar-toggle"' . $hidden . '><input type="text" name="ct[query_var]" value="' . $query_var . '" style="width:50%;" /><div class="description wpcf-form-description wpcf-form-description-checkbox description-checkbox">' . __( 'Optional', 'wpcf' ) . '. ' . __( 'String to customize query var', 'wpcf' ) . '</div></div>',
        '#inline' => true,
    );
    $form['permalink_epmask'] = array(
        '#type' => 'textfield',
        '#name' => 'ct[permalink_epmask]',
        '#title' => __( 'Permalink epmask', 'wpcf' ),
        '#description' => sprintf( __( 'Default value EP_PERMALINK. More info here %s.', 'wpcf' ),
                '<a href="http://core.trac.wordpress.org/ticket/12605" target="_blank">link</a>' ),
        '#value' => isset( $ct['permalink_epmask'] ) ? $ct['permalink_epmask'] : '',
        '#inline' => true,
    );
    $form['table-6-close'] = wpcf_admin_metabox_end();
    return $form;
}
