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

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