Is there some sort of injury mechanism built into the cap where we can recoup some of the money given to Stanley since he hasn't played in basically two seasons?
Really depends. Most contracts have injury guarantees, meaning the player gets his money fully if he's injured.
From what I can tell, his 2022 salary is fully guaranteed, which is $9.5M. So seems likely he gets that regardless of whether he plays or not.
The only thing that would change this is IF he were to retire. In those cases, there's some "wiggle room" for franchises to try to claw back prorated portions of signing bonuses. My guess is the Ravens wouldn't do that, however. We tend to only see that when an otherwise healthy player opts to retire (ala Calvin Johnson).
IF Stanley is unable to ever play again, I would imagine it'll be 2023 before that decision is made to retire. In which case, Stanley's compensation and cap hit ($9.5M he's owed, with a $18.5M cap hit) for 2022 are really unchanged by whether he plays or doesn't play.
Assuming he would then retire before 2023 starts, the Ravens would be off the hook for the remaining $29.5M in salary they owe him for 2023-2025, but would still have a dead money cap hit in 2023 of ~$22.6M. The only thing I don't know is whether the Ravens could spread that out over two years, much like a post-June 1 cut.
Bottom line... his 2022 cap hit and compensation basically won't be impacted unless he opts to retire, which I doubt he will. And 2023 is a total wildcard until we see what happens in 2022.