elements.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor elements manager.
  8. *
  9. * Elementor elements manager handler class is responsible for registering and
  10. * initializing all the supported elements.
  11. *
  12. * @since 1.0.0
  13. */
  14. class Elements_Manager {
  15. /**
  16. * Element types.
  17. *
  18. * Holds the list of all the element types.
  19. *
  20. * @access private
  21. *
  22. * @var Element_Base[]
  23. */
  24. private $_element_types;
  25. /**
  26. * Element categories.
  27. *
  28. * Holds the list of all the element categories.
  29. *
  30. * @access private
  31. *
  32. * @var
  33. */
  34. private $categories;
  35. /**
  36. * Elements constructor.
  37. *
  38. * Initializing Elementor elements manager.
  39. *
  40. * @since 1.0.0
  41. * @access public
  42. */
  43. public function __construct() {
  44. $this->require_files();
  45. }
  46. /**
  47. * Create element instance.
  48. *
  49. * This method creates a new element instance for any given element.
  50. *
  51. * @since 1.0.0
  52. * @access public
  53. *
  54. * @param array $element_data Element data.
  55. * @param array $element_args Optional. Element arguments. Default is
  56. * an empty array.
  57. * @param Element_Base $element_type Optional. Element type. Default is null.
  58. *
  59. * @return Element_Base|null Element instance if element created, or null
  60. * otherwise.
  61. */
  62. public function create_element_instance( array $element_data, array $element_args = [], Element_Base $element_type = null ) {
  63. if ( null === $element_type ) {
  64. if ( 'widget' === $element_data['elType'] ) {
  65. $element_type = Plugin::$instance->widgets_manager->get_widget_types( $element_data['widgetType'] );
  66. } else {
  67. $element_type = $this->get_element_types( $element_data['elType'] );
  68. }
  69. }
  70. if ( ! $element_type ) {
  71. return null;
  72. }
  73. $args = array_merge( $element_type->get_default_args(), $element_args );
  74. $element_class = $element_type->get_class_name();
  75. try {
  76. $element = new $element_class( $element_data, $args );
  77. } catch ( \Exception $e ) {
  78. return null;
  79. }
  80. return $element;
  81. }
  82. /**
  83. * Get element categories.
  84. *
  85. * Retrieve the list of categories the element belongs to.
  86. *
  87. * @since 1.0.0
  88. * @access public
  89. *
  90. * @return array Element categories.
  91. */
  92. public function get_categories() {
  93. if ( null === $this->categories ) {
  94. $this->init_categories();
  95. }
  96. return $this->categories;
  97. }
  98. /**
  99. * Add element category.
  100. *
  101. * Register new category for the element.
  102. *
  103. * @since 1.7.12
  104. * @since 2.0.0 The third parameter was deprecated.
  105. * @access public
  106. *
  107. * @param string $category_name Category name.
  108. * @param array $category_properties Category properties.
  109. */
  110. public function add_category( $category_name, $category_properties ) {
  111. if ( null === $this->categories ) {
  112. $this->get_categories();
  113. }
  114. if ( ! isset( $this->categories[ $category_name ] ) ) {
  115. $this->categories[ $category_name ] = $category_properties;
  116. }
  117. }
  118. /**
  119. * Register element type.
  120. *
  121. * Add new type to the list of registered types.
  122. *
  123. * @since 1.0.0
  124. * @access public
  125. *
  126. * @param Element_Base $element Element instance.
  127. *
  128. * @return bool Whether the element type was registered.
  129. */
  130. public function register_element_type( Element_Base $element ) {
  131. $this->_element_types[ $element->get_name() ] = $element;
  132. return true;
  133. }
  134. /**
  135. * Unregister element type.
  136. *
  137. * Remove element type from the list of registered types.
  138. *
  139. * @since 1.0.0
  140. * @access public
  141. *
  142. * @param string $name Element name.
  143. *
  144. * @return bool Whether the element type was unregister, or not.
  145. */
  146. public function unregister_element_type( $name ) {
  147. if ( ! isset( $this->_element_types[ $name ] ) ) {
  148. return false;
  149. }
  150. unset( $this->_element_types[ $name ] );
  151. return true;
  152. }
  153. /**
  154. * Get element types.
  155. *
  156. * Retrieve the list of all the element types, or if a specific element name
  157. * was provided retrieve his element types.
  158. *
  159. * @since 1.0.0
  160. * @access public
  161. *
  162. * @param string $element_name Optional. Element name. Default is null.
  163. *
  164. * @return null|Element_Base|Element_Base[] Element types, or a list of all the element
  165. * types, or null if element does not exist.
  166. */
  167. public function get_element_types( $element_name = null ) {
  168. if ( is_null( $this->_element_types ) ) {
  169. $this->init_elements();
  170. }
  171. if ( null !== $element_name ) {
  172. return isset( $this->_element_types[ $element_name ] ) ? $this->_element_types[ $element_name ] : null;
  173. }
  174. return $this->_element_types;
  175. }
  176. /**
  177. * Get element types config.
  178. *
  179. * Retrieve the config of all the element types.
  180. *
  181. * @since 1.0.0
  182. * @access public
  183. *
  184. * @return array Element types config.
  185. */
  186. public function get_element_types_config() {
  187. $config = [];
  188. foreach ( $this->get_element_types() as $element ) {
  189. $config[ $element->get_name() ] = $element->get_config();
  190. }
  191. return $config;
  192. }
  193. /**
  194. * Render elements content.
  195. *
  196. * Used to generate the elements templates on the editor.
  197. *
  198. * @since 1.0.0
  199. * @access public
  200. */
  201. public function render_elements_content() {
  202. foreach ( $this->get_element_types() as $element_type ) {
  203. $element_type->print_template();
  204. }
  205. }
  206. /**
  207. * Ajax discard changes.
  208. *
  209. * Ajax handler for Elementor discard_changes. Handles the discarded changes
  210. * in the builder by deleting auto-saved revisions.
  211. *
  212. * Fired by `wp_ajax_elementor_discard_changes` action.
  213. *
  214. * @since 1.9.0
  215. * @deprecated 2.0.0 Use `Plugin::$instance->documents->ajax_discard_changes()` method instead.
  216. * @access public
  217. *
  218. * @param $request
  219. *
  220. * @return bool
  221. */
  222. public function ajax_discard_changes( $request ) {
  223. _deprecated_function( __METHOD__, '2.0.0', 'Plugin::$instance->documents->ajax_discard_changes()' );
  224. return Plugin::$instance->documents->ajax_discard_changes( $request );
  225. }
  226. /**
  227. * Ajax save builder.
  228. *
  229. * Ajax handler for Elementor save_builder. Handles the saved data returned
  230. * by the builder.
  231. *
  232. * Fired by `wp_ajax_elementor_save_builder` action.
  233. *
  234. * @since 1.0.0
  235. * @deprecated 2.0.0 Use `Plugin::$instance->documents->ajax_save()` method instead.
  236. * @access public
  237. *
  238. * @param array $request
  239. *
  240. * @return mixed
  241. */
  242. public function ajax_save_builder( $request ) {
  243. _deprecated_function( __METHOD__, '2.0.0', 'Plugin::$instance->documents->ajax_save()' );
  244. $return_data = Plugin::$instance->documents->ajax_save( $request );
  245. /**
  246. * Returned ajax data.
  247. *
  248. * Filters the ajax data returned when saving the post on the builder.
  249. *
  250. * @since 1.0.0
  251. * @deprecated 2.0.0 Use `elementor/documents/ajax_save/return_data` filter instead.
  252. *
  253. * @param array $return_data The returned data. Default is an empty array.
  254. */
  255. $return_data = apply_filters_deprecated( 'elementor/ajax_save_builder/return_data', [ $return_data, $request['editor_post_id'] ], '2.0.0', 'elementor/documents/ajax_save/return_data' );
  256. return $return_data;
  257. }
  258. /**
  259. * Init elements.
  260. *
  261. * Initialize Elementor elements by registering the supported elements.
  262. * Elementor supports by default `section` element and `column` element.
  263. *
  264. * @since 2.0.0
  265. * @access private
  266. */
  267. private function init_elements() {
  268. $this->_element_types = [];
  269. foreach ( [ 'section', 'column' ] as $element_name ) {
  270. $class_name = __NAMESPACE__ . '\Element_' . $element_name;
  271. $this->register_element_type( new $class_name() );
  272. }
  273. /**
  274. * After elements registered.
  275. *
  276. * Fires after Elementor elements are registered.
  277. *
  278. * @since 1.0.0
  279. */
  280. do_action( 'elementor/elements/elements_registered' );
  281. }
  282. /**
  283. * Init categories.
  284. *
  285. * Initialize the element categories.
  286. *
  287. * @since 1.7.12
  288. * @access private
  289. */
  290. private function init_categories() {
  291. $this->categories = [
  292. 'basic' => [
  293. 'title' => __( 'Basic', 'elementor' ),
  294. 'icon' => 'eicon-font',
  295. ],
  296. 'pro-elements' => [
  297. 'title' => __( 'Pro', 'elementor' ),
  298. ],
  299. 'general' => [
  300. 'title' => __( 'General', 'elementor' ),
  301. 'icon' => 'eicon-font',
  302. ],
  303. 'theme-elements' => [
  304. 'title' => __( 'Site', 'elementor' ),
  305. 'active' => false,
  306. ],
  307. 'woocommerce-elements' => [
  308. 'title' => __( 'WooCommerce', 'elementor' ),
  309. 'active' => false,
  310. ],
  311. ];
  312. /**
  313. * When categories are registered.
  314. *
  315. * Fires after basic categories are registered, before WordPress
  316. * category have been registered.
  317. *
  318. * This is where categories registered by external developers are
  319. * added.
  320. *
  321. * @since 2.0.0
  322. *
  323. * @param Elements_Manager $this Elements manager instance.
  324. */
  325. do_action( 'elementor/elements/categories_registered', $this );
  326. $this->categories['pojo'] = [
  327. 'title' => __( 'Pojo Themes', 'elementor' ),
  328. 'icon' => 'eicon-pojome',
  329. ];
  330. $this->categories['wordpress'] = [
  331. 'title' => __( 'WordPress', 'elementor' ),
  332. 'icon' => 'eicon-wordpress',
  333. 'active' => false,
  334. ];
  335. }
  336. /**
  337. * Require files.
  338. *
  339. * Require Elementor element base class and column, section and repeater
  340. * elements.
  341. *
  342. * @since 1.0.0
  343. * @access private
  344. */
  345. private function require_files() {
  346. require_once ELEMENTOR_PATH . 'includes/base/element-base.php';
  347. require ELEMENTOR_PATH . 'includes/elements/column.php';
  348. require ELEMENTOR_PATH . 'includes/elements/section.php';
  349. require ELEMENTOR_PATH . 'includes/elements/repeater.php';
  350. }
  351. }