/** * Menu page display. */ function wpcf_admin_menu_summary() { echo wpcf_add_admin_header(__('Custom Fields', 'wpcf')); require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-list.php'; wpcf_admin_fields_list(); $to_display = wpcf_admin_fields_get_fields(); if (!empty($to_display)) { $promotional_text = '<div class="message updated" style="padding: 5px 20px;"><h3>' . __('Want to display custom content easily?', 'wpcf') . '</h3>'; $promotional_text .= '<p style="font-size: 110%;">' . __('<a href="http://wp-types.com">Views</a> plugin let\'s you create dynamic templates for single pages and complex content lists. It queries content from the database, filters it and displays in any way you choose.', 'wpcf') . '</p>'; if (defined('WPV_VERSION')) { // Views active $promotional_text .= '<ul style="list-style-type:disc; list-style-position: inside; font-size: 110%;"><li><a href="' . admin_url('edit.php?post_type=view-template') . '">' . __('Create <strong>View Templates</strong> for single pages', 'wpcf') . '</a></li>'; $promotional_text .= '<li><a href="' . admin_url('edit.php?post_type=view') . '">' . __('Create <strong>Views</strong> for content lists', 'wpcf') . '</a></li>'; $promotional_text .= '<li><a href="http://wp-types.com">' . __('Find <strong>documentation</strong> and <strong>help</strong>', 'wpcf') . '</a></li></ul>'; } else { $promotional_text .= '<p style="font-size: 110%;">' . __('Learn more:', 'wpcf') . '</p>'; $promotional_text .= '<ul style="list-style-type:disc; list-style-position: inside; font-size: 110%;"><li><a href="http://wp-types.com/documentation/user-guides/view-templates/">' . __('Creating dynamic templates', 'wpcf') . '</a></li>'; $promotional_text .= '<li><a href="http://wp-types.com/documentation/user-guides/views/">' . __('Query content and display it', 'wpcf') . '</a></li>'; $promotional_text .= '</ul><p style="font-size: 110%;">' . __('Get Views:', 'wpcf') . '</p><ul>'; $promotional_text .= '<li style="font-size: 110%;"><a href="http://wp-types.com/buy/">' . __('Buy and download Views', 'wpcf') . '</a></li>'; $promotional_text .= '</ul>'; } $promotional_text .= '</div>'; echo '<br />' . $promotional_text; } echo wpcf_add_admin_footer(); }
static function wpv_filters_add_filter_usermeta_field($filters) { $basic = array(array(__('First Name', 'wpv-views'), 'first_name', 'Basic', ''), array(__('Last Name', 'wpv-views'), 'last_name', 'Basic', ''), array(__('Nickname', 'wpv-views'), 'nickname', 'Basic', ''), array(__('Description', 'wpv-views'), 'description', 'Basic', ''), array(__('Yahoo IM', 'wpv-views'), 'yim', 'Basic', ''), array(__('Jabber', 'wpv-views'), 'jabber', 'Basic', ''), array(__('AIM', 'wpv-views'), 'aim', 'Basic', '')); foreach ($basic as $b_filter) { $filters['usermeta-field-basic-' . str_replace(' ', '_', $b_filter[1])] = array('name' => sprintf(__('User field - %s', 'wpv-views'), $b_filter[0]), 'present' => 'usermeta-field-' . $b_filter[1] . '_compare', 'callback' => array('WPV_Usermeta_Field_Filter', 'wpv_add_new_filter_usermeta_field_list_item'), 'args' => array('name' => 'usermeta-field-' . $b_filter[1]), 'group' => __('User data', 'wpv-views')); } // @todo review this for gods sake!!!!!!!!!!!!!!!!!!!!!!!! if (function_exists('wpcf_admin_fields_get_groups')) { $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']; $filters['usermeta-field-' . str_replace(' ', '_', $field['meta_key'])] = array('name' => sprintf(__('User field - %s', 'wpv-views'), $field['name']), 'present' => 'usermeta-field-' . $field['meta_key'] . '_compare', 'callback' => array('WPV_Usermeta_Field_Filter', 'wpv_add_new_filter_usermeta_field_list_item'), 'args' => array('name' => 'usermeta-field-' . $field['meta_key'])); } } } } $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)) { $filters['usermeta-field-' . str_replace(' ', '_', $cf['meta_key'])] = array('name' => sprintf(__('User field - %s', 'wpv-views'), $cf['name']), 'present' => 'usermeta-field-' . $cf['meta_key'] . '_compare', 'callback' => array('WPV_Usermeta_Field_Filter', 'wpv_add_new_filter_usermeta_field_list_item'), 'args' => array('name' => 'usermeta-field-' . $cf['meta_key'])); } } } $meta_keys = get_user_meta_keys(); foreach ($meta_keys as $key) { $key_nicename = ''; if (stripos($key, 'wpcf-') === 0) { if (function_exists('wpcf_admin_fields_get_groups')) { continue; } } else { $key_nicename = $key; } $filters['usermeta-field-' . str_replace(' ', '_', $key)] = array('name' => sprintf(__('User field - %s', 'wpv-views'), $key_nicename), 'present' => 'usermeta-field-' . $key . '_compare', 'callback' => array('WPV_Usermeta_Field_Filter', 'wpv_add_new_filter_usermeta_field_list_item'), 'args' => array('name' => 'usermeta-field-' . $key)); } return $filters; }
/** * * Views-Shortcode: wpv-control * * Description: Add filters for View * * Parameters: * type: type of retrieved field layout (radio, checkbox, select, textfield, checkboxes, datepicker) * url_param: the URL parameter passed as an argument * values: Optional. a list of supplied values * display_values: Optional. A list of values to display for the corresponding values * auto_fill: Optional. When set to a "field-slug" the control will be populated with custom field values from the database. * auto_fill_default: Optional. Used to set the default, unselected, value of the control. eg Ignore or Don't care * auto_fill_sort: Optional. 'asc', 'desc', 'ascnum', 'descnum', 'none'. Defaults to ascending. * field: Optional. a Types field to retrieve values from * title: Optional. Use for the checkbox title * taxonomy: Optional. Use when a taxonomy control should be displayed. * default_label: Optional. Use when a taxonomy control should be displayed using select input type. * date_format: Optional. Used for a datepicker control * * Example usage: * * Link: * More details about this shortcode here: <a href="http://wp-types.com/documentation/wpv-control-fields-in-front-end-filters/" title="wpv-control – Displaying fields in front-end filters">http://wp-types.com/documentation/wpv-control-fields-in-front-end-filters/</a> * * Note: * */ function wpv_shortcode_wpv_control($atts) { if (!isset($atts['url_param'])) { return __('The url_param is missing from the wpv-control shortcode argument.', 'wpv-views'); } if ((!isset($atts['type']) || $atts == '') && !isset($atts['field'])) { return __('The "type" or "field" needs to be set in the wpv-control shortcode argument.', 'wpv-views'); } extract(shortcode_atts(array('type' => '', 'values' => array(), 'display_values' => array(), 'field' => '', 'url_param' => '', 'title' => '', 'taxonomy' => '', 'default_label' => '', 'auto_fill' => '', 'auto_fill_default' => '', 'auto_fill_sort' => '', 'date_format' => ''), $atts)); if ($taxonomy != '') { // pass the new shortcode attribute $default_label return _wpv_render_taxonomy_control($taxonomy, $type, $url_param, $default_label); } if ($auto_fill != '') { // See if we should handle types checkboxes $types_checkboxes_field = false; if (_wpv_is_field_of_type($auto_fill, 'checkboxes')) { if (!function_exists('wpcf_admin_fields_get_fields')) { if (defined('WPCF_EMBEDDED_ABSPATH')) { include WPCF_EMBEDDED_ABSPATH . '/includes/fields.php'; } } if (function_exists('wpcf_admin_fields_get_fields')) { $fields = wpcf_admin_fields_get_fields(); $field_name = substr($auto_fill, 5); if (isset($fields[$field_name])) { $types_checkboxes_field = true; $db_values = array(); $options = $fields[$field_name]['data']['options']; foreach ($options as $option) { $db_values[] = $option['title']; } switch (strtolower($auto_fill_sort)) { case 'desc': sort($db_values); $db_values = array_reverse($db_values); break; case 'descnum': sort($db_values, SORT_NUMERIC); $db_values = array_reverse($db_values); break; case 'none': break; case 'ascnum': sort($db_values, SORT_NUMERIC); break; default: sort($db_values); break; } } } } if (!$types_checkboxes_field) { if (!function_exists('wpcf_admin_fields_get_fields')) { if (defined('WPCF_EMBEDDED_ABSPATH')) { include WPCF_EMBEDDED_ABSPATH . '/includes/fields.php'; } } if (function_exists('wpcf_admin_fields_get_fields')) { $fields = wpcf_admin_fields_get_fields(); } $field_name = substr($auto_fill, 5); if (isset($fields) && isset($fields[$field_name]) && isset($fields[$field_name]['data']['options'])) { $display_text = array(); $options = $fields[$field_name]['data']['options']; if (isset($options['default'])) { unset($options['default']); } // remove the default option from the array if (isset($fields[$field_name]['data']['display'])) { $display_option = $fields[$field_name]['data']['display']; } foreach ($options as $option) { if (isset($option['value'])) { $db_values[] = $option['value']; } if (isset($display_option) && 'value' == $display_option && isset($option['display_value'])) { $display_text[$option['value']] = $option['display_value']; // fill an array with the actual display values } else { $display_text[$option['value']] = $option['title']; } } } else { global $wpdb; switch (strtolower($auto_fill_sort)) { case 'desc': $db_values = $wpdb->get_col("SELECT DISTINCT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '{$auto_fill}' ORDER BY meta_value DESC"); break; case 'descnum': $db_values = $wpdb->get_col("SELECT DISTINCT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '{$auto_fill}' ORDER BY meta_value + 0 DESC"); break; case 'none': $db_values = $wpdb->get_col("SELECT DISTINCT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '{$auto_fill}'"); break; case 'ascnum': $db_values = $wpdb->get_col("SELECT DISTINCT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '{$auto_fill}' ORDER BY meta_value + 0 ASC"); break; default: $db_values = $wpdb->get_col("SELECT DISTINCT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '{$auto_fill}' ORDER BY meta_value ASC"); break; } } } if ($auto_fill_default != '') { $values = ''; $display_values = $auto_fill_default; $first = false; } else { $values = ''; $display_values = ''; $first = true; } foreach ($db_values as $value) { if ($value) { if (!$first) { $values .= ','; $display_values .= ','; } $values .= $value; if (isset($display_text[$value])) { $display_values .= $display_text[$value]; } else { $display_values .= $value; } $first = false; } } } $out = ''; // Use when values attributes are defined (predefined values to list) if (!empty($values)) { $values_arr = explode(',', $values); if (!empty($display_values)) { $display_values = explode(',', $display_values); } $options = array(); if (!in_array($type, array('radio', 'radios', 'select', 'checkboxes'))) { $type = 'select'; } if ($type == 'radio') { $type = 'radios'; } switch ($type) { case 'checkboxes': $defaults = array(); $original_get = null; if (isset($auto_fill_default)) { // check if the defaul value already exists and set the appropriate arrays and values $num_auto_fill_default_display = array_count_values($display_values); if (isset($num_auto_fill_default_display[$auto_fill_default]) && $num_auto_fill_default_display[$auto_fill_default] > 1 || in_array($auto_fill_default, $values_arr)) { // if the default value is an existing display value or stored value $values_arr_def = array_shift($values_arr); $display_values_def = array_shift($display_values); } $defaults = explode(',', $auto_fill_default); $defaults = array_map('trim', $defaults); } if (isset($_GET[$url_param])) { $original_get = $_GET[$url_param]; $defaults = $_GET[$url_param]; if (is_string($defaults)) { $defaults = explode(',', $defaults); } unset($_GET[$url_param]); } for ($i = 0; $i < count($values_arr); $i++) { $value = $values_arr[$i]; $value = trim($value); // Check for a display value. if (isset($display_values[$i])) { $display_value = $display_values[$i]; } else { $display_value = $value; } $options[$value]['#name'] = $url_param . '[]'; $options[$value]['#title'] = $display_value; $options[$value]['#value'] = $value; $options[$value]['#default_value'] = in_array($value, $defaults) || in_array($options[$value]['#title'], $defaults); // set default using option titles too // $options[$value]['#inline'] = true; // $options[$value]['#after'] = ' '; } $element = wpv_form_control(array('field' => array('#type' => $type, '#id' => 'wpv_control_' . $type . '_' . $url_param, '#name' => $url_param . '[]', '#attributes' => array('style' => ''), '#inline' => true, '#options' => $options))); if ($original_get) { $_GET[$url_param] = $original_get; } break; default: for ($i = 0; $i < count($values_arr); $i++) { $value = $values_arr[$i]; $value = trim($value); // Check for a display value. if (isset($display_values[$i])) { $display_value = $display_values[$i]; } else { $display_value = $value; } $options[$display_value] = $value; } if (count($values_arr) != count($options)) { // if the $values_arr has one more item than $options, there is a repeating value reset on creation: the existing default $default_value = reset($options); } else { // so the default value in this case is the first element in $values_arr $default_value = $values_arr[0]; } if (isset($_GET[$url_param]) && in_array($_GET[$url_param], $options)) { $default_value = $_GET[$url_param]; } $element = wpv_form_control(array('field' => array('#type' => $type, '#id' => 'wpv_control_' . $type . '_' . $url_param, '#name' => $url_param, '#attributes' => array('style' => ''), '#inline' => true, '#options' => $options, '#default_value' => $default_value))); break; } return $element; } else { if (!empty($field)) { // check if Types is active if (!function_exists('wpcf_admin_fields_get_field')) { if (defined('WPCF_EMBEDDED_ABSPATH')) { include WPCF_EMBEDDED_ABSPATH . '/includes/fields.php'; } else { return __('Types plugin is required.', 'wpv-views'); } } if (!function_exists('wpv_form_control')) { include '../common/functions.php'; } // get field options $field_options = wpcf_admin_fields_get_field($field); if (empty($field_options)) { return __('Empty field values or incorrect field defined. ', 'wpv-views'); } $field_options['name'] = wpcf_translate('field ' . $field_options['id'] . ' name', $field_options['name']); // get the type of custom field (radio, checkbox, other) $field_type = $field_options['type']; // override with type if (!empty($type)) { $field_type = $type; } if (!in_array($field_type, array('radio', 'checkbox', 'checkboxes', 'select', 'textfield', 'date', 'datepicker'))) { $field_type = 'textfield'; } // Radio field if ($field_type == 'radio') { $field_radio_options = $field_options['data']['options']; $options = array(); foreach ($field_radio_options as $key => $opts) { if (is_array($opts)) { if (isset($field_options['data']['display']) && 'value' == $field_options['data']['display'] && isset($opts['display_value'])) { $options[$opts['display_value']] = $opts['value']; // if we have an actual display value and is set to be used, use it } else { $options[$opts['title']] = $opts['value']; // else, use the field value title } } } // get the form content $element = wpv_form_control(array('field' => array('#type' => 'radios', '#id' => 'wpv_control_radio_' . $field, '#name' => $url_param, '#attributes' => array('style' => ''), '#inline' => true, '#options' => $options, '#default_value' => isset($_GET[$url_param]) ? $_GET[$url_param] : null))); return $element; } else { if ($field_type == 'checkbox') { if (isset($atts['title'])) { $checkbox_name = $title; } else { $checkbox_name = $field_options['name']; } $element = wpv_form_control(array('field' => array('#type' => 'checkbox', '#id' => 'wpv_control_checkbox_' . $field, '#name' => $url_param, '#attributes' => array('style' => ''), '#inline' => true, '#title' => $checkbox_name, '#value' => $field_options['data']['set_value'], '#default_value' => 0))); return $element; } else { if ($field_type == 'checkboxes') { $defaults = array(); $original_get = null; if (isset($_GET[$url_param])) { $original_get = $_GET[$url_param]; $defaults = $_GET[$url_param]; if (is_string($defaults)) { $defaults = explode(',', $defaults); } unset($_GET[$url_param]); } foreach ($field_options['data']['options'] as $value) { $value = trim($value['title']); $display_value = $value; $options[$value]['#name'] = $url_param . '[]'; $options[$value]['#title'] = $display_value; $options[$value]['#value'] = $value; $options[$value]['#default_value'] = in_array($value, $defaults); // $options[$value]['#inline'] = true; // $options[$value]['#after'] = ' '; } $element = wpv_form_control(array('field' => array('#type' => 'checkboxes', '#id' => 'wpv_control_checkbox_' . $field, '#name' => $url_param . '[]', '#attributes' => array('style' => ''), '#inline' => true, '#options' => $options))); if ($original_get) { $_GET[$url_param] = $original_get; } return $element; } else { if ($field_type == 'select') { $field_select_options = $field_options['data']['options']; $options = array(); foreach ($field_select_options as $key => $opts) { if (is_array($opts)) { $options[$opts['title']] = $opts['value']; } } $default_value = false; if (isset($_GET[$url_param]) && in_array($_GET[$url_param], $options)) { $default_value = $_GET[$url_param]; } $element = wpv_form_control(array('field' => array('#type' => 'select', '#id' => 'wpv_control_select_' . $url_param, '#name' => $url_param, '#attributes' => array('style' => ''), '#inline' => true, '#options' => $options, '#default_value' => $default_value))); return $element; } else { if ($field_type == 'textfield') { $default_value = ''; if (isset($_GET[$url_param])) { $default_value = esc_attr($_GET[$url_param]); } $element = wpv_form_control(array('field' => array('#type' => 'textfield', '#id' => 'wpv_control_textfield_' . $url_param, '#name' => $url_param, '#attributes' => array('style' => ''), '#inline' => true, '#value' => $default_value))); return $element; } else { if ($field_type == 'date' || $field_type == 'datepicker') { $out = wpv_render_datepicker($url_param, $date_format); return $out; } } } } } } return ''; } else { // type parameter without values $default_value = ''; if (isset($_GET[$url_param])) { $default_value = $_GET[$url_param]; } switch ($type) { case 'checkbox': $element = array('field' => array('#type' => $type, '#id' => 'wpv_control_' . $type . '_' . $url_param, '#name' => $url_param, '#attributes' => array('style' => ''), '#inline' => true, '#value' => $default_value)); $element['field']['#title'] = $title; $element = wpv_form_control($element); break; case 'datepicker': $element = wpv_render_datepicker($url_param, $date_format); break; default: $element = array('field' => array('#type' => $type, '#id' => 'wpv_control_' . $type . '_' . $url_param, '#name' => $url_param, '#attributes' => array('style' => ''), '#inline' => true, '#value' => $default_value)); $element = wpv_form_control($element); break; } return $element; } } }
/** * Single condition form elements. * * @param type $data * @param type $condition * @param type $key * @return string */ function wpcf_cd_admin_form_single_filter($data, $condition, $key = null, $group = false, $force_multi = false) { global $wpcf; if ($group) { $name = 'wpcf[group][conditional_display]'; } else { $name = 'wpcf[fields][' . $data['id'] . '][conditional_display]'; } $group_id = isset($_GET['group_id']) ? intval($_GET['group_id']) : false; /* * * * TODO Review this allowing fields from same group as conditional (self loop) * I do not remember allowing fields from same group as conditional (self loop) * on Group Fields edit screen. */ // if ( $group_id && !$group ) {// Allow group to use other fields // $fields = wpcf_admin_fields_get_fields_by_group( $group_id ); // } else { $fields = wpcf_admin_fields_get_fields(true, false, true); ksort($fields, SORT_STRING); // } if ($group) { $_distinct = wpcf_admin_fields_get_fields_by_group($group_id); foreach ($_distinct as $_field_id => $_field) { if (isset($fields[$_field_id])) { unset($fields[$_field_id]); } } } $options = array(); $ignore_field_type_array = array('audio', 'checkboxes', 'embed', 'file', 'image', 'video', 'wysiwyg'); $flag_repetitive = false; foreach ($fields as $field_id => $field) { if (!$group && $data['id'] == $field_id) { continue; } // WE DO NOT ALLOW repetitive fields to be compared. if (wpcf_admin_is_repetitive($field)) { $flag_repetitive = true; continue; } /** * Skip some files */ if (in_array($field['type'], $ignore_field_type_array)) { continue; } /** * build options */ $options[$field_id] = array('#value' => $field_id, '#title' => stripslashes($field['name']), '#attributes' => array('class' => 'wpcf-conditional-select-' . $field['type'])); } /* * Special case * https://icanlocalize.basecamphq.com/projects/7393061-wp-views/todo_items/153565054/comments * * When field is new and only one diff field in list - that * means one field is saved but other not yet. */ $is_new = isset($data['id']) && isset($fields[$data['id']]) ? false : true; $special_stop = false; if ($is_new) { if (count($options) == 1) { $special_stop = true; } } /* * * This means all fields are repetitive and no one left to compare with. * WE DO NOT ALLOW repetitive fields to be compared. */ if (!$group && empty($options) && $flag_repetitive) { return array('cd' => array('#type' => 'markup', '#markup' => '<p class="wpcf-error">' . __('Conditional display is only working based on non-repeating fields. All fields in this group are repeating, so you cannot set their display based on other fields.', 'wpcf') . '</p>' . wpcf_conditional_disable_add_js($data['id']))); } else { if (!$group && (empty($options) || $special_stop)) { return array('cd' => array('#type' => 'markup', '#markup' => '<p>' . __('You will be able to set conditional field display when you save more fields.', 'wpcf') . '</p>')); } } $id = !is_null($key) ? $key : strval('condition_' . wpcf_unique_id(serialize($data) . serialize($condition) . $key . $group)); $form = array(); $before = '<div class="wpcf-cd-entry"><br />'; $form['cd']['field_' . $id] = array('#type' => 'select', '#name' => $name . '[conditions][' . $id . '][field]', '#options' => $options, '#inline' => true, '#before' => $before, '#default_value' => isset($condition['field']) ? $condition['field'] : null, '#attributes' => array('class' => 'wpcf-cd-field')); $form['cd']['operation_' . $id] = array('#type' => 'select', '#name' => $name . '[conditions][' . $id . '][operation]', '#options' => array_flip(wpcf_cd_admin_operations()), '#inline' => true, '#default_value' => isset($condition['operation']) ? $condition['operation'] : null, '#attributes' => array('class' => 'wpcf-cd-operation')); $form['cd']['value_' . $id] = array('#type' => 'textfield', '#name' => $name . '[conditions][' . $id . '][value]', '#inline' => true, '#value' => isset($condition['value']) ? $condition['value'] : '', '#attributes' => array('class' => 'wpcf-cd-value')); /* * * Adjust for date */ if (!empty($condition['value'])) { WPCF_Loader::loadInclude('fields/date/functions.php'); $timestamp = wpcf_fields_date_convert_datepicker_to_timestamp($condition['value']); if ($timestamp !== false) { $date_value = adodb_date('d', $timestamp) . ',' . adodb_date('m', $timestamp) . ',' . adodb_date('Y', $timestamp); $date_function = 'date'; } else { if (wpcf_fields_date_timestamp_is_valid($condition['value'])) { $date_value = adodb_date('d', $condition['value']) . ',' . adodb_date('m', $condition['value']) . ',' . adodb_date('Y', $condition['value']); $date_function = 'date'; } } } if (empty($date_value)) { $date_value = ''; $date_function = false; } $form['cd']['value_date_' . $id] = array('#type' => 'markup', '#markup' => '<br />' . wpcf_conditional_add_date_controls($date_function, $date_value, $name . '[conditions][' . $id . ']'), '#attributes' => array('class' => 'wpcf-cd-value-date')); $form['cd']['remove_' . $id] = array('#type' => 'button', '#name' => 'remove', '#value' => __('Remove condition', 'wpcf'), '#attributes' => array('onclick' => 'wpcfCdRemoveCondition(jQuery(this));', 'class' => 'wpcf-add-condition'), '#after' => '</div><br />'); return $form['cd']; }
/** * 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;" /> <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&wpcf_action=fields_insert' . '&field=' . basename($filename, '.php') . '&page=wpcf-edit-usermeta') . '&_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' . '&page=wpcf-edit' . '&wpcf_action=usermeta_insert_existing' . '&field=' . $field['id']) . '&_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' . '&wpcf_action=remove_from_history2' . '&field_id=' . $field['id']) . '&_wpnonce=' . wp_create_nonce('remove_from_history2') . '&wpcf_warning=' . sprintf(__('Are you sure that you want to remove field %s from history?', 'wpcf'), htmlspecialchars(stripslashes($field['name']))) . '&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; }
/** * Submitted Bulk actions. */ function wpcf_admin_custom_fields_control_bulk_actions($action = '') { if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'custom_fields_control_bulk')) { return; } switch ($action) { case 'wpcf-change-type-bulk': if (true && isset($_POST['wpcf-id']) && isset($_POST['fields'])) { wpcf_admin_custom_fields_change_type($_POST['fields'], $_POST['wpcf-id'], TYPES_CUSTOM_FIELD_GROUP_CPT_NAME); } break; case 'wpcf-deactivate-bulk': $fields = wpcf_admin_fields_get_fields(false, true); foreach ($_POST['fields'] as $field_id) { $field_id = sanitize_text_field($field_id); if (isset($fields[$field_id])) { $fields[$field_id]['data']['disabled'] = 1; wpcf_admin_message_store(sprintf(__('Removed from Types control: %s', 'wpcf'), $fields[$field_id]['name'])); } } wpcf_admin_fields_save_fields($fields); break; case 'wpcf-activate-bulk': $fields = wpcf_admin_fields_get_fields(false, true); $fields_bulk = wpcf_types_cf_under_control('add', array('fields' => $_POST['fields'])); foreach ($fields_bulk as $field_id) { if (isset($fields[$field_id])) { $fields[$field_id]['data']['disabled'] = 0; } wpcf_admin_message_store(sprintf(__('Added to Types control: %s', 'wpcf'), $field_id)); } wpcf_admin_fields_save_fields($fields); break; case 'wpcf-delete-bulk': require_once WPCF_INC_ABSPATH . '/fields.php'; $failed = array(); $success = array(); foreach ($_POST['fields'] as $field_id) { $field_id = sanitize_text_field($field_id); $response = wpcf_admin_fields_delete_field($field_id); if (!$response) { $failed[] = str_replace('_' . md5('wpcf_not_controlled'), '', $field_id); } else { $success[] = $field_id; } } if (!empty($success)) { wpcf_admin_message_store(sprintf(__('Fields %s have been deleted.', 'wpcf'), implode(', ', $success))); } if (!empty($failed)) { wpcf_admin_message_store(sprintf(__('Fields %s are not Types fields. Types wont delete these fields.', 'wpcf'), implode(', ', $failed)), 'error'); } break; case 'wpcf-add-to-group-bulk': case 'wpcf-remove-from-group-bulk': if (true && isset($_POST['wpcf-id']) && isset($_POST['fields'])) { $fields = array_values((array) $_POST['fields']); if (!empty($fields)) { $groups = explode(',', $_POST['wpcf-id']); foreach ($groups as $group_id) { switch ($action) { case 'wpcf-add-to-group-bulk': wpcf_admin_fields_save_group_fields($group_id, $fields, true); break; case 'wpcf-remove-from-group-bulk': wpcf_admin_fields_remove_field_from_group_bulk($group_id, $fields); break; } } } } break; } wp_safe_redirect(esc_url_raw(add_query_arg(array('page' => 'wpcf-custom-fields-control'), admin_url('admin.php')))); die; }
/** * Menu page display. */ function wpcf_admin_menu_summary() { echo wpcf_add_admin_header(__('Custom Fields', 'wpcf')); require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-list.php'; $to_display = wpcf_admin_fields_get_fields(); if (!empty($to_display)) { add_action('wpcf_groups_list_table_after', 'wpcf_admin_promotional_text'); } wpcf_admin_fields_list(); echo wpcf_add_admin_footer(); }
/** * Custom Import function for Module Manager. * * Import selected items given by xmlstring. * * @global type $wpdb * @global type $iclTranslationManagement * @param type $data * @param type $_type * @return \WP_Error|boolean */ function wpcf_admin_import_data_from_xmlstring($data = '', $_type = 'types', $context = 'types') { global $wpdb, $wpcf; /* * * TODO Types 1.3 * Merge with wpcf_admin_import_data() */ $result = array('updated' => 0, 'new' => 0, 'failed' => 0, 'errors' => array()); libxml_use_internal_errors(true); $data = simplexml_load_string($data); if (!$data) { echo '<div class="message error"><p>' . __('Error parsing XML', 'wpcf') . '</p></div>'; foreach (libxml_get_errors() as $error) { return new WP_Error('error_parsing_xml', __('Error parsing XML', 'wpcf') . ' ' . $error->message); } libxml_clear_errors(); return false; } $errors = array(); $imported = false; // Process groups if (!empty($data->groups) && 'groups' == $_type) { $imported = true; $groups = array(); // Set Groups insert data from XML foreach ($data->groups->group as $group) { $group = (array) $group; // TODO 1.2.1 Remove // $_id = wpcf_modman_set_submitted_id( _GROUPS_MODULE_MANAGER_KEY_, // $group['ID'] ); $_id = $group['__types_id']; // If Types check if exists in $_POST if ($context == 'types' || $context == 'modman') { if (!isset($_POST['items']['groups'][$_id])) { continue; } } $group = wpcf_admin_import_export_simplexml2array($group); $group['add'] = true; $group['update'] = false; $groups[$_id] = $group; } // Insert groups foreach ($groups as $group) { $post = array('post_status' => $group['post_status'], 'post_type' => 'wp-types-group', 'post_title' => $group['post_title'], 'post_content' => !empty($group['post_content']) ? $group['post_content'] : ''); if (isset($group['add']) && $group['add']) { $post_to_update = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts}\n WHERE post_title = %s AND post_type = %s", $group['post_title'], 'wp-types-group')); // Update (may be forced by bulk action) if ($group['update'] || !empty($post_to_update)) { if (!empty($post_to_update)) { $post['ID'] = $post_to_update; /* * * Compare checksum to see if updated */ $_checksum = $wpcf->import->checksum('group', $post_to_update, $group['checksum']); $group_wp_id = wp_update_post($post); if (!$group_wp_id) { $errors[] = new WP_Error('group_update_failed', sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title'])); $result['errors'][] = sprintf(__('Group %s update failed', 'wpcf'), $group['post_title']); $result['failed'] += 1; } else { if (!$_checksum) { $result['updated'] += 1; } else { } } } else { $errors[] = new WP_Error('group_update_failed', sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title'])); } } else { // Insert $group_wp_id = wp_insert_post($post, true); if (is_wp_error($group_wp_id)) { $errors[] = new WP_Error('group_insert_failed', sprintf(__('Group "%s" insert failed', 'wpcf'), $group['post_title'])); $result['errors'][] = sprintf(__('Group %s insert failed', 'wpcf'), $group['post_title']); $result['failed'] += 1; } else { $result['new'] += 1; } } // Update meta if (!empty($group['meta'])) { foreach ($group['meta'] as $meta_key => $meta_value) { update_post_meta($group_wp_id, $meta_key, maybe_unserialize($meta_value)); } } $group_check[] = $group_wp_id; if (!empty($post_to_update)) { $group_check[] = $post_to_update; } } } // Process fields if (!empty($data->fields)) { $fields_existing = wpcf_admin_fields_get_fields(); $fields = array(); $fields_check = array(); // Set insert data from XML foreach ($data->fields->field as $field) { $field = wpcf_admin_import_export_simplexml2array($field); $fields[$field['id']] = $field; } // Insert fields foreach ($fields as $field_id => $field) { // If Types check if exists in $_POST // TODO Regular import do not have structure like this if ($context == 'types' || $context == 'modman') { if (!isset($_POST['items']['groups']['__fields__' . $field['slug']])) { continue; } } if (isset($field['add']) && !$field['add'] && !$overwrite_fields) { continue; } if (empty($field['id']) || empty($field['name']) || empty($field['slug'])) { continue; } $_new_field = !isset($fields_existing[$field_id]); if ($_new_field) { $result['new'] += 1; } else { $_checksum = $wpcf->import->checksum('field', $fields_existing[$field_id]['slug'], $field['checksum']); if (!$_checksum) { $result['updated'] += 1; } } $field_data = array(); $field_data['description'] = isset($field['description']) ? $field['description'] : ''; $field_data['data'] = isset($field['data']) && is_array($field['data']) ? $field['data'] : array(); foreach (array('id', 'name', 'type', 'slug', 'meta_key', 'meta_type') as $key) { if (array_key_exists($key, $field)) { $field_data[$key] = $field[$key]; } } $fields_existing[$field_id] = $field_data; $fields_check[] = $field_id; // WPML global $iclTranslationManagement; if (!empty($iclTranslationManagement) && isset($field['wpml_action'])) { $iclTranslationManagement->settings['custom_fields_translation'][wpcf_types_get_meta_prefix($field) . $field_id] = $field['wpml_action']; $iclTranslationManagement->save_settings(); } } update_option('wpcf-fields', $fields_existing); } } // Process types if (!empty($data->types) && 'types' == $_type) { $imported = true; $types_existing = get_option('wpcf-custom-types', array()); $types = array(); $types_check = array(); // Set insert data from XML foreach ($data->types->type as $type) { $type = (array) $type; $type = wpcf_admin_import_export_simplexml2array($type); // TODO 1.2.1 Remove // $_id = wpcf_modman_get_submitted_id( _TYPES_MODULE_MANAGER_KEY_, // $type['id'] ); $_id = strval($type['__types_id']); // If Types check if exists in $_POST if ($context == 'types' || $context == 'modman') { if (!isset($_POST['items']['types'][$_id])) { continue; } } $types[$_id] = $type; } // Insert types foreach ($types as $type_id => $type) { if (isset($type['add']) && !$type['add']) { continue; } if (isset($types_existing[$type_id])) { /* * * Compare checksum to see if updated */ $_checksum = $wpcf->import->checksum('custom_post_type', $type_id, $type['checksum']); if (!$_checksum) { $result['updated'] += 1; } } else { $result['new'] += 1; } /* * Set type */ unset($type['add'], $type['update'], $type['checksum']); $types_existing[$type_id] = $type; $types_check[] = $type_id; } update_option('wpcf-custom-types', $types_existing); // Add relationships /** EMERSON: Restore Types relationships when importing modules */ if (!empty($data->post_relationships)) { $relationship_existing = get_option('wpcf_post_relationship', array()); /** * be sure, $relationship_existing is a array! */ if (!is_array($relationship_existing)) { $relationship_existing = array(); } $relationship = json_decode($data->post_relationships->data, true); if (is_array($relationship)) { $relationship = array_merge($relationship_existing, $relationship); update_option('wpcf_post_relationship', $relationship); } } } // Process taxonomies if (!empty($data->taxonomies) && 'taxonomies' == $_type) { $imported = true; $taxonomies_existing = get_option('wpcf-custom-taxonomies', array()); $taxonomies = array(); $taxonomies_check = array(); // Set insert data from XML foreach ($data->taxonomies->taxonomy as $taxonomy) { // TODO 1.2.1 Remove // $_id = wpcf_modman_get_submitted_id( _TAX_MODULE_MANAGER_KEY_, // $taxonomy['__types_id'] ); $_id = strval($taxonomy->__types_id); // If Types check if exists in $_POST if ($context == 'types' || $context == 'modman') { if (!isset($_POST['items']['taxonomies'][$_id])) { continue; } } $taxonomy = wpcf_admin_import_export_simplexml2array($taxonomy); $taxonomies[$_id] = $taxonomy; } // Insert taxonomies foreach ($taxonomies as $taxonomy_id => $taxonomy) { if (isset($taxonomy['add']) && !$taxonomy['add'] && !$overwrite_tax) { continue; } if (isset($taxonomies_existing[$taxonomy_id])) { /* * * Compare checksum to see if updated */ $_checksum = $wpcf->import->checksum('custom_taxonomy', $taxonomy_id, $taxonomy['checksum']); if (!$_checksum) { $result['updated'] += 1; } } else { $result['new'] += 1; } // Set tax unset($taxonomy['add'], $taxonomy['update'], $taxonomy['checksum']); $taxonomies_existing[$taxonomy_id] = $taxonomy; $taxonomies_check[] = $taxonomy_id; } update_option('wpcf-custom-taxonomies', $taxonomies_existing); } if ($imported) { // WPML bulk registration // TODO WPML move if (wpcf_get_settings('register_translations_on_import')) { wpcf_admin_bulk_string_translation(); } // Flush rewrite rules wpcf_init_custom_types_taxonomies(); flush_rewrite_rules(); } return $result; }
/** * * Views-Shortcode: wpv-control * * Description: Add filters for View * * Parameters: * type: type of retrieved field layout (radio, checkbox, select, textfield, checkboxes, datepicker) * url_param: the URL parameter passed as an argument * values: Optional. a list of supplied values * display_values: Optional. A list of values to display for the corresponding values * auto_fill: Optional. When set to a "field-slug" the control will be populated with custom field values from the database. * auto_fill_default: Optional. Used to set the default, unselected, value of the control. eg Ignore or Don't care * auto_fill_sort: Optional. 'asc', 'desc', 'ascnum', 'descnum', 'none'. Defaults to ascending. * field: Optional. a Types field to retrieve values from * title: Optional. Use for the checkbox title * taxonomy: Optional. Use when a taxonomy control should be displayed. * default_label: Optional. Use when a taxonomy control should be displayed using select input type. * date_format: Optional. Used for a datepicker control * * Example usage: * * Link: * More details about this shortcode here: <a href="http://wp-types.com/documentation/wpv-control-fields-in-front-end-filters/" title="wpv-control – Displaying fields in front-end filters">http://wp-types.com/documentation/wpv-control-fields-in-front-end-filters/</a> * * Note: * */ function wpv_shortcode_wpv_control($atts) { global $WP_Views; $aux_array = $WP_Views->view_used_ids; $view_name = get_post_field('post_name', end($aux_array)); if (!isset($atts['url_param'])) { return __('The url_param is missing from the wpv-control shortcode argument.', 'wpv-views'); } if ((!isset($atts['type']) || $atts == '') && !isset($atts['field'])) { return __('The "type" or "field" needs to be set in the wpv-control shortcode argument.', 'wpv-views'); } extract(shortcode_atts(array('type' => '', 'values' => array(), 'display_values' => array(), 'field' => '', 'url_param' => '', 'title' => '', 'taxonomy' => '', 'taxonomy_orderby' => 'name', 'taxonomy_order' => 'ASC', 'format' => false, 'default_label' => '', 'hide_empty' => 'false', 'auto_fill' => '', 'auto_fill_default' => '', 'auto_fill_sort' => '', 'date_format' => '', 'default_date' => ''), $atts)); if ($taxonomy != '') { // pass the new shortcode attribute $default_label $default_label = wpv_translate($url_param . '_default_label', $default_label, false, 'View ' . $view_name); return _wpv_render_taxonomy_control($taxonomy, $type, $url_param, $default_label, $taxonomy_orderby, $taxonomy_order, $format, $hide_empty); } $multi = ''; $display_values_trans = false; if ($type == 'multi-select') { $type = 'select'; $multi = 'multiple'; } if ($auto_fill != '') { // See if we should handle types checkboxes $types_checkboxes_field = false; $auto_fill_default_trans = false; $display_values_traans = false; if (_wpv_is_field_of_type($auto_fill, 'checkboxes')) { if (!function_exists('wpcf_admin_fields_get_fields')) { if (defined('WPCF_EMBEDDED_ABSPATH')) { include WPCF_EMBEDDED_ABSPATH . '/includes/fields.php'; } } if (function_exists('wpcf_admin_fields_get_fields')) { $fields = wpcf_admin_fields_get_fields(); $field_name = substr($auto_fill, 5); if (isset($fields[$field_name])) { $types_checkboxes_field = true; $db_values = array(); $options = $fields[$field_name]['data']['options']; foreach ($options as $field_key => $option) { $db_values[] = $option['title']; $display_text[$option['title']] = wpv_translate('field ' . $fields[$field_name]['id'] . ' option ' . $field_key . ' title', $option['title'], false, 'plugin Types'); } switch (strtolower($auto_fill_sort)) { case 'desc': sort($db_values); $db_values = array_reverse($db_values); break; case 'descnum': sort($db_values, SORT_NUMERIC); $db_values = array_reverse($db_values); break; case 'none': break; case 'ascnum': sort($db_values, SORT_NUMERIC); break; default: sort($db_values); break; } } } } if (!$types_checkboxes_field) { if (!function_exists('wpcf_admin_fields_get_fields')) { if (defined('WPCF_EMBEDDED_ABSPATH')) { include WPCF_EMBEDDED_ABSPATH . '/includes/fields.php'; } } if (function_exists('wpcf_admin_fields_get_fields')) { $fields = wpcf_admin_fields_get_fields(); } $field_name = substr($auto_fill, 5); if (isset($fields) && isset($fields[$field_name]) && isset($fields[$field_name]['data']['options'])) { $display_text = array(); $options = $fields[$field_name]['data']['options']; if (isset($options['default'])) { unset($options['default']); } // remove the default option from the array if (isset($fields[$field_name]['data']['display'])) { $display_option = $fields[$field_name]['data']['display']; } foreach ($options as $field_key => $option) { if (isset($option['value'])) { $db_values[] = $option['value']; } if (isset($display_option) && 'value' == $display_option && isset($option['display_value'])) { // $display_text[$option['value']] = $option['display_value']; // fill an array with the actual display values $display_text[$option['value']] = wpv_translate('field ' . $fields[$field_name]['id'] . ' option ' . $field_key . ' title', $option['display_value'], false, 'plugin Types'); } else { //$display_text[$option['value']] = $option['title']; $display_text[$option['value']] = wpv_translate('field ' . $fields[$field_name]['id'] . ' option ' . $field_key . ' title', $option['title'], false, 'plugin Types'); } if ($auto_fill_default != '') { // translate the auto_fill_default option if needed, just when it's one of the existing options $auto_fill_default = str_replace('\\,', ',', $auto_fill_default); if ($auto_fill_default == $option['title']) { $auto_fill_default = wpv_translate('field ' . $fields[$field_name]['id'] . ' option ' . $field_key . ' title', $option['title'], false, 'plugin Types'); $auto_fill_default_trans = true; } $auto_fill_default = str_replace(',', '\\,', $auto_fill_default); } } switch (strtolower($auto_fill_sort)) { case 'desc': sort($db_values); $db_values = array_reverse($db_values); break; case 'descnum': sort($db_values, SORT_NUMERIC); $db_values = array_reverse($db_values); break; case 'none': break; case 'ascnum': sort($db_values, SORT_NUMERIC); break; default: sort($db_values); break; } } else { global $wpdb; switch (strtolower($auto_fill_sort)) { case 'desc': $db_values = $wpdb->get_col("SELECT DISTINCT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '{$auto_fill}' ORDER BY meta_value DESC"); break; case 'descnum': $db_values = $wpdb->get_col("SELECT DISTINCT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '{$auto_fill}' ORDER BY meta_value + 0 DESC"); break; case 'none': $db_values = $wpdb->get_col("SELECT DISTINCT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '{$auto_fill}'"); break; case 'ascnum': $db_values = $wpdb->get_col("SELECT DISTINCT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '{$auto_fill}' ORDER BY meta_value + 0 ASC"); break; default: $db_values = $wpdb->get_col("SELECT DISTINCT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '{$auto_fill}' ORDER BY meta_value ASC"); break; } } } if ($auto_fill_default != '') { if (!$auto_fill_default_trans) { // translate the auto_fill_default option when it's not one of the existing options $auto_fill_default = str_replace('\\,', ',', $auto_fill_default); $auto_fill_default = wpv_translate($url_param . '_auto_fill_default', stripslashes($auto_fill_default), false, 'View ' . $view_name); $auto_fill_default = str_replace(',', '\\,', $auto_fill_default); } $values = ''; $display_values = str_replace('\\,', '%comma%', $auto_fill_default); $first = false; } else { $values = ''; $display_values = ''; $first = true; } foreach ($db_values as $value) { if ($value) { if (!$first) { $values .= ','; $display_values .= ','; } $values .= str_replace(',', '%comma%', $value); if (isset($display_text[$value])) { $display_values .= str_replace(',', '%comma%', $display_text[$value]); } else { $display_values .= str_replace(',', '%comma%', $value); } $first = false; } } } else { if (!empty($display_values)) { $display_values_trans = true; } } $out = ''; // Use when values attributes are defined (predefined values to list) if (!empty($values)) { $values_fix = str_replace('\\,', '%comma%', $values); //print_r($values_fix); $values_arr = explode(',', $values_fix); $values_arr = str_replace('%comma%', ',', $values_arr); if (!empty($display_values)) { $display_values = str_replace('\\,', '%comma%', $display_values); $display_values = explode(',', $display_values); $display_values = str_replace('%comma%', ',', $display_values); if ($display_values_trans) { $translated_values = array(); foreach ($display_values as $index => $valuetrans) { $translated_values[$index] = wpv_translate($url_param . '_display_values_' . ($index + 1), stripslashes($valuetrans), false, 'View ' . $view_name); } $display_values = $translated_values; } } $options = array(); if (!in_array($type, array('radio', 'radios', 'select', 'checkboxes'))) { $type = 'select'; } if ($type == 'radio') { $type = 'radios'; } switch ($type) { case 'checkboxes': $defaults = array(); $original_get = null; if (isset($auto_fill_default)) { // check if the defaul value already exists and set the appropriate arrays and values $num_auto_fill_default_display = array_count_values($display_values); $auto_fill_default_trans = str_replace('\\,', ',', $auto_fill_default); if (isset($num_auto_fill_default_display[$auto_fill_default_trans]) && $num_auto_fill_default_display[$auto_fill_default_trans] > 1 || in_array($auto_fill_default_trans, $values_arr)) { // if the default value is an existing display value or stored value $values_arr_def = array_shift($values_arr); $display_values_def = array_shift($display_values); } $defaults = str_replace('\\,', '%comma%', $auto_fill_default); $defaults = explode(',', $defaults); $defaults = str_replace('%comma%', ',', $defaults); $defaults = array_map('trim', $defaults); } if (isset($_GET[$url_param])) { $original_get = $_GET[$url_param]; $defaults = $_GET[$url_param]; if (is_string($defaults)) { $defaults = explode(',', $defaults); } unset($_GET[$url_param]); } for ($i = 0; $i < count($values_arr); $i++) { $value = $values_arr[$i]; $value = trim($value); // Check for a display value. if (isset($display_values[$i])) { $display_value = $display_values[$i]; } else { $display_value = $value; } $options[$value]['#name'] = $url_param . '[]'; $options[$value]['#title'] = $display_value; $options[$value]['#value'] = $value; $options[$value]['#default_value'] = in_array($value, $defaults) || in_array($options[$value]['#title'], $defaults); // set default using option titles too // $options[$value]['#inline'] = true; // $options[$value]['#after'] = ' '; } $element = wpv_form_control(array('field' => array('#type' => $type, '#id' => 'wpv_control_' . $type . '_' . $url_param, '#name' => $url_param . '[]', '#attributes' => array('style' => ''), '#inline' => true, '#options' => $options, '#before' => '<div class="wpcf-checboxes-group">', '#after' => '</div>'))); if ($original_get) { $_GET[$url_param] = $original_get; } break; default: for ($i = 0; $i < count($values_arr); $i++) { $value = $values_arr[$i]; $value = trim($value); // Check for a display value. if (isset($display_values[$i])) { $display_value = $display_values[$i]; } else { $display_value = $value; } $options[$display_value] = $value; } if (count($values_arr) != count($options)) { // if the $values_arr has one more item than $options, there is a repeating value reset on creation: the existing default $default_value = reset($options); } else { // so the default value in this case is the first element in $values_arr $default_value = $values_arr[0]; } if ($type == 'radios') { if (isset($_GET[$url_param]) && in_array($_GET[$url_param], $options)) { $default_value = $_GET[$url_param]; } $element = wpv_form_control(array('field' => array('#type' => $type, '#id' => 'wpv_control_' . $type . '_' . $url_param, '#name' => $url_param, '#attributes' => array('style' => ''), '#inline' => true, '#options' => $options, '#default_value' => $default_value, '#multiple' => $multi))); } else { if (isset($_GET[$url_param])) { if (is_array($_GET[$url_param])) { if (count(array_intersect($_GET[$url_param], $options)) > 0) { $default_value = $_GET[$url_param]; } } else { if (in_array($_GET[$url_param], $options)) { $default_value = $_GET[$url_param]; } } } $element = wpv_form_control(array('field' => array('#type' => $type, '#id' => 'wpv_control_' . $type . '_' . $url_param, '#name' => $url_param . '[]', '#attributes' => array('style' => ''), '#inline' => true, '#options' => $options, '#default_value' => $default_value, '#multiple' => $multi))); } break; } return $element; } else { if (!empty($field)) { // check if Types is active if (!function_exists('wpcf_admin_fields_get_field')) { if (defined('WPCF_EMBEDDED_ABSPATH')) { include WPCF_EMBEDDED_ABSPATH . '/includes/fields.php'; } else { return __('Types plugin is required.', 'wpv-views'); } } if (!function_exists('wpv_form_control')) { include '../common/functions.php'; } //This is important cause wpcf_admin_fields_get_field works with id: $field - 'wpcf-' and search with 'wpcf-'.$field /*if( strpos($field, 'wpcf-') !== false ) { $tmp = explode('wpcf-', $field); $field = $tmp[1]; }*/ // get field options $field_options = wpcf_admin_fields_get_field($field); if (empty($field_options)) { return __('Empty field values or incorrect field defined. ', 'wpv-views'); } $field_options['name'] = wpv_translate('field ' . $field_options['id'] . ' name', $field_options['name'], false, 'plugin Types'); // get the type of custom field (radio, checkbox, other) $field_type = $field_options['type']; // override with type if (!empty($type)) { $field_type = $type; } if (!in_array($field_type, array('radio', 'checkbox', 'checkboxes', 'select', 'textfield', 'date', 'datepicker'))) { $field_type = 'textfield'; } // Radio field if ($field_type == 'radio') { // print_r( $field_options ); $field_radio_options = $field_options['data']['options']; $options = array(); foreach ($field_radio_options as $key => $opts) { if (is_array($opts)) { if (isset($field_options['data']['display']) && 'value' == $field_options['data']['display'] && isset($opts['display_value'])) { $options[$opts['display_value']] = $opts['value']; // if we have an actual display value and is set to be used, use it } else { // else, use the field value title and watch out because checkboxes fields need their titles as values if (_wpv_is_field_of_type('wpcf-' . $field, 'checkboxes')) { $options[wpv_translate('field ' . $field_options['id'] . ' option ' . $key . ' title', $opts['title'], false, 'plugin Types')] = $opts['title']; } else { $options[wpv_translate('field ' . $field_options['id'] . ' option ' . $key . ' title', $opts['title'], false, 'plugin Types')] = $opts['value']; } } } } // get the form content $element = wpv_form_control(array('field' => array('#type' => 'radios', '#id' => 'wpv_control_radio_' . $field, '#name' => $url_param, '#attributes' => array('style' => ''), '#inline' => true, '#options' => $options, '#default_value' => isset($_GET[$url_param]) ? $_GET[$url_param] : null))); return $element; } else { if ($field_type == 'checkbox') { if (isset($atts['title'])) { $checkbox_name = wpv_translate($url_param . '_title', $title, false, 'View ' . $view_name); } else { $checkbox_name = wpv_translate('field ' . $field_options['name'] . ' name', $field_options['name'], false, 'plugin Types'); } $element = wpv_form_control(array('field' => array('#type' => 'checkbox', '#id' => 'wpv_control_checkbox_' . $field, '#name' => $url_param, '#attributes' => array('style' => ''), '#inline' => true, '#title' => $checkbox_name, '#value' => $field_options['data']['set_value'], '#default_value' => 0))); return $element; } else { if ($field_type == 'checkboxes') { $defaults = array(); $original_get = null; if (isset($_GET[$url_param])) { $original_get = $_GET[$url_param]; $defaults = $_GET[$url_param]; if (is_string($defaults)) { $defaults = explode(',', $defaults); } unset($_GET[$url_param]); } if (isset($field_options['data']['options']['default'])) { unset($field_options['data']['options']['default']); } // remove the default option from the array foreach ($field_options['data']['options'] as $key => $value) { $display_value = wpv_translate('field ' . $field_options['id'] . ' option ' . $key . ' title', trim($value['title']), false, 'plugin Types'); if (_wpv_is_field_of_type('wpcf-' . $field, 'checkboxes')) { $value = trim($value['title']); } else { $value = trim($value['value']); } $options[$value]['#name'] = $url_param . '[]'; $options[$value]['#title'] = $display_value; $options[$value]['#value'] = $value; $options[$value]['#default_value'] = in_array($value, $defaults); // $options[$value]['#inline'] = true; // $options[$value]['#after'] = ' '; } $element = wpv_form_control(array('field' => array('#type' => 'checkboxes', '#id' => 'wpv_control_checkbox_' . $field, '#name' => $url_param . '[]', '#attributes' => array('style' => ''), '#inline' => true, '#options' => $options))); if ($original_get) { $_GET[$url_param] = $original_get; } return $element; } else { if ($field_type == 'select') { $field_select_options = $field_options['data']['options']; $options = array(); foreach ($field_select_options as $key => $opts) { if (is_array($opts)) { if (_wpv_is_field_of_type('wpcf-' . $field, 'checkboxes')) { $options[wpv_translate('field ' . $field_options['id'] . ' option ' . $key . ' title', $opts['title'], false, 'plugin Types')] = $opts['title']; } else { $options[wpv_translate('field ' . $field_options['id'] . ' option ' . $key . ' title', $opts['title'], false, 'plugin Types')] = $opts['value']; } } } $default_value = false; if (isset($_GET[$url_param]) && in_array($_GET[$url_param], $options)) { $default_value = $_GET[$url_param]; } $element = wpv_form_control(array('field' => array('#type' => 'select', '#id' => 'wpv_control_select_' . $url_param, '#name' => $url_param, '#attributes' => array('style' => ''), '#inline' => true, '#options' => $options, '#default_value' => $default_value))); return $element; } else { if ($field_type == 'textfield') { $default_value = ''; if (isset($_GET[$url_param])) { $default_value = stripslashes(urldecode(sanitize_text_field($_GET[$url_param]))); } $element = wpv_form_control(array('field' => array('#type' => 'textfield', '#id' => 'wpv_control_textfield_' . $url_param, '#name' => $url_param, '#attributes' => array('style' => ''), '#inline' => true, '#value' => $default_value))); return $element; } else { if ($field_type == 'date' || $field_type == 'datepicker') { $out = wpv_render_datepicker($url_param, $date_format, $default_date); return $out; } } } } } } return ''; } else { // type parameter without values $default_value = ''; if (isset($_GET[$url_param])) { $default_value = $_GET[$url_param]; } switch ($type) { case 'checkbox': $element = array('field' => array('#type' => $type, '#id' => 'wpv_control_' . $type . '_' . $url_param, '#name' => $url_param, '#attributes' => array('style' => ''), '#inline' => true, '#value' => $default_value)); $element['field']['#title'] = wpv_translate($url_param . '_title', $title, false, 'View ' . $view_name); $element = wpv_form_control($element); break; case 'datepicker': $element = wpv_render_datepicker($url_param, $date_format, $default_date); break; default: $element = array('field' => array('#type' => $type, '#id' => 'wpv_control_' . $type . '_' . $url_param, '#name' => $url_param, '#attributes' => array('style' => ''), '#inline' => true, '#value' => $default_value)); $element = wpv_form_control($element); break; } return $element; } } }
/** * wpcf_get_fields * * returns the fields handled by types * */ function wpcf_get_post_meta_field_names() { require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; $fields = wpcf_admin_fields_get_fields(); $field_names = array(); foreach ($fields as $field) { $field_names[] = wpcf_types_get_meta_prefix($field) . $field['slug']; } return $field_names; }
/** * Add custom fields column helper. * * Add custom fields as a sortable columns on custom post admin list * * @since 1.7 * * @param array $columns Hashtable of columns; * @param string $mode Work Mode. * * @return array Hashtable of columns; */ private function manage_posts_columns_common($columns, $mode = 'normal') { $screen = get_current_screen(); if (!isset($screen->post_type)) { return $columns; } $custom_post_types = wpcf_get_active_custom_types(); if (!isset($custom_post_types[$screen->post_type]) || !$this->check_has_custom_fields($custom_post_types[$screen->post_type]) || !isset($custom_post_types[$screen->post_type]['custom_fields']) || empty($custom_post_types[$screen->post_type]['custom_fields'])) { return $columns; } $fields = wpcf_admin_fields_get_fields(); foreach (array_keys($custom_post_types[$screen->post_type]['custom_fields']) as $full_id) { $data = array(); $key = null; foreach ($fields as $field_key => $field_data) { if (!isset($field_data['meta_key'])) { continue; } if ($full_id != $field_data['meta_key']) { continue; } $key = $field_key; $data = $field_data; } if (!isset($data['meta_key'])) { continue; } if (isset($custom_post_types[$screen->post_type]['custom_fields'][$data['meta_key']])) { switch ($mode) { case 'sortable': switch ($data['type']) { /** * turn of sorting for complex data */ case 'skype': $columns[$data['meta_key']] = false; break; default: $columns[$data['meta_key']] = $data['meta_key']; break; } break; case 'normal': default: $columns[$data['meta_key']] = $data['name']; break; } } } return $columns; }
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(); }
/** * Auto-register the computed values as Types fields. * This method automatically creates the Woocommerce Views filter custom fields and group. * Hooked to wp_loaded * @access public * @return void */ public function wpv_register_typesfields_func() { //Define WC Views canonical custom field array $wc_views_custom_fields_array = array('views_woo_price', 'views_woo_on_sale', 'views_woo_in_stock'); //Preparation to Types control $wc_views_fields_array = array(); $string_wpcf_not_controlled = md5('wpcf_not_controlled'); foreach ($wc_views_custom_fields_array as $key => $value) { $wc_views_fields_array[] = $value . '_' . $string_wpcf_not_controlled; } if (defined('WPCF_INC_ABSPATH')) { //First, check if WC Views Types Group field does not exist if (!$this->check_if_types_group_exist('WooCommerce Views filter fields')) { require_once WPCF_INC_ABSPATH . '/fields.php'; //Part 1: Assign to Types Control //Get Fields $fields = wpcf_admin_fields_get_fields(false, true); $fields_bulk = wpcf_types_cf_under_control('add', array('fields' => $wc_views_fields_array)); foreach ($fields_bulk as $field_id) { if (isset($fields[$field_id])) { $fields[$field_id]['data']['disabled'] = 0; } } //Save fields wpcf_admin_fields_save_fields($fields); //Retrieve updated fields $fields = wpcf_admin_fields_get_fields(false, false); //Assign names foreach ($fields as $key => $value) { if ($key == 'views_woo_price') { $fields['views_woo_price']['name'] = 'WooCommerce Product Price'; } elseif ($key == 'views_woo_on_sale') { $fields['views_woo_on_sale']['name'] = 'Product On Sale Status'; } elseif ($key == 'views_woo_in_stock') { $fields['views_woo_in_stock']['name'] = 'Product In Stock Status'; } } //Save fields wpcf_admin_fields_save_fields($fields); //Define group $group = array('name' => 'WooCommerce Views filter fields', 'description' => '', 'filters_association' => 'any', 'conditional_display' => array('relation' => 'AND', 'custom' => ''), 'preview' => 'edit_mode', 'admin_html_preview' => '', 'admin_styles' => '', 'slug' => 'wc-views-types-groups-fields'); //Save group $group_id = wpcf_admin_fields_save_group($group); //Save group fields wpcf_admin_fields_save_group_fields($group_id, $fields_bulk); } } }
/** * Get fields. * * Parameters for * wpcf_admin_fields_get_fields() * * $only_active = false * $disabled_by_type = false * $strictly_active = false * * @return \stdClass */ public static function getFields($args = array(), $toolset = 'types') { $active = isset($args['active']) ? (bool) $args['active'] : true; return wpcf_admin_fields_get_fields($active); }
/** * Initialize any url param controls that haven't been set already * This is for Views that were created before we had front end filters. * */ function _wpv_initialize_url_param_controls($view_settings) { if (function_exists('wpcf_admin_fields_get_fields')) { $fields = wpcf_admin_fields_get_fields(); } else { $fields = array(); } if (!isset($view_settings['filter_controls_param'])) { $view_settings['filter_controls_field_name'] = array(); $view_settings['filter_controls_param'] = array(); $view_settings['filter_controls_enable'] = array(); $view_settings['filter_controls_label'] = array(); $view_settings['filter_controls_values'] = array(); $view_settings['filter_controls_type'] = array(); $view_settings['filter_controls_mode'] = array(); } $url_params = wpv_custom_fields_get_url_params($view_settings); $url_params = array_merge($url_params, wpv_taxonomy_get_url_params($view_settings)); $search_param = wpv_search_get_url_params($view_settings); $url_params = array_merge($url_params, $search_param); foreach ($url_params as $url_param) { // see if it's already set $exists = false; foreach ($view_settings['filter_controls_param'] as $param) { if ($param == $url_param['param']) { $exists = true; break; } } if (!$exists) { // Doesn't exist so we add the control. $view_settings['filter_controls_field_name'][] = $url_param['name']; $view_settings['filter_controls_param'][] = $url_param['param']; $view_settings['filter_controls_enable'][] = 0; $label = $url_param['param']; $type = 'text'; switch ($url_param['mode']) { case 'cf': foreach ($fields as $field) { if ($url_param['name'] == wpcf_types_get_meta_prefix($field) . $field['slug']) { $label = $field['name']; $type = 'types-auto'; break; } } break; case 'tax': $label = $url_param['cat']->labels->name; break; case 'search': $label = $url_param['name']; break; } $view_settings['filter_controls_label'][] = $label; $view_settings['filter_controls_values'][] = ''; $view_settings['filter_controls_type'][] = $type; $view_settings['filter_controls_mode'][] = $url_param['mode']; } } return $view_settings; }
/** * Changes field type. * * @param type $fields * @param type $type */ function wpcf_admin_custom_fields_change_type($fields, $type) { if (!is_array($fields)) { $fields = array(strval($fields)); } $fields = wpcf_types_cf_under_control('add', array('fields' => $fields, 'type' => $type)); $allowed = array('textfield' => array('wysiwyg', 'textfield', 'textarea', 'email', 'url', 'date', 'phone', 'file', 'image', 'numeric'), 'textarea' => array('wysiwyg', 'textfield', 'textarea', 'email', 'url', 'date', 'phone', 'file', 'image', 'numeric'), 'date' => array('wysiwyg', 'date', 'textarea', 'textfield', 'email', 'url', 'phone', 'file', 'image', 'numeric'), 'email' => array('wysiwyg', 'email', 'textarea', 'textfield', 'date', 'url', 'phone', 'file', 'image', 'numeric'), 'file' => array('wysiwyg', 'file', 'textarea', 'textfield', 'email', 'url', 'phone', 'fdate', 'image', 'numeric'), 'image' => array('wysiwyg', 'image', 'textarea', 'textfield', 'email', 'url', 'phone', 'file', 'idate', 'numeric'), 'numeric' => array('wysiwyg', 'numeric', 'textarea', 'textfield', 'email', 'url', 'phone', 'file', 'image', 'date'), 'phone' => array('wysiwyg', 'phone', 'textarea', 'textfield', 'email', 'url', 'date', 'file', 'image', 'numeric'), 'select' => array('wysiwyg', 'select', 'textarea', 'textfield', 'date', 'email', 'url', 'phone', 'file', 'image', 'numeric'), 'skype' => array('wysiwyg', 'skype', 'textarea', 'textfield', 'date', 'email', 'url', 'phone', 'file', 'image', 'numeric'), 'url' => array('wysiwyg', 'url', 'textarea', 'textfield', 'email', 'date', 'phone', 'file', 'image', 'numeric'), 'checkbox' => array('wysiwyg', 'checkbox', 'textarea', 'textfield', 'email', 'url', 'date', 'phone', 'file', 'image', 'numeric'), 'radio' => array('wysiwyg', 'radio', 'textarea', 'textfield', 'email', 'url', 'date', 'phone', 'file', 'image', 'numeric'), 'wysiwyg' => array('wysiwyg', 'textarea')); $all_fields = wpcf_admin_fields_get_fields(); foreach ($fields as $field_id) { if (!isset($all_fields[$field_id])) { continue; } $field = $all_fields[$field_id]; if (!in_array($type, $allowed[$field['type']])) { wpcf_admin_message_store(sprintf(__('Field "%s" type was converted from %s to %s. You need to set some further settings in the group editor.', 'wpcf'), $field['name'], $field['type'], $type)); $all_fields[$field_id]['data']['disabled_by_type'] = 1; } else { $all_fields[$field_id]['data']['disabled'] = 0; $all_fields[$field_id]['data']['disabled_by_type'] = 0; } $all_fields[$field_id]['type'] = $type; } update_option('wpcf-fields', $all_fields); }
function wpv_ajax_wpv_get_types_field_name() { // TODO check where this is used, maybe create a function to handle it past wpnonce verification if (wp_verify_nonce($_POST['wpv_nonce'], 'wpv_get_types_field_name_nonce')) { if (!defined('WPCF_VERSION')) { echo json_encode(array('found' => false, 'name' => $_POST['field'])); } else { if (defined('WPCF_INC_ABSPATH')) { require_once WPCF_INC_ABSPATH . '/fields.php'; } if (function_exists('wpcf_admin_fields_get_fields')) { $fields = wpcf_admin_fields_get_fields(); } else { $fields = array(); } $found = false; foreach ($fields as $field) { if ($_POST['field'] == wpcf_types_get_meta_prefix($field) . $field['slug']) { echo json_encode(array('found' => true, 'name' => $field['name'])); $found = true; break; } } if (!$found) { echo json_encode(array('found' => false, 'name' => $_POST['field'])); } } } 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 get_field_conditionals($form, $data) { /** * Sanitize form */ if (!is_array($form)) { $form = array(); } if (isset($data['meta_type'])) { switch ($data['meta_type']) { case 'postmeta': $form['data-dependant-meta'] = array('#type' => 'markup', '#markup' => '<span id="data-dependant-meta" style="display:none;" data-wpcf-action="wpcf_edit_field_condition_get" data-wpcf-id="' . $data['name'] . '" data-wpcf-group-id="' . $data['id'] . '" data-wpcf-buttons-apply-nonce="' . wp_create_nonce('wpcf-conditional-apply-' . $data['id']) . '" data-wpcf-meta-type="postmeta"></span>'); break; case 'custom_fields_group': case 'custom-fields-group': $form['data-dependant-meta'] = array('#type' => 'markup', '#markup' => '<span id="data-dependant-meta" style="display:none;" data-wpcf-action="wpcf_edit_custom_field_group_get" data-wpcf-id="' . $data['name'] . '" data-wpcf-group-id="' . $data['id'] . '" data-wpcf-buttons-apply-nonce="' . wp_create_nonce('wpcf-conditional-apply-' . $data['id']) . '" data-wpcf-meta-type="custom_fields_group"></span>'); break; } } $use_custom_logic = $this->use_custom_logic($data); $datas = wpcf_admin_fields_get_fields(true, false, true); ksort($datas, SORT_STRING); $form['form-begin'] = array('#type' => 'markup', '#markup' => '<form>'); $form['simple-logic-open'] = array('#type' => 'markup', '#markup' => sprintf('<div class="js-wpcf-simple-logic %s">', $use_custom_logic ? 'hidden' : '')); $form['description'] = array('#type' => 'markup', '#markup' => wpautop(__("Specify additional filters that control this fields group display, based on values of other custom fields.", 'wpcf'))); $show_relation = true && isset($data['data']['conditional_display']) && isset($data['data']['conditional_display']['conditions']) && !empty($data['data']['conditional_display']['conditions']); $form['cd-relation'] = array('#title' => __('Relation between conditions:', 'wpcf'), '#type' => 'radios', '#name' => 'relation', '#options' => array('AND' => array('#title' => 'AND', '#inline' => true, '#value' => 'AND'), 'OR' => array('#title' => 'OR', '#inline' => true, '#value' => 'OR')), '#default_value' => isset($data['data']['conditional_display']['relation']) ? $data['data']['conditional_display']['relation'] : 'AND', '#inline' => true, '#before' => sprintf('<div class="wpcf-cd-relation simple-logic %s">', $show_relation && 1 < count($data['data']['conditional_display']['conditions']) ? '' : 'hidden'), '#after' => '</div>'); $form['options-markup-open'] = array('#type' => 'markup', '#title' => __('Options', 'wpcf'), '#markup' => sprintf('<table id="wpcf-conditional"><thead><tr>' . '<td class="wpcf-form-options-header-title">%s</th>' . '<td class="wpcf-form-options-header-value">%s</th>' . '<td class="wpcf-form-options-header-default">%s</th>' . '</tr></thead>' . '<tbody id="%s-sortable" class="js-wpcf-fields-conditions">', __('Field', 'wpcf'), __('Condition', 'wpcf'), __('Value', 'wpcf'), esc_attr($data['id']))); if ($show_relation) { $group = isset($data['meta_type']) && 'custom-fields-group' == $data['meta_type']; require_once WPCF_ABSPATH . '/includes/conditional-display.php'; foreach ($data['data']['conditional_display']['conditions'] as $condition) { $form += wpcf_cd_admin_form_single_filter($data, $condition, null, $group); } } $form['options-response-close'] = array('#type' => 'markup', '#markup' => '</table>'); $form['button-add'] = array('#type' => 'button', '#inline' => true, '#value' => isset($data['data']['conditional_display']['conditions']) ? __('Add another condition', 'wpcf') : __('Add condition', 'wpcf'), '#name' => 'button-add', '#attributes' => array('class' => sprintf('js-wpcf-condition-button-add-row %s', isset($data['data']['conditional_display']['conditions']) ? 'alignright' : 'wpcf-block-center'), 'data-wpcf-nonce' => wp_create_nonce($this->get_nonce(__CLASS__, $data['id'], $data['meta_type'])), 'data-wpcf-id' => $data['id'], 'data-wpcf-meta-type' => $data['meta_type'], 'data-wpcf-label-add-condition' => __('Add condition', 'wpcf'), 'data-wpcf-label-add-another-condition' => __('Add another condition', 'wpcf')), '#before' => '<p>', '#after' => '</p>'); $form['simple-logic-close'] = array('#type' => 'markup', '#markup' => '</div>'); /** * Advanced conditional */ $form['toggle_open_area'] = array('#type' => 'markup', '#markup' => sprintf('<div class="js-wpcf-advance-logic %s">', $use_custom_logic ? '' : 'hidden')); $form['custom-description'] = array('#type' => 'markup', '#markup' => wpautop(__('Customize conditions', 'wpcf'))); $form['custom'] = array('#type' => 'textarea', '#name' => 'custom', '#inline' => true, '#value' => isset($data['data']['conditional_display']['custom']) ? $data['data']['conditional_display']['custom'] : ''); $form['date_notice'] = array('#type' => 'markup', '#markup' => '<div style="display:none; margin-top:15px;" class="wpcf-cd-notice-date">' . sprintf(__('%sDates can be entered using the date filters »%s', 'wpcf'), '<a href="http://wp-types.com/documentation/user-guides/date-filters/" target="_blank">', '</a>') . '</div>'); $form['toggle_close'] = array('#type' => 'markup', '#markup' => '</div>'); /** * switch button */ $form['switch'] = array('#type' => 'markup', '#markup' => sprintf('<a href="#" data-wpcf-content-advanced="%s" data-wpcf-content-simple="%s" data-wpcf-custom-logic="%s" class="js-wpcf-condition-button-display-logic">%s</a>', __('Advanced text mode', 'wpcf'), __('Switch to basic mode', 'wpcf'), $use_custom_logic ? 'advance-logic' : 'simple-logic', $use_custom_logic ? __('Switch to basic mode', 'wpcf') : __('Advanced text mode', 'wpcf')), '#before' => '<p class="wpcf-fields-conditional-switch-container">', '#after' => '</p>', '#pattern' => '<BEFORE><ELEMENT><AFTER>'); $form['custom_use'] = array('#type' => 'hidden', '#name' => 'custom_use', '#value' => $use_custom_logic, '#attributes' => array('class' => 'js-wpcf-condition-custom-use')); $form['form-end'] = array('#type' => 'markup', '#markup' => '</form>'); return $form; }
/** * Bulk translation. */ function wpcf_admin_bulk_string_translation() { if (!function_exists('icl_register_string')) { return false; } require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/custom-types.php'; require_once WPCF_INC_ABSPATH . '/custom-types-form.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/custom-taxonomies.php'; require_once WPCF_INC_ABSPATH . '/custom-taxonomies-form.php'; // Register groups $groups = wpcf_admin_fields_get_groups(); foreach ($groups as $group_id => $group) { wpcf_translate_register_string('plugin Types', 'group ' . $group_id . ' name', $group['name']); if (isset($group['description'])) { wpcf_translate_register_string('plugin Types', 'group ' . $group_id . ' description', $group['description']); } } // Register fields $fields = wpcf_admin_fields_get_fields(); foreach ($fields as $field_id => $field) { wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' name', $field['name']); if (isset($field['description'])) { wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' description', $field['description']); } // For radios or select if (!empty($field['data']['options'])) { foreach ($field['data']['options'] as $name => $option) { if ($name == 'default') { continue; } if (isset($option['title'])) { wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' title', $option['title']); } if (isset($option['value'])) { wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' value', $option['value']); } if (isset($option['display_value'])) { wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' option ' . $name . ' display value', $option['display_value']); } } } if ($field['type'] == 'checkbox' && (isset($field['set_value']) && $field['set_value'] != '1')) { // we need to translate the check box value to store wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value', $field['set_value']); } if ($field['type'] == 'checkbox' && !empty($field['display_value_selected'])) { // we need to translate the check box value to store wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value selected', $field['display_value_selected']); } if ($field['type'] == 'checkbox' && !empty($field['display_value_not_selected'])) { // we need to translate the check box value to store wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' checkbox value not selected', $field['display_value_not_selected']); } // Validation message if (!empty($field['data']['validate'])) { foreach ($field['data']['validate'] as $method => $validation) { if (!empty($validation['message'])) { // Skip if it's same as default $default_message = wpcf_admin_validation_messages($method); if ($validation['message'] != $default_message) { wpcf_translate_register_string('plugin Types', 'field ' . $field_id . ' validation message ' . $method, $validation['message']); } } } } } // Register types $custom_types = get_option('wpcf-custom-types', array()); foreach ($custom_types as $post_type => $data) { wpcf_custom_types_register_translation($post_type, $data); } // Register taxonomies $custom_taxonomies = get_option('wpcf-custom-taxonomies', array()); foreach ($custom_taxonomies as $taxonomy => $data) { wpcf_custom_taxonimies_register_translation($taxonomy, $data); } }
/** * Header HTML formatted output. * * Each header <th> is array element. Sortable. * * @return array 'header_id' => html */ function headers() { // Sorting $dir = isset($_GET['sort']) && $_GET['sort'] == 'ASC' ? 'DESC' : 'ASC'; $dir_default = 'ASC'; $sort_field = isset($_GET['field']) ? sanitize_text_field($_GET['field']) : ''; // Set values $post = $this->parent; $post_type = $this->child_post_type; $parent_post_type = $this->parent_post_type; $data = $this->data; $wpcf_fields = wpcf_admin_fields_get_fields(true); $headers = array(); foreach ($this->headers as $k => $header) { if ($k === '__parents' || $k === '__taxonomies') { continue; } if ($header == '_wp_title') { if ($this->child_supports['title']) { $title_dir = $sort_field == '_wp_title' ? $dir : 'ASC'; $headers[$header] = ''; $headers[$header] .= $sort_field == '_wp_title' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : ''; $headers[$header] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&wpcf_action=pr_sort&field=' . '_wp_title&sort=' . $title_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Title', 'wpcf') . '</a>'; } else { $headers[$header] = 'ID'; } } else { if ($header == '_wp_body') { $body_dir = $sort_field == '_wp_body' ? $dir : $dir_default; $headers[$header] = ''; $headers[$header] .= $sort_field == '_wp_body' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : ''; $headers[$header] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&wpcf_action=pr_sort&field=' . '_wp_body&sort=' . $body_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . __('Post Body', 'wpcf') . '</a>'; } else { if ($header == '_wp_excerpt' || $header == '_wp_featured_image') { $headers[$header] = $this->get_header($header); } else { $link_text = $this->get_header($header); if (strpos($header, WPCF_META_PREFIX) === 0 && isset($wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)])) { wpcf_field_enqueue_scripts($wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)]['type']); $link_text = stripslashes($wpcf_fields[str_replace(WPCF_META_PREFIX, '', $header)]['name']); } $field_dir = $sort_field == $header ? $dir : $dir_default; $headers[$header] = ''; $headers[$header] .= $sort_field == $header ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : ''; $headers[$header] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&wpcf_action=pr_sort&field=' . $header . '&sort=' . $field_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $link_text . '</a>'; } } } } if (!empty($this->headers['__parents'])) { foreach ($this->headers['__parents'] as $_parent => $data) { if ($_parent == $parent_post_type) { continue; } $temp_parent_type = get_post_type_object($_parent); if (empty($temp_parent_type)) { continue; } $parent_dir = $sort_field == '_wpcf_pr_parent' ? $dir : $dir_default; $headers['_wpcf_pr_parent_' . $_parent] = $sort_field == '_wpcf_pr_parent' ? '<div class="wpcf-pr-sort-' . $dir . '"></div>' : ''; $headers['_wpcf_pr_parent_' . $_parent] .= '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&wpcf_action=pr_sort&field=' . '_wpcf_pr_parent&sort=' . $parent_dir . '&post_id=' . $post->ID . '&post_type=' . $post_type . '&post_type_sort_parent=' . $_parent . '&_wpnonce=' . wp_create_nonce('pr_sort')) . '">' . $temp_parent_type->label . '</a>'; } } if (!empty($this->headers['__taxonomies'])) { foreach ($this->headers['__taxonomies'] as $tax_id => $taxonomy) { $headers["_wpcf_pr_taxonomy_{$tax_id}"] = $taxonomy; } } return $headers; }
/** * All AJAX calls go here. * * @global object $wpdb * */ function wpcf_ajax() { if (!current_user_can('manage_options') || (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], $_REQUEST['wpcf_action']))) { die; } switch ($_REQUEST['wpcf_action']) { /* User meta actions*/ case 'user_fields_control_bulk': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-control.php'; require_once WPCF_INC_ABSPATH . '/usermeta-control.php'; wpcf_admin_user_fields_control_bulk_ajax(); break; case 'usermeta_delete': case 'delete_usermeta': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['field_id'])) { $field_id = sanitize_text_field($_GET['field_id']); wpcf_admin_fields_delete_field($field_id, 'wp-types-user-group', 'wpcf-usermeta'); } if (isset($_GET['field'])) { $field = sanitize_text_field($_GET['field']); wpcf_admin_fields_delete_field($field, 'wp-types-user-group', 'wpcf-usermeta'); } echo json_encode(array('output' => '')); break; case 'remove_from_history2': require_once WPCF_INC_ABSPATH . '/fields.php'; $fields = wpcf_admin_fields_get_fields(true, true, false, 'wpcf-usermeta'); if (isset($_GET['field_id']) && isset($fields[$_GET['field_id']])) { $fields[$_GET['field_id']]['data']['removed_from_history'] = 1; wpcf_admin_fields_save_fields($fields, true, 'wpcf-usermeta'); } echo json_encode(array('output' => '')); break; case 'deactivate_user_group': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/usermeta.php'; $success = wpcf_admin_fields_deactivate_group(intval($_GET['group_id']), 'wp-types-user-group'); if ($success) { echo json_encode(array('output' => __('Group deactivated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_user_group': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/usermeta.php'; $success = wpcf_admin_fields_activate_group(intval($_GET['group_id']), 'wp-types-user-group'); if ($success) { echo json_encode(array('output' => __('Group activated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_usermeta_group': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/usermeta.php'; wpcf_admin_fields_delete_group(intval($_GET['group_id']), 'wp-types-user-group'); echo json_encode(array('output' => '', 'execute' => 'location.reload();')); break; case 'usermeta_insert_existing': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; require_once WPCF_INC_ABSPATH . '/usermeta-form.php'; wpcf_usermeta_insert_existing_ajax(); wpcf_form_render_js_validation(); break; /* End Usertmeta actions*/ /* End Usertmeta actions*/ case 'fields_insert': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; wpcf_fields_insert_ajax(); wpcf_form_render_js_validation(); break; case 'fields_insert_existing': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; wpcf_fields_insert_existing_ajax(); wpcf_form_render_js_validation(); break; case 'remove_field_from_group': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['group_id']) && isset($_GET['field_id'])) { wpcf_admin_fields_remove_field_from_group(intval($_GET['group_id']), sanitize_text_field($_GET['field_id'])); } break; case 'deactivate_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $success = wpcf_admin_fields_deactivate_group(intval($_GET['group_id'])); if ($success) { echo json_encode(array('output' => __('Group deactivated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $success = wpcf_admin_fields_activate_group(intval($_GET['group_id'])); if ($success) { echo json_encode(array('output' => __('Group activated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_group': require_once WPCF_INC_ABSPATH . '/fields.php'; wpcf_admin_fields_delete_group(intval($_GET['group_id'])); echo json_encode(array('output' => '', 'execute' => 'location.reload();')); break; case 'deactivate_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-types.php'; $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); if (isset($custom_types[$_GET['wpcf-post-type']])) { $custom_types[$_GET['wpcf-post-type']]['disabled'] = 1; $custom_types[$_GET['wpcf-post-type']][TOOLSET_EDIT_LAST] = time(); update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types); echo json_encode(array('output' => __('Post type deactivated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-types.php'; $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); if (isset($custom_types[$_GET['wpcf-post-type']])) { unset($custom_types[$_GET['wpcf-post-type']]['disabled']); $custom_types[$_GET['wpcf-post-type']][TOOLSET_EDIT_LAST] = time(); update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types); echo json_encode(array('output' => __('Post type activated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); $custom_type = strval($_GET['wpcf-post-type']); /** * Delete relation between custom posts types * * Filter allow to delete all custom fields used to make * a relation between posts. * * @since 1.6.4 * * @param bool $delete True or false flag to delete relationships. * @param string $var Currently deleted custom post type. */ if (apply_filters('wpcf_delete_relation_meta', false, $custom_type)) { global $wpdb; $wpdb->delete($wpdb->postmeta, array('meta_key' => sprintf('_wpcf_belongs_%s_id', $custom_type)), array('%s')); } unset($custom_types[$custom_type]); /** * remove post relation */ foreach (array_keys($custom_types) as $post_type) { if (array_key_exists('post_relationship', $custom_types[$post_type])) { /** * remove "has" relation */ if (array_key_exists('has', $custom_types[$post_type]['post_relationship']) && array_key_exists($custom_type, $custom_types[$post_type]['post_relationship']['has'])) { unset($custom_types[$post_type]['post_relationship']['has'][$custom_type]); $custom_types[$post_type][TOOLSET_EDIT_LAST] = time(); } /** * remove "belongs" relation */ if (array_key_exists('belongs', $custom_types[$post_type]['post_relationship']) && array_key_exists($custom_type, $custom_types[$post_type]['post_relationship']['belongs'])) { unset($custom_types[$post_type]['post_relationship']['belongs'][$custom_type]); $custom_types[$post_type][TOOLSET_EDIT_LAST] = time(); } } } update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types); wpcf_admin_deactivate_content('post_type', $custom_type); echo json_encode(array('output' => '', 'execute' => 'location.reload();')); break; case 'duplicate_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); $custom_type = esc_html(strval($_GET['wpcf-post-type'])); if (isset($custom_types[$custom_type])) { $i = 0; $key = false; do { $key = sprintf($custom_type . '-%d', ++$i); } while (isset($custom_types[$key])); if ($key) { /** * duplicate custom post type */ $custom_types[$key] = $custom_types[$custom_type]; /** * update some options */ $custom_types[$key]['labels']['name'] .= sprintf(' (%d)', $i); $custom_types[$key]['labels']['singular_name'] .= sprintf(' (%d)', $i); $custom_types[$key]['slug'] = $key; $custom_types[$key]['__types_id'] = $key; /** * update custom post types */ update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types); /** * update custom taxonomies too */ $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); foreach ($custom_taxonomies as $taxonomy_key => $taxonomy_data) { if (isset($taxonomy_data['supports']) && isset($taxonomy_data['supports'][$custom_type])) { $custom_taxonomies[$taxonomy_key]['supports'][$key] = 1; } } update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies); echo json_encode(array('execute' => 'location.reload();')); } } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'taxonomy_duplicate': if (!isset($_GET['wpcf-tax'])) { die; } $custom_taxonomy = esc_html(strval($_GET['wpcf-tax'])); require_once WPCF_INC_ABSPATH . '/custom-taxonomies.php'; $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); if (isset($custom_taxonomies[$custom_taxonomy])) { $i = 0; $key = false; do { $key = sprintf($custom_taxonomy . '-%d', ++$i); } while (isset($custom_taxonomies[$key])); if ($key) { /** * duplicate custom taxonomies */ $custom_taxonomies[$key] = $custom_taxonomies[$custom_taxonomy]; /** * update some options */ $custom_taxonomies[$key]['labels']['name'] .= sprintf(' (%d)', $i); $custom_taxonomies[$key]['labels']['singular_name'] .= sprintf(' (%d)', $i); $custom_taxonomies[$key]['slug'] = $key; $custom_taxonomies[$key]['id'] = $key; $custom_taxonomies[$key]['__types_id'] = $key; /** * update custom taxonomies */ update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies); /** * update post types */ if (isset($custom_taxonomies[$key]['supports']) && is_array($custom_taxonomies[$key]['supports']) && !empty($custom_taxonomies[$key]['supports'])) { $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); foreach (array_keys($custom_taxonomies[$key]['supports']) as $custom_type) { /** * avoid to create fake CPT from old data */ if (!isset($custom_types[$custom_type])) { continue; } if (!isset($custom_types[$custom_type]['taxonomies'])) { $custom_types[$custom_type]['taxonomies'] = array(); } $custom_types[$custom_type]['taxonomies'][$key] = 1; } /** * update custom post types */ update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types); } echo json_encode(array('execute' => 'location.reload();')); } } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'deactivate_taxonomy': if (!isset($_GET['wpcf-tax'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-taxonomies.php'; $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); if (isset($custom_taxonomies[$_GET['wpcf-tax']])) { $custom_taxonomies[$_GET['wpcf-tax']]['disabled'] = 1; $custom_taxonomies[$_GET['wpcf-tax']][TOOLSET_EDIT_LAST] = time(); update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies); echo json_encode(array('output' => __('Taxonomy deactivated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_taxonomy': if (!isset($_GET['wpcf-tax'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-taxonomies.php'; $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); if (isset($custom_taxonomies[$_GET['wpcf-tax']])) { $custom_taxonomies[$_GET['wpcf-tax']]['disabled'] = 0; $custom_taxonomies[$_GET['wpcf-tax']][TOOLSET_EDIT_LAST] = time(); update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies); echo json_encode(array('output' => __('Taxonomy activated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_taxonomy': if (!isset($_GET['wpcf-tax'])) { die; } $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); $custom_taxonomy = strval($_GET['wpcf-tax']); unset($custom_taxonomies[$custom_taxonomy]); update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies); wpcf_admin_deactivate_content('taxonomy', $custom_taxonomy); echo json_encode(array('output' => '', 'execute' => 'location.reload();')); break; case 'add_radio_option': require_once WPCF_INC_ABSPATH . '/fields/radio.php'; $element = wpcf_fields_radio_get_option(urldecode($_GET['parent_name'])); $id = array_shift($element); $element_txt = wpcf_fields_radio_get_option_alt_text($id, urldecode($_GET['parent_name'])); echo json_encode(array('output' => wpcf_form_simple($element), 'execute' => 'jQuery("#wpcf-form-groups-radio-ajax-response-' . urldecode($_GET['wpcf_ajax_update_add']) . '").append(\'' . trim(str_replace("\r\n", '', wpcf_form_simple($element_txt))) . '\');', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'add_select_option': require_once WPCF_INC_ABSPATH . '/fields/select.php'; $element = wpcf_fields_select_get_option(urldecode($_GET['parent_name'])); echo json_encode(array('output' => wpcf_form_simple($element))); break; case 'add_checkboxes_option': require_once WPCF_INC_ABSPATH . '/fields/checkboxes.php'; $element = wpcf_fields_checkboxes_get_option(urldecode($_GET['parent_name'])); $id = array_shift($element); $element_txt = wpcf_fields_checkboxes_get_option_alt_text($id, urldecode($_GET['parent_name'])); echo json_encode(array('output' => wpcf_form_simple($element), 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'group_form_collapsed': require_once WPCF_INC_ABSPATH . '/fields-form.php'; $group_id = sanitize_text_field($_GET['group_id']); $action = sanitize_text_field($_GET['toggle']); $fieldset = sanitize_text_field($_GET['id']); wpcf_admin_fields_form_save_open_fieldset($action, $fieldset, $group_id); break; case 'form_fieldset_toggle': $action = sanitize_text_field($_GET['toggle']); $fieldset = sanitize_text_field($_GET['id']); wpcf_admin_form_fieldset_save_toggle($action, $fieldset); break; case 'group_update_post_types': require_once WPCF_INC_ABSPATH . '/fields.php'; // @todo Sanitize this $post_types $post_types = empty($_GET['wpcf']['group']['supports']) ? array() : $_GET['wpcf']['group']['supports']; if (!empty($_GET['group_id'])) { wpcf_admin_fields_save_group_post_types(sanitize_text_field($_GET['group_id']), $post_types); $output = array(); foreach ($post_types as $post_type) { $post_type = get_post_type_object($post_type); if (!empty($post_type->label)) { $output[] = $post_type->label; } } if (empty($post_types)) { $output[] = __('No post types associated', 'wpcf'); } $output = implode(', ', $output); } else { $output = __('No post types associated', 'wpcf'); } echo json_encode(array('output' => $output)); break; case 'group_update_taxonomies': require_once WPCF_INC_ABSPATH . '/fields.php'; $taxonomies_post = empty($_GET['wpcf']['group']['taxonomies']) ? array() : $_GET['wpcf']['group']['taxonomies']; $terms = array(); foreach ($taxonomies_post as $taxonomy) { foreach ($taxonomy as $tax => $term) { $terms[] = $term; } } if (!empty($_GET['group_id'])) { wpcf_admin_fields_save_group_terms(sanitize_text_field($_GET['group_id']), $terms); $output = array(); foreach ($taxonomies_post as $taxonomy => $terms) { $taxonomy = get_taxonomy($taxonomy); if (!empty($taxonomy)) { $title = $taxonomy->label . ': '; foreach ($terms as $term_id) { $term = get_term($term_id, $taxonomy->name); $output[] = $title . $term->name; $title = ''; } } } if (empty($output)) { $output[] = __('No taxonomies associated', 'wpcf'); } $output = implode(', ', $output); } else { $output = __('No taxonomies associated', 'wpcf'); } echo json_encode(array('output' => $output)); break; case 'custom_fields_control_bulk': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-control.php'; wpcf_admin_custom_fields_control_bulk_ajax(); break; case 'fields_delete': case 'delete_field': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['field_id'])) { wpcf_admin_fields_delete_field(sanitize_text_field($_GET['field_id'])); } if (isset($_GET['field'])) { wpcf_admin_fields_delete_field(sanitize_text_field($_GET['field'])); } echo json_encode(array('output' => '')); break; case 'remove_from_history': require_once WPCF_INC_ABSPATH . '/fields.php'; $fields = wpcf_admin_fields_get_fields(); if (isset($_GET['field_id']) && isset($fields[$_GET['field_id']])) { $fields[$_GET['field_id']]['data']['removed_from_history'] = 1; wpcf_admin_fields_save_fields($fields, true); } echo json_encode(array('output' => '')); break; case 'add_condition': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_ABSPATH . '/includes/conditional-display.php'; if (!empty($_GET['field']) || !empty($_GET['group'])) { $data = array(); if (isset($_GET['group'])) { $output = wpcf_form_simple(wpcf_cd_admin_form_single_filter(array(), array(), null, true)); echo json_encode(array('output' => $output)); } else { $data['id'] = str_replace('_conditional_display', '', sanitize_text_field($_GET['field'])); $output = wpcf_form_simple(wpcf_cd_admin_form_single_filter($data, array(), null, false)); if (!empty($data['id'])) { echo json_encode(array('output' => $output)); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } } } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'pt_edit_fields': if (!empty($_GET['parent']) && !empty($_GET['child'])) { require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/post-relationship.php'; wpcf_pr_admin_edit_fields(sanitize_text_field($_GET['parent']), sanitize_text_field($_GET['child'])); } break; case 'toggle': $option = get_option('wpcf_toggle', array()); $hidden = isset($_GET['hidden']) ? (bool) $_GET['hidden'] : 1; $_GET['div'] = strval($_GET['div']); if (!$hidden) { unset($option[$_GET['div']]); } else { $option[$_GET['div']] = 1; } update_option('wpcf_toggle', $option); break; case 'cb_save_empty_migrate': $output = sprintf('<span style="color:red;">%s</div>', __('Migration process is not yet finished - please save group first, then change settings of this field.', 'wpcf')); if (isset($_GET['field']) && isset($_GET['subaction'])) { require_once WPCF_INC_ABSPATH . '/fields.php'; $option = $_GET['meta_type'] == 'usermeta' ? 'wpcf-usermeta' : 'wpcf-fields'; $meta_type = sanitize_text_field($_GET['meta_type']); $field = wpcf_admin_fields_get_field(sanitize_text_field($_GET['field']), false, false, false, $option); $_txt_updates = $meta_type == 'usermeta' ? __('%d users require update', 'wpcf') : __('%d posts require update', 'wpcf'); $_txt_no_updates = $meta_type == 'usermeta' ? __('No users require update', 'wpcf') : __('No posts require update', 'wpcf'); $_txt_updated = $meta_type == 'usermeta' ? __('Users updated', 'wpcf') : __('Posts updated', 'wpcf'); if (!empty($field)) { if ($_GET['subaction'] == 'save_check' || $_GET['subaction'] == 'do_not_save_check') { if ($field['type'] == 'checkbox') { $posts = wpcf_admin_fields_checkbox_migrate_empty_check($field, $_GET['subaction']); } else { if ($field['type'] == 'checkboxes') { $posts = wpcf_admin_fields_checkboxes_migrate_empty_check($field, $_GET['subaction']); } } if (!empty($posts)) { $output = '<div class="message updated"><p>' . sprintf($_txt_updates, count($posts)) . ' ' . '<a href="javascript:void(0);" class="button-primary" onclick="' . 'wpcfCbSaveEmptyMigrate(jQuery(this).parent().parent().parent(), \'' . sanitize_text_field($_GET['field']) . '\', ' . count($posts) . ', \'' . wp_create_nonce('cb_save_empty_migrate') . '\', \''; $output .= $_GET['subaction'] == 'save_check' ? 'save' : 'do_not_save'; $output .= '\', \'' . $meta_type . '\');' . '">' . __('Update') . '</a>' . '</p></div>'; } else { $output = '<div class="message updated"><p><em>' . $_txt_no_updates . '</em></p></div>'; } } else { if ($_GET['subaction'] == 'save' || $_GET['subaction'] == 'do_not_save') { if ($field['type'] == 'checkbox') { $posts = wpcf_admin_fields_checkbox_migrate_empty($field, $_GET['subaction']); } else { if ($field['type'] == 'checkboxes') { $posts = wpcf_admin_fields_checkboxes_migrate_empty($field, $_GET['subaction']); } } if (isset($posts['offset'])) { if (!isset($_GET['total'])) { $output = '<span style="color:red;">' . __('Error occured', 'wpcf') . '</div>'; } else { $output = '<script type="text/javascript">wpcfCbMigrateStep(' . intval($_GET['total']) . ',' . $posts['offset'] . ',' . '\'' . sanitize_text_field($_GET['field']) . '\',' . '\'' . wp_create_nonce('cb_save_empty_migrate') . '\', \'' . $meta_type . '\');</script>' . number_format($posts['offset']) . '/' . number_format(intval($_GET['total'])) . '<div class="wpcf-ajax-loading-small"></div>'; } } else { $output = '<div class="message updated"><p>' . $_txt_updated . '</p></div>'; } } } } } echo json_encode(array('output' => $output)); break; default: break; } die; }
/** * Imports data from XML. * * @global object $wpdb * */ function wpcf_admin_import_data($data = '', $redirect = true, $context = 'types', $args = array()) { global $wpdb; $data_installer = false; libxml_use_internal_errors(true); $data = simplexml_load_string($data); if (!$data) { echo '<div class="message error"><p>' . __('Error parsing XML', 'wpcf') . '</p></div>'; foreach (libxml_get_errors() as $error) { echo '<div class="message error"><p>' . $error->message . '</p></div>'; } libxml_clear_errors(); return false; } $overwrite_settings = isset($_POST['overwrite-settings']); $overwrite_groups = isset($_POST['overwrite-groups']); $overwrite_fields = isset($_POST['overwrite-fields']); $overwrite_types = isset($_POST['overwrite-types']); $overwrite_tax = isset($_POST['overwrite-tax']); $delete_groups = isset($_POST['delete-groups']); $delete_fields = isset($_POST['delete-fields']); $delete_types = isset($_POST['delete-types']); $delete_tax = isset($_POST['delete-tax']); if ('wpvdemo' == $context && !empty($args)) { /** * allow overwrite */ $overwrite_groups = true; $overwrite_fields = true; $overwrite_types = true; $overwrite_tax = true; include_once dirname(__FILE__) . '/classes/class.types.data.installer.php'; $data_installer = new Types_Data_Installer($data, $args); $data = $data_installer->wpvdemo(); } /** * process settings */ if ($overwrite_settings && isset($data->settings)) { $wpcf_settings = wpcf_get_settings(); foreach (wpcf_admin_import_export_simplexml2array($data->settings) as $key => $value) { $wpcf_settings[$key] = $value; } wpcf_save_settings($wpcf_settings); wpcf_admin_message_store(__('Settings are updated.', 'wpcf')); } // Process groups $groups_check = array(); if (!empty($data->groups)) { $groups = array(); // Set insert data from XML foreach ($data->groups->group as $group) { $group = wpcf_admin_import_export_simplexml2array($group); $groups[$group['ID']] = $group; } // Set insert data from POST if (!empty($_POST['groups'])) { foreach ($_POST['groups'] as $group_id => $group) { if (empty($groups[$group_id])) { continue; } $groups[$group_id]['add'] = !empty($group['add']); $groups[$group_id]['update'] = isset($group['update']) && $group['update'] == 'update' ? true : false; } } else { foreach ($groups as $group_id => $group) { $groups[$group_id]['add'] = true; $groups[$group_id]['update'] = false; } } // Insert groups $show_import_fail_version_message = true; foreach ($groups as $group_id => $group) { $post = array('post_status' => $group['post_status'], 'post_type' => TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, 'post_title' => $group['post_title'], 'post_content' => !empty($group['post_content']) ? $group['post_content'] : ''); /** * preserve slug */ if (array_key_exists('__types_id', $group)) { $post['post_name'] = $group['__types_id']; } if (isset($group['add']) && $group['add']) { $post_to_update = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s", $group['post_title'], TYPES_CUSTOM_FIELD_GROUP_CPT_NAME)); // Update (may be forced by bulk action) if ($group['update'] || $overwrite_groups && !empty($post_to_update)) { if (!empty($post_to_update)) { $post['ID'] = $post_to_update; $group_wp_id = wp_update_post($post); if (!$group_wp_id) { wpcf_admin_message_store(sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title']), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" updated', 'wpcf'), $group['post_title'])); } } else { wpcf_admin_message_store(sprintf(__('Group "%s" update failed', 'wpcf'), $group['post_title']), 'error'); } } else { // Insert $group_wp_id = wp_insert_post($post, true); if (is_wp_error($group_wp_id)) { wpcf_admin_message_store(sprintf(__('Group "%s" insert failed', 'wpcf'), $group['post_title']), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" added', 'wpcf'), $group['post_title'])); } } // Update meta if (!empty($group['meta'])) { foreach ($group['meta'] as $meta_key => $meta_value) { if ('_wpcf_conditional_display' == $meta_key) { if (!empty($meta_value)) { $meta_value = wpcf_admin_import_export_simplexml2array($meta_value); if (!is_array($meta_value)) { $meta_value = array(); if ($show_import_fail_version_message) { $show_import_fail_version_message = false; wpcf_admin_message_store(__('The Types settings were not fully imported because it contained unsecured data. You should re-export your Types settings using the latest version of Types', 'wpcf'), 'error'); } } } } update_post_meta($group_wp_id, $meta_key, $meta_value); } } $groups_check[] = $group_wp_id; if (!empty($post_to_update)) { $groups_check[] = $post_to_update; } } } // Delete groups (forced, set in bulk actions) } if ($delete_groups) { $groups_to_delete = get_posts(array('post_type' => TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, 'post_status' => 'any', 'posts_per_page' => -1)); if (!empty($groups_to_delete)) { foreach ($groups_to_delete as $group_to_delete) { if (!in_array($group_to_delete->ID, $groups_check)) { $deleted = wp_delete_post($group_to_delete->ID, true); if (!$deleted) { wpcf_admin_message_store(sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete->post_title), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" deleted', 'wpcf'), $group_to_delete->post_title)); } } } } } else { // If not forced, look in POST if (!empty($_POST['groups-to-be-deleted'])) { foreach ($_POST['groups-to-be-deleted'] as $group_to_delete) { $group_to_delete_post = get_post($group_to_delete); if (!empty($group_to_delete_post) && $group_to_delete_post->post_type == TYPES_CUSTOM_FIELD_GROUP_CPT_NAME) { $deleted = wp_delete_post($group_to_delete, true); if (!$deleted) { wpcf_admin_message_store(sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete_post->post_title), 'error'); } else { wpcf_admin_message_store(sprintf(__('Group "%s" deleted', 'wpcf'), $group_to_delete_post->post_title)); } } else { wpcf_admin_message_store(sprintf(__('Group "%s" delete failed', 'wpcf'), $group_to_delete), 'error'); } } } } // Process fields $fields_check = array(); $fields_existing = wpcf_admin_fields_get_fields(); if (!empty($data->fields)) { $fields = array(); // Set insert data from XML foreach ($data->fields->field as $field) { $field = wpcf_admin_import_export_simplexml2array($field); // Set if submitted in 'types' context if ($context == 'types') { // Process only if marked if (isset($_POST['fields'][$field['id']])) { $fields[$field['id']] = $field; } } else { $fields[$field['id']] = $field; } } // Set insert data from POST if (!empty($_POST['fields'])) { foreach ($_POST['fields'] as $field_id => $field) { if (empty($fields[$field_id])) { continue; } $fields[$field_id]['add'] = !empty($field['add']); $fields[$field_id]['update'] = isset($field['update']) && $field['update'] == 'update' ? true : false; } } // Insert fields foreach ($fields as $field_id => $field) { if (isset($field['add']) && !$field['add'] && !$overwrite_fields) { continue; } if (empty($field['id']) || empty($field['name']) || empty($field['slug'])) { continue; } $field_data = array(); $field_data['description'] = isset($field['description']) ? $field['description'] : ''; $field_data['data'] = isset($field['data']) && is_array($field['data']) ? $field['data'] : array(); foreach (array('id', 'name', 'type', 'slug', 'meta_key', 'meta_type') as $key) { if (array_key_exists($key, $field)) { $field_data[$key] = $field[$key]; } } $fields_existing[$field_id] = $field_data; $fields_check[] = $field_id; // WPML global $iclTranslationManagement; if (!empty($iclTranslationManagement) && isset($field['wpml_action'])) { $iclTranslationManagement->settings['custom_fields_translation'][wpcf_types_get_meta_prefix($field) . $field_id] = $field['wpml_action']; $iclTranslationManagement->save_settings(); } wpcf_admin_message_store(sprintf(__('Field "%s" added/updated', 'wpcf'), $field['name'])); } } // Delete fields if ($delete_fields) { foreach ($fields_existing as $k => $v) { if (!empty($v['data']['controlled'])) { continue; } if (!in_array($k, $fields_check)) { wpcf_admin_message_store(sprintf(__('Field "%s" deleted', 'wpcf'), $fields_existing[$k]['name'])); unset($fields_existing[$k]); } } } else { if (!empty($_POST['fields-to-be-deleted'])) { foreach ($_POST['fields-to-be-deleted'] as $field_to_delete) { wpcf_admin_message_store(sprintf(__('Field "%s" deleted', 'wpcf'), $fields_existing[$field_to_delete]['name'])); unset($fields_existing[$field_to_delete]); } } } update_option('wpcf-fields', $fields_existing); // Process user groups //print_r($data->user_groups);exit; $groups_check = array(); if (!empty($data->user_groups) && isset($data->user_groups->group)) { $groups = array(); // Set insert data from XML foreach ($data->user_groups->group as $group) { $group = wpcf_admin_import_export_simplexml2array($group); $groups[$group['ID']] = $group; } // Set insert data from POST if (!empty($_POST['user_groups'])) { foreach ($_POST['user_groups'] as $group_id => $group) { if (empty($groups[$group_id])) { continue; } $groups[$group_id]['add'] = !empty($group['add']); $groups[$group_id]['update'] = isset($group['update']) && $group['update'] == 'update' ? true : false; } } else { foreach ($groups as $group_id => $group) { $groups[$group_id]['add'] = true; $groups[$group_id]['update'] = false; } } // Insert groups foreach ($groups as $group_id => $group) { $post = array('post_status' => $group['post_status'], 'post_type' => TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'post_title' => $group['post_title'], 'post_content' => !empty($group['post_content']) ? $group['post_content'] : ''); if (isset($group['add']) && $group['add']) { $post_to_update = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = %s", $group['post_title'], TYPES_USER_META_FIELD_GROUP_CPT_NAME)); // Update (may be forced by bulk action) if ($group['update'] || $overwrite_groups && !empty($post_to_update)) { if (!empty($post_to_update)) { $post['ID'] = $post_to_update; $group_wp_id = wp_update_post($post); if (!$group_wp_id) { wpcf_admin_message_store(sprintf(__('User group "%s" update failed', 'wpcf'), $group['post_title']), 'error'); } else { wpcf_admin_message_store(sprintf(__('User group "%s" updated', 'wpcf'), $group['post_title'])); } } else { wpcf_admin_message_store(sprintf(__('User group "%s" update failed', 'wpcf'), $group['post_title']), 'error'); } } else { // Insert $group_wp_id = wp_insert_post($post, true); if (is_wp_error($group_wp_id)) { wpcf_admin_message_store(sprintf(__('User group "%s" insert failed', 'wpcf'), $group['post_title']), 'error'); } else { wpcf_admin_message_store(sprintf(__('User group "%s" added', 'wpcf'), $group['post_title'])); } } // Update meta if (!empty($group['meta'])) { foreach ($group['meta'] as $meta_key => $meta_value) { update_post_meta($group_wp_id, $meta_key, wpcf_admin_import_export_simplexml2array($meta_value)); } } $groups_check[] = $group_wp_id; if (!empty($post_to_update)) { $groups_check[] = $post_to_update; } } } } // Delete groups (forced, set in bulk actions) if ($delete_groups) { $groups_to_delete = get_posts(array('post_type' => TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'post_status' => 'any', 'posts_per_page' => -1)); if (!empty($groups_to_delete)) { foreach ($groups_to_delete as $group_to_delete) { if (!in_array($group_to_delete->ID, $groups_check)) { $deleted = wp_delete_post($group_to_delete->ID, true); if (!$deleted) { wpcf_admin_message_store(sprintf(__('User group "%s" delete failed', 'wpcf'), $group_to_delete->post_title), 'error'); } else { wpcf_admin_message_store(sprintf(__('User group "%s" deleted', 'wpcf'), $group_to_delete->post_title)); } } } } } else { // If not forced, look in POST if (!empty($_POST['user-groups-to-be-deleted'])) { foreach ($_POST['user-groups-to-be-deleted'] as $group_to_delete) { $group_to_delete_post = get_post($group_to_delete); if (!empty($group_to_delete_post) && $group_to_delete_post->post_type == TYPES_USER_META_FIELD_GROUP_CPT_NAME) { $deleted = wp_delete_post($group_to_delete, true); if (!$deleted) { wpcf_admin_message_store(sprintf(__('User group "%s" delete failed', 'wpcf'), $group_to_delete_post->post_title), 'error'); } else { wpcf_admin_message_store(sprintf(__('User group "%s" deleted', 'wpcf'), $group_to_delete_post->post_title)); } } else { wpcf_admin_message_store(sprintf(__('User group "%s" delete failed', 'wpcf'), $group_to_delete), 'error'); } } } } // Process fields $fields_existing = wpcf_admin_fields_get_fields(false, false, false, 'wpcf-usermeta'); $fields_check = array(); if (!empty($data->user_fields)) { $fields = array(); // Set insert data from XML foreach ($data->user_fields->field as $field) { $field = wpcf_admin_import_export_simplexml2array($field); // Set if submitted in 'types' context if ($context == 'types') { // Process only if marked if (isset($_POST['user_fields'][$field['id']])) { $fields[$field['id']] = $field; } } else { $fields[$field['id']] = $field; } } // Set insert data from POST if (!empty($_POST['user_fields'])) { foreach ($_POST['user_fields'] as $field_id => $field) { if (empty($fields[$field_id])) { continue; } $fields[$field_id]['add'] = !empty($field['add']); $fields[$field_id]['update'] = isset($field['update']) && $field['update'] == 'update' ? true : false; } } // Insert fields foreach ($fields as $field_id => $field) { if (isset($field['add']) && !$field['add'] && !$overwrite_fields) { continue; } if (empty($field['id']) || empty($field['name']) || empty($field['slug'])) { continue; } $field_data = array(); $field_data['id'] = $field['id']; $field_data['name'] = $field['name']; $field_data['description'] = isset($field['description']) ? $field['description'] : ''; $field_data['type'] = $field['type']; $field_data['slug'] = $field['slug']; $field_data['data'] = isset($field['data']) && is_array($field['data']) ? $field['data'] : array(); $fields_existing[$field_id] = $field_data; $fields_check[] = $field_id; // WPML global $iclTranslationManagement; if (!empty($iclTranslationManagement) && isset($field['wpml_action'])) { $iclTranslationManagement->settings['custom_fields_translation'][wpcf_types_get_meta_prefix($field) . $field_id] = $field['wpml_action']; $iclTranslationManagement->save_settings(); } wpcf_admin_message_store(sprintf(__('User field "%s" added/updated', 'wpcf'), $field['name'])); } } // Delete fields if ($delete_fields) { foreach ($fields_existing as $k => $v) { if (!empty($v['data']['controlled'])) { continue; } if (!in_array($k, $fields_check)) { wpcf_admin_message_store(sprintf(__('User field "%s" deleted', 'wpcf'), $fields_existing[$k]['name'])); unset($fields_existing[$k]); } } } else { if (!empty($_POST['user-fields-to-be-deleted'])) { foreach ($_POST['user-fields-to-be-deleted'] as $field_to_delete) { wpcf_admin_message_store(sprintf(__('User field "%s" deleted', 'wpcf'), $fields_existing[$field_to_delete]['name'])); unset($fields_existing[$field_to_delete]); } } } update_option('wpcf-usermeta', $fields_existing); // Process types $types_existing = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); $types_check = array(); if (!empty($data->types) && isset($data->types->type)) { $types = array(); // Set insert data from XML foreach ($data->types->type as $type) { $type = wpcf_admin_import_export_simplexml2array($type); // Set if submitted in 'types' context if ($context == 'types') { if (isset($_POST['types'][$type['id']])) { $types[$type['id']] = $type; } } else { $types[$type['id']] = $type; } } // Set insert data from POST if (!empty($_POST['types'])) { foreach ($_POST['types'] as $type_id => $type) { if (empty($types[$type_id])) { continue; } $types[$type_id]['add'] = !empty($type['add']); $types[$type_id]['update'] = isset($type['update']) && $type['update'] == 'update' ? true : false; } } // Insert types foreach ($types as $type_id => $type) { if (isset($type['add']) && !$type['add'] && !$overwrite_types) { continue; } unset($type['add'], $type['update']); $types_existing[$type_id] = $type; $types_check[] = $type_id; wpcf_admin_message_store(sprintf(__('Custom post type "%s" added/updated', 'wpcf'), $type_id)); } } // Delete types if ($delete_types) { foreach ($types_existing as $k => $v) { if (!in_array($k, $types_check)) { unset($types_existing[$k]); wpcf_admin_message_store(sprintf(__('Custom post type "%s" deleted', 'wpcf'), esc_html($k))); } } } else { if (!empty($_POST['types-to-be-deleted'])) { foreach ($_POST['types-to-be-deleted'] as $type_to_delete) { wpcf_admin_message_store(sprintf(__('Custom post type "%s" deleted', 'wpcf'), $types_existing[$type_to_delete]['labels']['name'])); unset($types_existing[$type_to_delete]); } } } update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $types_existing); // Process taxonomies $taxonomies_existing = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); $taxonomies_check = array(); if (!empty($data->taxonomies) && isset($data->taxonomies->taxonomy)) { $taxonomies = array(); // Set insert data from XML foreach ($data->taxonomies->taxonomy as $taxonomy) { $taxonomy = wpcf_admin_import_export_simplexml2array($taxonomy); // Set if submitted in 'types' context if ($context == 'types') { if (isset($_POST['taxonomies'][$taxonomy['id']])) { $taxonomies[$taxonomy['id']] = $taxonomy; } } else { $taxonomies[$taxonomy['id']] = $taxonomy; } } // Set insert data from POST if (!empty($_POST['taxonomies'])) { foreach ($_POST['taxonomies'] as $taxonomy_id => $taxonomy) { if (empty($taxonomies[$taxonomy_id])) { continue; } $taxonomies[$taxonomy_id]['add'] = !empty($taxonomy['add']); $taxonomies[$taxonomy_id]['update'] = isset($taxonomy['update']) && $taxonomy['update'] == 'update' ? true : false; } } // Insert taxonomies foreach ($taxonomies as $taxonomy_id => $taxonomy) { if (isset($taxonomy['add']) && !$taxonomy['add'] && !$overwrite_tax) { continue; } unset($taxonomy['add'], $taxonomy['update']); $taxonomies_existing[$taxonomy_id] = $taxonomy; $taxonomies_check[] = $taxonomy_id; wpcf_admin_message_store(sprintf(__('Custom taxonomy "%s" added/updated', 'wpcf'), $taxonomy_id)); } } /** * reset TOOLSET_EDIT_LAST */ if ($data_installer) { $data_installer->reset_toolset_edit_last(); } // Delete taxonomies if ($delete_tax) { foreach ($taxonomies_existing as $k => $v) { if (!in_array($k, $taxonomies_check)) { unset($taxonomies_existing[$k]); wpcf_admin_message_store(sprintf(__('Custom taxonomy "%s" deleted', 'wpcf'), $k)); } } } else { if (!empty($_POST['taxonomies-to-be-deleted'])) { foreach ($_POST['taxonomies-to-be-deleted'] as $taxonomy_to_delete) { wpcf_admin_message_store(sprintf(__('Custom taxonomy "%s" deleted', 'wpcf'), $taxonomies_existing[$taxonomy_to_delete]['labels']['name'])); unset($taxonomies_existing[$taxonomy_to_delete]); } } } update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $taxonomies_existing); // Add relationships if (!empty($data->post_relationships) && !empty($_POST['post_relationship'])) { $relationship_existing = get_option('wpcf_post_relationship', array()); /** * be sure, $relationship_existing is a array! */ if (!is_array($relationship_existing)) { $relationship_existing = array(); } $relationship = json_decode($data->post_relationships->data, true); if (is_array($relationship)) { $relationship = array_merge($relationship_existing, $relationship); update_option('wpcf_post_relationship', $relationship); wpcf_admin_message_store(__('Post relationships created', 'wpcf')); } else { wpcf_admin_message_store(__('Post relationships settings were not imported because it contained unsecured data. You should re-export your Types settings using the latest version of Types', 'wpcf'), 'error'); } } // WPML bulk registration if (wpcf_get_settings('register_translations_on_import')) { wpcf_admin_bulk_string_translation(); } // Flush rewrite rules wpcf_init_custom_types_taxonomies(); flush_rewrite_rules(); if ($redirect) { echo '<script type="text/javascript"> <!-- window.location = "' . admin_url('admin.php?page=wpcf-import-export') . '" //--> </script>'; die; } }
/** * All AJAX calls go here. */ function wpcf_ajax() { if (!current_user_can('manage_options') || (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], $_REQUEST['wpcf_action']))) { die; } switch ($_REQUEST['wpcf_action']) { case 'fields_insert': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; wpcf_fields_insert_ajax(); wpcf_form_render_js_validation(); break; case 'fields_insert_existing': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; wpcf_fields_insert_existing_ajax(); wpcf_form_render_js_validation(); break; case 'remove_field_from_group': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['group_id']) && isset($_GET['field_id'])) { wpcf_admin_fields_remove_field_from_group($_GET['group_id'], $_GET['field_id']); } break; case 'deactivate_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $success = wpcf_admin_fields_deactivate_group(intval($_GET['group_id'])); if ($success) { echo json_encode(array('output' => __('Group deactivated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . intval($_GET['group_id']) . '").replaceWith(\'' . wpcf_admin_fields_get_ajax_activation_link(intval($_GET['group_id'])) . '\');jQuery(".wpcf-table-column-active-' . intval($_GET['group_id']) . '").html("' . __('No', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $success = wpcf_admin_fields_activate_group(intval($_GET['group_id'])); if ($success) { echo json_encode(array('output' => __('Group activated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . intval($_GET['group_id']) . '").replaceWith(\'' . wpcf_admin_fields_get_ajax_deactivation_link(intval($_GET['group_id'])) . '\');jQuery(".wpcf-table-column-active-' . intval($_GET['group_id']) . '").html("' . __('Yes', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_group': require_once WPCF_INC_ABSPATH . '/fields.php'; wpcf_admin_fields_delete_group(intval($_GET['group_id'])); echo json_encode(array('output' => '', 'execute' => 'jQuery("#wpcf-list-activate-' . intval($_GET['group_id']) . '").parents("tr").css("background-color", "#FF0000").fadeOut();', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'deactivate_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-types.php'; $custom_types = get_option('wpcf-custom-types', array()); if (isset($custom_types[$_GET['wpcf-post-type']])) { $custom_types[$_GET['wpcf-post-type']]['disabled'] = 1; update_option('wpcf-custom-types', $custom_types); echo json_encode(array('output' => __('Post type deactivated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-post-type'] . '").replaceWith(\'' . wpcf_admin_custom_types_get_ajax_activation_link(esc_attr($_GET['wpcf-post-type'])) . '\');jQuery(".wpcf-table-column-active-' . $_GET['wpcf-post-type'] . '").html("' . __('No', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-types.php'; $custom_types = get_option('wpcf-custom-types', array()); if (isset($custom_types[$_GET['wpcf-post-type']])) { $custom_types[$_GET['wpcf-post-type']]['disabled'] = 0; update_option('wpcf-custom-types', $custom_types); echo json_encode(array('output' => __('Post type activated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-post-type'] . '").replaceWith(\'' . wpcf_admin_custom_types_get_ajax_deactivation_link($_GET['wpcf-post-type']) . '\');jQuery(".wpcf-table-column-active-' . $_GET['wpcf-post-type'] . '").html("' . __('Yes', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } $custom_types = get_option('wpcf-custom-types', array()); unset($custom_types[$_GET['wpcf-post-type']]); update_option('wpcf-custom-types', $custom_types); echo json_encode(array('output' => '', 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-post-type'] . '").parents("tr").css("background-color", "#FF0000").fadeOut();', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'deactivate_taxonomy': if (!isset($_GET['wpcf-tax'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-taxonomies.php'; $custom_taxonomies = get_option('wpcf-custom-taxonomies', array()); if (isset($custom_taxonomies[$_GET['wpcf-tax']])) { $custom_taxonomies[$_GET['wpcf-tax']]['disabled'] = 1; update_option('wpcf-custom-taxonomies', $custom_taxonomies); echo json_encode(array('output' => __('Taxonomy deactivated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-tax'] . '").replaceWith(\'' . wpcf_admin_custom_taxonomies_get_ajax_activation_link($_GET['wpcf-tax']) . '\');jQuery(".wpcf-table-column-active-' . $_GET['wpcf-tax'] . '").html("' . __('No', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_taxonomy': if (!isset($_GET['wpcf-tax'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-taxonomies.php'; $custom_taxonomies = get_option('wpcf-custom-taxonomies', array()); if (isset($custom_taxonomies[$_GET['wpcf-tax']])) { $custom_taxonomies[$_GET['wpcf-tax']]['disabled'] = 0; update_option('wpcf-custom-taxonomies', $custom_taxonomies); echo json_encode(array('output' => __('Taxonomy activated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-tax'] . '").replaceWith(\'' . wpcf_admin_custom_taxonomies_get_ajax_deactivation_link($_GET['wpcf-tax']) . '\');jQuery(".wpcf-table-column-active-' . $_GET['wpcf-tax'] . '").html("' . __('Yes', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_taxonomy': if (!isset($_GET['wpcf-tax'])) { die; } $custom_taxonomies = get_option('wpcf-custom-taxonomies', array()); unset($custom_taxonomies[$_GET['wpcf-tax']]); update_option('wpcf-custom-taxonomies', $custom_taxonomies); echo json_encode(array('output' => '', 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-tax'] . '").parents("tr").css("background-color", "#FF0000").fadeOut();', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'add_radio_option': require_once WPCF_INC_ABSPATH . '/fields/radio.php'; $element = wpcf_fields_radio_get_option(urldecode($_GET['parent_name'])); $id = array_shift($element); $element_txt = wpcf_fields_radio_get_option_alt_text($id, urldecode($_GET['parent_name'])); echo json_encode(array('output' => wpcf_form_simple($element), 'execute' => 'jQuery("#wpcf-form-groups-radio-ajax-response-' . urldecode($_GET['wpcf_ajax_update_add']) . '").append(\'' . trim(str_replace("\r\n", '', wpcf_form_simple($element_txt))) . '\');', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'add_select_option': require_once WPCF_INC_ABSPATH . '/fields/select.php'; $element = wpcf_fields_select_get_option(urldecode($_GET['parent_name'])); echo json_encode(array('output' => wpcf_form_simple($element))); break; case 'group_form_collapsed': require_once WPCF_INC_ABSPATH . '/fields-form.php'; $group_id = $_GET['group_id']; $action = $_GET['toggle']; $fieldset = $_GET['id']; wpcf_admin_fields_form_save_open_fieldset($action, $fieldset, $group_id); break; case 'form_fieldset_toggle': $action = $_GET['toggle']; $fieldset = $_GET['id']; wpcf_admin_form_fieldset_save_toggle($action, $fieldset); break; case 'group_update_post_types': require_once WPCF_INC_ABSPATH . '/fields.php'; $post_types = empty($_GET['wpcf']['group']['supports']) ? array() : $_GET['wpcf']['group']['supports']; if (!empty($_GET['group_id'])) { wpcf_admin_fields_save_group_post_types($_GET['group_id'], $post_types); $output = array(); foreach ($post_types as $post_type) { $post_type = get_post_type_object($post_type); if (!empty($post_type->label)) { $output[] = $post_type->label; } } if (empty($post_types)) { $output[] = __('No post types associated', 'wpcf'); } $output = implode(', ', $output); } else { $output = __('No post types associated', 'wpcf'); } echo json_encode(array('output' => $output)); break; case 'group_update_taxonomies': require_once WPCF_INC_ABSPATH . '/fields.php'; $taxonomies_post = empty($_GET['wpcf']['group']['taxonomies']) ? array() : $_GET['wpcf']['group']['taxonomies']; $terms = array(); foreach ($taxonomies_post as $taxonomy) { foreach ($taxonomy as $tax => $term) { $terms[] = $term; } } if (!empty($_GET['group_id'])) { wpcf_admin_fields_save_group_terms($_GET['group_id'], $terms); $output = array(); foreach ($taxonomies_post as $taxonomy => $terms) { $taxonomy = get_taxonomy($taxonomy); if (!empty($taxonomy)) { $title = $taxonomy->label . ': '; foreach ($terms as $term_id) { $term = get_term($term_id, $taxonomy->name); $output[] = $title . $term->name; $title = ''; } } } if (empty($output)) { $output[] = __('No taxonomies associated', 'wpcf'); } $output = implode(', ', $output); } else { $output = __('No taxonomies associated', 'wpcf'); } echo json_encode(array('output' => $output)); break; case 'custom_fields_control_bulk': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-control.php'; wpcf_admin_custom_fields_control_bulk_ajax(); break; case 'fields_delete': case 'delete_field': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['field_id'])) { wpcf_admin_fields_delete_field($_GET['field_id']); } if (isset($_GET['field'])) { wpcf_admin_fields_delete_field($_GET['field']); } echo json_encode(array('output' => '')); break; case 'remove_from_history': require_once WPCF_INC_ABSPATH . '/fields.php'; $fields = wpcf_admin_fields_get_fields(); if (isset($_GET['field_id']) && isset($fields[$_GET['field_id']])) { $fields[$_GET['field_id']]['data']['removed_from_history'] = 1; wpcf_admin_fields_save_fields($fields, true); } echo json_encode(array('output' => '')); break; default: break; } 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&wpcf_action=fields_insert' . '&field=' . basename($filename, '.php')) . '&_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' . '&wpcf_action=fields_insert_existing' . '&field=' . $field['id']) . '&_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' . '&wpcf_action=remove_from_history' . '&field_id=' . $field['id']) . '&_wpnonce=' . wp_create_nonce('remove_from_history') . '&wpcf_warning=' . sprintf(__('Are you sure that you want to remove field %s from history?', 'wpcf'), htmlspecialchars(stripslashes($field['name']))) . '&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>' . ' <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> ' . '<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>' . ' <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> ' . '<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; }
/** * Actions for outside fields control. * * @param type $action */ function wpcf_types_cf_under_control($action = 'add', $args = array(), $post_type = 'wp-types-group', $meta_name = 'wpcf-fields') { global $wpcf_types_under_control; $wpcf_types_under_control['errors'] = array(); switch ($action) { case 'add': $fields = wpcf_admin_fields_get_fields(false, true, false, $meta_name, false); foreach ($args['fields'] as $field_id) { $field_type = !empty($args['type']) ? $args['type'] : 'textfield'; if (strpos($field_id, md5('wpcf_not_controlled')) !== false) { $field_id_name = str_replace('_' . md5('wpcf_not_controlled'), '', $field_id); $field_id_add = preg_replace('/^wpcf\\-/', '', $field_id_name); $adding_field_with_wpcf_prefix = $field_id_add != $field_id_name; // Activating field that previously existed in Types if (array_key_exists($field_id_add, $fields)) { $fields[$field_id_add]['data']['disabled'] = 0; } else { // Adding from outside $fields[$field_id_add]['id'] = $field_id_add; $fields[$field_id_add]['type'] = $field_type; if ($adding_field_with_wpcf_prefix) { $fields[$field_id_add]['name'] = $field_id_add; $fields[$field_id_add]['slug'] = $field_id_add; } else { $fields[$field_id_add]['name'] = $field_id_name; $fields[$field_id_add]['slug'] = $field_id_name; } $fields[$field_id_add]['description'] = ''; $fields[$field_id_add]['data'] = array(); if ($adding_field_with_wpcf_prefix) { // This was most probably a previous Types field // let's take full control $fields[$field_id_add]['data']['controlled'] = 0; } else { // @TODO WATCH THIS! MUST NOT BE DROPPED IN ANY CASE $fields[$field_id_add]['data']['controlled'] = 1; } } $unset_key = array_search($field_id, $args['fields']); if ($unset_key !== false) { unset($args['fields'][$unset_key]); $args['fields'][$unset_key] = $field_id_add; } } } wpcf_admin_fields_save_fields($fields, true, $meta_name); return $args['fields']; break; case 'check_exists': $fields = wpcf_admin_fields_get_fields(false, true, false, $meta_name, false); $field = $args; if (array_key_exists($field, $fields) && empty($fields[$field]['data']['disabled'])) { return true; } return false; break; case 'check_outsider': $fields = wpcf_admin_fields_get_fields(false, true, false, $meta_name, false); $field = $args; if (array_key_exists($field, $fields) && !empty($fields[$field]['data']['controlled'])) { return true; } return false; break; default: break; } }
/** * Usermeta groups listing * * @author Gen gen.i@icanlocalize.com * @since Types 1.3 */ function wpcf_usermeta_summary() { wpcf_add_admin_header(__('User Field Groups', 'wpcf'), array('page' => 'wpcf-edit-usermeta'), __('Add New', 'wpcf')); require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/usermeta.php'; require_once WPCF_INC_ABSPATH . '/usermeta-list.php'; $to_display = wpcf_admin_fields_get_fields(); if (!empty($to_display)) { add_action('wpcf_groups_list_table_after', 'wpcf_admin_promotional_text'); } wpcf_admin_usermeta_list(); wpcf_add_admin_footer(); }
/** * Gets all fields that belong to specific group. * * @global type $wpdb * @param type $group_id * @param type $key * @param type $only_active * @return type */ function wpcf_admin_fields_get_fields_by_group($group_id, $key = 'slug', $only_active = false, $disabled_by_type = false, $strictly_active = false) { static $cache = array(); $cache_key = md5($group_id . $key . $only_active . $disabled_by_type . $strictly_active); if (isset($cache[$cache_key])) { return $cache[$cache_key]; } $group_fields = get_post_meta($group_id, '_wp_types_group_fields', true); if (empty($group_fields)) { return array(); } $group_fields = explode(',', trim($group_fields, ',')); $fields = wpcf_admin_fields_get_fields($only_active, $disabled_by_type, $strictly_active); $results = array(); foreach ($group_fields as $field_id) { if (!isset($fields[$field_id])) { continue; } $field = wpcf_admin_fields_get_field($field_id); if (!empty($field)) { $results[$field_id] = $field; } } $cache[$cache_key] = $results; return $results; }
/** * Function return the array of Types fields. * * Function return the array of Types active fields slugs. * * @since x.x.x * * @return array List of slugs */ function wpcf_get_all_fields_slugs($fields) { $post_meta_keys = array(); foreach (wpcf_admin_fields_get_fields(true, true) as $key => $data) { $post_meta_keys[] = $data['meta_key']; } return $post_meta_keys; }
/** * * @global object $wpdb * */ function prepare_items() { global $wpdb; $wpcf_per_page = 15; // Get ours and enabled $cf_types = wpcf_admin_fields_get_fields(true, true); $__groups = wpcf_admin_fields_get_groups(); foreach ($__groups as $__group_id => $__group) { $__groups[$__group_id]['fields'] = wpcf_admin_fields_get_fields_by_group($__group['id'], 'slug', false, true, false); } foreach ($cf_types as $cf_id => $cf) { foreach ($__groups as $__group) { if (isset($__group['fields'][$cf_id])) { $cf_types[$cf_id]['groups'][$__group['id']] = $__group['name']; } } $cf_types[$cf_id]['groups_txt'] = empty($cf_types[$cf_id]['groups']) ? __('None', 'wpcf') : implode(', ', $cf_types[$cf_id]['groups']); } // Get others (cache this result?) $cf_other = $wpdb->get_results("\r\n\t\tSELECT meta_id, meta_key\r\n\t\tFROM {$wpdb->postmeta}\r\n\t\tGROUP BY meta_key\r\n\t\tHAVING meta_key NOT LIKE '\\_%'\r\n\t\tORDER BY meta_key"); // 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'])) { $sort_matches = array('c' => 'name', 'g' => 'groups_txt', 't' => 'slug', 'f' => 'type'); $sorted_keys = array(); $new_array = array(); foreach ($cf_types as $cf_id_temp => $cf_temp) { if (isset($sort_matches[$_REQUEST['orderby']])) { $sorted_keys[$cf_temp['id']] = strtolower($cf_temp[$sort_matches[$_REQUEST['orderby']]]); } else { $sorted_keys[$cf_temp['id']] = strtolower($cf_temp[$sort_matches['c']]); } } asort($sorted_keys, SORT_STRING); if ('desc' == $_REQUEST['order']) { $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()); }
/** * * Views-Shortcode: wpv-control * * Description: Add filters for View * * Parameters: * type: Type of retrieved field layout 'radio', 'checkbox', 'select', 'multi-select', 'textfield', 'checkboxes', 'datepicker' * url_param: The URL parameter passed as an argument * values: Optional, a list of supplied values * display_values: Optional, a list of values to display for the corresponding values * auto_fill: Optional, when set to a "field-slug" the control will be populated with custom field values from the database. * auto_fill_default: Optional, use to set the default, unselected, value of the control. eg Ignore or Don't care * auto_fill_sort: Optional. 'asc', 'desc', 'ascnum', 'descnum', 'none'. Defaults to ascending. * field: Optional, a custom field to retrieve values from * title: Optional, use for the checkbox title * taxonomy: Optional, use when a taxonomy control should be displayed. * taxonomy_orderby. Optional. 'name', 'id', 'count', 'slug', 'term_group', 'none'. Defaults to 'name' * taxonomy_order: Optional 'ASC', 'DESC'. Defaults to ascending. * default_label: Optional, use when a taxonomy control should be displayed using select input type. * date_format: Optional, use for a datepicker control * * Example usage: * * Link: * More details about this shortcode here: <a href="http://wp-types.com/documentation/wpv-control-fields-in-front-end-filters/?utm_source=viewsplugin&utm_campaign=views&utm_medium=filter-help-link&utm_term=http://wp-types.com/documentation/wpv-control-fields-in-front-end-filters/" title="wpv-control – Displaying fields in front-end filters">http://wp-types.com/documentation/wpv-control-fields-in-front-end-filters/</a> * * Note: * */ function wpv_shortcode_wpv_control($atts) { // First control checks if ( !isset( $atts['url_param'] ) ) { return __('The url_param is missing from the wpv-control shortcode argument.', 'wpv-views'); } if ( ( !isset( $atts['type'] ) || $atts == '' ) && !isset( $atts['field'] ) ) { return __('The "type" or "field" needs to be set in the wpv-control shortcode argument.', 'wpv-views'); } //Start the shortcode management global $WP_Views, $no_parameter_found; $aux_array = $WP_Views->view_used_ids; $view_name = get_post_field( 'post_name', end( $aux_array ) ); extract( shortcode_atts(array( 'type' => '', // select, multi-select, checbox, checkboxes, radio/radios, date/datepicker, textfield 'values' => array(), // (optional) comma-separated list of user-provided values 'display_values' => array(), // (optional) comma-separated list of user-provided display values 'field' => '', // name of the custom field 'url_param' => '', // URL parameter to be used 'title' => '', // title to be used on a checkbox field type 'taxonomy' => '', // name of the taxonomy for taxonomies filter controls 'taxonomy_orderby' => 'name', // order of the terms for taxonomies filter controls 'taxonomy_order' => 'ASC', // orderby of the terms for taxonomies filter controls 'format' => false, // format of the display value, use %%NAME%% or %%COUNT%% as placeholders 'default_label' => '', // default label for taxonomies filter controls when using select input type 'hide_empty' => 'false', // option to hide empty terms for taxonomies filter controls 'auto_fill' => '', // options to auto fill values for custom fields filter controls - provide the field name 'auto_fill_default' => '', // default value when using auto_fill for custom fields filter controls 'auto_fill_sort' => '', // order when using auto_fill for custom fields filter controls 'date_format' => '', // date format for date controls 'default_date' => '', // default date for date controls 'force_zero' => 'false', 'style' => '', // inline styles for input 'class' => '', // input classes 'label_style' => '', // inline styles for input label 'label_class' => '' // classes for input label ), $atts) ); $style = esc_attr( $style ); $class = esc_attr( $class ); $label_style = esc_attr( $label_style ); $label_class = esc_attr( $label_class ); // First, parametric search control for taxonomy if ( $taxonomy != '' ) { // Translate the default label if any if ( !empty( $default_label ) ) { $default_label = wpv_translate( $url_param . '_default_label', $default_label, false, 'View ' . $view_name ); $atts['default_label'] = $default_label; } // Render the taxonomy control return wpv_render_taxonomy_control( $atts ); } // Before doing anything else, rule out textfields if ( $type == 'textfield' ) { // Textfield field $default_value = ''; if ( isset( $_GET[ $url_param ] ) ) { $default_value = stripslashes( urldecode( sanitize_text_field( $_GET[ $url_param ] ) ) ); } // Render the form content $element = wpv_form_control( array( 'field' => array( '#type' => 'textfield', '#id' => 'wpv_control_textfield_' . $url_param, '#name' => $url_param, '#attributes' => array( 'style' => $style, 'class' => 'js-wpv-filter-trigger-delayed ' . $class ), '#inline' => true, '#value' => $default_value ) ) ); return $element; } // Check if the View has dependency enabled $view_settings = $WP_Views->get_view_settings(); $dependant = false; $counters = ( $format && strpos( $format, '%%COUNT%%' ) !== false ) ? true : false; $empty_action = array(); if ( isset( $view_settings['dps'] ) && is_array( $view_settings['dps'] ) && isset( $view_settings['dps']['enable_dependency'] ) && $view_settings['dps']['enable_dependency'] == 'enable' ) { $dependant = true; $force_disable_dependant = $WP_Views->get_force_disable_dependant_parametric_search(); if ( $force_disable_dependant ) { $dependant = false; } } // Some basic values if ( empty( $field ) ) { if ( empty( $auto_fill ) ) { // In this case, the shortcode is not about a custom field filter but a generic one without taxonomy, field or auto_fill attributes // It can be used to generate custom form inputs, given that the user provides values (and maybe display_values) // So we need to disable dependency $dependant = false; $counters = false; } else { $field_real_name = _wpv_get_field_real_slug( $auto_fill ); } } else { $field_real_name = _wpv_get_field_real_slug( $field ); } $display_values_trans = false; // flag to whether the display_values need to be translated $out = ''; // If dependency is ON, build the basic data and cache if ( $dependant || $counters ) { $empty_default = 'hide'; $empty_alt = 'disable'; $empty_options = array( 'select', 'radios', 'checkboxes' ); // multi-select is a special case because of dashes and underscores foreach ( $empty_options as $empty_opt ) { if ( isset( $view_settings['dps'][ 'empty_' . $empty_opt ] ) && $view_settings['dps'][ 'empty_' . $empty_opt ] == $empty_alt ) { $empty_action[ $empty_opt ] = $empty_alt; } else { $empty_action[ $empty_opt ] = $empty_default; } } if ( isset( $view_settings['dps']['empty_multi_select'] ) && $view_settings['dps']['empty_multi_select'] == $empty_alt ) { $empty_action['multi-select'] = $empty_alt; } else { $empty_action['multi-select'] = $empty_default; } $wpv_data_cache = array(); $original_value = $view_settings[ 'custom-field-' . $field_real_name . '_value' ]; $processed_value = wpv_apply_user_functions( $original_value ); $compare_function = $view_settings[ 'custom-field-' . $field_real_name . '_compare' ]; $current_value_key = false; // @todo check IN, NOT IN and != compare functions $comparator = 'equal'; $filter_full_list = false; if ( $compare_function == 'BETWEEN' ) { $original_value_array = array_map( 'trim', explode( ',', $original_value ) ); $processed_value_array = array_map( 'trim', explode( ',', $processed_value ) ); $current_value_key = array_search( 'URL_PARAM(' . $url_param . ')', $original_value_array ); if ( $current_value_key !== false ) { $processed_value = isset( $processed_value_array[ $current_value_key ] ) ? $processed_value_array[ $current_value_key ] : $no_parameter_found; if ( $current_value_key < 1 ) { $comparator = 'lower-equal-than'; } else if ( $current_value_key > 0 ) { $comparator = 'greater-equal-than'; } } } else if ( $compare_function == '>' ) { $comparator = 'lower-than'; } else if ( $compare_function == '>=' ) { $comparator = 'lower-equal-than'; } else if ( $compare_function == '<' ) { $comparator = 'greater-than'; } else if ( $compare_function == '<=' ) { $comparator = 'greater-equal-than'; } // Construct $wpv_data_cache['post_meta'] if ( $processed_value == $no_parameter_found ) { global $wp_object_cache; $wpv_data_cache = isset( $wp_object_cache->cache ) ? $wp_object_cache->cache : array(); $aux_query_count = null; } else { // When there is a selected value, create a pseudo-cache based on all the other filters // Note that checkboxes filters can generate nested meta_query entries $query = wpv_get_dependant_view_query_args(); $aux_cache_query = null; $filter_full_list = true; if ( isset( $query['meta_query'] ) && is_array( $query['meta_query'] ) ) { foreach ( $query['meta_query'] as $qt_index => $qt_val ) { if ( is_array( $qt_val ) ) { foreach ( $qt_val as $qt_val_key => $qt_val_val ) { if ( $qt_val_key == 'key' && $qt_val_val == $field_real_name ) { if ( $compare_function == 'BETWEEN' ) { if ( $qt_val['compare'] == 'BETWEEN' && $current_value_key !== false ) { $qt_val['value'] = isset( $qt_val['value'] ) ? $qt_val['value'] : ''; $passed_values = is_array( $qt_val['value'] ) ? $qt_val['value'] : array_map( 'trim', explode( ',', $qt_val['value'] ) ); if ( $current_value_key < 1 && isset( $passed_values[1] ) ) { $query['meta_query'][ $qt_index ]['compare'] = '<='; $query['meta_query'][ $qt_index ]['value']= $passed_values[1]; } else if ( $current_value_key > 0 && isset( $passed_values[0] ) ) { $query['meta_query'][ $qt_index ]['compare'] = '>='; $query['meta_query'][ $qt_index ]['value']= $passed_values[0]; } } else { unset( $query['meta_query'][ $qt_index ] ); } // if $compare_function is BETWEEN and we have a meta_query not using BETWEEN, we have a partial query here, so keep it } else { unset( $query['meta_query'][$qt_index] ); } } else if ( is_array( $qt_val_val ) && isset( $qt_val_val['key'] ) && $qt_val_val['key'] == $field_real_name ) { if ( $compare_function == 'BETWEEN' ) { if ( $qt_val_val['compare'] == 'BETWEEN' && $current_value_key !== false ) { $qt_val_val['value'] = isset( $qt_val_val['value'] ) ? $qt_val_val['value'] : ''; $passed_values = is_array( $qt_val_val['value'] ) ? $qt_val_val['value'] : array_map( 'trim', explode( ',', $qt_val_val['value'] ) ); if ( $current_value_key < 1 && isset( $passed_values[1] ) ) { $query['meta_query'][ $qt_index ][ $qt_val_key ]['compare'] = '<='; $query['meta_query'][ $qt_index ][ $qt_val_key ]['value']= $passed_values[1]; } else if ( $current_value_key > 0 && isset( $passed_values[0] ) ) { $query['meta_query'][ $qt_index ][ $qt_val_key ]['compare'] = '>='; $query['meta_query'][ $qt_index ][ $qt_val_key ]['value']= $passed_values[0]; } } else { unset( $query['meta_query'][ $qt_index ][ $qt_val_key ] ); } // if $compare_function is BETWEEN and we have a meta_query not using BETWEEN, we have a partial query here, so keep it } else { unset( $query['meta_query'][$qt_index][ $qt_val_key ] ); } } } } } } $aux_cache_query = new WP_Query($query); if ( is_array( $aux_cache_query->posts ) && !empty( $aux_cache_query->posts ) ) { $aux_query_count = count( $aux_cache_query->posts ); $f_fields = array( $field_real_name ); $wpv_data_cache = wpv_custom_cache_metadata( $aux_cache_query->posts, array( 'cf' => $f_fields ) ); } } if ( !isset( $wpv_data_cache['post_meta'] ) ) { $wpv_data_cache['post_meta'] = array(); } // OK, for checkboxes custom fields the stored value is NOT the one we use for filtering // So instead of filtering $wpv_data_cache['post_meta'] we will loop it to see if the $field_real_name key exists // AND check the serialized value to see if it contains the given real value (warning, not the label!) // AND break as soon as true because we need no counters // Expensive, but not sure if more than wp_list_filter though } // Management of multiselect $multi = ''; if ( $type == 'multi-select') { $type = 'select'; $multi = 'multiple'; } // $filter_check_type = _wpv_is_field_of_type( $auto_fill, 'checkboxes' ) ? 'checkboxes' : 'other'; $filter_check_type = wpv_types_get_field_type( $field ); if ( $auto_fill != '' ) { /** * If using auto_fill, populate the values and display_values arrays */ /** * First we are going to populate those variables */ $fields = array(); // this will hold the Types fields from the Options $db_values = array(); // this will hold the field values from Types options or from the database $display_text = array(); // this will hold the field values pretty display text, if it is a Types field with options $auto_fill_default_trans = false; // flag to whether the auto_fill_default has translated, based on whether it is one of the existing values if ( !function_exists( 'wpcf_admin_fields_get_fields' ) ) { if( defined( 'WPCF_EMBEDDED_ABSPATH' ) ) { include WPCF_EMBEDDED_ABSPATH . '/includes/fields.php'; } } if ( function_exists( 'wpcf_admin_fields_get_fields' ) ) { $fields = wpcf_admin_fields_get_fields(); } // $field_name = substr($auto_fill, 5); // TODO DONE check this for fields created outside of Types and brought under Types control if ( strpos( $auto_fill, 'wpcf-' ) === 0 ) { $field_name = substr( $auto_fill, 5 ); } else { $field_name = $auto_fill; } // If it is a Types field with options if ( isset( $fields[ $field_name ] ) && isset( $fields[ $field_name ]['data']['options'] ) ) { // If it is a checkboxes Types field if ( _wpv_is_field_of_type( $auto_fill, 'checkboxes' ) ) { $options = $fields[ $field_name ]['data']['options']; foreach( $options as $field_key => $option ) { // Fill the db_values and display_text (translated if needed) arrays $db_values[] = $option['title']; $display_text[ $option['title'] ] = wpv_translate( 'field '. $fields[ $field_name ]['id'] .' option '. $field_key .' title', $option['title'], false, 'plugin Types' ); } } else { // If it is a Types field different from checkboxes but with options $options = $fields[ $field_name ]['data']['options']; if ( isset( $options['default'] ) ) { // remove the default option from the array unset( $options['default'] ); } if ( isset( $fields[ $field_name ]['data']['display'] ) ) { $display_option = $fields[ $field_name ]['data']['display']; } foreach ( $options as $field_key => $option ) { if ( isset( $option['value'] ) ) { $db_values[] = $option['value']; } if ( isset( $display_option ) && 'value' == $display_option && isset( $option['display_value'] ) ) { $display_text[ $option['value'] ] = wpv_translate( 'field '. $fields[ $field_name ]['id'] .' option '. $field_key .' title', $option['display_value'], false, 'plugin Types' ); } else { $display_text[ $option['value'] ] = wpv_translate( 'field '. $fields[ $field_name ]['id'] .' option '. $field_key .' title', $option['title'], false, 'plugin Types' ); } if ( $auto_fill_default != '' ) { // translate the auto_fill_default option if needed, just when it's one of the existing options $auto_fill_default = str_replace( '\,', ',', $auto_fill_default ); if ( $auto_fill_default == $option['title'] ) { $auto_fill_default = wpv_translate( 'field '. $fields[ $field_name ]['id'] .' option '. $field_key .' title', $option['title'], false, 'plugin Types' ); // set this flat to true: we already have translated auto_fill_default $auto_fill_default_trans = true; } $auto_fill_default = str_replace( ',', '\,', $auto_fill_default ); } } } // Now sort the values based on auto_fill_sort switch ( strtolower( $auto_fill_sort ) ) { case 'desc': sort( $db_values ); $db_values = array_reverse( $db_values ); break; case 'descnum': sort( $db_values, SORT_NUMERIC ); $db_values = array_reverse( $db_values ); break; case 'none': break; case 'ascnum': sort( $db_values, SORT_NUMERIC ); break; default: sort( $db_values ); break; } } else { // If it is not a Types field OR is a Types field without options global $wpdb; $values_to_prepare = array(); $values_to_prepare[] = $auto_fill; $wpdb_where = ''; if ( isset( $view_settings['post_type'] ) && is_array( $view_settings['post_type'] ) && ! empty( $view_settings['post_type'] ) && ! in_array( 'any', $view_settings['post_type'] ) ) { $post_type_count = count( $view_settings['post_type'] ); $post_type_placeholders = array_fill( 0, $post_type_count, '%s' ); $wpdb_where .= " AND p.post_type IN (" . implode( ",", $post_type_placeholders ) . ") "; foreach ( $view_settings['post_type'] as $pt ) { $values_to_prepare[] = $pt; } } if ( isset( $view_settings['post_status'] ) && is_array( $view_settings['post_status'] ) && ! empty( $view_settings['post_status'] ) ) { if ( ! in_array( 'any', $view_settings['post_status'] ) ) { $post_status_count = count( $view_settings['post_status'] ); $post_status_placeholders = array_fill( 0, $post_status_count, '%s' ); $wpdb_where .= " AND p.post_status IN (" . implode( ",", $post_status_placeholders ) . ") "; foreach ( $view_settings['post_status'] as $ps ) { $values_to_prepare[] = $ps; } } } else { $status = array( 'publish' ); if ( current_user_can( 'read_private_posts' ) ) { $status[] = 'private'; } $wpdb_where .= " AND p.post_status IN ( '" . implode( "','", $status ) . "' ) "; } $wpdb_orderby = ''; switch ( strtolower( $auto_fill_sort ) ) { case 'desc': $wpdb_orderby = "ORDER BY pm.meta_value DESC"; break; case 'descnum': $wpdb_orderby = "ORDER BY pm.meta_value + 0 DESC"; break; case 'ascnum': $wpdb_orderby = "ORDER BY pm.meta_value + 0 ASC"; break; default: $wpdb_orderby = "ORDER BY pm.meta_value ASC"; break; } $db_values = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT pm.meta_value FROM {$wpdb->postmeta} pm LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id WHERE pm.meta_key = %s AND pm.meta_value IS NOT NULL AND pm.meta_value != '' {$wpdb_where} {$wpdb_orderby}", $values_to_prepare ) ); } /** * Now we are going to fill the $values and $display_values comma-separated strings based on $db_values and, in case, $display_text * NOTE if $auto_fill_default_trans is FALSE then the auto_fill_default is NOT one of the existing option titles so we will translate it */ if ( $auto_fill_default != '' ) { // If auto_fill_default is not empty, adjust and translate when needed if ( !$auto_fill_default_trans ) { // translate the auto_fill_default option when it's not one of the existing options $auto_fill_default = str_replace( '\,', ',', $auto_fill_default ); $auto_fill_default = wpv_translate( $url_param . '_auto_fill_default', stripslashes( $auto_fill_default ), false, 'View ' . $view_name ); $auto_fill_default = str_replace( ',', '\,', $auto_fill_default ); } $values = ''; $display_values = str_replace( '\,', '%comma%', $auto_fill_default ); // flag to whether there is an auto_fill_default value that we ad at the beginning of the $display_value string $first = false; } else { $values = ''; $display_values = ''; $first = true; } foreach( $db_values as $value ) { if ( $value !== false ) { if ( !$first ) { $values .= ','; $display_values .= ','; } // HACK to handle commas in values $values .= str_replace( ',', '%comma%', $value ); if ( isset( $display_text[$value] ) ) { // HACK to handle commas in display_values $display_values .= str_replace( ',', '%comma%', $display_text[ $value ] ); } else { // HACK to handle commas in display_values $display_values .= str_replace( ',', '%comma%', $value ); } $first = false; } } // If not using auto_fill, check if there are manually added display_values } else if ( !empty( $display_values ) ) { // mark that the display_values need to be translated $display_values_trans = true; } /* * Now we have a comma-separated list of $values and $display_values, hopefully ;-D * In fact, we count with a $values comma-separated list * We will fill the $values_arr array and transform $display_values into an array */ if( !empty( $values ) ) { // When values attributes are manually defined, the inner commas are formatted as \, and we need to apply the same HACK as for the automatically set values $values_fix = str_replace( '\,', '%comma%', $values ); // Now, get the $values_arr array of values $values_arr = explode( ',', $values_fix ); // And undo the comma HACK $values_arr = str_replace( '%comma%', ',', $values_arr ); if ( !empty( $display_values ) ) { // If there are display_values,again sync the comma HACK $display_values = str_replace( '\,', '%comma%', $display_values ); // Get an array of $display_values $display_values = explode( ',', $display_values ); // And undo the comma HACK $display_values = str_replace( '%comma%', ',', $display_values ); if ( $display_values_trans ) { // If we need to translate the $display_values $translated_values = array(); foreach ( $display_values as $index => $valuetrans ) { $translated_values[ $index ] = wpv_translate( $url_param . '_display_values_' . ( $index + 1 ), stripslashes( $valuetrans ), false, 'View ' . $view_name ); } $display_values = $translated_values; } } // Parse date expressions in values. $values_count = count( $values_arr ); for( $i = 0; $i < $values_count; ++$i ) { $values_arr[ $i ] = wpv_filter_parse_date( $values_arr[ $i ] ); } /** * Now that we have the $values_arr and $display_values we focus on the kind of output * Based on $type we will popuate an $options variable and use the wpv_form_control() function */ if( !in_array( $type, array( 'radio', 'radios', 'select', 'checkboxes' ) ) ) { // For wpv-control shortcodes using auto_fill or values/display_values we only allow those kind of types $type = 'select'; } if ( $type == 'radio' ) { // In fact, radios == radio $type = 'radios'; } $options = array(); // Now, depending on $type switch ( $type ) { case 'checkboxes': // If we need to render CHECKBOXES $defaults = array(); $original_get = null; if ( isset( $auto_fill_default ) ) { // First, check if the defaul value already exists and set the appropriate arrays and values $num_auto_fill_default_display = array_count_values( $display_values ); $auto_fill_default_trans = str_replace( '\,', ',', $auto_fill_default ); if ( // if the auto_fill_default is one of the display_values ( isset( $num_auto_fill_default_display[ $auto_fill_default_trans ] ) && $num_auto_fill_default_display[ $auto_fill_default_trans ] > 1 ) || // OR if the auto_fill_default is one of the values in_array( $auto_fill_default_trans, $values_arr ) ) { // Take out the first element of the $values_arr and the $display_values, which holds and empty string and the auto_fill_default value $values_arr_def = array_shift( $values_arr ); $display_values_def = array_shift( $display_values ); } // Then, set the preliminary $defaults value based on auto_fill_default $defaults = str_replace( '\,', '%comma%', $auto_fill_default ); $defaults = explode( ',', $defaults ); $defaults = str_replace( '%comma%', ',', $defaults ); $defaults = array_map( 'trim', $defaults ); } if ( isset( $_GET[ $url_param ] ) ) { // Override $defaults if a set of values is coming from the URL parameter $original_get = $_GET[ $url_param ]; $defaults = $_GET[ $url_param ]; if ( is_string( $defaults ) ) { $defaults = explode( ',',$defaults ); } unset( $_GET[ $url_param ] ); } $count_values_array = count( $values_arr ); for( $i = 0; $i < $count_values_array; $i++ ) { // Loop through the $values_arr $value = $values_arr[ $i ]; $value = trim( $value ); // Check for a display value if ( isset( $display_values[ $i ] ) ) { $display_value = $display_values[ $i ]; } else { $display_value = $value; } // Compose the $options for this value $options[ $value ]['#name'] = $url_param . '[]'; $options[ $value ]['#title'] = $display_value; $options[ $value ]['#value'] = $value; // set default using option titles too $options[ $value ]['#default_value'] = in_array( $value, $defaults ) || in_array( $options[ $value ]['#title'], $defaults); $options[ $value ]['#attributes']['class'] = 'js-wpv-filter-trigger ' . $class; $options[ $value ]['#attributes']['style'] = $style; $options[ $value ]['#labelclass'] = $label_class; $options[ $value ]['#labelstyle'] = $label_style; // Dependant stuff if ( $dependant || $counters ) { if ( $format ) { $display_value_formatted_name = str_replace( '%%NAME%%', $options[ $value ]['#title'], $format ); $options[ $value ]['#title'] = $display_value_formatted_name; } $meta_criteria_to_filter = array( $field_real_name => array( $value ) ); $this_query = $WP_Views->get_query(); if ( empty( $value ) && !is_numeric( $value ) && is_object( $this_query ) ) { if ( isset( $aux_query_count ) ) { $this_checker = $aux_query_count; } else { $this_checker = $this_query->found_posts; } } else { $data = array(); $data['list'] = $wpv_data_cache['post_meta']; $data['args'] = $meta_criteria_to_filter; $data['kind'] = $filter_check_type; $data['comparator'] = $comparator; if ( $counters ) { $data['count_matches'] = true; } $data['filter_full_list'] = $filter_full_list; $this_checker = wpv_list_filter_checker( $data ); } if ( $counters ) { $display_value_formatted_name = str_replace( '%%COUNT%%', $this_checker, $options[ $value ]['#title'] ); $options[ $value ]['#title'] = $display_value_formatted_name; } if ( !$this_checker && ( !empty( $value ) || is_numeric( $value ) ) && !$options[ $value ]['#default_value'] && $dependant ) { $options[ $value ]['#attributes']['#disabled'] = 'true'; $options[ $value ]['#labelclass'] .= ' wpv-parametric-disabled '; if ( isset( $empty_action['checkboxes'] ) && $empty_action['checkboxes'] == 'hide' ) { unset( $options[ $value ] ); } } } // $options[$value]['#inline'] = true; // $options[$value]['#after'] = ' '; } // Render the form control element $element = wpv_form_control( array( 'field' => array( '#type' => $type, '#id' => 'wpv_control_' . $type . '_' . $url_param, '#name' => $url_param . '[]', '#attributes' => array( 'style' => '' ), '#inline' => true, '#options' => $options, '#before' => '<div class="wpcf-checkboxes-grou">', //we need to wrap them for js purposes '#after' => '</div>' ) ) ); if ( $original_get ) { $_GET[ $url_param ] = $original_get; } break; default: // If we need to check any other field with values and a type that is not checkboxes (radios or select) $options_array = array(); // This one will hold options in a display_vaue => value format so we can use it to compose the default_value later $options = array(); $count_values_array = count( $values_arr ); for( $i = 0; $i < $count_values_array; $i++ ) { // Loop through the $values_arr $value = $values_arr[ $i ]; $value = trim( $value ); // Check for a display value if ( isset( $display_values[ $i ] ) ) { $display_value = $display_values[ $i ]; } else { $display_value = $value; } // Compose the $options for this value $options[ $display_value ] = $value; $options_array[ $display_value ] = array( '#title' => $display_value, '#value' => $value, '#inline' => true, '#after' => '<br />' ); $options_array[ $display_value ]['#attributes']['class'] = 'js-wpv-filter-trigger'; if ( $type == 'radios' ) { $options_array[ $display_value ]['#attributes']['class'] .= ' ' . $class; $options_array[ $display_value ]['#attributes']['style'] = $style; $options_array[ $display_value ]['#labelclass'] = $label_class; $options_array[ $display_value ]['#labelstyle'] = $label_style; } // Dependant stuff if ( $dependant || $counters ) { if ( $format ) { $display_value_formatted_name = str_replace( '%%NAME%%', $options_array[ $display_value ]['#title'], $format ); $options_array[ $display_value ]['#title'] = $display_value_formatted_name; } $this_query = $WP_Views->get_query(); if ( empty( $value ) && !is_numeric( $value ) && is_object( $this_query ) ) { if ( isset( $aux_query_count ) ) { $this_checker = $aux_query_count; } else { $this_checker = $this_query->found_posts; } } else { $meta_criteria_to_filter = array( $field_real_name => array( $value ) ); $data = array(); $data['list'] = $wpv_data_cache['post_meta']; $data['args'] = $meta_criteria_to_filter; $data['kind'] = $filter_check_type; $data['comparator'] = $comparator; if ( $counters ) { $data['count_matches'] = true; } $data['filter_full_list'] = $filter_full_list; $this_checker = wpv_list_filter_checker( $data ); } if ( $counters ) { $display_value_formatted_counter = str_replace( '%%COUNT%%', $this_checker, $options_array[ $display_value ]['#title'] ); $options_array[ $display_value ]['#title'] = $display_value_formatted_counter; } if ( !$this_checker && ( !empty( $value ) || is_numeric( $value ) ) && $dependant ) { // TODO DONE need to merge this with the default_value below, to avoid hiddin or disabling selected items $options_array[ $display_value ]['#disable'] = 'true'; $options_array[ $display_value ]['#labelclass'] = 'wpv-parametric-disabled'; if ( $type == 'select' && $multi == 'multiple' ) { if ( isset( $empty_action['multi-select'] ) && $empty_action['multi-select'] == 'hide' ) { unset( $options_array[ $display_value ] ); } } else if ( isset( $empty_action[ $type ] ) && $empty_action[ $type ] == 'hide' ) { unset( $options_array[ $display_value ] ); } } } } if ( count( $values_arr ) != count( $options ) ) { // if the $values_arr has one more item than $options, there is a repeating value: the default one added to the beginning $default_value = reset( $options ); } else { if ( $type == 'radios' || $multi == 'multiple' ) { $default_value = ''; } else { // so the default value in this case is the first element in $values_arr $default_value = isset( $values_arr[0] ) ? $values_arr[0] : ''; } } if ( $type == 'radios' ) { if ( isset( $_GET[ $url_param ] ) && in_array( $_GET[ $url_param ], $options ) ) { $default_value = $_GET[ $url_param ]; } $name_aux = $url_param; } else { // Basically, if $type == 'select' if ( isset( $_GET[ $url_param ] ) ) { if ( is_array( $_GET[ $url_param ] ) ) { if ( count( array_intersect($_GET[ $url_param ], $options) ) > 0 ) { $default_value = $_GET[ $url_param ]; } } else { if ( in_array( $_GET[ $url_param ], $options ) ) { $default_value = $_GET[ $url_param ]; } } } $name_aux = $url_param . '[]'; } // Now we need to recreate the $options_array element if it is a default one and is disabled or removed if ( is_array( $default_value ) ) { foreach ( $default_value as $dv ) { $aux_display_values = array_keys( $options, $dv, true ); foreach ( $aux_display_values as $aux_dv ) { // TODO where is $aux_dv defined?? if ( isset( $options_array[ $aux_dv ] ) ) { if ( isset( $options_array[ $aux_dv ]['#disable'] ) ) { unset( $options_array[ $aux_dv ]['#disable'] ); } $options_array[ $aux_dv ]['#labelclass'] = ''; } else { $options_array[ $aux_dv ] = array( '#title' => $aux_dv, '#value' => $dv, '#inline' => true, '#after' => '<br />' ); $options_array[ $aux_dv ]['#attributes']['class'] = 'js-wpv-filter-trigger '; } } } } else { $aux_display_values = array_keys( $options, $default_value, true ); foreach ( $aux_display_values as $aux_dv ) { if ( isset( $options_array[ $aux_dv ] ) ) { if ( isset( $options_array[$aux_dv]['#disable'] ) ) { unset( $options_array[$aux_dv]['#disable'] ); } $options_array[ $aux_dv ]['#labelclass'] = ''; } else { $options_array[ $aux_dv ] = array( '#title' => $aux_dv, '#value' => $default_value, '#inline' => true, '#after' => '<br />' ); $options_array[ $aux_dv ]['#attributes']['class'] = 'js-wpv-filter-trigger'; } } } $element = wpv_form_control( array( 'field' => array( '#type' => $type, '#id' => 'wpv_control_' . $type . '_' . $url_param, '#name' => $name_aux, '#attributes' => array('style' => $style, 'class' => 'js-wpv-filter-trigger' . $class ), '#inline' => true, '#options' => $options_array, // NOTE this was originally $options but as it's not an array I can not set a "disabled" option '#default_value' => $default_value, '#multiple' => $multi // NOTE I'd say that radios do not need multiple but it should do no harm ) ) ); break; } return $element; } else if ( !empty( $field ) ) { /** * When field attribute is defined but we do not have auto_fill nor manually entered values * In this case, we display the control input based on $type or the field type itself if needed (mainly for Types auto style, but we can expect other combinations) */ // Check if Types is active because we are using wpcf_admin_fields_get_field() if ( !function_exists( 'wpcf_admin_fields_get_field' ) ) { if ( defined( 'WPCF_EMBEDDED_ABSPATH' ) ) { include WPCF_EMBEDDED_ABSPATH . '/includes/fields.php'; } else { return __( 'Types plugin is required.', 'wpv-views' ); } } if ( !function_exists( 'wpv_form_control' ) ) { include '../common/functions.php'; } //This is important cause wpcf_admin_fields_get_field works with id: $field - 'wpcf-' and search with 'wpcf-'.$field /*if( strpos($field, 'wpcf-') !== false ) { $tmp = explode('wpcf-', $field); $field = $tmp[1]; }*/ // Get field options and translate name if needed $field_options = wpcf_admin_fields_get_field( $field ); if ( empty( $field_options ) ) { return __( 'Empty field values or incorrect field defined. ', 'wpv-views' ); } $field_options['name'] = wpv_translate( 'field ' . $field_options['id'] . ' name', $field_options['name'], false, 'plugin Types' ); // Get field type, override if $type exists and default it to textfield if needed $field_type = $field_options['type']; if ( !empty( $type ) ) { // Watch out: this is where we can override the field type itself $field_type = $type; } if ( !in_array( $field_type, array( 'radio', 'checkbox', 'checkboxes', 'select', 'textfield', 'date', 'datepicker' ) ) ) { $field_type = 'textfield'; } // Display time!! if ( $field_type == 'radio' ) { // Radio field $field_radio_options = isset( $field_options['data']['options'] ) ? $field_options['data']['options'] : array(); $options = array(); foreach ( $field_radio_options as $key => $opts ) { if ( is_array( $opts ) ) { if ( isset( $field_options['data']['display'] ) && 'value' == $field_options['data']['display'] && isset( $opts['display_value'] ) ) { // if we have an actual display value and is set to be used, use it $display_value = $opts['display_value']; $value = $opts['value']; } else { // else, use the field value title and watch out because checkboxes fields need their titles as values $display_value = wpv_translate( 'field '. $field_options['id'] .' option '. $key .' title', $opts['title'], false, 'plugin Types' ); if ( _wpv_is_field_of_type( 'wpcf-' . $field, 'checkboxes' ) ) { $value = $opts['title']; } else { $value = $opts['value']; } } $options[ $display_value ] = array( '#title' => $display_value, '#value' => $value, '#inline' => true, '#after' => '<br />' ); $options[ $display_value ]['#attributes']['class'] = 'js-wpv-filter-trigger ' . $class; $options[ $display_value ]['#attributes']['style'] = $style; // Dependant stuff if ( $dependant || $counters ) { if ( $format ) { $display_value_formatted_name = str_replace( '%%NAME%%', $options[ $display_value ]['#title'], $format ); $options[ $display_value ]['#title'] = $display_value_formatted_name; } $this_query = $WP_Views->get_query(); if ( empty( $value ) && !is_numeric( $value ) && is_object( $this_query ) ) { if ( isset( $aux_query_count ) ) { $this_checker = $aux_query_count; } else { $this_checker = $this_query->found_posts; } } else { $meta_criteria_to_filter = array( $field_real_name => array( $value ) ); $data = array(); $data['list'] = $wpv_data_cache['post_meta']; $data['args'] = $meta_criteria_to_filter; $data['kind'] = $filter_check_type; $data['comparator'] = $comparator; if ( $counters ) { $data['count_matches'] = true; } $data['filter_full_list'] = $filter_full_list; $this_checker = wpv_list_filter_checker( $data ); } if ( $counters ) { $display_value_formatted_counter = str_replace( '%%COUNT%%', $this_checker, $options[ $display_value ]['#title'] ); $options[ $display_value ]['#title'] = $display_value_formatted_counter; } if ( !$this_checker && ( !empty( $value ) || is_numeric( $value ) ) && ( !isset( $_GET[$url_param] ) || $_GET[$url_param] !== $value ) && $dependant ) { $options[ $display_value ]['#disable'] = 'true'; $options[ $display_value ]['#labelclass'] = 'wpv-parametric-disabled'; if ( isset( $empty_action['radios'] ) && $empty_action['radios'] == 'hide' ) { unset( $options[ $display_value ] ); } } } } } // Render the form content $element = wpv_form_control( array( 'field' => array( '#type' => 'radios', '#id' => 'wpv_control_radio_' . $field, '#name' => $url_param, '#attributes' => array( 'style' => $style, 'class' => $class ), '#inline' => true, '#options' => $options, '#default_value' => isset( $_GET[ $url_param ] ) ? $_GET[ $url_param ] : '' ) ) ); return $element; } else if ( $field_type == 'checkbox' ) { // Checkbox field // Populate the $checkbox_name with the wpv-control title attribute OR the field name itself if ( isset( $atts['title'] ) ) { $checkbox_name = wpv_translate( $url_param . '_title', $title, false, 'View ' . $view_name ); } else { // NOTE mmmmmm we seem to have translated this $field_options['name'] right above... $checkbox_name = wpv_translate( 'field ' . $field_options['name'] . ' name', $field_options['name'], false, 'plugin Types' ); } $value = $field_options['data']['set_value']; $coming_value = ''; if ( isset( $_GET[ $url_param ] ) && !empty( $_GET[ $url_param ] ) ) { $value = esc_attr( $_GET[ $url_param ] ); $coming_value = esc_attr( $_GET[ $url_param ] ); } else if ( isset( $_GET[ $url_param ] ) && is_numeric( $_GET[ $url_param ] ) ) { // this only happens when the value to store when checked is actually zero - nonsense $value = 0; $coming_value = 0; } else if ( empty( $_GET[ $url_param ] ) ) { unset( $_GET[ $url_param ] ); } $attributes = array( 'style' => '', 'class' => 'js-wpv-filter-trigger' ); $labelclass = ''; $show_checkbox = true; // Dependant stuff if ( $dependant || $counters ) { if ( $format ) { $display_value_formatted_name = str_replace( '%%NAME%%', $checkbox_name, $format ); $checkbox_name = $display_value_formatted_name; } $meta_criteria_to_filter = array( $field_real_name => array( $value ) ); $data = array(); $data['list'] = $wpv_data_cache['post_meta']; $data['args'] = $meta_criteria_to_filter; $data['kind'] = $filter_check_type; $data['comparator'] = $comparator; if ( $counters ) { $data['count_matches'] = true; } $data['filter_full_list'] = $filter_full_list; $this_checker = wpv_list_filter_checker( $data ); if ( $counters ) { $display_value_formatted_count = str_replace( '%%COUNT%%', $this_checker, $checkbox_name ); $checkbox_name = $display_value_formatted_count; } if ( !$this_checker && empty( $coming_value ) && $dependant ) { $attributes['#disabled'] = 'true'; $labelclass = 'wpv-parametric-disabled'; if ( isset( $empty_action['checkboxes'] ) && $empty_action['checkboxes'] == 'hide' ) { $show_checkbox = false; } } } if ( $show_checkbox ) { // Render the form content $attributes['class'] .= ' ' . $class; $attributes['style'] = $style; $element = wpv_form_control( array( 'field' => array( '#type' => 'checkbox', '#id' => 'wpv_control_checkbox_' . $field, '#name' => $url_param, '#attributes' => $attributes, '#inline' => true, '#title' => $checkbox_name, '#labelclass' => $labelclass . ' ' . $label_class, '#labelstyle' => $label_style, '#value' => $field_options['data']['set_value'], '#default_value' => 0 ) ) ); if ( isset( $field_options['data']['save_empty'] ) && $field_options['data']['save_empty'] == 'yes' && $force_zero == 'true' ) { $attributes['class'] = ''; $attributes['checked'] = 'checked'; $element .= wpv_form_control( array( 'field' => array( '#type' => 'hidden', '#id' => 'wpv_control_checkbox_' . $field . '_fakezero', '#name' => $url_param . '_fakezero', '#attributes' => $attributes, '#inline' => true, '#value' => 'yes', '#default_value' => 0 ) ) ); } } else { $element = ''; } return $element; } else if ( $field_type == 'checkboxes' ) { // Checkboxes field $defaults = array(); $original_get = null; if ( isset( $_GET[ $url_param ] ) ) { $original_get = $_GET[ $url_param ]; $defaults = $_GET[ $url_param ]; if ( is_string( $defaults ) ) { $defaults = explode( ',',$defaults ); } unset( $_GET[ $url_param ] ); } $field_checkboxes_options = isset( $field_options['data']['options'] ) ? $field_options['data']['options'] : array(); if ( isset( $field_checkboxes_options['default'] ) ) { // Remove the default option from the array because it breaks the loop below unset( $field_checkboxes_options['default'] ); } foreach( $field_checkboxes_options as $key => $value ) { $display_value = wpv_translate( 'field '. $field_options['id'] .' option '. $key .' title', trim( $value['title'] ), false, 'plugin Types' ); if ( _wpv_is_field_of_type( 'wpcf-' . $field, 'checkboxes' ) ) { $value = trim( $value['title'] ); } else { $value = trim( $value['value'] ); } $options[ $value ]['#name'] = $url_param . '[]'; $options[ $value ]['#title'] = $display_value; $options[ $value ]['#value'] = $value; $options[ $value ]['#default_value'] = in_array( $value, $defaults ); //$options[$value]['#inline'] = true; //$options[$value]['#after'] = ' '; $options[ $value ]['#attributes']['class'] = 'js-wpv-filter-trigger ' . $class; $options[ $value ]['#attributes']['style'] = $style; $options[ $value ]['#labelclass'] = $label_class; $options[ $value ]['#labelstyle'] = $label_style; // Dependant stuff if ( $dependant || $counters ) { if ( $format ) { $display_value_formatted_name = str_replace( '%%NAME%%', $options[ $value ]['#title'], $format ); $options[ $value ]['#title'] = $display_value_formatted_name; } $meta_criteria_to_filter = array( $field_real_name => array( $value ) ); // TODO DONE IMPORTANT check what is coming here as value, maybe $opts['title'] sometimes $this_query = $WP_Views->get_query(); if ( empty( $value ) && !is_numeric( $value ) && is_object( $this_query ) ) { if ( isset( $aux_query_count ) ) { $this_checker = $aux_query_count; } else { $this_checker = $this_query->found_posts; } } else { $data = array(); $data['list'] = $wpv_data_cache['post_meta']; $data['args'] = $meta_criteria_to_filter; $data['kind'] = $filter_check_type; $data['comparator'] = $comparator; if ( $counters ) { $data['count_matches'] = true; } $data['filter_full_list'] = $filter_full_list; $this_checker = wpv_list_filter_checker( $data ); } if ( $counters ) { $display_value_formatted = str_replace( '%%COUNT%%', $this_checker, $options[ $value ]['#title'] ); $options[ $value ]['#title'] = $display_value_formatted; } if ( !$this_checker && ( !empty( $value ) || is_numeric( $value ) ) && !$options[ $value ]['#default_value'] && $dependant ) { $options[ $value ]['#attributes']['#disabled'] = 'true'; $options[ $value ]['#labelclass'] .= ' wpv-parametric-disabled'; if ( isset( $empty_action['checkboxes'] ) && $empty_action['checkboxes'] == 'hide' ) { unset( $options[ $value ] ); } } } } // Render the form content $element = wpv_form_control( array( 'field' => array( '#type' => 'checkboxes', '#id' => 'wpv_control_checkbox_' . $field, '#name' => $url_param . '[]', '#attributes' => array( 'style' => '' ), '#inline' => true, '#options' => $options ) ) ); if ( $original_get ) { $_GET[ $url_param ] = $original_get; } return $element; } else if ( $field_type == 'select' ) { // Select field $field_select_options = isset( $field_options['data']['options'] ) ? $field_options['data']['options'] : array();; $options = array(); $opt_aux = array(); foreach ( $field_select_options as $key => $opts ) { if ( is_array( $opts ) ) { $display_value = wpv_translate( 'field '. $field_options['id'] .' option '. $key .' title', $opts['title'], false, 'plugin Types' ); if ( _wpv_is_field_of_type( 'wpcf-' . $field, 'checkboxes' ) ) { $value = $opts['title']; } else { $value = $opts['value']; } $options[ $display_value ] = array( '#title' => $display_value, '#value' => $value, '#inline' => true, '#after' => '<br />' ); $opt_aux[ $display_value ] = $value; $options[ $display_value ]['#attributes']['class'] = 'js-wpv-filter-trigger'; // Dependant stuff if ( $dependant || $counters ) { if ( $format ) { $display_value_formatted_name = str_replace( '%%NAME%%', $options[ $display_value ]['#title'], $format ); $options[ $display_value ]['#title'] = $display_value_formatted_name; } $this_query = $WP_Views->get_query(); if ( empty( $value ) && !is_numeric( $value ) && is_object( $this_query ) ) { if ( isset( $aux_query_count ) ) { $this_checker = $aux_query_count; } else { $this_checker = $this_query->found_posts; } } else { $meta_criteria_to_filter = array( $field_real_name => array( $value ) ); $data = array(); $data['list'] = $wpv_data_cache['post_meta']; $data['args'] = $meta_criteria_to_filter; $data['kind'] = $filter_check_type; $data['comparator'] = $comparator; if ( $counters ) { $data['count_matches'] = true; } $data['filter_full_list'] = $filter_full_list; $this_checker = wpv_list_filter_checker( $data ); } if ( $counters ) { $display_value_formatted_counter = str_replace( '%%COUNT%%', $this_checker, $options[ $display_value ]['#title'] ); $options[ $display_value ]['#title'] = $display_value_formatted_counter; } if ( !$this_checker && ( !empty( $value ) || is_numeric( $value ) ) && $dependant ) { // TODO DONE we need to adjust this with the $default_value below $options[ $display_value ]['#disable'] = 'true'; $options[ $display_value ]['#labelclass'] = 'wpv-parametric-disabled'; if ( $multi == 'multiple' ) { if ( isset( $empty_action['multi-select'] ) && $empty_action['multi-select'] == 'hide' ) { unset( $options[ $display_value ] ); } } else if ( isset( $empty_action['select'] ) && $empty_action['select'] == 'hide' ) { unset( $options[ $display_value ] ); } } } } } $default_value = false; if ( isset( $_GET[ $url_param ] ) ) { if ( is_array( $_GET[ $url_param ] ) ) { if ( count( array_intersect($_GET[ $url_param ], $opt_aux) ) > 0 ) { $default_value = $_GET[ $url_param ]; } } else { if ( in_array( $_GET[ $url_param ], $opt_aux ) ) { $default_value = $_GET[ $url_param ]; } } } // Now we need to recreate the $options element if it is a default one and is disabled or removed if ( $default_value !== false && is_array( $default_value ) ) { foreach ( $default_value as $dv ) { $aux_display_values = array_keys( $opt_aux, $dv, true ); foreach ( $aux_display_values as $aux_dv ) { if ( isset( $options[ $aux_dv ] ) ) { if ( isset( $options[ $aux_dv ]['#disable'] ) ) { unset( $options[ $aux_dv ]['#disable'] ); } $options[ $aux_dv ]['#labelclass'] = ''; } else { $options[ $aux_dv ] = array( '#title' => $aux_dv, '#value' => $dv, '#inline' => true, '#after' => '<br />' ); $options[ $aux_dv ]['#attributes']['class'] = 'js-wpv-filter-trigger'; } } } } else if ( $default_value !== false ) { $aux_display_values = array_keys( $opt_aux, $default_value, true ); foreach ( $aux_display_values as $aux_dv ) { if ( isset( $options[ $aux_dv ] ) ) { if ( isset( $options[ $aux_dv ]['#disable'] ) ) { unset( $options[ $aux_dv ]['#disable'] ); } $options[ $aux_dv ]['#labelclass'] = ''; } else { $options[ $aux_dv ] = array( '#title' => $aux_dv, '#value' => $default_value, '#inline' => true, '#after' => '<br />' ); $options[ $aux_dv ]['#attributes']['class'] = 'js-wpv-filter-trigger'; } } } // Render the form content $element = wpv_form_control( array( 'field' => array( '#type' => 'select', '#id' => 'wpv_control_select_' . $url_param, '#name' => $url_param . '[]', '#attributes' => array( 'style' => $style, 'class' => $class ), '#inline' => true, '#options' => $options, '#default_value' => $default_value, '#multiple' => $multi ) ) ); return $element; } else if ( $field_type == 'textfield' ) { // Textfield field $default_value = ''; if ( isset( $_GET[ $url_param ] ) ) { $default_value = stripslashes( urldecode( sanitize_text_field( $_GET[ $url_param ] ) ) ); } // Render the form content $element = wpv_form_control( array( 'field' => array( '#type' => 'textfield', '#id' => 'wpv_control_textfield_' . $url_param, '#name' => $url_param, '#attributes' => array( 'style' => $style, 'class' => 'js-wpv-filter-trigger-delayed ' . $class ), '#inline' => true, '#value' => $default_value ) ) ); return $element; } else if ( $field_type == 'date' || $field_type == 'datepicker' ) { // Date or datepicker field $out = wpv_render_datepicker( $url_param, $date_format, $default_date ); return $out; } // In case we have a field attribute but it does not match any vaid type, return nothing return ''; } else { // When there is a type attribute without field or auto_fill or values attributes it's likely for a checkbox or a datepicker // But I'm not sure what is this used for, because it really does not filter by any field $default_value = ''; if ( isset( $_GET[ $url_param ] ) ) { $default_value = $_GET[ $url_param ]; } switch ( $type ) { case 'checkbox': // In this case, there is no way to implement dependant parametric search, because we have no field to check against $element = array( 'field' => array( '#type' => $type, '#id' => 'wpv_control_' . $type . '_' . $url_param, '#name' => $url_param, '#attributes' => array( 'style' => $style, 'class' => 'js-wpv-filter-trigger ' . $class ), '#inline' => true, '#value' => $default_value ) ); $element['field']['#title'] = wpv_translate( $url_param . '_title', $title, false, 'View ' . $view_name ); $element = wpv_form_control( $element ); break; case 'datepicker': $element = wpv_render_datepicker( $url_param, $date_format, $default_date ); break; default: $element = array( 'field' => array( '#type' => $type, '#id' => 'wpv_control_' . $type . '_' . $url_param, '#name' => $url_param, '#attributes' => array( 'style' => $style, 'class' => $class ), '#inline' => true, '#value' => $default_value ) ); $element = wpv_form_control( $element ); break; } return $element; } }