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

571 lines 16.1 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-top: 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-bottom: 20px;
202 }
203
204 .elementor-safe-mode-toast header > * {
205 margin-top: 10px;
206 }
207
208 .elementor-safe-mode-toast header i {
209 font-size: 25px;
210 color: var(--e-a-color-warning);
211 }
212
213 body:not(.rtl) .elementor-safe-mode-toast header i {
214 margin-right: 10px;
215 }
216
217 body.rtl .elementor-safe-mode-toast header i {
218 margin-left: 10px;
219 }
220
221 .elementor-safe-mode-toast header h2 {
222 flex-grow: 1;
223 font-size: 18px;
224 }
225
226 .elementor-safe-mode-list-item {
227 margin-top: 10px;
228 list-style: outside;
229 }
230
231 body:not(.rtl) .elementor-safe-mode-list-item {
232 margin-left: 15px;
233 }
234
235 body.rtl .elementor-safe-mode-list-item {
236 margin-right: 15px;
237 }
238
239 .elementor-safe-mode-list-item b {
240 font-size: 14px;
241 }
242
243 .elementor-safe-mode-list-item-content {
244 font-style: italic;
245 color: var(--e-a-color-txt);
246 }
247
248 .elementor-safe-mode-list-item-title {
249 font-weight: 500;
250 }
251
252 .elementor-safe-mode-mu-plugins {
253 background-color: var(--e-a-bg-hover);
254 color: var(--e-a-color-txt-hover);
255 margin-top: 20px;
256 padding: 10px 15px;
257 }
258 </style>
259 <?php
260 }
261
262 public function print_safe_mode_notice() {
263 $this->print_safe_mode_css()
264 ?>
265 <div class="elementor-safe-mode-toast" id="elementor-safe-mode-message">
266 <header>
267 <i class="eicon-warning"></i>
268 <h2><?php echo esc_html__( 'Safe Mode ON', 'elementor' ); ?></h2>
269 <a class="elementor-button elementor-safe-mode-button elementor-disable-safe-mode" target="_blank" href="<?php echo esc_url( $this->get_admin_page_url() ); ?>">
270 <?php echo esc_html__( 'Disable Safe Mode', 'elementor' ); ?>
271 </a>
272 </header>
273
274 <div class="elementor-toast-content">
275 <ul class="elementor-safe-mode-list">
276 <li class="elementor-safe-mode-list-item">
277 <div class="elementor-safe-mode-list-item-title"><?php echo esc_html__( 'Editor successfully loaded?', 'elementor' ); ?></div>
278 <div class="elementor-safe-mode-list-item-content">
279 <?php
280 echo esc_html__( 'The issue was probably caused by one of your plugins or theme.', 'elementor' );
281 echo ' ';
282
283 printf(
284 /* translators: %1$s Link open tag, %2$s: Link close tag. */
285 esc_html__( '%1$sClick here%2$s to troubleshoot', 'elementor' ),
286 '<a href="' . self::DOCS_HELPED_URL . '" target="_blank">', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
287 '</a>'
288 );
289 ?>
290 </div>
291 </li>
292 <li class="elementor-safe-mode-list-item">
293 <div class="elementor-safe-mode-list-item-title"><?php echo esc_html__( 'Still experiencing issues?', 'elementor' ); ?></div>
294 <div class="elementor-safe-mode-list-item-content">
295 <?php
296 printf(
297 /* translators: %1$s Link open tag, %2$s: Link close tag. */
298 esc_html__( '%1$sClick here%2$s to troubleshoot', 'elementor' ),
299 '<a href="' . self::DOCS_DIDNT_HELP_URL . '" target="_blank">', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
300 '</a>'
301 );
302 ?>
303 </div>
304 </li>
305 </ul>
306 <?php
307 $mu_plugins = wp_get_mu_plugins();
308
309 if ( 1 < count( $mu_plugins ) ) : ?>
310 <div class="elementor-safe-mode-mu-plugins">
311 <?php
312 printf(
313 /* translators: %1$s Link open tag, %2$s: Link close tag. */
314 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' ),
315 '<a href="' . self::DOCS_MU_PLUGINS_URL . '" target="_blank">', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
316 '</a>'
317 );
318 ?>
319 </div>
320 <?php endif; ?>
321 </div>
322 </div>
323
324 <script>
325 var ElementorSafeMode = function() {
326 var attachEvents = function() {
327 jQuery( '.elementor-disable-safe-mode' ).on( 'click', function( e ) {
328 if ( ! elementorCommon || ! elementorCommon.ajax ) {
329 return;
330 }
331
332 e.preventDefault();
333
334 elementorCommon.ajax.addRequest(
335 'disable_safe_mode', {
336 success: function() {
337 if ( -1 === location.href.indexOf( 'elementor-mode=safe' ) ) {
338 location.reload();
339 } else {
340 // Need to remove the URL from browser history.
341 location.replace( location.href.replace( '&elementor-mode=safe', '' ) );
342 }
343 },
344 error: function() {
345 alert( 'An error occurred' );
346 },
347 },
348 true
349 );
350 } );
351 };
352
353 var init = function() {
354 attachEvents();
355 };
356
357 init();
358 };
359
360 new ElementorSafeMode();
361 </script>
362 <?php
363 }
364
365 public function print_try_safe_mode() {
366 if ( ! $this->is_allowed_post_type() ) {
367 return;
368 }
369
370 $this->print_safe_mode_css();
371 ?>
372 <div class="elementor-safe-mode-toast" id="elementor-try-safe-mode">
373 <?php if ( current_user_can( 'install_plugins' ) ) : ?>
374 <header>
375 <i class="eicon-warning"></i>
376 <h2><?php echo esc_html__( 'Can\'t Edit?', 'elementor' ); ?></h2>
377 <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() ); ?>">
378 <?php echo esc_html__( 'Enable Safe Mode', 'elementor' ); ?>
379 </a>
380 </header>
381 <div class="elementor-toast-content">
382 <?php echo esc_html__( 'Having problems loading Elementor? Please enable Safe Mode to troubleshoot.', 'elementor' ); ?>
383 <a href="<?php Utils::print_unescaped_internal_string( self::DOCS_TRY_SAFE_MODE_URL ); ?>" target="_blank"><?php echo esc_html__( 'Learn More', 'elementor' ); ?></a>
384 </div>
385 <?php else : ?>
386 <header>
387 <i class="eicon-warning"></i>
388 <h2><?php echo esc_html__( 'Can\'t Edit?', 'elementor' ); ?></h2>
389 </header>
390 <div class="elementor-toast-content">
391 <?php echo esc_html__( 'If you are experiencing a loading issue, contact your site administrator to troubleshoot the problem using Safe Mode.', 'elementor' ); ?>
392 <a href="<?php Utils::print_unescaped_internal_string( self::DOCS_TRY_SAFE_MODE_URL ); ?>" target="_blank"><?php echo esc_html__( 'Learn More', 'elementor' ); ?></a>
393 </div>
394 <?php endif; ?>
395 </div>
396
397 <script>
398 var ElementorTrySafeMode = function() {
399 var attachEvents = function() {
400 jQuery( '.elementor-enable-safe-mode' ).on( 'click', function( e ) {
401 if ( ! elementorCommon || ! elementorCommon.ajax ) {
402 return;
403 }
404
405 e.preventDefault();
406
407 elementorCommon.ajax.addRequest(
408 'enable_safe_mode', {
409 data: {
410 editor_post_id: '<?php
411 // PHPCS - the method get_post_id is safe.
412 echo Plugin::$instance->editor->get_post_id(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
413 ?>',
414 },
415 success: function( url ) {
416 location.assign( url );
417 },
418 error: function() {
419 alert( 'An error occurred' );
420 },
421 },
422 true
423 );
424 } );
425 };
426
427 var isElementorLoaded = function() {
428 if ( 'undefined' === typeof elementor ) {
429 return false;
430 }
431
432 if ( ! elementor.loaded ) {
433 return false;
434 }
435
436 if ( jQuery( '#elementor-loading' ).is( ':visible' ) ) {
437 return false;
438 }
439
440 return true;
441 };
442
443 var handleTrySafeModeNotice = function() {
444 var $notice = jQuery( '#elementor-try-safe-mode' );
445
446 if ( isElementorLoaded() ) {
447 $notice.remove();
448 return;
449 }
450
451 if ( ! $notice.data( 'visible' ) ) {
452 $notice.show().data( 'visible', true );
453 }
454
455 // Re-check after 500ms.
456 setTimeout( handleTrySafeModeNotice, 500 );
457 };
458
459 var init = function() {
460 setTimeout( handleTrySafeModeNotice, <?php Utils::print_unescaped_internal_string( self::EDITOR_NOTICE_TIMEOUT ); ?> );
461
462 attachEvents();
463 };
464
465 init();
466 };
467
468 new ElementorTrySafeMode();
469 </script>
470
471 <?php
472 }
473
474 public function run_safe_mode() {
475 remove_action( 'elementor/editor/footer', [ $this, 'print_try_safe_mode' ] );
476
477 // Avoid notices like for comment.php.
478 add_filter( 'deprecated_file_trigger_error', '__return_false' );
479
480 add_filter( 'template_include', [ $this, 'filter_template' ], 999 );
481 add_filter( 'elementor/document/urls/preview', [ $this, 'filter_preview_url' ] );
482 add_action( 'elementor/editor/footer', [ $this, 'print_safe_mode_notice' ] );
483 add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'register_scripts' ], 11 /* After Common Scripts */ );
484 }
485
486 public function register_scripts() {
487 wp_add_inline_script( 'elementor-common', 'elementorCommon.ajax.addRequestConstant( "elementor-mode", "safe" );' );
488 }
489
490 private function is_enabled() {
491 return get_option( self::OPTION_ENABLED, '' );
492 }
493
494 private function get_admin_page_url() {
495 // A fallback URL if the Js doesn't work.
496 return Tools::get_url();
497 }
498
499 public function plugin_action_links( $actions ) {
500 $actions['disable'] = '<a href="' . self::get_admin_page_url() . '">' . esc_html__( 'Disable Safe Mode', 'elementor' ) . '</a>';
501
502 return $actions;
503 }
504
505 public function on_deactivated_plugin( $plugin ) {
506 if ( ELEMENTOR_PLUGIN_BASE === $plugin ) {
507 $this->disable_safe_mode();
508 return;
509 }
510
511 $allowed_plugins = get_option( 'elementor_safe_mode_allowed_plugins', [] );
512 $plugin_key = array_search( $plugin, $allowed_plugins, true );
513
514 if ( $plugin_key ) {
515 unset( $allowed_plugins[ $plugin_key ] );
516 update_option( 'elementor_safe_mode_allowed_plugins', $allowed_plugins );
517 }
518 }
519
520 public function update_allowed_plugins() {
521 $allowed_plugins = [
522 'elementor' => ELEMENTOR_PLUGIN_BASE,
523 ];
524
525 if ( defined( 'ELEMENTOR_PRO_PLUGIN_BASE' ) ) {
526 $allowed_plugins['elementor_pro'] = ELEMENTOR_PRO_PLUGIN_BASE;
527 }
528
529 if ( defined( 'WC_PLUGIN_BASENAME' ) ) {
530 $allowed_plugins['woocommerce'] = WC_PLUGIN_BASENAME;
531 }
532
533 update_option( 'elementor_safe_mode_allowed_plugins', $allowed_plugins );
534 }
535
536 public function __construct() {
537 if ( current_user_can( 'install_plugins' ) ) {
538 add_action( 'elementor/admin/after_create_settings/elementor-tools', [ $this, 'add_admin_button' ] );
539 }
540
541 add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] );
542
543 $plugin_file = self::MU_PLUGIN_FILE_NAME;
544 add_filter( "plugin_action_links_{$plugin_file}", [ $this, 'plugin_action_links' ] );
545
546 // Use pre_update, in order to catch cases that $value === $old_value and it not updated.
547 add_filter( 'pre_update_option_elementor_safe_mode', [ $this, 'on_update_safe_mode' ], 10, 2 );
548
549 add_action( 'elementor/safe_mode/init', [ $this, 'run_safe_mode' ] );
550 add_action( 'elementor/editor/footer', [ $this, 'print_try_safe_mode' ] );
551
552 if ( $this->is_enabled() ) {
553 add_action( 'activated_plugin', [ $this, 'update_allowed_plugins' ] );
554 add_action( 'deactivated_plugin', [ $this, 'on_deactivated_plugin' ] );
555 }
556 }
557
558 private function is_allowed_post_type() {
559 $allowed_post_types = [
560 'post',
561 'page',
562 'product',
563 Source_Local::CPT,
564 ];
565
566 $current_post_type = get_post_type( Plugin::$instance->editor->get_post_id() );
567
568 return in_array( $current_post_type, $allowed_post_types );
569 }
570 }
571