/**
 * Add to or update our CPTUI option with new data.
 *
 * @since 1.0.0
 *
 * @internal
 *
 * @param array $data Array of post type data to update.
 *
 * @return bool|string False on failure, string on success.
 */
function cptui_update_post_type($data = array())
{
    /**
     * Fires before a post_type is updated to our saved options.
     *
     * @since 1.0.0
     *
     * @param array $data Array of post_type data we are updating.
     */
    do_action('cptui_before_update_post_type', $data);
    // They need to provide a name.
    if (empty($data['cpt_custom_post_type']['name'])) {
        return cptui_admin_notices('error', '', false, __('Please provide a post type name', 'custom-post-type-ui'));
    }
    if (!empty($data['cpt_original']) && $data['cpt_original'] != $data['cpt_custom_post_type']['name']) {
        if (!empty($data['update_post_types'])) {
            cptui_convert_post_type_posts($data['cpt_original'], $data['cpt_custom_post_type']['name']);
        }
    }
    // clean up $_POST data.
    foreach ($data as $key => $value) {
        if (is_string($value)) {
            $data[$key] = sanitize_text_field($value);
        } else {
            array_map('sanitize_text_field', $data[$key]);
        }
    }
    // Check if they didn't put quotes in the name or rewrite slug.
    if (false !== strpos($data['cpt_custom_post_type']['name'], '\'') || false !== strpos($data['cpt_custom_post_type']['name'], '\\"') || false !== strpos($data['cpt_custom_post_type']['rewrite_slug'], '\'') || false !== strpos($data['cpt_custom_post_type']['rewrite_slug'], '\\"')) {
        return cptui_admin_notices('error', '', false, __('Please do not use quotes in post type names or rewrite slugs', 'custom-post-type-ui'));
    }
    $post_types = cptui_get_post_type_data();
    /**
     * Check if we already have a post type of that name.
     *
     * @since 1.3.0
     *
     * @param bool   $value Assume we have no conflict by default.
     * @param string $value Post type slug being saved.
     * @param array  $post_types Array of existing post types from CPTUI.
     */
    $slug_exists = apply_filters('cptui_post_type_slug_exists', false, $data['cpt_custom_post_type']['name'], $post_types);
    $slug_as_page = cptui_check_page_slugs($data['cpt_custom_post_type']['name']);
    if ('new' == $data['cpt_type_status']) {
        if (true === $slug_exists) {
            return cptui_admin_notices('error', '', false, sprintf(__('Please choose a different post type name. %s is already registered.', 'custom-post-type-ui'), $data['cpt_custom_post_type']['name']));
        }
        if (true === $slug_as_page) {
            return cptui_admin_notices('error', '', false, sprintf(__('Please choose a different post type name. %s matches an existing page slug, which can cause conflicts.', 'custom-post-type-ui'), $data['cpt_custom_post_type']['name']));
        }
    }
    if (empty($data['cpt_addon_taxes']) || !is_array($data['cpt_addon_taxes'])) {
        $data['cpt_addon_taxes'] = array();
    }
    if (empty($data['cpt_supports']) || !is_array($data['cpt_supports'])) {
        $data['cpt_supports'] = array();
    }
    foreach ($data['cpt_labels'] as $key => $label) {
        if (empty($label)) {
            unset($data['cpt_labels'][$key]);
        }
        $label = str_replace('"', '', htmlspecialchars_decode($label));
        $label = htmlspecialchars($label, ENT_QUOTES);
        $label = trim($label);
        $data['cpt_labels'][$key] = stripslashes_deep($label);
    }
    if (empty($data['cpt_custom_post_type']['menu_icon'])) {
        $data['cpt_custom_post_type']['menu_icon'] = null;
    }
    $label = ucwords(str_replace('_', ' ', $data['cpt_custom_post_type']['name']));
    if (!empty($data['cpt_custom_post_type']['label'])) {
        $label = str_replace('"', '', htmlspecialchars_decode($data['cpt_custom_post_type']['label']));
        $label = htmlspecialchars(stripslashes($label), ENT_QUOTES);
    }
    $singular_label = ucwords(str_replace('_', ' ', $data['cpt_custom_post_type']['name']));
    if (!empty($data['cpt_custom_post_type']['singular_label'])) {
        $singular_label = str_replace('"', '', htmlspecialchars_decode($data['cpt_custom_post_type']['singular_label']));
        $singular_label = htmlspecialchars(stripslashes($singular_label), ENT_QUOTES);
    }
    $name = trim($data['cpt_custom_post_type']['name']);
    $description = stripslashes_deep($data['cpt_custom_post_type']['description']);
    $rest_base = trim($data['cpt_custom_post_type']['rest_base']);
    $has_archive_string = trim($data['cpt_custom_post_type']['has_archive_string']);
    $capability_type = trim($data['cpt_custom_post_type']['capability_type']);
    $rewrite_slug = trim($data['cpt_custom_post_type']['rewrite_slug']);
    $query_var_slug = trim($data['cpt_custom_post_type']['query_var_slug']);
    $menu_position = trim($data['cpt_custom_post_type']['menu_position']);
    $show_in_menu_string = trim($data['cpt_custom_post_type']['show_in_menu_string']);
    $menu_icon = trim($data['cpt_custom_post_type']['menu_icon']);
    $custom_supports = trim($data['cpt_custom_post_type']['custom_supports']);
    $post_types[$data['cpt_custom_post_type']['name']] = array('name' => $name, 'label' => $label, 'singular_label' => $singular_label, 'description' => $description, 'public' => disp_boolean($data['cpt_custom_post_type']['public']), 'show_ui' => disp_boolean($data['cpt_custom_post_type']['show_ui']), 'show_in_nav_menus' => disp_boolean($data['cpt_custom_post_type']['show_in_nav_menus']), 'show_in_rest' => disp_boolean($data['cpt_custom_post_type']['show_in_rest']), 'rest_base' => $rest_base, 'has_archive' => disp_boolean($data['cpt_custom_post_type']['has_archive']), 'has_archive_string' => $has_archive_string, 'exclude_from_search' => disp_boolean($data['cpt_custom_post_type']['exclude_from_search']), 'capability_type' => $capability_type, 'hierarchical' => disp_boolean($data['cpt_custom_post_type']['hierarchical']), 'rewrite' => disp_boolean($data['cpt_custom_post_type']['rewrite']), 'rewrite_slug' => $rewrite_slug, 'rewrite_withfront' => disp_boolean($data['cpt_custom_post_type']['rewrite_withfront']), 'query_var' => disp_boolean($data['cpt_custom_post_type']['query_var']), 'query_var_slug' => $query_var_slug, 'menu_position' => $menu_position, 'show_in_menu' => disp_boolean($data['cpt_custom_post_type']['show_in_menu']), 'show_in_menu_string' => $show_in_menu_string, 'menu_icon' => $menu_icon, 'supports' => $data['cpt_supports'], 'taxonomies' => $data['cpt_addon_taxes'], 'labels' => $data['cpt_labels'], 'custom_supports' => $custom_supports);
    /**
     * Filters whether or not 3rd party options were saved successfully within post type add/update.
     *
     * @since 1.3.0
     *
     * @param bool  $value      Whether or not someone else saved successfully. Default false.
     * @param array $post_types Array of our updated post types data.
     * @param array $data       Array of submitted post type to update.
     */
    if (false === ($success = apply_filters('cptui_post_type_update_save', false, $post_types, $data))) {
        $success = update_option('cptui_post_types', $post_types);
    }
    /**
     * Fires after a post type is updated to our saved options.
     *
     * @since 1.0.0
     *
     * @param array $data Array of post type data that was updated.
     */
    do_action('cptui_after_update_post_type', $data);
    // Used to help flush rewrite rules on init.
    set_transient('cptui_flush_rewrite_rules', 'true', 5 * 60);
    if (isset($success)) {
        if ('new' == $data['cpt_type_status']) {
            return cptui_admin_notices('add', $data['cpt_custom_post_type']['name'], $success);
        }
    }
    return cptui_admin_notices('update', $data['cpt_custom_post_type']['name'], true);
}
示例#2
0
/**
 * Add to or update our CPTUI option with new data.
 *
 * @since 1.0.0
 *
 * @param array $data Array of post type data to update.
 *
 * @return bool|string False on failure, string on success.
 */
function cptui_update_post_type($data = array())
{
    /**
     * Fires before a post_type is updated to our saved options.
     *
     * @since 1.0.0
     *
     * @param array $data Array of post_type data we are updating.
     */
    do_action('cptui_before_update_post_type', $data);
    # They need to provide a name
    if (empty($data['cpt_custom_post_type']['name'])) {
        return cptui_admin_notices('error', '', false, __('Please provide a post type name', 'custom-post-type-ui'));
    }
    if (!empty($data['cpt_original']) && $data['cpt_original'] != $data['cpt_custom_post_type']['name']) {
        if (!empty($data['update_post_types'])) {
            cptui_convert_post_type_posts($data['cpt_original'], $data['cpt_custom_post_type']['name']);
        }
    }
    # clean up $_POST data
    foreach ($data as $key => $value) {
        if (is_string($value)) {
            $data[$key] = sanitize_text_field($value);
        } else {
            array_map('sanitize_text_field', $data[$key]);
        }
    }
    # Check if they didn't put quotes in the name or rewrite slug.
    if (false !== strpos($data['cpt_custom_post_type']['name'], '\'') || false !== strpos($data['cpt_custom_post_type']['name'], '\\"') || false !== strpos($data['cpt_custom_post_type']['rewrite_slug'], '\'') || false !== strpos($data['cpt_custom_post_type']['rewrite_slug'], '\\"')) {
        return cptui_admin_notices('error', '', false, __('Please do not use quotes in post type names or rewrite slugs', 'custom-post-type-ui'));
    }
    $post_types = get_option('cptui_post_types', array());
    # Check if we already have a post type of that name.
    if ('new' == $data['cpt_type_status'] && (array_key_exists(strtolower($data['cpt_custom_post_type']['name']), $post_types) || in_array($data['cpt_custom_post_type']['name'], cptui_reserved_post_types()))) {
        return cptui_admin_notices('error', '', false, sprintf(__('Please choose a different post type name. %s is already registered.', 'custom-post-type-ui'), $data['cpt_custom_post_type']['name']));
    }
    if (empty($data['cpt_addon_taxes']) || !is_array($data['cpt_addon_taxes'])) {
        $data['cpt_addon_taxes'] = array();
    }
    if (empty($data['cpt_supports']) || !is_array($data['cpt_supports'])) {
        $data['cpt_supports'] = array();
    }
    foreach ($data['cpt_labels'] as $key => $label) {
        if (empty($label)) {
            unset($data['cpt_labels'][$key]);
        }
        $label = str_replace('"', '', htmlspecialchars_decode($label));
        $label = htmlspecialchars($label, ENT_QUOTES);
        $label = trim($label);
        $data['cpt_labels'][$key] = stripslashes_deep($label);
    }
    if (empty($data['cpt_custom_post_type']['menu_icon'])) {
        $data['cpt_custom_post_type']['menu_icon'] = null;
    }
    $label = str_replace('"', '', htmlspecialchars_decode($data['cpt_custom_post_type']['label']));
    $label = htmlspecialchars(stripslashes($label), ENT_QUOTES);
    $singular_label = str_replace('"', '', htmlspecialchars_decode($data['cpt_custom_post_type']['singular_label']));
    $singular_label = htmlspecialchars(stripslashes($singular_label), ENT_QUOTES);
    $name = trim($data['cpt_custom_post_type']['name']);
    $description = stripslashes_deep($data['cpt_custom_post_type']['description']);
    $rest_base = trim($data['cpt_custom_post_type']['rest_base']);
    $has_archive_string = trim($data['cpt_custom_post_type']['has_archive_string']);
    $capability_type = trim($data['cpt_custom_post_type']['capability_type']);
    $rewrite_slug = trim($data['cpt_custom_post_type']['rewrite_slug']);
    $query_var_slug = trim($data['cpt_custom_post_type']['query_var_slug']);
    $menu_position = trim($data['cpt_custom_post_type']['menu_position']);
    $show_in_menu_string = trim($data['cpt_custom_post_type']['show_in_menu_string']);
    $menu_icon = trim($data['cpt_custom_post_type']['menu_icon']);
    $custom_supports = trim($data['cpt_custom_post_type']['custom_supports']);
    $post_types[$data['cpt_custom_post_type']['name']] = array('name' => $name, 'label' => $label, 'singular_label' => $singular_label, 'description' => $description, 'public' => disp_boolean($data['cpt_custom_post_type']['public']), 'show_ui' => disp_boolean($data['cpt_custom_post_type']['show_ui']), 'show_in_nav_menus' => disp_boolean($data['cpt_custom_post_type']['show_in_nav_menus']), 'show_in_rest' => disp_boolean($data['cpt_custom_post_type']['show_in_rest']), 'rest_base' => $rest_base, 'has_archive' => disp_boolean($data['cpt_custom_post_type']['has_archive']), 'has_archive_string' => $has_archive_string, 'exclude_from_search' => disp_boolean($data['cpt_custom_post_type']['exclude_from_search']), 'capability_type' => $capability_type, 'hierarchical' => disp_boolean($data['cpt_custom_post_type']['hierarchical']), 'rewrite' => disp_boolean($data['cpt_custom_post_type']['rewrite']), 'rewrite_slug' => $rewrite_slug, 'rewrite_withfront' => disp_boolean($data['cpt_custom_post_type']['rewrite_withfront']), 'query_var' => disp_boolean($data['cpt_custom_post_type']['query_var']), 'query_var_slug' => $query_var_slug, 'menu_position' => $menu_position, 'show_in_menu' => disp_boolean($data['cpt_custom_post_type']['show_in_menu']), 'show_in_menu_string' => $show_in_menu_string, 'menu_icon' => $menu_icon, 'supports' => $data['cpt_supports'], 'taxonomies' => $data['cpt_addon_taxes'], 'labels' => $data['cpt_labels'], 'custom_supports' => $custom_supports);
    $success = update_option('cptui_post_types', $post_types);
    /**
     * Fires after a post type is updated to our saved options.
     *
     * @since 1.0.0
     *
     * @param array $data Array of post type data that was updated.
     */
    do_action('cptui_after_update_post_type', $data);
    flush_rewrite_rules();
    if (isset($success)) {
        if ('new' == $data['cpt_type_status']) {
            return cptui_admin_notices('add', $data['cpt_custom_post_type']['name'], $success);
        }
    }
    return cptui_admin_notices('update', $data['cpt_custom_post_type']['name'], true);
}
示例#3
0
/**
 * Handle the conversion of post type posts.
 *
 * This function came to be because we needed to convert AFTER registration.
 *
 * @since 1.4.3
 */
function cptui_do_convert_post_type_posts()
{
    /**
     * Whether or not to convert post type posts.
     *
     * @since 1.4.3
     *
     * @param bool $value Whether or not to convert.
     */
    if (apply_filters('cptui_convert_post_type_posts', false)) {
        cptui_convert_post_type_posts(sanitize_text_field($_POST['cpt_original']), sanitize_text_field($_POST['cpt_custom_post_type']['name']));
    }
}