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