PluginProbe
Elementor Website Builder – more than just a page builder / 3.17.1
Elementor Website Builder – more than just a page builder v3.17.1
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / managers / widgets.php

widgets.php in Elementor Website Builder – more than just a page builder 3.17.1, at includes/managers/widgets.php

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