I think the if can be replaced with 
```
if (!$vsite = spaces_get_space()) {
  return;
}
```

because the query only executed when the user is in a vsite.