| 1 |
<?php |
| 2 |
namespace Elementor; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Elementor select2 control. |
| 10 |
* |
| 11 |
* A base control for creating select2 control. Displays a select box control |
| 12 |
* based on select2 jQuery plugin @see https://select2.github.io/ . |
| 13 |
* It accepts an array in which the `key` is the value and the `value` is the |
| 14 |
* option name. Set `multiple` to `true` to allow multiple value selection. |
| 15 |
* |
| 16 |
* @since 1.0.0 |
| 17 |
*/ |
| 18 |
class Control_Select2 extends Base_Data_Control { |
| 19 |
|
| 20 |
/** |
| 21 |
* Get select2 control type. |
| 22 |
* |
| 23 |
* Retrieve the control type, in this case `select2`. |
| 24 |
* |
| 25 |
* @since 1.0.0 |
| 26 |
* @access public |
| 27 |
* |
| 28 |
* @return string Control type. |
| 29 |
*/ |
| 30 |
public function get_type() { |
| 31 |
return 'select2'; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get select2 control default settings. |
| 36 |
* |
| 37 |
* Retrieve the default settings of the select2 control. Used to return the |
| 38 |
* default settings while initializing the select2 control. |
| 39 |
* |
| 40 |
* @since 1.8.0 |
| 41 |
* @access protected |
| 42 |
* |
| 43 |
* @return array Control default settings. |
| 44 |
*/ |
| 45 |
protected function get_default_settings() { |
| 46 |
return [ |
| 47 |
'options' => [], |
| 48 |
'multiple' => false, |
| 49 |
'select2options' => [], |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Render select2 control output in the editor. |
| 55 |
* |
| 56 |
* Used to generate the control HTML in the editor using Underscore JS |
| 57 |
* template. The variables for the class are available using `data` JS |
| 58 |
* object. |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
* @access public |
| 62 |
*/ |
| 63 |
public function content_template() { |
| 64 |
$control_uid = $this->get_control_uid(); |
| 65 |
?> |
| 66 |
<div class="elementor-control-field"> |
| 67 |
<# if ( data.label ) {#> |
| 68 |
<label for="<?php echo $control_uid; ?>" class="elementor-control-title">{{{ data.label }}}</label> |
| 69 |
<# } #> |
| 70 |
<div class="elementor-control-input-wrapper elementor-control-unit-5"> |
| 71 |
<# var multiple = ( data.multiple ) ? 'multiple' : ''; #> |
| 72 |
<select id="<?php echo $control_uid; ?>" class="elementor-select2" type="select2" {{ multiple }} data-setting="{{ data.name }}"> |
| 73 |
<# _.each( data.options, function( option_title, option_value ) { |
| 74 |
var value = data.controlValue; |
| 75 |
if ( typeof value == 'string' ) { |
| 76 |
var selected = ( option_value === value ) ? 'selected' : ''; |
| 77 |
} else if ( null !== value ) { |
| 78 |
var value = _.values( value ); |
| 79 |
var selected = ( -1 !== value.indexOf( option_value ) ) ? 'selected' : ''; |
| 80 |
} |
| 81 |
#> |
| 82 |
<option {{ selected }} value="{{ option_value }}">{{{ option_title }}}</option> |
| 83 |
<# } ); #> |
| 84 |
</select> |
| 85 |
</div> |
| 86 |
</div> |
| 87 |
<# if ( data.description ) { #> |
| 88 |
<div class="elementor-control-field-description">{{{ data.description }}}</div> |
| 89 |
<# } #> |
| 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; |
| 137 |
} |
| 138 |
} |
| 139 |
|