| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
use Sellkit_Elementor_Optin_Module as Module; |
| 6 |
|
| 7 |
class Sellkit_Elementor_Optin_Field_Select extends Sellkit_Elementor_Optin_Field_Base { |
| 8 |
|
| 9 |
public static function get_field_type() { |
| 10 |
return 'select'; |
| 11 |
} |
| 12 |
|
| 13 |
public function get_input_type() { |
| 14 |
return 'select'; |
| 15 |
} |
| 16 |
|
| 17 |
public function add_field_render_attribute() { |
| 18 |
parent::add_field_render_attribute(); |
| 19 |
|
| 20 |
if ( 'true' === $this->field['multiple_selection'] ) { |
| 21 |
$this->widget->add_render_attribute( 'field-' . $this->get_id(), 'multiple' ); |
| 22 |
|
| 23 |
if ( ! empty( $this->field['select_rows'] ) ) { |
| 24 |
$this->widget->add_render_attribute( 'field-' . $this->get_id(), 'size', $this->field['select_rows'] ); |
| 25 |
} |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
public function render_content() { |
| 30 |
$field = $this->field; |
| 31 |
$options = preg_split( '/\\r\\n|\\r|\\n/', $field['field_options'], -1, PREG_SPLIT_NO_EMPTY ); |
| 32 |
|
| 33 |
if ( empty( $options ) ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
?> |
| 38 |
<div class="sellkit-field-subgroup"> |
| 39 |
<?php if ( empty( $this->field['multiple_selection'] ) ) : ?> |
| 40 |
<div class="sellkit-field-select-arrow"> |
| 41 |
<?php Module::render_icon( $this->widget->get_settings_for_display()['select_arrow_icon'] ); ?> |
| 42 |
</div> |
| 43 |
<?php endif ?> |
| 44 |
<select <?php echo $this->widget->get_render_attribute_string( 'field-' . esc_attr( $this->get_id() ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor-generated attributes. ?>> |
| 45 |
<?php $this->render_options( $options ); ?> |
| 46 |
</select> |
| 47 |
</div> |
| 48 |
<?php |
| 49 |
} |
| 50 |
|
| 51 |
private function render_options( $options ) { |
| 52 |
foreach ( $options as $key => $option ) { |
| 53 |
$option_id = $this->get_id() . $key; |
| 54 |
$option_label = $option; |
| 55 |
$option_value = $option; |
| 56 |
|
| 57 |
if ( false !== strpos( $option, '|' ) ) { |
| 58 |
list( $option_label, $option_value ) = explode( '|', $option ); |
| 59 |
} |
| 60 |
|
| 61 |
$option_args = [ 'value' => $option_value ]; |
| 62 |
|
| 63 |
if ( $this->field['field_value'] === $option_value ) { |
| 64 |
$option_args['selected'] = 'selected'; |
| 65 |
} |
| 66 |
|
| 67 |
$this->widget->add_render_attribute( $option_id, $option_args ); |
| 68 |
|
| 69 |
?> |
| 70 |
<option <?php echo $this->widget->get_render_attribute_string( $option_id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor-generated attributes. ?>> |
| 71 |
<?php echo esc_html( $option_label ); ?> |
| 72 |
</option> |
| 73 |
<?php |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public static function get_additional_controls() { |
| 78 |
$commons = parent::get_common_controls(); |
| 79 |
|
| 80 |
return [ |
| 81 |
'label' => $commons['label'], |
| 82 |
'field_value' => $commons['field_value'], |
| 83 |
'field_options' => [ |
| 84 |
'label' => esc_html__( 'Options', 'sellkit' ), |
| 85 |
'type' => 'textarea', |
| 86 |
'default' => '', |
| 87 |
'conditions' => [ 'terms' => [ parent::get_type_condition() ] ], |
| 88 |
'description' => esc_html__( 'Enter each option in a separate line. To differentiate between label and value, separate them with a pipe char ("|"). For example: First Name|f_name', 'sellkit' ), |
| 89 |
], |
| 90 |
'multiple_selection' => [ |
| 91 |
'label' => esc_html__( 'Multiple Selection', 'sellkit' ), |
| 92 |
'type' => 'switcher', |
| 93 |
'return_value' => 'true', |
| 94 |
'conditions' => [ 'terms' => [ parent::get_type_condition() ] ], |
| 95 |
], |
| 96 |
'select_rows' => [ |
| 97 |
'label' => esc_html__( 'Rows', 'sellkit' ), |
| 98 |
'type' => 'number', |
| 99 |
'default' => 5, |
| 100 |
'conditions' => [ |
| 101 |
'terms' => [ |
| 102 |
parent::get_type_condition(), |
| 103 |
[ |
| 104 |
'name' => 'multiple_selection', |
| 105 |
'operator' => '===', |
| 106 |
'value' => 'true', |
| 107 |
], |
| 108 |
], |
| 109 |
], |
| 110 |
], |
| 111 |
'required' => $commons['required'], |
| 112 |
'width_responsive' => $commons['width_responsive'], |
| 113 |
]; |
| 114 |
} |
| 115 |
} |
| 116 |
|