function add_media_credits_to_end($content)
{
    // Find the attachment_IDs of all media used in $content
    preg_match_all('/' . WP_IMAGE_CLASS_NAME_PREFIX . '(\\d+)/', $content, $matches);
    $images = $matches[1];
    if (count($images) == 0) {
        return $content;
    }
    $credit_unique = array();
    foreach ($images as $image) {
        $credit_unique[] = get_media_credit_html($image);
    }
    $credit_unique = array_unique($credit_unique);
    $image_credit = (count($images) > 1 ? 'Images' : 'Image') . ' courtesy of ';
    $count = 0;
    foreach ($credit_unique as $credit) {
        if ($count > 0) {
            if ($count < count($credit_unique) - 1) {
                $image_credit .= ', ';
            } else {
                $image_credit .= __(' and ');
            }
        }
        $image_credit .= $credit;
        $count++;
    }
    return $content . '<div class="media-credit-end">' . $image_credit . '</div>';
}
 function do_tagged_text($post)
 {
     //You can set a format to define how fields are mapped
     $format = get_post_meta($post->ID, '_format', TRUE);
     //Check for a nonsystem byline
     $byline = get_post_meta($post->ID, '_byline', TRUE);
     //If there's not a nonsystem byline, use coauthors
     if (empty($byline) && function_exists('get_coauthors')) {
         //Get the WordPress users attached to the post
         $authors = get_coauthors($post->ID);
         $byline = '';
         //Go through each author
         foreach ($authors as $authorentry) {
             $byline .= $authorentry->display_name;
             if ($authorentry != end($authors)) {
                 $byline .= ' and ';
             } else {
                 //At the end, we append the user's title (BDN Staff, Special to the BDN, etc.)
                 //This doesn't take into account when a post has multiple authors with different titles
                 $byline_title = get_user_meta($authorentry->ID, '_staff_title', TRUE);
                 if (!empty($byline_title)) {
                     $byline .= ', ' . $byline_title;
                 }
             }
         }
     } elseif (empty($byline)) {
         $author = get_user_by('id', $post->post_author);
         $byline_title = get_user_meta($post->post_author, '_staff_title', TRUE);
         $byline = $author->display_name;
         if (!empty($byline_title)) {
             $byline .= ', ' . $byline_title;
         }
     }
     //Try to turn special chars, etc into unicode
     $copy = htmlspecialchars_decode(html_entity_decode($post->post_content));
     //Get rid of youtube embeds
     //@TODO: Strip out all shortcodes
     $copy = preg_replace('/\\[youtube=(.*?)>\\]/', '', $copy);
     //Strip tags we don't need
     $copy = strip_tags($copy, '<p><br><b><strong><li><i><em><h4><h3><h5>');
     //Get rid of all classes and IDs
     $copy = preg_replace('/<p(.*?)>/', '<p>', $copy);
     $copy = preg_replace('/<h4(.*?)>/', '<h4>', $copy);
     $copy = preg_replace('/<h3(.*?)>/', '<h3>', $copy);
     $copy = preg_replace('/<h5(.*?)>/', '<h5>', $copy);
     //Convert our HTML tags to InDesign tags
     //<ct:Bold> is a character tag. This will need to match the character tags for your particular font
     //<ct:> sets the text back to the default style
     $original = array('/<\\/p>/', '/<li>/', '/<br><br>/', '/<p>/', '/<b>/', '/<strong>/', '/<\\/b>/', '/<\\/strong>/', '/<i>/', '/<em>/', '/<\\/i>/', '/<\\/em>/');
     $replacements = array('', "\r\n", "\r\n", "\r\n", '<ct:Bold>', '<ct:Bold>', '<ct:>', '<ct:>', '<ct:Italic>', '<ct:Italic>', '<ct:>', '<ct:>');
     $copy = preg_replace($original, $replacements, $copy);
     //Convert mdashes to unicode
     $copy = preg_replace(array('/&mdash;/', '/—/', '/--/'), '<0x2014>', $copy);
     //superscript lowercase a? That doesn't seem right.
     $copy = str_replace(chr(226) . chr(133) . chr(148), '<0x00AA>', $copy);
     //Convert smart quotes to unicode
     $smartquotes = array(chr(145), chr(146), chr(147), chr(148), chr(151));
     $smartquotesreplace = array('<0x2018>', '<0x2019>', '<0x201C>', '<0x201D>', '<0x2014>');
     $copy = str_replace($smartquotes, $smartquotesreplace, $copy);
     //And straight quotes
     $copy = str_replace(array('&#39;', '&#34;'), array('\'', '"'), $copy);
     $copy = str_replace(array('&#039;', '&#034;'), array('\'', '"'), $copy);
     //Curly quotes to unicode
     $quotesoriginal = array('/‘/', '/&lsquo;/', '/’/', '/&rsquo;/', '/“/', '/&ldquo;/', '/”/', '/&rdquo;/', '/…/', '/&hellip;/');
     $quotesreplace = array('<0x2018>', '<0x2018>', '<0x2019>', '<0x2019>', '<0x201C>', '<0x201C>', '<0x201D>', '<0x201D>', '<0x2026>', '<0x2026>');
     $copy = preg_replace($quotesoriginal, $quotesreplace, $copy);
     //Convert fractions to unicode
     //					1/4			1/2			3/4
     $fractions = array(chr(188), chr(189), chr(190));
     $fractionsreplace = array('<0x00BC>', '<0x00BD>', '<0x00BE>');
     $copy = str_replace($fractions, $fractionsreplace, $copy);
     //Strip out Maine in the datelines
     $copy = preg_replace('/, Maine <0x2014>/', ' <0x2014>', $copy);
     //Do some voodoo to standardize line breaks
     $copy = preg_replace('/<br>/', '<br />', $copy);
     $copy = preg_replace('|<br />\\s*<br />|', "\n", $copy);
     $copy = preg_replace('/<br \\/>/', "\n", $copy);
     $copy = str_replace(array("\r\n", "\r"), "\n", $copy);
     $copy = preg_replace("/\n\n+/", "\n", $copy);
     $copy = str_replace('\\n\\n', '\\n', $copy);
     $copy = preg_replace('/\\n /', "\n", $copy);
     $copy = preg_replace('/\\n/', "\r\n", $copy);
     //Convert spaces to spaces and remove extras
     $copy = str_replace('&nbsp;', ' ', $copy);
     $copy = preg_replace("/ +/", " ", $copy);
     //Convert headers to paragraph styles
     //And on end, convert back to body text
     $copy = str_replace('<h3>', "\r\n<pstyle:Spot brief head>", $copy);
     $copy = str_replace('</h3>', "\r\n<pstyle:Body Text>", $copy);
     $copy = str_replace('<h4>', "\r\n<pstyle:Text subhead>", $copy);
     $copy = str_replace('</h4>', "\r\n<pstyle:Body Text>", $copy);
     $copy = str_replace('</h5>', "\r\n<pstyle:Heading>", $copy);
     $copy = str_replace('</h5>', "\r\n<pstyle:Body Text>", $copy);
     //Trim the copy, explode it and trim each graf
     $copy = trim($copy);
     $exploosh = explode("\r\n", $copy);
     $trimmed = array();
     foreach ($exploosh as $graf) {
         if (empty($graf)) {
             continue;
         }
         $trimmed[] = trim($graf);
     }
     //Then implode it and replace shits with shits
     $copy = implode("\r\n", $trimmed);
     //Take care of multiple carriage returns
     $copy = preg_replace("/\n\n+/", "\n", $copy);
     //With \r\n line endings (needed on Windows), tell Indesign it's a Windows tagged text file
     $string = "<ASCII-WIN>\r\n";
     //If there are images attached to the post, grab the credit and cutline for each image
     //Image functions available at https://github.com/bangordailynews/BDN-image-functions
     if (function_exists('bdn_has_images')) {
         if (bdn_has_images($post->ID)) {
             $images = bdn_get_images($post->ID);
             if ($images) {
                 foreach ($images as $image) {
                     $meta = wp_get_attachment_metadata($image->ID);
                     //Integreates with Scott Bressler's media credit
                     if (function_exists('get_media_credit_html')) {
                         $credit = get_media_credit_html($image->ID);
                     }
                     //We use a photo system called Merlin, and print out the ID of the photo if it exists
                     if ($merlin_id = get_post_meta($image->ID, '_merlin_id', TRUE)) {
                         $identifier = 'Merlin ID: ' . $merlin_id;
                     } elseif (($photo_file = wp_get_attachment_image_src($image->ID, 'full')) && !empty($photo_file) && is_array($photo_file)) {
                         $identifier = 'Filename: ' . basename(reset($photo_file));
                     }
                     $string .= '<pstyle:Cut credit>' . $identifier . "\r\n";
                     $string .= '<pstyle:Cut credit>' . strip_tags($credit) . "\r\n";
                     $string .= '<pstyle:Cut>' . html_entity_decode(strip_tags($image->post_excerpt)) . "\r\n\r\n";
                 }
                 // Foreach loop
             }
             // If Images
         }
     }
     // If function exists
     list($byline1, $byline2) = explode(',', $byline);
     if (!empty($byline) && $byline1 == 'Reuters') {
         $string .= '<pstyle:Byline 2>' . trim($byline1) . "\r\n";
     } elseif (!empty($byline)) {
         $string .= "<pstyle:Byline 1>By " . trim($byline1) . "\r\n";
         if ($byline2) {
             $string .= "<pstyle:Byline 2>" . trim($byline2) . "\r\n";
         }
     }
     //Remove extra carriage returns
     $string .= "<pstyle:Body Text>" . $copy;
     $string = str_replace("<pstyle:Body Text>\r\n", '<pstyle:Body Text>', $string);
     $string = str_replace("<pstyle:Body Text>\r", '<pstyle:Body Text>', $string);
     $string = str_replace('<pstyle:Body Text><pstyle:', '<pstyle:', $string);
     //Convert paragraph styles if this is a different format
     if (trim(strip_tags($format)) == 'spagate') {
         //This is an example of a nested style
         $string = str_replace('<pstyle:Body Text>', '<pstyle:Sports\\:Sports agate>', $string);
         $string = str_replace('<pstyle:Spot brief head>', "\r\n\r\n<pstyle:Sports\\:Sports agate reverse head>", $string);
         $string = str_replace('<pstyle:Text subhead>', '<pstyle:Sports\\:Sports agate head>', $string);
         $string = str_replace('<ct:Bold>', '<ct:75 Bold>', $string);
         $string = str_replace('<ct:>', '<ct:>', $string);
     }
     //Again, extra carriage returns
     $string = str_replace("\r\n\r\n<pstyle:Text subhead>", "\r\n<pstyle:Text subhead>", $string);
     return $string;
 }