Refactor duplicate code

Lots of duplicate code between SugarCRM and Wordpress. Adding Drupal
will make this worse, so clean it up now :)

cc @ptarjan @oyamauchi
as I've got others stacked on this and github is a pain, going to land as-is and put up a corrective PR