'GET', 'permission_callback' => array( $this, 'is_signed_in' ), 'callback' => array( $this, 'notifications' ), 'args' => array( 'unread' => array( 'type' => 'boolean' ), 'page' => array( 'type' => 'integer' ), 'per_page' => array( 'type' => 'integer' ) ), ) ); register_rest_route( self::NS, '/notifications/read', array( 'methods' => 'POST', 'permission_callback' => array( $this, 'is_signed_in' ), 'callback' => array( $this, 'read' ), 'args' => array( 'id' => array( 'type' => 'integer', 'required' => false ) ), ) ); register_rest_route( self::NS, '/me', array( 'methods' => 'GET', 'permission_callback' => array( $this, 'is_signed_in' ), 'callback' => array( $this, 'me' ), ) ); register_rest_route( self::NS, '/me/widgets', array( 'methods' => 'POST', 'permission_callback' => array( $this, 'is_signed_in' ), 'callback' => array( $this, 'widgets' ), ) ); register_rest_route( self::NS, '/me/preferences', array( 'methods' => 'POST', 'permission_callback' => array( $this, 'is_signed_in' ), 'callback' => array( $this, 'preferences' ), 'args' => array( 'mode' => array( 'type' => 'string', 'enum' => array( 'light', 'dark', 'auto' ), 'required' => false ), 'sidebar' => array( 'type' => 'string', 'enum' => array( 'expanded', 'collapsed' ), 'required' => false ), ), ) ); } public function notifications( \WP_REST_Request $r ): \WP_REST_Response { $uid = get_current_user_id(); return new \WP_REST_Response( array( 'items' => \Dashbread\Notifications\Store::list( $uid, (bool) $r->get_param( 'unread' ), max( 1, (int) $r->get_param( 'page' ) ), min( 50, max( 1, (int) ( $r->get_param( 'per_page' ) ?: 20 ) ) ) ), 'unread' => \Dashbread\Notifications\Store::unread_count( $uid ), ) ); } public function read( \WP_REST_Request $r ): \WP_REST_Response { $uid = get_current_user_id(); $id = (int) $r->get_param( 'id' ); if ( $id > 0 ) { \Dashbread\Notifications\Store::mark_read( $uid, $id ); } else { \Dashbread\Notifications\Store::mark_all_read( $uid ); } return new \WP_REST_Response( array( 'unread' => \Dashbread\Notifications\Store::unread_count( $uid ) ) ); } public function me( \WP_REST_Request $r ): \WP_REST_Response { $u = wp_get_current_user(); return new \WP_REST_Response( array( 'id' => $u->ID, 'displayName' => $u->display_name, 'email' => $u->user_email, 'roles' => array_values( (array) $u->roles ), 'isStaff' => current_user_can( 'edit_posts' ), 'mode' => (string) ( get_user_meta( $u->ID, 'dashbread_mode', true ) ?: 'default' ), 'sidebar' => (string) ( get_user_meta( $u->ID, 'dashbread_sidebar', true ) ?: 'expanded' ), ) ); } /** Collapsed / hidden dashboard widgets for the current member. */ public function widgets( \WP_REST_Request $r ): \WP_REST_Response { $in = (array) $r->get_json_params(); $clean = static fn( $v ) => array_values( array_unique( array_filter( array_map( 'sanitize_key', (array) $v ) ) ) ); update_user_meta( get_current_user_id(), 'dashbread_widgets', array( 'collapsed' => $clean( $in['collapsed'] ?? array() ), 'hidden' => $clean( $in['hidden'] ?? array() ) ) ); return new \WP_REST_Response( \Dashbread\Shell\Shell::widget_state() ); } public function preferences( \WP_REST_Request $r ): \WP_REST_Response { $id = get_current_user_id(); if ( null !== $r->get_param( 'mode' ) ) { update_user_meta( $id, 'dashbread_mode', sanitize_key( (string) $r->get_param( 'mode' ) ) ); } if ( null !== $r->get_param( 'sidebar' ) ) { update_user_meta( $id, 'dashbread_sidebar', sanitize_key( (string) $r->get_param( 'sidebar' ) ) ); } return $this->me( $r ); } }