'button', 'id' => 'safe-mode-button', 'name' => '', 'input_class' => 'pwp-btn pwp-btn-primary ae-ajax-btn', 'label' => $active ? __( 'Turn safe mode off', 'adminease' ) : __( 'Turn AdminEase off for me', 'adminease' ), 'description' => __( 'For the next hour, only you see your site with AdminEase\'s cleanup switches off (emojis, feeds, embeds, comments and similar). Nothing is saved or changed, and protections like Site lock, maintenance mode and blocks stay on. Use it to compare your site\'s speed with and without AdminEase, or to find out whether a switch broke something. It only affects pages on the front end.', 'adminease' ), ]; return $fields; } /** * Adds the Safe mode section to the Tools sidebar. * * @param array $subgroups The existing sub-section map. * * @return array The modified sub-section map. */ public function adminease_settings_subgroups( array $subgroups ): array { if( !isset( $subgroups['tools'] ) ) { return $subgroups; } $subgroups['tools']['safe-mode'] = [ 'title' => __( 'Safe mode', 'adminease' ), 'icon' => '', 'field_ids' => [ 'safe-mode-button' ], ]; return $subgroups; } /** * Enqueues the button script on the AdminEase dashboard. * * @param string $hook The current admin page hook suffix. * * @return void */ public function admin_enqueue_scripts( string $hook ): void { if( 'toplevel_page_adminease' !== $hook ) { return; } wp_enqueue_script( ADMINEASE_NAME . 'SafeMode', ADMINEASE_PLUGIN_URL . 'assets/js/AdminEaseSafeMode.js', [ 'jquery', ADMINEASE_NAME ], filemtime( ADMINEASE_DIR . 'assets/js/AdminEaseSafeMode.js' ), true ); wp_localize_script( ADMINEASE_NAME . 'SafeMode', ADMINEASE_NAME . 'SafeModeAjaxObj', [ 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'security' => wp_create_nonce( 'adminease_safe_mode' ), 'homeUrl' => home_url( '/' ), 'active' => Plugin::has_safe_mode_cookie(), 'i18n' => [ 'turnOn' => esc_html__( 'Turn AdminEase off for me', 'adminease' ), 'turnOff' => esc_html__( 'Turn safe mode off', 'adminease' ), 'onMessage' => esc_html__( 'Safe mode is on for one hour. Open your site in this browser to see it.', 'adminease' ), 'offMessage' => esc_html__( 'Safe mode is off.', 'adminease' ), 'failed' => esc_html__( 'Could not change safe mode.', 'adminease' ), 'networkError' => esc_html__( 'Network error', 'adminease' ), 'btnProcessing' => esc_html__( 'Processing…', 'adminease' ), 'btnComplete' => esc_html__( 'Complete!', 'adminease' ), ], ] ); } /** * Turns safe mode on or off for the administrator who clicked. * * @return void */ public function ajax_safe_mode(): void { if( !isset( $_POST['security'] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_POST['security'] ) ), 'adminease_safe_mode' ) ) { wp_send_json_error( new WP_Error( 'security_error', __( 'An error occurred. Refresh the page and try again.', 'adminease' ) ), 403 ); } if( !current_user_can( 'manage_options' ) ) { wp_send_json_error( new WP_Error( 'permissions', esc_html__( 'You do not have sufficient permissions to perform this action', 'adminease' ) ) ); } $turn_on = isset( $_POST['enable'] ) && '1' === sanitize_text_field( wp_unslash( $_POST['enable'] ) ); $expires = $turn_on ? time() + self::LIFETIME : time() - DAY_IN_SECONDS; $value = $turn_on ? Plugin::safe_mode_cookie_value( $expires ) : ''; if( $turn_on && '' === $value ) { wp_send_json_error( new WP_Error( 'no_keys', esc_html__( 'Your site has no secret keys in wp-config.php, so safe mode cannot be signed.', 'adminease' ) ) ); } setcookie( 'adminease_safe_mode', $value, [ 'expires' => $expires, 'path' => COOKIEPATH, 'domain' => COOKIE_DOMAIN, 'secure' => is_ssl(), 'httponly' => true, 'samesite' => 'Lax', ] ); wp_send_json_success( [ 'enabled' => $turn_on ] ); } /** * Tells the administrator on the front end that safe mode is on. * * @param \WP_Admin_Bar $wp_admin_bar The admin bar. * * @return void */ public function admin_bar_notice( $wp_admin_bar ): void { $wp_admin_bar->add_node( [ 'id' => 'adminease-safe-mode', 'parent' => 'top-secondary', 'title' => esc_html__( 'AdminEase safe mode is on', 'adminease' ), 'href' => admin_url( 'admin.php?page=adminease' ), ] ); } }