/**
  * buildRawFieldToEdit function
  *
  * @param array $field
  * @param array $settings
  * @return array
  * @author Riccardo Strobbia
  **/
 private function buildRawFieldToEdit($field, $settings = array())
 {
     $opts = $settings ? $settings : get_post_meta($this->view_id, '_wpv_settings', true);
     $ret = array();
     $index = self::get_param_index_cmp($settings, $field);
     //print "index first method " . $index ."\n";
     //FIXME: fall back for retrocompatibility
     if ($index === -1) {
         //	$index = self::get_param_index($settings, $field);
     }
     $index = self::get_param_index($settings, $field);
     if ($index > -1) {
         $field['url_param'] = self::$tmp_settings['filter_controls_param'][$index];
         $field['can_force_zero'] = false;
         self::$tmp_settings = null;
         if (isset($field['taxonomy'])) {
             $name = $settings['filter_controls_field_name'][$index];
             $id = $name;
             $id = $settings['filter_controls_label'][$index];
             $ret['is_types'] = false;
             $ret['group'] = 'taxonomy';
             ///do processing for taxes and return
         } else {
             if (isset($field['field'])) {
                 $g = '';
                 $name = $field['field'];
                 $nice_name = explode('wpcf-', $name);
                 $id = isset($nice_name[1]) ? $nice_name[1] : $name;
                 $field_options = array();
                 if (function_exists('wpcf_admin_fields_get_groups_by_field')) {
                     $field_options = wpcf_admin_fields_get_field($id);
                     foreach (wpcf_admin_fields_get_groups_by_field($id) as $gs) {
                         $g = $gs['name'];
                     }
                 }
                 $ret['group'] = $g ? $g : "Custom fields";
                 $name = $g ? $name : $field['field'];
                 $ret['is_types'] = $g ? true : false;
                 if (!empty($field_options) && isset($field_options['meta_key'])) {
                     $name = $field_options['meta_key'];
                 }
                 if (!empty($field_options) && $field_options['type'] == 'checkbox' && $field_options['data']['save_empty'] == 'yes') {
                     $ret['can_force_zero'] = true;
                 }
                 $id = $g ? $id : $field['field'];
             } else {
                 if (isset($field['relationship'])) {
                     $name = 'relationship';
                     $id = __('Post relationship', 'wpv-views');
                     // TODO what are we doing here??
                     $id = $settings['filter_controls_label'][$index];
                     $ret['is_types'] = false;
                     $ret['group'] = 'basic_filters';
                     $ret['kind'] = 'relationship';
                     $ret['basic_filter_type'] = 'relationship';
                 } else {
                     $name = $settings['filter_controls_field_name'][$index];
                     $id = $name;
                     $ret['is_types'] = false;
                     $ret['group'] = 'Custom fields';
                 }
             }
         }
         //	print "\n'custom-field-'.$name.'_value'\n";
         //	print_r( $settings['custom-field-'.$name.'_value'] );
         $ret['field'] = $name;
         $ret['id'] = $id;
         if (isset($field['taxonomy'])) {
             $ret['kind'] = 'taxonomy';
             $ret['group'] = $ret['kind'];
             $ret['compare'] = isset($settings['taxonomy-' . $name . '-attribute-operator']) ? $settings['taxonomy-' . $name . '-attribute-operator'] : 'IN';
             //	$ret['hide_empty'] = isset( $field['hide_empty'] ) ? $field['hide_empty'] : 'false';
         } else {
             if (isset($field['relationship'])) {
                 $ret['group'] = 'basic_filters';
             } else {
                 $ret['compare'] = isset($settings['custom-field-' . $name . '_compare']) ? $settings['custom-field-' . $name . '_compare'] : '=';
                 $ret['data_type'] = isset($settings['custom-field-' . $name . '_type']) ? $settings['custom-field-' . $name . '_type'] : 'CHAR';
                 $ret['relation'] = isset($settings['custom-field-' . $name . '_relationship']) ? $settings['custom-field-' . $name . '_relationship'] : 'AND';
                 $ret['kind'] = 'field';
             }
         }
         $ret['name'] = $ret['is_types'] ? $settings['filter_controls_label'][$index] : $id;
         $ret['type'] = $settings['filter_controls_type'][$index];
         $ret['values'] = $settings['filter_controls_values'][$index];
         $ret['enabled'] = $settings['filter_controls_enable'][$index];
         $ret['index'] = $index;
         /*	$ret_aux = $settings['filter_controls_values'][$index];
         			$ret['taxonomy_order'] = isset( $ret_aux['taxonomy_order'] ) ? $ret_aux['taxonomy_order'] : 'ASC';
         			$ret['taxonomy_orderby'] = isset( $ret_aux['taxonomy_orderby'] ) ? $ret_aux['taxonomy_orderby'] : 'name';
         			$ret['hide_empty'] = isset( $ret_aux['hide_empty'] ) ? $ret_aux['hide_empty'] : 'false';*/
         //implement for tax
         return array_merge($field, $ret);
     } else {
         return array('error', __(sprintf('There is something wrong url parameter is missing %s', __METHOD__), 'wpv-views'));
     }
 }
Example #2
0
 /**
  * Returns HTML formatted row
  *
  * While generating rows we collect headers too.
  *
  * @return type
  */
 function row()
 {
     /*
      * Start output.
      * Output is returned as array - each element is <td> content.
      */
     $row = array();
     /*
      * LOOP over fields
      * Custom settings (specific)
      */
     if ($this->data['fields_setting'] == 'specific' && !empty($this->data['fields'])) {
         // Set title
         if (isset($this->data['fields']['_wp_title'])) {
             $this->headers[] = '_wp_title';
             $row[] = $this->title();
         }
         // Set body
         if (isset($this->data['fields']['_wp_body'])) {
             $this->headers[] = '_wp_body';
             $row[] = $this->body();
         }
         // Loop over Types fields
         foreach ($this->data['fields'] as $field_key => $true) {
             // If field belongs only to disabled group - remove it.
             $groups = wpcf_admin_fields_get_groups_by_field($this->cf->__get_slug_no_prefix($field_key));
             if (empty($groups)) {
                 continue;
             }
             $_continue = false;
             // If at least one active - proceed
             foreach ($groups as $group) {
                 if ($group['is_active']) {
                     $_continue = true;
                 }
             }
             if (!$_continue) {
                 continue;
             }
             // Skip parents
             if (in_array($field_key, array('_wp_title', '_wp_body', '_wpcf_pr_parents', '_wpcf_pr_taxonomies'))) {
                 continue;
             } else {
                 /*
                  * Set field
                  */
                 //                    $field_key = $this->cf->__get_slug_no_prefix( $field_key );
                 $this->cf->set($this->child, $field_key);
                 $row[] = $this->field_form();
                 $this->_field_triggers();
                 // Add to header
                 //                    $this->headers[] = WPCF_META_PREFIX . $field_key;
                 $this->headers[] = $field_key;
             }
         }
         // Add parent forms
         if (!empty($this->data['fields']['_wpcf_pr_parents'])) {
             $_temp = (array) $this->data['fields']['_wpcf_pr_parents'];
             foreach ($_temp as $_parent => $_true) {
                 $row[] = $this->_parent_form($_parent);
                 // Add to header
                 $this->headers['__parents'][$_parent] = $_true;
             }
         }
         // Add taxonomies forms
         if (!empty($this->data['fields']['_wpcf_pr_taxonomies'])) {
             $_temp = (array) $this->data['fields']['_wpcf_pr_taxonomies'];
             foreach ($_temp as $taxonomy => $_true) {
                 $_taxonomy = get_taxonomy($taxonomy);
                 if (!empty($_taxonomy)) {
                     $row[] = $this->taxonomy_form($_taxonomy);
                     // Add to header
                     $this->headers['__taxonomies'][$taxonomy] = $_taxonomy->label;
                 }
             }
         }
         /*
          *
          *
          *
          *
          * DEFAULT SETTINGS
          */
     } else {
         // Set title
         $row[] = $this->title();
         $this->headers[] = '_wp_title';
         // Set body if needed
         if ($this->data['fields_setting'] == 'all_cf_standard') {
             $this->headers[] = '_wp_body';
             $row[] = $this->body();
         }
         /*
          * Loop over groups and fields
          */
         // Get groups
         $groups = wpcf_admin_post_get_post_groups_fields($this->child, 'post_relationships');
         foreach ($groups as $group) {
             if (empty($group['fields'])) {
                 continue;
             }
             /*
              * Loop fields
              */
             foreach ($group['fields'] as $field_key => $field) {
                 /*
                  * Set field
                  */
                 $field_key = $this->cf->__get_slug_no_prefix($field_key);
                 $this->cf->set($this->child, $field_key);
                 $row[] = $this->field_form();
                 $this->_field_triggers();
                 // Add to header{
                 $this->headers[] = WPCF_META_PREFIX . $field_key;
             }
         }
         // Add parent forms
         if ($this->data['fields_setting'] == 'all_cf') {
             $this->data['fields']['_wpcf_pr_parents'] = wpcf_pr_admin_get_belongs($this->child_post_type);
             if (!empty($this->data['fields']['_wpcf_pr_parents'])) {
                 $_temp = (array) $this->data['fields']['_wpcf_pr_parents'];
                 foreach ($_temp as $_parent => $_true) {
                     if ($_parent == $this->parent_post_type) {
                         continue;
                     }
                     $row[] = $this->_parent_form($_parent);
                     // Add to header
                     $this->headers['__parents'][$_parent] = $_true;
                 }
             }
         }
     }
     return $row;
 }
 function prepare_items()
 {
     $time = time();
     global $wpdb;
     $wpcf_per_page = 15;
     $this->wpcf_groups = wpcf_admin_fields_get_groups();
     $this->wpcf_field_types = wpcf_admin_fields_get_available_types();
     // Get ours and enabled
     $cf_types = wpcf_admin_fields_get_fields(true, true);
     foreach ($cf_types as $cf_id => $cf) {
         $groups_temp = wpcf_admin_fields_get_groups_by_field($cf['id']);
         $output_temp = array();
         foreach ($groups_temp as $group_id_temp => $group_temp) {
             $output_temp[$group_temp['id']] = $group_temp['name'];
         }
         if (empty($output_temp)) {
             $output_temp[] = __('None', 'wpcf');
         }
         $cf_types[$cf_id]['groups_txt'] = implode(', ', $output_temp);
         $cf_types[$cf_id]['groups_ids'] = $groups_temp;
     }
     // Get others (cache this result?)
     $cf_other = $wpdb->get_results("\n\t\tSELECT meta_id, meta_key\n\t\tFROM {$wpdb->postmeta}\n\t\tGROUP BY meta_key\n\t\tHAVING meta_key NOT LIKE '\\_%'\n\t\tORDER BY meta_key");
     $output = '';
     // Clean from ours
     foreach ($cf_other as $type_id => $type_data) {
         if (strpos($type_data->meta_key, WPCF_META_PREFIX) !== false) {
             $field_temp = wpcf_admin_fields_get_field(str_replace(WPCF_META_PREFIX, '', $type_data->meta_key));
             if (!empty($field_temp)) {
                 if (!empty($field_temp['data']['disabled'])) {
                     $cf_types[$field_temp['id']] = array('id' => $field_temp['id'], 'slug' => $type_data->meta_key, 'name' => $type_data->meta_key, 'type' => 0, 'groups_txt' => __('None', 'wpcf'));
                 } else {
                     unset($cf_other[$type_id]);
                 }
             } else {
                 if (wpcf_types_cf_under_control('check_exists', $type_data->meta_key)) {
                     unset($cf_other[$type_id]);
                 } else {
                     $cf_types[$type_data->meta_key] = array('id' => $type_data->meta_key, 'slug' => $type_data->meta_key, 'name' => $type_data->meta_key, 'type' => 0, 'groups_txt' => __('None', 'wpcf'));
                 }
             }
         } else {
             if (wpcf_types_cf_under_control('check_exists', $type_data->meta_key)) {
                 unset($cf_other[$type_id]);
             } else {
                 $cf_types[$type_data->meta_key] = array('id' => $type_data->meta_key, 'slug' => $type_data->meta_key, 'name' => $type_data->meta_key, 'type' => 0, 'groups_txt' => __('None', 'wpcf'));
             }
         }
     }
     // Set some values
     foreach ($cf_types as $cf_id_temp => $cf_temp) {
         if (empty($cf_temp['type']) || !empty($cf_temp['data']['controlled'])) {
             $cf_types[$cf_id_temp]['slug'] = $cf_temp['name'];
         } else {
             $cf_types[$cf_id_temp]['slug'] = wpcf_types_get_meta_prefix($cf_temp) . $cf_temp['slug'];
         }
     }
     // Order
     if (!empty($_REQUEST['orderby'])) {
         if ($_REQUEST['orderby'] == 'c') {
             ksort($cf_types, SORT_STRING);
             if ($_REQUEST['order'] == 'desc') {
                 $cf_types = array_reverse($cf_types, true);
             }
         } else {
             $sort_matches = array('g' => 'groups_txt', 't' => 'slug', 'f' => 'type');
             $sorted_keys = array();
             $new_array = array();
             foreach ($cf_types as $cf_id_temp => $cf_temp) {
                 $sorted_keys[$cf_temp['id']] = $cf_temp[$sort_matches[$_REQUEST['orderby']]];
             }
             asort($sorted_keys, SORT_STRING);
             if ($_REQUEST['order'] == 'desc') {
                 $sorted_keys = array_reverse($sorted_keys, true);
             }
             foreach ($sorted_keys as $cf_id_temp => $groups_txt) {
                 $new_array[$cf_id_temp] = $cf_types[$cf_id_temp];
             }
             $cf_types = $new_array;
         }
     }
     // Search
     if (!empty($_REQUEST['s'])) {
         $search_results = array();
         foreach ($cf_types as $search_id => $search_field) {
             if (strpos(strval($search_field['name']), strval(trim(stripslashes($_REQUEST['s'])))) !== false) {
                 $search_results[$search_id] = $cf_types[$search_id];
             }
         }
         $cf_types = $search_results;
     }
     if (empty($_GET['display_all'])) {
         $total_items = count($cf_types);
         if ($total_items < $wpcf_per_page) {
             $wpcf_per_page = $total_items;
         }
         if ($this->get_pagenum() == 1) {
             $offset = 0;
         } else {
             $offset = ($this->get_pagenum() - 1) * $wpcf_per_page;
         }
         // Display required number of entries on page
         $this->items = array_slice($cf_types, $offset, $wpcf_per_page);
         $this->set_pagination_args(array('total_items' => $total_items, 'per_page' => $wpcf_per_page));
     } else {
         $this->items = $cf_types;
     }
     $this->_column_headers = array($this->get_columns(), array(), $this->get_sortable_columns());
 }
Example #4
0
/**
 * Gets all filters required for field to be used.
 * 
 * @param type $field
 * @return boolean|string 
 */
function wpcf_admin_fields_get_filter_by_field($field)
{
    $field = wpcf_admin_fields_get_field($field);
    if (empty($field)) {
        return false;
    }
    $filter = array();
    $filter['types'] = array();
    $filter['terms'] = array();
    $filter['templates'] = array();
    $groups = wpcf_admin_fields_get_groups_by_field($field['id']);
    foreach ($groups as $group_id => $group_data) {
        // Get filters
        $filter['types'] = array_merge($filter['types'], explode(',', trim(get_post_meta($group_id, '_wp_types_group_post_types', true), ',')));
        $filter['terms'] = array_merge($filter['terms'], explode(',', trim(get_post_meta($group_id, '_wp_types_group_terms', true), ',')));
        $filter['templates'] = array_merge($filter['templates'], explode(',', trim(get_post_meta($group_id, '_wp_types_group_templates', true), ',')));
        $filter['association'] = isset($group_data['filters_association']) && $group_data['filters_association'] == 'any' ? 'OR' : 'AND';
    }
    if (in_array('all', $filter['types'])) {
        $filter['types'] = 'all';
    }
    if (in_array('all', $filter['terms'])) {
        $filter['terms'] = 'all';
    }
    if (in_array('all', $filter['templates'])) {
        $filter['templates'] = 'all';
    }
    return $filter;
}
function give_group_to_field($filters)
{
    $generics = array('post_author', 'post_status', 'post_search', 'post_parent', 'post_relationship', 'post_id');
    $users_filters = array('users_filter', 'usermeta_filter');
    $groups = array();
    foreach ($filters as $type => $filter) {
        if (in_array($type, $generics)) {
            $groups["Post filters"][$type] = $filter;
        } else {
            if ($type == 'post_category' || strpos($type, 'tax_input') !== false) {
                $groups['Taxonomy'][$type] = $filter;
            } else {
                if (strpos($type, 'custom-field-wpcf-') !== false) {
                    $g = '';
                    $nice_name = explode('custom-field-wpcf-', $type);
                    $id = isset($nice_name[1]) ? $nice_name[1] : $type;
                    if (function_exists('wpcf_admin_fields_get_groups_by_field')) {
                        foreach (wpcf_admin_fields_get_groups_by_field($id) as $gs) {
                            $g = $gs['name'];
                        }
                    }
                    $gr = $g ? $g : "Custom fields";
                    $groups[$gr][$type] = $filter;
                } else {
                    if (strpos($type, 'custom-field-views_woo_') !== false) {
                        $g = '';
                        $nice_name = explode('custom-field-', $type);
                        $id = isset($nice_name[1]) ? $nice_name[1] : $type;
                        if (function_exists('wpcf_admin_fields_get_groups_by_field')) {
                            foreach (wpcf_admin_fields_get_groups_by_field($id) as $gs) {
                                $g = $gs['name'];
                            }
                        }
                        $gr = $g ? $g : "WooCommerce Views filter fields";
                        $groups[$gr][$type] = $filter;
                    } else {
                        if (strpos($type, 'usermeta-field-basic-') !== false) {
                            $gr = "Basic fields";
                            $groups[$gr][$type] = $filter;
                        } else {
                            if (strpos($type, 'usermeta-field-wpcf-') !== false) {
                                $g = '';
                                $nice_name = explode('usermeta-field-wpcf-', $type);
                                $id = isset($nice_name[1]) ? $nice_name[1] : $type;
                                if (function_exists('wpcf_admin_fields_get_groups_by_field')) {
                                    foreach (wpcf_admin_fields_get_groups_by_field($id, 'wp-types-user-group') as $gs) {
                                        $g = $gs['name'];
                                    }
                                }
                                $gr = $g ? $g : "Users fields";
                                $groups[$gr][$type] = $filter;
                            } else {
                                if (strpos($type, 'usermeta-field-') !== false && strpos($type, 'usermeta-field-basic-') === false && strpos($type, 'usermeta-field-wpcf-') === false) {
                                    $gr = "User fields";
                                    $groups[$gr][$type] = $filter;
                                } else {
                                    if (in_array($type, $users_filters)) {
                                        $groups["Users filters"][$type] = $filter;
                                    } else {
                                        $groups['Custom fields'][$type] = $filter;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return $groups;
}
function wpv_get_custom_field_summary($type, $view_settings = array())
{
    $field_name = substr($type, strlen('custom-field-'));
    $args = array('name' => $field_name);
    $all_types_fields = get_option('wpcf-fields', array());
    $field_nicename = '';
    if (stripos($field_name, 'wpcf-') === 0) {
        if (isset($all_types_fields[substr($field_name, 5)]) && isset($all_types_fields[substr($field_name, 5)]['name'])) {
            $field_nicename = $all_types_fields[substr($field_name, 5)]['name'];
        } else {
            $field_nicename = $field_name;
        }
    } else {
        if (stripos($field_name, 'views_woo_') === 0) {
            if (isset($all_types_fields[$field_name]) && isset($all_types_fields[$field_name]['name'])) {
                $field_nicename = $all_types_fields[$field_name]['name'];
            } else {
                $field_nicename = $field_name;
            }
        } else {
            $field_nicename = $field_name;
        }
    }
    // Check if the field is in a Types group - if not, register with the full $key
    if (function_exists('wpcf_admin_fields_get_groups_by_field')) {
        $g = '';
        foreach (wpcf_admin_fields_get_groups_by_field($field_nicename) as $gs) {
            $g = $gs['name'];
        }
        $field_nicename = $g ? $field_nicename : $field_name;
    }
    ob_start();
    ?>
	<strong><?php 
    echo $field_nicename . ' ' . $view_settings[$type . '_compare'] . ' ' . $view_settings[$type . '_value'];
    ?>
</strong>
	
	<?php 
    $buffer = ob_get_clean();
    return $buffer;
}
function wpv_filter_give_group_to_field($filters)
{
    $post_meta_label = __('Custom fields', 'wpv-views');
    $post_woocommerce_views_meta_label = __('WooCommerce Views filter fields', 'wpv-views');
    $user_meta_label = __('User fields', 'wpv-views');
    $groups = array();
    foreach ($filters as $type => $filter) {
        if (isset($filter['group'])) {
            $group = $filter['group'];
            $groups[$group][$type] = $filter;
        } else {
            if (strpos($type, 'custom-field-wpcf-') !== false) {
                $g = '';
                $nice_name = explode('custom-field-wpcf-', $type);
                $id = isset($nice_name[1]) ? $nice_name[1] : $type;
                if (function_exists('wpcf_admin_fields_get_groups_by_field')) {
                    foreach (wpcf_admin_fields_get_groups_by_field($id) as $gs) {
                        $g = $gs['name'];
                    }
                }
                $gr = $g ? $g : $post_meta_label;
                $groups[$gr][$type] = $filter;
            } else {
                if (strpos($type, 'custom-field-views_woo_') !== false) {
                    $g = '';
                    $nice_name = explode('custom-field-', $type);
                    $id = isset($nice_name[1]) ? $nice_name[1] : $type;
                    if (function_exists('wpcf_admin_fields_get_groups_by_field')) {
                        foreach (wpcf_admin_fields_get_groups_by_field($id) as $gs) {
                            $g = $gs['name'];
                        }
                    }
                    $gr = $g ? $g : $post_woocommerce_views_meta_label;
                    $groups[$gr][$type] = $filter;
                } else {
                    if (strpos($type, 'usermeta-field-wpcf-') !== false) {
                        $g = '';
                        $nice_name = explode('usermeta-field-wpcf-', $type);
                        $id = isset($nice_name[1]) ? $nice_name[1] : $type;
                        if (function_exists('wpcf_admin_fields_get_groups_by_field')) {
                            foreach (wpcf_admin_fields_get_groups_by_field($id, 'wp-types-user-group') as $gs) {
                                $g = $gs['name'];
                            }
                        }
                        $gr = $g ? $g : $user_meta_label;
                        $groups[$gr][$type] = $filter;
                    } else {
                        if (strpos($type, 'usermeta-field-') !== false && strpos($type, 'usermeta-field-basic-') === false && strpos($type, 'usermeta-field-wpcf-') === false) {
                            $gr = $user_meta_label;
                            $groups[$gr][$type] = $filter;
                        } else {
                            $groups[$post_meta_label][$type] = $filter;
                        }
                    }
                }
            }
        }
    }
    return $groups;
}
function wpv_layout_wizard_add_field()
{
    // TODO this might need localization TODO this is seriously broken
    if (!isset($_POST["wpnonce"]) || !wp_verify_nonce($_POST["wpnonce"], 'layout_wizard_nonce')) {
        die("Undefined Nonce.");
    }
    global $WP_Views, $wpdb;
    $settings = $WP_Views->get_view_settings($_POST["view_id"]);
    $WP_Views->editor_addon = new Editor_addon('wpv-views', __('Insert Views Shortcodes', 'wpv-views'), WPV_URL . '/res/js/views_editor_plugin.js', WPV_URL . '/res/img/bw_icon16.png');
    if ((string) $settings["query_type"][0] == 'posts') {
        add_short_codes_to_js(array('body-view-templates', 'post', 'taxonomy', 'post-view', 'taxonomy-view', 'user-view'), $WP_Views->editor_addon);
    } else {
        if ((string) $settings["query_type"][0] == 'taxonomy') {
            add_short_codes_to_js(array('post-view', 'taxonomy-view', 'user-view'), $WP_Views->editor_addon);
        } else {
            if ((string) $settings["query_type"][0] == 'users') {
            }
        }
    }
    $fields_list = $WP_Views->editor_addon->get_fields_list();
    if (empty($fields_list)) {
        $fields_list = array();
    }
    $fields_accos = array();
    // Show taxonomy fields only if we are in Taxonomy mode
    if ((string) $settings["query_type"][0] == 'taxonomy') {
        $tax_fields = array();
        $tax_fields[__('Taxonomy title', 'wpv-views')] = 'wpv-taxonomy-title';
        $tax_fields[__('Taxonomy title with a link', 'wpv-views')] = 'wpv-taxonomy-link';
        $tax_fields[__('Taxonomy URL', 'wpv-views')] = 'wpv-taxonomy-url';
        $tax_fields[__('Taxonomy slug', 'wpv-views')] = 'wpv-taxonomy-slug';
        $tax_fields[__('Taxonomy description', 'wpv-views')] = 'wpv-taxonomy-description';
        $tax_fields[__('Taxonomy post count', 'wpv-views')] = 'wpv-taxonomy-post-count';
        foreach ($tax_fields as $name => $value) {
            $fields_accos[__('Taxonomy fields', 'wpv-views')][] = array($name, $value);
        }
    }
    if ((string) $settings["query_type"][0] == 'users') {
        $user_fields = array();
        $user_fields[__('User ID', 'wpv-views')] = 'wpv-user field="ID"';
        $user_fields[__('User Email', 'wpv-views')] = 'wpv-user field="user_email"';
        $user_fields[__('User Login', 'wpv-views')] = 'wpv-user field="user_login"';
        $user_fields[__('First Name', 'wpv-views')] = 'wpv-user field="user_firstname"';
        $user_fields[__('Last Name', 'wpv-views')] = 'wpv-user field="user_lastname"';
        $user_fields[__('Nickname', 'wpv-views')] = 'wpv-user field="nickname"';
        $user_fields[__('Display Name', 'wpv-views')] = 'wpv-user field="display_name"';
        $user_fields[__('Description', 'wpv-views')] = 'wpv-user field="description"';
        $user_fields[__('Yahoo IM', 'wpv-views')] = 'wpv-user field="yim"';
        $user_fields[__('Jabber', 'wpv-views')] = 'wpv-user field="jabber"';
        $user_fields[__('AIM', 'wpv-views')] = 'wpv-user field="aim"';
        $user_fields[__('User Url', 'wpv-views')] = 'wpv-user field="user_url"';
        $user_fields[__('Registration Date', 'wpv-views')] = 'wpv-user field="user_registered"';
        $user_fields[__('User Status', 'wpv-views')] = 'wpv-user field="user_status"';
        $user_fields[__('User Spam Status', 'wpv-views')] = 'wpv-user field="spam"';
        foreach ($user_fields as $name => $value) {
            $fields_accos[__('Basic', 'wpv-views')][] = array($name, $value);
        }
        $unused_field = array('comment_shortcuts', 'managenav-menuscolumnshidden', 'dismissed_wp_pointers', 'meta-box-order_dashboard', 'nav_menu_recently_edited', 'primary_blog', 'rich_editing', 'source_domain', 'use_ssl', 'user_level', 'user-settings-time', 'user-settings', 'dashboard_quick_press_last_post_id', 'capabilities', 'new_date', 'show_admin_bar_front', 'show_welcome_panel', 'show_highlight', 'admin_color', 'language_pairs', 'first_name', 'last_name', 'name', 'nickname', 'description', 'yim', 'jabber', 'aim');
        $exclude_these_hidden_var = '/(' . implode('|', $unused_field) . ')/';
        $meta_keys = get_user_meta_keys();
        $all_types_fields = get_option('wpcf-fields', array());
        foreach ($meta_keys as $key) {
            $key_nicename = '';
            if (function_exists('wpcf_init')) {
                if (stripos($key, 'wpcf-') === 0) {
                    //
                } else {
                    if (preg_match($exclude_these_hidden_var, $key)) {
                        continue;
                    }
                    $fields_accos[__('Users fields', 'wpv-views')][] = array($key, 'wpv-user field="' . $key . '"');
                }
            } else {
                if (preg_match($exclude_these_hidden_var, $key)) {
                    continue;
                }
                $fields_accos[__('Users fields', 'wpv-views')][] = array($key, 'wpv-user field="' . $key . '"');
            }
        }
        if (function_exists('wpcf_init')) {
            // TODO do the same for wpcf-fields for posts
            //Get types groups and fields
            $groups = wpcf_admin_fields_get_groups('wp-types-user-group');
            $user_id = wpcf_usermeta_get_user();
            $add = array();
            if (!empty($groups)) {
                foreach ($groups as $group_id => $group) {
                    if (empty($group['is_active'])) {
                        continue;
                    }
                    $fields = wpcf_admin_fields_get_fields_by_group($group['id'], 'slug', true, false, true, 'wp-types-user-group', 'wpcf-usermeta');
                    if (!empty($fields)) {
                        foreach ($fields as $field_id => $field) {
                            $add[] = $field['meta_key'];
                            $callback = 'wpcfFieldsEditorCallback(\'' . $field['id'] . '\', \'views-usermeta\', -1)';
                            /*$this->items[] = array($field['name'], 
                              'types usermeta="'.$field['meta_key'].'"][/types',
                              $group['name'],$callback);  */
                            $fields_accos[$group['name']][] = array($field['name'], 'types usermeta="' . $field['slug'] . '"][/types');
                        }
                    }
                }
            }
            //Get unused types fields
            $cf_types = wpcf_admin_fields_get_fields(true, true, false, 'wpcf-usermeta');
            foreach ($cf_types as $cf_id => $cf) {
                if (!in_array($cf['meta_key'], $add)) {
                    $callback = 'wpcfFieldsEditorCallback(\'' . $cf['id'] . '\', \'views-usermeta\', -1)';
                    /* $this->items[] = array($cf['name'], 
                       'types usermeta="'.$cf['meta_key'].'"][/types',
                       'Types fields',$callback); */
                    $fields_accos[__('Types fields', 'wpv-views')][] = array($cf['name'], 'types usermeta="' . $cf['slug'] . '"][/types');
                }
            }
        }
        $view_available = $wpdb->get_results("SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type='view' AND post_status in ('publish')");
        $fields_accos[__('Types fields', 'wpv-views')] = array();
        foreach ($view_available as $view) {
            $view_settings = get_post_meta($view->ID, '_wpv_settings', true);
            if (isset($view_settings['query_type'][0]) && $view_settings['query_type'][0] == 'posts' && !$WP_Views->is_archive_view($view->ID)) {
                $fields_accos[__('Post View', 'wpv-views')][] = array($view->post_title, 'wpv-view name="' . $view->post_title . '"');
            }
        }
    }
    $content_templates = array('Content template' => array(array('None', 'wpv-post-body view_template="None"')));
    if (function_exists('types_get_fields')) {
        $tmp = types_get_fields();
    } else {
        $tmp = array();
    }
    if (isset($tmp['fields'])) {
        $tmp = $tmp['fields'];
    }
    foreach ($fields_list as $items) {
        if (function_exists('wpcf_admin_fields_get_groups_by_field') && (preg_match('/-!-wpcf/', $items[2]) || preg_match('/-!-views/', $items[2]) || preg_match('/wpcf-/', $items[0]) || preg_match('/\\[types.*?field=\\"(.*?)\\"/', $items[0]))) {
            if (preg_match('/\\[types.*?field=\\"(.*?)\\"/', $items[0], $outp)) {
                $split = $outp[1];
            } else {
                $split = preg_replace('/wpcf-/', '', $items[0]);
            }
            //Field name, not a slug
            if (isset($tmp[$split]['name'])) {
                // if: fix PHP Notice in the AJAX response when showing hidden fields
                $items[0] = $tmp[$split]['name'];
                $group = wpcf_admin_fields_get_groups_by_field($tmp[$split]['id']);
                foreach ($group as $id => $params) {
                    $group = $params['name'];
                }
            }
        } else {
            if ($items[2] == 'Field' || strpos($items[2], 'Field') === 0) {
                $items[2] = 'Custom fields';
            }
            $group = $items[2];
        }
        if ($items[2] == __('Content template', 'wpv-views')) {
            //    global $wpdb;
            //  $items[0] = $wpdb->get_var("SELECT post_title  FROM {$wpdb->posts} WHERE post_title = '{$items[0]}'");
            $content_templates['Content template'][] = array($items[0], $items[1]);
        }
        if ($items[1] == 'wpml-string') {
            // Take out of the Layout Wizard the new wpml-string Translatable string shortcode added to V popups
            $group = '';
        }
        if (!empty($group)) {
            $fields_accos[$group][] = array($items[0], $items[1]);
        }
    }
    if ((string) $settings["query_type"][0] == 'posts') {
        // add taxonomies
        $assoc = array();
        $taxonomies = get_taxonomies(array(), 'objects');
        $exclude_tax_slugs = array();
        $exclude_tax_slugs = apply_filters('wpv_admin_exclude_tax_slugs', $exclude_tax_slugs);
        foreach ($taxonomies as $tname => $tax) {
            if (!in_array($tname, $exclude_tax_slugs) && $tax->show_ui) {
                $assoc['Taxonomies'][] = array($tax->label, 'wpv-post-taxonomy type="' . $tname . '" separator=", " format="link" show="name"');
            }
        }
        // add user meta fields
        if (function_exists('wpcf_admin_post_add_usermeta_to_editor_js')) {
            $usermeta_fields_list = wpcf_admin_post_add_usermeta_to_editor_js(array());
            foreach ($usermeta_fields_list as $group => $items) {
                foreach ($items as $field) {
                    $assoc[$group][] = array($field[0], $field[1]);
                }
            }
        }
        // Add after the Basic fields.
        $fields_accos = array_slice($fields_accos, 0, 1, true) + $assoc + array_slice($fields_accos, 1, count($fields_accos) - 1, true);
    }
    ob_start();
    ?>
<li id="layout-wizard-style_<?php 
    echo isset($_POST['id']) ? $_POST['id'] : $count;
    ?>
">
    <i class="icon-move js-layout-wizard-move-field"></i>
    <select name="layout-wizard-style" class="js-select2 js-layout-wizard-item">
        <?php 
    $selected_value = '';
    $typename = '';
    $selected_body = '';
    $selected_body_template = '';
    $selected_found = false;
    $user_fields_with_head = array('user_email', 'display_name', 'user_login', 'user_url', 'user_registered', 'user_status', 'spam');
    if (!isset($_POST['selected'])) {
        $_POST['selected'] = '';
    }
    foreach ($fields_accos as $group_title => $group_items) {
        ?>
            <optgroup label="<?php 
        echo $group_title;
        ?>
">
                <?php 
        foreach ($group_items as $items) {
            $value = $items[1];
            $istype = false;
            $typename2 = '';
            $selected = mysql_real_escape_string($_POST['selected']) == '[' . mysql_real_escape_string($items[1]) . ']' ? "selected" : "";
            $selected_striped = substr(stripslashes($_POST['selected']), 1, -1);
            if ($selected_striped == stripslashes($value)) {
                // Dirty hack: sometimes the selected item was not being set for user shortcodes
                $selected = "selected";
                if (preg_match('/\\[types.*usermeta=\\"(.*?)\\"/', $_POST['selected'], $outp)) {
                    $typename = $outp[1];
                }
            }
            $_POST['selected'] = stripslashes($_POST['selected']);
            if (!$selected && preg_match('/wpv-post-taxonomy/', $items[1]) && trim($_POST['selected'], '[]') == $items[1]) {
                $selected = 'selected';
            }
            if (!$selected && preg_match('/wpv-view/', $items[1]) && trim($_POST['selected'], '[]') == $items[1]) {
                $selected = 'selected';
            }
            if (!$selected && preg_match('/\\[types.*?field=\\"(.*?)\\"/', $_POST['selected']) && preg_match('/"wpcf\\-.*?"/', $items[1])) {
                preg_match('/\\[types.*?field=\\"(.*?)\\"/', $_POST['selected'], $outp);
                $sel = $outp[1];
                preg_match('/"wpcf\\-(.*?)"/', $items[1], $outp);
                $cur = $outp[1];
                $items[1] = trim($_POST['selected'], '[]');
                $selected = $cur == $sel ? 'selected' : '';
                $typename = $sel;
            }
            if (!$selected && preg_match('/\\[types.*usermeta=\\"(.*?)\\"/', $_POST['selected']) && preg_match('/types.*usermeta=\\"(.*?)\\"/', $items[1])) {
                preg_match('/\\[types.*usermeta=\\"(.*?)\\"/', $_POST['selected'], $outp);
                $sel = $outp[1];
                preg_match('/types.*usermeta=\\"(.*?)\\"/', $items[1], $outp);
                $cur = $outp[1];
                $usermeta_field = $sel;
                $items[1] = trim($_POST['selected'], '[]');
                $selected = $cur == $sel ? 'selected' : '';
                if ($selected) {
                    $value = $items[1];
                }
                $typename = $sel;
                $typename2 = $cur;
                $istype = true;
            }
            if (!$selected && preg_match('/wpv-post-body/', $_POST['selected']) && preg_match('/Body/', $items[0])) {
                $selected_body = $items[0];
                preg_match('/view_template\\="(.*?)"/', $_POST['selected'], $out);
                $selected_body_template = $out[1];
                global $wpdb;
                $selected_body_template = $wpdb->get_var("SELECT post_title FROM {$wpdb->posts} WHERE post_name = '{$selected_body_template}'");
                //     $value = trim($_POST['selected'], '[]');
                //     if (!$selected_body_template) {
                //     $value = $items[1];
                //     }
                $selected = 'selected';
            }
            if ($selected == 'selected') {
                $selected_value = $items[1];
                $selected_found = true;
            }
            $s = preg_match('/"wpcf\\-(.*?)"/', $value, $outp);
            if ($s) {
                $saved_fields = array();
                $sets = get_post_meta($_POST["view_id"], '_wpv_layout_settings', true);
                if (isset($sets["real_fields"])) {
                    $saved_fields = $sets["real_fields"];
                }
                $typename2 = $outp[1];
                $value = isset($saved_fields[$_POST["id"]]) && preg_match('/types.*?field=\\"' . $outp[1] . '\\"/', $saved_fields[$_POST["id"]]) ? trim($saved_fields[$_POST["id"]], '[]') : 'types field="' . $outp[1] . '" id=""][/types';
                $istype = true;
                if (!function_exists('wpcf_init')) {
                    $istype = false;
                    if (strpos($items[1], 'wpv-user') === 0) {
                        $value = $items[1];
                    } else {
                        $value = 'wpv-post-field name="wpcf-' . $outp[1] . '"';
                    }
                }
            }
            $head = '';
            if (substr($value, 0, 17) === "wpv-post-taxonomy") {
                $head = 'wpv-post-taxonomy';
            } else {
                if (substr($value, 0, 14) === "wpv-post-field") {
                    $head = 'post-field-' . $items[0];
                    // if it is a new WooCommerce Views field
                    if (preg_match('/\\wpv-post-field.*?name=\\"views_woo(.*?)\\"/', $value, $woo_match)) {
                        $head = 'post-field-views_woo' . $woo_match[1];
                    }
                } else {
                    if (substr($value, 0, 8) === "wpv-post") {
                        $head = substr(current(explode(' ', $value)), 4);
                        if (substr($value, 0, 15) === "wpv-post-status" || substr($value, 0, 14) === "wpv-post-class") {
                            $head = '';
                        }
                    } else {
                        if (substr($value, 0, 8) === "wpv-view") {
                            $head = 'post-view';
                        } else {
                            if (substr($value, 0, 5) === "types") {
                                if (!isset($outp[1]) && isset($usermeta_field)) {
                                    $outp[1] = $usermeta_field;
                                } else {
                                    if (!isset($outp[1])) {
                                        $outp[1] = '';
                                    }
                                }
                                if (empty($typename2)) {
                                    $typename2 = $outp[1];
                                }
                                $head = 'types-field-' . $outp[1];
                                // Add a table column header only if it's a field for posts
                                if (empty($typename2) || empty($outp[1])) {
                                    preg_match("/(usermeta|field)=\"([^\"]+)\"/", $value, $new_match);
                                    $typename2 = $outp[1] = $new_match[2];
                                    $head = '';
                                    // If it's a usermeta field, do not add the table column header
                                }
                                if (!empty($typename2)) {
                                    $istype = true;
                                }
                            } else {
                                if (substr($value, 0, 12) === "wpv-taxonomy") {
                                    // heading table solumns for wpv-taxonomy-* shortcodes
                                    if (in_array($value, array('wpv-taxonomy-link', 'wpv-taxonomy-title'))) {
                                        $head = substr($value, 4);
                                    }
                                    if ($value == 'wpv-taxonomy-post-count') {
                                        $head = 'taxonomy-post_count';
                                    }
                                } else {
                                    if (substr($value, 0, 8) === "wpv-user") {
                                        // heaading table columns for wpv-user shortcodes
                                        preg_match('/\\wpv-user.*?field=\\"(.*?)\\"/', $value, $new_match);
                                        if (isset($new_match[1]) && in_array($new_match[1], $user_fields_with_head)) {
                                            $head = $new_match[1];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            ?>
                    <option value="<?php 
            echo base64_encode('[' . $value . ']');
            ?>
"
                            data-fieldname="<?php 
            echo $items[0];
            ?>
"
                            data-headename="<?php 
            echo $head;
            ?>
"
                            <?php 
            if ($istype) {
                ?>
                            
                            data-istype="1"
                            data-typename="<?php 
                echo $typename2;
                ?>
"
                            <?php 
            }
            ?>
                            data-rowtitle="<?php 
            echo $items[0];
            ?>
" <?php 
            echo $selected;
            ?>
>
                        <?php 
            echo $items[0];
            ?>
                    </option>

                <?php 
        }
        ?>
            </optgroup>
        <?php 
    }
    ?>
    </select>
    <?php 
    //aditional combo for body-templates
    ?>

    <p class="layout-wizard-body-template-text js-layout-wizard-body-template-text <?php 
    if (!preg_match('/wpv-post-body/', $_POST['selected']) || !empty($selected_body_template)) {
        ?>
hidden<?php 
    }
    ?>
"><?php 
    echo __('Using Content Template', 'wpv-views');
    ?>
</p>
    <select name="layout-wizard-body-template" class="layout-wizard-body-template <?php 
    if (!preg_match('/wpv-post-body/', $_POST['selected']) || !empty($selected_body_template)) {
        ?>
hidden<?php 
    }
    ?>
">
        <?php 
    foreach ($content_templates['Content template'] as $items) {
        ?>
        	<option value="<?php 
        echo base64_encode('[' . $items[1] . ']');
        ?>
" data-rowtitle="<?php 
        echo $items[0];
        ?>
" <?php 
        if (trim($items[0]) == trim($selected_body_template)) {
            echo 'selected';
        }
        ?>
 > <?php 
        echo $items[0];
        ?>
</option>
        <?php 
    }
    ?>
    </select>
    <?php 
    if ((string) $settings["query_type"][0] == 'users') {
        $type_usermeta_addon = ' data-type="views-usermeta"';
    }
    ?>
    <button class="button-secondary js-custom-types-fields" 
    <?php 
    if (!preg_match('/types.*?field=|types.*?usermeta=/', $selected_value) || !function_exists('types_get_fields')) {
        ?>
 style="display: none" <?php 
    } else {
        ?>
  rel="<?php 
        echo $typename;
        ?>
" <?php 
    }
    ?>
        <?php 
    if (isset($type_usermeta_addon)) {
        echo $type_usermeta_addon;
    }
    ?>
>
    	<?php 
    echo __('Edit', 'wpv-views');
    ?>
    </button>
    <i class="icon-remove-sign js-layout-wizard-remove-field"></i>
</li>
<?php 
    $result = array('html' => ob_get_clean(), 'selected_found' => $selected_found);
    echo json_encode($result);
    die;
}