| 1 |
<?php |
| 2 |
namespace Elementor; |
| 3 |
|
| 4 |
use Elementor\Core\Common\Modules\Ajax\Module as Ajax; |
| 5 |
use Elementor\Core\Utils\Exceptions; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Elementor widgets manager. |
| 13 |
* |
| 14 |
* Elementor widgets manager handler class is responsible for registering and |
| 15 |
* initializing all the supported Elementor widgets. |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
class Widgets_Manager { |
| 20 |
|
| 21 |
/** |
| 22 |
* Widget types. |
| 23 |
* |
| 24 |
* Holds the list of all the widget types. |
| 25 |
* |
| 26 |
* @since 1.0.0 |
| 27 |
* @access private |
| 28 |
* |
| 29 |
* @var Widget_Base[] |
| 30 |
*/ |
| 31 |
private $_widget_types = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* Init widgets. |
| 35 |
* |
| 36 |
* Initialize Elementor widgets manager. Include all the the widgets files |
| 37 |
* and register each Elementor and WordPress widget. |
| 38 |
* |
| 39 |
* @since 2.0.0 |
| 40 |
* @access private |
| 41 |
*/ |
| 42 |
private function init_widgets() { |
| 43 |
$build_widgets_filename = [ |
| 44 |
'common', |
| 45 |
'inner-section', |
| 46 |
'heading', |
| 47 |
'image', |
| 48 |
'text-editor', |
| 49 |
'video', |
| 50 |
'button', |
| 51 |
'divider', |
| 52 |
'spacer', |
| 53 |
'image-box', |
| 54 |
'google-maps', |
| 55 |
'icon', |
| 56 |
'icon-box', |
| 57 |
'star-rating', |
| 58 |
'image-carousel', |
| 59 |
'image-gallery', |
| 60 |
'icon-list', |
| 61 |
'counter', |
| 62 |
'progress', |
| 63 |
'testimonial', |
| 64 |
'tabs', |
| 65 |
'accordion', |
| 66 |
'toggle', |
| 67 |
'social-icons', |
| 68 |
'alert', |
| 69 |
'audio', |
| 70 |
'shortcode', |
| 71 |
'html', |
| 72 |
'menu-anchor', |
| 73 |
'sidebar', |
| 74 |
'read-more', |
| 75 |
]; |
| 76 |
|
| 77 |
$this->_widget_types = []; |
| 78 |
|
| 79 |
foreach ( $build_widgets_filename as $widget_filename ) { |
| 80 |
include( ELEMENTOR_PATH . 'includes/widgets/' . $widget_filename . '.php' ); |
| 81 |
|
| 82 |
$class_name = str_replace( '-', '_', $widget_filename ); |
| 83 |
|
| 84 |
$class_name = __NAMESPACE__ . '\Widget_' . $class_name; |
| 85 |
|
| 86 |
$this->register( new $class_name() ); |
| 87 |
} |
| 88 |
|
| 89 |
$this->register_wp_widgets(); |
| 90 |
|
| 91 |
/** |
| 92 |
* After widgets registered. |
| 93 |
* |
| 94 |
* Fires after Elementor widgets are registered. |
| 95 |
* |
| 96 |
* @since 1.0.0 |
| 97 |
* @deprecated 3.5.0 Use `elementor/widgets/register` hook instead. |
| 98 |
* |
| 99 |
* @param Widgets_Manager $this The widgets manager. |
| 100 |
*/ |
| 101 |
Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->do_deprecated_action( |
| 102 |
'elementor/widgets/widgets_registered', |
| 103 |
[ $this ], |
| 104 |
'3.5.0', |
| 105 |
'elementor/widgets/register' |
| 106 |
); |
| 107 |
|
| 108 |
/** |
| 109 |
* After widgets registered. |
| 110 |
* |
| 111 |
* Fires after Elementor widgets are registered. |
| 112 |
* |
| 113 |
* @since 3.5.0 |
| 114 |
* |
| 115 |
* @param Widgets_Manager $this The widgets manager. |
| 116 |
*/ |
| 117 |
do_action( 'elementor/widgets/register', $this ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Register WordPress widgets. |
| 122 |
* |
| 123 |
* Add native WordPress widget to the list of registered widget types. |
| 124 |
* |
| 125 |
* Exclude the widgets that are in Elementor widgets black list. Theme and |
| 126 |
* plugin authors can filter the black list. |
| 127 |
* |
| 128 |
* @since 2.0.0 |
| 129 |
* @access private |
| 130 |
*/ |
| 131 |
private function register_wp_widgets() { |
| 132 |
global $wp_widget_factory; |
| 133 |
|
| 134 |
// Skip Pojo widgets. |
| 135 |
$pojo_allowed_widgets = [ |
| 136 |
'Pojo_Widget_Recent_Posts', |
| 137 |
'Pojo_Widget_Posts_Group', |
| 138 |
'Pojo_Widget_Gallery', |
| 139 |
'Pojo_Widget_Recent_Galleries', |
| 140 |
'Pojo_Slideshow_Widget', |
| 141 |
'Pojo_Forms_Widget', |
| 142 |
'Pojo_Widget_News_Ticker', |
| 143 |
|
| 144 |
'Pojo_Widget_WC_Products', |
| 145 |
'Pojo_Widget_WC_Products_Category', |
| 146 |
'Pojo_Widget_WC_Product_Categories', |
| 147 |
]; |
| 148 |
|
| 149 |
// Allow themes/plugins to filter out their widgets. |
| 150 |
$black_list = []; |
| 151 |
|
| 152 |
/** |
| 153 |
* Elementor widgets black list. |
| 154 |
* |
| 155 |
* Filters the widgets black list that won't be displayed in the panel. |
| 156 |
* |
| 157 |
* @since 1.0.0 |
| 158 |
* |
| 159 |
* @param array $black_list A black list of widgets. Default is an empty array. |
| 160 |
*/ |
| 161 |
$black_list = apply_filters( 'elementor/widgets/black_list', $black_list ); |
| 162 |
|
| 163 |
foreach ( $wp_widget_factory->widgets as $widget_class => $widget_obj ) { |
| 164 |
|
| 165 |
if ( in_array( $widget_class, $black_list ) ) { |
| 166 |
continue; |
| 167 |
} |
| 168 |
|
| 169 |
if ( $widget_obj instanceof \Pojo_Widget_Base && ! in_array( $widget_class, $pojo_allowed_widgets ) ) { |
| 170 |
continue; |
| 171 |
} |
| 172 |
|
| 173 |
$elementor_widget_class = __NAMESPACE__ . '\Widget_WordPress'; |
| 174 |
|
| 175 |
$this->register( |
| 176 |
new $elementor_widget_class( [], [ |
| 177 |
'widget_name' => $widget_class, |
| 178 |
] ) |
| 179 |
); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Require files. |
| 185 |
* |
| 186 |
* Require Elementor widget base class. |
| 187 |
* |
| 188 |
* @since 2.0.0 |
| 189 |
* @access private |
| 190 |
*/ |
| 191 |
private function require_files() { |
| 192 |
require ELEMENTOR_PATH . 'includes/base/widget-base.php'; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Register widget type. |
| 197 |
* |
| 198 |
* Add a new widget type to the list of registered widget types. |
| 199 |
* |
| 200 |
* @since 1.0.0 |
| 201 |
* @access public |
| 202 |
* @deprecated 3.5.0 Use `$this->register()` instead. |
| 203 |
* |
| 204 |
* @param Widget_Base $widget Elementor widget. |
| 205 |
* |
| 206 |
* @return true True if the widget was registered. |
| 207 |
*/ |
| 208 |
public function register_widget_type( Widget_Base $widget ) { |
| 209 |
Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( |
| 210 |
__METHOD__, |
| 211 |
'3.5.0', |
| 212 |
'register' |
| 213 |
); |
| 214 |
|
| 215 |
return $this->register( $widget ); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Register a new widget type. |
| 220 |
* |
| 221 |
* @param \Elementor\Widget_Base $widget_instance Elementor Widget. |
| 222 |
* |
| 223 |
* @return true True if the widget was registered. |
| 224 |
* @since 3.5.0 |
| 225 |
* @access public |
| 226 |
* |
| 227 |
*/ |
| 228 |
public function register( Widget_Base $widget_instance ) { |
| 229 |
if ( is_null( $this->_widget_types ) ) { |
| 230 |
$this->init_widgets(); |
| 231 |
} |
| 232 |
|
| 233 |
$this->_widget_types[ $widget_instance->get_name() ] = $widget_instance; |
| 234 |
|
| 235 |
return true; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Unregister widget type. |
| 240 |
* |
| 241 |
* Removes widget type from the list of registered widget types. |
| 242 |
* |
| 243 |
* @since 1.0.0 |
| 244 |
* @access public |
| 245 |
* @deprecated 3.5.0 Use `$this->unregister()` instead. |
| 246 |
* |
| 247 |
* @param string $name Widget name. |
| 248 |
* |
| 249 |
* @return true True if the widget was unregistered, False otherwise. |
| 250 |
*/ |
| 251 |
public function unregister_widget_type( $name ) { |
| 252 |
Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( |
| 253 |
__METHOD__, |
| 254 |
'3.5.0', |
| 255 |
'unregister' |
| 256 |
); |
| 257 |
|
| 258 |
return $this->unregister( $name ); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Unregister widget type. |
| 263 |
* |
| 264 |
* Removes widget type from the list of registered widget types. |
| 265 |
* |
| 266 |
* @since 3.5.0 |
| 267 |
* @access public |
| 268 |
* |
| 269 |
* @param string $name Widget name. |
| 270 |
* |
| 271 |
* @return boolean Whether the widget was unregistered. |
| 272 |
*/ |
| 273 |
public function unregister( $name ) { |
| 274 |
if ( ! isset( $this->_widget_types[ $name ] ) ) { |
| 275 |
return false; |
| 276 |
} |
| 277 |
|
| 278 |
unset( $this->_widget_types[ $name ] ); |
| 279 |
|
| 280 |
return true; |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Get widget types. |
| 285 |
* |
| 286 |
* Retrieve the registered widget types list. |
| 287 |
* |
| 288 |
* @since 1.0.0 |
| 289 |
* @access public |
| 290 |
* |
| 291 |
* @param string $widget_name Optional. Widget name. Default is null. |
| 292 |
* |
| 293 |
* @return Widget_Base|Widget_Base[]|null Registered widget types. |
| 294 |
*/ |
| 295 |
public function get_widget_types( $widget_name = null ) { |
| 296 |
if ( is_null( $this->_widget_types ) ) { |
| 297 |
$this->init_widgets(); |
| 298 |
} |
| 299 |
|
| 300 |
if ( null !== $widget_name ) { |
| 301 |
return isset( $this->_widget_types[ $widget_name ] ) ? $this->_widget_types[ $widget_name ] : null; |
| 302 |
} |
| 303 |
|
| 304 |
return $this->_widget_types; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Get widget types config. |
| 309 |
* |
| 310 |
* Retrieve all the registered widgets with config for each widgets. |
| 311 |
* |
| 312 |
* @since 1.0.0 |
| 313 |
* @access public |
| 314 |
* |
| 315 |
* @return array Registered widget types with each widget config. |
| 316 |
*/ |
| 317 |
public function get_widget_types_config() { |
| 318 |
$config = []; |
| 319 |
|
| 320 |
foreach ( $this->get_widget_types() as $widget_key => $widget ) { |
| 321 |
$config[ $widget_key ] = $widget->get_config(); |
| 322 |
} |
| 323 |
|
| 324 |
return $config; |
| 325 |
} |
| 326 |
|
| 327 |
public function ajax_get_widget_types_controls_config( array $data ) { |
| 328 |
$config = []; |
| 329 |
|
| 330 |
foreach ( $this->get_widget_types() as $widget_key => $widget ) { |
| 331 |
if ( isset( $data['exclude'][ $widget_key ] ) ) { |
| 332 |
continue; |
| 333 |
} |
| 334 |
|
| 335 |
$config[ $widget_key ] = [ |
| 336 |
'controls' => $widget->get_stack( false )['controls'], |
| 337 |
'tabs_controls' => $widget->get_tabs_controls(), |
| 338 |
]; |
| 339 |
} |
| 340 |
|
| 341 |
return $config; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Ajax render widget. |
| 346 |
* |
| 347 |
* Ajax handler for Elementor render_widget. |
| 348 |
* |
| 349 |
* Fired by `wp_ajax_elementor_render_widget` action. |
| 350 |
* |
| 351 |
* @since 1.0.0 |
| 352 |
* @access public |
| 353 |
* |
| 354 |
* @throws \Exception If current user don't have permissions to edit the post. |
| 355 |
* |
| 356 |
* @param array $request Ajax request. |
| 357 |
* |
| 358 |
* @return array { |
| 359 |
* Rendered widget. |
| 360 |
* |
| 361 |
* @type string $render The rendered HTML. |
| 362 |
* } |
| 363 |
*/ |
| 364 |
public function ajax_render_widget( $request ) { |
| 365 |
$document = Plugin::$instance->documents->get( $request['editor_post_id'] ); |
| 366 |
|
| 367 |
if ( ! $document->is_editable_by_current_user() ) { |
| 368 |
throw new \Exception( 'Access denied.', Exceptions::FORBIDDEN ); |
| 369 |
} |
| 370 |
|
| 371 |
// Override the global $post for the render. |
| 372 |
query_posts( |
| 373 |
[ |
| 374 |
'p' => $request['editor_post_id'], |
| 375 |
'post_type' => 'any', |
| 376 |
] |
| 377 |
); |
| 378 |
|
| 379 |
$editor = Plugin::$instance->editor; |
| 380 |
$is_edit_mode = $editor->is_edit_mode(); |
| 381 |
$editor->set_edit_mode( true ); |
| 382 |
|
| 383 |
Plugin::$instance->documents->switch_to_document( $document ); |
| 384 |
|
| 385 |
$render_html = $document->render_element( $request['data'] ); |
| 386 |
|
| 387 |
$editor->set_edit_mode( $is_edit_mode ); |
| 388 |
|
| 389 |
return [ |
| 390 |
'render' => $render_html, |
| 391 |
]; |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Ajax get WordPress widget form. |
| 396 |
* |
| 397 |
* Ajax handler for Elementor editor get_wp_widget_form. |
| 398 |
* |
| 399 |
* Fired by `wp_ajax_elementor_editor_get_wp_widget_form` action. |
| 400 |
* |
| 401 |
* @since 1.0.0 |
| 402 |
* @access public |
| 403 |
* |
| 404 |
* @param array $request Ajax request. |
| 405 |
* |
| 406 |
* @return bool|string Rendered widget form. |
| 407 |
*/ |
| 408 |
public function ajax_get_wp_widget_form( $request ) { |
| 409 |
if ( empty( $request['widget_type'] ) ) { |
| 410 |
return false; |
| 411 |
} |
| 412 |
|
| 413 |
if ( empty( $request['data'] ) ) { |
| 414 |
$request['data'] = []; |
| 415 |
} |
| 416 |
|
| 417 |
$element_data = [ |
| 418 |
'id' => $request['id'], |
| 419 |
'elType' => 'widget', |
| 420 |
'widgetType' => $request['widget_type'], |
| 421 |
'settings' => $request['data'], |
| 422 |
]; |
| 423 |
|
| 424 |
/** |
| 425 |
* @var $widget_obj Widget_WordPress |
| 426 |
*/ |
| 427 |
$widget_obj = Plugin::$instance->elements_manager->create_element_instance( $element_data ); |
| 428 |
|
| 429 |
if ( ! $widget_obj ) { |
| 430 |
return false; |
| 431 |
} |
| 432 |
|
| 433 |
return $widget_obj->get_form(); |
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
* Render widgets content. |
| 438 |
* |
| 439 |
* Used to generate the widget templates on the editor using Underscore JS |
| 440 |
* template, for all the registered widget types. |
| 441 |
* |
| 442 |
* @since 1.0.0 |
| 443 |
* @access public |
| 444 |
*/ |
| 445 |
public function render_widgets_content() { |
| 446 |
foreach ( $this->get_widget_types() as $widget ) { |
| 447 |
$widget->print_template(); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* Get widgets frontend settings keys. |
| 453 |
* |
| 454 |
* Retrieve frontend controls settings keys for all the registered widget |
| 455 |
* types. |
| 456 |
* |
| 457 |
* @since 1.3.0 |
| 458 |
* @access public |
| 459 |
* |
| 460 |
* @return array Registered widget types with settings keys for each widget. |
| 461 |
*/ |
| 462 |
public function get_widgets_frontend_settings_keys() { |
| 463 |
$keys = []; |
| 464 |
|
| 465 |
foreach ( $this->get_widget_types() as $widget_type_name => $widget_type ) { |
| 466 |
$widget_type_keys = $widget_type->get_frontend_settings_keys(); |
| 467 |
|
| 468 |
if ( $widget_type_keys ) { |
| 469 |
$keys[ $widget_type_name ] = $widget_type_keys; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
return $keys; |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* Enqueue widgets scripts. |
| 478 |
* |
| 479 |
* Enqueue all the scripts defined as a dependency for each widget. |
| 480 |
* |
| 481 |
* @since 1.3.0 |
| 482 |
* @access public |
| 483 |
*/ |
| 484 |
public function enqueue_widgets_scripts() { |
| 485 |
foreach ( $this->get_widget_types() as $widget ) { |
| 486 |
$widget->enqueue_scripts(); |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 491 |
* Enqueue widgets styles |
| 492 |
* |
| 493 |
* Enqueue all the styles defined as a dependency for each widget |
| 494 |
* |
| 495 |
* @access public |
| 496 |
*/ |
| 497 |
public function enqueue_widgets_styles() { |
| 498 |
foreach ( $this->get_widget_types() as $widget ) { |
| 499 |
$widget->enqueue_styles(); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
/** |
| 504 |
* Retrieve inline editing configuration. |
| 505 |
* |
| 506 |
* Returns general inline editing configurations like toolbar types etc. |
| 507 |
* |
| 508 |
* @access public |
| 509 |
* @since 1.8.0 |
| 510 |
* |
| 511 |
* @return array { |
| 512 |
* Inline editing configuration. |
| 513 |
* |
| 514 |
* @type array $toolbar { |
| 515 |
* Toolbar types and the actions each toolbar includes. |
| 516 |
* Note: Wysiwyg controls uses the advanced toolbar, textarea controls |
| 517 |
* uses the basic toolbar and text controls has no toolbar. |
| 518 |
* |
| 519 |
* @type array $basic Basic actions included in the edit tool. |
| 520 |
* @type array $advanced Advanced actions included in the edit tool. |
| 521 |
* } |
| 522 |
* } |
| 523 |
*/ |
| 524 |
public function get_inline_editing_config() { |
| 525 |
$basic_tools = [ |
| 526 |
'bold', |
| 527 |
'underline', |
| 528 |
'italic', |
| 529 |
]; |
| 530 |
|
| 531 |
$advanced_tools = array_merge( $basic_tools, [ |
| 532 |
'createlink', |
| 533 |
'unlink', |
| 534 |
'h1' => [ |
| 535 |
'h1', |
| 536 |
'h2', |
| 537 |
'h3', |
| 538 |
'h4', |
| 539 |
'h5', |
| 540 |
'h6', |
| 541 |
'p', |
| 542 |
'blockquote', |
| 543 |
'pre', |
| 544 |
], |
| 545 |
'list' => [ |
| 546 |
'insertOrderedList', |
| 547 |
'insertUnorderedList', |
| 548 |
], |
| 549 |
] ); |
| 550 |
|
| 551 |
return [ |
| 552 |
'toolbar' => [ |
| 553 |
'basic' => $basic_tools, |
| 554 |
'advanced' => $advanced_tools, |
| 555 |
], |
| 556 |
]; |
| 557 |
} |
| 558 |
|
| 559 |
/** |
| 560 |
* Widgets manager constructor. |
| 561 |
* |
| 562 |
* Initializing Elementor widgets manager. |
| 563 |
* |
| 564 |
* @since 1.0.0 |
| 565 |
* @access public |
| 566 |
*/ |
| 567 |
public function __construct() { |
| 568 |
$this->require_files(); |
| 569 |
|
| 570 |
add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] ); |
| 571 |
} |
| 572 |
|
| 573 |
/** |
| 574 |
* Register ajax actions. |
| 575 |
* |
| 576 |
* Add new actions to handle data after an ajax requests returned. |
| 577 |
* |
| 578 |
* @since 2.0.0 |
| 579 |
* @access public |
| 580 |
* |
| 581 |
* @param Ajax $ajax_manager |
| 582 |
*/ |
| 583 |
public function register_ajax_actions( Ajax $ajax_manager ) { |
| 584 |
$ajax_manager->register_ajax_action( 'render_widget', [ $this, 'ajax_render_widget' ] ); |
| 585 |
$ajax_manager->register_ajax_action( 'editor_get_wp_widget_form', [ $this, 'ajax_get_wp_widget_form' ] ); |
| 586 |
$ajax_manager->register_ajax_action( 'get_widgets_config', [ $this, 'ajax_get_widget_types_controls_config' ] ); |
| 587 |
} |
| 588 |
} |
| 589 |
|