/** * * 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; } } }
/** * * 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; } } }
/** * *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', '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. * date_format: Optional. Used for a datepicker control * * @param array $atts An associative array of arributes to be used. */ 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' => '', 'auto_fill' => '', 'auto_fill_default' => '', 'auto_fill_sort' => '', 'date_format' => ''), $atts)); if ($taxonomy != '') { return _wpv_render_taxonomy_control($taxonomy, $type, $url_param); } if ($auto_fill != '') { 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 'none': $db_values = $wpdb->get_col("SELECT DISTINCT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '{$auto_fill}'"); 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; $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($_GET[$url_param])) { $original_get = $_GET[$url_param]; $defaults = $_GET[$url_param]; 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); // $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; } $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'); } // 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)) { $options[$opts['title']] = $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 = $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]; 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 = $_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; } } }