PluginProbe
Elementor Website Builder – more than just a page builder / 3.13.0-beta1
Elementor Website Builder – more than just a page builder v3.13.0-beta1
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.13.0-beta1, at core/editor/editor.php

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