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