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