settings = $settings; } /** Register admin-only hooks. */ public function register_hooks() { add_action( 'admin_menu', array( $this, 'menu' ) ); add_action( 'admin_post_loginstudio_reset', array( $this, 'reset' ) ); } /** Add the requested LoginStudio > Customize entry. */ public function menu() { $this->screen_hook = add_menu_page( __( 'Login Brush', 'login-brush' ), __( 'Login Brush', 'login-brush' ), Settings::capability(), self::SLUG, array( $this, 'render' ), 'dashicons-admin-customizer', 81 ); add_submenu_page( self::SLUG, __( 'Customize Login', 'login-brush' ), __( 'Customize', 'login-brush' ), Settings::capability(), self::SLUG, array( $this, 'render' ) ); } /** * Return the menu-generated editor screen identifier. * * @return string Exact screen hook for asset gating. */ public function screen_hook() { return $this->screen_hook; } /** * Build the fixed authorized editor destination. * * @return string Fixed editor URL; never a caller-supplied redirect. */ public static function url() { return admin_url( 'admin.php?page=' . self::SLUG ); } /** Render escaped controls from normalized settings. */ public function render() { if ( ! current_user_can( Settings::capability() ) ) { wp_die( esc_html__( 'You cannot access Login Brush.', 'login-brush' ), '', array( 'response' => 403 ) ); } require_once __DIR__ . '/class-controls.php'; $settings = $this->settings->all(); $supported = $this->settings->is_supported(); $preview = $supported ? Preview::configuration() : array(); $controls = new Controls( $settings ); $sections = $controls->sections(); $errors = get_settings_errors(); $has_error = false; foreach ( $errors as $error ) { if ( 'error' === $error['type'] && ( Settings::OPTION === $error['setting'] || 'general' === $error['setting'] ) ) { $has_error = true; } } // These flags only describe status; neither flag performs or authorizes a write. $reset_status = isset( $_GET['loginstudio-reset'] ) && is_string( $_GET['loginstudio-reset'] ) ? sanitize_key( wp_unslash( $_GET['loginstudio-reset'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $status = __( 'All changes saved', 'login-brush' ); if ( $has_error || 'error' === $reset_status ) { $status = __( 'Changes were not saved', 'login-brush' ); } if ( ! $supported ) { $status = __( 'Settings are read-only', 'login-brush' ); } include __DIR__ . '/views/editor.php'; } /** Reset only after capability, HTTP method, nonce and explicit confirmation. */ public function reset() { if ( ! current_user_can( Settings::capability() ) ) { wp_die( esc_html__( 'You cannot reset Login Brush.', 'login-brush' ), '', array( 'response' => 403 ) ); } if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || 'POST' !== $_SERVER['REQUEST_METHOD'] ) { wp_die( esc_html__( 'Reset requires a POST request.', 'login-brush' ), '', array( 'response' => 405 ) ); } if ( ! isset( $_POST['loginstudio_reset_nonce'] ) || ! is_string( $_POST['loginstudio_reset_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['loginstudio_reset_nonce'] ) ), 'loginstudio_reset' ) ) { wp_die( esc_html__( 'You cannot reset Login Brush.', 'login-brush' ), '', array( 'response' => 403 ) ); } if ( ! isset( $_POST['loginstudio_confirm_reset'] ) || '1' !== $_POST['loginstudio_confirm_reset'] ) { wp_die( esc_html__( 'Please confirm that you want to reset Login Brush.', 'login-brush' ), '', array( 'response' => 400 ) ); } $result = $this->settings->reset(); wp_safe_redirect( add_query_arg( 'loginstudio-reset', true === $result ? 'success' : 'error', self::url() ) ); exit; } }