/**
  * Add/change custom fields to the Edit Media screen and Modal Window
  *
  * Called from /wp-admin/includes/media.php, function get_compat_media_markup();
  * If "get_media_item_args"['in_modal'] => false ) its the Edit Media screen.
  * If "get_media_item_args"['in_modal'] => true ) its the Media Manager Modal Window.
  * For the Modal Window, $form_fields contains all the "compat-attachment-fields"
  * including the taxonomies, which we want to enhance.
  * Declared public because it is a filter.
  *
  * @since 1.71
  *
  * @param	array	descriptors for the "compat-attachment-fields" 
  * @param	object	the post to be edited
  *
  * @return	array	updated descriptors for the "compat-attachment-fields"
  */
 public static function mla_attachment_fields_to_edit_filter($form_fields, $post)
 {
     $id = $post->ID;
     /*
      * This logic is only required for the MLA-enhanced Media Manager Modal Window.
      * For the non-Modal Media/Edit Media screen, the MLAEdit::mla_add_meta_boxes_action
      * function changes the default meta box to the MLA searchable meta box.
      */
     if (isset(self::$media_item_args['in_modal']) && self::$media_item_args['in_modal']) {
         foreach (get_taxonomies(array('show_ui' => true), 'objects') as $key => $value) {
             if (MLAOptions::mla_taxonomy_support($key)) {
                 if (isset($form_fields[$key])) {
                     $field = $form_fields[$key];
                 } else {
                     continue;
                 }
                 if (!($use_checklist = $value->hierarchical)) {
                     $use_checklist = MLAOptions::mla_taxonomy_support($key, 'flat-checklist');
                 }
                 /*
                  * Make sure the appropriate MMMW Enhancement option has been checked
                  */
                 if ($use_checklist) {
                     if ('checked' !== MLAOptions::mla_get_option(MLAOptions::MLA_MEDIA_MODAL_DETAILS_CATEGORY_METABOX)) {
                         continue;
                     }
                 } else {
                     if ('checked' !== MLAOptions::mla_get_option(MLAOptions::MLA_MEDIA_MODAL_DETAILS_TAG_METABOX)) {
                         continue;
                     }
                 }
                 /*
                  * Remove "Media Categories" meta box, if present.
                  */
                 if (isset($form_fields[$key . '_metabox'])) {
                     unset($form_fields[$key . '_metabox']);
                 }
                 /*
                  * Simulate the default MMMW text box with a hidden field;
                  * use term names for flat taxonomies and term_ids for hierarchical.
                  */
                 $post_id = $post->ID;
                 $label = $field['labels']->name;
                 $terms = get_object_term_cache($post_id, $key);
                 if (false === $terms) {
                     $terms = wp_get_object_terms($post_id, $key);
                     wp_cache_add($post_id, $terms, $key . '_relationships');
                 }
                 if (is_wp_error($terms) || empty($terms)) {
                     $terms = array();
                 }
                 $list = array();
                 foreach ($terms as $term) {
                     if ($value->hierarchical) {
                         $list[] = $term->term_id;
                     } else {
                         $list[] = $term->name;
                     }
                 }
                 // foreach $term
                 sort($list);
                 $list = join(',', $list);
                 $class = $value->hierarchical ? 'categorydiv' : 'tagsdiv';
                 $row = "\t\t<tr class='compat-field-{$key} mla-taxonomy-row' style='display: none'>\n";
                 $row .= "\t\t<th class='label' valign='top' scope='row'>\n";
                 $row .= "\t\t<label for='mla-attachments-{$post_id}-{$key}'>\n";
                 $row .= "\t\t<span title='" . __('Click to toggle', 'media-library-assistant') . "' class='alignleft'>{$label}</span><br class='clear'>\n";
                 $row .= "\t\t</label></th>\n";
                 $row .= "\t\t<td class='field'>\n";
                 $row .= "\t\t<div class='mla-taxonomy-field'>\n";
                 $row .= "\t\t<input name='mla_attachments[{$post_id}][{$key}]' class='text' id='mla-attachments-{$post_id}-{$key}' type='hidden' value='{$list}'>\n";
                 $row .= "\t\t<div id='mla-taxonomy-{$key}' class='{$class}'>\n";
                 $row .= '&lt;- ' . __('Click to toggle', 'media-library-assistant') . "\n";
                 $row .= "\t\t</div>\n";
                 $row .= "\t\t</div>\n";
                 $row .= "\t\t</td>\n";
                 $row .= "\t\t</tr>\n";
                 //$form_fields[ $key ] = array( 'tr' => $row );
                 $form_fields['mla-' . $key] = array('tr' => $row);
             }
             // is supported
         }
         // foreach
         $form_fields = apply_filters('mla_media_modal_form_fields', $form_fields, $post);
     }
     // in_modal
     self::$media_item_args = array('errors' => null, 'in_modal' => false);
     return $form_fields;
 }