PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.0
Elementor Website Builder – more than just a page builder v4.2.0
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / controls / repeater.php

repeater.php in Elementor Website Builder – more than just a page builder 4.2.0, at includes/controls/repeater.php

202 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 /**
9 * Elementor repeater control.
10 *
11 * A base control for creating repeater control. Repeater control allows you to
12 * build repeatable blocks of fields. You can create, for example, a set of
13 * fields that will contain a title and a WYSIWYG text - the user will then be
14 * able to add "rows", and each row will contain a title and a text. The data
15 * can be wrapper in custom HTML tags, designed using CSS, and interact using JS
16 * or external libraries.
17 *
18 * @since 1.0.0
19 */
20 class Control_Repeater extends Base_Data_Control implements Has_Validation {
21
22 /**
23 * Get repeater control type.
24 *
25 * Retrieve the control type, in this case `repeater`.
26 *
27 * @since 1.0.0
28 * @access public
29 *
30 * @return string Control type.
31 */
32 public function get_type() {
33 return 'repeater';
34 }
35
36 /**
37 * Get repeater control default value.
38 *
39 * Retrieve the default value of the data control. Used to return the default
40 * values while initializing the repeater control.
41 *
42 * @since 2.0.0
43 * @access public
44 *
45 * @return array Control default value.
46 */
47 public function get_default_value() {
48 return [];
49 }
50
51 /**
52 * Get repeater control default settings.
53 *
54 * Retrieve the default settings of the repeater control. Used to return the
55 * default settings while initializing the repeater control.
56 *
57 * @since 1.0.0
58 * @access protected
59 *
60 * @return array Control default settings.
61 */
62 protected function get_default_settings() {
63 return [
64 'fields' => [],
65 'title_field' => '',
66 'prevent_empty' => true,
67 'is_repeater' => true,
68 'max_items' => 0,
69 'min_items' => 0,
70 'item_actions' => [
71 'add' => true,
72 'duplicate' => true,
73 'remove' => true,
74 'sort' => true,
75 ],
76 ];
77 }
78
79 /**
80 * Get repeater control value.
81 *
82 * Retrieve the value of the repeater control from a specific Controls_Stack.
83 *
84 * @since 1.0.0
85 * @access public
86 *
87 * @param array $control Control.
88 * @param array $settings Controls_Stack settings.
89 *
90 * @return mixed Control values.
91 */
92 public function get_value( $control, $settings ) {
93 $value = parent::get_value( $control, $settings );
94
95 if ( ! empty( $value ) ) {
96 foreach ( $value as &$item ) {
97 foreach ( $control['fields'] as $field ) {
98 $control_obj = Plugin::$instance->controls_manager->get_control( $field['type'] );
99
100 // Prior to 1.5.0 the fields may contains non-data controls.
101 if ( ! $control_obj instanceof Base_Data_Control ) {
102 continue;
103 }
104
105 $item[ $field['name'] ] = $control_obj->get_value( $field, $item );
106 }
107 }
108 }
109
110 return $value;
111 }
112
113 /**
114 * Import repeater.
115 *
116 * Used as a wrapper method for inner controls while importing Elementor
117 * template JSON file, and replacing the old data.
118 *
119 * @since 1.8.0
120 * @access public
121 *
122 * @param array $settings Control settings.
123 * @param array $control_data Optional. Control data. Default is an empty array.
124 *
125 * @return array Control settings.
126 */
127 public function on_import( $settings, $control_data = [] ) {
128 if ( empty( $settings ) || empty( $control_data['fields'] ) ) {
129 return $settings;
130 }
131
132 $method = 'on_import';
133
134 foreach ( $settings as &$item ) {
135 foreach ( $control_data['fields'] as $field ) {
136 if ( empty( $field['name'] ) || empty( $item[ $field['name'] ] ) ) {
137 continue;
138 }
139
140 $control_obj = Plugin::$instance->controls_manager->get_control( $field['type'] );
141
142 if ( ! $control_obj ) {
143 continue;
144 }
145
146 if ( method_exists( $control_obj, $method ) ) {
147 $item[ $field['name'] ] = $control_obj->{$method}( $item[ $field['name'] ], $field );
148 }
149 }
150 }
151
152 return $settings;
153 }
154
155 /**
156 * Render repeater control output in the editor.
157 *
158 * Used to generate the control HTML in the editor using Underscore JS
159 * template. The variables for the class are available using `data` JS
160 * object.
161 *
162 * @since 1.0.0
163 * @access public
164 */
165 public function content_template() {
166 ?>
167 <label>
168 <span class="elementor-control-title">{{{ data.label }}}</span>
169 </label>
170 <div class="elementor-repeater-fields-wrapper" role="list"></div>
171 <# if ( itemActions.add ) { #>
172 <div class="elementor-button-wrapper">
173 <button class="elementor-button elementor-repeater-add" type="button">
174 <i class="eicon-plus" aria-hidden="true"></i>
175 <# if ( data.button_text ) { #>
176 {{{ data.button_text }}}
177 <# } else { #>
178 <?php echo esc_html__( 'Add Item', 'elementor' ); ?>
179 <# } #>
180 </button>
181 </div>
182 <# } #>
183 <?php
184 }
185
186 public function validate( array $control_data ): bool {
187 if ( isset( $control_data['min_items'] ) ) {
188
189 if (
190 ! isset( $control_data['default'] ) ||
191 count( $control_data['default'] ) < $control_data['min_items']
192 ) {
193 throw new \Exception(
194 esc_html__( 'In a Repeater control, if you specify a minimum number of items, you must also specify a default value that contains at least that number of items.', 'elementor' )
195 );
196 }
197 }
198
199 return true;
200 }
201 }
202