sanitize( get_user_meta( $id, $key, true ) ); } /** * Validate every supported field and discard unknown input keys. * * @param mixed $input Untrusted preference data. * @return array Boolean opt-in, validated colors, and an allowed radius. */ public function sanitize( mixed $input ): array { $input = is_array( $input ) ? $input : array(); $accent = is_string( $input['accent'] ?? null ) ? sanitize_hex_color( $input['accent'] ) : null; $background = is_string( $input['chrome_background'] ?? null ) ? sanitize_hex_color( $input['chrome_background'] ) : null; $radius = is_scalar( $input['radius'] ?? null ) ? (int) $input['radius'] : 16; return array( 'enabled' => ! empty( $input['enabled'] ), 'accent' => $accent ? $accent : '#303030', 'chrome_background' => $background ? $background : '#000000', 'radius' => in_array( $radius, array( 8, 12, 16 ), true ) ? $radius : 16, ); } /** Add a personal settings page under Users or Profile as permitted. */ public function menu(): void { add_submenu_page( current_user_can( 'list_users' ) ? 'users.php' : 'profile.php', __( 'Adminlume', 'adminlume' ), __( 'Adminlume', 'adminlume' ), 'read', self::PAGE, array( $this, 'render' ) ); } /** * Add a personal-preferences link to the plugin row. * * @param string[] $links Existing action links. * @return string[] Updated action links. */ public function links( array $links ): array { array_unshift( $links, '' . esc_html__( 'Personal preferences', 'adminlume' ) . '' ); return $links; } /** * Resolve the current user's settings URL. * * @return string Settings URL. */ private function url(): string { return admin_url( ( current_user_can( 'list_users' ) ? 'users.php' : 'profile.php' ) . '?page=' . self::PAGE ); } /** Persist only the authenticated account's validated preferences via POST. */ public function save(): void { if ( ! current_user_can( 'read' ) ) { wp_die( esc_html__( 'Access denied.', 'adminlume' ), '', array( 'response' => 403 ) ); } if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || 'POST' !== sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) { wp_die( esc_html__( 'Only POST requests are allowed.', 'adminlume' ), '', array( 'response' => 405 ) ); } check_admin_referer( 'adminlume_save_' . get_current_user_id() ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitize() validates each field against strict color, boolean, and radius rules. $preferences = $this->sanitize( wp_unslash( $_POST['preferences'] ?? array() ) ); update_user_meta( get_current_user_id(), self::META_KEY, $preferences ); wp_safe_redirect( add_query_arg( 'saved', '1', $this->url() ) ); exit; } /** Render escaped personal settings without accepting a client-selected user. */ public function render(): void { if ( ! current_user_can( 'read' ) ) { return; } $preferences = $this->preferences(); ?>