PluginProbe
WP Coder – Insert & Manage Code Snippets / 3.2
WP Coder – Insert & Manage Code Snippets v3.2
4.5.1 1.1 2.3.1 2.3.2 2.4.1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 3.2 3.2.1 3.3 3.4 3.5 3.5.1 All 39 releases
wp-coder / includes / safe-mode.php

safe-mode.php in WP Coder – Insert & Manage Code Snippets 3.2, at includes/safe-mode.php

100 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Safe mode query var logic.
4 *
5 * @package WPCoder
6 */
7
8 defined( 'ABSPATH' ) || exit;
9
10 add_action( 'plugins_loaded', 'wpcoder_maybe_enable_safe_mode' );
11
12 /**
13 * Simple check to see if we should be adding safe-mode logic.
14 *
15 * @return void
16 */
17 function wpcoder_maybe_enable_safe_mode() {
18 if ( ! isset( $_GET['wpcoder-safe-mode'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
19 return;
20 }
21
22 // If we're in safe mode, let's make sure all URLs keep the param until we are safe to get out.
23 add_filter( 'home_url', 'wpcoder_keep_safe_mode' );
24 add_filter( 'admin_url', 'wpcoder_keep_safe_mode' );
25 add_filter( 'site_url', 'wpcoder_keep_safe_mode_login', 10, 3 );
26 // The admin menu doesn't offer a hook to change all the menu links so we do it with JS.
27 add_action( 'admin_footer', 'wpcoder_keep_safe_mode_admin_menu' );
28 // Show a notice informing the user we're in safe mode and offer a way to get out.
29 add_action( 'admin_notices', 'wpcoder_safe_mode_notice' );
30 add_action( 'wpcoder_admin_notices', 'wpcoder_safe_mode_notice' );
31 }
32
33 /**
34 * Make sure the URL keeps the safe mode variable.
35 *
36 * @param string $url The home or admin base URL.
37 *
38 * @return string
39 */
40 function wpcoder_keep_safe_mode( $url ) {
41 return add_query_arg( 'wpcoder-safe-mode', 1, $url );
42 }
43
44 /**
45 * Force safe mode to all URLs displayed in the admin so we can keep navigating
46 * using safe mode as there's no hook in WP to change the main admin menu.
47 *
48 * @return void
49 */
50 function wpcoder_keep_safe_mode_admin_menu() {
51 // There's no reliable way to filter all the admin menu links so we have to force them via JS.
52 // There's also a notice being added to allow users to "exit" safe mode.
53 ?>
54 <script type="text/javascript">
55 [...document.querySelectorAll( 'a:not(.wpcoder-safe-mode)' )].forEach( e => {
56 const url = new URL( e.href );
57 url.searchParams.set( 'wpcoder-safe-mode', '1' );
58 e.href = url.toString();
59 } );
60 </script>
61 <?php
62 }
63
64 /**
65 * Show a notice informing the user we're in safe mode and offer a way to get out.
66 *
67 * @return void
68 */
69 function wpcoder_safe_mode_notice() {
70 ?>
71 <div class="notice notice-warning">
72 <p><?php esc_html_e( 'WPCoder is in Safe Mode which means no codes are getting executed. Please disable any snippets that have caused errors and when done click the button below to exit safe mode.', 'wpcoder' ); ?></p>
73 <p><?php esc_html_e( 'The link will open in a new window so if you are still encountering issues you safely can return to this tab and make further adjustments', 'wpcoder' ); ?></p>
74 <p>
75 <a class="button button-secondary wpcoder-safe-mode" href="<?php echo esc_url( remove_query_arg( 'wpcoder-safe-mode' ) ); ?>" target="_blank"><?php esc_html_e( 'Exit safe mode', 'wpcoder' ); ?></a>
76 </p>
77 </div>
78 <?php
79 }
80
81 /**
82 * Checks schema passed to site_url and adds the safe mode query param
83 * so we can login using safe mode.
84 *
85 * @param string $url The site_url already processed.
86 * @param string $path The path that was added to the URL.
87 * @param string $scheme The scheme that was requested.
88 *
89 * @return string
90 */
91 function wpcoder_keep_safe_mode_login( $url, $path, $scheme ) {
92 if ( 'login_post' !== $scheme ) {
93 return $url;
94 }
95
96 return add_query_arg( 'wpcoder-safe-mode', 1, $url );
97 }
98
99
100