Jetpack_Media_Summary

Class Jetpack_Media_Summary.

Defined (1)

The class is defined in the following location(s).

/class.media-summary.php  
  1. class Jetpack_Media_Summary { 
  2.  
  3. static function get( $post_id, $blog_id = 0, $args = array() ) { 
  4. $defaults = array( 
  5. 'max_words' => 16,  
  6. 'max_chars' => 256,  
  7. ); 
  8. $args = wp_parse_args( $args, $defaults ); 
  9.  
  10. $switched = false; 
  11. if ( !empty( $blog_id ) && $blog_id != get_current_blog_id() && function_exists( 'switch_to_blog' ) ) { 
  12. switch_to_blog( $blog_id ); 
  13. $switched = true; 
  14. } else { 
  15. $blog_id = get_current_blog_id(); 
  16.  
  17. $post = get_post( $post_id ); 
  18. $permalink = get_permalink( $post_id ); 
  19.  
  20. $return = array( 
  21. 'type' => 'standard',  
  22. 'permalink' => $permalink,  
  23. 'image' => '',  
  24. 'excerpt' => '',  
  25. 'word_count' => 0,  
  26. 'secure' => array( 
  27. 'image' => '',  
  28. ),  
  29. 'count' => array( 
  30. 'image' => 0,  
  31. 'video' => 0,  
  32. 'word' => 0,  
  33. 'link' => 0,  
  34. ),  
  35. ); 
  36.  
  37. if ( empty( $post->post_password ) ) { 
  38. $return['excerpt'] = self::get_excerpt( $post->post_content, $post->post_excerpt, $args['max_words'], $args['max_chars'] ); 
  39. $return['count']['word'] = self::get_word_count( $post->post_content ); 
  40. $return['count']['word_remaining'] = self::get_word_remaining_count( $post->post_content, $return['excerpt'] ); 
  41. $return['count']['link'] = self::get_link_count( $post->post_content ); 
  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. $return['count']['video']++; 
  60. break; 
  61. case 'youtube': 
  62. if ( 0 == $return['count']['video'] ) { 
  63. $return['type'] = 'video'; 
  64. $return['video'] = esc_url_raw( 'http://www.youtube.com/watch?feature=player_embedded&v=' . $extract['shortcode']['youtube']['id'][0] ); 
  65. $return['image'] = self::get_video_poster( 'youtube', $extract['shortcode']['youtube']['id'][0] ); 
  66. $return['secure']['video'] = self::https( $return['video'] ); 
  67. $return['secure']['image'] = self::https( $return['image'] ); 
  68. $return['count']['video']++; 
  69. break; 
  70. case 'vimeo': 
  71. if ( 0 == $return['count']['video'] ) { 
  72. $return['type'] = 'video'; 
  73. $return['video'] = esc_url_raw( 'http://vimeo.com/' . $extract['shortcode']['vimeo']['id'][0] ); 
  74. $return['secure']['video'] = self::https( $return['video'] ); 
  75.  
  76. $poster_image = get_post_meta( $post_id, 'vimeo_poster_image', true ); 
  77. if ( !empty( $poster_image ) ) { 
  78. $return['image'] = $poster_image; 
  79. $poster_url_parts = parse_url( $poster_image ); 
  80. $return['secure']['image'] = 'https://secure-a.vimeocdn.com' . $poster_url_parts['path']; 
  81. $return['count']['video']++; 
  82. break; 
  83.  
  84.  
  85. if ( !empty( $extract['has']['embed'] ) ) { 
  86. foreach( $extract['embed']['url'] as $embed ) { 
  87. if ( preg_match( '/((youtube|vimeo|dailymotion)\.com|youtu.be)/', $embed ) ) { 
  88. if ( 0 == $return['count']['video'] ) { 
  89. $return['type'] = 'video'; 
  90. $return['video'] = 'http://' . $embed; 
  91. $return['secure']['video'] = self::https( $return['video'] ); 
  92. if ( false !== strpos( $embed, 'youtube' ) ) { 
  93. $return['image'] = self::get_video_poster( 'youtube', jetpack_get_youtube_id( $return['video'] ) ); 
  94. $return['secure']['image'] = self::https( $return['image'] ); 
  95. } else if ( false !== strpos( $embed, 'youtu.be' ) ) { 
  96. $youtube_id = jetpack_get_youtube_id( $return['video'] ); 
  97. $return['video'] = 'http://youtube.com/watch?v=' . $youtube_id . '&feature=youtu.be'; 
  98. $return['secure']['video'] = self::https( $return['video'] ); 
  99. $return['image'] = self::get_video_poster( 'youtube', jetpack_get_youtube_id( $return['video'] ) ); 
  100. $return['secure']['image'] = self::https( $return['image'] ); 
  101. } else if ( false !== strpos( $embed, 'vimeo' ) ) { 
  102. $poster_image = get_post_meta( $post_id, 'vimeo_poster_image', true ); 
  103. if ( !empty( $poster_image ) ) { 
  104. $return['image'] = $poster_image; 
  105. $poster_url_parts = parse_url( $poster_image ); 
  106. $return['secure']['image'] = 'https://secure-a.vimeocdn.com' . $poster_url_parts['path']; 
  107. } else if ( false !== strpos( $embed, 'dailymotion' ) ) { 
  108. $return['image'] = str_replace( 'dailymotion.com/video/', 'dailymotion.com/thumbnail/video/', $embed ); 
  109. $return['image'] = parse_url( $return['image'], PHP_URL_SCHEME ) === null ? 'http://' . $return['image'] : $return['image']; 
  110. $return['secure']['image'] = self::https( $return['image'] ); 
  111.  
  112. $return['count']['video']++; 
  113.  
  114. // Do we really want to make the video the primary focus of the post? 
  115. if ( 'video' == $return['type'] ) { 
  116. $content = wpautop( strip_tags( $post->post_content ) ); 
  117. $paragraphs = explode( '</p>', $content ); 
  118. $number_of_paragraphs = 0; 
  119.  
  120. foreach ( $paragraphs as $i => $paragraph ) { 
  121. // Don't include blank lines as a paragraph 
  122. if ( '' == trim( $paragraph ) ) { 
  123. unset( $paragraphs[$i] ); 
  124. continue; 
  125. $number_of_paragraphs++; 
  126.  
  127. $number_of_paragraphs = $number_of_paragraphs - $return['count']['video']; // subtract amount for videos.. 
  128.  
  129. // More than 2 paragraph? The video is not the primary focus so we can do some more analysis 
  130. if ( $number_of_paragraphs > 2 ) 
  131. $return['type'] = 'standard'; 
  132.  
  133. // If we don't have any prioritized embed... 
  134. if ( 'standard' == $return['type'] ) { 
  135. if ( ( ! empty( $extract['has']['gallery'] ) || ! empty( $extract['shortcode']['gallery']['count'] ) ) && ! empty( $extract['image'] ) ) { 
  136. //... Then we prioritize galleries first (multiple images returned) 
  137. $return['type'] = 'gallery'; 
  138. $return['images'] = $extract['image']; 
  139. foreach ( $return['images'] as $image ) { 
  140. $return['secure']['images'][] = array( 'url' => self::ssl_img( $image['url'] ) ); 
  141. $return['count']['image']++; 
  142. } else if ( ! empty( $extract['has']['image'] ) ) { 
  143. // ... Or we try and select a single image that would make sense 
  144. $content = wpautop( strip_tags( $post->post_content ) ); 
  145. $paragraphs = explode( '</p>', $content ); 
  146. $number_of_paragraphs = 0; 
  147.  
  148. foreach ( $paragraphs as $i => $paragraph ) { 
  149. // Don't include 'actual' captions as a paragraph 
  150. if ( false !== strpos( $paragraph, '[caption' ) ) { 
  151. unset( $paragraphs[$i] ); 
  152. continue; 
  153. // Don't include blank lines as a paragraph 
  154. if ( '' == trim( $paragraph ) ) { 
  155. unset( $paragraphs[$i] ); 
  156. continue; 
  157. $number_of_paragraphs++; 
  158.  
  159. $return['image'] = $extract['image'][0]['url']; 
  160. $return['secure']['image'] = self::ssl_img( $return['image'] ); 
  161. $return['count']['image']++; 
  162.  
  163. if ( $number_of_paragraphs <= 2 && 1 == count( $extract['image'] ) ) { 
  164. // If we have lots of text or images, let's not treat it as an image post, but return its first image 
  165. $return['type'] = 'image'; 
  166.  
  167. if ( $switched ) { 
  168. restore_current_blog(); 
  169.  
  170. return $return; 
  171.  
  172. static function https( $str ) { 
  173. return str_replace( 'http://', 'https://', $str ); 
  174.  
  175. static function ssl_img( $url ) { 
  176. if ( false !== strpos( $url, 'files.wordpress.com' ) ) { 
  177. return self::https( $url ); 
  178. } else { 
  179. return self::https( jetpack_photon_url( $url ) ); 
  180.  
  181. static function get_video_poster( $type, $id ) { 
  182. if ( 'videopress' == $type ) { 
  183. if ( function_exists( 'video_get_highest_resolution_image_url' ) ) { 
  184. return video_get_highest_resolution_image_url( $id ); 
  185. } else if ( class_exists( 'VideoPress_Video' ) ) { 
  186. $video = new VideoPress_Video( $id ); 
  187. return $video->poster_frame_uri; 
  188. } else if ( 'youtube' == $type ) { 
  189. return 'http://img.youtube.com/vi/'.$id.'/0.jpg'; 
  190.  
  191. static function clean_text( $text ) { 
  192. return trim( 
  193. preg_replace( 
  194. '/[\s]+/',  
  195. ' ',  
  196. preg_replace( 
  197. '@https?://[\S]+@',  
  198. '',  
  199. strip_shortcodes( 
  200. strip_tags( 
  201. $text 
  202. ); 
  203.  
  204. static function get_excerpt( $post_content, $post_excerpt, $max_words = 16, $max_chars = 256 ) { 
  205. if ( function_exists( 'wpcom_enhanced_excerpt_extract_excerpt' ) ) { 
  206. return self::clean_text( wpcom_enhanced_excerpt_extract_excerpt( array( 
  207. 'text' => $post_content,  
  208. 'excerpt_only' => true,  
  209. 'show_read_more' => false,  
  210. 'max_words' => $max_words,  
  211. 'max_chars' => $max_chars,  
  212. 'read_more_threshold' => 25,  
  213. ) ) ); 
  214. } else { 
  215.  
  216. /** This filter is documented in core/src/wp-includes/post-template.php */ 
  217. $post_excerpt = apply_filters( 'get_the_excerpt', $post_excerpt ); 
  218. return self::clean_text( $post_excerpt ); 
  219.  
  220. static function get_word_count( $post_content ) { 
  221. return str_word_count( self::clean_text( $post_content ) ); 
  222.  
  223. static function get_word_remaining_count( $post_content, $excerpt_content ) { 
  224. return str_word_count( self::clean_text( $post_content ) ) - str_word_count( self::clean_text( $excerpt_content ) ); 
  225.  
  226. static function get_link_count( $post_content ) { 
  227. return preg_match_all( '/\<a[\> ]/', $post_content, $matches );