skin-base.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor skin base.
  8. *
  9. * An abstract class to register new skins for Elementor widgets. Skins allows
  10. * you to add new templates, set custom controls and more.
  11. *
  12. * To register new skins for your widget use the `add_skin()` method inside the
  13. * widget's `_register_skins()` method.
  14. *
  15. * @since 1.0.0
  16. * @abstract
  17. */
  18. abstract class Skin_Base {
  19. /**
  20. * Parent widget.
  21. *
  22. * Holds the parent widget of the skin. Default value is null, no parent widget.
  23. *
  24. * @access protected
  25. *
  26. * @var Widget_Base|null
  27. */
  28. protected $parent = null;
  29. /**
  30. * Skin base constructor.
  31. *
  32. * Initializing the skin base class by setting parent widget and registering
  33. * controls actions.
  34. *
  35. * @since 1.0.0
  36. * @access public
  37. * @param Widget_Base $parent
  38. */
  39. public function __construct( Widget_Base $parent ) {
  40. $this->parent = $parent;
  41. $this->_register_controls_actions();
  42. }
  43. /**
  44. * Get skin ID.
  45. *
  46. * Retrieve the skin ID.
  47. *
  48. * @since 1.0.0
  49. * @access public
  50. * @abstract
  51. */
  52. abstract public function get_id();
  53. /**
  54. * Get skin title.
  55. *
  56. * Retrieve the skin title.
  57. *
  58. * @since 1.0.0
  59. * @access public
  60. * @abstract
  61. */
  62. abstract public function get_title();
  63. /**
  64. * Render skin.
  65. *
  66. * Generates the final HTML on the frontend.
  67. *
  68. * @since 1.0.0
  69. * @access public
  70. * @abstract
  71. */
  72. abstract public function render();
  73. /**
  74. * Render skin output in the editor.
  75. *
  76. * Written as a Backbone JavaScript template and used to generate the live preview.
  77. *
  78. * @since 1.0.0
  79. * @deprecated 1.7.6
  80. * @access public
  81. */
  82. public function _content_template() {}
  83. /**
  84. * Register skin controls actions.
  85. *
  86. * Run on init and used to register new skins to be injected to the widget.
  87. * This method is used to register new actions that specify the location of
  88. * the skin in the widget.
  89. *
  90. * Example usage:
  91. * `add_action( 'elementor/element/{widget_id}/{section_id}/before_section_end', [ $this, 'register_controls' ] );`
  92. *
  93. * @since 1.0.0
  94. * @access protected
  95. */
  96. protected function _register_controls_actions() {}
  97. /**
  98. * Get skin control ID.
  99. *
  100. * Retrieve the skin control ID. Note that skin controls have special prefix
  101. * to distinguish them from regular controls, and from controls in other
  102. * skins.
  103. *
  104. * @since 1.0.0
  105. * @access protected
  106. *
  107. * @param string $control_base_id Control base ID.
  108. *
  109. * @return string Control ID.
  110. */
  111. protected function get_control_id( $control_base_id ) {
  112. $skin_id = str_replace( '-', '_', $this->get_id() );
  113. return $skin_id . '_' . $control_base_id;
  114. }
  115. /**
  116. * Get skin settings.
  117. *
  118. * Retrieve all the skin settings or, when requested, a specific setting.
  119. *
  120. * @since 1.0.0
  121. * @TODO: rename to get_setting() and create backward compatibility.
  122. *
  123. * @access public
  124. *
  125. * @param string $control_base_id Control base ID.
  126. *
  127. * @return Widget_Base Widget instance.
  128. */
  129. public function get_instance_value( $control_base_id ) {
  130. $control_id = $this->get_control_id( $control_base_id );
  131. return $this->parent->get_settings( $control_id );
  132. }
  133. /**
  134. * Start skin controls section.
  135. *
  136. * Used to add a new section of controls to the skin.
  137. *
  138. * @since 1.3.0
  139. * @access public
  140. *
  141. * @param string $id Section ID.
  142. * @param array $args Section arguments.
  143. */
  144. public function start_controls_section( $id, $args ) {
  145. $args['condition']['_skin'] = $this->get_id();
  146. $this->parent->start_controls_section( $this->get_control_id( $id ), $args );
  147. }
  148. /**
  149. * End skin controls section.
  150. *
  151. * Used to close an existing open skin controls section.
  152. *
  153. * @since 1.3.0
  154. * @access public
  155. */
  156. public function end_controls_section() {
  157. $this->parent->end_controls_section();
  158. }
  159. /**
  160. * Add new skin control.
  161. *
  162. * Register a single control to the allow the user to set/update skin data.
  163. *
  164. * @since 1.0.0
  165. * @access public
  166. *
  167. * @param string $id Control ID.
  168. * @param array $args Control arguments.
  169. *
  170. * @return bool True if skin added, False otherwise.
  171. */
  172. public function add_control( $id, $args ) {
  173. $args['condition']['_skin'] = $this->get_id();
  174. return $this->parent->add_control( $this->get_control_id( $id ), $args );
  175. }
  176. /**
  177. * Update skin control.
  178. *
  179. * Change the value of an existing skin control.
  180. *
  181. * @since 1.3.0
  182. * @since 1.8.1 New `$options` parameter added.
  183. *
  184. * @access public
  185. *
  186. * @param string $id Control ID.
  187. * @param array $args Control arguments. Only the new fields you want to update.
  188. * @param array $options Optional. Some additional options.
  189. */
  190. public function update_control( $id, $args, array $options = [] ) {
  191. $args['condition']['_skin'] = $this->get_id();
  192. $this->parent->update_control( $this->get_control_id( $id ), $args, $options );
  193. }
  194. /**
  195. * Remove skin control.
  196. *
  197. * Unregister an existing skin control.
  198. *
  199. * @since 1.3.0
  200. * @access public
  201. *
  202. * @param string $id Control ID.
  203. */
  204. public function remove_control( $id ) {
  205. $this->parent->remove_control( $this->get_control_id( $id ) );
  206. }
  207. /**
  208. * Add new responsive skin control.
  209. *
  210. * Register a set of controls to allow editing based on user screen size.
  211. *
  212. * @since 1.0.5
  213. * @access public
  214. *
  215. * @param string $id Responsive control ID.
  216. * @param array $args Responsive control arguments.
  217. */
  218. public function add_responsive_control( $id, $args ) {
  219. $args['condition']['_skin'] = $this->get_id();
  220. $this->parent->add_responsive_control( $this->get_control_id( $id ), $args );
  221. }
  222. /**
  223. * Update responsive skin control.
  224. *
  225. * Change the value of an existing responsive skin control.
  226. *
  227. * @since 1.3.5
  228. * @access public
  229. *
  230. * @param string $id Responsive control ID.
  231. * @param array $args Responsive control arguments.
  232. */
  233. public function update_responsive_control( $id, $args ) {
  234. $this->parent->update_responsive_control( $this->get_control_id( $id ), $args );
  235. }
  236. /**
  237. * Remove responsive skin control.
  238. *
  239. * Unregister an existing skin responsive control.
  240. *
  241. * @since 1.3.5
  242. * @access public
  243. *
  244. * @param string $id Responsive control ID.
  245. */
  246. public function remove_responsive_control( $id ) {
  247. $this->parent->remove_responsive_control( $this->get_control_id( $id ) );
  248. }
  249. /**
  250. * Start skin controls tab.
  251. *
  252. * Used to add a new tab inside a group of tabs.
  253. *
  254. * @since 1.5.0
  255. * @access public
  256. *
  257. * @param string $id Control ID.
  258. * @param array $args Control arguments.
  259. */
  260. public function start_controls_tab( $id, $args ) {
  261. $args['condition']['_skin'] = $this->get_id();
  262. $this->parent->start_controls_tab( $this->get_control_id( $id ), $args );
  263. }
  264. /**
  265. * End skin controls tab.
  266. *
  267. * Used to close an existing open controls tab.
  268. *
  269. * @since 1.5.0
  270. * @access public
  271. */
  272. public function end_controls_tab() {
  273. $this->parent->end_controls_tab();
  274. }
  275. /**
  276. * Start skin controls tabs.
  277. *
  278. * Used to add a new set of tabs inside a section.
  279. *
  280. * @since 1.5.0
  281. * @access public
  282. *
  283. * @param string $id Control ID.
  284. */
  285. public function start_controls_tabs( $id ) {
  286. $args['condition']['_skin'] = $this->get_id();
  287. $this->parent->start_controls_tabs( $this->get_control_id( $id ) );
  288. }
  289. /**
  290. * End skin controls tabs.
  291. *
  292. * Used to close an existing open controls tabs.
  293. *
  294. * @since 1.5.0
  295. * @access public
  296. */
  297. public function end_controls_tabs() {
  298. $this->parent->end_controls_tabs();
  299. }
  300. /**
  301. * Add new group control.
  302. *
  303. * Register a set of related controls grouped together as a single unified
  304. * control.
  305. *
  306. * @since 1.0.0
  307. * @access public
  308. *
  309. * @param string $group_name Group control name.
  310. * @param array $args Group control arguments. Default is an empty array.
  311. */
  312. final public function add_group_control( $group_name, $args = [] ) {
  313. $args['name'] = $this->get_control_id( $args['name'] );
  314. $args['condition']['_skin'] = $this->get_id();
  315. $this->parent->add_group_control( $group_name, $args );
  316. }
  317. /**
  318. * Set parent widget.
  319. *
  320. * Used to define the parent widget of the skin.
  321. *
  322. * @since 1.0.0
  323. * @access public
  324. *
  325. * @param Widget_Base $parent Parent widget.
  326. */
  327. public function set_parent( $parent ) {
  328. $this->parent = $parent;
  329. }
  330. }