Ticket #23593: 23593.diff

File 23593.diff, 4.9 KB (added by wonderboymusic, 3 months ago)
Line 
1diff --git wp-includes/functions.php wp-includes/functions.php
2index cbd7925..a88c843 100644
3--- wp-includes/functions.php
4+++ wp-includes/functions.php
5@@ -3883,4 +3883,24 @@ function wp_is_stream( $path ) {
6  */
7 function wp_checkdate( $month, $day, $year, $source_date ) {
8        return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date );
9+}
10+
11+/**
12+ * Return RegEx body to liberally match an opening HTML tag that:
13+ * 1. Is self-closing or
14+ * 2. Has no body but has a closing tag of the same name or
15+ * 3. Contains a body and a closing tag of the same name
16+ *
17+ * Note: this RegEx does not balance inner tags and does not attempt to produce valid HTML
18+ *
19+ * @since 3.6.0
20+ *
21+ * @param string $tag An HTML tag name. Example: 'video'
22+ * @return string
23+ */
24+function get_tag_regex( $tag ) {
25+    if ( empty( $tag ) )
26+        return;
27+
28+    return sprintf( '(<%1$s[^>]*(?:/?>$|>[\s\S]*?</%1$s>))', tag_escape( $tag ) );
29 }
30\ No newline at end of file
31diff --git wp-includes/media.php wp-includes/media.php
32index f1f3737..c90d802 100644
33--- wp-includes/media.php
34+++ wp-includes/media.php
35@@ -1542,3 +1542,105 @@ function wp_enqueue_media( $args = array() ) {
36 
37        do_action( 'wp_enqueue_media' );
38 }
39+
40+/**
41+ * Retrieve images attached to the passed post
42+ *
43+ * @since 3.6.0
44+ *
45+ * @param int $post_id  Post ID
46+ * @return array Found image attachments
47+ */
48+function get_post_images( $post_id = 0 ) {
49+       $post = empty( $post_id ) ? get_post() : get_post( $post_id );
50+       if ( empty( $post ) )
51+               return;
52+
53+       $children = get_children( array(
54+               'post_parent' => $post->ID,
55+               'post_type' => 'attachment',
56+               'post_mime_type' => 'image',
57+               'posts_per_page' => -1,
58+               'orderby' => 'menu_order',
59+               'order' => 'ASC'
60+       ) );
61+
62+       if ( ! empty( $children ) )
63+               return $children;
64+}
65+
66+/**
67+ * Check the content blob for an <img>
68+ * If no HTML tag is found, check the first line of the post for a URL
69+ *
70+ * @param string $content A string which might contain image data.
71+ * @param boolean $remove Whether to remove the found data from the passed content.
72+ * @return string The found data
73+ */
74+function get_content_image( &$content, $remove = false ) {
75+       $matches = $match = $src = '';
76+
77+       if ( preg_match( '/' . get_shortcode_regex() . '/s', $content, $match ) && 'gallery' == $match[2] ) {
78+               $count = 1;
79+               if ( $remove )
80+                       $content = str_replace( $match[0], '', $content, $count );
81+
82+               $gallery = do_shortcode_tag( $match );
83+               preg_match( '#src=[\'"](.+?)[\'"]#is', $gallery, $src );
84+               if ( ! empty( $src[1] ) )
85+                       return $src[1];
86+       }
87+
88+       if ( preg_match( '#' . get_tag_regex( 'img' ) . '#i', $content, $matches ) ) {
89+               $count = 1;
90+               if ( $remove )
91+                       $content = str_replace( $matches[0], '', $content, $count );
92+
93+               preg_match( '#src=[\'"](.+?)[\'"]#is', $matches[1], $src );
94+               if ( ! empty( $src[1] ) )
95+                       return $src[1];
96+       }
97+
98+       $lines = explode( "\n", trim( $content ) );
99+       $line = trim( array_shift( $lines  ) );
100+
101+       if ( 0 === stripos( $line, 'http' ) ) {
102+               if ( $remove )
103+                       $content = join( "\n", $lines );
104+
105+               return $line;
106+       }
107+}
108+
109+/**
110+ * Return the found image data for the passed post
111+ *
112+ * @since 3.6.0
113+ *
114+ * @param int $id Optional. Post ID
115+ */
116+function get_the_image( $id = 0 ) {
117+       $post = empty( $id ) ? get_post() : get_post( $id );
118+       if ( empty( $post ) )
119+               return '';
120+
121+       $data = get_content_image( $post->post_content );
122+       if ( ! empty( $data ) )
123+               return $data;
124+
125+       $images = get_post_images( $post->ID );
126+       if ( empty( $images ) )
127+               return '';
128+
129+       $image = reset( $images );
130+       return wp_get_attachment_url( $image->ID );
131+}
132+
133+/**
134+ * Output the found image data for the current post
135+ *
136+ * @since 3.6.0
137+ */
138+function the_image() {
139+       echo apply_filters( 'the_image', get_the_image() );
140+}
141\ No newline at end of file
142diff --git wp-includes/post-formats.php wp-includes/post-formats.php
143index 6d32aea..f971627 100644
144--- wp-includes/post-formats.php
145+++ wp-includes/post-formats.php
146@@ -364,6 +364,26 @@ function post_formats_compat( $content, $id = 0 ) {
147                                                );
148                                        }
149                                }
150+                       } else {
151+                               $data = get_content_image( $content, true );
152+                               $img_class = empty( $compat['image_class'] ) ? '' : sprintf( 'class="%s" ', esc_attr( $compat['image_class'] ) );
153+                               if ( ! empty( $data ) && 0 === stripos( $data, 'http' ) ) {
154+                                       if ( empty( $meta['url'] ) ) {
155+                                               $sformat = '<img %ssrc="%s"/>';
156+                                               $format_output .= sprintf( $sformat, $img_class, esc_url( $data ) );
157+                                       } else {
158+                                               $sformat = '<a href="%s"><img %ssrc="%s"/></a>';
159+                                               $format_output .= sprintf( $sformat, esc_url( $meta['url'] ), $img_class, esc_url( $data ) );
160+                                       }
161+                               } else {
162+                                       $images = get_post_images( $post->ID );
163+                                       if ( ! empty( $images ) ) {
164+                                               $image = reset( $images );
165+                                               $url = wp_get_attachment_url( $image->ID );
166+                                               $sformat = '<img %ssrc="%s"/>';
167+                                               $format_output .= sprintf( $sformat, $img_class, esc_url( $url ) );
168+                                       }
169+                               }
170                        }
171                        break;
172