PluginProbe
Elementor Website Builder – more than just a page builder / 3.13.1
Elementor Website Builder – more than just a page builder v3.13.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.13.1, at modules/safe-mode/module.php

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