function pugpig_get_attachments($post) { $output = ""; $featured_image_id = get_post_thumbnail_id($post->ID); if (isset($featured_image_id) && !empty($featured_image_id)) { $output .= "# Featured image\n"; $output .= pugpig_strip_domain(wp_get_attachment_url($featured_image_id)) . "\n"; $output .= "\n"; } $added = array(); $args = array('post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID); $attachments = get_posts($args); $output .= "# Post Attachments (" . count($attachments) . " original files, maybe not all used)\n"; $post_body = $post->post_content; $post_body = apply_filters('the_content', $post_body); foreach ($attachments as $attachment) { if (wp_attachment_is_image($attachment->ID)) { // For each attachment, we find all the possible sizes // Then we parse the body and include all those that are used $output .= "# Checking: " . pugpig_strip_domain(wp_get_attachment_url($attachment->ID)) . "\n"; $image_urls = array(); foreach (array('thumbnail', 'medium', 'large', 'full') as $size) { $image_info = wp_get_attachment_image_src($attachment->ID, $size); $image_urls[] = $image_info[0]; } $found_image = false; foreach (array_unique($image_urls) as $img) { if (strstr($post_body, $img)) { $output .= "# Image found in body text\n"; $output .= pugpig_get_inline_image_urls(pugpig_strip_domain($img)) . "\n"; $added[] = pugpig_get_inline_image_urls(pugpig_strip_domain($img)); $found_image = true; } } } else { // Not an image. Just include it $output .= pugpig_strip_domain(wp_get_attachment_url($attachment->ID)) . "\n"; } } // Find any image URLS in the main body that are from // our domain but not in the manifest $image_urls = pugpig_get_image_urls($post_body); $base = pugpig_get_current_base_url(); foreach ($image_urls as $i) { if (startsWith($i, $base) && !in_array(pugpig_strip_domain($i), $added)) { $output .= "# Extra absolute on our domain in markup " . pugpig_get_inline_image_urls($i) . "\n"; $output .= pugpig_get_inline_image_urls(pugpig_strip_domain($i)) . "\n"; } elseif (startsWith($i, "/")) { $output .= "# Extra relative in markup " . $i . "\n"; $output .= pugpig_get_inline_image_urls($i) . "\n"; } else { $output .= "# Rejecting: " . pugpig_get_inline_image_urls($i) . "\n"; } } return $output; }
function pugpig_add_inline_images_to_manifest($markup, $prefix, $base = '') { $fixed_markup = pugpig_replace_image_urls($markup, $prefix, $base, false); $images = pugpig_get_image_urls($fixed_markup); $ret = "\n# Adding images found in the body\n"; $ret .= implode("\n", $images) . "\n\n"; return $ret; }