A beforeQuery method is a good start but I feel there's potentially a lot of benefit in factoring out the bits inside the local/remote bifurcation, that's where most of the patching tends to happen. Coming up with a decent API for that without exposing too many implementation details may be tricky though.

Nested 'ifs' four levels deep followed by a massive bifurcation... just the indentation waveform is enough to set alarm bells ringing, you don't even have to read the code.