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