Beispiel #1
0
/**
 * Add to or update our CPTUI option with new data.
 *
 * @since 1.0.0
 *
 * @internal
 *
 * @param array $data Array of taxonomy data to update.
 * @return bool|string False on failure, string on success.
 */
function cptui_update_taxonomy($data = array())
{
    /**
     * Fires before a taxonomy is updated to our saved options.
     *
     * @since 1.0.0
     *
     * @param array $data Array of taxonomy data we are updating.
     */
    do_action('cptui_before_update_taxonomy', $data);
    // They need to provide a name.
    if (empty($data['cpt_custom_tax']['name'])) {
        return cptui_admin_notices('error', '', false, __('Please provide a taxonomy name', 'custom-post-type-ui'));
    }
    if (empty($data['cpt_post_types'])) {
        return cptui_admin_notices('error', '', false, __('Please provide a post type to attach to.', 'custom-post-type-ui'));
    }
    if (!empty($data['tax_original']) && $data['tax_original'] != $data['cpt_custom_tax']['name']) {
        if (!empty($data['update_taxonomy'])) {
            cptui_convert_taxonomy_terms($data['tax_original'], $data['cpt_custom_tax']['name']);
        }
    }
    foreach ($data as $key => $value) {
        if (is_string($value)) {
            $data[$key] = sanitize_text_field($value);
        } else {
            array_map('sanitize_text_field', $data[$key]);
        }
    }
    if (false !== strpos($data['cpt_custom_tax']['name'], '\'') || false !== strpos($data['cpt_custom_tax']['name'], '\\"') || false !== strpos($data['cpt_custom_tax']['rewrite_slug'], '\'') || false !== strpos($data['cpt_custom_tax']['rewrite_slug'], '\\"')) {
        return cptui_admin_notices('error', '', false, __('Please do not use quotes in taxonomy names or rewrite slugs', 'custom-post-type-ui'));
    }
    $taxonomies = cptui_get_taxonomy_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_taxonomy_slug_exists', false, $data['cpt_custom_tax']['name'], $taxonomies);
    if ('new' == $data['cpt_tax_status']) {
        if (true === $slug_exists) {
            return cptui_admin_notices('error', '', false, sprintf(__('Please choose a different taxonomy name. %s is already registered.', 'custom-post-type-ui'), $data['cpt_custom_tax']['name']));
        }
    }
    foreach ($data['cpt_tax_labels'] as $key => $label) {
        if (empty($label)) {
            unset($data['cpt_tax_labels'][$key]);
        }
        $label = str_replace('"', '', htmlspecialchars_decode($label));
        $label = htmlspecialchars($label, ENT_QUOTES);
        $label = trim($label);
        $data['cpt_tax_labels'][$key] = stripslashes_deep($label);
    }
    $label = ucwords(str_replace('_', ' ', $data['cpt_custom_tax']['name']));
    if (!empty($data['cpt_custom_tax']['label'])) {
        $label = str_replace('"', '', htmlspecialchars_decode($data['cpt_custom_tax']['label']));
        $label = htmlspecialchars(stripslashes($label), ENT_QUOTES);
    }
    $name = trim($data['cpt_custom_tax']['name']);
    $singular_label = ucwords(str_replace('_', ' ', $data['cpt_custom_tax']['name']));
    if (!empty($data['cpt_custom_tax']['singular_label'])) {
        $singular_label = str_replace('"', '', htmlspecialchars_decode($data['cpt_custom_tax']['singular_label']));
        $singular_label = htmlspecialchars(stripslashes($singular_label));
    }
    $description = stripslashes_deep($data['cpt_custom_tax']['description']);
    $query_var_slug = trim($data['cpt_custom_tax']['query_var_slug']);
    $rewrite_slug = trim($data['cpt_custom_tax']['rewrite_slug']);
    $rest_base = trim($data['cpt_custom_tax']['rest_base']);
    $show_quickpanel_bulk = !empty($data['cpt_custom_tax']['show_in_quick_edit']) ? disp_boolean($data['cpt_custom_tax']['show_in_quick_edit']) : '';
    $taxonomies[$data['cpt_custom_tax']['name']] = array('name' => $name, 'label' => $label, 'singular_label' => $singular_label, 'description' => $description, 'public' => disp_boolean($data['cpt_custom_tax']['public']), 'hierarchical' => disp_boolean($data['cpt_custom_tax']['hierarchical']), 'show_ui' => disp_boolean($data['cpt_custom_tax']['show_ui']), 'query_var' => disp_boolean($data['cpt_custom_tax']['query_var']), 'query_var_slug' => $query_var_slug, 'rewrite' => disp_boolean($data['cpt_custom_tax']['rewrite']), 'rewrite_slug' => $rewrite_slug, 'rewrite_withfront' => $data['cpt_custom_tax']['rewrite_withfront'], 'rewrite_hierarchical' => $data['cpt_custom_tax']['rewrite_hierarchical'], 'show_admin_column' => disp_boolean($data['cpt_custom_tax']['show_admin_column']), 'show_in_rest' => disp_boolean($data['cpt_custom_tax']['show_in_rest']), 'show_in_quick_edit' => $show_quickpanel_bulk, 'rest_base' => $rest_base, 'labels' => $data['cpt_tax_labels']);
    $taxonomies[$data['cpt_custom_tax']['name']]['object_types'] = $data['cpt_post_types'];
    /**
     * Filters whether or not 3rd party options were saved successfully within taxonomy add/update.
     *
     * @since 1.3.0
     *
     * @param bool  $value      Whether or not someone else saved successfully. Default false.
     * @param array $taxonomies Array of our updated taxonomies data.
     * @param array $data       Array of submitted taxonomy to update.
     */
    if (false === ($success = apply_filters('cptui_taxonomy_update_save', false, $taxonomies, $data))) {
        $success = update_option('cptui_taxonomies', $taxonomies);
    }
    /**
     * Fires after a taxonomy is updated to our saved options.
     *
     * @since 1.0.0
     *
     * @param array $data Array of taxonomy data that was updated.
     */
    do_action('cptui_after_update_taxonomy', $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_tax_status']) {
            return cptui_admin_notices('add', $data['cpt_custom_tax']['name'], $success);
        }
    }
    return cptui_admin_notices('update', $data['cpt_custom_tax']['name'], true);
}
/**
 * Handle the conversion of taxonomy terms.
 *
 * This function came to be because we needed to convert AFTER registration.
 *
 * @since 1.4.3
 */
function cptui_do_convert_taxonomy_terms()
{
    /**
     * Whether or not to convert taxonomy terms.
     *
     * @since 1.4.3
     *
     * @param bool $value Whether or not to convert.
     */
    if (apply_filters('cptui_convert_taxonomy_terms', false)) {
        cptui_convert_taxonomy_terms(sanitize_text_field($_POST['tax_original']), sanitize_text_field($_POST['cpt_custom_tax']['name']));
    }
}