How to hide formatting options in node forms - the right way!

How to hide formatting options for text fields in Drupal 7 is a topic which has been much discussed but from what I've seen, no clean solution has been presented so far. The most promising solution suggested that you change $form['body_field']['format']['#access'] in hook_form_alter(). However, for me this broke the default wysiwyg formatting and instead produced a plain text field. After some investigating, I finally came up with a solution that works and works well. I like to use permissions for customizability so in this solution we're going to use hook_permission(), hook_form_BASE_FORM_ID_alter() and $form['#after_build'] to hide the formatting options. This guide only covers node forms but the same principle can be applied to all forms. This solution removes the formatting options, guidelines and any trash div containers that remain without causing any nasty side effects. Because we use user_access() to determine whether the formatting options are shown, the administrator will always be able to see them.

/**
* Implements hook_permission().
*/
function MYMODULE_permission() {
  return array(
    'MYMODULE_nodeform_display_formatting' => array(
      'title' => t('Display formatting options for node fields'),
    ),
  );
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function MYMODULE_form_node_form_alter(&$form, $form_state) {
  //This places our functions at the very end of the form generation, making sure that the formatting options have been built
  $form['#after_build'][] = 'MYMODULE_node_form_after_build';
}
/**
* Callback for node form #after_build.
*/
function MYMODULE_node_form_after_build($form, &$form_state) {
  foreach(element_children($form) as $key) {
    $element_parent =& $form[$key];
    if(isset($element_parent['#language'])) {
      $language = $element_parent['#language'];
      if(is_array($element_parent[$language])) {
        foreach(element_children($element_parent[$language]) as $i) {
          $element =& $element_parent[$language][$i];
          if(isset($element['format'])) {
            if(!user_access('MYMODULE_nodeform_display_formatting')) {
              $element['format']['#theme_wrappers'] = array();
              if(isset($element['format']['guidelines'])) {
                $element['format']['guidelines']['#theme_wrappers'] = array();
              }
              if(isset($element['format']['help'])) {
                $element['format']['help']['#access'] = false;
              }
              if(isset($element['format']['format'])) {
                $default_value = $element['format']['format']['#default_value'];
                //Reduces the formatting options to the default value so that we can be certain the default formatting is used for the field
                $element['format']['format']['#options'] = array($default_value => $element['format']['format']['#options'][$default_value]);
                $element['format']['format']['#theme_wrappers'] = array();
                $element['format']['format']['#access'] = false;
              }
            }
          }
        }
      }
    }
  }
  return $form;
}