PluginProbe
Elementor Website Builder – more than just a page builder / 3.22.3
Elementor Website Builder – more than just a page builder v3.22.3
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 3.22.3, at includes/controls/repeater.php

185 lines 4.4 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 {
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 'item_actions' => [
70 'add' => true,
71 'duplicate' => true,
72 'remove' => true,
73 'sort' => true,
74 ],
75 ];
76 }
77
78 /**
79 * Get repeater control value.
80 *
81 * Retrieve the value of the repeater control from a specific Controls_Stack.
82 *
83 * @since 1.0.0
84 * @access public
85 *
86 * @param array $control Control
87 * @param array $settings Controls_Stack settings
88 *
89 * @return mixed Control values.
90 */
91 public function get_value( $control, $settings ) {
92 $value = parent::get_value( $control, $settings );
93
94 if ( ! empty( $value ) ) {
95 foreach ( $value as &$item ) {
96 foreach ( $control['fields'] as $field ) {
97 $control_obj = Plugin::$instance->controls_manager->get_control( $field['type'] );
98
99 // Prior to 1.5.0 the fields may contains non-data controls.
100 if ( ! $control_obj instanceof Base_Data_Control ) {
101 continue;
102 }
103
104 $item[ $field['name'] ] = $control_obj->get_value( $field, $item );
105 }
106 }
107 }
108
109 return $value;
110 }
111
112 /**
113 * Import repeater.
114 *
115 * Used as a wrapper method for inner controls while importing Elementor
116 * template JSON file, and replacing the old data.
117 *
118 * @since 1.8.0
119 * @access public
120 *
121 * @param array $settings Control settings.
122 * @param array $control_data Optional. Control data. Default is an empty array.
123 *
124 * @return array Control settings.
125 */
126 public function on_import( $settings, $control_data = [] ) {
127 if ( empty( $settings ) || empty( $control_data['fields'] ) ) {
128 return $settings;
129 }
130
131 $method = 'on_import';
132
133 foreach ( $settings as &$item ) {
134 foreach ( $control_data['fields'] as $field ) {
135 if ( empty( $field['name'] ) || empty( $item[ $field['name'] ] ) ) {
136 continue;
137 }
138
139 $control_obj = Plugin::$instance->controls_manager->get_control( $field['type'] );
140
141 if ( ! $control_obj ) {
142 continue;
143 }
144
145 if ( method_exists( $control_obj, $method ) ) {
146 $item[ $field['name'] ] = $control_obj->{$method}( $item[ $field['name'] ], $field );
147 }
148 }
149 }
150
151 return $settings;
152 }
153
154 /**
155 * Render repeater control output in the editor.
156 *
157 * Used to generate the control HTML in the editor using Underscore JS
158 * template. The variables for the class are available using `data` JS
159 * object.
160 *
161 * @since 1.0.0
162 * @access public
163 */
164 public function content_template() {
165 ?>
166 <label>
167 <span class="elementor-control-title">{{{ data.label }}}</span>
168 </label>
169 <div class="elementor-repeater-fields-wrapper"></div>
170 <# if ( itemActions.add ) { #>
171 <div class="elementor-button-wrapper">
172 <button class="elementor-button elementor-repeater-add" type="button">
173 <i class="eicon-plus" aria-hidden="true"></i>
174 <# if ( data.button_text ) { #>
175 {{{ data.button_text }}}
176 <# } else { #>
177 <?php echo esc_html__( 'Add Item', 'elementor' ); ?>
178 <# } #>
179 </button>
180 </div>
181 <# } #>
182 <?php
183 }
184 }
185