base.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor base control.
  8. *
  9. * An abstract class for creating new controls in the panel.
  10. *
  11. * @since 1.0.0
  12. * @abstract
  13. */
  14. abstract class Base_Control {
  15. /**
  16. * Base settings.
  17. *
  18. * Holds all the base settings of the control.
  19. *
  20. * @access private
  21. *
  22. * @var array
  23. */
  24. private $_base_settings = [
  25. 'label' => '',
  26. 'description' => '',
  27. 'show_label' => true,
  28. 'label_block' => false,
  29. 'separator' => 'default',
  30. ];
  31. /**
  32. * Settings.
  33. *
  34. * Holds all the settings of the control.
  35. *
  36. * @access private
  37. *
  38. * @var array
  39. */
  40. private $_settings = [];
  41. /**
  42. * Get features.
  43. *
  44. * Retrieve the list of all the available features. Currently Elementor uses only
  45. * the `UI` feature.
  46. *
  47. * @since 1.5.0
  48. * @access public
  49. * @static
  50. *
  51. * @return array Features array.
  52. */
  53. public static function get_features() {
  54. return [];
  55. }
  56. /**
  57. * Get control type.
  58. *
  59. * Retrieve the control type.
  60. *
  61. * @since 1.5.0
  62. * @access public
  63. * @abstract
  64. */
  65. abstract public function get_type();
  66. /**
  67. * Control base constructor.
  68. *
  69. * Initializing the control base class.
  70. *
  71. * @since 1.5.0
  72. * @access public
  73. */
  74. public function __construct() {
  75. $this->_settings = array_merge( $this->_base_settings, $this->get_default_settings() );
  76. $this->_settings['features'] = static::get_features();
  77. }
  78. /**
  79. * Enqueue control scripts and styles.
  80. *
  81. * Used to register and enqueue custom scripts and styles used by the control.
  82. *
  83. * @since 1.5.0
  84. * @access public
  85. */
  86. public function enqueue() {}
  87. /**
  88. * Get control settings.
  89. *
  90. * Retrieve the control settings or a specific setting value.
  91. *
  92. * @since 1.5.0
  93. * @access public
  94. *
  95. * @param string $setting_key Optional. Specific key to return from the
  96. * settings. If key set it will return the
  97. * specific value of the key, otherwise the
  98. * entire key array will be returned. Default is
  99. * null.
  100. *
  101. * @return mixed The control settings, or specific setting value.
  102. */
  103. final public function get_settings( $setting_key = null ) {
  104. if ( $setting_key ) {
  105. if ( isset( $this->_settings[ $setting_key ] ) ) {
  106. return $this->_settings[ $setting_key ];
  107. }
  108. return null;
  109. }
  110. return $this->_settings;
  111. }
  112. /**
  113. * Set control settings.
  114. *
  115. * Used to set or to update the settings of an existing control.
  116. *
  117. * @since 1.5.0
  118. * @access public
  119. *
  120. * @param string $key Control settings key.
  121. * @param mixed $value Control settings value.
  122. */
  123. final public function set_settings( $key, $value ) {
  124. $this->_settings[ $key ] = $value;
  125. }
  126. /**
  127. * Control content template.
  128. *
  129. * Used to generate the control HTML in the editor using Underscore JS
  130. * template. The variables for the class are available using `data` JS
  131. * object.
  132. *
  133. * Note that the content template is wrapped by Base_Control::print_template().
  134. *
  135. * @since 1.5.0
  136. * @access public
  137. * @abstract
  138. */
  139. abstract public function content_template();
  140. /**
  141. * Print control template.
  142. *
  143. * Used to generate the control HTML in the editor using Underscore JS
  144. * template. The variables for the class are available using `data` JS
  145. * object.
  146. *
  147. * @since 1.5.0
  148. * @access public
  149. */
  150. final public function print_template() {
  151. ?>
  152. <script type="text/html" id="tmpl-elementor-control-<?php echo esc_attr( $this->get_type() ); ?>-content">
  153. <div class="elementor-control-content">
  154. <?php $this->content_template(); ?>
  155. </div>
  156. </script>
  157. <?php
  158. }
  159. /**
  160. * Get default control settings.
  161. *
  162. * Retrieve the default settings of the control. Used to return the default
  163. * settings while initializing the control.
  164. *
  165. * @since 1.5.0
  166. * @access protected
  167. *
  168. * @return array Control default settings.
  169. */
  170. protected function get_default_settings() {
  171. return [];
  172. }
  173. }