Jetpack_PostImages

Useful for finding an image to display alongside/in representation of a specific post.

Defined (1)

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

/class.jetpack-post-images.php  
  1. class Jetpack_PostImages { 
  2. /** 
  3. * If a slideshow is embedded within a post, then parse out the images involved and return them 
  4. */ 
  5. static function from_slideshow( $post_id, $width = 200, $height = 200 ) { 
  6. $images = array(); 
  7.  
  8. $post = get_post( $post_id ); 
  9. if ( !empty( $post->post_password ) ) 
  10. return $images; 
  11.  
  12. if ( false === has_shortcode( $post->post_content, 'slideshow' ) ) { 
  13. return false; // no slideshow - bail 
  14.  
  15. $permalink = get_permalink( $post->ID ); 
  16.  
  17. // Mechanic: Somebody set us up the bomb 
  18. $old_post = $GLOBALS['post']; 
  19. $GLOBALS['post'] = $post; 
  20. $old_shortcodes = $GLOBALS['shortcode_tags']; 
  21. $GLOBALS['shortcode_tags'] = array( 'slideshow' => $old_shortcodes['slideshow'] ); 
  22.  
  23. // Find all the slideshows 
  24. preg_match_all( '/' . get_shortcode_regex() . '/sx', $post->post_content, $slideshow_matches, PREG_SET_ORDER ); 
  25.  
  26. ob_start(); // The slideshow shortcode handler calls wp_print_scripts and wp_print_styles... not too happy about that 
  27.  
  28. foreach ( $slideshow_matches as $slideshow_match ) { 
  29. $slideshow = do_shortcode_tag( $slideshow_match ); 
  30. 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 
  31. continue; 
  32. $start = strpos( $slideshow, '[', $pos ); 
  33. $end = strpos( $slideshow, ']', $start ); 
  34. $post_images = json_decode( str_replace( "'", '"', substr( $slideshow, $start, $end - $start + 1 ) ) ); // parse via JSON 
  35. foreach ( $post_images as $post_image ) { 
  36. if ( !$post_image_id = absint( $post_image->id ) ) 
  37. continue; 
  38.  
  39. $meta = wp_get_attachment_metadata( $post_image_id ); 
  40.  
  41. // Must be larger than 200x200 (or user-specified) 
  42. if ( !isset( $meta['width'] ) || $meta['width'] < $width ) 
  43. continue; 
  44. if ( !isset( $meta['height'] ) || $meta['height'] < $height ) 
  45. continue; 
  46.  
  47. $url = wp_get_attachment_url( $post_image_id ); 
  48.  
  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. ob_end_clean(); 
  58.  
  59. // Operator: Main screen turn on 
  60. $GLOBALS['shortcode_tags'] = $old_shortcodes; 
  61. $GLOBALS['post'] = $old_post; 
  62.  
  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.  
  71. $post = get_post( $post_id ); 
  72. if ( ! empty( $post->post_password ) ) { 
  73. return $images; 
  74.  
  75. $permalink = get_permalink( $post->ID ); 
  76.  
  77. $gallery_images = get_post_galleries_images( $post->ID, false ); 
  78.  
  79. foreach ( $gallery_images as $galleries ) { 
  80. foreach ( $galleries as $src ) { 
  81. list( $raw_src ) = explode( '?', $src ); // pull off any Query string (?w=250) 
  82. $raw_src = wp_specialchars_decode( $raw_src ); // rawify it 
  83. $raw_src = esc_url_raw( $raw_src ); // clean it 
  84. $images[] = array( 
  85. 'type' => 'image',  
  86. 'from' => 'gallery',  
  87. 'src' => $raw_src,  
  88. 'href' => $permalink,  
  89. ); 
  90.  
  91. return $images; 
  92.  
  93. /** 
  94. * Get attachment images for a specified post and return them. Also make sure 
  95. * their dimensions are at or above a required minimum. 
  96. */ 
  97. static function from_attachment( $post_id, $width = 200, $height = 200 ) { 
  98. $images = array(); 
  99.  
  100. $post = get_post( $post_id ); 
  101. if ( !empty( $post->post_password ) ) 
  102. return $images; 
  103.  
  104. $post_images = get_posts( array( 
  105. 'post_parent' => $post_id, // Must be children of post 
  106. 'numberposts' => 5, // No more than 5 
  107. 'post_type' => 'attachment', // Must be attachments 
  108. 'post_mime_type' => 'image', // Must be images 
  109. ) ); 
  110.  
  111. if ( !$post_images ) 
  112. return false; 
  113.  
  114. $permalink = get_permalink( $post_id ); 
  115.  
  116. foreach ( $post_images as $post_image ) { 
  117. $meta = wp_get_attachment_metadata( $post_image->ID ); 
  118. // Must be larger than 200x200 
  119. if ( !isset( $meta['width'] ) || $meta['width'] < $width ) 
  120. continue; 
  121. if ( !isset( $meta['height'] ) || $meta['height'] < $height ) 
  122. continue; 
  123.  
  124. $url = wp_get_attachment_url( $post_image->ID ); 
  125.  
  126. $images[] = array( 
  127. 'type' => 'image',  
  128. 'from' => 'attachment',  
  129. 'src' => $url,  
  130. 'src_width' => $meta['width'],  
  131. 'src_height' => $meta['height'],  
  132. 'href' => $permalink,  
  133. ); 
  134.  
  135. /** 
  136. * We only want to pass back attached images that were actually inserted. 
  137. * We can load up all the images found in the HTML source and then 
  138. * compare URLs to see if an image is attached AND inserted. 
  139. */ 
  140. $html_images = self::from_html( $post_id ); 
  141. $inserted_images = array(); 
  142.  
  143. foreach( $html_images as $html_image ) { 
  144. $src = parse_url( $html_image['src'] ); 
  145. // strip off any query strings from src 
  146. if( ! empty( $src['scheme'] ) && ! empty( $src['host'] ) ) { 
  147. $inserted_images[] = $src['scheme'] . '://' . $src['host'] . $src['path']; 
  148. } elseif( ! empty( $src['host'] ) ) { 
  149. $inserted_images[] = set_url_scheme( 'http://' . $src['host'] . $src['path'] ); 
  150. } else { 
  151. $inserted_images[] = site_url( '/' ) . $src['path']; 
  152. foreach( $images as $i => $image ) { 
  153. if ( !in_array( $image['src'], $inserted_images ) ) 
  154. unset( $images[$i] ); 
  155.  
  156. return $images; 
  157.  
  158. /** 
  159. * Check if a Featured Image is set for this post, and return it in a similar 
  160. * format to the other images?_from_*() methods. 
  161. * @param int $post_id The post ID to check 
  162. * @return Array containing details of the Featured Image, or empty array if none. 
  163. */ 
  164. static function from_thumbnail( $post_id, $width = 200, $height = 200 ) { 
  165. $images = array(); 
  166.  
  167. $post = get_post( $post_id ); 
  168. if ( !empty( $post->post_password ) ) 
  169. return $images; 
  170.  
  171. if ( !function_exists( 'get_post_thumbnail_id' ) ) 
  172. return $images; 
  173.  
  174. $thumb = get_post_thumbnail_id( $post_id ); 
  175.  
  176. if ( $thumb ) { 
  177. $meta = wp_get_attachment_metadata( $thumb ); 
  178.  
  179. // Must be larger than requested minimums 
  180. if ( !isset( $meta['width'] ) || $meta['width'] < $width ) 
  181. return $images; 
  182. if ( !isset( $meta['height'] ) || $meta['height'] < $height ) 
  183. return $images; 
  184.  
  185. $too_big = ( ( ! empty( $meta['width'] ) && $meta['width'] > 1200 ) || ( ! empty( $meta['height'] ) && $meta['height'] > 1200 ) ); 
  186.  
  187. if ( $too_big ) { 
  188. $img_src = wp_get_attachment_image_src( $thumb, array( 1200, 1200 ) ); 
  189. } else { 
  190. $img_src = wp_get_attachment_image_src( $thumb, 'full' ); 
  191.  
  192. $url = $img_src[0]; 
  193.  
  194. $images = array( array( // Other methods below all return an array of arrays 
  195. 'type' => 'image',  
  196. 'from' => 'thumbnail',  
  197. 'src' => $url,  
  198. 'src_width' => $img_src[1],  
  199. 'src_height' => $img_src[2],  
  200. 'href' => get_permalink( $thumb ),  
  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.  
  212. if ( is_numeric( $html_or_id ) ) { 
  213. $post = get_post( $html_or_id ); 
  214. if ( empty( $post ) || !empty( $post->post_password ) ) 
  215. return $images; 
  216.  
  217. $html = $post->post_content; // DO NOT apply the_content filters here, it will cause loops 
  218. } else { 
  219. $html = $html_or_id; 
  220.  
  221. if ( !$html ) 
  222. return $images; 
  223.  
  224. preg_match_all( '!<img.*src=[\'"]([^"]+)[\'"].*/?>!iUs', $html, $matches ); 
  225. if ( !empty( $matches[1] ) ) { 
  226. foreach ( $matches[1] as $match ) { 
  227. if ( stristr( $match, '/smilies/' ) ) 
  228. continue; 
  229.  
  230. $images[] = array( 
  231. 'type' => 'image',  
  232. 'from' => 'html',  
  233. 'src' => html_entity_decode( $match ),  
  234. 'href' => '', // No link to apply to these. Might potentially parse for that as well, but not for now 
  235. ); 
  236.  
  237. return $images; 
  238.  
  239. /** 
  240. * @param int $post_id The post ID to check 
  241. * @param int $size 
  242. * @return Array containing details of the image, or empty array if none. 
  243. */ 
  244. static function from_blavatar( $post_id, $size = 96 ) { 
  245.  
  246. $permalink = get_permalink( $post_id ); 
  247.  
  248. if ( function_exists( 'blavatar_domain' ) && function_exists( 'blavatar_exists' ) && function_exists( 'blavatar_url' ) ) { 
  249. $domain = blavatar_domain( $permalink ); 
  250.  
  251. if ( ! blavatar_exists( $domain ) ) { 
  252. return array(); 
  253.  
  254. $url = blavatar_url( $domain, 'img', $size ); 
  255. } elseif ( function_exists( 'jetpack_has_site_icon' ) && jetpack_has_site_icon() ) { 
  256. $url = jetpack_site_icon_url( null, $size, $default = false ); 
  257. } else { 
  258. return array(); 
  259.  
  260. return array( array( 
  261. 'type' => 'image',  
  262. 'from' => 'blavatar',  
  263. 'src' => $url,  
  264. 'src_width' => $size,  
  265. 'src_height' => $size,  
  266. 'href' => $permalink,  
  267. ) ); 
  268.  
  269. /** 
  270. * @param int $post_id The post ID to check 
  271. * @param int $size 
  272. * @param string $default The default image to use. 
  273. * @return Array containing details of the image, or empty array if none. 
  274. */ 
  275. static function from_gravatar( $post_id, $size = 96, $default = false ) { 
  276. $post = get_post( $post_id ); 
  277. $permalink = get_permalink( $post_id ); 
  278.  
  279. if ( function_exists( 'wpcom_get_avatar_url' ) ) { 
  280. $url = wpcom_get_avatar_url( $post->post_author, $size, $default, true ); 
  281. if ( $url && is_array( $url ) ) { 
  282. $url = $url[0]; 
  283. } else { 
  284. $has_filter = has_filter( 'pre_option_show_avatars', '__return_true' ); 
  285. if ( !$has_filter ) { 
  286. add_filter( 'pre_option_show_avatars', '__return_true' ); 
  287. $avatar = get_avatar( $post->post_author, $size, $default ); 
  288. if ( !$has_filter ) { 
  289. remove_filter( 'pre_option_show_avatars', '__return_true' ); 
  290.  
  291. if ( !$avatar ) { 
  292. return array(); 
  293.  
  294. if ( !preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $matches ) ) { 
  295. return array(); 
  296.  
  297. $url = wp_specialchars_decode( $matches[1], ENT_QUOTES ); 
  298.  
  299. return array( array( 
  300. 'type' => 'image',  
  301. 'from' => 'gravatar',  
  302. 'src' => $url,  
  303. 'src_width' => $size,  
  304. 'src_height' => $size,  
  305. 'href' => $permalink,  
  306. ) ); 
  307.  
  308. /** 
  309. * Run through the different methods that we have available to try to find a single good 
  310. * display image for this post. 
  311. * @param int $post_id 
  312. * @param array $args Other arguments (currently width and height required for images where possible to determine) 
  313. * @return Array containing details of the best image to be used 
  314. */ 
  315. static function get_image( $post_id, $args = array() ) { 
  316. $image = ''; 
  317.  
  318. /** 
  319. * Fires before we find a single good image for a specific post. 
  320. * @since 2.2.0 
  321. * @param int $post_id Post ID. 
  322. */ 
  323. do_action( 'jetpack_postimages_pre_get_image', $post_id ); 
  324. $media = self::get_images( $post_id, $args ); 
  325.  
  326.  
  327. if ( is_array( $media ) ) { 
  328. foreach ( $media as $item ) { 
  329. if ( 'image' == $item['type'] ) { 
  330. $image = $item; 
  331. break; 
  332.  
  333. /** 
  334. * Fires after we find a single good image for a specific post. 
  335. * @since 2.2.0 
  336. * @param int $post_id Post ID. 
  337. */ 
  338. do_action( 'jetpack_postimages_post_get_image', $post_id ); 
  339.  
  340. return $image; 
  341.  
  342. /** 
  343. * Get an array containing a collection of possible images for this post, stopping once we hit a method 
  344. * that returns something useful. 
  345. * @param int $post_id 
  346. * @param array $args Optional args, see defaults list for details 
  347. * @return Array containing images that would be good for representing this post 
  348. */ 
  349. static function get_images( $post_id, $args = array() ) { 
  350. // Figure out which image to attach to this post. 
  351. $media = false; 
  352.  
  353. /** 
  354. * Filters the array of images that would be good for a specific post. 
  355. * This filter is applied before options ($args) filter the original array. 
  356. * @since 2.0.0 
  357. * @param array $media Array of images that would be good for a specific post. 
  358. * @param int $post_id Post ID. 
  359. * @param array $args Array of options to get images. 
  360. */ 
  361. $media = apply_filters( 'jetpack_images_pre_get_images', $media, $post_id, $args ); 
  362. if ( $media ) 
  363. return $media; 
  364.  
  365. $defaults = array( 
  366. 'width' => 200, // Required minimum width (if possible to determine) 
  367. 'height' => 200, // Required minimum height (if possible to determine) 
  368.  
  369. 'fallback_to_avatars' => false, // Optionally include Blavatar and Gravatar (in that order) in the image stack 
  370. 'avatar_size' => 96, // Used for both Grav and Blav 
  371. 'gravatar_default' => false, // Default image to use if we end up with no Gravatar 
  372.  
  373. 'from_thumbnail' => true, // Use these flags to specify which methods to use to find an image 
  374. 'from_slideshow' => true,  
  375. 'from_gallery' => true,  
  376. 'from_attachment' => true,  
  377. 'from_html' => true,  
  378.  
  379. 'html_content' => '' // HTML string to pass to from_html() 
  380. ); 
  381. $args = wp_parse_args( $args, $defaults ); 
  382.  
  383. $media = false; 
  384. if ( $args['from_thumbnail'] ) 
  385. $media = self::from_thumbnail( $post_id, $args['width'], $args['height'] ); 
  386. if ( !$media && $args['from_slideshow'] ) 
  387. $media = self::from_slideshow( $post_id, $args['width'], $args['height'] ); 
  388. if ( !$media && $args['from_gallery'] ) 
  389. $media = self::from_gallery( $post_id ); 
  390. if ( !$media && $args['from_attachment'] ) 
  391. $media = self::from_attachment( $post_id, $args['width'], $args['height'] ); 
  392. if ( !$media && $args['from_html'] ) { 
  393. if ( empty( $args['html_content'] ) ) 
  394. $media = self::from_html( $post_id ); // Use the post_id, which will load the content 
  395. else 
  396. $media = self::from_html( $args['html_content'] ); // If html_content is provided, use that 
  397.  
  398. if ( !$media && $args['fallback_to_avatars'] ) { 
  399. $media = self::from_blavatar( $post_id, $args['avatar_size'] ); 
  400. if ( !$media ) 
  401. $media = self::from_gravatar( $post_id, $args['avatar_size'], $args['gravatar_default'] ); 
  402.  
  403. /** 
  404. * Filters the array of images that would be good for a specific post. 
  405. * This filter is applied after options ($args) filter the original array. 
  406. * @since 2.0.0 
  407. * @param array $media Array of images that would be good for a specific post. 
  408. * @param int $post_id Post ID. 
  409. * @param array $args Array of options to get images. 
  410. */ 
  411. return apply_filters( 'jetpack_images_get_images', $media, $post_id, $args ); 
  412.  
  413. /** 
  414. * Takes an image URL and pixel dimensions then returns a URL for the 
  415. * resized and croped image. 
  416. * @param string $src 
  417. * @param int $dimension 
  418. * @return string Transformed image URL 
  419. */ 
  420. static function fit_image_url( $src, $width, $height ) { 
  421. $width = (int) $width; 
  422. $height = (int) $height; 
  423.  
  424. // Umm... 
  425. if ( $width < 1 || $height < 1 ) { 
  426. return $src; 
  427.  
  428. // See if we should bypass WordPress.com SaaS resizing 
  429. if ( has_filter( 'jetpack_images_fit_image_url_override' ) ) { 
  430. /** 
  431. * Filters the image URL used after dimensions are set by Photon. 
  432. * @since 3.3.0 
  433. * @param string $src Image URL. 
  434. * @param int $width Image width. 
  435. * @param int $width Image height. 
  436. */ 
  437. return apply_filters( 'jetpack_images_fit_image_url_override', $src, $width, $height ); 
  438.  
  439. // If WPCOM hosted image use native transformations 
  440. $img_host = parse_url( $src, PHP_URL_HOST ); 
  441. if ( '.files.wordpress.com' == substr( $img_host, -20 ) ) { 
  442. return add_query_arg( array( 'w' => $width, 'h' => $height, 'crop' => 1 ), $src ); 
  443.  
  444. // Use Photon magic 
  445. if( function_exists( 'jetpack_photon_url' ) ) { 
  446. return jetpack_photon_url( $src, array( 'resize' => "$width, $height" ) ); 
  447.  
  448. // Arg... no way to resize image using WordPress.com infrastructure! 
  449. return $src;