Image_select with presets=>true does nothing
Hi, I cant get the values from the options array to set in the other options:
array(
'id'=>'cv_country_color_presets',
'type' => 'image_select',
'presets' => true,
'title' => __('Theme Color Presets', 'cv_country'),
'subtitle' => __('Select your themes color scheme.', 'cv_country'),
'default' => 1,
'options' => array(
'1' => array(
'alt' => 'Brown',
'img' => CV_THEME_URI . '/inc/assets/brown.jpg',
'presets' => array(
'cv_country_color_scheme' => '1',
'cv_country_bgmode' => '2',
'cv_country_bgimg' => '5'
),
),
'2' => array(
'alt' => 'Blue',
'img' => CV_THEME_URI . '/inc/assets/blue.jpg',
'presets' => array(
'cv_country_color_scheme' => '2',
'cv_country_bgmode' => '2',
'cv_country_bgimg' => '1'
)
),
)
),
And also the js that trigger the popup doesnt seem to work.
Ok, I figure out. The field id must be presets. If is an issue its releated to JS, if not, this must be on the Wiki.
Confirmed. Not sure if this is by design or a bug. WIll check into it.
I see two things to change: First in the PHP:
/**
* Field Render Function.
*
* Takes the vars and outputs the HTML for the field in the settings
*
* @since 1.0.0
* @access public
* @return void
*/
public function render() {
if( !empty( $this->field['options'] ) ) {
echo '<ul class="redux-image-select">';
$x = 1;
foreach( $this->field['options'] as $k => $v ) {
if( !is_array( $v ) )
$v = array( 'img' => $v );
if( !isset( $v['title'] ) )
$v['title'] = '';
if( !isset( $v['alt'] ) )
$v['alt'] = $v['title'];
$style = '';
if( !empty( $this->field['width'] ) ) {
$style .= 'width: ' . $this->field['width'];
if( is_numeric( $this->field['width'] ) )
$style .= 'px';
$style .= ';';
}
if( !empty( $this->field['height'] ) ) {
$style .= 'height: ' . $this->field['height'];
if( is_numeric( $this->field['height'] ) )
$style .= 'px';
$style .= ';';
}
$style .= " max-width: 100%; ";
$theValue = $k;
if( !empty( $this->field['tiles'] ) && $this->field['tiles'] == true ) {
$theValue = $v['img'];
}
$selected = ( checked( $this->value, $theValue, false ) != '' ) ? ' redux-image-select-selected' : '';
$presets = '';
$this->field['class'] .= ' noUpdate ';
if( !empty( $this->field['presets'] ) && $this->field['presets'] && !empty( $v['presets'] ) ) {
if( !is_array( $v['presets'] ) )
$v['presets'] = json_decode( $v['presets'], true );
$v['presets']['redux-backup'] = 1;
$presets = ' data-presets="' . htmlspecialchars( json_encode( $v['presets'] ), ENT_QUOTES, 'UTF-8' ) . '"';
$selected = '';
$this->field['class'] .= 'redux-presets';
}
echo '<li class="redux-image-select">';
echo '<label class="' . $selected . ' redux-image-select-' . $this->field['id'] . '_' . $x . '" for="' . $this->field['id'] . '_' . (array_search( $k, array_keys( $this->field['options'] ) ) + 1) . '">';
echo '<input type="radio" class="' . $this->field['class'] . '" id="' . $this->field['id'] . '_' . (array_search( $k, array_keys( $this->field['options'] ) ) + 1) . '" name="' . $this->args['opt_name'] . '[' . $this->field['id'] . ']" value="' . $theValue . '" ' . checked( $this->value, $theValue, false ) . $presets . '/>';
if( !empty( $this->field['tiles'] ) && $this->field['tiles'] == true ) {
echo '<span class="tiles" style="background-image: url(' . $v['img'] . ');" rel="'.$v['img'].'""> </span>';
} else {
echo '<img src="' . $v['img'] . '" alt="' . $v['alt'] . '" style="' . $style . '"' . $presets . ' />';
}
if ( $v['title'] != '' )
echo '<br /><span>' . $v['title'] . '</span>';
echo '</label>';
echo '</li>';
$x++;
}
echo '</ul>';
}
}
Just the addition of a $x counter and some concatenation on the label creation.
And second on the JS:
// On label click, change the input and class
jQuery('.redux-image-select label img, .redux-image-select label .tiles').click(function(e) {
var id = jQuery(this).closest('label').attr('for');
jQuery(this).parents("fieldset:first").find('.redux-image-select-selected').removeClass('redux-image-select-selected');
jQuery(this).closest('label').find('input[type="radio"]').prop('checked');
if (jQuery(this).closest('label').hasClass('redux-image-select-' + id)) { // If they clicked on a preset, import!
e.preventDefault();
var presets = jQuery(this).closest('label').find('input');
var data = presets.data('presets');
if (presets !== undefined && presets !== null) {
var answer = confirm(redux_opts.preset_confirm);
if (answer) {
jQuery('label[for="' + id + '"]').addClass('redux-image-select-selected');
window.onbeforeunload = null;
jQuery('#import-code-value').val(JSON.stringify(data));
jQuery('#redux-import').click();
}
} else {}
return false;
} else {
redux_change(jQuery(this).closest('label').find('input[type="radio"]'));
jQuery('label[for="' + id + '"]').addClass('redux-image-select-selected');
}
});
Here only the addition of the id to the label selector for class redux.image-select-
Could you do a pull request with this? :)
Off course, I was just testing testing other things. The way presets are propagated, its not very clear. For example, I have an image_select with tiles ('tiles' => true). If by preset, I want to select the 5th image, i cannot set the option by index ('my_image_select' => 5), i need to pass it as an URI, which, i think, is the way image_select works. It could be great to pass preset options by index to fields. Maybe a feature for version 4?
I doubt we'll ever make it so you can pass a preset to the DB via index. The reason being is the items in the DB are not stored in the same way as the sections. Just a fact. I don't think it's a big enough need.
I'm open to a use case if you can give it to me. Unless there is something viable to many users it would have to be something you do on your own and contribute. Sorry.