Abstract the logic to a general function.

Wrap that function with `if ( ! function_exists() )` logic in case it
gets rolled into core at some point.