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