PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/thisishayat/itv-2016
PHP | 544 lines | 372 code | 63 blank | 109 comment | 71 complexity | 321c3bfe7df83a3b18377f9b92399dfb 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. if ( empty( $images ) && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
  195. $meta_thumbnail = get_post_meta( $post_id, '_jetpack_post_thumbnail', true );
  196. if ( ! empty( $meta_thumbnail ) ) {
  197. if ( ! isset( $meta_thumbnail['width'] ) || $meta_thumbnail['width'] < $width ) {
  198. return $images;
  199. }
  200. if ( ! isset( $meta_thumbnail['height'] ) || $meta_thumbnail['height'] < $height ) {
  201. return $images;
  202. }
  203. $images = array( array( // Other methods below all return an array of arrays
  204. 'type' => 'image',
  205. 'from' => 'thumbnail',
  206. 'src' => $meta_thumbnail['URL'],
  207. 'src_width' => $meta_thumbnail['width'],
  208. 'src_height' => $meta_thumbnail['height'],
  209. 'href' => $meta_thumbnail['URL'],
  210. ) );
  211. }
  212. }
  213. return $images;
  214. }
  215. /**
  216. * Very raw -- just parse the HTML and pull out any/all img tags and return their src
  217. * @param mixed $html_or_id The HTML string to parse for images, or a post id
  218. * @return Array containing images
  219. */
  220. static function from_html( $html_or_id ) {
  221. $images = array();
  222. if ( is_numeric( $html_or_id ) ) {
  223. $post = get_post( $html_or_id );
  224. if ( empty( $post ) || !empty( $post->post_password ) )
  225. return $images;
  226. $html = $post->post_content; // DO NOT apply the_content filters here, it will cause loops
  227. } else {
  228. $html = $html_or_id;
  229. }
  230. if ( !$html )
  231. return $images;
  232. preg_match_all( '!<img.*src=[\'"]([^"]+)[\'"].*/?>!iUs', $html, $matches );
  233. if ( !empty( $matches[1] ) ) {
  234. foreach ( $matches[1] as $match ) {
  235. if ( stristr( $match, '/smilies/' ) )
  236. continue;
  237. $images[] = array(
  238. 'type' => 'image',
  239. 'from' => 'html',
  240. 'src' => html_entity_decode( $match ),
  241. 'href' => '', // No link to apply to these. Might potentially parse for that as well, but not for now
  242. );
  243. }
  244. }
  245. return $images;
  246. }
  247. /**
  248. * @param int $post_id The post ID to check
  249. * @param int $size
  250. * @return Array containing details of the image, or empty array if none.
  251. */
  252. static function from_blavatar( $post_id, $size = 96 ) {
  253. $permalink = get_permalink( $post_id );
  254. if ( function_exists( 'blavatar_domain' ) && function_exists( 'blavatar_exists' ) && function_exists( 'blavatar_url' ) ) {
  255. $domain = blavatar_domain( $permalink );
  256. if ( ! blavatar_exists( $domain ) ) {
  257. return array();
  258. }
  259. $url = blavatar_url( $domain, 'img', $size );
  260. } elseif ( function_exists( 'jetpack_has_site_icon' ) && jetpack_has_site_icon() ) {
  261. $url = jetpack_site_icon_url( null, $size, $default = false );
  262. } else {
  263. return array();
  264. }
  265. return array( array(
  266. 'type' => 'image',
  267. 'from' => 'blavatar',
  268. 'src' => $url,
  269. 'src_width' => $size,
  270. 'src_height' => $size,
  271. 'href' => $permalink,
  272. ) );
  273. }
  274. /**
  275. * @param int $post_id The post ID to check
  276. * @param int $size
  277. * @param string $default The default image to use.
  278. * @return Array containing details of the image, or empty array if none.
  279. */
  280. static function from_gravatar( $post_id, $size = 96, $default = false ) {
  281. $post = get_post( $post_id );
  282. $permalink = get_permalink( $post_id );
  283. if ( function_exists( 'wpcom_get_avatar_url' ) ) {
  284. $url = wpcom_get_avatar_url( $post->post_author, $size, $default, true );
  285. if ( $url && is_array( $url ) ) {
  286. $url = $url[0];
  287. }
  288. } else {
  289. $has_filter = has_filter( 'pre_option_show_avatars', '__return_true' );
  290. if ( !$has_filter ) {
  291. add_filter( 'pre_option_show_avatars', '__return_true' );
  292. }
  293. $avatar = get_avatar( $post->post_author, $size, $default );
  294. if ( !$has_filter ) {
  295. remove_filter( 'pre_option_show_avatars', '__return_true' );
  296. }
  297. if ( !$avatar ) {
  298. return array();
  299. }
  300. if ( !preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $matches ) ) {
  301. return array();
  302. }
  303. $url = wp_specialchars_decode( $matches[1], ENT_QUOTES );
  304. }
  305. return array( array(
  306. 'type' => 'image',
  307. 'from' => 'gravatar',
  308. 'src' => $url,
  309. 'src_width' => $size,
  310. 'src_height' => $size,
  311. 'href' => $permalink,
  312. ) );
  313. }
  314. /**
  315. * Run through the different methods that we have available to try to find a single good
  316. * display image for this post.
  317. * @param int $post_id
  318. * @param array $args Other arguments (currently width and height required for images where possible to determine)
  319. * @return Array containing details of the best image to be used
  320. */
  321. static function get_image( $post_id, $args = array() ) {
  322. $image = '';
  323. /**
  324. * Fires before we find a single good image for a specific post.
  325. *
  326. * @since 2.2.0
  327. *
  328. * @param int $post_id Post ID.
  329. */
  330. do_action( 'jetpack_postimages_pre_get_image', $post_id );
  331. $media = self::get_images( $post_id, $args );
  332. if ( is_array( $media ) ) {
  333. foreach ( $media as $item ) {
  334. if ( 'image' == $item['type'] ) {
  335. $image = $item;
  336. break;
  337. }
  338. }
  339. }
  340. /**
  341. * Fires after we find a single good image for a specific post.
  342. *
  343. * @since 2.2.0
  344. *
  345. * @param int $post_id Post ID.
  346. */
  347. do_action( 'jetpack_postimages_post_get_image', $post_id );
  348. return $image;
  349. }
  350. /**
  351. * Get an array containing a collection of possible images for this post, stopping once we hit a method
  352. * that returns something useful.
  353. * @param int $post_id
  354. * @param array $args Optional args, see defaults list for details
  355. * @return Array containing images that would be good for representing this post
  356. */
  357. static function get_images( $post_id, $args = array() ) {
  358. // Figure out which image to attach to this post.
  359. $media = false;
  360. /**
  361. * Filters the array of images that would be good for a specific post.
  362. * This filter is applied before options ($args) filter the original array.
  363. *
  364. * @since 2.0.0
  365. *
  366. * @param array $media Array of images that would be good for a specific post.
  367. * @param int $post_id Post ID.
  368. * @param array $args Array of options to get images.
  369. */
  370. $media = apply_filters( 'jetpack_images_pre_get_images', $media, $post_id, $args );
  371. if ( $media )
  372. return $media;
  373. $defaults = array(
  374. 'width' => 200, // Required minimum width (if possible to determine)
  375. 'height' => 200, // Required minimum height (if possible to determine)
  376. 'fallback_to_avatars' => false, // Optionally include Blavatar and Gravatar (in that order) in the image stack
  377. 'avatar_size' => 96, // Used for both Grav and Blav
  378. 'gravatar_default' => false, // Default image to use if we end up with no Gravatar
  379. 'from_thumbnail' => true, // Use these flags to specify which methods to use to find an image
  380. 'from_slideshow' => true,
  381. 'from_gallery' => true,
  382. 'from_attachment' => true,
  383. 'from_html' => true,
  384. 'html_content' => '' // HTML string to pass to from_html()
  385. );
  386. $args = wp_parse_args( $args, $defaults );
  387. $media = false;
  388. if ( $args['from_thumbnail'] )
  389. $media = self::from_thumbnail( $post_id, $args['width'], $args['height'] );
  390. if ( !$media && $args['from_slideshow'] )
  391. $media = self::from_slideshow( $post_id, $args['width'], $args['height'] );
  392. if ( !$media && $args['from_gallery'] )
  393. $media = self::from_gallery( $post_id );
  394. if ( !$media && $args['from_attachment'] )
  395. $media = self::from_attachment( $post_id, $args['width'], $args['height'] );
  396. if ( !$media && $args['from_html'] ) {
  397. if ( empty( $args['html_content'] ) )
  398. $media = self::from_html( $post_id ); // Use the post_id, which will load the content
  399. else
  400. $media = self::from_html( $args['html_content'] ); // If html_content is provided, use that
  401. }
  402. if ( !$media && $args['fallback_to_avatars'] ) {
  403. $media = self::from_blavatar( $post_id, $args['avatar_size'] );
  404. if ( !$media )
  405. $media = self::from_gravatar( $post_id, $args['avatar_size'], $args['gravatar_default'] );
  406. }
  407. /**
  408. * Filters the array of images that would be good for a specific post.
  409. * This filter is applied after options ($args) filter the original array.
  410. *
  411. * @since 2.0.0
  412. *
  413. * @param array $media Array of images that would be good for a specific post.
  414. * @param int $post_id Post ID.
  415. * @param array $args Array of options to get images.
  416. */
  417. return apply_filters( 'jetpack_images_get_images', $media, $post_id, $args );
  418. }
  419. /**
  420. * Takes an image URL and pixel dimensions then returns a URL for the
  421. * resized and croped image.
  422. *
  423. * @param string $src
  424. * @param int $dimension
  425. * @return string Transformed image URL
  426. */
  427. static function fit_image_url( $src, $width, $height ) {
  428. $width = (int) $width;
  429. $height = (int) $height;
  430. // Umm...
  431. if ( $width < 1 || $height < 1 ) {
  432. return $src;
  433. }
  434. // See if we should bypass WordPress.com SaaS resizing
  435. if ( has_filter( 'jetpack_images_fit_image_url_override' ) ) {
  436. /**
  437. * Filters the image URL used after dimensions are set by Photon.
  438. *
  439. * @since 3.3.0
  440. *
  441. * @param string $src Image URL.
  442. * @param int $width Image width.
  443. * @param int $width Image height.
  444. */
  445. return apply_filters( 'jetpack_images_fit_image_url_override', $src, $width, $height );
  446. }
  447. // If WPCOM hosted image use native transformations
  448. $img_host = parse_url( $src, PHP_URL_HOST );
  449. if ( '.files.wordpress.com' == substr( $img_host, -20 ) ) {
  450. return add_query_arg( array( 'w' => $width, 'h' => $height, 'crop' => 1 ), $src );
  451. }
  452. // Use Photon magic
  453. if( function_exists( 'jetpack_photon_url' ) ) {
  454. return jetpack_photon_url( $src, array( 'resize' => "$width,$height" ) );
  455. }
  456. // Arg... no way to resize image using WordPress.com infrastructure!
  457. return $src;
  458. }
  459. }