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