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 / modules / safe-mode / module.php

module.php in Elementor Website Builder – more than just a page builder 3.17.1, at modules/safe-mode/module.php

563 lines 16.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\SafeMode;
3
4 use Elementor\Plugin;
5 use Elementor\Settings;
6 use Elementor\Tools;
7 use Elementor\TemplateLibrary\Source_Local;
8 use Elementor\Core\Common\Modules\Ajax\Module as Ajax;
9 use Elementor\Utils;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly
13 }
14
15 class Module extends \Elementor\Core\Base\Module {
16
17 const OPTION_ENABLED = 'elementor_safe_mode';
18 const OPTION_TOKEN = self::OPTION_ENABLED . '_token';
19 const MU_PLUGIN_FILE_NAME = 'elementor-safe-mode.php';
20 const DOCS_HELPED_URL = 'https://go.elementor.com/safe-mode-helped/';
21 const DOCS_DIDNT_HELP_URL = 'https://go.elementor.com/safe-mode-didnt-helped/';
22 const DOCS_MU_PLUGINS_URL = 'https://go.elementor.com/safe-mode-mu-plugins/';
23 const DOCS_TRY_SAFE_MODE_URL = 'https://go.elementor.com/safe-mode/';
24
25 const EDITOR_NOTICE_TIMEOUT = 30000; /* ms */
26
27 public function get_name() {
28 return 'safe-mode';
29 }
30
31 public function register_ajax_actions( Ajax $ajax ) {
32 $ajax->register_ajax_action( 'enable_safe_mode', [ $this, 'ajax_enable_safe_mode' ] );
33 $ajax->register_ajax_action( 'disable_safe_mode', [ $this, 'disable_safe_mode' ] );
34 }
35
36 /**
37 * @param Tools $tools_page
38 */
39 public function add_admin_button( $tools_page ) {
40 $tools_page->add_fields( Settings::TAB_GENERAL, 'tools', [
41 'safe_mode' => [
42 'label' => esc_html__( 'Safe Mode', 'elementor' ),
43 'field_args' => [
44 'type' => 'select',
45 'std' => $this->is_enabled() ? 'global' : '',
46 'options' => [
47 '' => esc_html__( 'Disable', 'elementor' ),
48 'global' => esc_html__( 'Enable', 'elementor' ),
49
50 ],
51 'desc' => esc_html__( 'Safe Mode allows you to troubleshoot issues by only loading the editor, without loading the theme or any other plugin.', 'elementor' ),
52 ],
53 ],
54 ] );
55 }
56
57 public function on_update_safe_mode( $value ) {
58 if ( 'yes' === $value || 'global' === $value ) {
59 $this->enable_safe_mode();
60 } else {
61 $this->disable_safe_mode();
62 }
63
64 return $value;
65 }
66
67 /**
68 * @throws \Exception
69 */
70 public function ajax_enable_safe_mode( $data ) {
71 if ( ! current_user_can( 'install_plugins' ) ) {
72 throw new \Exception( 'Access denied.' );
73 }
74
75 // It will run `$this->>update_safe_mode`.
76 update_option( 'elementor_safe_mode', 'yes' );
77
78 $document = Plugin::$instance->documents->get( $data['editor_post_id'] );
79
80 if ( $document ) {
81 return add_query_arg( 'elementor-mode', 'safe', $document->get_edit_url() );
82 }
83
84 return false;
85 }
86
87 public function enable_safe_mode() {
88 if ( ! current_user_can( 'install_plugins' ) ) {
89 return;
90 }
91
92 WP_Filesystem();
93
94 $this->update_allowed_plugins();
95
96 if ( ! is_dir( WPMU_PLUGIN_DIR ) ) {
97 wp_mkdir_p( WPMU_PLUGIN_DIR );
98 add_option( 'elementor_safe_mode_created_mu_dir', true );
99 }
100
101 if ( ! is_dir( WPMU_PLUGIN_DIR ) ) {
102 wp_die( esc_html__( 'Cannot enable Safe Mode', 'elementor' ) );
103 }
104
105 $results = copy_dir( __DIR__ . '/mu-plugin/', WPMU_PLUGIN_DIR );
106
107 if ( is_wp_error( $results ) ) {
108 return;
109 }
110
111 $token = hash( 'sha256', wp_rand() );
112
113 // Only who own this key can use 'elementor-safe-mode'.
114 update_option( self::OPTION_TOKEN, $token );
115
116 // Save for later use.
117 setcookie( self::OPTION_TOKEN, $token, time() + HOUR_IN_SECONDS, COOKIEPATH, '', is_ssl(), true );
118 }
119
120 public function disable_safe_mode() {
121 if ( ! current_user_can( 'install_plugins' ) ) {
122 return;
123 }
124
125 $file_path = WP_CONTENT_DIR . '/mu-plugins/elementor-safe-mode.php';
126 if ( file_exists( $file_path ) ) {
127 unlink( $file_path );
128 }
129
130 if ( get_option( 'elementor_safe_mode_created_mu_dir' ) ) {
131 // It will be removed only if it's empty and don't have other mu-plugins.
132 @rmdir( WPMU_PLUGIN_DIR );
133 }
134
135 delete_option( 'elementor_safe_mode' );
136 delete_option( 'elementor_safe_mode_allowed_plugins' );
137 delete_option( 'theme_mods_elementor-safe' );
138 delete_option( 'elementor_safe_mode_created_mu_dir' );
139
140 delete_option( self::OPTION_TOKEN );
141 setcookie( self::OPTION_TOKEN, '', 1, '', '', is_ssl(), true );
142 }
143
144 public function filter_preview_url( $url ) {
145 return add_query_arg( 'elementor-mode', 'safe', $url );
146 }
147
148 public function filter_template() {
149 return ELEMENTOR_PATH . 'modules/page-templates/templates/canvas.php';
150 }
151
152 public function print_safe_mode_css() {
153 ?>
154 <style>
155 .elementor-safe-mode-toast {
156 position: absolute;
157 z-index: 10000; /* Over the loading layer */
158 bottom: 10px;
159 width: 400px;
160 line-height: 30px;
161 color: var(--e-a-color-txt);
162 background: var(--e-a-bg-default);
163 padding: 20px 25px 25px;
164 box-shadow: 0 5px 20px rgba(0, 0, 0, 0.15);
165 border-radius: 5px;
166 font-family: var(--e-a-font-family);
167 }
168
169 body.rtl .elementor-safe-mode-toast {
170 left: 10px;
171 }
172
173 body:not(.rtl) .elementor-safe-mode-toast {
174 right: 10px;
175 }
176
177 #elementor-try-safe-mode {
178 display: none;
179 }
180
181 .elementor-safe-mode-toast .elementor-toast-content {
182 font-size: 13px;
183 line-height: 22px;
184 }
185
186 .elementor-safe-mode-toast .elementor-toast-content a {
187 color: var(--e-a-color-info);
188 }
189
190 .elementor-safe-mode-toast .elementor-toast-content hr {
191 margin: 15px auto;
192 border: 0 none;
193 border-block-start: var(--e-a-border);
194 }
195
196 .elementor-safe-mode-toast header {
197 display: flex;
198 align-items: center;
199 justify-content: space-between;
200 flex-wrap: wrap;
201 margin-block-end: 20px;
202 }
203
204 .elementor-safe-mode-toast header > * {
205 margin-block-start: 10px;
206 }
207
208 .elementor-safe-mode-toast header i {
209 font-size: 25px;
210 color: var(--e-a-color-warning);
211 }
212
213 .elementor-safe-mode-toast header i {
214 margin-inline-end: 10px;
215 }
216
217 .elementor-safe-mode-toast header h2 {
218 flex-grow: 1;
219 font-size: 18px;
220 }
221
222 .elementor-safe-mode-list-item {
223 margin-block-start: 10px;
224 list-style: outside;
225 }
226
227 .elementor-safe-mode-list-item {
228 margin-inline-start: 15px;
229 }
230
231 .elementor-safe-mode-list-item b {
232 font-size: 14px;
233 }
234
235 .elementor-safe-mode-list-item-content {
236 font-style: italic;
237 color: var(--e-a-color-txt);
238 }
239
240 .elementor-safe-mode-list-item-title {
241 font-weight: 500;
242 }
243
244 .elementor-safe-mode-mu-plugins {
245 background-color: var(--e-a-bg-hover);
246 color: var(--e-a-color-txt-hover);
247 margin-block-start: 20px;
248 padding: 10px 15px;
249 }
250 </style>
251 <?php
252 }
253
254 public function print_safe_mode_notice() {
255 $this->print_safe_mode_css()
256 ?>
257 <div class="elementor-safe-mode-toast" id="elementor-safe-mode-message">
258 <header>
259 <i class="eicon-warning"></i>
260 <h2><?php echo esc_html__( 'Safe Mode ON', 'elementor' ); ?></h2>
261 <a class="elementor-button elementor-safe-mode-button elementor-disable-safe-mode" target="_blank" href="<?php echo esc_url( $this->get_admin_page_url() ); ?>">
262 <?php echo esc_html__( 'Disable Safe Mode', 'elementor' ); ?>
263 </a>
264 </header>
265
266 <div class="elementor-toast-content">
267 <ul class="elementor-safe-mode-list">
268 <li class="elementor-safe-mode-list-item">
269 <div class="elementor-safe-mode-list-item-title"><?php echo esc_html__( 'Editor successfully loaded?', 'elementor' ); ?></div>
270 <div class="elementor-safe-mode-list-item-content">
271 <?php
272 echo esc_html__( 'The issue was probably caused by one of your plugins or theme.', 'elementor' );
273 echo ' ';
274
275 printf(
276 /* translators: %1$s Link open tag, %2$s: Link close tag. */
277 esc_html__( '%1$sClick here%2$s to troubleshoot', 'elementor' ),
278 '<a href="' . self::DOCS_HELPED_URL . '" target="_blank">', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
279 '</a>'
280 );
281 ?>
282 </div>
283 </li>
284 <li class="elementor-safe-mode-list-item">
285 <div class="elementor-safe-mode-list-item-title"><?php echo esc_html__( 'Still experiencing issues?', 'elementor' ); ?></div>
286 <div class="elementor-safe-mode-list-item-content">
287 <?php
288 printf(
289 /* translators: %1$s Link open tag, %2$s: Link close tag. */
290 esc_html__( '%1$sClick here%2$s to troubleshoot', 'elementor' ),
291 '<a href="' . self::DOCS_DIDNT_HELP_URL . '" target="_blank">', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
292 '</a>'
293 );
294 ?>
295 </div>
296 </li>
297 </ul>
298 <?php
299 $mu_plugins = wp_get_mu_plugins();
300
301 if ( 1 < count( $mu_plugins ) ) : ?>
302 <div class="elementor-safe-mode-mu-plugins">
303 <?php
304 printf(
305 /* translators: %1$s Link open tag, %2$s: Link close tag. */
306 esc_html__( 'Please note! We couldn\'t deactivate all of your plugins on Safe Mode. Please %1$sread more%2$s about this issue', 'elementor' ),
307 '<a href="' . self::DOCS_MU_PLUGINS_URL . '" target="_blank">', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
308 '</a>'
309 );
310 ?>
311 </div>
312 <?php endif; ?>
313 </div>
314 </div>
315
316 <script>
317 var ElementorSafeMode = function() {
318 var attachEvents = function() {
319 jQuery( '.elementor-disable-safe-mode' ).on( 'click', function( e ) {
320 if ( ! elementorCommon || ! elementorCommon.ajax ) {
321 return;
322 }
323
324 e.preventDefault();
325
326 elementorCommon.ajax.addRequest(
327 'disable_safe_mode', {
328 success: function() {
329 if ( -1 === location.href.indexOf( 'elementor-mode=safe' ) ) {
330 location.reload();
331 } else {
332 // Need to remove the URL from browser history.
333 location.replace( location.href.replace( '&elementor-mode=safe', '' ) );
334 }
335 },
336 error: function() {
337 alert( 'An error occurred.' );
338 },
339 },
340 true
341 );
342 } );
343 };
344
345 var init = function() {
346 attachEvents();
347 };
348
349 init();
350 };
351
352 new ElementorSafeMode();
353 </script>
354 <?php
355 }
356
357 public function print_try_safe_mode() {
358 if ( ! $this->is_allowed_post_type() ) {
359 return;
360 }
361
362 $this->print_safe_mode_css();
363 ?>
364 <div class="elementor-safe-mode-toast" id="elementor-try-safe-mode">
365 <?php if ( current_user_can( 'install_plugins' ) ) : ?>
366 <header>
367 <i class="eicon-warning"></i>
368 <h2><?php echo esc_html__( 'Can\'t Edit?', 'elementor' ); ?></h2>
369 <a class="elementor-button e-primary elementor-safe-mode-button elementor-enable-safe-mode" target="_blank" href="<?php echo esc_url( $this->get_admin_page_url() ); ?>">
370 <?php echo esc_html__( 'Enable Safe Mode', 'elementor' ); ?>
371 </a>
372 </header>
373 <div class="elementor-toast-content">
374 <?php echo esc_html__( 'Having problems loading Elementor? Please enable Safe Mode to troubleshoot.', 'elementor' ); ?>
375 <a href="<?php Utils::print_unescaped_internal_string( self::DOCS_TRY_SAFE_MODE_URL ); ?>" target="_blank"><?php echo esc_html__( 'Learn More', 'elementor' ); ?></a>
376 </div>
377 <?php else : ?>
378 <header>
379 <i class="eicon-warning"></i>
380 <h2><?php echo esc_html__( 'Can\'t Edit?', 'elementor' ); ?></h2>
381 </header>
382 <div class="elementor-toast-content">
383 <?php echo esc_html__( 'If you are experiencing a loading issue, contact your site administrator to troubleshoot the problem using Safe Mode.', 'elementor' ); ?>
384 <a href="<?php Utils::print_unescaped_internal_string( self::DOCS_TRY_SAFE_MODE_URL ); ?>" target="_blank"><?php echo esc_html__( 'Learn More', 'elementor' ); ?></a>
385 </div>
386 <?php endif; ?>
387 </div>
388
389 <script>
390 var ElementorTrySafeMode = function() {
391 var attachEvents = function() {
392 jQuery( '.elementor-enable-safe-mode' ).on( 'click', function( e ) {
393 if ( ! elementorCommon || ! elementorCommon.ajax ) {
394 return;
395 }
396
397 e.preventDefault();
398
399 elementorCommon.ajax.addRequest(
400 'enable_safe_mode', {
401 data: {
402 editor_post_id: '<?php
403 // PHPCS - the method get_post_id is safe.
404 echo Plugin::$instance->editor->get_post_id(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
405 ?>',
406 },
407 success: function( url ) {
408 location.assign( url );
409 },
410 error: function() {
411 alert( 'An error occurred.' );
412 },
413 },
414 true
415 );
416 } );
417 };
418
419 var isElementorLoaded = function() {
420 if ( 'undefined' === typeof elementor ) {
421 return false;
422 }
423
424 if ( ! elementor.loaded ) {
425 return false;
426 }
427
428 if ( jQuery( '#elementor-loading' ).is( ':visible' ) ) {
429 return false;
430 }
431
432 return true;
433 };
434
435 var handleTrySafeModeNotice = function() {
436 var $notice = jQuery( '#elementor-try-safe-mode' );
437
438 if ( isElementorLoaded() ) {
439 $notice.remove();
440 return;
441 }
442
443 if ( ! $notice.data( 'visible' ) ) {
444 $notice.show().data( 'visible', true );
445 }
446
447 // Re-check after 500ms.
448 setTimeout( handleTrySafeModeNotice, 500 );
449 };
450
451 var init = function() {
452 setTimeout( handleTrySafeModeNotice, <?php Utils::print_unescaped_internal_string( self::EDITOR_NOTICE_TIMEOUT ); ?> );
453
454 attachEvents();
455 };
456
457 init();
458 };
459
460 new ElementorTrySafeMode();
461 </script>
462
463 <?php
464 }
465
466 public function run_safe_mode() {
467 remove_action( 'elementor/editor/footer', [ $this, 'print_try_safe_mode' ] );
468
469 // Avoid notices like for comment.php.
470 add_filter( 'deprecated_file_trigger_error', '__return_false' );
471
472 add_filter( 'template_include', [ $this, 'filter_template' ], 999 );
473 add_filter( 'elementor/document/urls/preview', [ $this, 'filter_preview_url' ] );
474 add_action( 'elementor/editor/footer', [ $this, 'print_safe_mode_notice' ] );
475 add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'register_scripts' ], 11 /* After Common Scripts */ );
476 }
477
478 public function register_scripts() {
479 wp_add_inline_script( 'elementor-common', 'elementorCommon.ajax.addRequestConstant( "elementor-mode", "safe" );' );
480 }
481
482 private function is_enabled() {
483 return get_option( self::OPTION_ENABLED, '' );
484 }
485
486 private function get_admin_page_url() {
487 // A fallback URL if the Js doesn't work.
488 return Tools::get_url();
489 }
490
491 public function plugin_action_links( $actions ) {
492 $actions['disable'] = '<a href="' . self::get_admin_page_url() . '">' . esc_html__( 'Disable Safe Mode', 'elementor' ) . '</a>';
493
494 return $actions;
495 }
496
497 public function on_deactivated_plugin( $plugin ) {
498 if ( ELEMENTOR_PLUGIN_BASE === $plugin ) {
499 $this->disable_safe_mode();
500 return;
501 }
502
503 $allowed_plugins = get_option( 'elementor_safe_mode_allowed_plugins', [] );
504 $plugin_key = array_search( $plugin, $allowed_plugins, true );
505
506 if ( $plugin_key ) {
507 unset( $allowed_plugins[ $plugin_key ] );
508 update_option( 'elementor_safe_mode_allowed_plugins', $allowed_plugins );
509 }
510 }
511
512 public function update_allowed_plugins() {
513 $allowed_plugins = [
514 'elementor' => ELEMENTOR_PLUGIN_BASE,
515 ];
516
517 if ( defined( 'ELEMENTOR_PRO_PLUGIN_BASE' ) ) {
518 $allowed_plugins['elementor_pro'] = ELEMENTOR_PRO_PLUGIN_BASE;
519 }
520
521 if ( defined( 'WC_PLUGIN_BASENAME' ) ) {
522 $allowed_plugins['woocommerce'] = WC_PLUGIN_BASENAME;
523 }
524
525 update_option( 'elementor_safe_mode_allowed_plugins', $allowed_plugins );
526 }
527
528 public function __construct() {
529 if ( current_user_can( 'install_plugins' ) ) {
530 add_action( 'elementor/admin/after_create_settings/elementor-tools', [ $this, 'add_admin_button' ] );
531 }
532
533 add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] );
534
535 $plugin_file = self::MU_PLUGIN_FILE_NAME;
536 add_filter( "plugin_action_links_{$plugin_file}", [ $this, 'plugin_action_links' ] );
537
538 // Use pre_update, in order to catch cases that $value === $old_value and it not updated.
539 add_filter( 'pre_update_option_elementor_safe_mode', [ $this, 'on_update_safe_mode' ], 10, 2 );
540
541 add_action( 'elementor/safe_mode/init', [ $this, 'run_safe_mode' ] );
542 add_action( 'elementor/editor/footer', [ $this, 'print_try_safe_mode' ] );
543
544 if ( $this->is_enabled() ) {
545 add_action( 'activated_plugin', [ $this, 'update_allowed_plugins' ] );
546 add_action( 'deactivated_plugin', [ $this, 'on_deactivated_plugin' ] );
547 }
548 }
549
550 private function is_allowed_post_type() {
551 $allowed_post_types = [
552 'post',
553 'page',
554 'product',
555 Source_Local::CPT,
556 ];
557
558 $current_post_type = get_post_type( Plugin::$instance->editor->get_post_id() );
559
560 return in_array( $current_post_type, $allowed_post_types );
561 }
562 }
563