/**
  * Summary.
  *
  * Description.
  *
  * @since x.x.x
  * @access (for functions: only use if private)
  *
  * @see Function/method/class relied on
  * @link URL
  * @global type $varname Description.
  * @global type $varname Description.
  *
  * @param type $var Description.
  * @param type $var Optional. Description.
  * @return type Description.
  */
 public function get_post_type($post_type_slug)
 {
     if (empty($post_type_slug)) {
         return wpcf_custom_types_default();
     }
     $post_type = array();
     $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array());
     if (isset($custom_types[$post_type_slug])) {
         $post_type = $custom_types[$post_type_slug];
         $post_type['update'] = true;
     } else {
         $buildin_post_types = wpcf_get_builtin_in_post_types();
         if (isset($buildin_post_types[$post_type_slug])) {
             $post_type = get_object_vars(get_post_type_object($post_type_slug));
             $post_type['labels'] = get_object_vars($post_type['labels']);
             $post_type['slug'] = esc_attr($post_type_slug);
             $post_type['_builtin'] = true;
         } else {
             return false;
         }
     }
     if (!isset($post_type['update'])) {
         $post_type['update'] = false;
     }
     return $post_type;
 }
 /** ************************************************************************
  * REQUIRED. Set up a constructor that references the parent constructor. We
  * use the parent reference to set some default configs.
  ***************************************************************************/
 function __construct()
 {
     //Set parent defaults
     parent::__construct(array('singular' => 'Post Type', 'plural' => 'Post Types', 'ajax' => true));
     $this->custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array());
     $this->buildin_post_types = wpcf_get_builtin_in_post_types();
 }
Example #3
0
function wpcf_is_builtin_post_types($post_type)
{
    $post_types = wpcf_get_builtin_in_post_types();
    return in_array($post_type, $post_types);
}
/**
 * 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);
}