namespace, '/notifications', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_items' ), 'permission_callback' => array( $this, 'permission_logged_in' ), 'args' => array( 'page' => array( 'type' => 'integer', 'default' => 1, 'minimum' => 1, ), 'per_page' => array( 'type' => 'integer', 'default' => 20, 'minimum' => 1, 'maximum' => 50, ), 'unread' => array( 'type' => 'boolean', 'default' => false, ), ), ), ) ); register_rest_route( $this->namespace, '/notifications/count', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_count' ), 'permission_callback' => array( $this, 'permission_logged_in' ), ), ) ); register_rest_route( $this->namespace, '/notifications/read', array( array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'mark_read' ), 'permission_callback' => array( $this, 'permission_logged_in' ), 'args' => array( 'ids' => array( 'type' => 'array', 'items' => array( 'type' => 'integer' ), ), 'all' => array( 'type' => 'boolean', 'default' => false, ), ), ), ) ); } /** * Lists notifications. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function get_items( $request ) { $result = Notifications::for_user( get_current_user_id(), $request['page'], $request['per_page'], (bool) $request['unread'] ); return $this->paged_response( $result['items'], $result['total'], $request['per_page'] ); } /** * Returns the unread count. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function get_count( $request ) { unset( $request ); return rest_ensure_response( array( 'unread' => Notifications::unread_count( get_current_user_id() ) ) ); } /** * Marks some or all notifications as read. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function mark_read( $request ) { Notifications::mark_read( get_current_user_id(), $request['all'] ? null : (array) $request['ids'] ); return $this->get_count( $request ); } }