| @@ -45,12 +45,9 @@ | ||
| 45 | 45 | protected function get_default_settings() { |
| 46 | 46 | return [ |
| 47 | 47 | 'options' => [], |
| 48 | 48 | 'multiple' => false, |
| 49 | - // Select2 library options | |
| 50 | 49 | 'select2options' => [], |
| 51 | - // the lockedOptions array can be passed option keys. The passed option keys will be non-deletable. | |
| 52 | - 'lockedOptions' => [], | |
| 53 | 50 | ]; |
| 54 | 51 | } |
| 55 | 52 | |
| 56 | 53 | /** |
| @@ -63,16 +60,17 @@ | ||
| 63 | 60 | * @since 1.0.0 |
| 64 | 61 | * @access public |
| 65 | 62 | */ |
| 66 | 63 | public function content_template() { |
| 64 | + $control_uid = $this->get_control_uid(); | |
| 67 | 65 | ?> |
| 68 | 66 | <div class="elementor-control-field"> |
| 69 | 67 | <# if ( data.label ) {#> |
| 70 | - <label for="<?php $this->print_control_uid(); ?>" class="elementor-control-title">{{{ data.label }}}</label> | |
| 68 | + <label for="<?php echo $control_uid; ?>" class="elementor-control-title">{{{ data.label }}}</label> | |
| 71 | 69 | <# } #> |
| 72 | 70 | <div class="elementor-control-input-wrapper elementor-control-unit-5"> |
| 73 | 71 | <# var multiple = ( data.multiple ) ? 'multiple' : ''; #> |
| 74 | - <select id="<?php $this->print_control_uid(); ?>" class="elementor-select2" type="select2" {{ multiple }} data-setting="{{ data.name }}"> | |
| 72 | + <select id="<?php echo $control_uid; ?>" class="elementor-select2" type="select2" {{ multiple }} data-setting="{{ data.name }}"> | |
| 75 | 73 | <# _.each( data.options, function( option_title, option_value ) { |
| 76 | 74 | var value = data.controlValue; |
| 77 | 75 | if ( typeof value == 'string' ) { |
| 78 | 76 | var selected = ( option_value === value ) ? 'selected' : ''; |
| @@ -80,9 +78,9 @@ | ||
| 80 | 78 | var value = _.values( value ); |
| 81 | 79 | var selected = ( -1 !== value.indexOf( option_value ) ) ? 'selected' : ''; |
| 82 | 80 | } |
| 83 | 81 | #> |
| 84 | - <option {{ selected }} value="{{ _.escape( option_value ) }}">{{{ _.escape( option_title ) }}}</option> | |
| 82 | + <option {{ selected }} value="{{ option_value }}">{{{ option_title }}}</option> | |
| 85 | 83 | <# } ); #> |
| 86 | 84 | </select> |
| 87 | 85 | </div> |
| 88 | 86 | </div> |
| @@ -89,6 +87,52 @@ | ||
| 89 | 87 | <# if ( data.description ) { #> |
| 90 | 88 | <div class="elementor-control-field-description">{{{ data.description }}}</div> |
| 91 | 89 | <# } #> |
| 92 | 90 | <?php |
| 91 | + } | |
| 92 | + | |
| 93 | + | |
| 94 | + /** | |
| 95 | + * @param string|array $value | |
| 96 | + * @param array $config | |
| 97 | + * | |
| 98 | + * @return string|array | |
| 99 | + */ | |
| 100 | + public function before_save( $value, array $config ) { | |
| 101 | + return $this->validate_value( $value, $config ); | |
| 102 | + } | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * Validate the value is listed in the control options config. | |
| 106 | + * | |
| 107 | + * Avoid change settings like `html_tag` & `title_size` to a `script` tag. | |
| 108 | + * | |
| 109 | + * @param string|array $value | |
| 110 | + * @param array $config | |
| 111 | + * | |
| 112 | + * @return string|array | |
| 113 | + */ | |
| 114 | + private function validate_value( $value, array $config ) { | |
| 115 | + // Handle multiple select. | |
| 116 | + if ( ! empty( $config['multiple'] ) ) { | |
| 117 | + $validated_value = []; | |
| 118 | + | |
| 119 | + foreach ( $value as $index => $item ) { | |
| 120 | + if ( isset( $config['options'][ $item ] ) ) { | |
| 121 | + $validated_value[ $index ] = $item; | |
| 122 | + } | |
| 123 | + } | |
| 124 | + | |
| 125 | + $value = $validated_value; | |
| 126 | + $is_valid = true; | |
| 127 | + } else { | |
| 128 | + $is_valid = isset( $config['options'][ $value ] ); | |
| 129 | + } | |
| 130 | + | |
| 131 | + // If it's not one of the control options. reset it to default. | |
| 132 | + if ( ! $is_valid ) { | |
| 133 | + $value = $config['default']; | |
| 134 | + } | |
| 135 | + | |
| 136 | + return $value; | |
| 93 | 137 | } |
| 94 | 138 | } |