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

/184.168.182.1/wp-content/plugins/jetpack/class.jetpack-post-images.php

https://gitlab.com/endomorphosis/falkenstein
PHP | 484 lines | 365 code | 54 blank | 65 comment | 53 complexity | 238a430f1d02c0e1f7bcf2d999eb6f74 MD5 | raw file
  1. <?php
  2. /**
  3. * Useful for finding an image to display alongside/in representation of a specific post.
  4. *
  5. * Includes a few different methods, all of which return a similar-format array containing
  6. * details of any images found. Everything can (should) be called statically, it's just a
  7. * function-bucket. You can also call Jetpack_PostImages::get_image() to cycle through all of the methods until
  8. * one of them finds something useful.
  9. *
  10. * This file is included verbatim in Jetpack
  11. */
  12. class Jetpack_PostImages {
  13. /**
  14. * If a slideshow is embedded within a post, then parse out the images involved and return them
  15. */
  16. static function from_slideshow( $post_id, $width = 200, $height = 200 ) {
  17. $images = array();
  18. $post = get_post( $post_id );
  19. if ( !empty( $post->post_password ) )
  20. return $images;
  21. if ( false === strpos( $post->post_content, '[slideshow' ) )
  22. return false; // no slideshow - bail
  23. $permalink = get_permalink( $post->ID );
  24. // Mechanic: Somebody set us up the bomb
  25. $old_post = $GLOBALS['post'];
  26. $GLOBALS['post'] = $post;
  27. $old_shortcodes = $GLOBALS['shortcode_tags'];
  28. $GLOBALS['shortcode_tags'] = array( 'slideshow' => $old_shortcodes['slideshow'] );
  29. // Find all the slideshows
  30. preg_match_all( '/' . get_shortcode_regex() . '/sx', $post->post_content, $slideshow_matches, PREG_SET_ORDER );
  31. ob_start(); // The slideshow shortcode handler calls wp_print_scripts and wp_print_styles... not too happy about that
  32. foreach ( $slideshow_matches as $slideshow_match ) {
  33. $slideshow = do_shortcode_tag( $slideshow_match );
  34. if ( false === $pos = stripos( $slideshow, 'slideShow.images' ) ) // must be something wrong - or we changed the output format in which case none of the following will work
  35. continue;
  36. $start = strpos( $slideshow, '[', $pos );
  37. $end = strpos( $slideshow, ']', $start );
  38. $post_images = json_decode( str_replace( "'", '"', substr( $slideshow, $start, $end - $start + 1 ) ) ); // parse via JSON
  39. foreach ( $post_images as $post_image ) {
  40. if ( !$post_image_id = absint( $post_image->id ) )
  41. continue;
  42. $meta = wp_get_attachment_metadata( $post_image_id );
  43. // Must be larger than 200x200 (or user-specified)
  44. if ( !isset( $meta['width'] ) || $meta['width'] < $width )
  45. continue;
  46. if ( !isset( $meta['height'] ) || $meta['height'] < $height )
  47. continue;
  48. $url = wp_get_attachment_url( $post_image_id );
  49. $images[] = array(
  50. 'type' => 'image',
  51. 'from' => 'slideshow',
  52. 'src' => $url,
  53. 'src_width' => $meta['width'],
  54. 'src_height' => $meta['height'],
  55. 'href' => $permalink,
  56. );
  57. }
  58. }
  59. ob_end_clean();
  60. // Operator: Main screen turn on
  61. $GLOBALS['shortcode_tags'] = $old_shortcodes;
  62. $GLOBALS['post'] = $old_post;
  63. return $images;
  64. }
  65. /**
  66. * If a gallery is detected, then get all the images from it.
  67. */
  68. static function from_gallery( $post_id ) {
  69. $images = array();
  70. $post = get_post( $post_id );
  71. if ( !empty( $post->post_password ) )
  72. return $images;
  73. if ( false === strpos( $post->post_content, '[gallery' ) )
  74. return false; // no gallery - bail
  75. $permalink = get_permalink( $post->ID );
  76. // CATS: All your base are belong to us
  77. $old_post = $GLOBALS['post'];
  78. $GLOBALS['post'] = $post;
  79. $old_shortcodes = $GLOBALS['shortcode_tags'];
  80. $GLOBALS['shortcode_tags'] = array( 'gallery' => $old_shortcodes['gallery'] );
  81. // Find all the galleries
  82. preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $gallery_matches, PREG_SET_ORDER );
  83. foreach ( $gallery_matches as $gallery_match ) {
  84. $gallery = do_shortcode_tag( $gallery_match );
  85. // Um... no images in the gallery - bail
  86. if ( false === $pos = stripos( $gallery, '<img' ) )
  87. continue;
  88. preg_match_all( '/<img\s+[^>]*src=([\'"])([^\'"]*)\\1/', $gallery, $image_match, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE );
  89. $a_pos = 0;
  90. foreach ( $image_match[2] as $src ) {
  91. list( $raw_src ) = explode( '?', $src[0] ); // pull off any Query string (?w=250)
  92. $raw_src = wp_specialchars_decode( $raw_src ); // rawify it
  93. $raw_src = esc_url_raw( $raw_src ); // clean it
  94. $a_pos = strrpos( substr( $gallery, 0, $src[1] ), '<a', $a_pos ); // is there surrounding <a>?
  95. if ( false !== $a_pos && preg_match( '/<a\s+[^>]*href=([\'"])([^\'"]*)\\1/', $gallery, $href_match, 0, $a_pos ) ) {
  96. $href = wp_specialchars_decode( $href_match[2] );
  97. $href = esc_url_raw( $href );
  98. } else {
  99. // CATS: You have no chance to survive make your time
  100. $href = $raw_src;
  101. }
  102. $a_pos = $src[1];
  103. $images[] = array(
  104. 'type' => 'image',
  105. 'from' => 'gallery',
  106. 'src' => $raw_src,
  107. 'href' => $permalink, // $href,
  108. );
  109. }
  110. }
  111. // Captain: For great justice
  112. $GLOBALS['shortcode_tags'] = $old_shortcodes;
  113. $GLOBALS['post'] = $old_post;
  114. return $images;
  115. }
  116. /**
  117. * Get attachment images for a specified post and return them. Also make sure
  118. * their dimensions are at or above a required minimum.
  119. */
  120. static function from_attachment( $post_id, $width = 200, $height = 200 ) {
  121. $images = array();
  122. $post = get_post( $post_id );
  123. if ( !empty( $post->post_password ) )
  124. return $images;
  125. $post_images = get_posts( array(
  126. 'post_parent' => $post_id, // Must be children of post
  127. 'numberposts' => 5, // No more than 5
  128. 'post_type' => 'attachment', // Must be attachments
  129. 'post_mime_type' => 'image', // Must be images
  130. ) );
  131. if ( !$post_images )
  132. return false;
  133. $permalink = get_permalink( $post_id );
  134. foreach ( $post_images as $post_image ) {
  135. $meta = wp_get_attachment_metadata( $post_image->ID );
  136. // Must be larger than 200x200
  137. if ( !isset( $meta['width'] ) || $meta['width'] < $width )
  138. continue;
  139. if ( !isset( $meta['height'] ) || $meta['height'] < $height )
  140. continue;
  141. $url = wp_get_attachment_url( $post_image->ID );
  142. $images[] = array(
  143. 'type' => 'image',
  144. 'from' => 'attachment',
  145. 'src' => $url,
  146. 'src_width' => $meta['width'],
  147. 'src_height' => $meta['height'],
  148. 'href' => $permalink,
  149. );
  150. }
  151. /*
  152. * We only want to pass back attached images that were actually inserted.
  153. * We can load up all the images found in the HTML source and then
  154. * compare URLs to see if an image is attached AND inserted.
  155. */
  156. $html_images = array();
  157. $html_images = self::from_html( $post_id );
  158. $inserted_images = array();
  159. foreach( $html_images as $html_image ) {
  160. $src = parse_url( $html_image['src'] );
  161. $inserted_images[] = $src['scheme'] . '://' . $src['host'] . $src['path']; // strip off any query strings
  162. }
  163. foreach( $images as $i => $image ) {
  164. if ( !in_array( $image['src'], $inserted_images ) )
  165. unset( $images[$i] );
  166. }
  167. return $images;
  168. }
  169. /**
  170. * Check if a Featured Image is set for this post, and return it in a similar
  171. * format to the other images?_from_*() methods.
  172. * @param int $post_id The post ID to check
  173. * @return Array containing details of the Featured Image, or empty array if none.
  174. */
  175. static function from_thumbnail( $post_id, $width = 200, $height = 200 ) {
  176. $images = array();
  177. $post = get_post( $post_id );
  178. if ( !empty( $post->post_password ) )
  179. return $images;
  180. if ( !function_exists( 'get_post_thumbnail_id' ) )
  181. return $images;
  182. $thumb = get_post_thumbnail_id( $post_id );
  183. if ( $thumb ) {
  184. $meta = wp_get_attachment_metadata( $thumb );
  185. // Must be larger than requested minimums
  186. if ( !isset( $meta['width'] ) || $meta['width'] < $width )
  187. return $images;
  188. if ( !isset( $meta['height'] ) || $meta['height'] < $height )
  189. return $images;
  190. $url = wp_get_attachment_url( $thumb );
  191. if ( stristr( $url, '?' ) )
  192. $url = substr( $url, 0, strpos( $url, '?' ) );
  193. $images = array( array( // Other methods below all return an array of arrays
  194. 'type' => 'image',
  195. 'from' => 'thumbnail',
  196. 'src' => $url,
  197. 'src_width' => $meta['width'],
  198. 'src_height' => $meta['height'],
  199. 'href' => get_permalink( $thumb ),
  200. ) );
  201. }
  202. return $images;
  203. }
  204. /**
  205. * Very raw -- just parse the HTML and pull out any/all img tags and return their src
  206. * @param mixed $html_or_id The HTML string to parse for images, or a post id
  207. * @return Array containing images
  208. */
  209. static function from_html( $html_or_id ) {
  210. $images = array();
  211. if ( is_numeric( $html_or_id ) ) {
  212. $post = get_post( $html_or_id );
  213. if ( empty( $post ) || !empty( $post->post_password ) )
  214. return $images;
  215. $html = $post->post_content; // DO NOT apply the_content filters here, it will cause loops
  216. } else {
  217. $html = $html_or_id;
  218. }
  219. if ( !$html )
  220. return $images;
  221. preg_match_all( '!<img.*src=[\'"]([^"]+)[\'"].*/?>!iUs', $html, $matches );
  222. if ( !empty( $matches[1] ) ) {
  223. foreach ( $matches[1] as $match ) {
  224. if ( stristr( $match, '/smilies/' ) )
  225. continue;
  226. $images[] = array(
  227. 'type' => 'image',
  228. 'from' => 'html',
  229. 'src' => html_entity_decode( $match ),
  230. 'href' => '', // No link to apply to these. Might potentially parse for that as well, but not for now
  231. );
  232. }
  233. }
  234. return $images;
  235. }
  236. /**
  237. * @param int $post_id The post ID to check
  238. * @param int $size
  239. * @return Array containing details of the image, or empty array if none.
  240. */
  241. static function from_blavatar( $post_id, $size = 96 ) {
  242. if ( !function_exists( 'blavatar_domain' ) || !function_exists( 'blavatar_exists' ) || !function_exists( 'blavatar_url' ) ) {
  243. return array();
  244. }
  245. $permalink = get_permalink( $post_id );
  246. $domain = blavatar_domain( $permalink );
  247. if ( !blavatar_exists( $domain ) ) {
  248. return array();
  249. }
  250. $url = blavatar_url( $domain, 'img', $size );
  251. return array( array(
  252. 'type' => 'image',
  253. 'from' => 'blavatar',
  254. 'src' => $url,
  255. 'src_width' => $size,
  256. 'src_height' => $size,
  257. 'href' => $permalink,
  258. ) );
  259. }
  260. /**
  261. * @param int $post_id The post ID to check
  262. * @param int $size
  263. * @param string $default The default image to use.
  264. * @return Array containing details of the image, or empty array if none.
  265. */
  266. static function from_gravatar( $post_id, $size = 96, $default = false ) {
  267. $post = get_post( $post_id );
  268. $permalink = get_permalink( $post_id );
  269. if ( function_exists( 'get_avatar_url' ) ) {
  270. $url = get_avatar_url( $post->post_author, $size, $default, true );
  271. if ( $url && is_array( $url ) ) {
  272. $url = $url[0];
  273. }
  274. } else {
  275. $has_filter = has_filter( 'pre_option_show_avatars', '__return_true' );
  276. if ( !$has_filter ) {
  277. add_filter( 'pre_option_show_avatars', '__return_true' );
  278. }
  279. $avatar = get_avatar( $post->post_author, $size, $default );
  280. if ( !$has_filter ) {
  281. remove_filter( 'pre_option_show_avatars', '__return_true' );
  282. }
  283. if ( !$avatar ) {
  284. return array();
  285. }
  286. if ( !preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $matches ) ) {
  287. return array();
  288. }
  289. $url = wp_specialchars_decode( $matches[1], ENT_QUOTES );
  290. }
  291. return array( array(
  292. 'type' => 'image',
  293. 'from' => 'gravatar',
  294. 'src' => $url,
  295. 'src_width' => $size,
  296. 'src_height' => $size,
  297. 'href' => $permalink,
  298. ) );
  299. }
  300. /**
  301. * Run through the different methods that we have available to try to find a single good
  302. * display image for this post.
  303. * @param int $post_id
  304. * @param array $args Other arguments (currently width and height required for images where possible to determine)
  305. * @return Array containing details of the best image to be used
  306. */
  307. static function get_image( $post_id, $args = array() ) {
  308. $image = '';
  309. do_action( 'jetpack_postimages_pre_get_image', $post_id );
  310. $media = self::get_images( $post_id, $args );
  311. if ( is_array( $media ) ) {
  312. foreach ( $media as $item ) {
  313. if ( 'image' == $item['type'] ) {
  314. $image = $item;
  315. break;
  316. }
  317. }
  318. }
  319. do_action( 'jetpack_postimages_post_get_image', $post_id );
  320. return $image;
  321. }
  322. /**
  323. * Get an array containing a collection of possible images for this post, stopping once we hit a method
  324. * that returns something useful.
  325. * @param int $post_id
  326. * @param array $args Optional args, see defaults list for details
  327. * @return Array containing images that would be good for representing this post
  328. */
  329. static function get_images( $post_id, $args = array() ) {
  330. // Figure out which image to attach to this post.
  331. $media = false;
  332. $media = apply_filters( 'jetpack_images_pre_get_images', $media, $post_id, $args );
  333. if ( $media )
  334. return $media;
  335. $defaults = array(
  336. 'width' => 200, // Required minimum width (if possible to determine)
  337. 'height' => 200, // Required minimum height (if possible to determine)
  338. 'fallback_to_avatars' => false, // Optionally include Blavatar and Gravatar (in that order) in the image stack
  339. 'avatar_size' => 96, // Used for both Grav and Blav
  340. 'gravatar_default' => false, // Default image to use if we end up with no Gravatar
  341. 'from_thumbnail' => true, // Use these flags to specifcy which methods to use to find an image
  342. 'from_slideshow' => true,
  343. 'from_gallery' => true,
  344. 'from_attachment' => true,
  345. 'from_html' => true,
  346. 'html_content' => '' // HTML string to pass to from_html()
  347. );
  348. $args = wp_parse_args( $args, $defaults );
  349. $media = false;
  350. if ( $args['from_thumbnail'] )
  351. $media = self::from_thumbnail( $post_id, $args['width'], $args['height'] );
  352. if ( !$media && $args['from_slideshow'] )
  353. $media = self::from_slideshow( $post_id, $args['width'], $args['height'] );
  354. if ( !$media && $args['from_gallery'] )
  355. $media = self::from_gallery( $post_id );
  356. if ( !$media && $args['from_attachment'] )
  357. $media = self::from_attachment( $post_id, $args['width'], $args['height'] );
  358. if ( !$media && $args['from_html'] ) {
  359. if ( empty( $args['html_content'] ) )
  360. $media = self::from_html( $post_id ); // Use the post_id, which will load the content
  361. else
  362. $media = self::from_html( $args['html_content'] ); // If html_content is provided, use that
  363. }
  364. if ( !$media && $args['fallback_to_avatars'] ) {
  365. $media = self::from_blavatar( $post_id, $args['avatar_size'] );
  366. if ( !$media )
  367. $media = self::from_gravatar( $post_id, $args['avatar_size'], $args['gravatar_default'] );
  368. }
  369. return apply_filters( 'jetpack_images_get_images', $media, $post_id, $args );
  370. }
  371. /**
  372. * Takes an image URL and pixel dimensions then returns a URL for the
  373. * resized and croped image.
  374. *
  375. * @param string $src
  376. * @param int $dimension
  377. * @return string Transformed image URL
  378. */
  379. static function fit_image_url( $src, $width, $height ) {
  380. $width = (int) $width;
  381. $height = (int) $height;
  382. // Umm...
  383. if ( $width < 1 || $height < 1 ) {
  384. return $src;
  385. }
  386. // If WPCOM hosted image use native transformations
  387. $img_host = parse_url( $src, PHP_URL_HOST );
  388. if ( '.files.wordpress.com' == substr( $img_host, -20 ) ) {
  389. return add_query_arg( array( 'w' => $width, 'h' => $height, 'crop' => 1 ), $src );
  390. }
  391. // Use Photon magic
  392. if( function_exists( 'jetpack_photon_url' ) ) {
  393. return jetpack_photon_url( $src, array( 'resize' => "$width,$height" ) );
  394. }
  395. // Arg... no way to resize image using WordPress.com infrastructure!
  396. return $src;
  397. }
  398. }