PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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