PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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