/**
  * 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;
 }
/**
 * Generates form data.
 */
function wpcf_admin_usermeta_form()
{
    global $wpcf;
    wpcf_admin_add_js_settings('wpcf_nonce_toggle_group', '\'' . wp_create_nonce('group_form_collapsed') . '\'');
    wpcf_admin_add_js_settings('wpcf_nonce_toggle_fieldset', '\'' . wp_create_nonce('form_fieldset_toggle') . '\'');
    $default = array();
    global $wpcf_button_style;
    global $wpcf_button_style30;
    // If it's update, get data
    $update = false;
    if (isset($_REQUEST['group_id'])) {
        $update = wpcf_admin_fields_get_group(intval($_REQUEST['group_id']), 'wp-types-user-group');
        if (empty($update)) {
            $update = false;
            wpcf_admin_message(sprintf(__("Group with ID %d do not exist", 'wpcf'), intval($_REQUEST['group_id'])));
        } else {
            $update['fields'] = wpcf_admin_fields_get_fields_by_group($_REQUEST['group_id'], 'slug', false, true, false, 'wp-types-user-group', 'wpcf-usermeta');
            $update['show_for'] = wpcf_admin_get_groups_showfor_by_group($_REQUEST['group_id']);
            $update['admin_styles'] = wpcf_admin_get_groups_admin_styles_by_group($_REQUEST['group_id']);
        }
    }
    $form = array();
    $form['#form']['callback'] = array('wpcf_admin_save_usermeta_groups_submit');
    // Form sidebars
    $form['open-sidebar'] = array('#type' => 'markup', '#markup' => '<div class="wpcf-form-fields-align-right">');
    // Set help icon
    $form['help-icon'] = array('#type' => 'markup', '#markup' => '<div class="wpcf-admin-fields-help"><img src="' . WPCF_EMBEDDED_RELPATH . '/common/res/images/question.png" style="position:relative;top:2px;" />&nbsp;<a href="http://wp-types.com/documentation/user-guides/using-custom-fields/" target="_blank">' . __('Usermeta help', 'wpcf') . '</a></div>');
    $form['submit2'] = array('#type' => 'submit', '#name' => 'save', '#value' => __('Save', 'wpcf'), '#attributes' => array('class' => 'button-primary wpcf-disabled-on-submit'));
    $form['fields'] = array('#type' => 'fieldset', '#title' => __('Available fields', 'wpcf'));
    // Get field types
    $fields_registered = wpcf_admin_fields_get_available_types();
    foreach ($fields_registered as $filename => $data) {
        $form['fields'][basename($filename, '.php')] = array('#type' => 'markup', '#markup' => '<a href="' . admin_url('admin-ajax.php' . '?action=wpcf_ajax&amp;wpcf_action=fields_insert' . '&amp;field=' . basename($filename, '.php') . '&amp;page=wpcf-edit-usermeta') . '&amp;_wpnonce=' . wp_create_nonce('fields_insert') . '" ' . 'class="wpcf-fields-add-ajax-link button-secondary">' . $data['title'] . '</a> ');
        // Process JS
        if (!empty($data['group_form_js'])) {
            foreach ($data['group_form_js'] as $handle => $script) {
                if (isset($script['inline'])) {
                    add_action('admin_footer', $script['inline']);
                    continue;
                }
                $deps = !empty($script['deps']) ? $script['deps'] : array();
                $in_footer = !empty($script['in_footer']) ? $script['in_footer'] : false;
                wp_register_script($handle, $script['src'], $deps, WPCF_VERSION, $in_footer);
                wp_enqueue_script($handle);
            }
        }
        // Process CSS
        if (!empty($data['group_form_css'])) {
            foreach ($data['group_form_css'] as $handle => $script) {
                if (isset($script['src'])) {
                    $deps = !empty($script['deps']) ? $script['deps'] : array();
                    wp_enqueue_style($handle, $script['src'], $deps, WPCF_VERSION);
                } else {
                    if (isset($script['inline'])) {
                        add_action('admin_head', $script['inline']);
                    }
                }
            }
        }
    }
    // Get fields created by user
    $fields = wpcf_admin_fields_get_fields(true, true, false, 'wpcf-usermeta');
    if (!empty($fields)) {
        $form['fields-existing'] = array('#type' => 'fieldset', '#title' => __('User created fields', 'wpcf'), '#id' => 'wpcf-form-groups-user-fields');
        foreach ($fields as $key => $field) {
            if (isset($update['fields']) && array_key_exists($key, $update['fields'])) {
                continue;
            }
            if (!empty($field['data']['removed_from_history'])) {
                continue;
            }
            $form['fields-existing'][$key] = array('#type' => 'markup', '#markup' => '<div id="wpcf-user-created-fields-wrapper-' . $field['id'] . '" style="float:left; margin-right: 10px;"><a href="' . admin_url('admin-ajax.php' . '?action=wpcf_ajax' . '&amp;page=wpcf-edit' . '&amp;wpcf_action=usermeta_insert_existing' . '&amp;field=' . $field['id']) . '&amp;_wpnonce=' . wp_create_nonce('usermeta_insert_existing') . '" ' . 'class="wpcf-fields-add-ajax-link button-secondary" onclick="jQuery(this).parent().fadeOut();" ' . 'data-slug="' . $field['id'] . '">' . htmlspecialchars(stripslashes($field['name'])) . '</a>' . '<a href="' . admin_url('admin-ajax.php' . '?action=wpcf_ajax' . '&amp;wpcf_action=remove_from_history2' . '&amp;field_id=' . $field['id']) . '&amp;_wpnonce=' . wp_create_nonce('remove_from_history2') . '&amp;wpcf_warning=' . sprintf(__('Are you sure that you want to remove field %s from history?', 'wpcf'), htmlspecialchars(stripslashes($field['name']))) . '&amp;wpcf_ajax_update=wpcf-user-created-fields-wrapper-' . $field['id'] . '" title="' . sprintf(__('Remove field %s', 'wpcf'), htmlspecialchars(stripslashes($field['name']))) . '" class="wpcf-ajax-link"><img src="' . WPCF_RES_RELPATH . '/images/delete-2.png" style="postion:absolute;margin-top:5px;margin-left:-4px;" /></a></div>');
        }
    }
    $form['close-sidebar'] = array('#type' => 'markup', '#markup' => '</div>');
    // Group data
    $form['open-main'] = array('#type' => 'markup', '#markup' => '<div id="wpcf-form-fields-main">');
    $form['title'] = array('#type' => 'textfield', '#name' => 'wpcf[group][name]', '#id' => 'wpcf-group-name', '#value' => $update ? $update['name'] : __('Enter group title', 'wpcf'), '#inline' => true, '#attributes' => array('style' => 'width:100%;margin-bottom:10px;'), '#validate' => array('required' => array('value' => true)));
    if (!$update) {
        $form['title']['#attributes']['data-label'] = addcslashes(__('Enter group title', 'wpcf'), '"');
        $form['title']['#attributes']['onfocus'] = 'if (jQuery(this).val() == jQuery(this).data(\'label\')) { jQuery(this).val(\'\'); }';
        $form['title']['#attributes']['onblur'] = 'if (jQuery(this).val() == \'\') { jQuery(this).val(jQuery(this).data(\'label\')) }';
    }
    $form['description'] = array('#type' => 'textarea', '#id' => 'wpcf-group-description', '#name' => 'wpcf[group][description]', '#value' => $update ? $update['description'] : __('Enter a description for this group', 'wpcf'));
    if (!$update) {
        $form['description']['#attributes']['data-label'] = addcslashes(__('Enter a description for this group', 'wpcf'), '"');
        $form['description']['#attributes']['onfocus'] = 'if (jQuery(this).val() == jQuery(this).data(\'label\')) { jQuery(this).val(\'\'); }';
        $form['description']['#attributes']['onblur'] = 'if (jQuery(this).val() == \'\') { jQuery(this).val(jQuery(this).data(\'label\')) }';
    }
    // Show Fields for
    global $wp_roles;
    $options = array();
    $users_currently_supported = array();
    $form_types = array();
    foreach ($wp_roles->role_names as $role => $name) {
        $options[$role]['#name'] = 'wpcf[group][supports][' . $role . ']';
        $options[$role]['#title'] = ucwords($role);
        $options[$role]['#default_value'] = $update && !empty($update['show_for']) && in_array($role, $update['show_for']) ? 1 : 0;
        $options[$role]['#value'] = $role;
        $options[$role]['#inline'] = TRUE;
        $options[$role]['#suffix'] = '<br />';
        $options[$role]['#id'] = 'wpcf-form-groups-show-for-' . $role;
        $options[$role]['#attributes'] = array('class' => 'wpcf-form-groups-support-post-type');
        if ($update && !empty($update['show_for']) && in_array($role, $update['show_for'])) {
            $users_currently_supported[] = ucwords($role);
        }
    }
    if (empty($users_currently_supported)) {
        $users_currently_supported[] = __('Displayed for all users roles', 'wpcf');
    }
    /*
     * Show for FILTER
     */
    $temp = array('#type' => 'checkboxes', '#options' => $options, '#name' => 'wpcf[group][supports]', '#inline' => true);
    /*
     * 
     * Here we use unique function for all filters
     * Since Types 1.1.4
     */
    $form_users = _wpcf_filter_wrap('custom_post_types', __('Show For:', 'wpcf'), implode(', ', $users_currently_supported), __('Displayed for all users roles', 'wpcf'), $temp);
    /*
     * Now starting form
     */
    $access_notification = '';
    if (function_exists('wpcf_access_register_caps')) {
        $access_notification = '<div class="message custom wpcf-notif"><span class="wpcf-notif-congrats">' . __('This groups visibility is also controlled by the Access plugin.', 'wpcf') . '</span></div>';
    }
    $form['supports-table-open'] = array('#type' => 'markup', '#markup' => '<table class="widefat"><thead><tr><th>' . __('Where to display this group', 'wpcf') . '</th></tr></thead><tbody><tr><td>' . '<p>' . __('Each usermeta group can display different fields for user roles.', 'wpcf') . $access_notification . '</p>');
    /*
     * Join filter forms
     */
    // User Roles
    $form['p_wrap_1_' . wpcf_unique_id(serialize($form_users))] = array('#type' => 'markup', '#markup' => '<p class="wpcf-filter-wrap">');
    $form = $form + $form_users;
    $form['supports-table-close'] = array('#type' => 'markup', '#markup' => '</td></tr></tbody></table><br />');
    /** Admin styles**/
    $form['adminstyles-table-open'] = array('#type' => 'markup', '#markup' => '<table class="widefat" id="wpcf-admin-styles-box"><thead><tr><th>' . __('Styling Editor', 'wpcf') . '</th></tr></thead><tbody><tr><td>' . '<p>' . __('Customize Fields for admin panel.', 'wpcf') . '</p>');
    $admin_styles_value = $preview_profile = $edit_profile = '';
    if (isset($update['admin_styles'])) {
        $admin_styles_value = $update['admin_styles'];
    }
    $temp = '';
    if ($update) {
        require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
        require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta.php';
        require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
        require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta-post.php';
        $user_id = wpcf_usermeta_get_user();
        $preview_profile = wpcf_usermeta_preview_profile($user_id, $update, 1);
        $group = $update;
        $group['fields'] = wpcf_admin_usermeta_process_fields($user_id, $group['fields'], true, false);
        $edit_profile = wpcf_admin_render_fields($group, $user_id, 1);
        add_action('admin_enqueue_scripts', 'wpcf_admin_fields_form_fix_styles', PHP_INT_MAX);
    }
    $temp[] = array('#type' => 'radio', '#suffix' => '<br />', '#value' => 'edit_mode', '#title' => 'Edit mode', '#name' => 'wpcf[group][preview]', '#default_value' => '', '#before' => '<div class="wpcf-admin-css-preview-style-edit">', '#inline' => true, '#attributes' => array('onclick' => 'changePreviewHtml(\'editmode\')', 'checked' => 'checked'));
    $temp[] = array('#type' => 'radio', '#title' => 'Read Only', '#name' => 'wpcf[group][preview]', '#default_value' => '', '#after' => '</div>', '#inline' => true, '#attributes' => array('onclick' => 'changePreviewHtml(\'readonly\')'));
    $temp[] = array('#type' => 'textarea', '#name' => 'wpcf[group][admin_html_preview]', '#inline' => true, '#value' => '', '#id' => 'wpcf-form-groups-admin-html-preview', '#before' => '<h3>Field group HTML</h3>');
    $temp[] = array('#type' => 'textarea', '#name' => 'wpcf[group][admin_styles]', '#inline' => true, '#value' => $admin_styles_value, '#default_value' => '', '#id' => 'wpcf-form-groups-css-fields-editor', '#after' => '
		<div class="wpcf-update-preview-btn"><input type="button" value="Update preview" onclick="wpcfPreviewHtml()" style="float:right;" class="button-secondary"></div>
		<h3>Field group preview</h3>
		<div id="wpcf-update-preview-div">Preview here</div>
		<script type="text/javascript">
			var wpcfReadOnly = ' . json_encode($preview_profile) . ';
			var wpcfEditMode = ' . json_encode($edit_profile) . ';
			var wpcfDefaultCss = ' . json_encode($admin_styles_value) . ';
		</script>
		', '#before' => '<h3>Your CSS</h3>');
    $admin_styles = _wpcf_filter_wrap('admin_styles', __('Admin styles for fields:', 'wpcf'), '', '', $temp, __('Open style editor', 'wpcf'));
    $form['p_wrap_1_' . wpcf_unique_id(serialize($admin_styles))] = array('#type' => 'markup', '#markup' => '<p class="wpcf-filter-wrap">');
    $form = $form + $admin_styles;
    $form['adminstyles-table-close'] = array('#type' => 'markup', '#markup' => '</td></tr></tbody></table><br />');
    /** End admin Styles **/
    // Group fields
    $form['fields_title'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Fields', 'wpcf') . '</h2>');
    $show_under_title = true;
    $form['ajax-response-open'] = array('#type' => 'markup', '#markup' => '<div id="wpcf-fields-sortable" class="ui-sortable">');
    // If it's update, display existing fields
    $existing_fields = array();
    if ($update && isset($update['fields'])) {
        foreach ($update['fields'] as $slug => $field) {
            $field['submitted_key'] = $slug;
            $field['group_id'] = $update['id'];
            $form_field = wpcf_fields_get_field_form_data($field['type'], $field);
            if (is_array($form_field)) {
                $form['draggable-open-' . rand()] = array('#type' => 'markup', '#markup' => '<div class="ui-draggable">');
                $form = $form + $form_field;
                $form['draggable-close-' . rand()] = array('#type' => 'markup', '#markup' => '</div>');
            }
            $existing_fields[] = $slug;
            $show_under_title = false;
        }
    }
    // Any new fields submitted but failed? (Don't double it)
    if (!empty($_POST['wpcf']['fields'])) {
        foreach ($_POST['wpcf']['fields'] as $key => $field) {
            if (in_array($key, $existing_fields)) {
                continue;
            }
            $field['submitted_key'] = $key;
            $form_field = wpcf_fields_get_field_form_data($field['type'], $field);
            if (is_array($form_field)) {
                $form['draggable-open-' . rand()] = array('#type' => 'markup', '#markup' => '<div class="ui-draggable">');
                $form = $form + $form_field;
                $form['draggable-close-' . rand()] = array('#type' => 'markup', '#markup' => '</div>');
            }
        }
        $show_under_title = false;
    }
    $form['ajax-response-close'] = array('#type' => 'markup', '#markup' => '</div>' . '<div id="wpcf-ajax-response"></div>');
    if ($show_under_title) {
        $form['fields_title']['#markup'] = $form['fields_title']['#markup'] . '<div id="wpcf-fields-under-title">' . __('There are no fields in this group. To add a field, click on the field buttons at the right.', 'wpcf') . '</div>';
    }
    // If update, create ID field
    if ($update) {
        $form['group_id'] = array('#type' => 'hidden', '#name' => 'group_id', '#value' => $update['id'], '#forced_value' => true);
    }
    $form['submit'] = array('#type' => 'submit', '#name' => 'save', '#value' => __('Save', 'wpcf'), '#attributes' => array('class' => 'button-primary wpcf-disabled-on-submit'));
    // Close main div
    $form['close-sidebar'] = array('#type' => 'markup', '#markup' => '</div>');
    wpcf_admin_add_js_settings('wpcf_filters_association_or', '\'' . __('This group will appear on %pt% edit pages where content belongs to taxonomy: %tx% or View Template is: %vt%', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcf_filters_association_and', '\'' . __('This group will appear on %pt% edit pages where content belongs to taxonomy: %tx% and View Template is: %vt%', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcf_filters_association_all_pages', '\'' . __('all', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcf_filters_association_all_taxonomies', '\'' . __('any', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcf_filters_association_all_templates', '\'' . __('any', 'wpcf') . '\'');
    // Add JS settings
    wpcf_admin_add_js_settings('wpcfFormUniqueValuesCheckText', '\'' . __('Warning: same values selected', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcfFormUniqueNamesCheckText', '\'' . __('Warning: field name already used', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcfFormUniqueSlugsCheckText', '\'' . __('Warning: field slug already used', 'wpcf') . '\'');
    return $form;
}
 /**
  * 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;
 }
/**
 * Generates form data.
 */
function wpcf_admin_fields_form()
{
    wpcf_admin_add_js_settings('wpcf_nonce_toggle_group', '\'' . wp_create_nonce('group_form_collapsed') . '\'');
    wpcf_admin_add_js_settings('wpcf_nonce_toggle_fieldset', '\'' . wp_create_nonce('form_fieldset_toggle') . '\'');
    $default = array();
    // If it's update, get data
    $update = false;
    if (isset($_REQUEST['group_id'])) {
        $update = wpcf_admin_fields_get_group(intval($_REQUEST['group_id']));
        if (empty($update)) {
            $update = false;
            wpcf_admin_message(sprintf(__("Group with ID %d do not exist", 'wpcf'), intval($_REQUEST['group_id'])));
        } else {
            $update['fields'] = wpcf_admin_fields_get_fields_by_group($_REQUEST['group_id']);
            $update['post_types'] = wpcf_admin_get_post_types_by_group($_REQUEST['group_id']);
            $update['taxonomies'] = wpcf_admin_get_taxonomies_by_group($_REQUEST['group_id']);
            $update['templates'] = wpcf_admin_get_templates_by_group($_REQUEST['group_id']);
        }
    }
    $form = array();
    $form['#form']['callback'] = array('wpcf_admin_save_fields_groups_submit');
    // Form sidebars
    $form['open-sidebar'] = array('#type' => 'markup', '#markup' => '<div class="wpcf-form-fields-align-right">');
    $form['submit2'] = array('#type' => 'submit', '#name' => 'save', '#value' => __('Save', 'wpcf'), '#attributes' => array('class' => 'button-primary'));
    $form['fields'] = array('#type' => 'fieldset', '#title' => __('Available fields', 'wpcf'));
    // Get field types
    $fields_registered = wpcf_admin_fields_get_available_types();
    //    foreach (glob(WPCF_EMBEDDED_INC_ABSPATH . '/fields/*.php') as $filename) {
    foreach ($fields_registered as $filename => $data) {
        //        require_once $filename;
        //        if (function_exists('wpcf_fields_' . basename($filename, '.php'))) {
        //            $data = call_user_func('wpcf_fields_' . basename($filename, '.php'));
        //            if (isset($data['wp_version']) && wpcf_compare_wp_version($data['wp_version'],
        //                            '<')) {
        //                continue;
        //            }
        $form['fields'][basename($filename, '.php')] = array('#type' => 'markup', '#markup' => '<a href="' . admin_url('admin-ajax.php' . '?action=wpcf_ajax&amp;wpcf_action=fields_insert' . '&amp;field=' . basename($filename, '.php')) . '&amp;_wpnonce=' . wp_create_nonce('fields_insert') . '" ' . 'class="wpcf-fields-add-ajax-link button-secondary">' . $data['title'] . '</a> ');
        // Process JS
        if (!empty($data['group_form_js'])) {
            foreach ($data['group_form_js'] as $handle => $script) {
                if (isset($script['inline'])) {
                    add_action('admin_footer', $script['inline']);
                    continue;
                }
                $deps = !empty($script['deps']) ? $script['deps'] : array();
                $in_footer = !empty($script['in_footer']) ? $script['in_footer'] : false;
                wp_register_script($handle, $script['src'], $deps, WPCF_VERSION, $in_footer);
                wp_enqueue_script($handle);
            }
        }
        // Process CSS
        if (!empty($data['group_form_css'])) {
            foreach ($data['group_form_css'] as $handle => $script) {
                if (isset($script['src'])) {
                    $deps = !empty($script['deps']) ? $script['deps'] : array();
                    wp_enqueue_style($handle, $script['src'], $deps, WPCF_VERSION);
                } else {
                    if (isset($script['inline'])) {
                        add_action('admin_head', $script['inline']);
                    }
                }
            }
        }
        //        }
    }
    // Get fields created by user
    $fields = wpcf_admin_fields_get_fields(true, true);
    if (!empty($fields)) {
        $form['fields-existing'] = array('#type' => 'fieldset', '#title' => __('User created fields', 'wpcf'), '#id' => 'wpcf-form-groups-user-fields');
        foreach ($fields as $key => $field) {
            if (isset($update['fields']) && array_key_exists($key, $update['fields'])) {
                continue;
            }
            if (!empty($field['data']['removed_from_history'])) {
                continue;
            }
            $form['fields-existing'][$key] = array('#type' => 'markup', '#markup' => '<div id="wpcf-user-created-fields-wrapper-' . $field['id'] . '" style="float:left; margin-right: 10px;"><a href="' . admin_url('admin-ajax.php' . '?action=wpcf_ajax' . '&amp;wpcf_action=fields_insert_existing' . '&amp;field=' . $field['id']) . '&amp;_wpnonce=' . wp_create_nonce('fields_insert_existing') . '" ' . 'class="wpcf-fields-add-ajax-link button-secondary" onclick="jQuery(this).parent().fadeOut();">' . htmlspecialchars(stripslashes($field['name'])) . '</a>' . '<a href="' . admin_url('admin-ajax.php' . '?action=wpcf_ajax' . '&amp;wpcf_action=remove_from_history' . '&amp;field_id=' . $field['id']) . '&amp;_wpnonce=' . wp_create_nonce('remove_from_history') . '&amp;wpcf_warning=' . sprintf(__('Are you sure that you want to remove field %s from history?', 'wpcf'), htmlspecialchars(stripslashes($field['name']))) . '&amp;wpcf_ajax_update=wpcf-user-created-fields-wrapper-' . $field['id'] . '" title="' . sprintf(__('Remove field %s', 'wpcf'), htmlspecialchars(stripslashes($field['name']))) . '" class="wpcf-ajax-link"><img src="' . WPCF_RES_RELPATH . '/images/delete-2.png" style="postion:absolute;margin-top:5px;margin-left:-4px;" /></a></div>');
        }
    }
    $form['close-sidebar'] = array('#type' => 'markup', '#markup' => '</div>');
    // Group data
    $form['open-main'] = array('#type' => 'markup', '#markup' => '<div id="wpcf-form-fields-main">');
    $form['title'] = array('#type' => 'textfield', '#name' => 'wpcf[group][name]', '#id' => 'wpcf-group-name', '#value' => $update ? $update['name'] : __('Enter group title', 'wpcf'), '#inline' => true, '#attributes' => array('style' => 'width:100%;margin-bottom:10px;'), '#validate' => array('required' => array('value' => true)));
    if (!$update) {
        $form['title']['#attributes']['onfocus'] = 'if (jQuery(this).val() == \'' . __('Enter group title', 'wpcf') . '\') { jQuery(this).val(\'\'); }';
        $form['title']['#attributes']['onblur'] = 'if (jQuery(this).val() == \'\') { jQuery(this).val(\'' . __('Enter group title', 'wpcf') . '\') }';
    }
    $form['description'] = array('#type' => 'textarea', '#id' => 'wpcf-group-description', '#name' => 'wpcf[group][description]', '#value' => $update ? $update['description'] : __('Enter a description for this group', 'wpcf'));
    if (!$update) {
        $form['description']['#attributes']['onfocus'] = 'if (jQuery(this).val() == \'' . __('Enter a description for this group', 'wpcf') . '\') { jQuery(this).val(\'\'); }';
        $form['description']['#attributes']['onblur'] = 'if (jQuery(this).val() == \'\') { jQuery(this).val(\'' . __('Enter a description for this group', 'wpcf') . '\') }';
    }
    // Support post types and taxonomies
    $post_types = get_post_types('', 'objects');
    $options = array();
    $post_types_currently_supported = array();
    $form_types = array();
    foreach ($post_types as $post_type_slug => $post_type) {
        if (in_array($post_type_slug, array('attachment', 'revision', 'nav_menu_item', 'view', 'view-template')) || !$post_type->show_ui) {
            continue;
        }
        $options[$post_type_slug]['#name'] = 'wpcf[group][supports][' . $post_type_slug . ']';
        $options[$post_type_slug]['#title'] = $post_type->label;
        $options[$post_type_slug]['#default_value'] = $update && !empty($update['post_types']) && in_array($post_type_slug, $update['post_types']) ? 1 : 0;
        $options[$post_type_slug]['#value'] = $post_type_slug;
        $options[$post_type_slug]['#inline'] = TRUE;
        $options[$post_type_slug]['#suffix'] = '<br />';
        $options[$post_type_slug]['#id'] = 'wpcf-form-groups-support-post-type-' . $post_type_slug;
        $options[$post_type_slug]['#attributes'] = array('class' => 'wpcf-form-groups-support-post-type');
        if ($update && !empty($update['post_types']) && in_array($post_type_slug, $update['post_types'])) {
            $post_types_currently_supported[] = $post_type->label;
        }
    }
    if (empty($post_types_currently_supported)) {
        $post_types_currently_supported[] = __('Displayed on all content types', 'wpcf');
    }
    $post_types_no_currently_supported_txt = __('Post Types:', 'wpcf') . ' ' . __('Displayed on all content types', 'wpcf');
    $form_types = array('#type' => 'checkboxes', '#options' => $options, '#name' => 'wpcf[group][supports]', '#inline' => true, '#before' => '<span id="wpcf-group-form-update-types-ajax-response"' . ' style="font-style:italic;font-weight:bold;display:inline-block;">' . __('Post Types:', 'wpcf') . ' ' . implode(', ', $post_types_currently_supported) . '</span>' . '&nbsp;&nbsp;<a href="javascript:void(0);" style="line-height: 30px;"' . ' class="button-secondary" onclick="' . 'window.wpcfPostTypesText = new Array(); window.wpcfFormGroupsSupportPostTypesState = new Array(); ' . 'jQuery(this).next().slideToggle()' . '.find(\'.checkbox\').each(function(index){' . 'if (jQuery(this).is(\':checked\')) { ' . 'window.wpcfPostTypesText.push(jQuery(this).next().html()); ' . 'window.wpcfFormGroupsSupportPostTypesState.push(jQuery(this).attr(\'id\'));' . '}' . '});' . ' jQuery(this).css(\'visibility\', \'hidden\');">' . __('Edit', 'wpcf') . '</a>' . '<div class="hidden" id="wpcf-form-fields-post_types">', '#after' => '<a href="javascript:void(0);" style="line-height: 35px;" ' . 'class="button-primary wpcf-groups-form-ajax-update-post-types-ok"' . ' onclick="window.wpcfPostTypesText = new Array(); window.wpcfFormGroupsSupportPostTypesState = new Array(); ' . 'jQuery(this).parent().slideUp().find(\'.checkbox\').each(function(index){' . 'if (jQuery(this).is(\':checked\')) { ' . 'window.wpcfPostTypesText.push(jQuery(this).next().html()); ' . 'window.wpcfFormGroupsSupportPostTypesState.push(jQuery(this).attr(\'id\'));' . '}' . '});' . 'if (window.wpcfPostTypesText.length < 1) { ' . 'jQuery(\'#wpcf-group-form-update-types-ajax-response\').html(\'' . $post_types_no_currently_supported_txt . '\'); ' . '} else { jQuery(\'#wpcf-group-form-update-types-ajax-response\').html(\'' . __('Post Types:', 'wpcf') . ' \'+wpcfPostTypesText.join(\', \'));}' . ' jQuery(this).parent().parent().children(\'a\').css(\'visibility\', \'visible\');' . '">' . __('OK', 'wpcf') . '</a>&nbsp;' . '<a href="javascript:void(0);" style="line-height: 35px;" ' . 'class="button-secondary wpcf-groups-form-ajax-update-post-types-cancel"' . ' onclick="jQuery(this).parent().slideUp().find(\'input\').removeAttr(\'checked\');' . 'if (window.wpcfFormGroupsSupportPostTypesState.length > 0) { ' . 'for (var element in window.wpcfFormGroupsSupportPostTypesState) { ' . 'jQuery(\'#\'+window.wpcfFormGroupsSupportPostTypesState[element]).attr(\'checked\', \'checked\'); }}' . 'jQuery(\'#wpcf-group-form-update-types-ajax-response\').html(\'' . __('Post Types:', 'wpcf') . ' \'+window.wpcfPostTypesText.join(\', \'));' . ' jQuery(this).parent().parent().children(\'a\').css(\'visibility\', \'visible\');' . '">' . __('Cancel', 'wpcf') . '</a>' . '</div></div><br />');
    $taxonomies = get_taxonomies('', 'objects');
    $options = array();
    $tax_currently_supported = array();
    $form_tax = array();
    $form_tax_single = array();
    foreach ($taxonomies as $category_slug => $category) {
        if ($category_slug == 'nav_menu' || $category_slug == 'link_category' || $category_slug == 'post_format') {
            continue;
        }
        $terms = get_terms($category_slug, array('hide_empty' => false));
        if (!empty($terms)) {
            $options = array();
            $add_title = '<div class="taxonomy-title">' . $category->labels->name . '</div>';
            $title = '';
            foreach ($terms as $term) {
                $checked = 0;
                if ($update && !empty($update['taxonomies']) && array_key_exists($category_slug, $update['taxonomies'])) {
                    if (array_key_exists($term->term_id, $update['taxonomies'][$category_slug])) {
                        $checked = 1;
                        $tax_currently_supported[$term->term_id] = $title . $term->name;
                        $title = '';
                    }
                }
                $options[$term->term_id]['#name'] = 'wpcf[group][taxonomies][' . $category_slug . '][' . $term->term_id . ']';
                $options[$term->term_id]['#title'] = $term->name;
                $options[$term->term_id]['#default_value'] = $checked;
                $options[$term->term_id]['#value'] = $term->term_id;
                $options[$term->term_id]['#inline'] = true;
                $options[$term->term_id]['#prefix'] = $add_title;
                $options[$term->term_id]['#suffix'] = '<br />';
                $options[$term->term_id]['#id'] = 'wpcf-form-groups-support-tax-' . $term->term_id;
                $options[$term->term_id]['#attributes'] = array('class' => 'wpcf-form-groups-support-tax');
                $add_title = '';
            }
            $form_tax_single['taxonomies-' . $category_slug] = array('#type' => 'checkboxes', '#options' => $options, '#name' => 'wpcf[group][taxonomies][' . $category_slug . ']', '#suffix' => '<br />', '#inline' => true);
        }
    }
    if (empty($tax_currently_supported)) {
        $tax_currently_supported[] = __('Not Selected', 'wpcf');
    }
    $tax_no_currently_supported_txt = __('Terms:', 'wpcf') . ' ' . __('Not Selected', 'wpcf');
    $form_tax['taxonomies-open'] = array('#type' => 'markup', '#markup' => '<span id="wpcf-group-form-update-tax-ajax-response" ' . 'style="font-style:italic;font-weight:bold;display:inline-block;">' . __('Terms:', 'wpcf') . ' ' . implode(', ', $tax_currently_supported) . '</span>' . '&nbsp;&nbsp;<a href="javascript:void(0);" style="line-height: 30px;" ' . 'class="button-secondary" onclick="' . 'window.wpcfTaxText = new Array(); window.wpcfFormGroupsSupportTaxState = new Array(); ' . 'jQuery(this).next().slideToggle()' . '.find(\'.checkbox\').each(function(index){' . 'if (jQuery(this).is(\':checked\')) { ' . 'window.wpcfTaxText.push(jQuery(this).next().html()); ' . 'window.wpcfFormGroupsSupportTaxState.push(jQuery(this).attr(\'id\'));' . '}' . '});' . ' jQuery(this).css(\'visibility\', \'hidden\');">' . __('Edit', 'wpcf') . '</a>' . '<div class="hidden" id="wpcf-form-fields-taxonomies">');
    $form_tax = $form_tax + $form_tax_single;
    $form_tax['taxonomies-close'] = array('#type' => 'markup', '#markup' => '<a href="javascript:void(0);" style="line-height: 35px;" ' . 'class="button-primary wpcf-groups-form-ajax-update-tax-ok"' . ' onclick="window.wpcfTaxText = new Array(); window.wpcfFormGroupsSupportTaxState = new Array(); ' . 'jQuery(this).parent().slideUp().find(\'.checkbox\').each(function(index){' . 'if (jQuery(this).is(\':checked\')) { ' . 'window.wpcfTaxText.push(jQuery(this).next().html()); ' . 'window.wpcfFormGroupsSupportTaxState.push(jQuery(this).attr(\'id\'));' . '}' . '});' . 'if (window.wpcfTaxText.length < 1) { ' . 'jQuery(\'#wpcf-group-form-update-tax-ajax-response\').html(\'' . $tax_no_currently_supported_txt . '\'); ' . '} else { jQuery(\'#wpcf-group-form-update-tax-ajax-response\').html(\'' . __('Terms:', 'wpcf') . ' \'+wpcfTaxText.join(\', \'));' . '}' . ' jQuery(this).parent().parent().children(\'a\').css(\'visibility\', \'visible\');' . '">' . __('OK', 'wpcf') . '</a>&nbsp;' . '<a href="javascript:void(0);" style="line-height: 35px;" ' . 'class="button-secondary wpcf-groups-form-ajax-update-tax-cancel"' . ' onclick="jQuery(this).parent().slideUp().find(\'input\').removeAttr(\'checked\');' . 'if (window.wpcfFormGroupsSupportTaxState.length > 0) { ' . 'for (var element in window.wpcfFormGroupsSupportTaxState) { ' . 'jQuery(\'#\'+window.wpcfFormGroupsSupportTaxState[element]).attr(\'checked\', \'checked\'); }}' . 'jQuery(\'#wpcf-group-form-update-tax-ajax-response\').html(\'' . __('Terms:', 'wpcf') . ' \'+window.wpcfTaxText.join(\', \'));' . ' jQuery(this).parent().parent().children(\'a\').css(\'visibility\', \'visible\');' . '">' . __('Cancel', 'wpcf') . '</a>' . '</div><br />');
    $form['supports-table-open'] = array('#type' => 'markup', '#markup' => '<table class="widefat"><thead><tr><th>' . __('Where to display this group', 'wpcf') . '</th></tr></thead><tbody><tr><td>' . __('Each custom fields group can display on different content types or different taxonomy.', 'wpcf') . '<br />');
    $form['types'] = $form_types;
    $form = $form + $form_tax;
    // Choose templates
    $templates = get_page_templates();
    $templates_views = get_posts('post_type=view-template&numberposts=-1&status=publish');
    $options = array();
    $options['default-template'] = array('#title' => __('Default Template'), '#default_value' => !empty($update['templates']) && in_array('default', $update['templates']), '#name' => 'wpcf[group][templates][]', '#value' => 'default', '#inline' => true, '#after' => '<br />');
    foreach ($templates as $template_name => $template_filename) {
        $options[$template_filename] = array('#title' => $template_name, '#default_value' => !empty($update['templates']) && in_array($template_filename, $update['templates']), '#name' => 'wpcf[group][templates][]', '#value' => $template_filename, '#inline' => true, '#after' => '<br />');
    }
    foreach ($templates_views as $template_view) {
        $options[$template_view->post_name] = array('#title' => 'View Template ' . $template_view->post_title, '#default_value' => !empty($update['templates']) && in_array($template_view->ID, $update['templates']), '#name' => 'wpcf[group][templates][]', '#value' => $template_view->ID, '#inline' => true, '#after' => '<br />');
        $templates_view_list_text[$template_view->ID] = $template_view->post_title;
    }
    $text = '';
    $empty_txt = __('Not Selected', 'wpcf');
    if (!empty($update['templates'])) {
        $text = array();
        $templates = array_flip($templates);
        foreach ($update['templates'] as $template) {
            if ($template == 'default') {
                $template = __('Default Template');
            } else {
                if (strpos($template, '.php') !== false) {
                    $template = $templates[$template];
                } else {
                    $template = 'View Template ' . $templates_view_list_text[$template];
                }
            }
            $text[] = $template;
        }
        $text = implode(', ', $text);
    } else {
        $text = __('Not Selected', 'wpcf');
    }
    $form['templates'] = array('#type' => 'checkboxes', '#name' => 'wpcf[group][templates]', '#options' => $options, '#inline' => true);
    $form['templates'] = wpcf_admin_fields_form_nested_elements('templates', $form['templates'], __('Content templates:', 'wpcf'), $text, $empty_txt);
    $count = 0;
    $count += !empty($update['post_types']) ? 1 : 0;
    $count += !empty($update['taxonomies']) ? 1 : 0;
    $count += !empty($update['templates']) ? 1 : 0;
    $display = $count > 1 ? '' : ' style="display:none;"';
    $form['filters_association'] = array('#type' => 'radios', '#name' => 'wpcf[group][filters_association]', '#id' => 'wpcf-fields-form-filters-association', '#options' => array(__('Display this group when ANY of the above conditions is met', 'wpcf') => 'any', __('Display this group when ALL the above conditions is met', 'wpcf') => 'all'), '#default_value' => !empty($update['filters_association']) ? $update['filters_association'] : 'any', '#inline' => true, '#before' => '<div id="wpcf-fields-form-filters-association-form"' . $display . '>', '#after' => '<div id="wpcf-fields-form-filters-association-summary" style="margin-top:10px;font-style:italic;"></div></div>');
    wpcf_admin_add_js_settings('wpcf_filters_association_or', '\'' . __('This group will appear on %pt% edit pages where content belongs to taxonomy: %tx% or View Template is: %vt%', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcf_filters_association_and', '\'' . __('This group will appear on %pt% edit pages where content belongs to taxonomy: %tx% and View Template is: %vt%', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcf_filters_association_all_pages', '\'' . __('all', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcf_filters_association_all_taxonomies', '\'' . __('any', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcf_filters_association_all_templates', '\'' . __('any', 'wpcf') . '\'');
    $form['supports-table-close'] = array('#type' => 'markup', '#markup' => '</td></tr></tbody></table><br />');
    // Group fields
    $form['fields_title'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Fields', 'wpcf') . '</h2>');
    $show_under_title = true;
    $form['ajax-response-open'] = array('#type' => 'markup', '#markup' => '<div id="wpcf-fields-sortable" class="ui-sortable">');
    // If it's update, display existing fields
    $existing_fields = array();
    if ($update && isset($update['fields'])) {
        foreach ($update['fields'] as $slug => $field) {
            $field['submitted_key'] = $slug;
            $field['group_id'] = $update['id'];
            $form_field = wpcf_fields_get_field_form_data($field['type'], $field);
            if (is_array($form_field)) {
                $form['draggable-open-' . rand()] = array('#type' => 'markup', '#markup' => '<div class="ui-draggable">');
                $form = $form + $form_field;
                $form['draggable-close-' . rand()] = array('#type' => 'markup', '#markup' => '</div>');
            }
            $existing_fields[] = $slug;
            $show_under_title = false;
        }
    }
    // Any new fields submitted but failed? (Don't double it)
    if (!empty($_POST['wpcf']['fields'])) {
        foreach ($_POST['wpcf']['fields'] as $key => $field) {
            if (in_array($key, $existing_fields)) {
                continue;
            }
            $field['submitted_key'] = $key;
            $form_field = wpcf_fields_get_field_form_data($field['type'], $field);
            if (is_array($form_field)) {
                $form['draggable-open-' . rand()] = array('#type' => 'markup', '#markup' => '<div class="ui-draggable">');
                $form = $form + $form_field;
                $form['draggable-close-' . rand()] = array('#type' => 'markup', '#markup' => '</div>');
            }
        }
        $show_under_title = false;
    }
    $form['ajax-response-close'] = array('#type' => 'markup', '#markup' => '</div>' . '<div id="wpcf-ajax-response"></div>');
    if ($show_under_title) {
        $form['fields_title']['#markup'] = $form['fields_title']['#markup'] . '<div id="wpcf-fields-under-title">' . __('There are no fields in this group. To add a field, click on the field buttons at the right.', 'wpcf') . '</div>';
    }
    // If update, create ID field
    if ($update) {
        $form['group_id'] = array('#type' => 'hidden', '#name' => 'group_id', '#value' => $update['id'], '#forced_value' => true);
    }
    $form['submit'] = array('#type' => 'submit', '#name' => 'save', '#value' => __('Save', 'wpcf'), '#attributes' => array('class' => 'button-primary'));
    // Close main div
    $form['close-sidebar'] = array('#type' => 'markup', '#markup' => '</div>');
    $form = apply_filters('wpcf_form_fields', $form);
    // Add JS settings
    wpcf_admin_add_js_settings('wpcfFormUniqueValuesCheckText', '\'' . __('Warning: same values selected', 'wpcf') . '\'');
    return $form;
}
Exemple #5
0
/**
 * Update an existing field group or create new one.
 *
 * @param array $group
 * @param string $post_type
 * @return int Group ID.
 */
function wpcf_admin_fields_save_group($group, $post_type = TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, $which_fields = 'none')
{
    if (!isset($group['name']) || empty($group['name'])) {
        return false;
    }
    $post = array('post_status' => 'publish', 'post_type' => $post_type, 'post_title' => sanitize_text_field($group['name']), 'post_name' => sanitize_text_field($group['name']), 'post_content' => empty($group['description']) ? '' : $group['description']);
    if (empty($post['post_title'])) {
        wpcf_admin_message(__('Please set name', 'wpcf'), 'error');
        return false;
    }
    $update = false;
    if (isset($group['id']) && !empty($group['id'])) {
        $update = true;
        $post_to_update = get_post($group['id']);
        if (empty($post_to_update) || $post_to_update->post_type != $post_type) {
            return false;
        }
        $post['ID'] = $post_to_update->ID;
        $post['post_status'] = $post_to_update->post_status;
    }
    if ($update) {
        $group_id = wp_update_post($post);
        if (!$group_id) {
            return false;
        }
        update_post_meta($group_id, TOOLSET_EDIT_LAST, time());
    } else {
        $group_id = wp_insert_post($post, true);
        if (is_wp_error($group_id)) {
            return false;
        }
    }
    if (isset($group['admin_styles'])) {
        wpcf_admin_fields_save_group_admin_styles($group_id, $group['admin_styles']);
    }
    if (!empty($group['filters_association'])) {
        update_post_meta($group_id, '_wp_types_group_filters_association', $group['filters_association']);
    } else {
        delete_post_meta($group_id, '_wp_types_group_filters_association');
    }
    // WPML register strings
    if (function_exists('icl_register_string')) {
        wpcf_translate_register_string('plugin Types', 'group ' . $group_id . ' name', $group['name']);
        wpcf_translate_register_string('plugin Types', 'group ' . $group_id . ' description', $group['description']);
    }
    // admin message
    wpcf_admin_fields_save_message($update, $which_fields);
    return $group_id;
}
/**
 * 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;
}
 /**
  * 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');
     }
 }
/**
 * 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;
}
/**
 * Generates form data.
 */
function wpcf_admin_fields_form()
{
    wpcf_admin_add_js_settings('wpcf_nonce_toggle_group', '\'' . wp_create_nonce('group_form_collapsed') . '\'');
    wpcf_admin_add_js_settings('wpcf_nonce_toggle_fieldset', '\'' . wp_create_nonce('form_fieldset_toggle') . '\'');
    $default = array();
    global $wpcf_button_style;
    global $wpcf_button_style30;
    global $wpcf;
    // If it's update, get data
    $update = false;
    if (isset($_REQUEST['group_id'])) {
        $update = wpcf_admin_fields_get_group(intval($_REQUEST['group_id']));
        if (empty($update)) {
            $update = false;
            wpcf_admin_message(sprintf(__("Group with ID %d do not exist", 'wpcf'), intval($_REQUEST['group_id'])));
        } else {
            $update['fields'] = wpcf_admin_fields_get_fields_by_group(sanitize_text_field($_REQUEST['group_id']), 'slug', false, true);
            $update['post_types'] = wpcf_admin_get_post_types_by_group(sanitize_text_field($_REQUEST['group_id']));
            $update['taxonomies'] = wpcf_admin_get_taxonomies_by_group(sanitize_text_field($_REQUEST['group_id']));
            $update['templates'] = wpcf_admin_get_templates_by_group(sanitize_text_field($_REQUEST['group_id']));
            $update['admin_styles'] = wpcf_admin_get_groups_admin_styles_by_group(sanitize_text_field($_REQUEST['group_id']));
        }
    }
    $form = array();
    $form['#form']['callback'] = array('wpcf_admin_save_fields_groups_submit');
    // Form sidebars
    $form['open-sidebar'] = array('#type' => 'markup', '#markup' => '<div class="wpcf-form-fields-align-right">');
    // Set help icon
    $form['help-icon'] = array('#type' => 'markup', '#markup' => '<div class="wpcf-admin-fields-help"><img src="' . WPCF_EMBEDDED_RELPATH . '/common/res/images/question.png" style="position:relative;top:2px;" />&nbsp;<a href="http://wp-types.com/documentation/user-guides/using-custom-fields/?utm_source=typesplugin&utm_medium=help&utm_term=fields-help&utm_content=fields-editor&utm_campaign=types" target="_blank">' . __('Custom fields help', 'wpcf') . '</a></div>');
    $form['submit2'] = array('#type' => 'submit', '#name' => 'save', '#value' => __('Save', 'wpcf'), '#attributes' => array('class' => 'button-primary wpcf-disabled-on-submit'));
    $form['fields'] = array('#type' => 'fieldset', '#title' => __('Available fields', 'wpcf'));
    // Get field types
    $fields_registered = wpcf_admin_fields_get_available_types();
    foreach ($fields_registered as $filename => $data) {
        $form['fields'][basename($filename, '.php')] = array('#type' => 'markup', '#markup' => '<a href="' . admin_url('admin-ajax.php' . '?action=wpcf_ajax&amp;wpcf_action=fields_insert' . '&amp;field=' . basename($filename, '.php') . '&amp;page=wpcf-edit') . '&amp;_wpnonce=' . wp_create_nonce('fields_insert') . '" ' . 'class="wpcf-fields-add-ajax-link button-secondary">' . $data['title'] . '</a> ');
        // Process JS
        if (!empty($data['group_form_js'])) {
            foreach ($data['group_form_js'] as $handle => $script) {
                if (isset($script['inline'])) {
                    add_action('admin_footer', $script['inline']);
                    continue;
                }
                $deps = !empty($script['deps']) ? $script['deps'] : array();
                $in_footer = !empty($script['in_footer']) ? $script['in_footer'] : false;
                wp_register_script($handle, $script['src'], $deps, WPCF_VERSION, $in_footer);
                wp_enqueue_script($handle);
            }
        }
        // Process CSS
        if (!empty($data['group_form_css'])) {
            foreach ($data['group_form_css'] as $handle => $script) {
                if (isset($script['src'])) {
                    $deps = !empty($script['deps']) ? $script['deps'] : array();
                    wp_enqueue_style($handle, $script['src'], $deps, WPCF_VERSION);
                } else {
                    if (isset($script['inline'])) {
                        add_action('admin_head', $script['inline']);
                    }
                }
            }
        }
    }
    // Get fields created by user
    $fields = wpcf_admin_fields_get_fields(true, true);
    if (!empty($fields)) {
        $form['fields-existing'] = array('#type' => 'fieldset', '#title' => __('User created fields', 'wpcf'), '#id' => 'wpcf-form-groups-user-fields');
        foreach ($fields as $key => $field) {
            if (isset($update['fields']) && array_key_exists($key, $update['fields'])) {
                continue;
            }
            if (!empty($field['data']['removed_from_history'])) {
                continue;
            }
            $form['fields-existing'][$key] = array('#type' => 'markup', '#markup' => '<div id="wpcf-user-created-fields-wrapper-' . $field['id'] . '" style="float:left; margin-right: 10px;"><a href="' . admin_url('admin-ajax.php' . '?action=wpcf_ajax' . '&amp;wpcf_action=fields_insert_existing' . '&amp;page=wpcf-edit' . '&amp;field=' . $field['id']) . '&amp;_wpnonce=' . wp_create_nonce('fields_insert_existing') . '" ' . 'class="wpcf-fields-add-ajax-link button-secondary" onclick="jQuery(this).parent().fadeOut();" ' . ' data-slug="' . $field['id'] . '">' . htmlspecialchars(stripslashes($field['name'])) . '</a>' . '<a href="' . admin_url('admin-ajax.php' . '?action=wpcf_ajax' . '&amp;wpcf_action=remove_from_history' . '&amp;field_id=' . $field['id']) . '&amp;_wpnonce=' . wp_create_nonce('remove_from_history') . '&amp;wpcf_warning=' . sprintf(__('Are you sure that you want to remove field %s from history?', 'wpcf'), htmlspecialchars(stripslashes($field['name']))) . '&amp;wpcf_ajax_update=wpcf-user-created-fields-wrapper-' . $field['id'] . '" title="' . sprintf(__('Remove field %s', 'wpcf'), htmlspecialchars(stripslashes($field['name']))) . '" class="wpcf-ajax-link"><img src="' . WPCF_RES_RELPATH . '/images/delete-2.png" style="postion:absolute;margin-top:5px;margin-left:-4px;" /></a></div>');
        }
    }
    $form['close-sidebar'] = array('#type' => 'markup', '#markup' => '</div>');
    // Group data
    $form['open-main'] = array('#type' => 'markup', '#markup' => '<div id="wpcf-form-fields-main">');
    $form['title'] = array('#type' => 'textfield', '#name' => 'wpcf[group][name]', '#id' => 'wpcf-group-name', '#value' => $update ? $update['name'] : '', '#inline' => true, '#attributes' => array('style' => 'width:100%;margin-bottom:10px;', 'placeholder' => __('Enter group title', 'wpcf')), '#validate' => array('required' => array('value' => true)));
    $form['description'] = array('#type' => 'textarea', '#id' => 'wpcf-group-description', '#name' => 'wpcf[group][description]', '#value' => $update ? $update['description'] : '', '#attributes' => array('placeholder' => __('Enter a description for this group', 'wpcf')));
    /**
     *
     * FILTER BOX
     * Since Types 1.2 we moved JS to /embedded/resources/js/custom-fields-form-filter.js
     *
     */
    // Support post types and taxonomies
    $post_types = get_post_types('', 'objects');
    $options = array();
    $post_types_currently_supported = array();
    $form_types = array();
    foreach ($post_types as $post_type_slug => $post_type) {
        if (in_array($post_type_slug, $wpcf->excluded_post_types) || !$post_type->show_ui) {
            continue;
        }
        $options[$post_type_slug]['#name'] = 'wpcf[group][supports][' . $post_type_slug . ']';
        $options[$post_type_slug]['#title'] = $post_type->label;
        $options[$post_type_slug]['#default_value'] = $update && !empty($update['post_types']) && in_array($post_type_slug, $update['post_types']) ? 1 : 0;
        $options[$post_type_slug]['#value'] = $post_type_slug;
        $options[$post_type_slug]['#inline'] = TRUE;
        $options[$post_type_slug]['#suffix'] = '<br />';
        $options[$post_type_slug]['#id'] = 'wpcf-form-groups-support-post-type-' . $post_type_slug;
        $options[$post_type_slug]['#attributes'] = array('class' => 'wpcf-form-groups-support-post-type');
        if ($update && !empty($update['post_types']) && in_array($post_type_slug, $update['post_types'])) {
            $post_types_currently_supported[] = $post_type->label;
        }
    }
    if (empty($post_types_currently_supported)) {
        $post_types_currently_supported[] = __('Displayed on all content types', 'wpcf');
    }
    /**
     * POST TYPE FILTER
     */
    $temp = array('#type' => 'checkboxes', '#options' => $options, '#name' => 'wpcf[group][supports]', '#inline' => true);
    /**
     * Here we use unique function for all filters
     * Since Types 1.2
     */
    $form_types = _wpcf_filter_wrap('custom_post_types', __('Post Types:', 'wpcf'), implode(',', $post_types_currently_supported), __('Displayed on all content types', 'wpcf'), $temp);
    /**
     * TAXONOMIES FILTER QUERY
     */
    $taxonomies = apply_filters('wpcf_group_form_filter_taxonomies', get_taxonomies('', 'objects'));
    $options = array();
    $tax_currently_supported = array();
    $form_tax = array();
    $form_tax_single = array();
    /**
     * Filter toxonomies
     */
    foreach ($taxonomies as $category_slug => $category) {
        if ($category_slug == 'nav_menu' || $category_slug == 'link_category' || $category_slug == 'post_format') {
            continue;
        }
        $terms = apply_filters('wpcf_group_form_filter_terms', get_terms($category_slug, array('hide_empty' => false)));
        if (!empty($terms)) {
            $options = array();
            $add_title = '<div class="taxonomy-title">' . $category->labels->name . '</div>';
            $title = '';
            foreach ($terms as $term) {
                $checked = 0;
                if ($update && !empty($update['taxonomies']) && array_key_exists($category_slug, $update['taxonomies'])) {
                    if (array_key_exists($term->term_taxonomy_id, $update['taxonomies'][$category_slug])) {
                        $checked = 1;
                        $tax_currently_supported[$term->term_taxonomy_id] = $term->name;
                        $title = '';
                    }
                }
                $options[$term->term_taxonomy_id]['#name'] = 'wpcf[group][taxonomies][' . $category_slug . '][' . $term->term_taxonomy_id . ']';
                $options[$term->term_taxonomy_id]['#title'] = $term->name;
                $options[$term->term_taxonomy_id]['#default_value'] = $checked;
                $options[$term->term_taxonomy_id]['#value'] = $term->term_taxonomy_id;
                $options[$term->term_taxonomy_id]['#inline'] = true;
                $options[$term->term_taxonomy_id]['#prefix'] = $add_title;
                $options[$term->term_taxonomy_id]['#suffix'] = '<br />';
                $options[$term->term_taxonomy_id]['#id'] = 'wpcf-form-groups-support-tax-' . $term->term_taxonomy_id;
                $options[$term->term_taxonomy_id]['#attributes'] = array('class' => 'wpcf-form-groups-support-tax');
                $add_title = '';
            }
            $form_tax_single['taxonomies-' . $category_slug] = array('#type' => 'checkboxes', '#options' => $options, '#name' => 'wpcf[group][taxonomies][' . $category_slug . ']', '#suffix' => '<br />', '#inline' => true);
        }
    }
    if (empty($tax_currently_supported)) {
        $tax_currently_supported[] = __('Not Selected', 'wpcf');
    }
    /**
     * Since Types 1.2 we use unique function
     */
    $form_tax = _wpcf_filter_wrap('custom_taxonomies', __('Terms:', 'wpcf'), implode(', ', array_values($tax_currently_supported)), __('Not Selected', 'wpcf'), $form_tax_single);
    /**
     * TEMPLATES
     */
    // Choose templates
    $templates = get_page_templates();
    $templates_views = get_posts('post_type=view-template&numberposts=-1&status=publish');
    $options = array();
    $options['default-template'] = array('#title' => __('Default Template'), '#default_value' => !empty($update['templates']) && in_array('default', $update['templates']), '#name' => 'wpcf[group][templates][]', '#value' => 'default', '#inline' => true, '#after' => '<br />');
    foreach ($templates as $template_name => $template_filename) {
        $options[$template_filename] = array('#title' => $template_name, '#default_value' => !empty($update['templates']) && in_array($template_filename, $update['templates']), '#name' => 'wpcf[group][templates][]', '#value' => $template_filename, '#inline' => true, '#after' => '<br />');
    }
    foreach ($templates_views as $template_view) {
        $options[$template_view->post_name] = array('#title' => 'View Template ' . $template_view->post_title, '#default_value' => !empty($update['templates']) && in_array($template_view->ID, $update['templates']), '#name' => 'wpcf[group][templates][]', '#value' => $template_view->ID, '#inline' => true, '#after' => '<br />');
        $templates_view_list_text[$template_view->ID] = $template_view->post_title;
    }
    $text = '';
    if (!empty($update['templates'])) {
        $text = array();
        $templates = array_flip($templates);
        foreach ($update['templates'] as $template) {
            if ($template == 'default') {
                $template = __('Default Template');
            } else {
                if (strpos($template, '.php') !== false) {
                    $template = $templates[$template];
                } else {
                    $template = 'View Template ' . $templates_view_list_text[$template];
                }
            }
            $text[] = $template;
        }
        $text = implode(', ', $text);
    } else {
        $text = __('Not Selected', 'wpcf');
    }
    // Add class
    foreach ($options as $_k => $_option) {
        $options[$_k]['#attributes'] = array('class' => 'wpcf-form-groups-support-templates');
    }
    $form_templates = array('#type' => 'checkboxes', '#name' => 'wpcf[group][templates]', '#options' => $options, '#inline' => true);
    /**
     * Since Types 1.2 we use unique function
     */
    $form_templates = _wpcf_filter_wrap('templates', __('Templates:', 'wpcf'), $text, __('Not Selected', 'wpcf'), $form_templates);
    /**
     * Now starting form
     */
    $form['supports-table-open'] = array('#type' => 'markup', '#markup' => '<table class="widefat"><thead><tr><th>' . __('Where to display this group', 'wpcf') . '</th></tr></thead><tbody><tr><td>' . '<p>' . __('Each custom fields group can display on different content types or different taxonomy.', 'wpcf') . '</p>');
    /**
     * Join filter forms
     */
    // Types
    $form['p_wrap_1_' . wpcf_unique_id(serialize($form_types))] = array('#type' => 'markup', '#markup' => '<p class="wpcf-filter-wrap">');
    $form = $form + $form_types;
    // Terms
    $form['p_wrap_2_' . wpcf_unique_id(serialize($form_tax))] = array('#type' => 'markup', '#markup' => '</p><p class="wpcf-filter-wrap">');
    $form = $form + $form_tax;
    // Templates
    $form['p_wrap_3_' . wpcf_unique_id(serialize($form_templates))] = array('#type' => 'markup', '#markup' => '</p><p class="wpcf-filter-wrap">');
    $form = $form + $form_templates;
    $form['p_wrap_4_' . wpcf_unique_id(serialize($form_templates))] = array('#type' => 'markup', '#markup' => '</p>');
    /**
     * TODO Code from now on should be revised
     */
    $count = 0;
    $count += !empty($update['post_types']) ? 1 : 0;
    $count += !empty($update['taxonomies']) ? 1 : 0;
    $count += !empty($update['templates']) ? 1 : 0;
    $display = $count > 1 ? '' : ' style="display:none;"';
    $form['filters_association'] = array('#type' => 'radios', '#name' => 'wpcf[group][filters_association]', '#id' => 'wpcf-fields-form-filters-association', '#options' => array(__('Display this group when ANY of the above conditions is met', 'wpcf') => 'any', __('Display this group when ALL the above conditions is met', 'wpcf') => 'all'), '#default_value' => !empty($update['filters_association']) ? $update['filters_association'] : 'any', '#inline' => true, '#before' => '<div id="wpcf-fields-form-filters-association-form"' . $display . '>', '#after' => '<div id="wpcf-fields-form-filters-association-summary" style="margin-top:10px;font-style:italic;margin-bottom:15px;"></div></div>');
    wpcf_admin_add_js_settings('wpcf_filters_association_or', '\'' . __('This group will appear on %pt% edit pages where content belongs to taxonomy: %tx% or View Template is: %vt%', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcf_filters_association_and', '\'' . __('This group will appear on %pt% edit pages where content belongs to taxonomy: %tx% and View Template is: %vt%', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcf_filters_association_all_pages', '\'' . __('all', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcf_filters_association_all_taxonomies', '\'' . __('any', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcf_filters_association_all_templates', '\'' . __('any', 'wpcf') . '\'');
    $additional_filters = apply_filters('wpcf_fields_form_additional_filters', array(), $update);
    $form = $form + $additional_filters;
    $form['supports-table-close'] = array('#type' => 'markup', '#markup' => '</td></tr></tbody></table><br />');
    /** Admin styles* */
    $form['adminstyles-table-open'] = array('#type' => 'markup', '#markup' => '<table class="widefat" id="wpcf-admin-styles-box"><thead><tr><th>' . __('Styling Editor', 'wpcf') . '</th></tr></thead><tbody><tr><td>' . '<p>' . __('Customize Fields for admin panel.', 'wpcf') . '</p>');
    $admin_styles_value = $preview_profile = $edit_profile = '';
    if (isset($update['admin_styles'])) {
        $admin_styles_value = $update['admin_styles'];
    }
    $temp = '';
    if ($update) {
        require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
        require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta.php';
        require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
        require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta-post.php';
        //Get sample post
        $post = query_posts('posts_per_page=1');
        if (!empty($post) && count($post) != '') {
            $post = $post[0];
        }
        $preview_profile = wpcf_admin_post_meta_box_preview($post, $update, 1);
        $group = $update;
        $group['fields'] = wpcf_admin_post_process_fields($post, $group['fields'], true, false);
        $edit_profile = wpcf_admin_post_meta_box($post, $group, 1, true);
        add_action('admin_enqueue_scripts', 'wpcf_admin_fields_form_fix_styles', PHP_INT_MAX);
    }
    $temp[] = array('#type' => 'radio', '#suffix' => '<br />', '#value' => 'edit_mode', '#title' => 'Edit mode', '#name' => 'wpcf[group][preview]', '#default_value' => '', '#before' => '<div class="wpcf-admin-css-preview-style-edit">', '#inline' => true, '#attributes' => array('onclick' => 'changePreviewHtml(\'editmode\')', 'checked' => 'checked'));
    $temp[] = array('#type' => 'radio', '#title' => 'Read Only', '#name' => 'wpcf[group][preview]', '#default_value' => '', '#after' => '</div>', '#inline' => true, '#attributes' => array('onclick' => 'changePreviewHtml(\'readonly\')'));
    $temp[] = array('#type' => 'textarea', '#name' => 'wpcf[group][admin_html_preview]', '#inline' => true, '#id' => 'wpcf-form-groups-admin-html-preview', '#before' => '<h3>Field group HTML</h3>');
    $temp[] = array('#type' => 'textarea', '#name' => 'wpcf[group][admin_styles]', '#inline' => true, '#value' => $admin_styles_value, '#default_value' => '', '#id' => 'wpcf-form-groups-css-fields-editor', '#after' => '
        <div class="wpcf-update-preview-btn"><input type="button" value="Update preview" onclick="wpcfPreviewHtml()" style="float:right;" class="button-secondary"></div>
        <h3>Field group preview</h3>
        <div id="wpcf-update-preview-div">Preview here</div>
        <script type="text/javascript">
var wpcfReadOnly = ' . json_encode($preview_profile) . ';
var wpcfEditMode = ' . json_encode($edit_profile) . ';
var wpcfDefaultCss = ' . json_encode($admin_styles_value) . ';
        </script>
        ', '#before' => '<h3>Your CSS</h3>');
    $admin_styles = _wpcf_filter_wrap('admin_styles', __('Admin styles for fields:', 'wpcf'), '', '', $temp, __('Open style editor', 'wpcf'));
    $form['p_wrap_1_' . wpcf_unique_id(serialize($admin_styles))] = array('#type' => 'markup', '#markup' => '<p class="wpcf-filter-wrap">');
    $form = $form + $admin_styles;
    $form['adminstyles-table-close'] = array('#type' => 'markup', '#markup' => '</td></tr></tbody></table><br />');
    /** End admin Styles * */
    // Group fields
    $form['fields_title'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Fields', 'wpcf') . '</h2>');
    $show_under_title = true;
    $form['ajax-response-open'] = array('#type' => 'markup', '#markup' => '<div id="wpcf-fields-sortable" class="ui-sortable">');
    // If it's update, display existing fields
    $existing_fields = array();
    if ($update && isset($update['fields'])) {
        foreach ($update['fields'] as $slug => $field) {
            $field['submitted_key'] = $slug;
            $field['group_id'] = $update['id'];
            $form_field = wpcf_fields_get_field_form_data($field['type'], $field);
            if (is_array($form_field)) {
                $form['draggable-open-' . rand()] = array('#type' => 'markup', '#markup' => '<div class="ui-draggable">');
                $form = $form + $form_field;
                $form['draggable-close-' . rand()] = array('#type' => 'markup', '#markup' => '</div>');
            }
            $existing_fields[] = $slug;
            $show_under_title = false;
        }
    }
    // Any new fields submitted but failed? (Don't double it)
    if (!empty($_POST['wpcf']['fields'])) {
        foreach ($_POST['wpcf']['fields'] as $key => $field) {
            if (in_array($key, $existing_fields)) {
                continue;
            }
            $field['submitted_key'] = $key;
            $form_field = wpcf_fields_get_field_form_data($field['type'], $field);
            if (is_array($form_field)) {
                $form['draggable-open-' . rand()] = array('#type' => 'markup', '#markup' => '<div class="ui-draggable">');
                $form = $form + $form_field;
                $form['draggable-close-' . rand()] = array('#type' => 'markup', '#markup' => '</div>');
            }
        }
        $show_under_title = false;
    }
    $form['ajax-response-close'] = array('#type' => 'markup', '#markup' => '</div>' . '<div id="wpcf-ajax-response"></div>');
    if ($show_under_title) {
        $form['fields_title']['#markup'] = $form['fields_title']['#markup'] . '<div id="wpcf-fields-under-title">' . __('There are no fields in this group. To add a field, click on the field buttons at the right.', 'wpcf') . '</div>';
    }
    // If update, create ID field
    if ($update) {
        $form['group_id'] = array('#type' => 'hidden', '#name' => 'group_id', '#value' => $update['id'], '#forced_value' => true);
    }
    $form['submit'] = array('#type' => 'submit', '#name' => 'save', '#value' => __('Save', 'wpcf'), '#attributes' => array('class' => 'button-primary wpcf-disabled-on-submit'));
    // Close main div
    $form['close-sidebar'] = array('#type' => 'markup', '#markup' => '</div>');
    $form = apply_filters('wpcf_form_fields', $form, $update);
    // Add JS settings
    wpcf_admin_add_js_settings('wpcfFormUniqueValuesCheckText', '\'' . __('Warning: same values selected', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcfFormUniqueNamesCheckText', '\'' . __('Warning: field name already used', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcfFormUniqueSlugsCheckText', '\'' . __('Warning: field slug already used', 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcfFormAlertOnlyPreview', sprintf("'%s'", __('Sorry, but this is only preview!', 'wpcf')));
    return $form;
}
/**
 * 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;
}
/**
 * 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;
}
 /**
  * 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_group_fields($group_id)
 {
     if (empty($_POST['wpcf']['fields'])) {
         delete_post_meta($group_id, '_wp_types_group_fields');
         return;
     }
     $fields = array();
     // First check all 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']))) {
                 $this->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']))) {
                 $this->triggerError();
                 wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error');
                 return $form;
             }
         }
         $field['submit-key'] = $key;
         // Field ID and slug are same thing
         $field_id = wpcf_admin_fields_save_field($field);
         if (is_wp_error($field_id)) {
             $this->triggerError();
             wpcf_admin_message($field_id->get_error_message(), 'error');
             return;
         }
         if (!empty($field_id)) {
             $fields[] = $field_id;
         }
         // WPML
         /** @var string $field_id */
         if (defined('ICL_SITEPRESS_VERSION') && version_compare(ICL_SITEPRESS_VERSION, '3.2', '<')) {
             if (function_exists('wpml_cf_translation_preferences_store')) {
                 $real_custom_field_name = wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_id)) . $field_id;
                 wpml_cf_translation_preferences_store($key, $real_custom_field_name);
             }
         }
     }
     wpcf_admin_fields_save_group_fields($group_id, $fields);
 }
Exemple #13
0
 private function save_filter_fields($group_id, $fields_data)
 {
     if (empty($fields_data)) {
         delete_post_meta($group_id, '_wp_types_group_fields');
         return;
     }
     $fields = array();
     // First check all fields
     foreach ($fields_data as $field_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']), WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION)) {
                 $this->triggerError();
                 wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error');
                 return;
             }
             if (isset($field['slug']) && wpcf_types_cf_under_control('check_exists', sanitize_title($field['slug']), WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION)) {
                 $this->triggerError();
                 wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error');
                 return;
             }
         }
         $field['submit-key'] = $field_key;
         // Field ID and slug are same thing
         $field_slug = wpcf_admin_fields_save_field($field, WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION);
         if (is_wp_error($field_slug)) {
             $this->triggerError();
             wpcf_admin_message($field_slug->get_error_message(), 'error');
             return;
         }
         if (!empty($field_slug)) {
             $fields[] = $field_slug;
         }
         // WPML
         if (defined('ICL_SITEPRESS_VERSION') && version_compare(ICL_SITEPRESS_VERSION, '3.2', '<')) {
             if (function_exists('wpml_cf_translation_preferences_store')) {
                 $real_custom_field_name = wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_slug, false, false, false, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION)) . $field_slug;
                 wpml_cf_translation_preferences_store($field_key, $real_custom_field_name);
             }
         }
     }
     wpcf_admin_fields_save_group_fields($group_id, $fields, false, WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION);
 }
/**
 * 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;
}
 private function process_bulk_action()
 {
     $action = $this->current_action();
     if (false == $action) {
         return;
     }
     if (!wp_verify_nonce(wpcf_getpost('_wpnonce'), WPCF_Page_Control_Termmeta::BULK_ACTION_NONCE)) {
         wp_die(__('Invalid nonce.', 'wpcf'));
     }
     $selected_field_definitions = wpcf_getpost(self::INPUT_SLUGS, array());
     if (is_string($selected_field_definitions)) {
         $selected_field_definitions = array($selected_field_definitions);
     }
     if (!is_array($selected_field_definitions) || empty($selected_field_definitions)) {
         // Nothing to do here
         return;
     }
     $factory = WPCF_Field_Term_Definition_Factory::get_instance();
     switch ($action) {
         case self::BULK_ACTION_ADD_TO_GROUP:
             $group_ids = $this->read_group_ids();
             foreach ($group_ids as $group_id) {
                 wpcf_admin_fields_save_group_fields($group_id, $selected_field_definitions, true, WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION);
             }
             break;
         case self::BULK_ACTION_REMOVE_FROM_TO_GROUP:
             $group_ids = $this->read_group_ids();
             foreach ($group_ids as $group_id) {
                 wpcf_admin_fields_remove_field_from_group_bulk($group_id, $selected_field_definitions);
             }
             break;
         case self::BULK_ACTION_CHANGE_TYPE:
             $field_type_slug = wpcf_getpost('wpcf-id');
             if (!empty($field_type_slug)) {
                 wpcf_admin_custom_fields_change_type($selected_field_definitions, $field_type_slug, WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION);
             }
             break;
         case self::BULK_ACTION_ACTIVATE:
             $fields = wpcf_admin_fields_get_fields(false, true, false, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION);
             $fields_bulk = wpcf_types_cf_under_control('add', array('fields' => $selected_field_definitions), WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION);
             foreach ($fields_bulk as $field_id) {
                 if (isset($fields[$field_id])) {
                     $fields[$field_id]['data']['disabled'] = 0;
                 }
                 wpcf_admin_message(sprintf(__('Added to Types control: %s', 'wpcf'), esc_html($field_id)), 'updated', 'echo');
             }
             wpcf_admin_fields_save_fields($fields, false, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION);
             break;
         case self::BULK_ACTION_DEACTIVATE:
             $fields = wpcf_admin_fields_get_fields(false, true, false, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION);
             foreach ($selected_field_definitions as $field_id) {
                 $field_id = sanitize_text_field($field_id);
                 if (isset($fields[$field_id])) {
                     $fields[$field_id]['data']['disabled'] = 1;
                     wpcf_admin_message(sprintf(__('Removed from Types control: %s', 'wpcf'), $fields[$field_id]['name']), 'updated', 'echo');
                 }
             }
             wpcf_admin_fields_save_fields($fields, false, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION);
             break;
         case self::BULK_ACTION_DELETE:
             $failed = array();
             $success = array();
             foreach ($selected_field_definitions as $field_id) {
                 $field_id = sanitize_text_field($field_id);
                 // Permanently single field definition and field data.
                 $field_definition = $factory->load_field_definition($field_id);
                 if (null == $field_definition) {
                     $response = false;
                 } else {
                     $response = $factory->delete_definition($field_definition);
                 }
                 if (!$response) {
                     $failed[] = str_replace('_' . md5('wpcf_not_controlled'), '', $field_id);
                 } else {
                     $success[] = $field_id;
                 }
             }
             if (!empty($success)) {
                 wpcf_admin_message(sprintf(__('Fields %s have been deleted.', 'wpcf'), esc_html(implode(', ', $success))), 'updated', 'echo');
             }
             if (!empty($failed)) {
                 wpcf_admin_message(sprintf(__('Fields %s are not Types fields. Types wont delete these fields.', 'wpcf'), esc_html(implode(', ', $failed))), 'error', 'echo');
             }
             break;
     }
     // We made changes to field definitions and now the listing table is going to be rendered.
     $factory->clear_definition_storage();
 }
Exemple #16
0
/**
 * Shows stored messages.
 */
function wpcf_show_admin_messages()
{
    $messages = get_option('wpcf-messages', array());
    $messages_for_user = isset($messages[get_current_user_id()]) ? $messages[get_current_user_id()] : array();
    if (!empty($messages_for_user)) {
        foreach ($messages_for_user as $message) {
            wpcf_admin_message($message['message'], $message['class']);
        }
        unset($messages[get_current_user_id()]);
    }
    update_option('wpcf-messages', $messages);
}
Exemple #17
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);
    }
}
Exemple #18
0
/**
 * Imports settings.
 */
function wpcf_embedded_check_import()
{
    if (file_exists(WPCF_EMBEDDED_ABSPATH . '/settings.php')) {
        require_once WPCF_EMBEDDED_ABSPATH . '/admin.php';
        require_once WPCF_EMBEDDED_ABSPATH . '/settings.php';
        $dismissed = get_option('wpcf_dismissed_messages', array());
        if (in_array($timestamp, $dismissed)) {
            return false;
        }
        if ($timestamp > get_option('wpcf-types-embedded-import', 0)) {
            if (!$auto_import) {
                $link = "<a href=\"" . admin_url('?types-embedded-import=1&amp;_wpnonce=' . wp_create_nonce('embedded-import')) . "\">";
                $text = sprintf(__('You have Types import pending. %sClick here to import.%s %sDismiss message.%s', 'wpcf'), $link, '</a>', "<a onclick=\"jQuery(this).parent().parent().fadeOut();\" class=\"wpcf-ajax-link\" href=\"" . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=dismiss_message&amp;id=' . $timestamp . '&amp;_wpnonce=' . wp_create_nonce('dismiss_message')) . "\">", '</a>');
                wpcf_admin_message($text);
            }
            if ($auto_import || isset($_GET['types-embedded-import']) && isset($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], 'embedded-import')) {
                if (file_exists(WPCF_EMBEDDED_ABSPATH . '/settings.xml')) {
                    $_POST['overwrite-groups'] = 1;
                    $_POST['overwrite-fields'] = 1;
                    $_POST['overwrite-types'] = 1;
                    $_POST['overwrite-tax'] = 1;
                    //                    $_POST['delete-groups'] = 0;
                    //                    $_POST['delete-fields'] = 0;
                    //                    $_POST['delete-types'] = 0;
                    //                    $_POST['delete-tax'] = 0;
                    $_POST['post_relationship'] = 1;
                    require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
                    require_once WPCF_EMBEDDED_INC_ABSPATH . '/import-export.php';
                    $data = @file_get_contents(WPCF_EMBEDDED_ABSPATH . '/settings.xml');
                    wpcf_admin_import_data($data, false, 'types-auto-import');
                    update_option('wpcf-types-embedded-import', $timestamp);
                    wp_redirect(admin_url());
                } else {
                    $code = __('settings.xml file missing', 'wpcf');
                    wpcf_admin_message($code, 'error');
                }
            }
        }
    }
}
/**
 * Update an existing field group or create new one.
 *
 * @param array $group
 * @param string $post_type
 * @return int Group ID.
 */
function wpcf_admin_fields_save_group($group, $post_type = TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, $which_fields = 'none')
{
    if (!isset($group['name']) || empty($group['name'])) {
        return false;
    }
    $post = array('post_status' => 'publish', 'post_type' => $post_type, 'post_title' => sanitize_text_field($group['name']), 'post_name' => sanitize_text_field($group['name']), 'post_content' => empty($group['description']) ? '' : $group['description']);
    if (empty($post['post_title'])) {
        wpcf_admin_message(__('Please set name', 'wpcf'), 'error');
        return false;
    }
    $update = false;
    $slug_pre_save = false;
    if (isset($group['id']) && !empty($group['id'])) {
        $update = true;
        $post_to_update = get_post($group['id']);
        if (empty($post_to_update) || $post_to_update->post_type != $post_type) {
            return false;
        }
        $post['ID'] = $post_to_update->ID;
        $slug_pre_save = $post_to_update->post_name;
        $post['post_status'] = $post_to_update->post_status;
    }
    if ($update) {
        $group_id = wp_update_post($post);
        if (!$group_id) {
            return false;
        }
        update_post_meta($group_id, TOOLSET_EDIT_LAST, time());
    } else {
        $group_id = wp_insert_post($post, true);
        if (is_wp_error($group_id)) {
            return false;
        }
    }
    if (isset($group['admin_styles'])) {
        wpcf_admin_fields_save_group_admin_styles($group_id, $group['admin_styles']);
    }
    if (!empty($group['filters_association'])) {
        update_post_meta($group_id, '_wp_types_group_filters_association', $group['filters_association']);
    } else {
        delete_post_meta($group_id, '_wp_types_group_filters_association');
    }
    // WPML register strings
    if (function_exists('icl_register_string')) {
        try {
            // Legacy function gives us only the underlying post type of the field group.
            $group_factory = Types_Field_Utils::get_group_factory_by_post_type($post_type);
            $field_group = $group_factory->load_field_group(sanitize_title($group['name']));
            // Skip registering if the group does not exist.
            if (null != $field_group) {
                $group_wpml = new Types_Wpml_Field_Group($field_group);
                $group_wpml->register($slug_pre_save);
            }
        } catch (InvalidArgumentException $e) {
            // Something is seriously wrong - there's no field group factory for given post type, bail.
        }
    }
    // admin message
    wpcf_admin_fields_save_message($update, $which_fields);
    return $group_id;
}