namespace, '/notifications/dismiss', [ 'methods' => WP_REST_Server::CREATABLE, 'callback' => [ $this, 'dismiss' ], 'permission_callback' => [ $this, 'permissions_check' ], 'args' => [ 'slug' => [ 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_key', 'validate_callback' => static fn( $slug ) => is_string( $slug ) && '' !== sanitize_key( $slug ), ], ], ] ); } /** * Notices belong to a user, so every logged-in user may hide their own. * * @return bool|WP_Error */ public function permissions_check(): bool|WP_Error { if ( ! is_user_logged_in() ) { return new WP_Error( 'rest_forbidden', __( 'You need to be logged in to dismiss notices.', 'adpresso' ), [ 'status' => rest_authorization_required_code() ] ); } return true; } /** * Dismisses the notice for the current user. * * @param WP_REST_Request $request Request. * * @return WP_REST_Response */ public function dismiss( WP_REST_Request $request ): WP_REST_Response { Notification_Center::get()->dismiss( (string) $request->get_param( 'slug' ), get_current_user_id(), '9999-12-31 23:59:59' ); return new WP_REST_Response( [ 'success' => true ], 200 ); } }