These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Class Jetpack_Media_Summary |
||
4 | * |
||
5 | * embed [video] > gallery > image > text |
||
6 | */ |
||
7 | class Jetpack_Media_Summary { |
||
8 | |||
9 | static function get( $post_id, $blog_id = 0, $args = array() ) { |
||
10 | $defaults = array( |
||
11 | 'max_words' => 16, |
||
12 | 'max_chars' => 256, |
||
13 | ); |
||
14 | $args = wp_parse_args( $args, $defaults ); |
||
15 | |||
16 | $switched = false; |
||
17 | if ( !empty( $blog_id ) && $blog_id != get_current_blog_id() && function_exists( 'switch_to_blog' ) ) { |
||
18 | switch_to_blog( $blog_id ); |
||
19 | $switched = true; |
||
20 | } else { |
||
21 | $blog_id = get_current_blog_id(); |
||
22 | } |
||
23 | |||
24 | $post = get_post( $post_id ); |
||
25 | $permalink = get_permalink( $post_id ); |
||
26 | |||
27 | $return = array( |
||
28 | 'type' => 'standard', |
||
29 | 'permalink' => $permalink, |
||
30 | 'image' => '', |
||
31 | 'excerpt' => '', |
||
32 | 'word_count' => 0, |
||
33 | 'secure' => array( |
||
34 | 'image' => '', |
||
35 | ), |
||
36 | 'count' => array( |
||
37 | 'image' => 0, |
||
38 | 'video' => 0, |
||
39 | 'word' => 0, |
||
40 | 'link' => 0, |
||
41 | ), |
||
42 | ); |
||
43 | |||
44 | if ( empty( $post->post_password ) ) { |
||
45 | $return['excerpt'] = self::get_excerpt( $post->post_content, $post->post_excerpt, $args['max_words'], $args['max_chars'] ); |
||
46 | $return['count']['word'] = self::get_word_count( $post->post_content ); |
||
47 | $return['count']['word_remaining'] = self::get_word_remaining_count( $post->post_content, $return['excerpt'] ); |
||
48 | $return['count']['link'] = self::get_link_count( $post->post_content ); |
||
49 | } |
||
50 | |||
51 | $extract = Jetpack_Media_Meta_Extractor::extract( $blog_id, $post_id, Jetpack_Media_Meta_Extractor::ALL ); |
||
52 | |||
53 | if ( empty( $extract['has'] ) ) |
||
54 | return $return; |
||
55 | |||
56 | // Prioritize [some] video embeds |
||
57 | if ( !empty( $extract['has']['shortcode'] ) ) { |
||
58 | foreach ( $extract['shortcode'] as $type => $data ) { |
||
59 | switch ( $type ) { |
||
60 | case 'wpvideo': |
||
61 | if ( 0 == $return['count']['video'] ) { |
||
62 | $return['type'] = 'video'; |
||
63 | $return['video'] = esc_url_raw( 'http://s0.videopress.com/player.swf?guid=' . $extract['shortcode']['wpvideo']['id'][0] . '&isDynamicSeeking=true' ); |
||
64 | $return['image'] = self::get_video_poster( 'videopress', $extract['shortcode']['wpvideo']['id'][0] ); |
||
65 | $return['secure']['video'] = preg_replace( '@http://[^\.]+.videopress.com/@', 'https://v0.wordpress.com/', $return['video'] ); |
||
66 | $return['secure']['image'] = str_replace( 'http://videos.videopress.com', 'https://videos.files.wordpress.com', $return['image'] ); |
||
67 | } |
||
68 | $return['count']['video']++; |
||
69 | break; |
||
70 | case 'youtube': |
||
71 | if ( 0 == $return['count']['video'] ) { |
||
72 | $return['type'] = 'video'; |
||
73 | $return['video'] = esc_url_raw( 'http://www.youtube.com/watch?feature=player_embedded&v=' . $extract['shortcode']['youtube']['id'][0] ); |
||
74 | $return['image'] = self::get_video_poster( 'youtube', $extract['shortcode']['youtube']['id'][0] ); |
||
75 | $return['secure']['video'] = self::https( $return['video'] ); |
||
76 | $return['secure']['image'] = self::https( $return['image'] ); |
||
77 | } |
||
78 | $return['count']['video']++; |
||
79 | break; |
||
80 | case 'vimeo': |
||
81 | if ( 0 == $return['count']['video'] ) { |
||
82 | $return['type'] = 'video'; |
||
83 | $return['video'] = esc_url_raw( 'http://vimeo.com/' . $extract['shortcode']['vimeo']['id'][0] ); |
||
84 | $return['secure']['video'] = self::https( $return['video'] ); |
||
85 | |||
86 | $poster_image = get_post_meta( $post_id, 'vimeo_poster_image', true ); |
||
87 | View Code Duplication | if ( !empty( $poster_image ) ) { |
|
88 | $return['image'] = $poster_image; |
||
89 | $poster_url_parts = parse_url( $poster_image ); |
||
90 | $return['secure']['image'] = 'https://secure-a.vimeocdn.com' . $poster_url_parts['path']; |
||
91 | } |
||
92 | } |
||
93 | $return['count']['video']++; |
||
94 | break; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | } |
||
99 | |||
100 | if ( !empty( $extract['has']['embed'] ) ) { |
||
101 | foreach( $extract['embed']['url'] as $embed ) { |
||
102 | if ( preg_match( '/((youtube|vimeo|dailymotion)\.com|youtu.be)/', $embed ) ) { |
||
103 | if ( 0 == $return['count']['video'] ) { |
||
104 | $return['type'] = 'video'; |
||
105 | $return['video'] = 'http://' . $embed; |
||
106 | $return['secure']['video'] = self::https( $return['video'] ); |
||
107 | if ( false !== strpos( $embed, 'youtube' ) ) { |
||
108 | $return['image'] = self::get_video_poster( 'youtube', jetpack_get_youtube_id( $return['video'] ) ); |
||
109 | $return['secure']['image'] = self::https( $return['image'] ); |
||
110 | } else if ( false !== strpos( $embed, 'youtu.be' ) ) { |
||
111 | $youtube_id = jetpack_get_youtube_id( $return['video'] ); |
||
112 | $return['video'] = 'http://youtube.com/watch?v=' . $youtube_id . '&feature=youtu.be'; |
||
113 | $return['secure']['video'] = self::https( $return['video'] ); |
||
114 | $return['image'] = self::get_video_poster( 'youtube', jetpack_get_youtube_id( $return['video'] ) ); |
||
115 | $return['secure']['image'] = self::https( $return['image'] ); |
||
116 | } else if ( false !== strpos( $embed, 'vimeo' ) ) { |
||
117 | $poster_image = get_post_meta( $post_id, 'vimeo_poster_image', true ); |
||
118 | View Code Duplication | if ( !empty( $poster_image ) ) { |
|
119 | $return['image'] = $poster_image; |
||
120 | $poster_url_parts = parse_url( $poster_image ); |
||
121 | $return['secure']['image'] = 'https://secure-a.vimeocdn.com' . $poster_url_parts['path']; |
||
122 | } |
||
123 | } else if ( false !== strpos( $embed, 'dailymotion' ) ) { |
||
124 | $return['image'] = str_replace( 'dailymotion.com/video/','dailymotion.com/thumbnail/video/', $embed ); |
||
125 | $return['image'] = parse_url( $return['image'], PHP_URL_SCHEME ) === null ? 'http://' . $return['image'] : $return['image']; |
||
126 | $return['secure']['image'] = self::https( $return['image'] ); |
||
127 | } |
||
128 | |||
129 | } |
||
130 | $return['count']['video']++; |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | |||
135 | // Do we really want to make the video the primary focus of the post? |
||
136 | if ( 'video' == $return['type'] ) { |
||
137 | $content = wpautop( strip_tags( $post->post_content ) ); |
||
138 | $paragraphs = explode( '</p>', $content ); |
||
139 | $number_of_paragraphs = 0; |
||
140 | |||
141 | foreach ( $paragraphs as $i => $paragraph ) { |
||
142 | // Don't include blank lines as a paragraph |
||
143 | if ( '' == trim( $paragraph ) ) { |
||
144 | unset( $paragraphs[$i] ); |
||
145 | continue; |
||
146 | } |
||
147 | $number_of_paragraphs++; |
||
148 | } |
||
149 | |||
150 | $number_of_paragraphs = $number_of_paragraphs - $return['count']['video']; // subtract amount for videos.. |
||
151 | |||
152 | // More than 2 paragraph? The video is not the primary focus so we can do some more analysis |
||
153 | if ( $number_of_paragraphs > 2 ) |
||
154 | $return['type'] = 'standard'; |
||
155 | } |
||
156 | |||
157 | // If we don't have any prioritized embed... |
||
158 | if ( 'standard' == $return['type'] ) { |
||
159 | if ( ( ! empty( $extract['has']['gallery'] ) || ! empty( $extract['shortcode']['gallery']['count'] ) ) && ! empty( $extract['image'] ) ) { |
||
160 | //... Then we prioritize galleries first (multiple images returned) |
||
161 | $return['type'] = 'gallery'; |
||
162 | $return['images'] = $extract['image']; |
||
163 | foreach ( $return['images'] as $image ) { |
||
164 | $return['secure']['images'][] = array( 'url' => self::ssl_img( $image['url'] ) ); |
||
165 | $return['count']['image']++; |
||
166 | } |
||
167 | } else if ( ! empty( $extract['has']['image'] ) ) { |
||
168 | // ... Or we try and select a single image that would make sense |
||
169 | $content = wpautop( strip_tags( $post->post_content ) ); |
||
170 | $paragraphs = explode( '</p>', $content ); |
||
171 | $number_of_paragraphs = 0; |
||
172 | |||
173 | foreach ( $paragraphs as $i => $paragraph ) { |
||
174 | // Don't include 'actual' captions as a paragraph |
||
175 | if ( false !== strpos( $paragraph, '[caption' ) ) { |
||
176 | unset( $paragraphs[$i] ); |
||
177 | continue; |
||
178 | } |
||
179 | // Don't include blank lines as a paragraph |
||
180 | if ( '' == trim( $paragraph ) ) { |
||
181 | unset( $paragraphs[$i] ); |
||
182 | continue; |
||
183 | } |
||
184 | $number_of_paragraphs++; |
||
185 | } |
||
186 | |||
187 | $return['image'] = $extract['image'][0]['url']; |
||
188 | $return['secure']['image'] = self::ssl_img( $return['image'] ); |
||
189 | $return['count']['image']++; |
||
190 | |||
191 | if ( $number_of_paragraphs <= 2 && 1 == count( $extract['image'] ) ) { |
||
192 | // If we have lots of text or images, let's not treat it as an image post, but return its first image |
||
193 | $return['type'] = 'image'; |
||
0 ignored issues
–
show
|
|||
194 | } |
||
195 | } |
||
196 | } |
||
197 | |||
198 | if ( $switched ) { |
||
199 | restore_current_blog(); |
||
200 | } |
||
201 | |||
202 | return $return; |
||
203 | } |
||
204 | |||
205 | static function https( $str ) { |
||
206 | return str_replace( 'http://', 'https://', $str ); |
||
207 | } |
||
208 | |||
209 | static function ssl_img( $url ) { |
||
210 | if ( false !== strpos( $url, 'files.wordpress.com' ) ) { |
||
211 | return self::https( $url ); |
||
212 | } else { |
||
213 | return self::https( jetpack_photon_url( $url ) ); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | static function get_video_poster( $type, $id ) { |
||
218 | if ( 'videopress' == $type ) { |
||
219 | if ( function_exists( 'video_get_highest_resolution_image_url' ) ) { |
||
220 | return video_get_highest_resolution_image_url( $id ); |
||
221 | } else if ( class_exists( 'VideoPress_Video' ) ) { |
||
222 | $video = new VideoPress_Video( $id ); |
||
223 | return $video->poster_frame_uri; |
||
224 | } |
||
225 | } else if ( 'youtube' == $type ) { |
||
226 | return 'http://img.youtube.com/vi/'.$id.'/0.jpg'; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | static function clean_text( $text ) { |
||
231 | return trim( |
||
232 | preg_replace( |
||
233 | '/[\s]+/', |
||
234 | ' ', |
||
235 | preg_replace( |
||
236 | '@https?://[\S]+@', |
||
237 | '', |
||
238 | strip_shortcodes( |
||
239 | strip_tags( |
||
240 | $text |
||
241 | ) |
||
242 | ) |
||
243 | ) |
||
244 | ) |
||
245 | ); |
||
246 | } |
||
247 | |||
248 | static function get_excerpt( $post_content, $post_excerpt, $max_words = 16, $max_chars = 256 ) { |
||
249 | if ( function_exists( 'wpcom_enhanced_excerpt_extract_excerpt' ) ) { |
||
250 | return self::clean_text( wpcom_enhanced_excerpt_extract_excerpt( array( |
||
251 | 'text' => $post_content, |
||
252 | 'excerpt_only' => true, |
||
253 | 'show_read_more' => false, |
||
254 | 'max_words' => $max_words, |
||
255 | 'max_chars' => $max_chars, |
||
256 | 'read_more_threshold' => 25, |
||
257 | ) ) ); |
||
258 | } else { |
||
259 | |||
260 | /** This filter is documented in core/src/wp-includes/post-template.php */ |
||
261 | $post_excerpt = apply_filters( 'get_the_excerpt', $post_excerpt ); |
||
262 | return self::clean_text( $post_excerpt ); |
||
263 | } |
||
264 | } |
||
265 | |||
266 | static function get_word_count( $post_content ) { |
||
267 | return str_word_count( self::clean_text( $post_content ) ); |
||
268 | } |
||
269 | |||
270 | static function get_word_remaining_count( $post_content, $excerpt_content ) { |
||
271 | return str_word_count( self::clean_text( $post_content ) ) - str_word_count( self::clean_text( $excerpt_content ) ); |
||
272 | } |
||
273 | |||
274 | static function get_link_count( $post_content ) { |
||
275 | return preg_match_all( '/\<a[\> ]/', $post_content, $matches ); |
||
276 | } |
||
277 | } |
||
278 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.