<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[23772] trunk/wp-includes/media.php: Add functions to extract images from posts in various forms</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/23772">23772</a></dd>
<dt>Author</dt> <dd>markjaquith</dd>
<dt>Date</dt> <dd>2013-03-22 05:20:38 +0000 (Fri, 22 Mar 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add functions to extract images from posts in various forms

props wonderboymusic. fixes <a href="http://core.trac.wordpress.org/ticket/22960">#22960</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesmediaphp">trunk/wp-includes/media.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesmediaphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/media.php (23771 => 23772)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/media.php        2013-03-21 23:23:27 UTC (rev 23771)
+++ trunk/wp-includes/media.php        2013-03-22 05:20:38 UTC (rev 23772)
</span><span class="lines">@@ -874,7 +874,7 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if ( ! $primary ) {
</span><del>-                $audios = get_post_audio( $post_id );
</del><ins>+                $audios = get_attached_audio( $post_id );
</ins><span class="cx">                 if ( empty( $audios ) )
</span><span class="cx">                         return;
</span><span class="cx"> 
</span><span class="lines">@@ -981,7 +981,7 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if ( ! $primary ) {
</span><del>-                $videos = get_post_video( $post_id );
</del><ins>+                $videos = get_attached_video( $post_id );
</ins><span class="cx">                 if ( empty( $videos ) )
</span><span class="cx">                         return;
</span><span class="cx"> 
</span><span class="lines">@@ -1781,7 +1781,7 @@
</span><span class="cx">  * @param int $post_id  Post ID
</span><span class="cx">  * @return array Found audio attachments
</span><span class="cx">  */
</span><del>-function get_post_audio( $post_id = 0 ) {
</del><ins>+function get_attached_audio( $post_id = 0 ) {
</ins><span class="cx">         $post = empty( $post_id ) ? get_post() : get_post( $post_id );
</span><span class="cx">         if ( empty( $post ) )
</span><span class="cx">                 return;
</span><span class="lines">@@ -1805,7 +1805,7 @@
</span><span class="cx">  * @param int $post_id  Post ID
</span><span class="cx">  * @return array Found video attachments
</span><span class="cx">  */
</span><del>-function get_post_video( $post_id = 0 ) {
</del><ins>+function get_attached_video( $post_id = 0 ) {
</ins><span class="cx">         $post = empty( $post_id ) ? get_post() : get_post( $post_id );
</span><span class="cx">         if ( empty( $post ) )
</span><span class="cx">                 return;
</span><span class="lines">@@ -1824,6 +1824,8 @@
</span><span class="cx"> /**
</span><span class="cx">  * Audio embed handler callback.
</span><span class="cx">  *
</span><ins>+ * @since 3.6.0
+ *
</ins><span class="cx">  * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}.
</span><span class="cx">  * @param array $attr Embed attributes.
</span><span class="cx">  * @param string $url The original URL that was matched by the regex.
</span><span class="lines">@@ -1841,6 +1843,8 @@
</span><span class="cx"> /**
</span><span class="cx">  * Video embed handler callback.
</span><span class="cx">  *
</span><ins>+ * @since 3.6.0
+ *
</ins><span class="cx">  * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}.
</span><span class="cx">  * @param array $attr Embed attributes.
</span><span class="cx">  * @param string $url The original URL that was matched by the regex.
</span><span class="lines">@@ -1860,3 +1864,226 @@
</span><span class="cx">         return apply_filters( 'wp_video_embed', $video, $attr, $url, $rawattr );
</span><span class="cx"> }
</span><span class="cx"> wp_embed_register_handler( 'wp_video_embed', '#https?://.+?\.(' . join( '|', wp_get_video_extensions() ) . ')#i', apply_filters( 'wp_video_embed_handler', 'wp_video_embed' ), 9999 );
</span><ins>+
+/**
+ * Retrieve images attached to the passed post
+ *
+ * @since 3.6.0
+ *
+ * @param int $post_id Optional. Post ID.
+ * @return array Found image attachments
+ */
+function get_attached_images( $post_id = 0 ) {
+        $post = empty( $post_id ) ? get_post() : get_post( $post_id );
+        if ( empty( $post ) )
+                return array();
+
+        $children = get_children( array(
+                'post_parent' =&gt; $post-&gt;ID,
+                'post_type' =&gt; 'attachment',
+                'post_mime_type' =&gt; 'image',
+                'posts_per_page' =&gt; -1,
+                'orderby' =&gt; 'menu_order',
+                'order' =&gt; 'ASC'
+        ) );
+
+        if ( ! empty( $children ) )
+                return $children;
+
+        return array();
+}
+
+/**
+ * Retrieve images attached to the passed post
+ *
+ * @since 3.6.0
+ *
+ * @param int $post_id Optional. Post ID.
+ * @return array Found image attachments
+ */
+function get_attached_image_srcs( $post_id = 0 ) {
+        $children = get_attached_images( $post_id );
+        if ( empty( $children ) )
+                return array();
+
+        $srcs = array();
+        foreach ( $children as $attachment )
+                $srcs[] = wp_get_attachment_url( $attachment-&gt;ID );
+
+        return $srcs;
+}
+
+/**
+ * Check the content blob for image srcs
+ *
+ * @since 3.6.0
+ *
+ * @param string $content A string which might contain image data.
+ * @param boolean $remove Whether to remove the found data from the passed content.
+ * @param int $limit Optional. The number of image srcs to return
+ * @return array The found image srcs
+ */
+function get_content_images( &amp;$content, $remove = false, $limit = 0 ) {
+        $src = '';
+        $srcs = array();
+        $matches = array();
+
+        if ( $remove &amp;&amp; preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) &amp;&amp; ! empty( $matches ) ) {
+                $captions = array();
+                foreach ( $matches as $shortcode ) {
+                        if ( 'caption' === $shortcode[2] )
+                                $captions[] = $shortcode[0];
+                }
+        }
+
+        if ( preg_match_all( '#&lt;img[^&gt;]+/?&gt;#i', $content, $matches, PREG_SET_ORDER ) &amp;&amp; ! empty( $matches ) ) {
+                foreach ( $matches as $tag ) {
+                        $count = 1;
+                        if ( $remove ) {
+                                foreach ( $captions as $caption ) {
+                                        if ( strstr( $caption, $tag[0] ) ) {
+                                                $content = str_replace( $caption, '', $content, $count );
+                                        }
+                                }
+
+                                $content = str_replace( $tag[0], '', $content, $count );
+                        }
+
+                        preg_match( '#src=[\'&quot;](.+?)[\'&quot;]#is', $tag[0], $src );
+                        if ( ! empty( $src[1] ) ) {
+                                $srcs[] = $src[1];
+                                if ( $limit &gt; 0 &amp;&amp; count( $srcs ) &gt;= $limit )
+                                        break;
+                        }
+                }
+        }
+
+        return array_values( array_unique( $srcs ) );
+}
+
+/**
+ * Check the content blob for image srcs and return the first
+ *
+ * @since 3.6.0
+ *
+ * @param string $content A string which might contain image data.
+ * @param boolean $remove Whether to remove the found data from the passed content.
+ * @return string The found data
+ */
+function get_content_image( &amp;$content, $remove = false ) {
+        $srcs = get_content_images( $content, $remove, 1 );
+        if ( empty( $srcs ) )
+                return '';
+
+        return reset( $srcs );
+}
+
+/**
+ * Check the content blob for galleries and return their image srcs
+ *
+ * @since 3.6.0
+ *
+ * @param string $content A string which might contain image data.
+ * @param boolean $remove Optional. Whether to remove the found data from the passed content.
+ * @param int $limit Optional. The number of galleries to return
+ * @return array A list of galleries, which in turn are a list of their srcs in order
+ */
+function get_content_galleries( &amp;$content, $remove = false, $limit = 0 ) {
+        $src = '';
+        $galleries = array();
+        $matches = array();
+
+        if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) &amp;&amp; ! empty( $matches ) ) {
+                foreach ( $matches as $shortcode ) {
+                        if ( 'gallery' === $shortcode[2] ) {
+                                $srcs = array();
+                                $count = 1;
+                                if ( $remove )
+                                        $content = str_replace( $shortcode[0], '', $content, $count );
+
+                                $data = shortcode_parse_atts( $shortcode[3] );
+                                $gallery = do_shortcode_tag( $shortcode );
+                                preg_match_all( '#src=[\'&quot;](.+?)[\'&quot;]#is', $gallery, $src, PREG_SET_ORDER );
+                                if ( ! empty( $src ) ) {
+                                        foreach ( $src as $s )
+                                                $srcs[] = $s[1];
+                                }
+
+                                $data['src'] = array_values( array_unique( $srcs ) );
+                                $galleries[] = $data;
+                                if ( $limit &gt; 0 &amp;&amp; count( $galleries ) &gt;= $limit )
+                                        break;
+                        }
+                }
+        }
+
+        return $galleries;
+}
+
+/**
+ * Retrieve galleries from the passed post's content
+ *
+ * @since 3.6.0
+ *
+ * @param int $post_id Optional. Post ID.
+ * @return array A list of arrays, each containing gallery data and srcs parsed
+ *                from the expanded shortcode
+ */
+function get_post_galleries( $post_id = 0 ) {
+        $post = empty( $post_id ) ? clone get_post() : get_post( $post_id );
+        if ( empty( $post ) || ! has_shortcode( $post-&gt;post_content, 'gallery' )  )
+                return array();
+
+        return get_content_galleries( $post-&gt;post_content );
+}
+
+/**
+ * Retrieve the image srcs from galleries from a post's content, if present
+ *
+ * @since 3.6.0
+ *
+ * @param int $post_id Optional. Post ID.
+ * @return array A list of lists, each containing image srcs parsed
+ *                from an expanded shortcode
+ */
+function get_post_galleries_images( $post_id = 0 ) {
+        $post = empty( $post_id ) ? clone get_post() : get_post( $post_id );
+        if ( empty( $post ) || ! has_shortcode( $post-&gt;post_content, 'gallery' )  )
+                return array();
+
+        $data = get_content_galleries( $post-&gt;post_content );
+        return wp_list_pluck( $data, 'src' );
+}
+
+/**
+ * Check a specified post's content for gallery and, if present, return the first
+ *
+ * @since 3.6.0
+ *
+ * @param int $post_id Optional. Post ID.
+ * @return array Gallery data and srcs parsed from the expanded shortcode
+ */
+function get_post_gallery( $post_id = 0 ) {
+        $post = empty( $post_id ) ? clone get_post() : get_post( $post_id );
+        if ( empty( $post ) || ! has_shortcode( $post-&gt;post_content, 'gallery' ) )
+                return array();
+
+        $data = get_content_galleries( $post-&gt;post_content, false, 1 );
+        return reset( $data );
+}
+
+/**
+ * Check a post's content for galleries and return the image srcs for the first found gallery
+ *
+ * @since 3.6.0
+ *
+ * @param int $post_id Optional. Post ID.
+ * @return array A list of a gallery's image srcs in order
+ */
+function get_post_gallery_images( $post_id = 0 ) {
+        $gallery = get_post_gallery( $post_id );
+        if ( empty( $gallery['src'] ) )
+                return array();
+
+        return $gallery['src'];
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>

</body>
</html>