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

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