PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/class.media-summary.php

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