element-base.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor element base.
  8. *
  9. * An abstract class to register new Elementor elements. It extended the
  10. * `Controls_Stack` class to inherit its properties.
  11. *
  12. * This abstract class must be extended in order to register new elements.
  13. *
  14. * @since 1.0.0
  15. * @abstract
  16. */
  17. abstract class Element_Base extends Controls_Stack {
  18. /**
  19. * Child elements.
  20. *
  21. * Holds all the child elements of the element.
  22. *
  23. * @access private
  24. *
  25. * @var Element_Base[]
  26. */
  27. private $_children;
  28. /**
  29. * Element render attributes.
  30. *
  31. * Holds all the render attributes of the element. Used to store data like
  32. * the HTML class name and the class value, or HTML element ID name and value.
  33. *
  34. * @access private
  35. *
  36. * @var array
  37. */
  38. private $_render_attributes = [];
  39. /**
  40. * Element default arguments.
  41. *
  42. * Holds all the default arguments of the element. Used to store additional
  43. * data. For example WordPress widgets use this to store widget names.
  44. *
  45. * @access private
  46. *
  47. * @var array
  48. */
  49. private $_default_args = [];
  50. /**
  51. * Element edit tools.
  52. *
  53. * Holds all the edit tools of the element. For example: delete, duplicate etc.
  54. *
  55. * @access protected
  56. * @static
  57. *
  58. * @var array
  59. */
  60. protected static $_edit_tools;
  61. /**
  62. * Is type instance.
  63. *
  64. * Whether the element is an instance of that type or not.
  65. *
  66. * @access private
  67. *
  68. * @var bool
  69. */
  70. private $_is_type_instance = true;
  71. /**
  72. * Depended scripts.
  73. *
  74. * Holds all the element depended scripts to enqueue.
  75. *
  76. * @since 1.9.0
  77. * @access private
  78. *
  79. * @var array
  80. */
  81. private $depended_scripts = [];
  82. /**
  83. * Depended styles.
  84. *
  85. * Holds all the element depended styles to enqueue.
  86. *
  87. * @since 1.9.0
  88. * @access private
  89. *
  90. * @var array
  91. */
  92. private $depended_styles = [];
  93. /**
  94. * Add script depends.
  95. *
  96. * Register new script to enqueue by the handler.
  97. *
  98. * @since 1.9.0
  99. * @access public
  100. *
  101. * @param string $handler Depend script handler.
  102. */
  103. public function add_script_depends( $handler ) {
  104. $this->depended_scripts[] = $handler;
  105. }
  106. /**
  107. * Add style depends.
  108. *
  109. * Register new style to enqueue by the handler.
  110. *
  111. * @since 1.9.0
  112. * @access public
  113. *
  114. * @param string $handler Depend style handler.
  115. */
  116. public function add_style_depends( $handler ) {
  117. $this->depended_styles[] = $handler;
  118. }
  119. /**
  120. * Get script dependencies.
  121. *
  122. * Retrieve the list of script dependencies the element requires.
  123. *
  124. * @since 1.3.0
  125. * @access public
  126. *
  127. * @return array Element scripts dependencies.
  128. */
  129. public function get_script_depends() {
  130. return $this->depended_scripts;
  131. }
  132. /**
  133. * Enqueue scripts.
  134. *
  135. * Registers all the scripts defined as element dependencies and enqueues
  136. * them. Use `get_script_depends()` method to add custom script dependencies.
  137. *
  138. * @since 1.3.0
  139. * @access public
  140. */
  141. final public function enqueue_scripts() {
  142. foreach ( $this->get_script_depends() as $script ) {
  143. wp_enqueue_script( $script );
  144. }
  145. }
  146. /**
  147. * Get style dependencies.
  148. *
  149. * Retrieve the list of style dependencies the element requires.
  150. *
  151. * @since 1.9.0
  152. * @access public
  153. *
  154. * @return array Element styles dependencies.
  155. */
  156. public function get_style_depends() {
  157. return $this->depended_styles;
  158. }
  159. /**
  160. * Enqueue styles.
  161. *
  162. * Registers all the styles defined as element dependencies and enqueues
  163. * them. Use `get_style_depends()` method to add custom style dependencies.
  164. *
  165. * @since 1.9.0
  166. * @access public
  167. */
  168. final public function enqueue_styles() {
  169. foreach ( $this->get_style_depends() as $style ) {
  170. wp_enqueue_style( $style );
  171. }
  172. }
  173. /**
  174. * Get element edit tools.
  175. *
  176. * Used to retrieve the element edit tools.
  177. *
  178. * @since 1.0.0
  179. * @access public
  180. * @static
  181. *
  182. * @return array Element edit tools.
  183. */
  184. final public static function get_edit_tools() {
  185. if ( ! Plugin::instance()->role_manager->user_can( 'design' ) ) {
  186. return [];
  187. }
  188. if ( null === static::$_edit_tools ) {
  189. self::init_edit_tools();
  190. }
  191. return static::$_edit_tools;
  192. }
  193. /**
  194. * Add new edit tool.
  195. *
  196. * Register new edit tool for the element.
  197. *
  198. * @since 1.0.0
  199. * @access public
  200. * @static
  201. *
  202. * @param string $tool_name Edit tool name.
  203. * @param string[] $tool_data Edit tool data.
  204. * @param string $after Optional. If tool ID defined, the new edit tool
  205. * will be added after it. If null, the new edit
  206. * tool will be added at the end. Default is null.
  207. *
  208. */
  209. final public static function add_edit_tool( $tool_name, $tool_data, $after = null ) {
  210. if ( null === static::$_edit_tools ) {
  211. self::init_edit_tools();
  212. }
  213. // Adding the tool at specific position
  214. // in the tools array if requested
  215. if ( $after ) {
  216. $after_index = array_search( $after, array_keys( static::$_edit_tools ), true ) + 1;
  217. static::$_edit_tools = array_slice( static::$_edit_tools, 0, $after_index, true ) +
  218. [
  219. $tool_name => $tool_data,
  220. ] +
  221. array_slice( static::$_edit_tools, $after_index, null, true );
  222. } else {
  223. static::$_edit_tools[ $tool_name ] = $tool_data;
  224. }
  225. }
  226. final public static function is_edit_buttons_enabled() {
  227. return get_option( 'elementor_edit_buttons' );
  228. }
  229. /**
  230. * Get default edit tools.
  231. *
  232. * Retrieve the element default edit tools. Used to set initial tools.
  233. * By default the element has no edit tools.
  234. *
  235. * @since 1.0.0
  236. * @access protected
  237. * @static
  238. *
  239. * @return array Default edit tools.
  240. */
  241. protected static function get_default_edit_tools() {
  242. return [];
  243. }
  244. /**
  245. * Initialize edit tools.
  246. *
  247. * Register default edit tools.
  248. *
  249. * @since 2.0.0
  250. * @access private
  251. * @static
  252. */
  253. private static function init_edit_tools() {
  254. static::$_edit_tools = static::get_default_edit_tools();
  255. }
  256. /**
  257. * Get default child type.
  258. *
  259. * Retrieve the default child type based on element data.
  260. *
  261. * Note that not all elements support children.
  262. *
  263. * @since 1.0.0
  264. * @access protected
  265. * @abstract
  266. *
  267. * @param array $element_data Element data.
  268. *
  269. * @return Element_Base
  270. */
  271. abstract protected function _get_default_child_type( array $element_data );
  272. /**
  273. * Before element rendering.
  274. *
  275. * Used to add stuff before the element.
  276. *
  277. * @since 1.0.0
  278. * @access public
  279. */
  280. public function before_render() {}
  281. /**
  282. * After element rendering.
  283. *
  284. * Used to add stuff after the element.
  285. *
  286. * @since 1.0.0
  287. * @access public
  288. */
  289. public function after_render() {}
  290. /**
  291. * Get element title.
  292. *
  293. * Retrieve the element title.
  294. *
  295. * @since 1.0.0
  296. * @access public
  297. *
  298. * @return string Element title.
  299. */
  300. public function get_title() {
  301. return '';
  302. }
  303. /**
  304. * Get element icon.
  305. *
  306. * Retrieve the element icon.
  307. *
  308. * @since 1.0.0
  309. * @access public
  310. *
  311. * @return string Element icon.
  312. */
  313. public function get_icon() {
  314. return 'eicon-columns';
  315. }
  316. /**
  317. * Whether the reload preview is required.
  318. *
  319. * Used to determine whether the reload preview is required or not.
  320. *
  321. * @since 1.0.0
  322. * @access public
  323. *
  324. * @return bool Whether the reload preview is required.
  325. */
  326. public function is_reload_preview_required() {
  327. return false;
  328. }
  329. /**
  330. * Print element content template.
  331. *
  332. * Used to generate the element content template on the editor, using a
  333. * Backbone JavaScript template.
  334. *
  335. * @access protected
  336. * @since 2.0.0
  337. *
  338. * @param string $template_content Template content.
  339. */
  340. protected function print_template_content( $template_content ) {
  341. $this->render_edit_tools();
  342. echo $template_content; // XSS ok.
  343. }
  344. /**
  345. * Get child elements.
  346. *
  347. * Retrieve all the child elements of this element.
  348. *
  349. * @since 1.0.0
  350. * @access public
  351. *
  352. * @return Element_Base[] Child elements.
  353. */
  354. public function get_children() {
  355. if ( null === $this->_children ) {
  356. $this->init_children();
  357. }
  358. return $this->_children;
  359. }
  360. /**
  361. * Get default arguments.
  362. *
  363. * Retrieve the element default arguments. Used to return all the default
  364. * arguments or a specific default argument, if one is set.
  365. *
  366. * @since 1.0.0
  367. * @access public
  368. *
  369. * @param array $item Optional. Default is null.
  370. *
  371. * @return array Default argument(s).
  372. */
  373. public function get_default_args( $item = null ) {
  374. return self::_get_items( $this->_default_args, $item );
  375. }
  376. /**
  377. * Get parent element.
  378. *
  379. * Retrieve the element parent. Used to check which element it belongs to.
  380. *
  381. * @since 1.0.0
  382. * @deprecated 1.7.6 Use `Element_Base::get_data( 'parent' )` instead.
  383. * @access public
  384. *
  385. * @return Element_Base Parent element.
  386. */
  387. public function get_parent() {
  388. _deprecated_function( __METHOD__, '1.7.6', __CLASS__ . '::get_data( \'parent\' )' );
  389. return $this->get_data( 'parent' );
  390. }
  391. /**
  392. * Add new child element.
  393. *
  394. * Register new child element to allow hierarchy.
  395. *
  396. * @since 1.0.0
  397. * @access public
  398. * @param array $child_data Child element data.
  399. * @param array $child_args Child element arguments.
  400. *
  401. * @return Element_Base|false Child element instance, or false if failed.
  402. */
  403. public function add_child( array $child_data, array $child_args = [] ) {
  404. if ( null === $this->_children ) {
  405. $this->init_children();
  406. }
  407. $child_type = $this->get_child_type( $child_data );
  408. if ( ! $child_type ) {
  409. return false;
  410. }
  411. $child = Plugin::$instance->elements_manager->create_element_instance( $child_data, $child_args, $child_type );
  412. if ( $child ) {
  413. $this->_children[] = $child;
  414. }
  415. return $child;
  416. }
  417. /**
  418. * Add render attribute.
  419. *
  420. * Used to add attributes to a specific HTML element.
  421. *
  422. * The HTML tag is represented by the element parameter, then you need to
  423. * define the attribute key and the attribute key. The final result will be:
  424. * `<element attribute_key="attribute_value">`.
  425. *
  426. * Example usage:
  427. *
  428. * `$this->add_render_attribute( 'wrapper', 'class', 'custom-widget-wrapper-class' );`
  429. * `$this->add_render_attribute( 'widget', 'id', 'custom-widget-id' );`
  430. * `$this->add_render_attribute( 'button', [ 'class' => 'custom-button-class', 'id' => 'custom-button-id' ] );`
  431. *
  432. * @since 1.0.0
  433. * @access public
  434. *
  435. * @param array|string $element The HTML element.
  436. * @param array|string $key Optional. Attribute key. Default is null.
  437. * @param array|string $value Optional. Attribute value. Default is null.
  438. * @param bool $overwrite Optional. Whether to overwrite existing
  439. * attribute. Default is false, not to overwrite.
  440. *
  441. * @return Element_Base Current instance of the element.
  442. */
  443. public function add_render_attribute( $element, $key = null, $value = null, $overwrite = false ) {
  444. if ( is_array( $element ) ) {
  445. foreach ( $element as $element_key => $attributes ) {
  446. $this->add_render_attribute( $element_key, $attributes, null, $overwrite );
  447. }
  448. return $this;
  449. }
  450. if ( is_array( $key ) ) {
  451. foreach ( $key as $attribute_key => $attributes ) {
  452. $this->add_render_attribute( $element, $attribute_key, $attributes, $overwrite );
  453. }
  454. return $this;
  455. }
  456. if ( empty( $this->_render_attributes[ $element ][ $key ] ) ) {
  457. $this->_render_attributes[ $element ][ $key ] = [];
  458. }
  459. settype( $value, 'array' );
  460. if ( $overwrite ) {
  461. $this->_render_attributes[ $element ][ $key ] = $value;
  462. } else {
  463. $this->_render_attributes[ $element ][ $key ] = array_merge( $this->_render_attributes[ $element ][ $key ], $value );
  464. }
  465. return $this;
  466. }
  467. /**
  468. * Set render attribute.
  469. *
  470. * Used to set the value of the HTML element render attribute or to update
  471. * an existing render attribute.
  472. *
  473. * @since 1.0.0
  474. * @access public
  475. *
  476. * @param array|string $element The HTML element.
  477. * @param array|string $key Optional. Attribute key. Default is null.
  478. * @param array|string $value Optional. Attribute value. Default is null.
  479. *
  480. * @return Element_Base Current instance of the element.
  481. */
  482. public function set_render_attribute( $element, $key = null, $value = null ) {
  483. return $this->add_render_attribute( $element, $key, $value, true );
  484. }
  485. /**
  486. * Get render attribute string.
  487. *
  488. * Used to retrieve the value of the render attribute.
  489. *
  490. * @since 1.0.0
  491. * @access public
  492. *
  493. * @param array|string $element The element.
  494. *
  495. * @return string Render attribute string, or an empty string if the attribute
  496. * is empty or not exist.
  497. */
  498. public function get_render_attribute_string( $element ) {
  499. if ( empty( $this->_render_attributes[ $element ] ) ) {
  500. return '';
  501. }
  502. $render_attributes = $this->_render_attributes[ $element ];
  503. $attributes = [];
  504. foreach ( $render_attributes as $attribute_key => $attribute_values ) {
  505. $attributes[] = sprintf( '%1$s="%2$s"', $attribute_key, esc_attr( implode( ' ', $attribute_values ) ) );
  506. }
  507. return implode( ' ', $attributes );
  508. }
  509. /**
  510. * Print render attribute string.
  511. *
  512. * Used to output the rendered attribute.
  513. *
  514. * @since 2.0.0
  515. * @access public
  516. *
  517. * @param array|string $element The element.
  518. */
  519. public function print_render_attribute_string( $element ) {
  520. echo $this->get_render_attribute_string( $element ); // XSS ok.
  521. }
  522. /**
  523. * Print element.
  524. *
  525. * Used to generate the element final HTML on the frontend and the editor.
  526. *
  527. * @since 1.0.0
  528. * @access public
  529. */
  530. public function print_element() {
  531. $element_type = $this->get_type();
  532. /**
  533. * Before frontend element render.
  534. *
  535. * Fires before Elementor element is rendered in the frontend.
  536. *
  537. * @since 2.2.0
  538. *
  539. * @param Element_Base $this The element.
  540. */
  541. do_action( 'elementor/frontend/before_render', $this );
  542. /**
  543. * Before frontend element render.
  544. *
  545. * Fires before Elementor element is rendered in the frontend.
  546. *
  547. * The dynamic portion of the hook name, `$element_type`, refers to the element type.
  548. *
  549. * @since 1.0.0
  550. *
  551. * @param Element_Base $this The element.
  552. */
  553. do_action( "elementor/frontend/{$element_type}/before_render", $this );
  554. $this->_add_render_attributes();
  555. $this->before_render();
  556. $this->_print_content();
  557. $this->after_render();
  558. $this->enqueue_scripts();
  559. $this->enqueue_styles();
  560. /**
  561. * After frontend element render.
  562. *
  563. * Fires after Elementor element was rendered in the frontend.
  564. *
  565. * The dynamic portion of the hook name, `$element_type`, refers to the element type.
  566. *
  567. * @since 1.0.0
  568. *
  569. * @param Element_Base $this The element.
  570. */
  571. do_action( "elementor/frontend/{$element_type}/after_render", $this );
  572. }
  573. /**
  574. * Get the element raw data.
  575. *
  576. * Retrieve the raw element data, including the id, type, settings, child
  577. * elements and whether it is an inner element.
  578. *
  579. * The data with the HTML used always to display the data, but the Elementor
  580. * editor uses the raw data without the HTML in order not to render the data
  581. * again.
  582. *
  583. * @since 1.0.0
  584. * @access public
  585. *
  586. * @param bool $with_html_content Optional. Whether to return the data with
  587. * HTML content or without. Used for caching.
  588. * Default is false, without HTML.
  589. *
  590. * @return array Element raw data.
  591. */
  592. public function get_raw_data( $with_html_content = false ) {
  593. $data = $this->get_data();
  594. $elements = [];
  595. foreach ( $this->get_children() as $child ) {
  596. $elements[] = $child->get_raw_data( $with_html_content );
  597. }
  598. return [
  599. 'id' => $this->get_id(),
  600. 'elType' => $data['elType'],
  601. 'settings' => $data['settings'],
  602. 'elements' => $elements,
  603. 'isInner' => $data['isInner'],
  604. ];
  605. }
  606. /**
  607. * Get unique selector.
  608. *
  609. * Retrieve the unique selector of the element. Used to set a unique HTML
  610. * class for each HTML element. This way Elementor can set custom styles for
  611. * each element.
  612. *
  613. * @since 1.0.0
  614. * @access public
  615. *
  616. * @return string Unique selector.
  617. */
  618. public function get_unique_selector() {
  619. return '.elementor-element-' . $this->get_id();
  620. }
  621. /**
  622. * Render element edit tools.
  623. *
  624. * Used to generate the edit tools HTML.
  625. *
  626. * @since 1.0.0
  627. * @deprecated 1.8.0 Use `Element_Base::render_edit_tools()` instead.
  628. * @access protected
  629. */
  630. protected function _render_settings() {
  631. _deprecated_function( sprintf( '%s::%s', get_called_class(), __FUNCTION__ ), '1.8.0', '$this->render_edit_tools()' );
  632. $this->render_edit_tools();
  633. }
  634. /**
  635. * Render element edit tools.
  636. *
  637. * Used to generate the edit tools HTML.
  638. *
  639. * @since 1.8.0
  640. * @access protected
  641. */
  642. protected function render_edit_tools() {
  643. ?>
  644. <div class="elementor-element-overlay">
  645. <ul class="elementor-editor-element-settings elementor-editor-<?php echo $this->get_type(); ?>-settings">
  646. <?php
  647. foreach ( self::get_edit_tools() as $edit_tool_name => $edit_tool ) {
  648. ?>
  649. <li class="elementor-editor-element-setting elementor-editor-element-<?php echo esc_attr( $edit_tool_name ); ?>" title="<?php echo esc_attr( $edit_tool['title'] ); ?>">
  650. <i class="eicon-<?php echo esc_attr( $edit_tool['icon'] ); ?>" aria-hidden="true"></i>
  651. <span class="elementor-screen-only"><?php echo esc_html( $edit_tool['title'] ); ?></span>
  652. </li>
  653. <?php } ?>
  654. </ul>
  655. </div>
  656. <?php
  657. }
  658. /**
  659. * Is type instance.
  660. *
  661. * Used to determine whether the element is an instance of that type or not.
  662. *
  663. * @since 1.0.0
  664. * @access public
  665. *
  666. * @return bool Whether the element is an instance of that type.
  667. */
  668. public function is_type_instance() {
  669. return $this->_is_type_instance;
  670. }
  671. /**
  672. * Add render attributes.
  673. *
  674. * Used to add attributes to the current element wrapper HTML tag.
  675. *
  676. * @since 1.3.0
  677. * @access protected
  678. */
  679. protected function _add_render_attributes() {
  680. $id = $this->get_id();
  681. $this->add_render_attribute( '_wrapper', 'data-id', $id );
  682. $this->add_render_attribute(
  683. '_wrapper', 'class', [
  684. 'elementor-element',
  685. 'elementor-element-' . $id,
  686. ]
  687. );
  688. $settings = $this->get_active_settings();
  689. $controls = $this->get_controls();
  690. $class_settings = [];
  691. foreach ( $settings as $setting_key => $setting ) {
  692. if ( isset( $controls[ $setting_key ]['prefix_class'] ) ) {
  693. $class_settings[ $setting_key ] = $setting;
  694. }
  695. }
  696. foreach ( $class_settings as $setting_key => $setting ) {
  697. if ( empty( $setting ) && '0' !== $setting ) {
  698. continue;
  699. }
  700. $this->add_render_attribute( '_wrapper', 'class', $controls[ $setting_key ]['prefix_class'] . $setting );
  701. }
  702. if ( ! empty( $settings['animation'] ) || ! empty( $settings['_animation'] ) ) {
  703. // Hide the element until the animation begins
  704. $this->add_render_attribute( '_wrapper', 'class', 'elementor-invisible' );
  705. }
  706. if ( ! empty( $settings['_element_id'] ) ) {
  707. $this->add_render_attribute( '_wrapper', 'id', trim( $settings['_element_id'] ) );
  708. }
  709. $frontend_settings = $this->get_frontend_settings();
  710. if ( $frontend_settings ) {
  711. $this->add_render_attribute( '_wrapper', 'data-settings', wp_json_encode( $frontend_settings ) );
  712. }
  713. }
  714. /**
  715. * Get default data.
  716. *
  717. * Retrieve the default element data. Used to reset the data on initialization.
  718. *
  719. * @since 1.0.0
  720. * @access protected
  721. *
  722. * @return array Default data.
  723. */
  724. protected function get_default_data() {
  725. $data = parent::get_default_data();
  726. return array_merge(
  727. $data, [
  728. 'elements' => [],
  729. 'isInner' => false,
  730. ]
  731. );
  732. }
  733. /**
  734. * Print element content.
  735. *
  736. * Output the element final HTML on the frontend.
  737. *
  738. * @since 1.0.0
  739. * @access protected
  740. */
  741. protected function _print_content() {
  742. foreach ( $this->get_children() as $child ) {
  743. $child->print_element();
  744. }
  745. }
  746. /**
  747. * Get initial config.
  748. *
  749. * Retrieve the current element initial configuration.
  750. *
  751. * Adds more configuration on top of the controls list and the tabs assigned
  752. * to the control. This method also adds element name, type, icon and more.
  753. *
  754. * @since 1.0.10
  755. * @access protected
  756. *
  757. * @return array The initial config.
  758. */
  759. protected function _get_initial_config() {
  760. $config = [
  761. 'name' => $this->get_name(),
  762. 'elType' => $this->get_type(),
  763. 'title' => $this->get_title(),
  764. 'icon' => $this->get_icon(),
  765. 'reload_preview' => $this->is_reload_preview_required(),
  766. ];
  767. return array_merge( parent::_get_initial_config(), $config );
  768. }
  769. /**
  770. * Get child type.
  771. *
  772. * Retrieve the element child type based on element data.
  773. *
  774. * @since 2.0.0
  775. * @access private
  776. *
  777. * @param array $element_data Element ID.
  778. *
  779. * @return Element_Base|false Child type or false if type not found.
  780. */
  781. private function get_child_type( $element_data ) {
  782. $child_type = $this->_get_default_child_type( $element_data );
  783. // If it's not a valid widget ( like a deactivated plugin )
  784. if ( ! $child_type ) {
  785. return false;
  786. }
  787. /**
  788. * Element child type.
  789. *
  790. * Filters the child type of the element.
  791. *
  792. * @since 1.0.0
  793. *
  794. * @param Element_Base $child_type The child element.
  795. * @param array $element_data The original element ID.
  796. * @param Element_Base $this The original element.
  797. */
  798. $child_type = apply_filters( 'elementor/element/get_child_type', $child_type, $element_data, $this );
  799. return $child_type;
  800. }
  801. /**
  802. * Initialize children.
  803. *
  804. * Initializing the element child elements.
  805. *
  806. * @since 2.0.0
  807. * @access private
  808. */
  809. private function init_children() {
  810. $this->_children = [];
  811. $children_data = $this->get_data( 'elements' );
  812. if ( ! $children_data ) {
  813. return;
  814. }
  815. foreach ( $children_data as $child_data ) {
  816. if ( ! $child_data ) {
  817. continue;
  818. }
  819. $this->add_child( $child_data );
  820. }
  821. }
  822. /**
  823. * Element base constructor.
  824. *
  825. * Initializing the element base class using `$data` and `$args`.
  826. *
  827. * The `$data` parameter is required for a normal instance because of the
  828. * way Elementor renders data when initializing elements.
  829. *
  830. * @since 1.0.0
  831. * @access public
  832. *
  833. * @param array $data Optional. Element data. Default is an empty array.
  834. * @param array|null $args Optional. Element default arguments. Default is null.
  835. **/
  836. public function __construct( array $data = [], array $args = null ) {
  837. if ( $data ) {
  838. $this->_is_type_instance = false;
  839. } elseif ( $args ) {
  840. $this->_default_args = $args;
  841. }
  842. parent::__construct( $data );
  843. }
  844. }