| 1 |
<?php |
| 2 |
|
| 3 |
namespace UltimatePostKit\Includes\Controls\SelectInput; |
| 4 |
|
| 5 |
use Elementor\Base_Data_Control; |
| 6 |
use Elementor\Plugin; |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* UltimatePostKit Dyanmic select control. |
| 14 |
* |
| 15 |
* A base control for creating Dyanmic_Select control. Displays a select box control |
| 16 |
* based on select on elementor select2 control . |
| 17 |
* It accepts an array in which the `key` is the value and the `value` is the |
| 18 |
* option name. Set `multiple` to `true` to allow multiple value selection. |
| 19 |
* |
| 20 |
* @since 5.8.0 |
| 21 |
*/ |
| 22 |
class Dynamic_Select extends Base_Data_Control { |
| 23 |
const TYPE = 'upk-dynamic-select'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Get control type. |
| 27 |
* |
| 28 |
* Retrieve the control type. |
| 29 |
* @since 5.8.0 |
| 30 |
* @access public |
| 31 |
*/ |
| 32 |
public function get_type() { |
| 33 |
return self::TYPE; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get select2 control default settings. |
| 38 |
* |
| 39 |
* Retrieve the default settings of the select2 control. Used to return the |
| 40 |
* default settings while initializing the select2 control. |
| 41 |
* |
| 42 |
* @return array Control default settings. |
| 43 |
* @since 5.8.0 |
| 44 |
* @access protected |
| 45 |
* |
| 46 |
*/ |
| 47 |
protected function get_default_settings() { |
| 48 |
return [ |
| 49 |
'options' => [], |
| 50 |
'multiple' => false, |
| 51 |
// Select2 library options |
| 52 |
'select2options' => [], |
| 53 |
// the lockedOptions array can be passed option keys. The passed option keys will be non-deletable. |
| 54 |
'lockedOptions' => [], |
| 55 |
// the query arguments array can be passed, |
| 56 |
'query_args' => array(), |
| 57 |
|
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Render select2 control output in the editor. |
| 63 |
* |
| 64 |
* Used to generate the control HTML in the editor using Underscore JS |
| 65 |
* template. The variables for the class are available using `data` JS |
| 66 |
* object. |
| 67 |
* @since 5.8.0 |
| 68 |
* @access public |
| 69 |
*/ |
| 70 |
public function content_template() { |
| 71 |
$control_uid = $this->get_control_uid(); |
| 72 |
?> |
| 73 |
<div class="elementor-control-field"> |
| 74 |
<# if ( data.label ) {#> |
| 75 |
<label for="<?php echo esc_attr($control_uid); ?>" class="elementor-control-title">{{{data.label }}}</label> |
| 76 |
<# } #> |
| 77 |
<div class="elementor-control-input-wrapper elementor-control-unit-5"> |
| 78 |
<# var multiple = ( data.multiple ) ? 'multiple' : ''; #> |
| 79 |
<select id="<?php echo esc_attr($control_uid); ?>" class="elementor-select2" type="select2" {{ multiple }} |
| 80 |
data-setting="{{ data.name }}"> |
| 81 |
<# _.each( data.options, function( option_title, option_value ) { |
| 82 |
var value = data.controlValue; |
| 83 |
if ( typeof value == 'string' ) { |
| 84 |
var selected = ( option_value === value ) ? 'selected' : ''; |
| 85 |
} else if ( null !== value ) { |
| 86 |
var value = _.values( value ); |
| 87 |
var selected = ( -1 !== value.indexOf( option_value ) ) ? 'selected' : ''; |
| 88 |
} |
| 89 |
#> |
| 90 |
<option {{ selected }} value="{{ option_value }}">{{{option_title }}}</option> |
| 91 |
<# } ); #> |
| 92 |
</select> |
| 93 |
</div> |
| 94 |
</div> |
| 95 |
<# if ( data.description ) { #> |
| 96 |
<div class="elementor-control-field-description">{{{data.description }}}</div> |
| 97 |
<# } #> |
| 98 |
<?php |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Enqueue control scripts and styles. |
| 103 |
* |
| 104 |
* Used to register and enqueue custom scripts and styles used by the control. |
| 105 |
* @since 5.8.0 |
| 106 |
*/ |
| 107 |
public function enqueue() { |
| 108 |
wp_enqueue_script('upk-dynamic-select', BDTUPK_URL . 'includes/controls/assets/js/upk-dynamic-select.min.js', array('jquery'), BDTUPK_VER, true); |
| 109 |
wp_localize_script( |
| 110 |
'upk-dynamic-select', |
| 111 |
'upk_dynamic_select', |
| 112 |
[ |
| 113 |
'nonce' => wp_create_nonce('upk_dynamic_select'), |
| 114 |
'action' => 'upk_dynamic_select_input_data', |
| 115 |
'ajax_url' => admin_url('admin-ajax.php') |
| 116 |
] |
| 117 |
); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
add_action('elementor/controls/register', function () { |
| 122 |
$controls_manager = Plugin::$instance->controls_manager; |
| 123 |
$controls_manager->register(new Dynamic_Select()); |
| 124 |
}); |
| 125 |
|