PluginProbe
Elementor Website Builder – more than just a page builder / 3.25.7
Elementor Website Builder – more than just a page builder v3.25.7
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 / core / editor / editor.php

editor.php in Elementor Website Builder – more than just a page builder 3.25.7, at core/editor/editor.php

653 lines 15.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Editor;
3
4 use Elementor\Core\Breakpoints\Manager as Breakpoints_Manager;
5 use Elementor\Core\Common\Modules\Ajax\Module;
6 use Elementor\Core\Debug\Loading_Inspection_Manager;
7 use Elementor\Core\Editor\Loader\Editor_Loader_Factory;
8 use Elementor\Core\Editor\Loader\Editor_Loader_Interface;
9 use Elementor\Core\Experiments\Manager as Experiments_Manager;
10 use Elementor\Core\Settings\Manager as SettingsManager;
11 use Elementor\Plugin;
12 use Elementor\TemplateLibrary\Source_Local;
13 use Elementor\Utils;
14 use Elementor\Core\Editor\Data;
15 use Elementor\Modules\EditorAppBar\Module as App_Bar_Module;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit; // Exit if accessed directly.
19 }
20
21 /**
22 * Elementor editor.
23 *
24 * Elementor editor handler class is responsible for initializing Elementor
25 * editor and register all the actions needed to display the editor.
26 *
27 * @since 1.0.0
28 */
29 class Editor {
30
31 /**
32 * User capability required to access Elementor editor.
33 */
34 const EDITING_CAPABILITY = 'edit_posts';
35
36 /**
37 * The const is deprecated, it remains here for backward compatibility.
38 *
39 * @deprecated Use App_Bar_Module::EXPERIMENT_NAME instead
40 */
41 const EDITOR_V2_EXPERIMENT_NAME = App_Bar_Module::EXPERIMENT_NAME;
42
43 /**
44 * Post ID.
45 *
46 * Holds the ID of the current post being edited.
47 *
48 * @since 1.0.0
49 * @access private
50 *
51 * @var int Post ID.
52 */
53 private $post_id;
54
55 /**
56 * Whether the edit mode is active.
57 *
58 * Used to determine whether we are in edit mode.
59 *
60 * @since 1.0.0
61 * @access private
62 *
63 * @var bool Whether the edit mode is active.
64 */
65 private $is_edit_mode;
66
67 /**
68 * @var Notice_Bar
69 */
70 public $notice_bar;
71
72 /**
73 * @var Promotion
74 */
75 public $promotion;
76
77 /**
78 * @var Editor_Loader_Interface
79 */
80 private $loader;
81
82 /**
83 * Init.
84 *
85 * Initialize Elementor editor. Registers all needed actions to run Elementor,
86 * removes conflicting actions etc.
87 *
88 * Fired by `admin_action_elementor` action.
89 *
90 * @since 1.0.0
91 * @access public
92 *
93 * @param bool $die Optional. Whether to die at the end. Default is `true`.
94 */
95 public function init( $die = true ) {
96 if ( empty( $_REQUEST['post'] ) ) {
97 return;
98 }
99
100 $this->set_post_id( absint( $_REQUEST['post'] ) );
101
102 if ( ! $this->is_edit_mode( $this->post_id ) ) {
103 return;
104 }
105
106 // BC: From 2.9.0, the editor shouldn't handle the global post / current document.
107 // Use requested id and not the global in order to avoid conflicts with plugins that changes the global post.
108 query_posts( [
109 'p' => $this->post_id,
110 'post_type' => get_post_type( $this->post_id ),
111 ] );
112
113 Plugin::$instance->db->switch_to_post( $this->post_id );
114
115 $document = Plugin::$instance->documents->get( $this->post_id );
116
117 Plugin::$instance->documents->switch_to_document( $document );
118
119 // Change mode to Builder
120 $document->set_is_built_with_elementor( true );
121
122 // End BC.
123
124 Loading_Inspection_Manager::instance()->register_inspections();
125
126 // Send MIME Type header like WP admin-header.
127 @header( 'Content-Type: ' . get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' ) );
128
129 add_filter( 'show_admin_bar', '__return_false' );
130
131 // Remove all WordPress actions
132 remove_all_actions( 'wp_head' );
133 remove_all_actions( 'wp_print_styles' );
134 remove_all_actions( 'wp_print_head_scripts' );
135 remove_all_actions( 'wp_footer' );
136
137 // Handle `wp_head`
138 add_action( 'wp_head', 'wp_enqueue_scripts', 1 );
139 add_action( 'wp_head', 'wp_print_styles', 8 );
140 add_action( 'wp_head', 'wp_print_head_scripts', 9 );
141 add_action( 'wp_head', 'wp_site_icon' );
142 add_action( 'wp_head', [ $this, 'editor_head_trigger' ], 30 );
143
144 // Handle `wp_footer`
145 add_action( 'wp_footer', 'wp_print_footer_scripts', 20 );
146 add_action( 'wp_footer', 'wp_auth_check_html', 30 );
147 add_action( 'wp_footer', [ $this, 'wp_footer' ] );
148
149 // Handle `wp_enqueue_scripts`
150 remove_all_actions( 'wp_enqueue_scripts' );
151
152 // Also remove all scripts hooked into after_wp_tiny_mce.
153 remove_all_actions( 'after_wp_tiny_mce' );
154
155 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ], 999999 );
156 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ], 999999 );
157
158 // Setup default heartbeat options
159 add_filter( 'heartbeat_settings', function( $settings ) {
160 $settings['interval'] = 15;
161 return $settings;
162 } );
163
164 // Tell to WP Cache plugins do not cache this request.
165 Utils::do_not_cache();
166
167 do_action( 'elementor/editor/init' );
168
169 $this->get_loader()->print_root_template();
170
171 // From the action it's an empty string, from tests its `false`
172 if ( false !== $die ) {
173 die;
174 }
175 }
176
177 /**
178 * Retrieve post ID.
179 *
180 * Get the ID of the current post.
181 *
182 * @since 1.8.0
183 * @access public
184 *
185 * @return int Post ID.
186 */
187 public function get_post_id() {
188 return $this->post_id;
189 }
190
191 /**
192 * Redirect to new URL.
193 *
194 * Used as a fallback function for the old URL structure of Elementor page
195 * edit URL.
196 *
197 * Fired by `template_redirect` action.
198 *
199 * @since 1.6.0
200 * @access public
201 */
202 public function redirect_to_new_url() {
203 if ( ! isset( $_GET['elementor'] ) ) {
204 return;
205 }
206
207 $document = Plugin::$instance->documents->get( get_the_ID() );
208
209 if ( ! $document ) {
210 wp_die( esc_html__( 'Document not found.', 'elementor' ) );
211 }
212
213 if ( ! $document->is_editable_by_current_user() || ! $document->is_built_with_elementor() ) {
214 return;
215 }
216
217 wp_safe_redirect( $document->get_edit_url() );
218 die;
219 }
220
221 /**
222 * Whether the edit mode is active.
223 *
224 * Used to determine whether we are in the edit mode.
225 *
226 * @since 1.0.0
227 * @access public
228 *
229 * @param int $post_id Optional. Post ID. Default is `null`, the current
230 * post ID.
231 *
232 * @return bool Whether the edit mode is active.
233 */
234 public function is_edit_mode( $post_id = null ) {
235 if ( null !== $this->is_edit_mode ) {
236 return $this->is_edit_mode;
237 }
238
239 if ( empty( $post_id ) ) {
240 $post_id = $this->post_id;
241 }
242
243 $document = Plugin::$instance->documents->get( $post_id );
244
245 if ( ! $document || ! $document->is_editable_by_current_user() ) {
246 return false;
247 }
248
249 /** @var Module ajax */
250 $ajax_data = Plugin::$instance->common->get_component( 'ajax' )->get_current_action_data();
251
252 if ( ! empty( $ajax_data ) && 'get_document_config' === $ajax_data['action'] ) {
253 return true;
254 }
255
256 // Ajax request as Editor mode
257 $actions = [
258 'elementor',
259
260 // Templates
261 'elementor_get_templates',
262 'elementor_save_template',
263 'elementor_get_template',
264 'elementor_delete_template',
265 'elementor_import_template',
266 'elementor_library_direct_actions',
267 ];
268
269 if ( isset( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], $actions ) ) {
270 return true;
271 }
272
273 return false;
274 }
275
276 /**
277 * Lock post.
278 *
279 * Mark the post as currently being edited by the current user.
280 *
281 * @since 1.0.0
282 * @access public
283 *
284 * @param int $post_id The ID of the post being edited.
285 */
286 public function lock_post( $post_id ) {
287 if ( ! function_exists( 'wp_set_post_lock' ) ) {
288 require_once ABSPATH . 'wp-admin/includes/post.php';
289 }
290
291 wp_set_post_lock( $post_id );
292 }
293
294 /**
295 * Get locked user.
296 *
297 * Check what user is currently editing the post.
298 *
299 * @since 1.0.0
300 * @access public
301 *
302 * @param int $post_id The ID of the post being edited.
303 *
304 * @return \WP_User|false User information or false if the post is not locked.
305 */
306 public function get_locked_user( $post_id ) {
307 if ( ! function_exists( 'wp_check_post_lock' ) ) {
308 require_once ABSPATH . 'wp-admin/includes/post.php';
309 }
310
311 $locked_user = wp_check_post_lock( $post_id );
312 if ( ! $locked_user ) {
313 return false;
314 }
315
316 return get_user_by( 'id', $locked_user );
317 }
318
319 /**
320 * NOTICE: This method not in use, it's here for backward compatibility.
321 *
322 * Print Editor Template.
323 *
324 * Include the wrapper template of the editor.
325 *
326 * @since 2.2.0
327 * @access public
328 */
329 public function print_editor_template() {
330 include ELEMENTOR_PATH . 'includes/editor-templates/editor-wrapper.php';
331 }
332
333 /**
334 * Enqueue scripts.
335 *
336 * Registers all the editor scripts and enqueues them.
337 *
338 * @since 1.0.0
339 * @access public
340 */
341 public function enqueue_scripts() {
342 remove_action( 'wp_enqueue_scripts', [ $this, __FUNCTION__ ], 999999 );
343
344 global $wp_styles, $wp_scripts;
345
346 // Reset global variable
347 $wp_styles = new \WP_Styles(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
348 $wp_scripts = new \WP_Scripts(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
349
350 $this->get_loader()->register_scripts();
351
352 /**
353 * Before editor enqueue scripts.
354 *
355 * Fires before Elementor editor scripts are enqueued.
356 *
357 * @since 1.0.0
358 */
359 do_action( 'elementor/editor/before_enqueue_scripts' );
360
361 // Tweak for WP Admin menu icons
362 wp_print_styles( 'editor-buttons' );
363
364 $this->get_loader()->enqueue_scripts();
365
366 Plugin::$instance->controls_manager->enqueue_control_scripts();
367
368 /**
369 * After editor enqueue scripts.
370 *
371 * Fires after Elementor editor scripts are enqueued.
372 *
373 * @since 1.0.0
374 */
375 do_action( 'elementor/editor/after_enqueue_scripts' );
376 }
377
378 /**
379 * Enqueue styles.
380 *
381 * Registers all the editor styles and enqueues them.
382 *
383 * @since 1.0.0
384 * @access public
385 */
386 public function enqueue_styles() {
387 /**
388 * Before editor enqueue styles.
389 *
390 * Fires before Elementor editor styles are enqueued.
391 *
392 * @since 1.0.0
393 */
394 do_action( 'elementor/editor/before_enqueue_styles' );
395
396 $this->get_loader()->register_styles();
397 $this->get_loader()->enqueue_styles();
398
399 $this->enqueue_theme_ui_styles();
400
401 $breakpoints = Plugin::$instance->breakpoints->get_breakpoints();
402
403 // The two breakpoints under 'tablet' need to be checked for values.
404 if ( $breakpoints[ Breakpoints_Manager::BREAKPOINT_KEY_MOBILE ]->is_custom() || $breakpoints[ Breakpoints_Manager::BREAKPOINT_KEY_MOBILE_EXTRA ]->is_enabled() ) {
405 wp_add_inline_style(
406 'elementor-editor',
407 '.elementor-device-tablet #elementor-preview-responsive-wrapper { width: ' . Plugin::$instance->breakpoints->get_device_min_breakpoint( Breakpoints_Manager::BREAKPOINT_KEY_TABLET ) . 'px; }'
408 );
409 }
410
411 /**
412 * After editor enqueue styles.
413 *
414 * Fires after Elementor editor styles are enqueued.
415 *
416 * @since 1.0.0
417 */
418 do_action( 'elementor/editor/after_enqueue_styles' );
419 }
420
421 private function enqueue_theme_ui_styles() {
422 $ui_theme_selected = SettingsManager::get_settings_managers( 'editorPreferences' )->get_model()->get_settings( 'ui_theme' );
423
424 $ui_themes = [
425 'light',
426 'dark',
427 ];
428
429 if ( 'auto' === $ui_theme_selected || ! in_array( $ui_theme_selected, $ui_themes, true ) ) {
430 $ui_light_theme_media_queries = '(prefers-color-scheme: light)';
431 $ui_dark_theme_media_queries = '(prefers-color-scheme: dark)';
432 } else {
433 $ui_light_theme_media_queries = 'none';
434 $ui_dark_theme_media_queries = 'none';
435
436 if ( 'light' === $ui_theme_selected ) {
437 $ui_light_theme_media_queries = 'all';
438 } elseif ( 'dark' === $ui_theme_selected ) {
439 $ui_dark_theme_media_queries = 'all';
440 }
441 }
442
443 $this->enqueue_theme_ui( 'light', $ui_light_theme_media_queries );
444 $this->enqueue_theme_ui( 'dark', $ui_dark_theme_media_queries );
445 }
446
447 private function enqueue_theme_ui( $ui_theme, $ui_theme_media_queries = 'all' ) {
448 $suffix = Utils::is_script_debug() ? '' : '.min';
449
450 wp_enqueue_style(
451 'e-theme-ui-' . $ui_theme,
452 ELEMENTOR_ASSETS_URL . 'css/theme-' . $ui_theme . $suffix . '.css',
453 [],
454 ELEMENTOR_VERSION,
455 $ui_theme_media_queries
456 );
457 }
458
459 /**
460 * Editor head trigger.
461 *
462 * Fires the 'elementor/editor/wp_head' action in the head tag in Elementor
463 * editor.
464 *
465 * @since 1.0.0
466 * @access public
467 */
468 public function editor_head_trigger() {
469 /**
470 * Elementor editor head.
471 *
472 * Fires on Elementor editor head tag.
473 *
474 * Used to prints scripts or any other data in the head tag.
475 *
476 * @since 1.0.0
477 */
478 do_action( 'elementor/editor/wp_head' );
479 }
480
481 /**
482 * WP footer.
483 *
484 * Prints Elementor editor with all the editor templates, and render controls,
485 * widgets and content elements.
486 *
487 * Fired by `wp_footer` action.
488 *
489 * @since 1.0.0
490 * @access public
491 */
492 public function wp_footer() {
493 $plugin = Plugin::$instance;
494
495 $plugin->controls_manager->render_controls();
496 $plugin->widgets_manager->render_widgets_content();
497 $plugin->elements_manager->render_elements_content();
498
499 $plugin->dynamic_tags->print_templates();
500
501 $this->get_loader()->register_additional_templates();
502
503 /**
504 * Elementor editor footer.
505 *
506 * Fires on Elementor editor before closing the body tag.
507 *
508 * Used to prints scripts or any other HTML before closing the body tag.
509 *
510 * @since 1.0.0
511 */
512 do_action( 'elementor/editor/footer' );
513 }
514
515 /**
516 * Set edit mode.
517 *
518 * Used to update the edit mode.
519 *
520 * @since 1.0.0
521 * @access public
522 *
523 * @param bool $edit_mode Whether the edit mode is active.
524 */
525 public function set_edit_mode( $edit_mode ) {
526 $this->is_edit_mode = $edit_mode;
527 }
528
529 /**
530 * Editor constructor.
531 *
532 * Initializing Elementor editor and redirect from old URL structure of
533 * Elementor editor.
534 *
535 * @since 1.0.0
536 * @access public
537 */
538 public function __construct() {
539 Plugin::$instance->data_manager_v2->register_controller( new Data\Globals\Controller() );
540
541 $this->notice_bar = new Notice_Bar();
542 $this->promotion = new Promotion();
543
544 add_action( 'admin_action_elementor', [ $this, 'init' ] );
545 add_action( 'template_redirect', [ $this, 'redirect_to_new_url' ] );
546
547 // Handle autocomplete feature for URL control.
548 add_filter( 'wp_link_query_args', [ $this, 'filter_wp_link_query_args' ] );
549 add_filter( 'wp_link_query', [ $this, 'filter_wp_link_query' ] );
550 }
551
552 /**
553 * @since 2.2.0
554 * @access public
555 */
556 public function filter_wp_link_query_args( $query ) {
557 $library_cpt_key = array_search( Source_Local::CPT, $query['post_type'], true );
558 if ( false !== $library_cpt_key ) {
559 unset( $query['post_type'][ $library_cpt_key ] );
560 }
561
562 return $query;
563 }
564
565 /**
566 * @since 2.2.0
567 * @access public
568 */
569 public function filter_wp_link_query( $results ) {
570
571 // PHPCS - The user data is not used.
572 if ( isset( $_POST['editor'] ) && 'elementor' === $_POST['editor'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
573 $post_type_object = get_post_type_object( 'post' );
574 $post_label = $post_type_object->labels->singular_name;
575
576 foreach ( $results as & $result ) {
577 if ( 'post' === get_post_type( $result['ID'] ) ) {
578 $result['info'] = $post_label;
579 }
580 }
581 }
582
583 return $results;
584 }
585
586 public function set_post_id( $post_id ) {
587 $this->post_id = $post_id;
588 }
589
590 /**
591 * Get loader.
592 *
593 * @return Editor_Loader_Interface
594 */
595 private function get_loader() {
596 if ( ! $this->loader ) {
597 $this->loader = Editor_Loader_Factory::create();
598
599 $this->loader->init();
600 }
601
602 return $this->loader;
603 }
604
605 /**
606 * Get elements presets.
607 *
608 * @return array
609 */
610 public function get_elements_presets() {
611 $element_types = Plugin::$instance->elements_manager->get_element_types();
612 $presets = [];
613
614 foreach ( $element_types as $el_type => $element ) {
615 $this->check_element_for_presets( $element, $el_type, $presets );
616 }
617
618 return $presets;
619 }
620
621 /**
622 * @return void
623 */
624 private function check_element_for_presets( $element, $el_type, &$presets ) {
625 $element_presets = $element->get_panel_presets();
626
627 if ( empty( $element_presets ) ) {
628 return;
629 }
630
631 foreach ( $element_presets as $key => $preset ) {
632 $this->maybe_add_preset( $el_type, $preset, $key, $presets );
633 }
634 }
635
636 /**
637 * @return void
638 */
639 private function maybe_add_preset( $el_type, $preset, $key, &$presets ) {
640 if ( $this->is_valid_preset( $el_type, $preset ) ) {
641 $presets[ $key ] = $preset;
642 }
643 }
644
645 /**
646 * @return boolean
647 */
648 private function is_valid_preset( $el_type, $preset ) {
649 return isset( $preset['replacements']['custom']['originalWidget'] )
650 && $el_type === $preset['replacements']['custom']['originalWidget'];
651 }
652 }
653