controls-stack.php 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  1. <?php
  2. namespace Elementor;
  3. use Elementor\Core\DynamicTags\Manager;
  4. if ( ! defined( 'ABSPATH' ) ) {
  5. exit; // Exit if accessed directly.
  6. }
  7. /**
  8. * Elementor controls stack.
  9. *
  10. * An abstract class that provides the needed properties and methods to
  11. * manage and handle controls in the editor panel to inheriting classes.
  12. *
  13. * @since 1.4.0
  14. * @abstract
  15. */
  16. abstract class Controls_Stack {
  17. /**
  18. * Responsive 'desktop' device name.
  19. */
  20. const RESPONSIVE_DESKTOP = 'desktop';
  21. /**
  22. * Responsive 'tablet' device name.
  23. */
  24. const RESPONSIVE_TABLET = 'tablet';
  25. /**
  26. * Responsive 'mobile' device name.
  27. */
  28. const RESPONSIVE_MOBILE = 'mobile';
  29. /**
  30. * Generic ID.
  31. *
  32. * Holds the unique ID.
  33. *
  34. * @access private
  35. *
  36. * @var string
  37. */
  38. private $id;
  39. /**
  40. * Parsed Settings.
  41. *
  42. * Holds the settings, which is the data entered by the user and processed
  43. * by elementor.
  44. *
  45. * @access private
  46. *
  47. * @var null|array
  48. */
  49. private $settings;
  50. private $active_settings;
  51. private $parsed_active_settings;
  52. /**
  53. * Parsed Dynamic Settings.
  54. *
  55. * @access private
  56. *
  57. * @var null|array
  58. */
  59. private $parsed_dynamic_settings;
  60. /**
  61. * Raw Data.
  62. *
  63. * Holds all the raw data including the element type, the child elements,
  64. * the user data.
  65. *
  66. * @access private
  67. *
  68. * @var null|array
  69. */
  70. private $data;
  71. /**
  72. * The configuration.
  73. *
  74. * Holds the configuration used to generate the Elementor editor. It includes
  75. * the element name, icon, categories, etc.
  76. *
  77. * @access private
  78. *
  79. * @var null|array
  80. */
  81. private $config;
  82. /**
  83. * Current section.
  84. *
  85. * Holds the current section while inserting a set of controls sections.
  86. *
  87. * @access private
  88. *
  89. * @var null|array
  90. */
  91. private $current_section;
  92. /**
  93. * Current tab.
  94. *
  95. * Holds the current tab while inserting a set of controls tabs.
  96. *
  97. * @access private
  98. *
  99. * @var null|array
  100. */
  101. private $current_tab;
  102. /**
  103. * Current popover.
  104. *
  105. * Holds the current popover while inserting a set of controls.
  106. *
  107. * @access private
  108. *
  109. * @var null|array
  110. */
  111. private $current_popover;
  112. /**
  113. * Injection point.
  114. *
  115. * Holds the injection point in the stack where the control will be inserted.
  116. *
  117. * @access private
  118. *
  119. * @var null|array
  120. */
  121. private $injection_point;
  122. /**
  123. * Data sanitized.
  124. *
  125. * @access private
  126. *
  127. * @var bool
  128. */
  129. private $settings_sanitized = false;
  130. /**
  131. * Get element name.
  132. *
  133. * Retrieve the element name.
  134. *
  135. * @since 1.4.0
  136. * @access public
  137. * @abstract
  138. *
  139. * @return string The name.
  140. */
  141. abstract public function get_name();
  142. /**
  143. * Get unique name.
  144. *
  145. * Some classes need to use unique names, this method allows you to create
  146. * them. By default it retrieves the regular name.
  147. *
  148. * @since 1.6.0
  149. * @access public
  150. *
  151. * @return string Unique name.
  152. */
  153. public function get_unique_name() {
  154. return $this->get_name();
  155. }
  156. /**
  157. * Get element ID.
  158. *
  159. * Retrieve the element generic ID.
  160. *
  161. * @since 1.4.0
  162. * @access public
  163. *
  164. * @return string The ID.
  165. */
  166. public function get_id() {
  167. return $this->id;
  168. }
  169. /**
  170. * Get element ID.
  171. *
  172. * Retrieve the element generic ID as integer.
  173. *
  174. * @since 1.8.0
  175. * @access public
  176. *
  177. * @return string The converted ID.
  178. */
  179. public function get_id_int() {
  180. return hexdec( $this->id );
  181. }
  182. /**
  183. * Get the type.
  184. *
  185. * Retrieve the type, e.g. 'stack', 'section', 'widget' etc.
  186. *
  187. * @since 1.4.0
  188. * @access public
  189. * @static
  190. *
  191. * @return string The type.
  192. */
  193. public static function get_type() {
  194. return 'stack';
  195. }
  196. /**
  197. * Get items.
  198. *
  199. * Utility method that receives an array with a needle and returns all the
  200. * items that match the needle. If needle is not defined the entire haystack
  201. * will be returned.
  202. *
  203. * @since 1.4.0
  204. * @access protected
  205. * @static
  206. *
  207. * @param array $haystack An array of items.
  208. * @param string $needle Optional. Needle. Default is null.
  209. *
  210. * @return mixed The whole haystack or the needle from the haystack when requested.
  211. */
  212. protected static function _get_items( array $haystack, $needle = null ) {
  213. if ( $needle ) {
  214. return isset( $haystack[ $needle ] ) ? $haystack[ $needle ] : null;
  215. }
  216. return $haystack;
  217. }
  218. /**
  219. * Get current section.
  220. *
  221. * When inserting new controls, this method will retrieve the current section.
  222. *
  223. * @since 1.7.1
  224. * @access public
  225. *
  226. * @return null|array Current section.
  227. */
  228. public function get_current_section() {
  229. return $this->current_section;
  230. }
  231. /**
  232. * Get current tab.
  233. *
  234. * When inserting new controls, this method will retrieve the current tab.
  235. *
  236. * @since 1.7.1
  237. * @access public
  238. *
  239. * @return null|array Current tab.
  240. */
  241. public function get_current_tab() {
  242. return $this->current_tab;
  243. }
  244. /**
  245. * Get controls.
  246. *
  247. * Retrieve all the controls or, when requested, a specific control.
  248. *
  249. * @since 1.4.0
  250. * @access public
  251. *
  252. * @param string $control_id The ID of the requested control. Optional field,
  253. * when set it will return a specific control.
  254. * Default is null.
  255. *
  256. * @return mixed Controls list.
  257. */
  258. public function get_controls( $control_id = null ) {
  259. return self::_get_items( $this->get_stack()['controls'], $control_id );
  260. }
  261. /**
  262. * Get active controls.
  263. *
  264. * Retrieve an array of active controls that meet the condition field.
  265. *
  266. * If specific controls was given as a parameter, retrieve active controls
  267. * from that list, otherwise check for all the controls available.
  268. *
  269. * @since 1.4.0
  270. * @since 2.0.9 Added the `controls` and the `settings` parameters.
  271. * @access public
  272. *
  273. * @param array $controls Optional. An array of controls. Default is null.
  274. * @param array $settings Optional. Controls settings. Default is null.
  275. *
  276. * @return array Active controls.
  277. */
  278. public function get_active_controls( array $controls = null, array $settings = null ) {
  279. if ( ! $controls ) {
  280. $controls = $this->get_controls();
  281. }
  282. if ( ! $settings ) {
  283. $settings = $this->get_controls_settings();
  284. }
  285. $active_controls = array_reduce(
  286. array_keys( $controls ), function( $active_controls, $control_key ) use ( $controls, $settings ) {
  287. $control = $controls[ $control_key ];
  288. if ( $this->is_control_visible( $control, $settings ) ) {
  289. $active_controls[ $control_key ] = $control;
  290. }
  291. return $active_controls;
  292. }, []
  293. );
  294. return $active_controls;
  295. }
  296. /**
  297. * Get controls settings.
  298. *
  299. * Retrieve the settings for all the controls that represent them.
  300. *
  301. * @since 1.5.0
  302. * @access public
  303. *
  304. * @return array Controls settings.
  305. */
  306. public function get_controls_settings() {
  307. return array_intersect_key( $this->get_settings(), $this->get_controls() );
  308. }
  309. /**
  310. * Add new control to stack.
  311. *
  312. * Register a single control to allow the user to set/update data.
  313. *
  314. * This method should be used inside `_register_controls()`.
  315. *
  316. * @since 1.4.0
  317. * @access public
  318. *
  319. * @param string $id Control ID.
  320. * @param array $args Control arguments.
  321. * @param array $options Optional. Control options. Default is an empty array.
  322. *
  323. * @return bool True if control added, False otherwise.
  324. */
  325. public function add_control( $id, array $args, $options = [] ) {
  326. $default_options = [
  327. 'overwrite' => false,
  328. 'position' => null,
  329. ];
  330. $options = array_merge( $default_options, $options );
  331. if ( $options['position'] ) {
  332. $this->start_injection( $options['position'] );
  333. }
  334. if ( $this->injection_point ) {
  335. $options['index'] = $this->injection_point['index']++;
  336. }
  337. if ( empty( $args['type'] ) || ! in_array( $args['type'], [ Controls_Manager::SECTION, Controls_Manager::WP_WIDGET ], true ) ) {
  338. $target_section_args = $this->current_section;
  339. $target_tab = $this->current_tab;
  340. if ( $this->injection_point ) {
  341. $target_section_args = $this->injection_point['section'];
  342. if ( ! empty( $this->injection_point['tab'] ) ) {
  343. $target_tab = $this->injection_point['tab'];
  344. }
  345. }
  346. if ( null !== $target_section_args ) {
  347. if ( ! empty( $args['section'] ) || ! empty( $args['tab'] ) ) {
  348. _doing_it_wrong( sprintf( '%s::%s', get_called_class(), __FUNCTION__ ), sprintf( 'Cannot redeclare control with `tab` or `section` args inside section "%s".', $id ), '1.0.0' );
  349. }
  350. $args = array_replace_recursive( $target_section_args, $args );
  351. if ( null !== $target_tab ) {
  352. $args = array_merge( $args, $target_tab );
  353. }
  354. } elseif ( empty( $args['section'] ) && ( ! $options['overwrite'] || is_wp_error( Plugin::$instance->controls_manager->get_control_from_stack( $this->get_unique_name(), $id ) ) ) ) {
  355. wp_die( sprintf( '%s::%s: Cannot add a control outside of a section (use `start_controls_section`).', get_called_class(), __FUNCTION__ ) );
  356. }
  357. }
  358. if ( $options['position'] ) {
  359. $this->end_injection();
  360. }
  361. unset( $options['position'] );
  362. if ( $this->current_popover && ! $this->current_popover['initialized'] ) {
  363. $args['popover'] = [
  364. 'start' => true,
  365. ];
  366. $this->current_popover['initialized'] = true;
  367. }
  368. return Plugin::$instance->controls_manager->add_control_to_stack( $this, $id, $args, $options );
  369. }
  370. /**
  371. * Remove control from stack.
  372. *
  373. * Unregister an existing control and remove it from the stack.
  374. *
  375. * @since 1.4.0
  376. * @access public
  377. *
  378. * @param string $control_id Control ID.
  379. *
  380. * @return bool|\WP_Error
  381. */
  382. public function remove_control( $control_id ) {
  383. return Plugin::$instance->controls_manager->remove_control_from_stack( $this->get_unique_name(), $control_id );
  384. }
  385. /**
  386. * Update control in stack.
  387. *
  388. * Change the value of an existing control in the stack. When you add new
  389. * control you set the `$args` parameter, this method allows you to update
  390. * the arguments by passing new data.
  391. *
  392. * @since 1.4.0
  393. * @since 1.8.1 New `$options` parameter added.
  394. *
  395. * @access public
  396. *
  397. * @param string $control_id Control ID.
  398. * @param array $args Control arguments. Only the new fields you want
  399. * to update.
  400. * @param array $options Optional. Some additional options. Default is
  401. * an empty array.
  402. *
  403. * @return bool
  404. */
  405. public function update_control( $control_id, array $args, array $options = [] ) {
  406. $is_updated = Plugin::$instance->controls_manager->update_control_in_stack( $this, $control_id, $args, $options );
  407. if ( ! $is_updated ) {
  408. return false;
  409. }
  410. $control = $this->get_controls( $control_id );
  411. if ( Controls_Manager::SECTION === $control['type'] ) {
  412. $section_args = $this->get_section_args( $control_id );
  413. $section_controls = $this->get_section_controls( $control_id );
  414. foreach ( $section_controls as $section_control_id => $section_control ) {
  415. $this->update_control( $section_control_id, $section_args );
  416. }
  417. }
  418. return true;
  419. }
  420. /**
  421. * Get stack.
  422. *
  423. * Retrieve the stack of controls.
  424. *
  425. * @since 1.9.2
  426. * @access public
  427. *
  428. * @return array Stack of controls.
  429. */
  430. public function get_stack() {
  431. $stack = Plugin::$instance->controls_manager->get_element_stack( $this );
  432. if ( null === $stack ) {
  433. $this->init_controls();
  434. return $this->get_stack();
  435. }
  436. return $stack;
  437. }
  438. /**
  439. * Get position information.
  440. *
  441. * Retrieve the position while injecting data, based on the element type.
  442. *
  443. * @since 1.7.0
  444. * @access public
  445. *
  446. * @param array $position {
  447. * The injection position.
  448. *
  449. * @type string $type Injection type, either `control` or `section`.
  450. * Default is `control`.
  451. * @type string $at Where to inject. If `$type` is `control` accepts
  452. * `before` and `after`. If `$type` is `section`
  453. * accepts `start` and `end`. Default values based on
  454. * the `type`.
  455. * @type string $of Control/Section ID.
  456. * @type array $fallback Fallback injection position. When the position is
  457. * not found it will try to fetch the fallback
  458. * position.
  459. * }
  460. *
  461. * @return bool|array Position info.
  462. */
  463. final public function get_position_info( array $position ) {
  464. $default_position = [
  465. 'type' => 'control',
  466. 'at' => 'after',
  467. ];
  468. if ( ! empty( $position['type'] ) && 'section' === $position['type'] ) {
  469. $default_position['at'] = 'end';
  470. }
  471. $position = array_merge( $default_position, $position );
  472. if (
  473. 'control' === $position['type'] && in_array( $position['at'], [ 'start', 'end' ], true ) ||
  474. 'section' === $position['type'] && in_array( $position['at'], [ 'before', 'after' ], true )
  475. ) {
  476. _doing_it_wrong( sprintf( '%s::%s', get_called_class(), __FUNCTION__ ), 'Invalid position arguments. Use `before` / `after` for control or `start` / `end` for section.', '1.7.0' );
  477. return false;
  478. }
  479. $target_control_index = $this->get_control_index( $position['of'] );
  480. if ( false === $target_control_index ) {
  481. if ( ! empty( $position['fallback'] ) ) {
  482. return $this->get_position_info( $position['fallback'] );
  483. }
  484. return false;
  485. }
  486. $target_section_index = $target_control_index;
  487. $registered_controls = $this->get_controls();
  488. $controls_keys = array_keys( $registered_controls );
  489. while ( Controls_Manager::SECTION !== $registered_controls[ $controls_keys[ $target_section_index ] ]['type'] ) {
  490. $target_section_index--;
  491. }
  492. if ( 'section' === $position['type'] ) {
  493. $target_control_index++;
  494. if ( 'end' === $position['at'] ) {
  495. while ( Controls_Manager::SECTION !== $registered_controls[ $controls_keys[ $target_control_index ] ]['type'] ) {
  496. if ( ++$target_control_index >= count( $registered_controls ) ) {
  497. break;
  498. }
  499. }
  500. }
  501. }
  502. $target_control = $registered_controls[ $controls_keys[ $target_control_index ] ];
  503. if ( 'after' === $position['at'] ) {
  504. $target_control_index++;
  505. }
  506. $section_id = $registered_controls[ $controls_keys[ $target_section_index ] ]['name'];
  507. $position_info = [
  508. 'index' => $target_control_index,
  509. 'section' => $this->get_section_args( $section_id ),
  510. ];
  511. if ( ! empty( $target_control['tabs_wrapper'] ) ) {
  512. $position_info['tab'] = [
  513. 'tabs_wrapper' => $target_control['tabs_wrapper'],
  514. 'inner_tab' => $target_control['inner_tab'],
  515. ];
  516. }
  517. return $position_info;
  518. }
  519. /**
  520. * Get control key.
  521. *
  522. * Retrieve the key of the control based on a given index of the control.
  523. *
  524. * @since 1.9.2
  525. * @access public
  526. *
  527. * @param string $control_index Control index.
  528. *
  529. * @return int Control key.
  530. */
  531. final public function get_control_key( $control_index ) {
  532. $registered_controls = $this->get_controls();
  533. $controls_keys = array_keys( $registered_controls );
  534. return $controls_keys[ $control_index ];
  535. }
  536. /**
  537. * Get control index.
  538. *
  539. * Retrieve the index of the control based on a given key of the control.
  540. *
  541. * @since 1.7.6
  542. * @access public
  543. *
  544. * @param string $control_key Control key.
  545. *
  546. * @return false|int Control index.
  547. */
  548. final public function get_control_index( $control_key ) {
  549. $controls = $this->get_controls();
  550. $controls_keys = array_keys( $controls );
  551. return array_search( $control_key, $controls_keys );
  552. }
  553. /**
  554. * Get section controls.
  555. *
  556. * Retrieve all controls under a specific section.
  557. *
  558. * @since 1.7.6
  559. * @access public
  560. *
  561. * @param string $section_id Section ID.
  562. *
  563. * @return array Section controls
  564. */
  565. final public function get_section_controls( $section_id ) {
  566. $section_index = $this->get_control_index( $section_id );
  567. $section_controls = [];
  568. $registered_controls = $this->get_controls();
  569. $controls_keys = array_keys( $registered_controls );
  570. while ( true ) {
  571. $section_index++;
  572. if ( ! isset( $controls_keys[ $section_index ] ) ) {
  573. break;
  574. }
  575. $control_key = $controls_keys[ $section_index ];
  576. if ( Controls_Manager::SECTION === $registered_controls[ $control_key ]['type'] ) {
  577. break;
  578. }
  579. $section_controls[ $control_key ] = $registered_controls[ $control_key ];
  580. };
  581. return $section_controls;
  582. }
  583. /**
  584. * Add new group control to stack.
  585. *
  586. * Register a set of related controls grouped together as a single unified
  587. * control. For example grouping together like typography controls into a
  588. * single, easy-to-use control.
  589. *
  590. * @since 1.4.0
  591. * @access public
  592. *
  593. * @param string $group_name Group control name.
  594. * @param array $args Group control arguments. Default is an empty array.
  595. * @param array $options Optional. Group control options. Default is an
  596. * empty array.
  597. */
  598. final public function add_group_control( $group_name, array $args = [], array $options = [] ) {
  599. $group = Plugin::$instance->controls_manager->get_control_groups( $group_name );
  600. if ( ! $group ) {
  601. wp_die( sprintf( '%s::%s: Group "%s" not found.', get_called_class(), __FUNCTION__, $group_name ) );
  602. }
  603. $group->add_controls( $this, $args, $options );
  604. }
  605. /**
  606. * Get scheme controls.
  607. *
  608. * Retrieve all the controls that use schemes.
  609. *
  610. * @since 1.4.0
  611. * @access public
  612. *
  613. * @return array Scheme controls.
  614. */
  615. final public function get_scheme_controls() {
  616. $enabled_schemes = Schemes_Manager::get_enabled_schemes();
  617. return array_filter(
  618. $this->get_controls(), function( $control ) use ( $enabled_schemes ) {
  619. return ( ! empty( $control['scheme'] ) && in_array( $control['scheme']['type'], $enabled_schemes ) );
  620. }
  621. );
  622. }
  623. /**
  624. * Get style controls.
  625. *
  626. * Retrieve style controls for all active controls or, when requested, from
  627. * a specific set of controls.
  628. *
  629. * @since 1.4.0
  630. * @since 2.0.9 Added the `settings` parameter.
  631. * @access public
  632. *
  633. * @param array $controls Optional. Controls list. Default is null.
  634. * @param array $settings Optional. Controls settings. Default is null.
  635. *
  636. * @return array Style controls.
  637. */
  638. final public function get_style_controls( array $controls = null, array $settings = null ) {
  639. $controls = $this->get_active_controls( $controls, $settings );
  640. $style_controls = [];
  641. foreach ( $controls as $control_name => $control ) {
  642. $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
  643. if ( ! $control_obj instanceof Base_Data_Control ) {
  644. continue;
  645. }
  646. $control = array_merge( $control_obj->get_settings(), $control );
  647. if ( Controls_Manager::REPEATER === $control['type'] ) {
  648. $style_fields = [];
  649. foreach ( $this->get_settings( $control_name ) as $item ) {
  650. $style_fields[] = $this->get_style_controls( $control['fields'], $item );
  651. }
  652. $control['style_fields'] = $style_fields;
  653. }
  654. if ( ! empty( $control['selectors'] ) || ! empty( $control['dynamic'] ) || ! empty( $control['style_fields'] ) ) {
  655. $style_controls[ $control_name ] = $control;
  656. }
  657. }
  658. return $style_controls;
  659. }
  660. /**
  661. * Get class controls.
  662. *
  663. * Retrieve the controls that use the same prefix class from all the active
  664. * controls
  665. *
  666. * @since 1.4.0
  667. * @deprecated 2.1.0
  668. * @access public
  669. *
  670. * @return array Class controls.
  671. */
  672. final public function get_class_controls() {
  673. return array_filter(
  674. $this->get_active_controls(), function( $control ) {
  675. return ( isset( $control['prefix_class'] ) );
  676. }
  677. );
  678. }
  679. /**
  680. * Get tabs controls.
  681. *
  682. * Retrieve all the tabs assigned to the control.
  683. *
  684. * @since 1.4.0
  685. * @access public
  686. *
  687. * @return array Tabs controls.
  688. */
  689. final public function get_tabs_controls() {
  690. return $this->get_stack()['tabs'];
  691. }
  692. /**
  693. * Add new responsive control to stack.
  694. *
  695. * Register a set of controls to allow editing based on user screen size.
  696. * This method registers three screen sizes: Desktop, Tablet and Mobile.
  697. *
  698. * @since 1.4.0
  699. * @access public
  700. *
  701. * @param string $id Responsive control ID.
  702. * @param array $args Responsive control arguments.
  703. * @param array $options Optional. Responsive control options. Default is
  704. * an empty array.
  705. */
  706. final public function add_responsive_control( $id, array $args, $options = [] ) {
  707. $args['responsive'] = [];
  708. $devices = [
  709. self::RESPONSIVE_DESKTOP,
  710. self::RESPONSIVE_TABLET,
  711. self::RESPONSIVE_MOBILE,
  712. ];
  713. if ( isset( $args['devices'] ) ) {
  714. $devices = array_intersect( $devices, $args['devices'] );
  715. $args['responsive']['devices'] = $devices;
  716. unset( $args['devices'] );
  717. }
  718. if ( isset( $args['default'] ) ) {
  719. $args['desktop_default'] = $args['default'];
  720. unset( $args['default'] );
  721. }
  722. foreach ( $devices as $device_name ) {
  723. $control_args = $args;
  724. if ( isset( $control_args['device_args'] ) ) {
  725. if ( ! empty( $control_args['device_args'][ $device_name ] ) ) {
  726. $control_args = array_merge( $control_args, $control_args['device_args'][ $device_name ] );
  727. }
  728. unset( $control_args['device_args'] );
  729. }
  730. if ( ! empty( $args['prefix_class'] ) ) {
  731. $device_to_replace = self::RESPONSIVE_DESKTOP === $device_name ? '' : '-' . $device_name;
  732. $control_args['prefix_class'] = sprintf( $args['prefix_class'], $device_to_replace );
  733. }
  734. $control_args['responsive']['max'] = $device_name;
  735. if ( isset( $control_args['min_affected_device'] ) ) {
  736. if ( ! empty( $control_args['min_affected_device'][ $device_name ] ) ) {
  737. $control_args['responsive']['min'] = $control_args['min_affected_device'][ $device_name ];
  738. }
  739. unset( $control_args['min_affected_device'] );
  740. }
  741. if ( isset( $control_args[ $device_name . '_default' ] ) ) {
  742. $control_args['default'] = $control_args[ $device_name . '_default' ];
  743. }
  744. unset( $control_args['desktop_default'] );
  745. unset( $control_args['tablet_default'] );
  746. unset( $control_args['mobile_default'] );
  747. $id_suffix = self::RESPONSIVE_DESKTOP === $device_name ? '' : '_' . $device_name;
  748. if ( ! empty( $options['overwrite'] ) ) {
  749. $this->update_control( $id . $id_suffix, $control_args, [
  750. 'recursive' => ! empty( $options['recursive'] ),
  751. ] );
  752. } else {
  753. $this->add_control( $id . $id_suffix, $control_args, $options );
  754. }
  755. }
  756. }
  757. /**
  758. * Update responsive control in stack.
  759. *
  760. * Change the value of an existing responsive control in the stack. When you
  761. * add new control you set the `$args` parameter, this method allows you to
  762. * update the arguments by passing new data.
  763. *
  764. * @since 1.4.0
  765. * @access public
  766. *
  767. * @param string $id Responsive control ID.
  768. * @param array $args Responsive control arguments.
  769. * @param array $options Optional. Additional options.
  770. */
  771. final public function update_responsive_control( $id, array $args, array $options = [] ) {
  772. $this->add_responsive_control( $id, $args, [
  773. 'overwrite' => true,
  774. 'recursive' => ! empty( $options['recursive'] ),
  775. ] );
  776. }
  777. /**
  778. * Remove responsive control from stack.
  779. *
  780. * Unregister an existing responsive control and remove it from the stack.
  781. *
  782. * @since 1.4.0
  783. * @access public
  784. *
  785. * @param string $id Responsive control ID.
  786. */
  787. final public function remove_responsive_control( $id ) {
  788. $devices = [
  789. self::RESPONSIVE_DESKTOP,
  790. self::RESPONSIVE_TABLET,
  791. self::RESPONSIVE_MOBILE,
  792. ];
  793. foreach ( $devices as $device_name ) {
  794. $id_suffix = self::RESPONSIVE_DESKTOP === $device_name ? '' : '_' . $device_name;
  795. $this->remove_control( $id . $id_suffix );
  796. }
  797. }
  798. /**
  799. * Get class name.
  800. *
  801. * Retrieve the name of the current class.
  802. *
  803. * @since 1.4.0
  804. * @access public
  805. *
  806. * @return string Class name.
  807. */
  808. final public function get_class_name() {
  809. return get_called_class();
  810. }
  811. /**
  812. * Get the config.
  813. *
  814. * Retrieve the config or, if non set, use the initial config.
  815. *
  816. * @since 1.4.0
  817. * @access public
  818. *
  819. * @return array|null The config.
  820. */
  821. final public function get_config() {
  822. if ( null === $this->config ) {
  823. $this->config = $this->_get_initial_config();
  824. }
  825. return $this->config;
  826. }
  827. /**
  828. * Get frontend settings keys.
  829. *
  830. * Retrieve settings keys for all frontend controls.
  831. *
  832. * @since 1.6.0
  833. * @access public
  834. *
  835. * @return array Settings keys for each control.
  836. */
  837. final public function get_frontend_settings_keys() {
  838. $controls = [];
  839. foreach ( $this->get_controls() as $control ) {
  840. if ( ! empty( $control['frontend_available'] ) ) {
  841. $controls[] = $control['name'];
  842. }
  843. }
  844. return $controls;
  845. }
  846. /**
  847. * Get controls pointer index.
  848. *
  849. * Retrieve pointer index where the next control should be added.
  850. *
  851. * While using injection point, it will return the injection point index.
  852. * Otherwise index of the last control plus one.
  853. *
  854. * @since 1.9.2
  855. * @access public
  856. *
  857. * @return int Controls pointer index.
  858. */
  859. public function get_pointer_index() {
  860. if ( null !== $this->injection_point ) {
  861. return $this->injection_point['index'];
  862. }
  863. return count( $this->get_controls() );
  864. }
  865. /**
  866. * Get the raw data.
  867. *
  868. * Retrieve all the items or, when requested, a specific item.
  869. *
  870. * @since 1.4.0
  871. * @access public
  872. *
  873. * @param string $item Optional. The requested item. Default is null.
  874. *
  875. * @return mixed The raw data.
  876. */
  877. public function get_data( $item = null ) {
  878. if ( ! $this->settings_sanitized && ( ! $item || 'settings' === $item ) ) {
  879. $this->data['settings'] = $this->sanitize_settings( $this->data['settings'] );
  880. $this->settings_sanitized = true;
  881. }
  882. return self::_get_items( $this->data, $item );
  883. }
  884. /**
  885. * Get the settings.
  886. *
  887. * Retrieve all the settings or, when requested, a specific setting.
  888. *
  889. * @since 1.4.0
  890. * @access public
  891. *
  892. * @param string $setting Optional. The requested setting. Default is null.
  893. *
  894. * @return mixed The settings.
  895. */
  896. public function get_settings( $setting = null ) {
  897. if ( ! $this->settings ) {
  898. $this->settings = $this->_get_parsed_settings();
  899. }
  900. return self::_get_items( $this->settings, $setting );
  901. }
  902. public function get_parsed_dynamic_settings( $setting = null ) {
  903. if ( null === $this->parsed_dynamic_settings ) {
  904. $this->parsed_dynamic_settings = $this->parse_dynamic_settings( $this->get_settings() );
  905. }
  906. return self::_get_items( $this->parsed_dynamic_settings, $setting );
  907. }
  908. /**
  909. * Get active settings.
  910. *
  911. * Retrieve the settings from all the active controls.
  912. *
  913. * @since 1.4.0
  914. * @since 2.1.0 Added the `controls` and the `settings` parameters.
  915. * @access public
  916. *
  917. * @param array $controls Optional. An array of controls. Default is null.
  918. * @param array $settings Optional. Controls settings. Default is null.
  919. *
  920. * @return array Active settings.
  921. */
  922. public function get_active_settings( $settings = null, $controls = null ) {
  923. $is_first_request = ! $settings && ! $this->active_settings;
  924. if ( ! $settings ) {
  925. if ( $this->active_settings ) {
  926. return $this->active_settings;
  927. }
  928. $settings = $this->get_controls_settings();
  929. $controls = $this->get_controls();
  930. }
  931. $active_settings = [];
  932. foreach ( $settings as $setting_key => $setting ) {
  933. if ( ! isset( $controls[ $setting_key ] ) ) {
  934. $active_settings[ $setting_key ] = $setting;
  935. continue;
  936. }
  937. $control = $controls[ $setting_key ];
  938. if ( $this->is_control_visible( $control, $settings ) ) {
  939. if ( Controls_Manager::REPEATER === $control['type'] ) {
  940. foreach ( $setting as & $item ) {
  941. $item = $this->get_active_settings( $item, $control['fields'] );
  942. }
  943. }
  944. $active_settings[ $setting_key ] = $setting;
  945. } else {
  946. $active_settings[ $setting_key ] = null;
  947. }
  948. }
  949. if ( $is_first_request ) {
  950. $this->active_settings = $active_settings;
  951. }
  952. return $active_settings;
  953. }
  954. /**
  955. * Get settings for display.
  956. *
  957. * Retrieve all the settings or, when requested, a specific setting for display.
  958. *
  959. * Unlike `get_settings()` method, this method retrieves only active settings
  960. * that passed all the conditions, rendered all the shortcodes and all the dynamic
  961. * tags.
  962. *
  963. * @since 2.0.0
  964. * @access public
  965. *
  966. * @param string $setting_key Optional. The key of the requested setting.
  967. * Default is null.
  968. *
  969. * @return array The settings.
  970. */
  971. public function get_settings_for_display( $setting_key = null ) {
  972. if ( ! $this->parsed_active_settings ) {
  973. $this->parsed_active_settings = $this->get_active_settings( $this->get_parsed_dynamic_settings(), $this->get_controls() );
  974. }
  975. return self::_get_items( $this->parsed_active_settings, $setting_key );
  976. }
  977. /**
  978. * Parse dynamic settings.
  979. *
  980. * Retrieve the settings with rendered dynamic tags.
  981. *
  982. * @since 2.0.0
  983. * @access public
  984. *
  985. * @param array $settings Optional. The requested setting. Default is null.
  986. * @param array $controls Optional. The controls array. Default is null.
  987. * @param array $all_settings Optional. All the settings. Default is null.
  988. *
  989. * @return array The settings with rendered dynamic tags.
  990. */
  991. public function parse_dynamic_settings( $settings, $controls = null, $all_settings = null ) {
  992. if ( null === $all_settings ) {
  993. $all_settings = $this->get_settings();
  994. }
  995. if ( null === $controls ) {
  996. $controls = $this->get_controls();
  997. }
  998. foreach ( $controls as $control ) {
  999. $control_name = $control['name'];
  1000. $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
  1001. if ( ! $control_obj instanceof Base_Data_Control ) {
  1002. continue;
  1003. }
  1004. if ( 'repeater' === $control_obj->get_type() ) {
  1005. foreach ( $settings[ $control_name ] as & $field ) {
  1006. $field = $this->parse_dynamic_settings( $field, $control['fields'], $field );
  1007. }
  1008. continue;
  1009. }
  1010. if ( empty( $control['dynamic'] ) || ! isset( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ] ) ) {
  1011. continue;
  1012. }
  1013. $dynamic_settings = array_merge( $control_obj->get_settings( 'dynamic' ), $control['dynamic'] );
  1014. if ( ! empty( $dynamic_settings['active'] ) && ! empty( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ] ) ) {
  1015. $parsed_value = $control_obj->parse_tags( $all_settings[ Manager::DYNAMIC_SETTING_KEY ][ $control_name ], $dynamic_settings );
  1016. $dynamic_property = ! empty( $dynamic_settings['property'] ) ? $dynamic_settings['property'] : null;
  1017. if ( $dynamic_property ) {
  1018. $settings[ $control_name ][ $dynamic_property ] = $parsed_value;
  1019. } else {
  1020. $settings[ $control_name ] = $parsed_value;
  1021. }
  1022. }
  1023. }
  1024. return $settings;
  1025. }
  1026. /**
  1027. * Get frontend settings.
  1028. *
  1029. * Retrieve the settings for all frontend controls.
  1030. *
  1031. * @since 1.6.0
  1032. * @access public
  1033. *
  1034. * @return array Frontend settings.
  1035. */
  1036. public function get_frontend_settings() {
  1037. $frontend_settings = array_intersect_key( $this->get_active_settings(), array_flip( $this->get_frontend_settings_keys() ) );
  1038. foreach ( $frontend_settings as $key => $setting ) {
  1039. if ( in_array( $setting, [ null, '' ], true ) ) {
  1040. unset( $frontend_settings[ $key ] );
  1041. }
  1042. }
  1043. return $frontend_settings;
  1044. }
  1045. /**
  1046. * Filter controls settings.
  1047. *
  1048. * Receives controls, settings and a callback function to filter the settings by
  1049. * and returns filtered settings.
  1050. *
  1051. * @since 1.5.0
  1052. * @access public
  1053. *
  1054. * @param callable $callback The callback function.
  1055. * @param array $settings Optional. Control settings. Default is an empty
  1056. * array.
  1057. * @param array $controls Optional. Controls list. Default is an empty
  1058. * array.
  1059. *
  1060. * @return array Filtered settings.
  1061. */
  1062. public function filter_controls_settings( callable $callback, array $settings = [], array $controls = [] ) {
  1063. if ( ! $settings ) {
  1064. $settings = $this->get_settings();
  1065. }
  1066. if ( ! $controls ) {
  1067. $controls = $this->get_controls();
  1068. }
  1069. return array_reduce(
  1070. array_keys( $settings ), function( $filtered_settings, $setting_key ) use ( $controls, $settings, $callback ) {
  1071. if ( isset( $controls[ $setting_key ] ) ) {
  1072. $result = $callback( $settings[ $setting_key ], $controls[ $setting_key ] );
  1073. if ( null !== $result ) {
  1074. $filtered_settings[ $setting_key ] = $result;
  1075. }
  1076. }
  1077. return $filtered_settings;
  1078. }, []
  1079. );
  1080. }
  1081. /**
  1082. * Whether the control is visible or not.
  1083. *
  1084. * Used to determine whether the control is visible or not.
  1085. *
  1086. * @since 1.4.0
  1087. * @access public
  1088. *
  1089. * @param array $control The control.
  1090. * @param array $values Optional. Condition values. Default is null.
  1091. *
  1092. * @return bool Whether the control is visible.
  1093. */
  1094. public function is_control_visible( $control, $values = null ) {
  1095. if ( null === $values ) {
  1096. $values = $this->get_settings();
  1097. }
  1098. if ( ! empty( $control['conditions'] ) ) {
  1099. return Conditions::check( $control['conditions'], $values );
  1100. }
  1101. if ( empty( $control['condition'] ) ) {
  1102. return true;
  1103. }
  1104. foreach ( $control['condition'] as $condition_key => $condition_value ) {
  1105. preg_match( '/([a-z_0-9]+)(?:\[([a-z_]+)])?(!?)$/i', $condition_key, $condition_key_parts );
  1106. $pure_condition_key = $condition_key_parts[1];
  1107. $condition_sub_key = $condition_key_parts[2];
  1108. $is_negative_condition = ! ! $condition_key_parts[3];
  1109. if ( ! isset( $values[ $pure_condition_key ] ) || null === $values[ $pure_condition_key ] ) {
  1110. return false;
  1111. }
  1112. $instance_value = $values[ $pure_condition_key ];
  1113. if ( $condition_sub_key && is_array( $instance_value ) ) {
  1114. if ( ! isset( $instance_value[ $condition_sub_key ] ) ) {
  1115. return false;
  1116. }
  1117. $instance_value = $instance_value[ $condition_sub_key ];
  1118. }
  1119. /**
  1120. * If the $condition_value is a non empty array - check if the $condition_value contains the $instance_value,
  1121. * If the $instance_value is a non empty array - check if the $instance_value contains the $condition_value
  1122. * otherwise check if they are equal. ( and give the ability to check if the value is an empty array )
  1123. */
  1124. if ( is_array( $condition_value ) && ! empty( $condition_value ) ) {
  1125. $is_contains = in_array( $instance_value, $condition_value, true );
  1126. } elseif ( is_array( $instance_value ) && ! empty( $instance_value ) ) {
  1127. $is_contains = in_array( $condition_value, $instance_value, true );
  1128. } else {
  1129. $is_contains = $instance_value === $condition_value;
  1130. }
  1131. if ( $is_negative_condition && $is_contains || ! $is_negative_condition && ! $is_contains ) {
  1132. return false;
  1133. }
  1134. }
  1135. return true;
  1136. }
  1137. /**
  1138. * Start controls section.
  1139. *
  1140. * Used to add a new section of controls. When you use this method, all the
  1141. * registered controls from this point will be assigned to this section,
  1142. * until you close the section using `end_controls_section()` method.
  1143. *
  1144. * This method should be used inside `_register_controls()`.
  1145. *
  1146. * @since 1.4.0
  1147. * @access public
  1148. *
  1149. * @param string $section_id Section ID.
  1150. * @param array $args Section arguments.
  1151. */
  1152. public function start_controls_section( $section_id, array $args ) {
  1153. $section_name = $this->get_name();
  1154. /**
  1155. * Before section start.
  1156. *
  1157. * Fires before Elementor section starts in the editor panel.
  1158. *
  1159. * @since 1.4.0
  1160. *
  1161. * @param Controls_Stack $this The control.
  1162. * @param string $section_id Section ID.
  1163. * @param array $args Section arguments.
  1164. */
  1165. do_action( 'elementor/element/before_section_start', $this, $section_id, $args );
  1166. /**
  1167. * Before section start.
  1168. *
  1169. * Fires before Elementor section starts in the editor panel.
  1170. *
  1171. * The dynamic portions of the hook name, `$section_name` and `$section_id`, refers to the section name and section ID, respectively.
  1172. *
  1173. * @since 1.4.0
  1174. *
  1175. * @param Controls_Stack $this The control.
  1176. * @param array $args Section arguments.
  1177. */
  1178. do_action( "elementor/element/{$section_name}/{$section_id}/before_section_start", $this, $args );
  1179. $args['type'] = Controls_Manager::SECTION;
  1180. $this->add_control( $section_id, $args );
  1181. if ( null !== $this->current_section ) {
  1182. wp_die( sprintf( 'Elementor: You can\'t start a section before the end of the previous section "%s".', $this->current_section['section'] ) ); // XSS ok.
  1183. }
  1184. $this->current_section = $this->get_section_args( $section_id );
  1185. if ( $this->injection_point ) {
  1186. $this->injection_point['section'] = $this->current_section;
  1187. }
  1188. /**
  1189. * After section start.
  1190. *
  1191. * Fires after Elementor section starts in the editor panel.
  1192. *
  1193. * @since 1.4.0
  1194. *
  1195. * @param Controls_Stack $this The control.
  1196. * @param string $section_id Section ID.
  1197. * @param array $args Section arguments.
  1198. */
  1199. do_action( 'elementor/element/after_section_start', $this, $section_id, $args );
  1200. /**
  1201. * After section start.
  1202. *
  1203. * Fires after Elementor section starts in the editor panel.
  1204. *
  1205. * The dynamic portions of the hook name, `$section_name` and `$section_id`, refers to the section name and section ID, respectively.
  1206. *
  1207. * @since 1.4.0
  1208. *
  1209. * @param Controls_Stack $this The control.
  1210. * @param array $args Section arguments.
  1211. */
  1212. do_action( "elementor/element/{$section_name}/{$section_id}/after_section_start", $this, $args );
  1213. }
  1214. /**
  1215. * End controls section.
  1216. *
  1217. * Used to close an existing open controls section. When you use this method
  1218. * it stops adding new controls to this section.
  1219. *
  1220. * This method should be used inside `_register_controls()`.
  1221. *
  1222. * @since 1.4.0
  1223. * @access public
  1224. */
  1225. public function end_controls_section() {
  1226. $stack_name = $this->get_name();
  1227. // Save the current section for the action.
  1228. $current_section = $this->current_section;
  1229. $section_id = $current_section['section'];
  1230. $args = [
  1231. 'tab' => $current_section['tab'],
  1232. ];
  1233. /**
  1234. * Before section end.
  1235. *
  1236. * Fires before Elementor section ends in the editor panel.
  1237. *
  1238. * @since 1.4.0
  1239. *
  1240. * @param Controls_Stack $this The control.
  1241. * @param string $section_id Section ID.
  1242. * @param array $args Section arguments.
  1243. */
  1244. do_action( 'elementor/element/before_section_end', $this, $section_id, $args );
  1245. /**
  1246. * Before section end.
  1247. *
  1248. * Fires before Elementor section ends in the editor panel.
  1249. *
  1250. * The dynamic portions of the hook name, `$stack_name` and `$section_id`, refers to the stack name and section ID, respectively.
  1251. *
  1252. * @since 1.4.0
  1253. *
  1254. * @param Controls_Stack $this The control.
  1255. * @param array $args Section arguments.
  1256. */
  1257. do_action( "elementor/element/{$stack_name}/{$section_id}/before_section_end", $this, $args );
  1258. $this->current_section = null;
  1259. /**
  1260. * After section end.
  1261. *
  1262. * Fires after Elementor section ends in the editor panel.
  1263. *
  1264. * @since 1.4.0
  1265. *
  1266. * @param Controls_Stack $this The control.
  1267. * @param string $section_id Section ID.
  1268. * @param array $args Section arguments.
  1269. */
  1270. do_action( 'elementor/element/after_section_end', $this, $section_id, $args );
  1271. /**
  1272. * After section end.
  1273. *
  1274. * Fires after Elementor section ends in the editor panel.
  1275. *
  1276. * The dynamic portions of the hook name, `$stack_name` and `$section_id`, refers to the section name and section ID, respectively.
  1277. *
  1278. * @since 1.4.0
  1279. *
  1280. * @param Controls_Stack $this The control.
  1281. * @param array $args Section arguments.
  1282. */
  1283. do_action( "elementor/element/{$stack_name}/{$section_id}/after_section_end", $this, $args );
  1284. }
  1285. /**
  1286. * Start controls tabs.
  1287. *
  1288. * Used to add a new set of tabs inside a section. You should use this
  1289. * method before adding new individual tabs using `start_controls_tab()`.
  1290. * Each tab added after this point will be assigned to this group of tabs,
  1291. * until you close it using `end_controls_tabs()` method.
  1292. *
  1293. * This method should be used inside `_register_controls()`.
  1294. *
  1295. * @since 1.4.0
  1296. * @access public
  1297. *
  1298. * @param string $tabs_id Tabs ID.
  1299. */
  1300. public function start_controls_tabs( $tabs_id ) {
  1301. if ( null !== $this->current_tab ) {
  1302. wp_die( sprintf( 'Elementor: You can\'t start tabs before the end of the previous tabs "%s".', $this->current_tab['tabs_wrapper'] ) ); // XSS ok.
  1303. }
  1304. $this->add_control(
  1305. $tabs_id,
  1306. [
  1307. 'type' => Controls_Manager::TABS,
  1308. ]
  1309. );
  1310. $this->current_tab = [
  1311. 'tabs_wrapper' => $tabs_id,
  1312. ];
  1313. if ( $this->injection_point ) {
  1314. $this->injection_point['tab'] = $this->current_tab;
  1315. }
  1316. }
  1317. /**
  1318. * End controls tabs.
  1319. *
  1320. * Used to close an existing open controls tabs. When you use this method it
  1321. * stops adding new controls to this tabs.
  1322. *
  1323. * This method should be used inside `_register_controls()`.
  1324. *
  1325. * @since 1.4.0
  1326. * @access public
  1327. */
  1328. public function end_controls_tabs() {
  1329. $this->current_tab = null;
  1330. }
  1331. /**
  1332. * Start controls tab.
  1333. *
  1334. * Used to add a new tab inside a group of tabs. Use this method before
  1335. * adding new individual tabs using `start_controls_tab()`.
  1336. * Each tab added after this point will be assigned to this group of tabs,
  1337. * until you close it using `end_controls_tab()` method.
  1338. *
  1339. * This method should be used inside `_register_controls()`.
  1340. *
  1341. * @since 1.4.0
  1342. * @access public
  1343. *
  1344. * @param string $tab_id Tab ID.
  1345. * @param array $args Tab arguments.
  1346. */
  1347. public function start_controls_tab( $tab_id, $args ) {
  1348. if ( ! empty( $this->current_tab['inner_tab'] ) ) {
  1349. wp_die( sprintf( 'Elementor: You can\'t start a tab before the end of the previous tab "%s".', $this->current_tab['inner_tab'] ) ); // XSS ok.
  1350. }
  1351. $args['type'] = Controls_Manager::TAB;
  1352. $args['tabs_wrapper'] = $this->current_tab['tabs_wrapper'];
  1353. $this->add_control( $tab_id, $args );
  1354. $this->current_tab['inner_tab'] = $tab_id;
  1355. if ( $this->injection_point ) {
  1356. $this->injection_point['tab']['inner_tab'] = $this->current_tab['inner_tab'];
  1357. }
  1358. }
  1359. /**
  1360. * End controls tab.
  1361. *
  1362. * Used to close an existing open controls tab. When you use this method it
  1363. * stops adding new controls to this tab.
  1364. *
  1365. * This method should be used inside `_register_controls()`.
  1366. *
  1367. * @since 1.4.0
  1368. * @access public
  1369. */
  1370. public function end_controls_tab() {
  1371. unset( $this->current_tab['inner_tab'] );
  1372. }
  1373. /**
  1374. * Start popover.
  1375. *
  1376. * Used to add a new set of controls in a popover. When you use this method,
  1377. * all the registered controls from this point will be assigned to this
  1378. * popover, until you close the popover using `end_popover()` method.
  1379. *
  1380. * This method should be used inside `_register_controls()`.
  1381. *
  1382. * @since 1.9.0
  1383. * @access public
  1384. */
  1385. final public function start_popover() {
  1386. $this->current_popover = [
  1387. 'initialized' => false,
  1388. ];
  1389. }
  1390. /**
  1391. * End popover.
  1392. *
  1393. * Used to close an existing open popover. When you use this method it stops
  1394. * adding new controls to this popover.
  1395. *
  1396. * This method should be used inside `_register_controls()`.
  1397. *
  1398. * @since 1.9.0
  1399. * @access public
  1400. */
  1401. final public function end_popover() {
  1402. $this->current_popover = null;
  1403. $last_control_key = $this->get_control_key( $this->get_pointer_index() - 1 );
  1404. $args = [
  1405. 'popover' => [
  1406. 'end' => true,
  1407. ],
  1408. ];
  1409. $options = [
  1410. 'recursive' => true,
  1411. ];
  1412. $this->update_control( $last_control_key, $args, $options );
  1413. }
  1414. /**
  1415. * Print element template.
  1416. *
  1417. * Used to generate the element template on the editor.
  1418. *
  1419. * @since 2.0.0
  1420. * @access public
  1421. */
  1422. public function print_template() {
  1423. ob_start();
  1424. $this->_content_template();
  1425. $template_content = ob_get_clean();
  1426. $element_type = $this->get_type();
  1427. /**
  1428. * Template content.
  1429. *
  1430. * Filters the controls stack template content before it's printed in the editor.
  1431. *
  1432. * The dynamic portion of the hook name, `$element_type`, refers to the element type.
  1433. *
  1434. * @since 1.0.0
  1435. *
  1436. * @param string $content_template The controls stack template in the editor.
  1437. * @param Controls_Stack $this The controls stack.
  1438. */
  1439. $template_content = apply_filters( "elementor/{$element_type}/print_template", $template_content, $this );
  1440. if ( empty( $template_content ) ) {
  1441. return;
  1442. }
  1443. ?>
  1444. <script type="text/html" id="tmpl-elementor-<?php echo esc_attr( $this->get_name() ); ?>-content">
  1445. <?php $this->print_template_content( $template_content ); ?>
  1446. </script>
  1447. <?php
  1448. }
  1449. /**
  1450. * Start injection.
  1451. *
  1452. * Used to inject controls and sections to a specific position in the stack.
  1453. *
  1454. * When you use this method, all the registered controls and sections will
  1455. * be injected to a specific position in the stack, until you stop the
  1456. * injection using `end_injection()` method.
  1457. *
  1458. * @since 1.7.1
  1459. * @access public
  1460. *
  1461. * @param array $position {
  1462. * The position where to start the injection.
  1463. *
  1464. * @type string $type Injection type, either `control` or `section`.
  1465. * Default is `control`.
  1466. * @type string $at Where to inject. If `$type` is `control` accepts
  1467. * `before` and `after`. If `$type` is `section`
  1468. * accepts `start` and `end`. Default values based on
  1469. * the `type`.
  1470. * @type string $of Control/Section ID.
  1471. * }
  1472. */
  1473. final public function start_injection( array $position ) {
  1474. if ( $this->injection_point ) {
  1475. wp_die( 'A controls injection is already opened. Please close current injection before starting a new one (use `end_injection`).' );
  1476. }
  1477. $this->injection_point = $this->get_position_info( $position );
  1478. }
  1479. /**
  1480. * End injection.
  1481. *
  1482. * Used to close an existing opened injection point.
  1483. *
  1484. * When you use this method it stops adding new controls and sections to
  1485. * this point and continue to add controls to the regular position in the
  1486. * stack.
  1487. *
  1488. * @since 1.7.1
  1489. * @access public
  1490. */
  1491. final public function end_injection() {
  1492. $this->injection_point = null;
  1493. }
  1494. /**
  1495. * Get injection point.
  1496. *
  1497. * Retrieve the injection point in the stack where new controls and sections
  1498. * will be inserted.
  1499. *
  1500. * @since 1.9.2
  1501. * @access public
  1502. *
  1503. * @return array|null An array when an injection point is defined, null
  1504. * otherwise.
  1505. */
  1506. final public function get_injection_point() {
  1507. return $this->injection_point;
  1508. }
  1509. /**
  1510. * Set settings.
  1511. *
  1512. * Change or add new settings to an existing control in the stack.
  1513. *
  1514. * @since 1.4.0
  1515. * @access public
  1516. *
  1517. * @param string|array $key Setting name, or an array of key/value.
  1518. * @param string|null $value Optional. Setting value. Optional field if
  1519. * `$key` is an array. Default is null.
  1520. */
  1521. final public function set_settings( $key, $value = null ) {
  1522. if ( ! $this->settings ) {
  1523. $this->get_settings();
  1524. }
  1525. // strict check if override all settings.
  1526. if ( is_array( $key ) ) {
  1527. $this->settings = $key;
  1528. } else {
  1529. $this->settings[ $key ] = $value;
  1530. }
  1531. }
  1532. /**
  1533. * Register controls.
  1534. *
  1535. * Used to add new controls to any element type. For example, external
  1536. * developers use this method to register controls in a widget.
  1537. *
  1538. * Should be inherited and register new controls using `add_control()`,
  1539. * `add_responsive_control()` and `add_group_control()`, inside control
  1540. * wrappers like `start_controls_section()`, `start_controls_tabs()` and
  1541. * `start_controls_tab()`.
  1542. *
  1543. * @since 1.4.0
  1544. * @access protected
  1545. */
  1546. protected function _register_controls() {}
  1547. /**
  1548. * Get default data.
  1549. *
  1550. * Retrieve the default data. Used to reset the data on initialization.
  1551. *
  1552. * @since 1.4.0
  1553. * @access protected
  1554. *
  1555. * @return array Default data.
  1556. */
  1557. protected function get_default_data() {
  1558. return [
  1559. 'id' => 0,
  1560. 'settings' => [],
  1561. ];
  1562. }
  1563. /**
  1564. * Get parsed settings.
  1565. *
  1566. * Retrieve the parsed settings for all the controls that represent them.
  1567. * The parser set default values and process the settings.
  1568. *
  1569. * Classes that extend `Controls_Stack` can add new process to the settings
  1570. * parser.
  1571. *
  1572. * @since 1.4.0
  1573. * @access protected
  1574. *
  1575. * @return array Parsed settings.
  1576. */
  1577. protected function _get_parsed_settings() {
  1578. $settings = $this->get_data( 'settings' );
  1579. foreach ( $this->get_controls() as $control ) {
  1580. $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
  1581. if ( ! $control_obj instanceof Base_Data_Control ) {
  1582. continue;
  1583. }
  1584. $control = array_merge_recursive( $control_obj->get_settings(), $control );
  1585. $settings[ $control['name'] ] = $control_obj->get_value( $control, $settings );
  1586. }
  1587. return $settings;
  1588. }
  1589. /**
  1590. * Sanitize initial data.
  1591. *
  1592. * Performs data cleaning and sanitization.
  1593. *
  1594. * @since 2.0.0
  1595. * @deprecated 2.1.5 Use `Controls_Stack::sanitize_settings` instead
  1596. * @access protected
  1597. *
  1598. * @param array $data Data to sanitize.
  1599. * @param array $controls Optional. An array of controls. Default is an
  1600. * empty array.
  1601. *
  1602. * @return array Sanitized data.
  1603. */
  1604. protected function sanitize_initial_data( $data, array $controls = [] ) {
  1605. _deprecated_function( __METHOD__, '2.1.5', 'Controls_Stack::sanitize_settings' );
  1606. $data['settings'] = $this->sanitize_settings( $data['settings'], $controls );
  1607. return $data;
  1608. }
  1609. /**
  1610. * Get initial config.
  1611. *
  1612. * Retrieve the current element initial configuration - controls list and
  1613. * the tabs assigned to the control.
  1614. *
  1615. * @since 1.4.0
  1616. * @access protected
  1617. *
  1618. * @return array The initial config.
  1619. */
  1620. protected function _get_initial_config() {
  1621. return [
  1622. 'controls' => $this->get_controls(),
  1623. 'tabs_controls' => $this->get_tabs_controls(),
  1624. ];
  1625. }
  1626. /**
  1627. * Get section arguments.
  1628. *
  1629. * Retrieve the section arguments based on section ID.
  1630. *
  1631. * @since 1.4.0
  1632. * @access protected
  1633. *
  1634. * @param string $section_id Section ID.
  1635. *
  1636. * @return array Section arguments.
  1637. */
  1638. protected function get_section_args( $section_id ) {
  1639. $section_control = $this->get_controls( $section_id );
  1640. $section_args_keys = [ 'tab', 'condition' ];
  1641. $args = array_intersect_key( $section_control, array_flip( $section_args_keys ) );
  1642. $args['section'] = $section_id;
  1643. return $args;
  1644. }
  1645. /**
  1646. * Render element.
  1647. *
  1648. * Generates the final HTML on the frontend.
  1649. *
  1650. * @since 2.0.0
  1651. * @access protected
  1652. */
  1653. protected function render() {}
  1654. /**
  1655. * Print content template.
  1656. *
  1657. * Used to generate the content template on the editor, using a
  1658. * Backbone JavaScript template.
  1659. *
  1660. * @access protected
  1661. * @since 2.0.0
  1662. *
  1663. * @param string $template_content Template content.
  1664. */
  1665. protected function print_template_content( $template_content ) {
  1666. echo $template_content;
  1667. }
  1668. /**
  1669. * Render element output in the editor.
  1670. *
  1671. * Used to generate the live preview, using a Backbone JavaScript template.
  1672. *
  1673. * @since 2.0.0
  1674. * @access protected
  1675. */
  1676. protected function _content_template() {}
  1677. /**
  1678. * Initialize controls.
  1679. *
  1680. * Register the all controls added by `_register_controls()`.
  1681. *
  1682. * @since 2.0.0
  1683. * @access protected
  1684. */
  1685. protected function init_controls() {
  1686. Plugin::$instance->controls_manager->open_stack( $this );
  1687. $this->_register_controls();
  1688. }
  1689. /**
  1690. * Initialize the class.
  1691. *
  1692. * Set the raw data, the ID and the parsed settings.
  1693. *
  1694. * @since 1.4.0
  1695. * @access protected
  1696. *
  1697. * @param array $data Initial data.
  1698. */
  1699. protected function _init( $data ) {
  1700. $this->data = array_merge( $this->get_default_data(), $data );
  1701. $this->id = $data['id'];
  1702. }
  1703. /**
  1704. * Sanitize initial data.
  1705. *
  1706. * Performs settings cleaning and sanitization.
  1707. *
  1708. * @since 2.1.5
  1709. * @access private
  1710. *
  1711. * @param array $settings Settings to sanitize.
  1712. * @param array $controls Optional. An array of controls. Default is an
  1713. * empty array.
  1714. *
  1715. * @return array Sanitized settings.
  1716. */
  1717. private function sanitize_settings( array $settings, array $controls = [] ) {
  1718. if ( ! $controls ) {
  1719. $controls = $this->get_controls();
  1720. }
  1721. foreach ( $controls as $control ) {
  1722. if ( 'repeater' === $control['type'] ) {
  1723. if ( empty( $settings[ $control['name'] ] ) ) {
  1724. continue;
  1725. }
  1726. foreach ( $settings[ $control['name'] ] as $index => $repeater_row_data ) {
  1727. $sanitized_row_data = $this->sanitize_settings( $repeater_row_data, $control['fields'] );
  1728. $settings[ $control['name'] ][ $index ] = $sanitized_row_data;
  1729. }
  1730. continue;
  1731. }
  1732. $is_dynamic = isset( $settings[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ] );
  1733. if ( ! $is_dynamic ) {
  1734. continue;
  1735. }
  1736. $value_to_check = $settings[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ];
  1737. $tag_text_data = Plugin::$instance->dynamic_tags->tag_text_to_tag_data( $value_to_check );
  1738. if ( ! Plugin::$instance->dynamic_tags->get_tag_info( $tag_text_data['name'] ) ) {
  1739. unset( $settings[ Manager::DYNAMIC_SETTING_KEY ][ $control['name'] ] );
  1740. }
  1741. }
  1742. return $settings;
  1743. }
  1744. /**
  1745. * Controls stack constructor.
  1746. *
  1747. * Initializing the control stack class using `$data`. The `$data` is required
  1748. * for a normal instance. It is optional only for internal `type instance`.
  1749. *
  1750. * @since 1.4.0
  1751. * @access public
  1752. *
  1753. * @param array $data Optional. Control stack data. Default is an empty array.
  1754. */
  1755. public function __construct( array $data = [] ) {
  1756. if ( $data ) {
  1757. $this->_init( $data );
  1758. }
  1759. }
  1760. }