예제 #1
0
/**
 * Saves settings.
 * 
 * @param type $form 
 */
function wpcf_admin_settings_form_submit($form)
{
    $settings = wpcf_get_settings();
    $data = $_POST['wpcf_settings'];
    foreach ($settings as $setting => $value) {
        if (!isset($data[$setting])) {
            $settings[$setting] = 0;
        } else {
            $settings[$setting] = $data[$setting];
        }
    }
    update_option('wpcf_settings', $settings);
    // Credits
    require_once WPCF_EMBEDDED_INC_ABSPATH . '/footer-credit.php';
    $option = get_option('wpcf_footer_credit', array());
    if (!isset($option['message'])) {
        $data = wpcf_footer_credit_defaults();
        shuffle($data);
        $option['message'] = rand(0, count($data));
    }
    if (!isset($_POST['show_credits'])) {
        update_option('wpcf_footer_credit', array('active' => 0, 'message' => $option['message']));
    } else {
        update_option('wpcf_footer_credit', array('active' => 1, 'message' => $option['message']));
    }
    wpcf_admin_message_store(__('Settings saved', 'wpcf'));
}
예제 #2
0
/**
 * Changes field type.
 * Modified by Gen, 13.02.2013
 *
 * @param type $fields
 * @param type $type
 */
function wpcf_admin_custom_fields_change_type($fields, $type, $post_type = TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, $meta_name = 'wpcf-fields')
{
    if (!is_array($fields)) {
        $fields = array(strval($fields));
    }
    $fields = wpcf_types_cf_under_control('add', array('fields' => $fields, 'type' => $type), $post_type, $meta_name);
    /**
     * wpcf_filter_field_control_change_type_allowed_types_from
     *
     * Filter the field types that you can switch to, given a type field
     *
     * @param array		Valid targets for a given origin type
     * @param string		Field type to switch from
     *
     * @since 1.8.9
     */
    $allowed = array('audio' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'url', 'textarea', 'textfield', 'email', 'date', 'phone', 'file', 'image', 'numeric', 'audio', 'video', 'embed'), 'audio'), 'textfield' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'textfield', 'textarea', 'email', 'url', 'date', 'phone', 'file', 'image', 'numeric', 'audio', 'video', 'embed'), 'textfield'), 'textarea' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'textfield', 'textarea', 'email', 'url', 'date', 'phone', 'file', 'image', 'numeric', 'audio', 'video', 'embed'), 'textarea'), 'date' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'date', 'textarea', 'textfield', 'email', 'url', 'phone', 'file', 'image', 'numeric', 'audio', 'video', 'embed'), 'date'), 'email' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'email', 'textarea', 'textfield', 'date', 'url', 'phone', 'file', 'image', 'numeric', 'audio', 'video', 'embed'), 'email'), 'embed' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'url', 'textarea', 'textfield', 'email', 'date', 'phone', 'file', 'image', 'numeric', 'audio', 'video', 'embed'), 'embed'), 'file' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'file', 'textarea', 'textfield', 'email', 'url', 'phone', 'fdate', 'image', 'numeric', 'audio', 'video', 'embed'), 'file'), 'image' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'image', 'textarea', 'textfield', 'email', 'url', 'phone', 'file', 'idate', 'numeric', 'audio', 'video', 'embed'), 'image'), 'numeric' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'numeric', 'textarea', 'textfield', 'email', 'url', 'phone', 'file', 'image', 'date', 'audio', 'video', 'embed'), 'numeric'), 'phone' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'phone', 'textarea', 'textfield', 'email', 'url', 'date', 'file', 'image', 'numeric', 'audio', 'video', 'embed'), 'phone'), 'select' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'select', 'textarea', 'textfield', 'date', 'email', 'url', 'phone', 'file', 'image', 'numeric', 'audio', 'video', 'embed'), 'select'), 'skype' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'skype', 'textarea', 'textfield', 'date', 'email', 'url', 'phone', 'file', 'image', 'numeric', 'audio', 'video', 'embed'), 'skype'), 'url' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'url', 'textarea', 'textfield', 'email', 'date', 'phone', 'file', 'image', 'numeric', 'audio', 'video', 'embed'), 'url'), 'checkbox' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'checkbox', 'textarea', 'textfield', 'email', 'url', 'date', 'phone', 'file', 'image', 'numeric', 'audio', 'video', 'embed'), 'checkbox'), 'radio' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'radio', 'textarea', 'textfield', 'email', 'url', 'date', 'phone', 'file', 'image', 'numeric', 'audio', 'video', 'embed'), 'radio'), 'video' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'url', 'textarea', 'textfield', 'email', 'date', 'phone', 'file', 'image', 'numeric', 'audio', 'video', 'embed'), 'video'), 'wysiwyg' => apply_filters('wpcf_filter_field_control_change_type_allowed_types_from', array('wysiwyg', 'textarea'), 'wysiwyg'));
    /**
     * wpcf_filter_field_control_change_type_allowed_types
     *
     * Filter the pairs field type origin -> valid field type targets when using the fields control change field type feature
     *
     * @param array $allowed Valid correspondence between field types and target field types
     *
     * @since 1.8.9
     */
    $allowed = apply_filters('wpcf_filter_field_control_change_type_allowed_types', $allowed);
    $all_fields = wpcf_admin_fields_get_fields(false, false, false, $meta_name);
    foreach ($fields as $field_id) {
        if (!isset($all_fields[$field_id])) {
            continue;
        }
        $field = $all_fields[$field_id];
        if (!in_array($type, $allowed[$field['type']])) {
            wpcf_admin_message_store(sprintf(__('Field "%s" type was converted from %s to %s. You need to set some further settings in the group editor.', 'wpcf'), $field['name'], $field['type'], $type));
            $all_fields[$field_id]['data']['disabled_by_type'] = 1;
        } else {
            $all_fields[$field_id]['data']['disabled'] = 0;
            $all_fields[$field_id]['data']['disabled_by_type'] = 0;
        }
        if ($field['type'] == 'numeric' && isset($all_fields[$field_id]['data']['validate']['number'])) {
            unset($all_fields[$field_id]['data']['validate']['number']);
        } else {
            if ($type == 'numeric') {
                $all_fields[$field_id]['data']['validate'] = array('number' => array('active' => true, 'message' => __('Please enter numeric data', 'wpcf')));
            }
        }
        $all_fields[$field_id]['type'] = $type;
    }
    update_option($meta_name, $all_fields);
}
예제 #3
0
/**
 * Submitted Bulk actions.
 */
function wpcf_admin_custom_fields_control_bulk_actions($action = '')
{
    if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'custom_fields_control_bulk')) {
        return;
    }
    switch ($action) {
        case 'wpcf-change-type-bulk':
            if (true && isset($_POST['wpcf-id']) && isset($_POST['fields'])) {
                wpcf_admin_custom_fields_change_type($_POST['fields'], $_POST['wpcf-id'], TYPES_CUSTOM_FIELD_GROUP_CPT_NAME);
            }
            break;
        case 'wpcf-deactivate-bulk':
            $fields = wpcf_admin_fields_get_fields(false, true);
            foreach ($_POST['fields'] as $field_id) {
                $field_id = sanitize_text_field($field_id);
                if (isset($fields[$field_id])) {
                    $fields[$field_id]['data']['disabled'] = 1;
                    wpcf_admin_message_store(sprintf(__('Removed from Types control: %s', 'wpcf'), $fields[$field_id]['name']));
                }
            }
            wpcf_admin_fields_save_fields($fields);
            break;
        case 'wpcf-activate-bulk':
            $fields = wpcf_admin_fields_get_fields(false, true);
            $fields_bulk = wpcf_types_cf_under_control('add', array('fields' => $_POST['fields']));
            foreach ($fields_bulk as $field_id) {
                if (isset($fields[$field_id])) {
                    $fields[$field_id]['data']['disabled'] = 0;
                }
                wpcf_admin_message_store(sprintf(__('Added to Types control: %s', 'wpcf'), $field_id));
            }
            wpcf_admin_fields_save_fields($fields);
            break;
        case 'wpcf-delete-bulk':
            require_once WPCF_INC_ABSPATH . '/fields.php';
            $failed = array();
            $success = array();
            foreach ($_POST['fields'] as $field_id) {
                $field_id = sanitize_text_field($field_id);
                $response = wpcf_admin_fields_delete_field($field_id);
                if (!$response) {
                    $failed[] = str_replace('_' . md5('wpcf_not_controlled'), '', $field_id);
                } else {
                    $success[] = $field_id;
                }
            }
            if (!empty($success)) {
                wpcf_admin_message_store(sprintf(__('Fields %s have been deleted.', 'wpcf'), implode(', ', $success)));
            }
            if (!empty($failed)) {
                wpcf_admin_message_store(sprintf(__('Fields %s are not Types fields. Types wont delete these fields.', 'wpcf'), implode(', ', $failed)), 'error');
            }
            break;
        case 'wpcf-add-to-group-bulk':
        case 'wpcf-remove-from-group-bulk':
            if (true && isset($_POST['wpcf-id']) && isset($_POST['fields'])) {
                $fields = array_values((array) $_POST['fields']);
                if (!empty($fields)) {
                    $groups = explode(',', $_POST['wpcf-id']);
                    foreach ($groups as $group_id) {
                        switch ($action) {
                            case 'wpcf-add-to-group-bulk':
                                wpcf_admin_fields_save_group_fields($group_id, $fields, true);
                                break;
                            case 'wpcf-remove-from-group-bulk':
                                wpcf_admin_fields_remove_field_from_group_bulk($group_id, $fields);
                                break;
                        }
                    }
                }
            }
            break;
    }
    wp_safe_redirect(esc_url_raw(add_query_arg(array('page' => 'wpcf-custom-fields-control'), admin_url('admin.php'))));
    die;
}
/**
 * Submit function
 */
function wpcf_admin_custom_taxonomies_form_submit($form)
{
    if (!isset($_POST['ct'])) {
        return false;
    }
    $data = $_POST['ct'];
    $update = false;
    // Sanitize data
    if (isset($data['wpcf-tax'])) {
        $update = true;
        $data['wpcf-tax'] = sanitize_title($data['wpcf-tax']);
    }
    if (isset($data['slug'])) {
        $data['slug'] = sanitize_title($data['slug']);
    }
    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']);
    }
    // Set tax name
    $tax = '';
    if (!empty($data['slug'])) {
        $tax = $data['slug'];
    } else {
        if (!empty($data['wpcf-tax'])) {
            $tax = $data['wpcf-tax'];
        } else {
            if (!empty($data['labels']['singular_name'])) {
                $tax = sanitize_title($data['labels']['singular_name']);
            }
        }
    }
    if (empty($tax)) {
        wpcf_admin_message(__('Please set taxonomy name', 'wpcf'), 'error');
        return false;
    }
    if (empty($data['labels']['singular_name'])) {
        $data['labels']['singular_name'] = $tax;
    }
    $data['slug'] = $tax;
    $custom_taxonomies = get_option('wpcf-custom-taxonomies', array());
    // Check reserved name
    if (wpcf_is_reserved_name($tax)) {
        wpcf_admin_message(sprintf(__('The name %s is reserved in WordPress and cannot be used in custom taxonomies. Please use a different name.', 'wpcf'), $tax), 'error');
        return false;
    }
    // Check if exists
    if ($update && !array_key_exists($data['wpcf-tax'], $custom_taxonomies)) {
        wpcf_admin_message(__("Custom taxonomy do not exist", 'wpcf'), 'error');
        return false;
    }
    // Check overwriting
    if (!$update && array_key_exists($tax, $custom_taxonomies)) {
        wpcf_admin_message(__('Custom taxonomy already exists', 'wpcf'), 'error');
        return false;
    }
    // Check if our tax overwrites some tax outside
    $tax_exists = get_taxonomy($tax);
    if (!$update && !empty($tax_exists)) {
        wpcf_admin_message(__('Taxonomy already exists', 'wpcf'), 'error');
        return false;
    }
    // Check if renaming
    if ($update && $data['wpcf-tax'] != $tax) {
        global $wpdb;
        $wpdb->update($wpdb->term_taxonomy, array('taxonomy' => $tax), array('taxonomy' => $data['wpcf-tax']), array('%s'), array('%s'));
        // Delete old type
        unset($custom_taxonomies[$data['wpcf-tax']]);
    }
    // Check if active
    if (isset($custom_taxonomies[$tax]['disabled'])) {
        $data['disabled'] = $custom_taxonomies[$tax]['disabled'];
    }
    // Sync with post types
    if (!empty($data['supports'])) {
        $post_types = get_option('wpcf-custom-types', array());
        foreach ($post_types as $id => $type) {
            if (array_key_exists($id, $data['supports'])) {
                $post_types[$id]['taxonomies'][$data['slug']] = 1;
            } else {
                unset($post_types[$id]['taxonomies'][$data['slug']]);
            }
        }
        update_option('wpcf-custom-types', $post_types);
    }
    $custom_taxonomies[$tax] = $data;
    update_option('wpcf-custom-taxonomies', $custom_taxonomies);
    // WPML register strings
    wpcf_custom_taxonimies_register_translation($tax, $data);
    wpcf_admin_message_store(__('Custom taxonomy saved', 'wpcf'));
    // Flush rewrite rules
    flush_rewrite_rules();
    // Redirect
    wp_redirect(admin_url('admin.php?page=wpcf-edit-tax&wpcf-tax=' . $tax . '&wpcf-rewrite=1'));
    die;
}
예제 #5
0
/**
 * Important save_post hook.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @internal breakpoint
 * @param type $post_ID
 * @param type $post
 */
function wpcf_admin_post_save_post_hook($post_ID, $post)
{
    if (defined('WPTOOLSET_FORMS_VERSION')) {
        global $wpcf;
        $errors = false;
        // Do not save post if is type of
        if (in_array($post->post_type, array('revision', 'view', 'view-template', 'cred-form', 'nav_menu_item', 'mediapage'))) {
            return false;
        }
        $_post_wpcf = array();
        if (!empty($_POST['wpcf'])) {
            $_post_wpcf = $_POST['wpcf'];
        }
        /**
         * handle checkbox
         */
        if (array_key_exists('_wptoolset_checkbox', $_POST) && is_array($_POST['_wptoolset_checkbox'])) {
            foreach ($_POST['_wptoolset_checkbox'] as $key => $field_value) {
                $field_slug = preg_replace('/^wpcf\\-/', '', $key);
                if (array_key_exists($field_slug, $_post_wpcf)) {
                    continue;
                }
                $_post_wpcf[$field_slug] = false;
            }
        }
        if (count($_post_wpcf)) {
            /**
             * check some attachment to delete
             */
            $delete_attachments = apply_filters('wpcf_delete_attachments', true);
            $images_to_delete = array();
            if ($delete_attachments && isset($_POST['wpcf']) && array_key_exists('delete-image', $_POST['wpcf']) && is_array($_POST['wpcf']['delete-image'])) {
                foreach ($_POST['wpcf']['delete-image'] as $image) {
                    $images_to_delete[$image] = 1;
                }
            }
            foreach ($_post_wpcf as $field_slug => $field_value) {
                // Get field by slug
                $field = wpcf_fields_get_field_by_slug($field_slug);
                if (empty($field)) {
                    continue;
                }
                // Skip copied fields
                if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) {
                    continue;
                }
                $_field_value = !types_is_repetitive($field) ? array($field_value) : $field_value;
                // Set config
                $config = wptoolset_form_filter_types_field($field, $post_ID, $_post_wpcf);
                /**
                 * remove from images_to_delete if user add again
                 */
                if ($delete_attachments && 'image' == $config['type']) {
                    $images = $_field_value;
                    if (!is_array($images)) {
                        $images = array($images);
                    }
                    foreach ($images as $image) {
                        if (array_key_exists($image, $images_to_delete)) {
                            unset($images_to_delete[$image]);
                        }
                    }
                }
                foreach ($_field_value as $_k => $_val) {
                    // Check if valid
                    $validation = wptoolset_form_validate_field('post', $config, $_val);
                    $conditional = wptoolset_form_conditional_check($config);
                    $not_valid = is_wp_error($validation) || !$conditional;
                    if (is_wp_error($validation)) {
                        $errors = true;
                        $_errors = $validation->get_error_data();
                        $_msg = sprintf(__('Field "%s" not updated:', 'wpcf'), $field['name']);
                        wpcf_admin_message_store($_msg . ' ' . implode(', ', $_errors), 'error');
                    }
                    if ($not_valid) {
                        if (types_is_repetitive($field)) {
                            unset($field_value[$_k]);
                        } else {
                            break;
                        }
                    }
                }
                // Save field
                if (types_is_repetitive($field)) {
                    $wpcf->repeater->set($post_ID, $field);
                    $wpcf->repeater->save($field_value);
                } else {
                    if (!$not_valid) {
                        $wpcf->field->set($post_ID, $field);
                        $wpcf->field->save($field_value);
                    }
                }
                do_action('wpcf_post_field_saved', $post_ID, $field);
                // TODO Move to checkboxes
                if ($field['type'] == 'checkboxes') {
                    if (!empty($field['data']['options'])) {
                        $update_data = array();
                        foreach ($field['data']['options'] as $option_id => $option_data) {
                            if (!isset($_POST['wpcf'][$field['id']][$option_id])) {
                                if (isset($field['data']['save_empty']) && $field['data']['save_empty'] == 'yes') {
                                    $update_data[$option_id] = 0;
                                }
                            } else {
                                $update_data[$option_id] = $_POST['wpcf'][$field['id']][$option_id];
                            }
                        }
                        update_post_meta($post_ID, $field['meta_key'], $update_data);
                    }
                }
            }
            /**
             * delete images
             */
            if ($delete_attachments && !empty($images_to_delete)) {
                $args = array('post_parent' => $post_ID, 'posts_per_page' => -1);
                $children_array = get_children($args);
                foreach ($children_array as $child) {
                    if (!array_key_exists($child->guid, $images_to_delete)) {
                        continue;
                    }
                    wp_delete_attachment($child->ID);
                }
            }
        }
        if ($errors) {
            update_post_meta($post_ID, '__wpcf-invalid-fields', true);
        }
        do_action('wpcf_post_saved', $post_ID);
        return;
    }
    // OLD
    global $wpcf;
    // Basic cheks
    /*
     * Allow this hook to be triggered only if Types form is submitted
     */
    //    if ( !isset( $_POST['_wpcf_post_wpnonce'] ) || !wp_verify_nonce( $_POST['_wpcf_post_wpnonce'],
    //                    'update-' . $post->post_type . '_' . $post_ID ) ) {
    //        return false;
    //    }
    /*
     * Do not save post if is type of:
     * revision
     * attachment
     * wp-types-group
     * view
     * view-template
     * cred-form
     */
    if (in_array($post->post_type, $wpcf->excluded_post_types)) {
        return false;
    }
    /*
     *
     *
     * Get all groups connected to this $post
     */
    $groups = wpcf_admin_post_get_post_groups_fields($post);
    if (empty($groups)) {
        return false;
    }
    $all_fields = array();
    $_not_valid = array();
    $_error = false;
    /*
     *
     *
     * Loop over each group
     *
     * TODO Document this
     * Connect 'wpcf-invalid-fields' with all fields
     */
    foreach ($groups as $group) {
        if (isset($group['fields'])) {
            // Process fields
            $fields = wpcf_admin_post_process_fields($post, $group['fields'], true, false, 'validation');
            // Validate fields
            $form = wpcf_form_simple_validate($fields);
            $all_fields = $all_fields + $fields;
            // Collect all not valid fields
            if ($form->isError()) {
                $_error = true;
                // Set error only to true
                $_not_valid = array_merge($_not_valid, (array) $form->get_not_valid());
            }
        }
    }
    // Set fields
    foreach ($all_fields as $k => $v) {
        // only Types field
        if (empty($v['wpcf-id'])) {
            continue;
        }
        $_temp = new WPCF_Field();
        $_temp->set($wpcf->post, $v['wpcf-id']);
        $all_fields[$k]['_field'] = $_temp;
    }
    foreach ($_not_valid as $k => $v) {
        // only Types field
        if (empty($v['wpcf-id'])) {
            continue;
        }
        $_temp = new WPCF_Field();
        $_temp->set($wpcf->post, $v['wpcf-id']);
        $_not_valid[$k]['_field'] = $_temp;
    }
    /*
     *
     * Allow interaction here.
     * Conditional will set $error to false if field is conditional
     * and not submitted.
     */
    $error = apply_filters('wpcf_post_form_error', $_error, $_not_valid, $all_fields);
    $not_valid = apply_filters('wpcf_post_form_not_valid', $_not_valid, $_error, $all_fields);
    // Notify user about error
    if ($error) {
        wpcf_admin_message_store(__('Please check your input data', 'wpcf'), 'error');
    }
    /*
     * Save invalid elements so user can be informed after redirect.
     */
    if (!empty($not_valid)) {
        update_post_meta($post_ID, 'wpcf-invalid-fields', $not_valid);
    }
    /*
     *
     *
     *
     *
     * Save meta fields
     */
    if (!empty($_POST['wpcf'])) {
        foreach ($_POST['wpcf'] as $field_slug => $field_value) {
            // Get field by slug
            $field = wpcf_fields_get_field_by_slug($field_slug);
            if (empty($field)) {
                continue;
            }
            // Set field
            $wpcf->field->set($post_ID, $field);
            // Skip copied fields
            // CHECKPOINT
            if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) {
                continue;
            }
            // Don't save invalid
            // CHECKPOINT
            if (isset($not_valid[$field['slug']])) {
                continue;
            }
            /*
             *
             *
             * Saving fields
             * @since 1.2
             *
             * We changed way repetitive fields are saved.
             * On each save fields are rewritten and order is saved in
             * '_$slug-sort-order' meta field.
             */
            /*
             *
             * We marked fields as repetitive in POST['__wpcf_repetitive']
             * Without this check we won't save any.
             * @see WPCF_Repeater::get_fields_form()
             */
            if (isset($_POST['__wpcf_repetitive'][$wpcf->field->slug])) {
                /*
                 * Use here WPCF_Repeater class.
                 * WPCF_Repeater::set() - switches to current post
                 * WPCF_Repeater::save() - saves repetitive field
                 */
                $wpcf->repeater->set($post_ID, $field);
                $wpcf->repeater->save();
            } else {
                /*
                 * Use WPCF_Field::save()
                 */
                $wpcf->field->save();
            }
            do_action('wpcf_post_field_saved', $post_ID, $field);
        }
    }
    /*
     * Process checkboxes
     *
     * TODO Revise and remove
     * Since Types 1.1.5 we moved this check to embedded/includes/checkbox.php
     * checkbox.php added permanently to bootstrap.
     */
    foreach ($all_fields as $field) {
        if (!isset($field['#type'])) {
            continue;
        }
        //        if ( $field['#type'] == 'checkbox'
        //                && !isset( $_POST['wpcf'][$field['wpcf-slug']] ) ) {
        //            $field_data = wpcf_admin_fields_get_field( $field['wpcf-id'] );
        //            if ( isset( $field_data['data']['save_empty'] )
        //                    && $field_data['data']['save_empty'] == 'yes' ) {
        //                update_post_meta( $post_ID,
        //                        wpcf_types_get_meta_prefix( $field ) . $field['wpcf-slug'],
        //                        0 );
        //            } else {
        //                delete_post_meta( $post_ID,
        //                        wpcf_types_get_meta_prefix( $field ) . $field['wpcf-slug'] );
        //            }
        //        }
        if ($field['#type'] == 'checkboxes') {
            $field_data = wpcf_admin_fields_get_field($field['wpcf-id']);
            if (!empty($field_data['data']['options'])) {
                $update_data = array();
                foreach ($field_data['data']['options'] as $option_id => $option_data) {
                    if (!isset($_POST['wpcf'][$field['wpcf-slug']][$option_id])) {
                        if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') {
                            $update_data[$option_id] = 0;
                        }
                    } else {
                        $update_data[$option_id] = $_POST['wpcf'][$field['wpcf-slug']][$option_id];
                    }
                }
                update_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], $update_data);
            }
        }
    }
    do_action('wpcf_post_saved', $post_ID);
}
예제 #6
0
function wpcf_admin_userprofilesave_init($user_id)
{
    if (defined('WPTOOLSET_FORMS_VERSION')) {
        global $wpcf;
        $errors = false;
        /**
         * check checkbox type fields to delete or save empty if needed
         */
        $groups = wpcf_admin_usermeta_get_groups_fields();
        foreach ($groups as $group) {
            if (!array_key_exists('fields', $group) || empty($group['fields'])) {
                continue;
            }
            foreach ($group['fields'] as $field) {
                switch ($field['type']) {
                    case 'checkboxes':
                        if (!array_key_exists('wpcf', $_POST) || !array_key_exists($field['slug'], $_POST['wpcf'])) {
                            delete_user_meta($user_id, $field['meta_key']);
                        }
                        break;
                    case 'checkbox':
                        if (!array_key_exists('wpcf', $_POST) || !array_key_exists($field['slug'], $_POST['wpcf'])) {
                            if ('yes' == $field['data']['save_empty']) {
                                $_POST['wpcf'][$field['slug']] = 0;
                            } else {
                                delete_user_meta($user_id, $field['meta_key']);
                            }
                        }
                        break;
                }
            }
        }
        // Save meta fields
        if (!empty($_POST['wpcf'])) {
            foreach ($_POST['wpcf'] as $field_slug => $field_value) {
                // Get field by slug
                $field = wpcf_fields_get_field_by_slug($field_slug, 'wpcf-usermeta');
                if (empty($field)) {
                    continue;
                }
                // Skip copied fields
                if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) {
                    continue;
                }
                $_field_value = !types_is_repetitive($field) ? array($field_value) : $field_value;
                // Set config
                $config = wptoolset_form_filter_types_field($field, $user_id);
                foreach ($_field_value as $_k => $_val) {
                    // Check if valid
                    $valid = wptoolset_form_validate_field('your-profile', $config, $_val);
                    if (is_wp_error($valid)) {
                        $errors = true;
                        $_errors = $valid->get_error_data();
                        $_msg = sprintf(__('Field "%s" not updated:', 'wpcf'), $field['name']);
                        wpcf_admin_message_store($_msg . ' ' . implode(', ', $_errors), 'error');
                        if (types_is_repetitive($field)) {
                            unset($field_value[$_k]);
                        } else {
                            break;
                        }
                    }
                }
                // Save field
                if (types_is_repetitive($field)) {
                    $wpcf->usermeta_repeater->set($user_id, $field);
                    $wpcf->usermeta_repeater->save($field_value);
                } else {
                    $wpcf->usermeta_field->set($user_id, $field);
                    $wpcf->usermeta_field->usermeta_save($field_value);
                }
                do_action('wpcf_user_field_saved', $user_id, $field);
                // TODO Move to checkboxes
                if ($field['type'] == 'checkboxes') {
                    $field_data = wpcf_admin_fields_get_field($field['id'], false, false, false, 'wpcf-usermeta');
                    if (!empty($field_data['data']['options'])) {
                        $update_data = array();
                        foreach ($field_data['data']['options'] as $option_id => $option_data) {
                            if (!isset($_POST['wpcf'][$field['id']][$option_id])) {
                                if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') {
                                    $update_data[$option_id] = 0;
                                }
                            } else {
                                $update_data[$option_id] = $_POST['wpcf'][$field['id']][$option_id];
                            }
                        }
                        update_user_meta($user_id, $field['meta_key'], $update_data);
                    }
                }
            }
        }
        if ($errors) {
            update_post_meta($user_id, '__wpcf-invalid-fields', true);
        }
        do_action('wpcf_user_saved', $user_id);
        return;
    }
    global $wpcf;
    $all_fields = array();
    $_not_valid = array();
    $_error = false;
    $error = '';
    $groups = $groups = wpcf_admin_usermeta_get_groups_fields();
    if (empty($groups)) {
        return false;
    }
    foreach ($groups as $group) {
        // Process fields
        $fields = wpcf_admin_usermeta_process_fields($user_id, $group['fields'], true, false, 'validation');
        // Validate fields
        $form = wpcf_form_simple_validate($fields);
        $all_fields = $all_fields + $fields;
        // Collect all not valid fields
        if ($form->isError()) {
            $_error = true;
            // Set error only to true
            $_not_valid = array_merge($_not_valid, (array) $form->get_not_valid());
        }
    }
    // Set fields
    foreach ($all_fields as $k => $v) {
        // only Types field
        if (empty($v['wpcf-id'])) {
            continue;
        }
        $_temp = new WPCF_Usermeta_Field();
        $_temp->set($user_id, $v['wpcf-id']);
        $all_fields[$k]['_field'] = $_temp;
    }
    foreach ($_not_valid as $k => $v) {
        // only Types field
        if (empty($v['wpcf-id'])) {
            continue;
        }
        $_temp = new WPCF_Usermeta_Field();
        $_temp->set($user_id, $v['wpcf-id']);
        $_not_valid[$k]['_field'] = $_temp;
    }
    $not_valid = apply_filters('wpcf_post_form_not_valid', $_not_valid, $_error, $all_fields);
    // Notify user about error
    if ($error) {
        wpcf_admin_message_store(__('Please check your input data', 'wpcf'), 'error');
    }
    /*
     * Save invalid elements so user can be informed after redirect.
     */
    if (!empty($not_valid)) {
        update_user_meta($user_id, 'wpcf-invalid-fields', $not_valid);
    }
    if (!empty($_POST['wpcf'])) {
        foreach ($_POST['wpcf'] as $field_slug => $field_value) {
            $field = wpcf_fields_get_field_by_slug($field_slug, 'wpcf-usermeta');
            if (empty($field)) {
                continue;
            }
            $wpcf->usermeta_field->set($user_id, $field);
            if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) {
                continue;
            }
            if (isset($_POST['__wpcf_repetitive'][$wpcf->usermeta_field->slug])) {
                $wpcf->usermeta_repeater->set($user_id, $field);
                $wpcf->usermeta_repeater->save();
            } else {
                $wpcf->usermeta_field->usermeta_save();
            }
            do_action('wpcf_post_field_saved', '', $field);
        }
        //end foreach
    }
    //end if
    foreach ($all_fields as $field) {
        if (!isset($field['#type'])) {
            continue;
        }
        if ($field['#type'] == 'checkbox') {
            $field_data = wpcf_admin_fields_get_field($field['wpcf-id'], false, false, false, 'wpcf-usermeta');
            if (!isset($_POST['wpcf'][$field['wpcf-slug']])) {
                if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') {
                    update_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], 0);
                } else {
                    delete_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug']);
                }
            }
        }
        if ($field['#type'] == 'checkboxes') {
            $field_data = wpcf_admin_fields_get_field($field['wpcf-id'], false, false, false, 'wpcf-usermeta');
            if (!empty($field_data['data']['options'])) {
                $update_data = array();
                foreach ($field_data['data']['options'] as $option_id => $option_data) {
                    if (!isset($_POST['wpcf'][$field['wpcf-slug']][$option_id])) {
                        if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') {
                            $update_data[$option_id] = 0;
                        }
                    } else {
                        $update_data[$option_id] = $_POST['wpcf'][$field['wpcf-slug']][$option_id];
                    }
                }
                update_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], $update_data);
            }
        }
    }
}
예제 #7
0
/**
 * Submit function
 */
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']);
    }
    if (isset($data['slug'])) {
        $data['slug'] = sanitize_title($data['slug']);
    }
    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']);
    }
    // Set post type name
    $post_type = '';
    if (!empty($data['slug'])) {
        $post_type = $data['slug'];
    } else {
        if (!empty($data['wpcf-post-type'])) {
            $post_type = $data['wpcf-post-type'];
        } else {
            if (!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');
        //        $form->triggerError();
        return false;
    }
    $data['slug'] = $post_type;
    $custom_types = get_option('wpcf-custom-types', array());
    // Check reserved name
    $reserved = wpcf_is_reserved_name($post_type);
    if (is_wp_error($reserved)) {
        wpcf_admin_message($reserved->get_error_message(), 'error');
        return false;
    }
    // Check overwriting
    if (!$update && array_key_exists($post_type, $custom_types)) {
        wpcf_admin_message(__('Custom post type already exists', 'wpcf'), 'error');
        //            $form->triggerError();
        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'));
        // 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-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-custom-taxonomies', $taxes);
    }
    // Preserve protected data
    foreach ($protected_data_check as $key => $value) {
        if (strpos($key, '_') !== 0) {
            unset($protected_data_check[$key]);
        }
    }
    // Merging protected data
    $custom_types[$post_type] = array_merge($protected_data_check, $data);
    update_option('wpcf-custom-types', $custom_types);
    // WPML register strings
    wpcf_custom_types_register_translation($post_type, $data);
    wpcf_admin_message_store(apply_filters('types_message_custom_post_type_saved', __('Custom post type saved', 'wpcf'), $data, $update), 'custom');
    // Flush rewrite rules
    flush_rewrite_rules();
    do_action('wpcf_custom_types_save', $data);
    // Redirect
    wp_redirect(admin_url('admin.php?page=wpcf-edit-type&wpcf-post-type=' . $post_type . '&wpcf-rewrite=1'));
    die;
}
 /**
  * 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.
  */
 private function save()
 {
     global $wpcf;
     if (!isset($_POST['ct'])) {
         return false;
     }
     $data = $_POST['ct'];
     $update = false;
     // Sanitize data
     if (isset($data[$this->get_id])) {
         $update = true;
         $data[$this->get_id] = sanitize_title($data[$this->get_id]);
     } else {
         $data[$this->get_id] = null;
     }
     if (isset($data['slug'])) {
         $data['slug'] = sanitize_title($data['slug']);
     } elseif ($_GET['wpcf-post-type'] == 'post' || $_GET['wpcf-post-type'] == 'page' || $_GET['wpcf-post-type'] == 'attachment') {
         $data['slug'] = $_GET['wpcf-post-type'];
     } 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[$this->get_id])) {
         $post_type = $data[$this->get_id];
     } 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;
         $update = 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($this->get_id, $data) || $data[$this->get_id] != $post_type) && array_key_exists($post_type, $custom_types)) {
             wpcf_admin_message(__('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[$this->get_id]) && $data[$this->get_id] != $post_type) {
             global $wpdb;
             $wpdb->update($wpdb->posts, array('post_type' => $post_type), array('post_type' => $data[$this->get_id]), 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[$this->get_id]);
             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[$this->get_id]}_id
              */
             $wpdb->update($wpdb->postmeta, array('meta_key' => sprintf('_wpcf_belongs_%s_id', $post_type)), array('meta_key' => sprintf('_wpcf_belongs_%s_id', $data[$this->get_id])), 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[$this->get_id]);
                 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[$this->get_id], $value['supports'])) {
                         unset($wpcf_custom_taxonomies[$key]['supports'][$data[$this->get_id]]);
                         $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[$this->get_id]);
             // Set protected data
             $protected_data_check = $custom_types[$data[$this->get_id]];
             // Delete old type
             unset($custom_types[$data[$this->get_id]]);
             $data[$this->get_id] = $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
     $taxes = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array());
     foreach ($taxes as $id => $tax) {
         if (isset($data['taxonomies']) && !empty($data['taxonomies']) && array_key_exists($id, $data['taxonomies'])) {
             $taxes[$id]['supports'][$data['slug']] = 1;
         } else {
             if (isset($taxes[$id]['supports'][$data['slug']])) {
                 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]);
         }
     }
     /**
      * save custom field group
      */
     /* removed types-608
        $post_to_groups = isset($_POST['ct']['custom-field-group'])?$_POST['ct']['custom-field-group']:array();
        $groups = $this->fields->get_groups_with_post_types();
        foreach( $groups as $group) {
            $post_types_to_save = $group['_wp_types_group_post_types'];
            // save
            if ( array_key_exists($group['id'], $post_to_groups)) {
                $post_types_to_save[] = $data['slug'];
            } else {
                if(($key = array_search($data['slug'], $post_types_to_save)) !== false) {
                    unset($post_types_to_save[$key]);
                }
                if (
                    false
                    || empty($post_types_to_save)
                    || (
                        true
                        && 1 == sizeof($post_types_to_save)
                        && 'all' == current($post_types_to_save)
                    )
                ) {
                    $post_types_to_save = array();
                    foreach( get_post_types() as $key => $value ) {
                        if ( $data['slug'] == $value) {
                            continue;
                        }
                        if ( in_array($value, $wpcf->excluded_post_types) ) {
                            continue;
                        }
                        $post_types_to_save[] = $value;
                    }
                }
            }
            wpcf_admin_fields_save_group_post_types($group['id'], $post_types_to_save);
        }
        */
     /**
      * 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
     $msg = $update ? __('Post Type saved.', 'wpcf') : __('New Post Type created.', 'wpcf');
     wpcf_admin_message_store($msg, 'updated notice notice-success is-dismissible');
     flush_rewrite_rules();
     if (!$data['_builtin']) {
         do_action('wpcf_custom_types_save', $data);
     }
     // Redirect
     wp_safe_redirect(esc_url_raw(add_query_arg(array('page' => 'wpcf-edit-type', $this->get_id => $post_type, 'wpcf-message' => 'view', 'flush' => '1'), admin_url('admin.php'))));
     die;
 }
/**
 * Submit function
 */
function wpcf_admin_custom_types_form_submit($form)
{
    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']);
    }
    if (isset($data['slug'])) {
        $data['slug'] = sanitize_title($data['slug']);
    }
    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']);
    }
    // Set post type name
    $post_type = '';
    if (!empty($data['slug'])) {
        $post_type = $data['slug'];
    } else {
        if (!empty($data['wpcf-post-type'])) {
            $post_type = $data['wpcf-post-type'];
        } else {
            if (!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');
        //        $form->triggerError();
        return false;
    }
    $data['slug'] = $post_type;
    $custom_types = get_option('wpcf-custom-types', array());
    // Check overwriting
    if (!$update && array_key_exists($post_type, $custom_types)) {
        wpcf_admin_message(__('Custom post type already exists', 'wpcf'), 'error');
        //            $form->triggerError();
        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'));
        // Delete old type
        unset($custom_types[$data['wpcf-post-type']]);
    }
    // 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-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-custom-taxonomies', $taxes);
    }
    $custom_types[$post_type] = $data;
    update_option('wpcf-custom-types', $custom_types);
    wpcf_admin_message_store(__('Custom post type saved', 'wpcf'));
    // Flush rewrite rules
    flush_rewrite_rules();
    // Redirect
    wp_redirect(admin_url('admin.php?page=wpcf-edit-type&wpcf-post-type=' . $post_type . '&wpcf-rewrite=1'));
    die;
}
예제 #10
0
/**
 * Saves fields and groups.
 *
 * If field name is changed in specific group - new one will be created,
 * otherwise old one will be updated and will appear in that way in other grups.
 *
 * @return type
 */
function wpcf_admin_save_fields_groups_submit($form)
{
    if (!isset($_POST['wpcf']) || !isset($_POST['wpcf']['group']) || !isset($_POST['wpcf']['group']['name'])) {
        return false;
    }
    // @todo maybe sanitize_text_field this too
    $_POST['wpcf']['group']['name'] = trim(strip_tags($_POST['wpcf']['group']['name']));
    $_POST['wpcf']['group'] = apply_filters('wpcf_group_pre_save', $_POST['wpcf']['group']);
    if (empty($_POST['wpcf']['group']['name'])) {
        $form->triggerError();
        wpcf_admin_message(__('Group name can not be empty.', 'wpcf'), 'error');
        return $form;
    }
    $new_group = false;
    $group_slug = $_POST['wpcf']['group']['slug'] = sanitize_title($_POST['wpcf']['group']['name']);
    // Basic check
    if (isset($_REQUEST['group_id'])) {
        // Check if group exists
        $post = get_post(intval($_REQUEST['group_id']));
        // Name changed
        if (strtolower($_POST['wpcf']['group']['name']) != strtolower($post->post_title)) {
            // Check if already exists
            $exists = get_page_by_title($_POST['wpcf']['group']['name'], 'OBJECT', 'wp-types-group');
            if (!empty($exists)) {
                $form->triggerError();
                wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), htmlspecialchars($_POST['wpcf']['group']['name'])), 'error');
                return $form;
            }
        }
        if (empty($post) || $post->post_type != 'wp-types-group') {
            $form->triggerError();
            wpcf_admin_message(sprintf(__("Wrong group ID %d", 'wpcf'), intval($_REQUEST['group_id'])), 'error');
            return $form;
        }
        $group_id = $post->ID;
    } else {
        $new_group = true;
        // Check if already exists
        $exists = get_page_by_title($_POST['wpcf']['group']['name'], 'OBJECT', 'wp-types-group');
        if (!empty($exists)) {
            $form->triggerError();
            wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), htmlspecialchars($_POST['wpcf']['group']['name'])), 'error');
            return $form;
        }
    }
    // Save fields for future use
    $fields = array();
    if (!empty($_POST['wpcf']['fields'])) {
        // Before anything - search unallowed characters
        foreach ($_POST['wpcf']['fields'] as $key => $field) {
            if (empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['name']) || !empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['slug'])) {
                $form->triggerError();
                wpcf_admin_message(sprintf(__('Field slugs cannot contain non-English characters. Please edit this field name %s and save again.', 'wpcf'), $field['name']), 'error');
                return $form;
            }
            if (!empty($field['name']) && is_numeric($field['name']) || !empty($field['slug']) && is_numeric($field['slug'])) {
                $form->triggerError();
                wpcf_admin_message(sprintf(__('Field names or slugs cannot contain only numbers.', 'wpcf'), $field['name']), 'error');
                return $form;
            }
        }
        // First check all fields
        foreach ($_POST['wpcf']['fields'] as $key => $field) {
            $field = apply_filters('wpcf_field_pre_save', $field);
            if (!empty($field['is_new'])) {
                // Check name and slug
                if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']))) {
                    $form->triggerError();
                    wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error');
                    return $form;
                }
                if (isset($field['slug']) && wpcf_types_cf_under_control('check_exists', sanitize_title($field['slug']))) {
                    $form->triggerError();
                    wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error');
                    return $form;
                }
            }
            // Field ID and slug are same thing
            $field_id = wpcf_admin_fields_save_field($field);
            if (is_wp_error($field_id)) {
                $form->triggerError();
                wpcf_admin_message($field_id->get_error_message(), 'error');
                return $form;
            }
            if (!empty($field_id)) {
                $fields[] = $field_id;
            }
            // WPML
            if (function_exists('wpml_cf_translation_preferences_store')) {
                $wpml_save_cf = wpml_cf_translation_preferences_store($key, wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_id)) . $field_id);
            }
        }
    }
    // Save group
    $post_types = isset($_POST['wpcf']['group']['supports']) ? $_POST['wpcf']['group']['supports'] : array();
    $taxonomies_post = isset($_POST['wpcf']['group']['taxonomies']) ? $_POST['wpcf']['group']['taxonomies'] : array();
    $admin_style = $_POST['wpcf']['group']['admin_styles'];
    $terms = array();
    foreach ($taxonomies_post as $taxonomy) {
        foreach ($taxonomy as $tax => $term) {
            $terms[] = $term;
        }
    }
    // Rename if needed
    if (isset($_REQUEST['group_id'])) {
        $_POST['wpcf']['group']['id'] = intval($_REQUEST['group_id']);
    }
    $group_id = wpcf_admin_fields_save_group($_POST['wpcf']['group']);
    $_POST['wpcf']['group']['id'] = $group_id;
    // Set open fieldsets
    if ($new_group && !empty($group_id)) {
        $open_fieldsets = get_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', true);
        if (isset($open_fieldsets[-1])) {
            $open_fieldsets[$group_id] = $open_fieldsets[-1];
            unset($open_fieldsets[-1]);
            update_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', $open_fieldsets);
        }
    }
    // Rest of processes
    if (!empty($group_id)) {
        wpcf_admin_fields_save_group_fields($group_id, $fields);
        wpcf_admin_fields_save_group_post_types($group_id, $post_types);
        wpcf_admin_fields_save_group_terms($group_id, $terms);
        wpcf_admin_fields_save_group_admin_styles($group_id, $admin_style);
        if (empty($_POST['wpcf']['group']['templates'])) {
            $_POST['wpcf']['group']['templates'] = array();
        }
        wpcf_admin_fields_save_group_templates($group_id, $_POST['wpcf']['group']['templates']);
        $_POST['wpcf']['group']['fields'] = isset($_POST['wpcf']['fields']) ? $_POST['wpcf']['fields'] : array();
        do_action('wpcf_save_group', $_POST['wpcf']['group']);
        wpcf_admin_message_store(apply_filters('types_message_custom_fields_saved', __('Group saved', 'wpcf'), $_POST['wpcf']['group']['name'], $new_group ? false : true), 'custom');
        wp_redirect(admin_url('admin.php?page=wpcf-edit&group_id=' . $group_id));
        die;
    } else {
        wpcf_admin_message_store(__('Error saving group', 'wpcf'), 'error');
    }
}
예제 #11
0
/**
 * save_post hook.
 * 
 * @param type $post_ID
 * @param type $post 
 */
function wpcf_admin_post_save_post_hook($post_ID, $post)
{
    // TODO Check if this prevents saving from outside of post form
    if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'update-' . $post->post_type . '_' . $post_ID)) {
        return false;
    }
    if (!in_array($post->post_type, array('revision', 'attachment', 'wp-types-group', 'view', 'view-template'))) {
        // Get groups
        $groups = wpcf_admin_post_get_post_groups_fields($post);
        if (empty($groups)) {
            return false;
        }
        $all_fields = array();
        foreach ($groups as $group) {
            // Process fields
            $fields = wpcf_admin_post_process_fields($post, $group['fields'], true);
            // Validate fields
            $form = wpcf_form_simple_validate($fields);
            $all_fields = $all_fields + $fields;
            $error = $form->isError();
            // Trigger form error
            if ($error) {
                wpcf_admin_message_store(__('Please check your input data', 'wpcf'), 'error');
            }
        }
        // Save invalid elements so user can be informed after redirect
        if (!empty($all_fields)) {
            update_post_meta($post_ID, 'wpcf-invalid-fields', $all_fields);
        }
        // Save meta fields
        if (!empty($_POST['wpcf'])) {
            foreach ($_POST['wpcf'] as $field_slug => $field_value) {
                // Don't save invalid
                if (isset($all_fields[wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_slug)) . $field_slug]) && isset($all_fields[wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_slug)) . $field_slug]['#error'])) {
                    continue;
                }
                // Get field by slug
                $field = wpcf_fields_get_field_by_slug($field_slug);
                if (!empty($field)) {
                    // Apply filters
                    $field_value = apply_filters('wpcf_fields_value_save', $field_value, $field['type'], $field_slug);
                    $field_value = apply_filters('wpcf_fields_slug_' . $field_slug . '_value_save', $field_value);
                    $field_value = apply_filters('wpcf_fields_type_' . $field['type'] . '_value_save', $field_value);
                    // Save field
                    update_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field_slug, $field_value);
                    do_action('wpcf_fields_slug_' . $field_slug . '_save', $field_value);
                    do_action('wpcf_fields_type_' . $field['type'] . '_save', $field_value);
                }
            }
        }
        // Process checkboxes
        foreach ($all_fields as $field) {
            if ($field['#type'] == 'checkbox' && !isset($_POST['wpcf'][$field['wpcf-slug']])) {
                delete_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug']);
            }
        }
    }
}
예제 #12
0
/**
 * Submit function
 *
 * @global object $wpdb
 *
 */
function wpcf_admin_custom_taxonomies_form_submit($form)
{
    if (!isset($_POST['ct'])) {
        return false;
    }
    $data = $_POST['ct'];
    $update = false;
    // Sanitize data
    if (isset($data['wpcf-tax'])) {
        $update = true;
        $data['wpcf-tax'] = sanitize_title($data['wpcf-tax']);
    }
    if (isset($data['slug'])) {
        $data['slug'] = sanitize_title($data['slug']);
    }
    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']);
    }
    // Set tax name
    $tax = '';
    if (!empty($data['slug'])) {
        $tax = $data['slug'];
    } else {
        if (!empty($data['wpcf-tax'])) {
            $tax = $data['wpcf-tax'];
        } else {
            if (!empty($data['labels']['singular_name'])) {
                $tax = sanitize_title($data['labels']['singular_name']);
            }
        }
    }
    if (empty($tax)) {
        wpcf_admin_message(__('Please set taxonomy name', 'wpcf'), 'error');
        return false;
    }
    if (empty($data['labels']['singular_name'])) {
        $data['labels']['singular_name'] = $tax;
    }
    $data['slug'] = $tax;
    $custom_taxonomies = get_option('wpcf-custom-taxonomies', array());
    // Check reserved name
    $reserved = wpcf_is_reserved_name($tax, 'taxonomy');
    if (is_wp_error($reserved)) {
        wpcf_admin_message($reserved->get_error_message(), 'error');
        return false;
    }
    // Check if exists
    if ($update && !array_key_exists($data['wpcf-tax'], $custom_taxonomies)) {
        wpcf_admin_message(__("Custom taxonomy do not exist", 'wpcf'), 'error');
        return false;
    }
    // Check overwriting
    if (!$update && array_key_exists($tax, $custom_taxonomies)) {
        wpcf_admin_message(__('Custom taxonomy already exists', 'wpcf'), 'error');
        return false;
    }
    // Check if our tax overwrites some tax outside
    $tax_exists = get_taxonomy($tax);
    if (!$update && !empty($tax_exists)) {
        wpcf_admin_message(__('Taxonomy already exists', 'wpcf'), 'error');
        return false;
    }
    // Check if renaming
    if ($update && $data['wpcf-tax'] != $tax) {
        global $wpdb;
        $wpdb->update($wpdb->term_taxonomy, array('taxonomy' => $tax), array('taxonomy' => $data['wpcf-tax']), array('%s'), array('%s'));
        // Sync action
        do_action('wpcf_taxonomy_renamed', $tax, $data['wpcf-tax']);
        // Delete old type
        unset($custom_taxonomies[$data['wpcf-tax']]);
    }
    // Check if active
    if (isset($custom_taxonomies[$tax]['disabled'])) {
        $data['disabled'] = $custom_taxonomies[$tax]['disabled'];
    }
    // Sync with post types
    if (!empty($data['supports'])) {
        $post_types = get_option('wpcf-custom-types', array());
        foreach ($post_types as $id => $type) {
            if (array_key_exists($id, $data['supports'])) {
                if (empty($post_types[$id]['taxonomies'][$data['slug']])) {
                    $post_types[$id][TOOLSET_EDIT_LAST] = time();
                }
                $post_types[$id]['taxonomies'][$data['slug']] = 1;
            } else {
                if (!empty($post_types[$id]['taxonomies'][$data['slug']])) {
                    $post_types[$id][TOOLSET_EDIT_LAST] = time();
                }
                unset($post_types[$id]['taxonomies'][$data['slug']]);
            }
        }
        update_option('wpcf-custom-types', $post_types);
    }
    $custom_taxonomies[$tax] = $data;
    $custom_taxonomies[$tax][TOOLSET_EDIT_LAST] = time();
    update_option('wpcf-custom-taxonomies', $custom_taxonomies);
    // WPML register strings
    wpcf_custom_taxonimies_register_translation($tax, $data);
    wpcf_admin_message_store(apply_filters('types_message_custom_taxonomy_saved', __('Custom taxonomy saved', 'wpcf'), $data, $update), 'custom');
    // Flush rewrite rules
    flush_rewrite_rules();
    // Redirect
    wp_redirect(add_query_arg(array('page' => 'wpcf-edit-tax', 'wpcf-tax' => $tax, 'wpcf-rewrite' => 1, 'wpcf-message' => get_user_option('types-modal')), admin_url('admin.php')));
    die;
}
예제 #13
0
function wpcf_admin_general_settings_form_submit($form)
{
    $settings = wpcf_get_settings();
    $data = $_POST['wpcf_settings'];
    foreach (array('register_translations_on_import', 'help_box') as $setting) {
        if (!isset($data[$setting])) {
            $settings[$setting] = 0;
        } else {
            // @todo Add sanitization here
            $settings[$setting] = $data[$setting];
        }
    }
    update_option('wpcf_settings', $settings);
    wpcf_admin_message_store(__('Settings saved', 'wpcf'));
}
 public function on_term_update($term_id, $tt_id)
 {
     // Get an array of fields that we need to update. We don't care about their groups here.
     $term = get_term_by('term_taxonomy_id', $tt_id);
     if (!$term instanceof WP_Term) {
         return;
     }
     $factory = Types_Field_Group_Term_Factory::get_instance();
     $groups = $factory->get_groups_by_taxonomy($term->taxonomy);
     if (empty($groups)) {
         return;
     }
     $field_definitions = Types_Field_Utils::get_field_definitions_from_groups($groups);
     $update_errors = $this->update_term_fields($term_id, $field_definitions);
     // Display errors if we have any.
     if (!empty($update_errors)) {
         foreach ($update_errors as $update_error) {
             wpcf_admin_message_store($update_error->get_error_message(), 'error');
         }
         wpcf_admin_message_store(sprintf('<strong>%s</strong>', __('There has been a problem while saving custom fields. Please fix it and try again.', 'wpcf')), 'error');
     }
 }
예제 #15
0
/**
 * save_post hook.
 * 
 * @param type $post_ID
 * @param type $post 
 */
function wpcf_admin_post_save_post_hook($post_ID, $post)
{
    if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'update-' . $post->post_type . '_' . $post_ID)) {
        return false;
    }
    if (!in_array($post->post_type, array('revision', 'attachment', 'wp-types-group', 'view', 'view-template'))) {
        // Get groups
        $groups = wpcf_admin_post_get_post_groups_fields($post);
        if (empty($groups)) {
            return false;
        }
        $all_fields = array();
        foreach ($groups as $group) {
            // Process fields
            // Fix duplicates for repetitive fields
            foreach ($group['fields'] as $temp_field_id => $temp_field_data) {
                if (isset($temp_field_data['data']['repetitive']) && isset($_REQUEST['wpcf'][$temp_field_id])) {
                    if (count($_POST['wpcf'][$temp_field_id]) < 2) {
                        continue;
                    }
                    $temp_check_duplicates_old_value = array();
                    $temp_check_duplicates_new_value = array();
                    foreach ($_POST['wpcf'][$temp_field_id] as $posted_field_key => $posted_field_value) {
                        $temp_old_value = base64_decode($posted_field_value['old_value']);
                        $temp_new_value = isset($posted_field_value['new_value']) ? $posted_field_value['new_value'] : serialize($posted_field_value);
                        $temp_field_key = wpcf_types_get_meta_prefix($temp_field_data) . $temp_field_id;
                        // Check new values
                        if (in_array(md5($temp_new_value), $temp_check_duplicates_new_value)) {
                            unset($_POST['wpcf'][$temp_field_id][$posted_field_key]);
                            if ($temp_old_value != '__wpcf_repetitive_new_field') {
                                // Rebuild
                                delete_post_meta($post_ID, $temp_field_key, $temp_old_value);
                                update_post_meta($post_ID, $temp_field_key, $temp_old_value);
                            }
                        } else {
                            if ($temp_old_value == '__wpcf_repetitive_new_field') {
                                $_POST['wpcf'][$temp_field_id][intval($posted_field_key) * 1000] = $_POST['wpcf'][$temp_field_id][$posted_field_key];
                                unset($_POST['wpcf'][$temp_field_id][$posted_field_key]);
                                continue;
                            }
                            $temp_check_duplicates_new_value[] = md5($temp_new_value);
                        }
                        // Check old values
                        //                        if ($temp_old_value != '__wpcf_repetitive_new_field') {
                        //                            if (in_array(md5($temp_old_value),
                        //                                            $temp_check_duplicates_old_value)) {
                        //                                unset($_POST['wpcf'][$temp_field_id][$posted_field_key]);
                        //                                if ($temp_old_value != '__wpcf_repetitive_new_field') {
                        //                                    // Rebuild
                        //                                    delete_post_meta($post_ID, $temp_field_key,
                        //                                            $temp_old_value);
                        //                                    update_post_meta($post_ID, $temp_field_key,
                        //                                            $temp_old_value);
                        //                                }
                        //                            } else {
                        //                                if ($temp_old_value == '__wpcf_repetitive_new_field') {
                        //                                    $_POST['wpcf'][$temp_field_id][intval($posted_field_key) * 1000] = $_POST['wpcf'][$temp_field_id][$posted_field_key];
                        //                                    unset($_POST['wpcf'][$temp_field_id][$posted_field_key]);
                        //                                    continue;
                        //                                }
                        //                                $temp_check_duplicates_old_value[] = md5($temp_old_value);
                        //                            }
                        //                        }
                    }
                }
            }
            $_REQUEST['wpcf'] = $_POST['wpcf'];
            // Fix indexes for repetitive
            foreach ($group['fields'] as $temp_field_id => $temp_field_data) {
                if (isset($temp_field_data['data']['repetitive']) && isset($_REQUEST['wpcf'][$temp_field_id])) {
                    $temp_count = 1;
                    $temp_data = $_REQUEST['wpcf'][$temp_field_id];
                    unset($_REQUEST['wpcf'][$temp_field_id]);
                    $_REQUEST['wpcf'][$temp_field_id] = array();
                    foreach ($temp_data as $temp_post_key => $temp_post_data) {
                        $_REQUEST['wpcf'][$temp_field_id][$temp_count] = $temp_post_data;
                        $temp_count += 1;
                    }
                }
            }
            $_POST['wpcf'] = $_REQUEST['wpcf'];
            $fields = wpcf_admin_post_process_fields($post, $group['fields'], true, false, 'validation');
            // Validate fields
            $form = wpcf_form_simple_validate($fields);
            $all_fields = $all_fields + $fields;
            $error = $form->isError();
            // Trigger form error
            if ($error) {
                wpcf_admin_message_store(__('Please check your input data', 'wpcf'), 'error');
            }
        }
        // Save invalid elements so user can be informed after redirect
        if (!empty($all_fields)) {
            update_post_meta($post_ID, 'wpcf-invalid-fields', $all_fields);
        }
        // Save meta fields
        if (!empty($_POST['wpcf'])) {
            foreach ($_POST['wpcf'] as $field_slug => $field_value) {
                // Repetitive fields
                if (isset($_POST['wpcf_repetitive'][$field_slug])) {
                    foreach ($field_value as $temp_id => $repetitive_data) {
                        $field = wpcf_fields_get_field_by_slug($field_slug);
                        // Skype specific
                        if ($field['type'] == 'skype') {
                            wpcf_admin_post_save_field($post_ID, $all_fields, $field_slug, $repetitive_data, base64_decode($repetitive_data['old_value']));
                        } else {
                            wpcf_admin_post_save_field($post_ID, $all_fields, $field_slug, $repetitive_data['new_value'], base64_decode($repetitive_data['old_value']));
                        }
                    }
                } else {
                    wpcf_admin_post_save_field($post_ID, $all_fields, $field_slug, $field_value);
                }
            }
        }
    }
}
예제 #16
0
/**
 * Saves settings.
 *
 * @param type $form
 */
function wpcf_admin_general_settings_form_submit($form)
{
    if (isset($_POST['clear-cache-images']) || isset($_POST['clear-cache-images-outdated'])) {
        require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/image.php';
        $cache_dir = wpcf_fields_image_get_cache_directory(true);
        if (is_wp_error($cache_dir)) {
            wpcf_admin_message_store($cache_dir->get_error_message());
        } else {
            if (isset($_POST['clear-cache-images'])) {
                wpcf_fields_image_clear_cache($cache_dir, 'all');
            } else {
                wpcf_fields_image_clear_cache($cache_dir);
            }
            wpcf_admin_message_store(__('Images cache cleared', 'wpcf'));
        }
        return true;
    }
    $settings = wpcf_get_settings();
    $data = $_POST['wpcf_settings'];
    $keys = array('add_resized_images_to_library' => 'esc_html', 'help_box' => 'esc_html', 'hide_standard_custom_fields_metabox' => 'esc_html', 'images_remote' => 'intval', 'images_remote_cache_time' => 'intval', 'register_translations_on_import' => 'esc_html', 'toolset_messages' => 'intval', 'postmeta_unfiltered_html' => 'on-off', 'usermeta_unfiltered_html' => 'on-off');
    foreach ($keys as $key => $validation) {
        if (!isset($data[$key])) {
            $settings[$key] = 0;
        } else {
            switch ($validation) {
                case 'intval':
                    $settings[$key] = intval($data[$key]);
                    break;
                case 'on-off':
                    if (preg_match('/^(on|off)$/', $data[$key])) {
                        $settings[$key] = $data[$key];
                    } else {
                        $settings[$key] = 'off';
                    }
                    break;
                case 'esc_html':
                default:
                    $settings[$key] = esc_html($data[$key]);
                    break;
            }
        }
    }
    /**
     * validate hide_standard_custom_fields_metabox
     */
    if (!preg_match('/^(show|hide)$/', $settings['hide_standard_custom_fields_metabox'])) {
        $settings['hide_standard_custom_fields_metabox'] = 'show';
    }
    /**
     * update_option
     */
    update_option('wpcf_settings', $settings);
    wpcf_admin_message_store(__('Settings saved.', 'wpcf'));
}
예제 #17
0
/**
 * Saves fields and groups.
 * 
 * If field name is changed in specific group - new one will be created,
 * otherwise old one will be updated and will appear in that way in other grups.
 * 
 * @return type 
 */
function wpcf_admin_save_fields_groups_submit($form)
{
    if (!isset($_POST['wpcf']['group']['name'])) {
        return false;
    }
    global $wpdb;
    $new_group = false;
    $group_slug = $_POST['wpcf']['group']['slug'] = sanitize_title($_POST['wpcf']['group']['name']);
    // Basic check
    if (isset($_REQUEST['group_id'])) {
        // Check if group exists
        $post = get_post($_REQUEST['group_id']);
        if (empty($post) || $post->post_type != 'wp-types-group') {
            $form->triggerError();
            wpcf_admin_message(sprintf(__("Wrong group ID %d", 'wpcf'), intval($_REQUEST['group_id'])), 'error');
            return false;
        }
        $group_id = $post->ID;
    } else {
        $new_group = true;
    }
    // Save fields for future use
    $fields = array();
    if (!empty($_POST['wpcf']['fields'])) {
        // Before anything - search unallowed characters
        foreach ($_POST['wpcf']['fields'] as $key => $field) {
            if (empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['name']) || !empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['slug'])) {
                $form->triggerError();
                wpcf_admin_message(sprintf(__('Field slugs cannot contain non-English characters. Please edit this field name %s and save again.', 'wpcf'), $field['name']), 'error');
                return false;
            }
        }
        foreach ($_POST['wpcf']['fields'] as $key => $field) {
            if (!empty($field['is_new'])) {
                if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']))) {
                    $form->triggerError();
                    wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error');
                    return false;
                }
            }
            // Field ID and slug are same thing
            //            $slug = $_POST['wpcf']['fields'][$key]['slug'] = sanitize_title($field['name']);
            $field_id = wpcf_admin_fields_save_field($field);
            if (!empty($field_id)) {
                $fields[] = $field_id;
            }
            // WPML
            if (function_exists('wpml_cf_translation_preferences_store')) {
                $wpml_save_cf = wpml_cf_translation_preferences_store($key, wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_id)) . $field_id);
            }
        }
    }
    // Save group
    $post_types = isset($_POST['wpcf']['group']['supports']) ? $_POST['wpcf']['group']['supports'] : array();
    $taxonomies_post = isset($_POST['wpcf']['group']['taxonomies']) ? $_POST['wpcf']['group']['taxonomies'] : array();
    $terms = array();
    foreach ($taxonomies_post as $taxonomy) {
        foreach ($taxonomy as $tax => $term) {
            $terms[] = $term;
        }
    }
    // Rename if needed
    if (isset($_REQUEST['group_id'])) {
        $_POST['wpcf']['group']['id'] = $_REQUEST['group_id'];
    }
    $group_id = wpcf_admin_fields_save_group($_POST['wpcf']['group']);
    // Set open fieldsets
    if ($new_group && !empty($group_id)) {
        $open_fieldsets = get_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', true);
        if (isset($open_fieldsets[-1])) {
            $open_fieldsets[$group_id] = $open_fieldsets[-1];
            unset($open_fieldsets[-1]);
            update_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', $open_fieldsets);
        }
    }
    // Rest of processes
    if (!empty($group_id)) {
        wpcf_admin_fields_save_group_fields($group_id, $fields);
        wpcf_admin_fields_save_group_post_types($group_id, $post_types);
        wpcf_admin_fields_save_group_terms($group_id, $terms);
        if (!empty($_POST['wpcf']['group']['templates'])) {
            wpcf_admin_fields_save_group_templates($group_id, $_POST['wpcf']['group']['templates']);
        }
        $_POST['wpcf']['group']['fields'] = isset($_POST['wpcf']['fields']) ? $_POST['wpcf']['fields'] : array();
        do_action('wpcf_save_group', $_POST['wpcf']['group']);
        wpcf_admin_message_store(__('Group saved', 'wpcf'));
        wp_redirect(admin_url('admin.php?page=wpcf-edit&group_id=' . $group_id));
        die;
    } else {
        wpcf_admin_message_store(__('Error saving group', 'wpcf'), 'error');
    }
}
 /**
  * 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 save()
 {
     if (!isset($_POST['wpcf']) || !isset($_POST['wpcf']['group']) || !isset($_POST['wpcf']['group']['name'])) {
         return false;
     }
     $_POST['wpcf']['group'] = apply_filters('wpcf_group_pre_save', $_POST['wpcf']['group']);
     $group_name = wp_kses_post($_POST['wpcf']['group']['name']);
     require_once WPCF_EMBEDDED_ABSPATH . '/classes/forms.php';
     $form = new Enlimbo_Forms_Wpcf();
     if (empty($group_name)) {
         $form->triggerError();
         wpcf_admin_message(__('Group name can not be empty.', 'wpcf'), 'error');
         return $form;
     }
     $new_group = false;
     $group_slug = sanitize_title($group_name);
     // Basic check
     if (isset($_REQUEST[$this->get_id])) {
         // Check if group exists
         $post = get_post(intval($_REQUEST[$this->get_id]));
         // Name changed
         if (strtolower($group_name) != strtolower($post->post_title)) {
             // Check if already exists
             $exists = get_page_by_title($group_name, 'OBJECT', TYPES_USER_META_FIELD_GROUP_CPT_NAME);
             if (!empty($exists)) {
                 $form->triggerError();
                 wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), apply_filters('the_title', $exists->post_title)), 'error');
                 return $form;
             }
         }
         if (empty($post) || $post->post_type != TYPES_USER_META_FIELD_GROUP_CPT_NAME) {
             $form->triggerError();
             wpcf_admin_message(sprintf(__("Wrong group ID %d", 'wpcf'), intval($_REQUEST[$this->get_id])), 'error');
             return $form;
         }
         $group_id = $post->ID;
     } else {
         $new_group = true;
         // Check if already exists
         $exists = get_page_by_title($group_name, 'OBJECT', TYPES_USER_META_FIELD_GROUP_CPT_NAME);
         if (!empty($exists)) {
             $form->triggerError();
             wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), apply_filters('the_title', $exists->post_title)), 'error');
             return $form;
         }
     }
     // Save fields for future use
     $fields = array();
     if (!empty($_POST['wpcf']['fields'])) {
         foreach ($_POST['wpcf']['fields'] as $key => $field) {
             $field = wpcf_sanitize_field($field);
             $field = apply_filters('wpcf_field_pre_save', $field);
             if (!empty($field['is_new'])) {
                 // Check name and slug
                 if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']), TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta')) {
                     $form->triggerError();
                     wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error');
                     return $form;
                 }
                 if (isset($field['slug']) && wpcf_types_cf_under_control('check_exists', sanitize_title($field['slug']), TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta')) {
                     $form->triggerError();
                     wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error');
                     return $form;
                 }
             }
             // Field ID and slug are same thing
             $field_id = wpcf_admin_fields_save_field($field, TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta');
             if (!empty($field_id)) {
                 $fields[] = $field_id;
             }
         }
     }
     // Save group
     $roles = isset($_POST['wpcf']['group']['supports']) ? $_POST['wpcf']['group']['supports'] : array();
     /**
      * Admin styles
      */
     if (isset($_POST['wpcf']['group']['admin_styles'])) {
         $admin_style = esc_html($_POST['wpcf']['group']['admin_styles']);
     }
     // Rename if needed
     if (isset($_REQUEST[$this->get_id])) {
         $_POST['wpcf']['group']['id'] = intval($_REQUEST[$this->get_id]);
     }
     $group_id = wpcf_admin_fields_save_group($_POST['wpcf']['group'], TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'user');
     // Set open fieldsets
     if ($new_group && !empty($group_id)) {
         $open_fieldsets = get_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', true);
         if (isset($open_fieldsets[-1])) {
             $open_fieldsets[$group_id] = $open_fieldsets[-1];
             unset($open_fieldsets[-1]);
             update_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', $open_fieldsets);
         }
     }
     // Rest of processes
     if (!empty($group_id)) {
         wpcf_admin_fields_save_group_fields($group_id, $fields, false, TYPES_USER_META_FIELD_GROUP_CPT_NAME);
         $this->save_group_showfor($group_id, $roles);
         /**
          * Admin styles
          */
         if (defined('TYPES_USE_STYLING_EDITOR') && TYPES_USE_STYLING_EDITOR && isset($admin_style)) {
             wpcf_admin_fields_save_group_admin_styles($group_id, $admin_style);
         }
         $_POST['wpcf']['group']['fields'] = isset($_POST['wpcf']['fields']) ? $_POST['wpcf']['fields'] : array();
         do_action('wpcf_save_group', $_POST['wpcf']['group']);
         // admin message
         if (!wpcf_is_client()) {
             wpcf_admin_message_store(apply_filters('types_message_usermeta_saved', __('Group saved', 'wpcf'), $group_name, $new_group ? false : true), 'custom');
         }
         wp_safe_redirect(admin_url(sprintf('admin.php?page=wpcf-edit-usermeta&group_id=%d', $group_id)));
         exit;
     } else {
         wpcf_admin_message_store(__('Error saving group', 'wpcf'), 'error');
     }
 }
예제 #19
0
/**
 * Saves user fields and groups.
 * 
 * If field name is changed in specific group - new one will be created,
 * otherwise old one will be updated and will appear in that way in other grups.
 * 
 * @return type 
 */
function wpcf_admin_save_usermeta_groups_submit($form)
{
    if (!isset($_POST['wpcf']['group']['name'])) {
        return false;
    }
    $_POST['wpcf']['group']['name'] = trim($_POST['wpcf']['group']['name']);
    $_POST['wpcf']['group'] = apply_filters('wpcf_group_pre_save', $_POST['wpcf']['group']);
    global $wpdb;
    $new_group = false;
    $group_slug = $_POST['wpcf']['group']['slug'] = sanitize_title($_POST['wpcf']['group']['name']);
    // Basic check
    if (isset($_REQUEST['group_id'])) {
        // Check if group exists
        $post = get_post($_REQUEST['group_id']);
        // Name changed
        if (strtolower($_POST['wpcf']['group']['name']) != strtolower($post->post_title)) {
            // Check if already exists
            $exists = get_page_by_title($_POST['wpcf']['group']['name'], 'OBJECT', 'wp-types-user-group');
            if (!empty($exists)) {
                $form->triggerError();
                wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), $_POST['wpcf']['group']['name']), 'error');
                return $form;
            }
        }
        if (empty($post) || $post->post_type != 'wp-types-user-group') {
            $form->triggerError();
            wpcf_admin_message(sprintf(__("Wrong group ID %d", 'wpcf'), intval($_REQUEST['group_id'])), 'error');
            return $form;
        }
        $group_id = $post->ID;
    } else {
        $new_group = true;
        // Check if already exists
        $exists = get_page_by_title($_POST['wpcf']['group']['name'], 'OBJECT', 'wp-types-user-group');
        if (!empty($exists)) {
            $form->triggerError();
            wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), $_POST['wpcf']['group']['name']), 'error');
            return $form;
        }
    }
    // Save fields for future use
    $fields = array();
    if (!empty($_POST['wpcf']['fields'])) {
        // Before anything - search unallowed characters
        foreach ($_POST['wpcf']['fields'] as $key => $field) {
            if (empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['name']) || !empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['slug'])) {
                $form->triggerError();
                wpcf_admin_message(sprintf(__('Field slugs cannot contain non-English characters. Please edit this field name %s and save again.', 'wpcf'), $field['name']), 'error');
                return $form;
            }
        }
        foreach ($_POST['wpcf']['fields'] as $key => $field) {
            $field = apply_filters('wpcf_field_pre_save', $field);
            if (!empty($field['is_new'])) {
                // Check name and slug
                if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']), 'wp-types-user-group', 'wpcf-usermeta')) {
                    $form->triggerError();
                    wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error');
                    return $form;
                }
                if (isset($field['slug']) && wpcf_types_cf_under_control('check_exists', sanitize_title($field['slug']), 'wp-types-user-group', 'wpcf-usermeta')) {
                    $form->triggerError();
                    wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error');
                    return $form;
                }
            }
            // Field ID and slug are same thing
            $field_id = wpcf_admin_fields_save_field($field, 'wp-types-user-group', 'wpcf-usermeta');
            if (!empty($field_id)) {
                $fields[] = $field_id;
            }
        }
    }
    // Save group
    $roles = isset($_POST['wpcf']['group']['supports']) ? $_POST['wpcf']['group']['supports'] : array();
    $admin_style = $_POST['wpcf']['group']['admin_styles'];
    // Rename if needed
    if (isset($_REQUEST['group_id'])) {
        $_POST['wpcf']['group']['id'] = $_REQUEST['group_id'];
    }
    $group_id = wpcf_admin_fields_save_group($_POST['wpcf']['group'], 'wp-types-user-group');
    $_POST['wpcf']['group']['id'] = $group_id;
    // Set open fieldsets
    if ($new_group && !empty($group_id)) {
        $open_fieldsets = get_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', true);
        if (isset($open_fieldsets[-1])) {
            $open_fieldsets[$group_id] = $open_fieldsets[-1];
            unset($open_fieldsets[-1]);
            update_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', $open_fieldsets);
        }
    }
    // Rest of processes
    if (!empty($group_id)) {
        wpcf_admin_fields_save_group_fields($group_id, $fields, false, 'wp-types-user-group');
        wpcf_admin_fields_save_group_showfor($group_id, $roles);
        wpcf_admin_fields_save_group_admin_styles($group_id, $admin_style);
        $_POST['wpcf']['group']['fields'] = isset($_POST['wpcf']['fields']) ? $_POST['wpcf']['fields'] : array();
        do_action('wpcf_save_group', $_POST['wpcf']['group']);
        wpcf_admin_message_store(apply_filters('types_message_usermeta_saved', __('Group saved', 'wpcf'), $_POST['wpcf']['group']['name'], $new_group ? false : true), 'custom');
        wp_redirect(admin_url('admin.php?page=wpcf-edit-usermeta&group_id=' . $group_id));
        die;
    } else {
        wpcf_admin_message_store(__('Error saving group', 'wpcf'), 'error');
    }
}
예제 #20
0
/**
 * Imports data from XML.
 *
 * @global object $wpdb
 *
 */
function wpcf_admin_import_data($data = '', $redirect = true, $context = 'types', $args = array())
{
    global $wpdb;
    $data_installer = false;
    libxml_use_internal_errors(true);
    $data = simplexml_load_string($data);
    if (!$data) {
        echo '<div class="message error"><p>' . __('Error parsing XML', 'wpcf') . '</p></div>';
        foreach (libxml_get_errors() as $error) {
            echo '<div class="message error"><p>' . $error->message . '</p></div>';
        }
        libxml_clear_errors();
        return false;
    }
    $overwrite_settings = isset($_POST['overwrite-settings']);
    $overwrite_groups = isset($_POST['overwrite-groups']);
    $overwrite_fields = isset($_POST['overwrite-fields']);
    $overwrite_types = isset($_POST['overwrite-types']);
    $overwrite_tax = isset($_POST['overwrite-tax']);
    $delete_groups = isset($_POST['delete-groups']);
    $delete_fields = isset($_POST['delete-fields']);
    $delete_types = isset($_POST['delete-types']);
    $delete_tax = isset($_POST['delete-tax']);
    if ('wpvdemo' == $context && !empty($args)) {
        /**
         * allow overwrite
         */
        $overwrite_groups = true;
        $overwrite_fields = true;
        $overwrite_types = true;
        $overwrite_tax = true;
        include_once dirname(__FILE__) . '/classes/class.types.data.installer.php';
        $data_installer = new Types_Data_Installer($data, $args);
        $data = $data_installer->wpvdemo();
    }
    /**
     * process settings
     */
    if ($overwrite_settings && isset($data->settings)) {
        $wpcf_settings = wpcf_get_settings();
        foreach (wpcf_admin_import_export_simplexml2array($data->settings) as $key => $value) {
            $wpcf_settings[$key] = $value;
        }
        wpcf_save_settings($wpcf_settings);
        wpcf_admin_message_store(__('Settings are updated.', 'wpcf'));
    }
    // Process groups
    $groups_check = array();
    if (!empty($data->groups)) {
        $groups = array();
        // Set insert data from XML
        foreach ($data->groups->group as $group) {
            $group = wpcf_admin_import_export_simplexml2array($group);
            $groups[$group['ID']] = $group;
        }
        // Set insert data from POST
        if (!empty($_POST['groups'])) {
            foreach ($_POST['groups'] as $group_id => $group) {
                if (empty($groups[$group_id])) {
                    continue;
                }
                $groups[$group_id]['add'] = !empty($group['add']);
                $groups[$group_id]['update'] = isset($group['update']) && $group['update'] == 'update' ? true : false;
            }
        } else {
            foreach ($groups as $group_id => $group) {
                $groups[$group_id]['add'] = true;
                $groups[$group_id]['update'] = false;
            }
        }
        // Insert groups
        $show_import_fail_version_message = true;
        foreach ($groups as $group_id => $group) {
            $post = array('post_status' => $group['post_status'], 'post_type' => TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, 'post_title' => $group['post_title'], 'post_content' => !empty($group['post_content']) ? $group['post_content'] : '');
            /**
             * preserve slug
             */
            if (array_key_exists('__types_id', $group)) {
                $post['post_name'] = $group['__types_id'];
            }
            if (isset($group['add']) && $group['add']) {
                $post_to_update = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s", $group['post_title'], TYPES_CUSTOM_FIELD_GROUP_CPT_NAME));
                // Update (may be forced by bulk action)
                if ($group['update'] || $overwrite_groups && !empty($post_to_update)) {
                    if (!empty($post_to_update)) {
                        $post['ID'] = $post_to_update;
                        $group_wp_id = wp_update_post($post);
                        if (!$group_wp_id) {
                            wpcf_admin_message_store(sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title']), 'error');
                        } else {
                            wpcf_admin_message_store(sprintf(__('Group "%s" updated', 'wpcf'), $group['post_title']));
                        }
                    } else {
                        wpcf_admin_message_store(sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title']), 'error');
                    }
                } else {
                    // Insert
                    $group_wp_id = wp_insert_post($post, true);
                    if (is_wp_error($group_wp_id)) {
                        wpcf_admin_message_store(sprintf(__('Group "%s" insert failed', 'wpcf'), $group['post_title']), 'error');
                    } else {
                        wpcf_admin_message_store(sprintf(__('Group "%s" added', 'wpcf'), $group['post_title']));
                    }
                }
                // Update meta
                if (!empty($group['meta'])) {
                    foreach ($group['meta'] as $meta_key => $meta_value) {
                        if ('_wpcf_conditional_display' == $meta_key) {
                            if (!empty($meta_value)) {
                                $meta_value = wpcf_admin_import_export_simplexml2array($meta_value);
                                if (!is_array($meta_value)) {
                                    $meta_value = array();
                                    if ($show_import_fail_version_message) {
                                        $show_import_fail_version_message = false;
                                        wpcf_admin_message_store(__('The Types settings were not fully imported because it contained unsecured data. You should re-export your Types settings using the latest version of Types', 'wpcf'), 'error');
                                    }
                                }
                            }
                        }
                        update_post_meta($group_wp_id, $meta_key, $meta_value);
                    }
                }
                $groups_check[] = $group_wp_id;
                if (!empty($post_to_update)) {
                    $groups_check[] = $post_to_update;
                }
            }
        }
        // Delete groups (forced, set in bulk actions)
    }
    if ($delete_groups) {
        $groups_to_delete = get_posts(array('post_type' => TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, 'post_status' => 'any', 'posts_per_page' => -1));
        if (!empty($groups_to_delete)) {
            foreach ($groups_to_delete as $group_to_delete) {
                if (!in_array($group_to_delete->ID, $groups_check)) {
                    $deleted = wp_delete_post($group_to_delete->ID, true);
                    if (!$deleted) {
                        wpcf_admin_message_store(sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete->post_title), 'error');
                    } else {
                        wpcf_admin_message_store(sprintf(__('Group "%s" deleted', 'wpcf'), $group_to_delete->post_title));
                    }
                }
            }
        }
    } else {
        // If not forced, look in POST
        if (!empty($_POST['groups-to-be-deleted'])) {
            foreach ($_POST['groups-to-be-deleted'] as $group_to_delete) {
                $group_to_delete_post = get_post($group_to_delete);
                if (!empty($group_to_delete_post) && $group_to_delete_post->post_type == TYPES_CUSTOM_FIELD_GROUP_CPT_NAME) {
                    $deleted = wp_delete_post($group_to_delete, true);
                    if (!$deleted) {
                        wpcf_admin_message_store(sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete_post->post_title), 'error');
                    } else {
                        wpcf_admin_message_store(sprintf(__('Group "%s" deleted', 'wpcf'), $group_to_delete_post->post_title));
                    }
                } else {
                    wpcf_admin_message_store(sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete), 'error');
                }
            }
        }
    }
    // Process fields
    $fields_check = array();
    $fields_existing = wpcf_admin_fields_get_fields();
    if (!empty($data->fields)) {
        $fields = array();
        // Set insert data from XML
        foreach ($data->fields->field as $field) {
            $field = wpcf_admin_import_export_simplexml2array($field);
            // Set if submitted in 'types' context
            if ($context == 'types') {
                // Process only if marked
                if (isset($_POST['fields'][$field['id']])) {
                    $fields[$field['id']] = $field;
                }
            } else {
                $fields[$field['id']] = $field;
            }
        }
        // Set insert data from POST
        if (!empty($_POST['fields'])) {
            foreach ($_POST['fields'] as $field_id => $field) {
                if (empty($fields[$field_id])) {
                    continue;
                }
                $fields[$field_id]['add'] = !empty($field['add']);
                $fields[$field_id]['update'] = isset($field['update']) && $field['update'] == 'update' ? true : false;
            }
        }
        // Insert fields
        foreach ($fields as $field_id => $field) {
            if (isset($field['add']) && !$field['add'] && !$overwrite_fields) {
                continue;
            }
            if (empty($field['id']) || empty($field['name']) || empty($field['slug'])) {
                continue;
            }
            $field_data = array();
            $field_data['description'] = isset($field['description']) ? $field['description'] : '';
            $field_data['data'] = isset($field['data']) && is_array($field['data']) ? $field['data'] : array();
            foreach (array('id', 'name', 'type', 'slug', 'meta_key', 'meta_type') as $key) {
                if (array_key_exists($key, $field)) {
                    $field_data[$key] = $field[$key];
                }
            }
            $fields_existing[$field_id] = $field_data;
            $fields_check[] = $field_id;
            // WPML
            global $iclTranslationManagement;
            if (!empty($iclTranslationManagement) && isset($field['wpml_action'])) {
                $iclTranslationManagement->settings['custom_fields_translation'][wpcf_types_get_meta_prefix($field) . $field_id] = $field['wpml_action'];
                $iclTranslationManagement->save_settings();
            }
            wpcf_admin_message_store(sprintf(__('Field "%s" added/updated', 'wpcf'), $field['name']));
        }
    }
    // Delete fields
    if ($delete_fields) {
        foreach ($fields_existing as $k => $v) {
            if (!empty($v['data']['controlled'])) {
                continue;
            }
            if (!in_array($k, $fields_check)) {
                wpcf_admin_message_store(sprintf(__('Field "%s" deleted', 'wpcf'), $fields_existing[$k]['name']));
                unset($fields_existing[$k]);
            }
        }
    } else {
        if (!empty($_POST['fields-to-be-deleted'])) {
            foreach ($_POST['fields-to-be-deleted'] as $field_to_delete) {
                wpcf_admin_message_store(sprintf(__('Field "%s" deleted', 'wpcf'), $fields_existing[$field_to_delete]['name']));
                unset($fields_existing[$field_to_delete]);
            }
        }
    }
    update_option('wpcf-fields', $fields_existing);
    // Process user groups
    //print_r($data->user_groups);exit;
    $groups_check = array();
    if (!empty($data->user_groups) && isset($data->user_groups->group)) {
        $groups = array();
        // Set insert data from XML
        foreach ($data->user_groups->group as $group) {
            $group = wpcf_admin_import_export_simplexml2array($group);
            $groups[$group['ID']] = $group;
        }
        // Set insert data from POST
        if (!empty($_POST['user_groups'])) {
            foreach ($_POST['user_groups'] as $group_id => $group) {
                if (empty($groups[$group_id])) {
                    continue;
                }
                $groups[$group_id]['add'] = !empty($group['add']);
                $groups[$group_id]['update'] = isset($group['update']) && $group['update'] == 'update' ? true : false;
            }
        } else {
            foreach ($groups as $group_id => $group) {
                $groups[$group_id]['add'] = true;
                $groups[$group_id]['update'] = false;
            }
        }
        // Insert groups
        foreach ($groups as $group_id => $group) {
            $post = array('post_status' => $group['post_status'], 'post_type' => TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'post_title' => $group['post_title'], 'post_content' => !empty($group['post_content']) ? $group['post_content'] : '');
            if (isset($group['add']) && $group['add']) {
                $post_to_update = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s", $group['post_title'], TYPES_USER_META_FIELD_GROUP_CPT_NAME));
                // Update (may be forced by bulk action)
                if ($group['update'] || $overwrite_groups && !empty($post_to_update)) {
                    if (!empty($post_to_update)) {
                        $post['ID'] = $post_to_update;
                        $group_wp_id = wp_update_post($post);
                        if (!$group_wp_id) {
                            wpcf_admin_message_store(sprintf(__('User group "%s" update failed', 'wpcf'), $group['post_title']), 'error');
                        } else {
                            wpcf_admin_message_store(sprintf(__('User group "%s" updated', 'wpcf'), $group['post_title']));
                        }
                    } else {
                        wpcf_admin_message_store(sprintf(__('User group "%s" update failed', 'wpcf'), $group['post_title']), 'error');
                    }
                } else {
                    // Insert
                    $group_wp_id = wp_insert_post($post, true);
                    if (is_wp_error($group_wp_id)) {
                        wpcf_admin_message_store(sprintf(__('User group "%s" insert failed', 'wpcf'), $group['post_title']), 'error');
                    } else {
                        wpcf_admin_message_store(sprintf(__('User group "%s" added', 'wpcf'), $group['post_title']));
                    }
                }
                // Update meta
                if (!empty($group['meta'])) {
                    foreach ($group['meta'] as $meta_key => $meta_value) {
                        update_post_meta($group_wp_id, $meta_key, wpcf_admin_import_export_simplexml2array($meta_value));
                    }
                }
                $groups_check[] = $group_wp_id;
                if (!empty($post_to_update)) {
                    $groups_check[] = $post_to_update;
                }
            }
        }
    }
    // Delete groups (forced, set in bulk actions)
    if ($delete_groups) {
        $groups_to_delete = get_posts(array('post_type' => TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'post_status' => 'any', 'posts_per_page' => -1));
        if (!empty($groups_to_delete)) {
            foreach ($groups_to_delete as $group_to_delete) {
                if (!in_array($group_to_delete->ID, $groups_check)) {
                    $deleted = wp_delete_post($group_to_delete->ID, true);
                    if (!$deleted) {
                        wpcf_admin_message_store(sprintf(__('User group "%s" delete failed', 'wpcf'), $group_to_delete->post_title), 'error');
                    } else {
                        wpcf_admin_message_store(sprintf(__('User group "%s" deleted', 'wpcf'), $group_to_delete->post_title));
                    }
                }
            }
        }
    } else {
        // If not forced, look in POST
        if (!empty($_POST['user-groups-to-be-deleted'])) {
            foreach ($_POST['user-groups-to-be-deleted'] as $group_to_delete) {
                $group_to_delete_post = get_post($group_to_delete);
                if (!empty($group_to_delete_post) && $group_to_delete_post->post_type == TYPES_USER_META_FIELD_GROUP_CPT_NAME) {
                    $deleted = wp_delete_post($group_to_delete, true);
                    if (!$deleted) {
                        wpcf_admin_message_store(sprintf(__('User group "%s" delete failed', 'wpcf'), $group_to_delete_post->post_title), 'error');
                    } else {
                        wpcf_admin_message_store(sprintf(__('User group "%s" deleted', 'wpcf'), $group_to_delete_post->post_title));
                    }
                } else {
                    wpcf_admin_message_store(sprintf(__('User group "%s" delete failed', 'wpcf'), $group_to_delete), 'error');
                }
            }
        }
    }
    // Process fields
    $fields_existing = wpcf_admin_fields_get_fields(false, false, false, 'wpcf-usermeta');
    $fields_check = array();
    if (!empty($data->user_fields)) {
        $fields = array();
        // Set insert data from XML
        foreach ($data->user_fields->field as $field) {
            $field = wpcf_admin_import_export_simplexml2array($field);
            // Set if submitted in 'types' context
            if ($context == 'types') {
                // Process only if marked
                if (isset($_POST['user_fields'][$field['id']])) {
                    $fields[$field['id']] = $field;
                }
            } else {
                $fields[$field['id']] = $field;
            }
        }
        // Set insert data from POST
        if (!empty($_POST['user_fields'])) {
            foreach ($_POST['user_fields'] as $field_id => $field) {
                if (empty($fields[$field_id])) {
                    continue;
                }
                $fields[$field_id]['add'] = !empty($field['add']);
                $fields[$field_id]['update'] = isset($field['update']) && $field['update'] == 'update' ? true : false;
            }
        }
        // Insert fields
        foreach ($fields as $field_id => $field) {
            if (isset($field['add']) && !$field['add'] && !$overwrite_fields) {
                continue;
            }
            if (empty($field['id']) || empty($field['name']) || empty($field['slug'])) {
                continue;
            }
            $field_data = array();
            $field_data['id'] = $field['id'];
            $field_data['name'] = $field['name'];
            $field_data['description'] = isset($field['description']) ? $field['description'] : '';
            $field_data['type'] = $field['type'];
            $field_data['slug'] = $field['slug'];
            $field_data['data'] = isset($field['data']) && is_array($field['data']) ? $field['data'] : array();
            $fields_existing[$field_id] = $field_data;
            $fields_check[] = $field_id;
            // WPML
            global $iclTranslationManagement;
            if (!empty($iclTranslationManagement) && isset($field['wpml_action'])) {
                $iclTranslationManagement->settings['custom_fields_translation'][wpcf_types_get_meta_prefix($field) . $field_id] = $field['wpml_action'];
                $iclTranslationManagement->save_settings();
            }
            wpcf_admin_message_store(sprintf(__('User field "%s" added/updated', 'wpcf'), $field['name']));
        }
    }
    // Delete fields
    if ($delete_fields) {
        foreach ($fields_existing as $k => $v) {
            if (!empty($v['data']['controlled'])) {
                continue;
            }
            if (!in_array($k, $fields_check)) {
                wpcf_admin_message_store(sprintf(__('User field "%s" deleted', 'wpcf'), $fields_existing[$k]['name']));
                unset($fields_existing[$k]);
            }
        }
    } else {
        if (!empty($_POST['user-fields-to-be-deleted'])) {
            foreach ($_POST['user-fields-to-be-deleted'] as $field_to_delete) {
                wpcf_admin_message_store(sprintf(__('User field "%s" deleted', 'wpcf'), $fields_existing[$field_to_delete]['name']));
                unset($fields_existing[$field_to_delete]);
            }
        }
    }
    update_option('wpcf-usermeta', $fields_existing);
    // Process types
    $types_existing = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array());
    $types_check = array();
    if (!empty($data->types) && isset($data->types->type)) {
        $types = array();
        // Set insert data from XML
        foreach ($data->types->type as $type) {
            $type = wpcf_admin_import_export_simplexml2array($type);
            // Set if submitted in 'types' context
            if ($context == 'types') {
                if (isset($_POST['types'][$type['id']])) {
                    $types[$type['id']] = $type;
                }
            } else {
                $types[$type['id']] = $type;
            }
        }
        // Set insert data from POST
        if (!empty($_POST['types'])) {
            foreach ($_POST['types'] as $type_id => $type) {
                if (empty($types[$type_id])) {
                    continue;
                }
                $types[$type_id]['add'] = !empty($type['add']);
                $types[$type_id]['update'] = isset($type['update']) && $type['update'] == 'update' ? true : false;
            }
        }
        // Insert types
        foreach ($types as $type_id => $type) {
            if (isset($type['add']) && !$type['add'] && !$overwrite_types) {
                continue;
            }
            unset($type['add'], $type['update']);
            $types_existing[$type_id] = $type;
            $types_check[] = $type_id;
            wpcf_admin_message_store(sprintf(__('Custom post type "%s" added/updated', 'wpcf'), $type_id));
        }
    }
    // Delete types
    if ($delete_types) {
        foreach ($types_existing as $k => $v) {
            if (!in_array($k, $types_check)) {
                unset($types_existing[$k]);
                wpcf_admin_message_store(sprintf(__('Custom post type "%s" deleted', 'wpcf'), esc_html($k)));
            }
        }
    } else {
        if (!empty($_POST['types-to-be-deleted'])) {
            foreach ($_POST['types-to-be-deleted'] as $type_to_delete) {
                wpcf_admin_message_store(sprintf(__('Custom post type "%s" deleted', 'wpcf'), $types_existing[$type_to_delete]['labels']['name']));
                unset($types_existing[$type_to_delete]);
            }
        }
    }
    update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $types_existing);
    // Process taxonomies
    $taxonomies_existing = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array());
    $taxonomies_check = array();
    if (!empty($data->taxonomies) && isset($data->taxonomies->taxonomy)) {
        $taxonomies = array();
        // Set insert data from XML
        foreach ($data->taxonomies->taxonomy as $taxonomy) {
            $taxonomy = wpcf_admin_import_export_simplexml2array($taxonomy);
            // Set if submitted in 'types' context
            if ($context == 'types') {
                if (isset($_POST['taxonomies'][$taxonomy['id']])) {
                    $taxonomies[$taxonomy['id']] = $taxonomy;
                }
            } else {
                $taxonomies[$taxonomy['id']] = $taxonomy;
            }
        }
        // Set insert data from POST
        if (!empty($_POST['taxonomies'])) {
            foreach ($_POST['taxonomies'] as $taxonomy_id => $taxonomy) {
                if (empty($taxonomies[$taxonomy_id])) {
                    continue;
                }
                $taxonomies[$taxonomy_id]['add'] = !empty($taxonomy['add']);
                $taxonomies[$taxonomy_id]['update'] = isset($taxonomy['update']) && $taxonomy['update'] == 'update' ? true : false;
            }
        }
        // Insert taxonomies
        foreach ($taxonomies as $taxonomy_id => $taxonomy) {
            if (isset($taxonomy['add']) && !$taxonomy['add'] && !$overwrite_tax) {
                continue;
            }
            unset($taxonomy['add'], $taxonomy['update']);
            $taxonomies_existing[$taxonomy_id] = $taxonomy;
            $taxonomies_check[] = $taxonomy_id;
            wpcf_admin_message_store(sprintf(__('Custom taxonomy "%s" added/updated', 'wpcf'), $taxonomy_id));
        }
    }
    /**
     * reset TOOLSET_EDIT_LAST
     */
    if ($data_installer) {
        $data_installer->reset_toolset_edit_last();
    }
    // Delete taxonomies
    if ($delete_tax) {
        foreach ($taxonomies_existing as $k => $v) {
            if (!in_array($k, $taxonomies_check)) {
                unset($taxonomies_existing[$k]);
                wpcf_admin_message_store(sprintf(__('Custom taxonomy "%s" deleted', 'wpcf'), $k));
            }
        }
    } else {
        if (!empty($_POST['taxonomies-to-be-deleted'])) {
            foreach ($_POST['taxonomies-to-be-deleted'] as $taxonomy_to_delete) {
                wpcf_admin_message_store(sprintf(__('Custom taxonomy "%s" deleted', 'wpcf'), $taxonomies_existing[$taxonomy_to_delete]['labels']['name']));
                unset($taxonomies_existing[$taxonomy_to_delete]);
            }
        }
    }
    update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $taxonomies_existing);
    // Add relationships
    if (!empty($data->post_relationships) && !empty($_POST['post_relationship'])) {
        $relationship_existing = get_option('wpcf_post_relationship', array());
        /**
         * be sure, $relationship_existing is a array!
         */
        if (!is_array($relationship_existing)) {
            $relationship_existing = array();
        }
        $relationship = json_decode($data->post_relationships->data, true);
        if (is_array($relationship)) {
            $relationship = array_merge($relationship_existing, $relationship);
            update_option('wpcf_post_relationship', $relationship);
            wpcf_admin_message_store(__('Post relationships created', 'wpcf'));
        } else {
            wpcf_admin_message_store(__('Post relationships settings were not imported because it contained unsecured data. You should re-export your Types settings using the latest version of Types', 'wpcf'), 'error');
        }
    }
    // WPML bulk registration
    if (wpcf_get_settings('register_translations_on_import')) {
        wpcf_admin_bulk_string_translation();
    }
    // Flush rewrite rules
    wpcf_init_custom_types_taxonomies();
    flush_rewrite_rules();
    if ($redirect) {
        echo '<script type="text/javascript">
<!--
window.location = "' . admin_url('admin.php?page=wpcf-import-export') . '"
//-->
</script>';
        die;
    }
}
예제 #21
0
/**
 * Adds 3rd party created types, taxonomies and fields
 */
function wpcf_admin_migration_form_submit()
{
    $cfui_types = get_option('cpt_custom_post_types', array());
    $cfui_taxonomies = get_option('cpt_custom_tax_types', array());
    $wpcf_types = get_option('wpcf-custom-types', array());
    $wpcf_taxonomies = get_option('wpcf-custom-taxonomies', array());
    $redirect_page = 'wpcf-ctt';
    if (!empty($_POST['cfui']['types'])) {
        $data = array();
        foreach ($_POST['cfui']['types'] as $key => $types_slug) {
            if (array_key_exists(sanitize_title($types_slug), $wpcf_types)) {
                continue;
            }
            foreach ($cfui_types as $cfui_type) {
                if (sanitize_title($cfui_type['name']) == $types_slug) {
                    $data[$types_slug] = wpcf_admin_migrate_get_cfui_type_data($cfui_type);
                    wpcf_admin_message_store(sprintf(__("Post Type %s added", 'wpcf'), '<em>' . $cfui_type['name'] . '</em>'));
                }
            }
        }
        $wpcf_types = array_merge($wpcf_types, $data);
        update_option('wpcf-custom-types', $wpcf_types);
    }
    if (!empty($_POST['cfui']['tax'])) {
        $data = array();
        foreach ($_POST['cfui']['tax'] as $key => $tax_slug) {
            if (array_key_exists(sanitize_title($tax_slug), $wpcf_taxonomies)) {
                continue;
            }
            foreach ($cfui_taxonomies as $cfui_tax) {
                if (sanitize_title($cfui_tax['name']) == $tax_slug) {
                    $data[$tax_slug] = wpcf_admin_migrate_get_cfui_tax_data($cfui_tax);
                    wpcf_admin_message_store(sprintf(__("Taxonomy %s added", 'wpcf'), '<em>' . $cfui_tax['name'] . '</em>'));
                }
            }
        }
        $wpcf_taxonomies = array_merge($wpcf_taxonomies, $data);
        update_option('wpcf-custom-taxonomies', $wpcf_taxonomies);
    }
    // ACF
    if (!empty($_POST['acf_posts']['migrate_groups'])) {
        foreach ($_POST['acf_posts']['migrate_groups'] as $acf_group_id) {
            if (empty($_POST['acf_posts'][$acf_group_id])) {
                continue;
            }
            global $wpdb;
            $group = $wpdb->get_row($wpdb->prepare("SELECT ID, post_title FROM {$wpdb->posts} WHERE post_title = %s AND post_type='wp-types-group'", $_POST['acf_posts'][$acf_group_id]['post_title']));
            if (empty($group)) {
                $group = array();
                $group['name'] = $_POST['acf_posts'][$acf_group_id]['post_title'];
                $group['description'] = $_POST['acf_posts'][$acf_group_id]['post_content'];
                $group_id = wpcf_admin_fields_save_group($group);
                wpcf_admin_message_store(sprintf(__("Group %s added", 'wpcf'), '<em>' . $group['name'] . '</em>'));
            } else {
                $group_id = $group->ID;
                wpcf_admin_message_store(sprintf(__("Group %s updated", 'wpcf'), '<em>' . $group->post_title . '</em>'));
            }
            $fields_to_add = array();
            if ($group_id && !empty($_POST['acf_posts'][$acf_group_id]['fields'])) {
                foreach ($_POST['acf_posts'][$acf_group_id]['fields'] as $field_id => $field) {
                    if (!in_array($field_id, $_POST['acf_posts'][$acf_group_id]['migrate_fields'])) {
                        continue;
                    }
                    if (!wpcf_types_cf_under_control('check_exists', $field['slug']) || wpcf_types_cf_under_control('check_outsider', $field['slug'])) {
                        // save field
                        $field['controlled'] = 1;
                        $temp = wpcf_admin_fields_save_field($field);
                        $fields_to_add[] = $temp;
                        wpcf_admin_message_store(sprintf(__("Field %s added", 'wpcf'), '<em>' . $temp . '</em>'));
                    }
                }
                wpcf_admin_fields_save_group_fields($group_id, $fields_to_add, false);
            }
            wpcf_admin_fields_save_group_post_types($group_id, array());
            wpcf_admin_fields_save_group_terms($group_id, array());
        }
        $redirect_page = 'wpcf';
    }
    flush_rewrite_rules();
    // Deactivate plugins
    if (!empty($_POST['deactivate-cfui'])) {
        $active_plugins = get_option('active_plugins', array());
        foreach ($active_plugins as $key => $file) {
            if (strpos($file, 'custom-post-type-ui.php') !== false) {
                unset($active_plugins[$key]);
            }
        }
        update_option('active_plugins', array_values($active_plugins));
    }
    wp_redirect(admin_url('admin.php?page=' . $redirect_page));
    die;
}
예제 #22
0
파일: fields.php 프로젝트: sriram911/pls
/**
 * Changes field type.
 * 
 * @param type $fields
 * @param type $type 
 */
function wpcf_admin_custom_fields_change_type($fields, $type)
{
    if (!is_array($fields)) {
        $fields = array(strval($fields));
    }
    $fields = wpcf_types_cf_under_control('add', array('fields' => $fields, 'type' => $type));
    $allowed = array('textfield' => array('wysiwyg', 'textfield', 'textarea', 'email', 'url', 'date', 'phone', 'file', 'image', 'numeric'), 'textarea' => array('wysiwyg', 'textfield', 'textarea', 'email', 'url', 'date', 'phone', 'file', 'image', 'numeric'), 'date' => array('wysiwyg', 'date', 'textarea', 'textfield', 'email', 'url', 'phone', 'file', 'image', 'numeric'), 'email' => array('wysiwyg', 'email', 'textarea', 'textfield', 'date', 'url', 'phone', 'file', 'image', 'numeric'), 'file' => array('wysiwyg', 'file', 'textarea', 'textfield', 'email', 'url', 'phone', 'fdate', 'image', 'numeric'), 'image' => array('wysiwyg', 'image', 'textarea', 'textfield', 'email', 'url', 'phone', 'file', 'idate', 'numeric'), 'numeric' => array('wysiwyg', 'numeric', 'textarea', 'textfield', 'email', 'url', 'phone', 'file', 'image', 'date'), 'phone' => array('wysiwyg', 'phone', 'textarea', 'textfield', 'email', 'url', 'date', 'file', 'image', 'numeric'), 'select' => array('wysiwyg', 'select', 'textarea', 'textfield', 'date', 'email', 'url', 'phone', 'file', 'image', 'numeric'), 'skype' => array('wysiwyg', 'skype', 'textarea', 'textfield', 'date', 'email', 'url', 'phone', 'file', 'image', 'numeric'), 'url' => array('wysiwyg', 'url', 'textarea', 'textfield', 'email', 'date', 'phone', 'file', 'image', 'numeric'), 'checkbox' => array('wysiwyg', 'checkbox', 'textarea', 'textfield', 'email', 'url', 'date', 'phone', 'file', 'image', 'numeric'), 'radio' => array('wysiwyg', 'radio', 'textarea', 'textfield', 'email', 'url', 'date', 'phone', 'file', 'image', 'numeric'), 'wysiwyg' => array('wysiwyg', 'textarea'));
    $all_fields = wpcf_admin_fields_get_fields();
    foreach ($fields as $field_id) {
        if (!isset($all_fields[$field_id])) {
            continue;
        }
        $field = $all_fields[$field_id];
        if (!in_array($type, $allowed[$field['type']])) {
            wpcf_admin_message_store(sprintf(__('Field "%s" type was converted from %s to %s. You need to set some further settings in the group editor.', 'wpcf'), $field['name'], $field['type'], $type));
            $all_fields[$field_id]['data']['disabled_by_type'] = 1;
        } else {
            $all_fields[$field_id]['data']['disabled'] = 0;
            $all_fields[$field_id]['data']['disabled_by_type'] = 0;
        }
        $all_fields[$field_id]['type'] = $type;
    }
    update_option('wpcf-fields', $all_fields);
}
예제 #23
0
function wpcf_admin_general_settings_form_submit($form)
{
    $settings = wpcf_get_settings();
    $data = $_POST['wpcf_settings'];
    foreach (array('register_translations_on_import', 'help_box') as $setting) {
        if (!isset($data[$setting])) {
            $settings[$setting] = 0;
        } else {
            $settings[$setting] = $data[$setting];
        }
    }
    update_option('wpcf_settings', $settings);
    // Credits
    // TODO Remove
    //    require_once WPCF_EMBEDDED_INC_ABSPATH . '/footer-credit.php';
    //    $option = get_option('wpcf_footer_credit', array());
    //    if (!isset($option['message'])) {
    //        $data = wpcf_footer_credit_defaults();
    //        shuffle($data);
    //        $option['message'] = rand(0, count($data));
    //    }
    //    if (!isset($_POST['show_credits'])) {
    //        update_option('wpcf_footer_credit',
    //                array('active' => 0, 'message' => $option['message']));
    //    } else {
    //        update_option('wpcf_footer_credit',
    //                array('active' => 1, 'message' => $option['message']));
    //    }
    wpcf_admin_message_store(__('Settings saved', 'wpcf'));
}
예제 #24
0
/**
 * Important save_post hook.
 * 
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 * 
 * @internal breakpoint
 * @param type $post_ID
 * @param type $post 
 */
function wpcf_admin_post_save_post_hook($post_ID, $post)
{
    global $wpcf;
    // Basic cheks
    /*
     * Allow this hook to be triggered only if Types form is submitted
     */
    if (!isset($_POST['_wpcf_post_wpnonce']) || !wp_verify_nonce($_POST['_wpcf_post_wpnonce'], 'update-' . $post->post_type . '_' . $post_ID)) {
        return false;
    }
    /*
     * Do not save post if is type of:
     * revision
     * attachment
     * wp-types-group
     * view
     * view-template
     * cred-form
     */
    if (in_array($post->post_type, $wpcf->excluded_post_types)) {
        return false;
    }
    /*
     * 
     * 
     * Get all groups connected to this $post
     */
    $groups = wpcf_admin_post_get_post_groups_fields($post);
    if (empty($groups)) {
        return false;
    }
    $all_fields = array();
    $_not_valid = array();
    $_error = false;
    /*
     * 
     * 
     * Loop over each group
     * 
     * TODO Document this
     * Connect 'wpcf-invalid-fields' with all fields
     */
    foreach ($groups as $group) {
        // Process fields
        $fields = wpcf_admin_post_process_fields($post, $group['fields'], true, false, 'validation');
        // Validate fields
        $form = wpcf_form_simple_validate($fields);
        $all_fields = $all_fields + $fields;
        // Collect all not valid fields
        if ($form->isError()) {
            $_error = true;
            // Set error only to true
            $_not_valid = array_merge($_not_valid, (array) $form->get_not_valid());
        }
    }
    // Set fields
    foreach ($all_fields as $k => $v) {
        // only Types field
        if (empty($v['wpcf-id'])) {
            continue;
        }
        $_temp = new WPCF_Field();
        $_temp->set($wpcf->post, $v['wpcf-id']);
        $all_fields[$k]['_field'] = $_temp;
    }
    foreach ($_not_valid as $k => $v) {
        // only Types field
        if (empty($v['wpcf-id'])) {
            continue;
        }
        $_temp = new WPCF_Field();
        $_temp->set($wpcf->post, $v['wpcf-id']);
        $_not_valid[$k]['_field'] = $_temp;
    }
    /*
     * 
     * Allow interaction here.
     * Conditional will set $error to false if field is conditional
     * and not submitted.
     */
    $error = apply_filters('wpcf_post_form_error', $_error, $_not_valid, $all_fields);
    $not_valid = apply_filters('wpcf_post_form_not_valid', $_not_valid, $_error, $all_fields);
    // Notify user about error
    if ($error) {
        wpcf_admin_message_store(__('Please check your input data', 'wpcf'), 'error');
    }
    /*
     * Save invalid elements so user can be informed after redirect.
     */
    if (!empty($not_valid)) {
        update_post_meta($post_ID, 'wpcf-invalid-fields', $not_valid);
    }
    /*
     * 
     * 
     * 
     * 
     * Save meta fields
     */
    if (!empty($_POST['wpcf'])) {
        foreach ($_POST['wpcf'] as $field_slug => $field_value) {
            // Get field by slug
            $field = wpcf_fields_get_field_by_slug($field_slug);
            if (empty($field)) {
                continue;
            }
            // Set field
            $wpcf->field->set($post_ID, $field);
            // Skip copied fields
            // CHECKPOINT
            if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) {
                continue;
            }
            // Don't save invalid
            // CHECKPOINT
            if (isset($not_valid[$field['slug']])) {
                continue;
            }
            /*
             * 
             * 
             * Saving fields
             * @since 1.2
             * 
             * We changed way repetitive fields are saved.
             * On each save fields are rewritten and order is saved in
             * '_$slug-sort-order' meta field.
             */
            /*
             * 
             * We marked fields as repetitive in POST['__wpcf_repetitive']
             * Without this check we won't save any.
             * @see WPCF_Repeater::get_fields_form()
             */
            if (isset($_POST['__wpcf_repetitive'][$wpcf->field->slug])) {
                /*
                 * Use here WPCF_Repeater class.
                 * WPCF_Repeater::set() - switches to current post
                 * WPCF_Repeater::save() - saves repetitive field
                 */
                $wpcf->repeater->set($post_ID, $field);
                $wpcf->repeater->save();
            } else {
                /*
                 * Use WPCF_Field::save()
                 */
                $wpcf->field->save();
            }
            do_action('wpcf_post_field_saved', $post_ID, $field);
        }
    }
    /*
     * Process checkboxes
     * 
     * TODO Revise and remove
     * Since Types 1.1.5 we moved this check to embedded/includes/checkbox.php
     * checkbox.php added permanently to bootstrap.
     */
    foreach ($all_fields as $field) {
        if (!isset($field['#type'])) {
            continue;
        }
        //        if ( $field['#type'] == 'checkbox'
        //                && !isset( $_POST['wpcf'][$field['wpcf-slug']] ) ) {
        //            $field_data = wpcf_admin_fields_get_field( $field['wpcf-id'] );
        //            if ( isset( $field_data['data']['save_empty'] )
        //                    && $field_data['data']['save_empty'] == 'yes' ) {
        //                update_post_meta( $post_ID,
        //                        wpcf_types_get_meta_prefix( $field ) . $field['wpcf-slug'],
        //                        0 );
        //            } else {
        //                delete_post_meta( $post_ID,
        //                        wpcf_types_get_meta_prefix( $field ) . $field['wpcf-slug'] );
        //            }
        //        }
        if ($field['#type'] == 'checkboxes') {
            $field_data = wpcf_admin_fields_get_field($field['wpcf-id']);
            if (!empty($field_data['data']['options'])) {
                $update_data = array();
                foreach ($field_data['data']['options'] as $option_id => $option_data) {
                    if (!isset($_POST['wpcf'][$field['wpcf-slug']][$option_id])) {
                        if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') {
                            $update_data[$option_id] = 0;
                        }
                    } else {
                        $update_data[$option_id] = $_POST['wpcf'][$field['wpcf-slug']][$option_id];
                    }
                }
                update_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], $update_data);
            }
        }
    }
    do_action('wpcf_post_saved', $post_ID);
}
예제 #25
0
/**
 * Submitted Bulk actions.
 */
function wpcf_admin_custom_fields_control_bulk_actions($action = '')
{
    if ($action == 'wpcf-deactivate-bulk') {
        $fields = wpcf_admin_fields_get_fields();
        foreach ($_POST['fields'] as $field_id) {
            if (isset($fields[$field_id])) {
                $fields[$field_id]['data']['disabled'] = 1;
                wpcf_admin_message_store(sprintf(__('Removed from Types control: %s', 'wpcf'), $fields[$field_id]['name']));
            }
        }
        wpcf_admin_fields_save_fields($fields);
    } else {
        if ($action == 'wpcf-activate-bulk') {
            $fields = wpcf_admin_fields_get_fields();
            $fields_bulk = wpcf_types_cf_under_control('add', array('fields' => $_POST['fields']));
            foreach ($fields_bulk as $field_id) {
                if (isset($fields[$field_id]) && empty($fields[$field_id]['data']['disabled_by_type'])) {
                    $fields[$field_id]['data']['disabled'] = 0;
                }
                wpcf_admin_message_store(sprintf(__('Added to Types control: %s', 'wpcf'), $field_id));
            }
            wpcf_admin_fields_save_fields($fields);
        } else {
            if ($action == 'wpcf-delete-bulk') {
                require_once WPCF_INC_ABSPATH . '/fields.php';
                $failed = array();
                $success = array();
                foreach ($_POST['fields'] as $field_id) {
                    $response = wpcf_admin_fields_delete_field($field_id);
                    if (!$response) {
                        $failed[] = str_replace('_' . md5('wpcf_not_controlled'), '', $field_id);
                    } else {
                        $success[] = $field_id;
                    }
                }
                if (!empty($success)) {
                    wpcf_admin_message_store(sprintf(__('Fields %s have been deleted.', 'wpcf'), implode(', ', $success)));
                }
                if (!empty($failed)) {
                    wpcf_admin_message_store(sprintf(__('Fields %s are not Types fields. Types wont delete these fields.', 'wpcf'), implode(', ', $failed)));
                }
            }
        }
    }
    wp_redirect($_SERVER['REQUEST_URI']);
    die;
}
예제 #26
0
/**
 * Admin notice with dismiss button.
 * 
 * @param type $ID
 * @param string $message
 * @param type $store
 * @return boolean 
 */
function wpcf_admin_message_dismiss($ID, $message, $store = true)
{
    $dismissed = get_option('wpcf_dismissed_messages', array());
    if (in_array($ID, $dismissed)) {
        return false;
    }
    $message = $message . '<div style="float:right; margin:-15px 0 0 15px;"><a onclick="jQuery(this).parent().parent().fadeOut();jQuery.get(\'' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=dismiss_message&amp;id=' . $ID . '&amp;_wpnonce=' . wp_create_nonce('dismiss_message')) . '\');return false;"' . 'class="button-secondary" href="javascript:void(0);">' . __('Dismiss', 'wpcf') . '</a></div>';
    if ($store) {
        wpcf_admin_message_store($message, 'updated', $ID);
    } else {
        wpcf_admin_message($message);
    }
}
예제 #27
0
 /**
  * Unified save child function.
  *
  * @param int $parent_id
  * @param int $child_id
  * @param array $save_fields
  * @return bool|WP_Error
  */
 function save_child($parent_id, $child_id, $save_fields = array())
 {
     $parent = get_post(intval($parent_id));
     $child = get_post(intval($child_id));
     $post_data = array();
     if (empty($parent) || empty($child)) {
         return new WP_Error('wpcf-relationship-save-child', 'no parent/child post');
     }
     // Save relationship
     update_post_meta($child->ID, '_wpcf_belongs_' . $parent->post_type . '_id', $parent->ID);
     // Check if added via AJAX
     $check = get_post_meta($child->ID, '_wpcf_relationship_new', true);
     $new = !empty($check);
     delete_post_meta($child->ID, '_wpcf_relationship_new');
     // Set post data
     $post_data['ID'] = $child->ID;
     // Title needs to be checked if submitted at all
     if (!isset($save_fields['_wp_title'])) {
         // If not submitted that means it is not offered to be edited
         if (!empty($child->post_title)) {
             $post_title = $child->post_title;
         } else {
             // DO NOT LET IT BE EMPTY
             $post_title = $child->post_type . ' ' . $child->ID;
         }
     } else {
         $post_title = $save_fields['_wp_title'];
     }
     $post_data['post_title'] = $post_title;
     $post_data['post_content'] = isset($save_fields['_wp_body']) ? $save_fields['_wp_body'] : $child->post_content;
     $post_data['post_type'] = $child->post_type;
     // Check post status - if new, convert to 'publish' else keep remaining
     if ($new) {
         $post_data['post_status'] = 'publish';
     } else {
         $post_data['post_status'] = get_post_status($child->ID);
     }
     /*
      *
      *
      *
      *
      *
      *
      * UPDATE POST
      */
     $cf = new WPCF_Field();
     if (isset($_POST['wpcf_post_relationship'][$parent_id]) && isset($_POST['wpcf_post_relationship'][$parent_id][$child_id])) {
         $_POST['wpcf'] = array();
         foreach ($_POST['wpcf_post_relationship'][$parent_id][$child_id] as $slug => $value) {
             $_POST['wpcf'][$cf->__get_slug_no_prefix($slug)] = $value;
             $_POST['wpcf'][$slug] = $value;
         }
     }
     unset($cf);
     /**
     * avoid filters for children
     * /
             global $wp_filter;
             $save_post = $wp_filter['save_post'];
             $wp_filter['save_post'] = array();
     */
     $updated_id = wp_update_post($post_data);
     /*
        $wp_filter['save_post'] = $save_post;
     */
     unset($save_post);
     if (empty($updated_id)) {
         return new WP_Error('relationship-update-post-failed', 'Updating post failed');
     }
     // Save parents
     if (!empty($save_fields['parents'])) {
         foreach ($save_fields['parents'] as $parent_post_type => $parent_post_id) {
             update_post_meta($child->ID, '_wpcf_belongs_' . $parent_post_type . '_id', $parent_post_id);
         }
     }
     // Update taxonomies
     if (!empty($save_fields['taxonomies']) && is_array($save_fields['taxonomies'])) {
         $_save_data = array();
         foreach ($save_fields['taxonomies'] as $taxonomy => $t) {
             if (!is_taxonomy_hierarchical($taxonomy)) {
                 $_save_data[$taxonomy] = strval($t);
                 continue;
             }
             foreach ($t as $term_id) {
                 if ($term_id != '-1') {
                     $term = get_term($term_id, $taxonomy);
                     if (empty($term)) {
                         continue;
                     }
                     $_save_data[$taxonomy][] = $term_id;
                 }
             }
         }
         wp_delete_object_term_relationships($child->ID, array_keys($save_fields['taxonomies']));
         foreach ($_save_data as $_taxonomy => $_terms) {
             wp_set_post_terms($child->ID, $_terms, $_taxonomy, $append = false);
         }
     }
     // Unset non-types
     unset($save_fields['_wp_title'], $save_fields['_wp_body'], $save_fields['parents'], $save_fields['taxonomies']);
     /*
      *
      *
      *
      *
      *
      *
      * UPDATE Loop over fields
      */
     foreach ($save_fields as $slug => $value) {
         if (defined('WPTOOLSET_FORMS_VERSION')) {
             // Get field by slug
             $field = wpcf_fields_get_field_by_slug(str_replace(WPCF_META_PREFIX, '', $slug));
             if (empty($field)) {
                 continue;
             }
             // Set config
             $config = wptoolset_form_filter_types_field($field, $child->ID);
             // Check if valid
             $valid = wptoolset_form_validate_field('post', $config, $value);
             if (is_wp_error($valid)) {
                 $errors = $valid->get_error_data();
                 $msg = sprintf(__('Child post "%s" field "%s" not updated:', 'wpcf'), $child->post_title, $field['name']);
                 wpcf_admin_message_store($msg . ' ' . implode(', ', $errors), 'error');
                 continue;
             }
         }
         $this->cf->set($child, $field);
         $this->cf->context = 'post_relationship';
         $this->cf->save($value);
     }
     do_action('wpcf_relationship_save_child', $child, $parent);
     clean_post_cache($parent->ID);
     clean_post_cache($child->ID);
     // Added because of caching meta 1.5.4
     wp_cache_flush();
     return true;
 }
예제 #28
0
/**
 * Submit function
 */
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']);
    }
    // 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-custom-types', array());
    // 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 = 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-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-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-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-custom-taxonomies', $taxes);
    }
    // Preserve protected data
    foreach ($protected_data_check as $key => $value) {
        if (strpos($key, '_') !== 0) {
            unset($protected_data_check[$key]);
        }
    }
    // Merging protected data
    $custom_types[$post_type] = array_merge($protected_data_check, $data);
    update_option('wpcf-custom-types', $custom_types);
    // WPML register strings
    wpcf_custom_types_register_translation($post_type, $data);
    wpcf_admin_message_store(apply_filters('types_message_custom_post_type_saved', __('Custom post type saved', 'wpcf'), $data, $update), 'custom');
    // Flush rewrite rules
    flush_rewrite_rules();
    do_action('wpcf_custom_types_save', $data);
    // Redirect
    wp_redirect(add_query_arg(array('page' => 'wpcf-edit-type', 'wpcf-post-type' => $post_type, 'wpcf-rewrite' => 1, 'wpcf-message' => 'view'), admin_url('admin.php')));
    die;
}
예제 #29
0
/**
 * Submitted Bulk actions.
 */
function wpcf_admin_custom_fields_control_bulk_actions($action = '')
{
    if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'custom_fields_control_bulk')) {
        return;
    }
    if ($action == 'wpcf-deactivate-bulk') {
        $fields = wpcf_admin_fields_get_fields(false, true);
        foreach ($_POST['fields'] as $field_id) {
            $field_id = sanitize_text_field($field_id);
            if (isset($fields[$field_id])) {
                $fields[$field_id]['data']['disabled'] = 1;
                wpcf_admin_message_store(sprintf(__('Removed from Types control: %s', 'wpcf'), $fields[$field_id]['name']));
            }
        }
        wpcf_admin_fields_save_fields($fields);
    } else {
        if ($action == 'wpcf-activate-bulk') {
            $fields = wpcf_admin_fields_get_fields(false, true);
            $fields_bulk = wpcf_types_cf_under_control('add', array('fields' => $_POST['fields']));
            foreach ($fields_bulk as $field_id) {
                if (isset($fields[$field_id])) {
                    $fields[$field_id]['data']['disabled'] = 0;
                }
                wpcf_admin_message_store(sprintf(__('Added to Types control: %s', 'wpcf'), $field_id));
            }
            wpcf_admin_fields_save_fields($fields);
        } else {
            if ($action == 'wpcf-delete-bulk') {
                require_once WPCF_INC_ABSPATH . '/fields.php';
                $failed = array();
                $success = array();
                foreach ($_POST['fields'] as $field_id) {
                    $field_id = sanitize_text_field($field_id);
                    $response = wpcf_admin_fields_delete_field($field_id);
                    if (!$response) {
                        $failed[] = str_replace('_' . md5('wpcf_not_controlled'), '', $field_id);
                    } else {
                        $success[] = $field_id;
                    }
                }
                if (!empty($success)) {
                    wpcf_admin_message_store(sprintf(__('Fields %s have been deleted.', 'wpcf'), implode(', ', $success)));
                }
                if (!empty($failed)) {
                    wpcf_admin_message_store(sprintf(__('Fields %s are not Types fields. Types wont delete these fields.', 'wpcf'), implode(', ', $failed)), 'error');
                }
            }
        }
    }
    $url = add_query_arg(array('page' => 'wpcf-custom-fields-control', 'display_all' => isset($_REQUEST['display_all']) ? 1 : 0), admin_url('admin.php'));
    wp_redirect($url);
    die;
}
 /**
  * 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.
  */
 private function save()
 {
     if (!isset($_POST['ct'])) {
         return false;
     }
     $data = $_POST['ct'];
     $update = false;
     // Sanitize data
     if (isset($data[$this->get_id])) {
         $update = true;
         $data[$this->get_id] = sanitize_title($data[$this->get_id]);
     }
     if (isset($data['slug'])) {
         $data['slug'] = sanitize_title($data['slug']);
     }
     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']);
     }
     // Set tax name
     $tax = '';
     if (!empty($data['slug'])) {
         $tax = $data['slug'];
     } else {
         if (!empty($data[$this->get_id])) {
             $tax = $data[$this->get_id];
         } else {
             if (!empty($data['labels']['singular_name'])) {
                 $tax = sanitize_title($data['labels']['singular_name']);
             }
         }
     }
     if (empty($tax)) {
         wpcf_admin_message(__('Please set taxonomy name', 'wpcf'), 'error');
         return false;
     }
     if (empty($data['labels']['singular_name'])) {
         $data['labels']['singular_name'] = $tax;
     }
     $data['slug'] = $tax;
     $taxonomies = $this->taxonomies->get();
     /**
      * is built-in?
      */
     $tax_is_built_in = wpcf_is_builtin_taxonomy($tax);
     // Check reserved name
     $reserved = wpcf_is_reserved_name($tax, 'taxonomy') && !$tax_is_built_in;
     if (is_wp_error($reserved)) {
         wpcf_admin_message($reserved->get_error_message(), 'error');
         return false;
     }
     // Check if exists
     if ($update && !array_key_exists($data[$this->get_id], $taxonomies)) {
         wpcf_admin_message(__("Taxonomy do not exist", 'wpcf'), 'error');
         return false;
     }
     // Check overwriting
     if (!$update && array_key_exists($tax, $taxonomies)) {
         /**
          * set last edit author
          */
         $data[WPCF_AUTHOR] = get_current_user_id();
         wpcf_admin_message(__('Taxonomy already exists', 'wpcf'), 'error');
         return false;
     }
     // Check if our tax overwrites some tax outside
     $tax_exists = get_taxonomy($tax);
     if (!$update && !empty($tax_exists)) {
         wpcf_admin_message(__('Taxonomy already exists', 'wpcf'), 'error');
         return false;
     }
     // Check if renaming
     if (!$tax_is_built_in && $update && $data[$this->get_id] != $tax) {
         global $wpdb;
         $wpdb->update($wpdb->term_taxonomy, array('taxonomy' => esc_sql($tax)), array('taxonomy' => esc_sql($data[$this->get_id])), array('%s'), array('%s'));
         // Sync action
         do_action('wpcf_taxonomy_renamed', $tax, $data[$this->get_id]);
         // Delete old type
         unset($taxonomies[$data[$this->get_id]]);
     }
     // Check if active
     if (isset($taxonomies[$tax]['disabled'])) {
         $data['disabled'] = $taxonomies[$tax]['disabled'];
     }
     /**
      * Sync with post types
      */
     $post_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array());
     foreach ($post_types as $id => $type) {
         if (!empty($data['supports']) && array_key_exists($id, $data['supports'])) {
             if (empty($post_types[$id]['taxonomies'][$data['slug']])) {
                 $post_types[$id][TOOLSET_EDIT_LAST] = time();
             }
             $post_types[$id]['taxonomies'][$data['slug']] = 1;
         } else {
             if (!empty($post_types[$id]['taxonomies'][$data['slug']])) {
                 $post_types[$id][TOOLSET_EDIT_LAST] = time();
             }
             unset($post_types[$id]['taxonomies'][$data['slug']]);
         }
     }
     update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $post_types);
     /**
      * fix built-in
      */
     if ($tax_is_built_in) {
         $data['_builtin'] = true;
         unset($data['icon']);
         // make sure default labels are used for the built-in taxonomies
         // for the case a smart user enables disabled="disabled" inputs
         $data['labels'] = $taxonomies[$tax]['labels'];
         unset($data['wpcf-tax']);
     }
     $taxonomies[$tax] = $data;
     $taxonomies[$tax][TOOLSET_EDIT_LAST] = time();
     // set last edit author
     $taxonomies[$tax][WPCF_AUTHOR] = get_current_user_id();
     foreach ($taxonomies as $id => $taxonomy) {
         // make sure "supports" field is saved for ALL taxonomies
         if (!isset($taxonomy['supports']) && isset($taxonomy['object_type'])) {
             if (!empty($taxonomy['object_type'])) {
                 foreach ($taxonomy['object_type'] as $supported_post) {
                     $taxonomy['supports'][$supported_post] = 1;
                 }
             }
         }
         // make sure "slug" field is set
         if (!isset($taxonomy['slug'])) {
             $taxonomy['slug'] = isset($taxonomy['name']) ? $taxonomy['name'] : $id;
         }
         // make sure "name" field is set
         if (!isset($taxonomy['name'])) {
             $taxonomy['name'] = isset($taxonomy['slug ']);
         }
         // make sure "supports" field is set
         if (!isset($taxonomy['supports'])) {
             $taxonomy['supports'] = array();
         }
         $taxonomies[$id] = $taxonomy;
     }
     /**
      * save
      */
     update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $taxonomies);
     // WPML register strings
     wpcf_custom_taxonimies_register_translation($tax, $data);
     if (wpcf_is_client()) {
         $msg = $update ? __('Taxonomy saved.', 'wpcf') : __('New Taxonomy created.', 'wpcf');
         wpcf_admin_message_store($msg, 'updated notice notice-success is-dismissible');
     } else {
         wpcf_admin_message_store(apply_filters('types_message_custom_taxonomy_saved', __('Taxonomy saved.', 'wpcf'), $data, $update), 'custom');
     }
     // Flush rewrite rules
     flush_rewrite_rules();
     // Redirect
     wp_safe_redirect(esc_url_raw(add_query_arg(array('page' => 'wpcf-edit-tax', $this->get_id => $tax, 'wpcf-message' => get_user_option('types-modal')), admin_url('admin.php'))));
     die;
 }