PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 2.7.5
Jetpack – WP Security, Backup, Speed, & Growth v2.7.5
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / class.media-summary.php

class.media-summary.php in Jetpack – WP Security, Backup, Speed, & Growth 2.7.5, at class.media-summary.php

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