PluginProbe
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets / 4.5.7
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets v4.5.7
4.5.7 4.5.6 4.5.5 4.5.4 4.2.1 4.2.2 4.2.3 4.5.0 4.5.2 4.5.3 4.2.0 4.1.18 4.1.17 4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.0.9 4.1.0 4.1.1 All 149 releases
ultimate-post-kit / includes / controls / select-input / dynamic-select.php

dynamic-select.php in Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets 4.5.7, at includes/controls/select-input/dynamic-select.php

157 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 static $done = false;
109
110 if ($done) {
111 return;
112 }
113 $done = true;
114
115 $settings = [
116 'nonce' => wp_create_nonce('upk_dynamic_select'),
117 'action' => 'upk_dynamic_select_input_data',
118 'ajax_url' => admin_url('admin-ajax.php')
119 ];
120
121 /**
122 * Ship the control view inline, right after Elementor's own editor
123 * script, instead of as a separate file.
124 *
125 * As a separate file it is printed after the editor loader (which
126 * calls elementor.start()), and on some sites the tag never runs at
127 * all: optimizers, CDNs or a stray crossorigin attribute can block
128 * or rewrite the request. The control then falls back to a plain
129 * <select> with no Select2. Inline, there is no request to block,
130 * and the view is registered before the editor starts.
131 */
132 $script_path = BDTUPK_PATH . 'includes/controls/assets/js/upk-dynamic-select.min.js';
133
134 if (wp_script_is('elementor-editor', 'registered') && is_readable($script_path)) {
135 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- reading a bundled local plugin file, not a remote URL.
136 $script = file_get_contents($script_path);
137
138 if ($script) {
139 wp_add_inline_script(
140 'elementor-editor',
141 'var upk_dynamic_select = ' . wp_json_encode($settings) . ';' . PHP_EOL . $script,
142 'after'
143 );
144 return;
145 }
146 }
147
148 wp_enqueue_script('upk-dynamic-select', BDTUPK_URL . 'includes/controls/assets/js/upk-dynamic-select.min.js', array('jquery'), BDTUPK_VER, true);
149 wp_localize_script('upk-dynamic-select', 'upk_dynamic_select', $settings);
150 }
151 }
152
153 add_action('elementor/controls/register', function () {
154 $controls_manager = Plugin::$instance->controls_manager;
155 $controls_manager->register(new Dynamic_Select());
156 });
157