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