repeater.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor repeater control.
  8. *
  9. * A base control for creating repeater control. Repeater control allows you to
  10. * build repeatable blocks of fields. You can create, for example, a set of
  11. * fields that will contain a title and a WYSIWYG text - the user will then be
  12. * able to add "rows", and each row will contain a title and a text. The data
  13. * can be wrapper in custom HTML tags, designed using CSS, and interact using JS
  14. * or external libraries.
  15. *
  16. * @since 1.0.0
  17. */
  18. class Control_Repeater extends Base_Data_Control {
  19. /**
  20. * Get repeater control type.
  21. *
  22. * Retrieve the control type, in this case `repeater`.
  23. *
  24. * @since 1.0.0
  25. * @access public
  26. *
  27. * @return string Control type.
  28. */
  29. public function get_type() {
  30. return 'repeater';
  31. }
  32. /**
  33. * Get repeater control default value.
  34. *
  35. * Retrieve the default value of the data control. Used to return the default
  36. * values while initializing the repeater control.
  37. *
  38. * @since 2.0.0
  39. * @access public
  40. *
  41. * @return array Control default value.
  42. */
  43. public function get_default_value() {
  44. return [];
  45. }
  46. /**
  47. * Get repeater control default settings.
  48. *
  49. * Retrieve the default settings of the repeater control. Used to return the
  50. * default settings while initializing the repeater control.
  51. *
  52. * @since 1.0.0
  53. * @access protected
  54. *
  55. * @return array Control default settings.
  56. */
  57. protected function get_default_settings() {
  58. return [
  59. 'fields' => [],
  60. 'title_field' => '',
  61. 'prevent_empty' => true,
  62. 'is_repeater' => true,
  63. ];
  64. }
  65. /**
  66. * Get repeater control value.
  67. *
  68. * Retrieve the value of the repeater control from a specific Controls_Stack.
  69. *
  70. * @since 1.0.0
  71. * @access public
  72. *
  73. * @param array $control Control
  74. * @param array $settings Controls_Stack settings
  75. *
  76. * @return mixed Control values.
  77. */
  78. public function get_value( $control, $settings ) {
  79. $value = parent::get_value( $control, $settings );
  80. if ( ! empty( $value ) ) {
  81. foreach ( $value as &$item ) {
  82. foreach ( $control['fields'] as $field ) {
  83. $control_obj = Plugin::$instance->controls_manager->get_control( $field['type'] );
  84. // Prior to 1.5.0 the fields may contains non-data controls.
  85. if ( ! $control_obj instanceof Base_Data_Control ) {
  86. continue;
  87. }
  88. $item[ $field['name'] ] = $control_obj->get_value( $field, $item );
  89. }
  90. }
  91. }
  92. return $value;
  93. }
  94. /**
  95. * Import repeater.
  96. *
  97. * Used as a wrapper method for inner controls while importing Elementor
  98. * template JSON file, and replacing the old data.
  99. *
  100. * @since 1.8.0
  101. * @access public
  102. *
  103. * @param array $settings Control settings.
  104. * @param array $control_data Optional. Control data. Default is an empty array.
  105. *
  106. * @return array Control settings.
  107. */
  108. public function on_import( $settings, $control_data = [] ) {
  109. if ( empty( $settings ) || empty( $control_data['fields'] ) ) {
  110. return $settings;
  111. }
  112. $method = 'on_import';
  113. foreach ( $settings as &$item ) {
  114. foreach ( $control_data['fields'] as $field ) {
  115. if ( empty( $field['name'] ) || empty( $item[ $field['name'] ] ) ) {
  116. continue;
  117. }
  118. $control_obj = Plugin::$instance->controls_manager->get_control( $field['type'] );
  119. if ( ! $control_obj ) {
  120. continue;
  121. }
  122. if ( method_exists( $control_obj, $method ) ) {
  123. $item[ $field['name'] ] = $control_obj->{$method}( $item[ $field['name'] ], $field );
  124. }
  125. }
  126. }
  127. return $settings;
  128. }
  129. /**
  130. * Render repeater control output in the editor.
  131. *
  132. * Used to generate the control HTML in the editor using Underscore JS
  133. * template. The variables for the class are available using `data` JS
  134. * object.
  135. *
  136. * @since 1.0.0
  137. * @access public
  138. */
  139. public function content_template() {
  140. ?>
  141. <label>
  142. <span class="elementor-control-title">{{{ data.label }}}</span>
  143. </label>
  144. <div class="elementor-repeater-fields-wrapper"></div>
  145. <div class="elementor-button-wrapper">
  146. <button class="elementor-button elementor-button-default elementor-repeater-add" type="button">
  147. <i class="eicon-plus" aria-hidden="true"></i><?php echo __( 'Add Item', 'elementor' ); ?>
  148. </button>
  149. </div>
  150. <?php
  151. }
  152. }