repeater.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor repeater element.
  8. *
  9. * Elementor repeater handler class is responsible for initializing the repeater.
  10. *
  11. * @since 1.0.0
  12. */
  13. class Repeater extends Element_Base {
  14. /**
  15. * Repeater counter.
  16. *
  17. * Holds the Repeater counter data. Default is `0`.
  18. *
  19. * @since 1.0.0
  20. * @access private
  21. * @static
  22. *
  23. * @var int Repeater counter.
  24. */
  25. private static $counter = 0;
  26. /**
  27. * Repeater constructor.
  28. *
  29. * Initializing Elementor repeater element.
  30. *
  31. * @since 1.0.7
  32. * @access public
  33. *
  34. * @param array $data Optional. Element data. Default is an empty array.
  35. * @param array|null $args Optional. Element default arguments. Default is null.
  36. *
  37. */
  38. public function __construct( array $data = [], array $args = null ) {
  39. self::$counter++;
  40. parent::__construct( $data, $args );
  41. }
  42. /**
  43. * Get repeater name.
  44. *
  45. * Retrieve the repeater name.
  46. *
  47. * @since 1.0.7
  48. * @access public
  49. *
  50. * @return string Repeater name.
  51. */
  52. public function get_name() {
  53. return 'repeater-' . self::$counter;
  54. }
  55. /**
  56. * Get repeater type.
  57. *
  58. * Retrieve the repeater type.
  59. *
  60. * @since 1.0.0
  61. * @access public
  62. * @static
  63. *
  64. * @return string Repeater type.
  65. */
  66. public static function get_type() {
  67. return 'repeater';
  68. }
  69. /**
  70. * Add new repeater control to stack.
  71. *
  72. * Register a repeater control to allow the user to set/update data.
  73. *
  74. * This method should be used inside `_register_controls()`.
  75. *
  76. * @since 1.0.0
  77. * @access public
  78. *
  79. * @param string $id Repeater control ID.
  80. * @param array $args Repeater control arguments.
  81. * @param array $options Optional. Repeater control options. Default is an
  82. * empty array.
  83. *
  84. * @return bool True if repeater control added, False otherwise.
  85. */
  86. public function add_control( $id, array $args, $options = [] ) {
  87. $current_tab = $this->get_current_tab();
  88. if ( null !== $current_tab ) {
  89. $args = array_merge( $args, $current_tab );
  90. }
  91. return Plugin::$instance->controls_manager->add_control_to_stack( $this, $id, $args, $options );
  92. }
  93. /**
  94. * Get repeater fields.
  95. *
  96. * Retrieve the fields from the current repeater control.
  97. *
  98. * @since 1.5.0
  99. * @deprecated 2.1.0 Use `Repeater::get_controls()` instead.
  100. * @access public
  101. *
  102. * @return array Repeater fields.
  103. */
  104. public function get_fields() {
  105. return array_values( $this->get_controls() );
  106. }
  107. /**
  108. * Get default child type.
  109. *
  110. * Retrieve the repeater child type based on element data.
  111. *
  112. * Note that repeater does not support children, therefore it returns false.
  113. *
  114. * @since 1.0.0
  115. * @access protected
  116. *
  117. * @param array $element_data Element ID.
  118. *
  119. * @return false Repeater default child type or False if type not found.
  120. */
  121. protected function _get_default_child_type( array $element_data ) {
  122. return false;
  123. }
  124. }