Nessuna descrizione

class.jetpack-post-images.php 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. }
  42. $start = strpos( $slideshow, '[', $pos );
  43. $end = strpos( $slideshow, ']', $start );
  44. $post_images = json_decode( wp_specialchars_decode( str_replace( "'", '"', substr( $slideshow, $start, $end - $start + 1 ) ), ENT_QUOTES ) ); // parse via JSON
  45. // If the JSON didn't decode don't try and act on it.
  46. if ( is_array( $post_images ) ) {
  47. foreach ( $post_images as $post_image ) {
  48. if ( ! $post_image_id = absint( $post_image->id ) ) {
  49. continue;
  50. }
  51. $meta = wp_get_attachment_metadata( $post_image_id );
  52. // Must be larger than 200x200 (or user-specified)
  53. if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) {
  54. continue;
  55. }
  56. if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) {
  57. continue;
  58. }
  59. $url = wp_get_attachment_url( $post_image_id );
  60. $images[] = array(
  61. 'type' => 'image',
  62. 'from' => 'slideshow',
  63. 'src' => $url,
  64. 'src_width' => $meta['width'],
  65. 'src_height' => $meta['height'],
  66. 'href' => $permalink,
  67. );
  68. }
  69. }
  70. }
  71. ob_end_clean();
  72. // Operator: Main screen turn on
  73. $GLOBALS['shortcode_tags'] = $old_shortcodes;
  74. $GLOBALS['post'] = $old_post;
  75. return $images;
  76. }
  77. /**
  78. * If a gallery is detected, then get all the images from it.
  79. */
  80. static function from_gallery( $post_id ) {
  81. $images = array();
  82. $post = get_post( $post_id );
  83. if ( ! $post ) {
  84. return $images;
  85. }
  86. if ( ! empty( $post->post_password ) ) {
  87. return $images;
  88. }
  89. $permalink = get_permalink( $post->ID );
  90. /**
  91. * Juggle global post object because the gallery shortcode uses the
  92. * global object.
  93. *
  94. * See core ticket:
  95. * https://core.trac.wordpress.org/ticket/39304
  96. */
  97. if ( isset( $GLOBALS['post'] ) ) {
  98. $juggle_post = $GLOBALS['post'];
  99. $GLOBALS['post'] = $post;
  100. $galleries = get_post_galleries( $post->ID, false );
  101. $GLOBALS['post'] = $juggle_post;
  102. } else {
  103. $GLOBALS['post'] = $post;
  104. $galleries = get_post_galleries( $post->ID, false );
  105. unset( $GLOBALS['post'] );
  106. }
  107. foreach ( $galleries as $gallery ) {
  108. if ( isset( $gallery['type'] ) && 'slideshow' === $gallery['type'] && ! empty( $gallery['ids'] ) ) {
  109. $image_ids = explode( ',', $gallery['ids'] );
  110. $image_size = isset( $gallery['size'] ) ? $gallery['size'] : 'thumbnail';
  111. foreach ( $image_ids as $image_id ) {
  112. $image = wp_get_attachment_image_src( $image_id, $image_size );
  113. if ( ! empty( $image[0] ) ) {
  114. list( $raw_src ) = explode( '?', $image[0] ); // pull off any Query string (?w=250)
  115. $raw_src = wp_specialchars_decode( $raw_src ); // rawify it
  116. $raw_src = esc_url_raw( $raw_src ); // clean it
  117. $images[] = array(
  118. 'type' => 'image',
  119. 'from' => 'gallery',
  120. 'src' => $raw_src,
  121. 'href' => $permalink,
  122. );
  123. }
  124. }
  125. } elseif ( ! empty( $gallery['src'] ) ) {
  126. foreach ( $gallery['src'] as $src ) {
  127. list( $raw_src ) = explode( '?', $src ); // pull off any Query string (?w=250)
  128. $raw_src = wp_specialchars_decode( $raw_src ); // rawify it
  129. $raw_src = esc_url_raw( $raw_src ); // clean it
  130. $images[] = array(
  131. 'type' => 'image',
  132. 'from' => 'gallery',
  133. 'src' => $raw_src,
  134. 'href' => $permalink,
  135. );
  136. }
  137. }
  138. }
  139. return $images;
  140. }
  141. /**
  142. * Get attachment images for a specified post and return them. Also make sure
  143. * their dimensions are at or above a required minimum.
  144. */
  145. static function from_attachment( $post_id, $width = 200, $height = 200 ) {
  146. $images = array();
  147. $post = get_post( $post_id );
  148. if ( ! empty( $post->post_password ) ) {
  149. return $images;
  150. }
  151. $post_images = get_posts(
  152. array(
  153. 'post_parent' => $post_id, // Must be children of post
  154. 'numberposts' => 5, // No more than 5
  155. 'post_type' => 'attachment', // Must be attachments
  156. 'post_mime_type' => 'image', // Must be images
  157. 'suppress_filters' => false,
  158. )
  159. );
  160. if ( ! $post_images ) {
  161. return $images;
  162. }
  163. $permalink = get_permalink( $post_id );
  164. foreach ( $post_images as $post_image ) {
  165. $current_image = self::get_attachment_data( $post_image->ID, $permalink, $width, $height );
  166. if ( false !== $current_image ) {
  167. $images[] = $current_image;
  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. foreach ( $html_images as $html_image ) {
  178. $src = wp_parse_url( $html_image['src'] );
  179. // strip off any query strings from src
  180. if ( ! empty( $src['scheme'] ) && ! empty( $src['host'] ) ) {
  181. $inserted_images[] = $src['scheme'] . '://' . $src['host'] . $src['path'];
  182. } elseif ( ! empty( $src['host'] ) ) {
  183. $inserted_images[] = set_url_scheme( 'http://' . $src['host'] . $src['path'] );
  184. } else {
  185. $inserted_images[] = site_url( '/' ) . $src['path'];
  186. }
  187. }
  188. foreach ( $images as $i => $image ) {
  189. if ( ! in_array( $image['src'], $inserted_images ) ) {
  190. unset( $images[ $i ] );
  191. }
  192. }
  193. return $images;
  194. }
  195. /**
  196. * Check if a Featured Image is set for this post, and return it in a similar
  197. * format to the other images?_from_*() methods.
  198. *
  199. * @param int $post_id The post ID to check
  200. * @return array containing details of the Featured Image, or empty array if none.
  201. */
  202. static function from_thumbnail( $post_id, $width = 200, $height = 200 ) {
  203. $images = array();
  204. $post = get_post( $post_id );
  205. if ( ! empty( $post->post_password ) ) {
  206. return $images;
  207. }
  208. if ( 'attachment' === get_post_type( $post ) && wp_attachment_is_image( $post ) ) {
  209. $thumb = $post_id;
  210. } else {
  211. $thumb = get_post_thumbnail_id( $post );
  212. }
  213. if ( $thumb ) {
  214. $meta = wp_get_attachment_metadata( $thumb );
  215. // Must be larger than requested minimums
  216. if ( ! isset( $meta['width'] ) || $meta['width'] < $width ) {
  217. return $images;
  218. }
  219. if ( ! isset( $meta['height'] ) || $meta['height'] < $height ) {
  220. return $images;
  221. }
  222. $too_big = ( ( ! empty( $meta['width'] ) && $meta['width'] > 1200 ) || ( ! empty( $meta['height'] ) && $meta['height'] > 1200 ) );
  223. if (
  224. $too_big &&
  225. (
  226. ( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'photon' ) ) ||
  227. ( defined( 'IS_WPCOM' ) && IS_WPCOM )
  228. )
  229. ) {
  230. $img_src = wp_get_attachment_image_src( $thumb, array( 1200, 1200 ) );
  231. } else {
  232. $img_src = wp_get_attachment_image_src( $thumb, 'full' );
  233. }
  234. if ( ! is_array( $img_src ) ) {
  235. // If wp_get_attachment_image_src returns false but we know that there should be an image that could be used.
  236. // we try a bit harder and user the data that we have.
  237. $thumb_post_data = get_post( $thumb );
  238. $img_src = array( $thumb_post_data->guid, $meta['width'], $meta['height'] );
  239. }
  240. $url = $img_src[0];
  241. $images = array(
  242. array( // Other methods below all return an array of arrays
  243. 'type' => 'image',
  244. 'from' => 'thumbnail',
  245. 'src' => $url,
  246. 'src_width' => $img_src[1],
  247. 'src_height' => $img_src[2],
  248. 'href' => get_permalink( $thumb ),
  249. 'alt_text' => self::get_alt_text( $thumb ),
  250. ),
  251. );
  252. }
  253. if ( empty( $images ) && ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
  254. $meta_thumbnail = get_post_meta( $post_id, '_jetpack_post_thumbnail', true );
  255. if ( ! empty( $meta_thumbnail ) ) {
  256. if ( ! isset( $meta_thumbnail['width'] ) || $meta_thumbnail['width'] < $width ) {
  257. return $images;
  258. }
  259. if ( ! isset( $meta_thumbnail['height'] ) || $meta_thumbnail['height'] < $height ) {
  260. return $images;
  261. }
  262. $images = array(
  263. array( // Other methods below all return an array of arrays
  264. 'type' => 'image',
  265. 'from' => 'thumbnail',
  266. 'src' => $meta_thumbnail['URL'],
  267. 'src_width' => $meta_thumbnail['width'],
  268. 'src_height' => $meta_thumbnail['height'],
  269. 'href' => $meta_thumbnail['URL'],
  270. 'alt_text' => self::get_alt_text( $thumb ),
  271. ),
  272. );
  273. }
  274. }
  275. return $images;
  276. }
  277. /**
  278. * Get images from Gutenberg Image blocks.
  279. *
  280. * @since 6.9.0
  281. *
  282. * @param mixed $html_or_id The HTML string to parse for images, or a post id.
  283. * @param int $width Minimum Image width.
  284. * @param int $height Minimum Image height.
  285. */
  286. public static function from_blocks( $html_or_id, $width = 200, $height = 200 ) {
  287. $images = array();
  288. $html_info = self::get_post_html( $html_or_id );
  289. if ( empty( $html_info['html'] ) ) {
  290. return $images;
  291. }
  292. // Look for block information in the HTML.
  293. $blocks = parse_blocks( $html_info['html'] );
  294. if ( empty( $blocks ) ) {
  295. return $images;
  296. }
  297. /*
  298. * Let's loop through our blocks.
  299. * Some blocks may include some other blocks. Let's go 2 levels deep to look for blocks
  300. * that we support and that may include images (see get_images_from_block)
  301. *
  302. * @to-do: instead of looping manually (that's a lot of if and loops), search recursively instead.
  303. */
  304. foreach ( $blocks as $block ) {
  305. if ( ! self::is_nested_block( $block ) || 'core/media-text' === $block['blockName'] ) {
  306. $images = self::get_images_from_block( $images, $block, $html_info, $width, $height );
  307. } else {
  308. foreach ( $block['innerBlocks'] as $inner_block ) {
  309. if ( ! self::is_nested_block( $inner_block ) ) {
  310. $images = self::get_images_from_block( $images, $inner_block, $html_info, $width, $height );
  311. } else {
  312. foreach ( $inner_block['innerBlocks'] as $inner_inner_block ) {
  313. $images = self::get_images_from_block( $images, $inner_inner_block, $html_info, $width, $height );
  314. }
  315. }
  316. }
  317. }
  318. }
  319. /**
  320. * Returning a filtered array because get_attachment_data returns false
  321. * for unsuccessful attempts.
  322. */
  323. return array_filter( $images );
  324. }
  325. /**
  326. * Very raw -- just parse the HTML and pull out any/all img tags and return their src
  327. *
  328. * @param mixed $html_or_id The HTML string to parse for images, or a post id.
  329. * @param int $width Minimum Image width.
  330. * @param int $height Minimum Image height.
  331. *
  332. * @uses DOMDocument
  333. *
  334. * @return array containing images
  335. */
  336. static function from_html( $html_or_id, $width = 200, $height = 200 ) {
  337. $images = array();
  338. $html_info = self::get_post_html( $html_or_id );
  339. if ( empty( $html_info['html'] ) ) {
  340. return $images;
  341. }
  342. // Do not go any further if DOMDocument is disabled on the server.
  343. if ( ! class_exists( 'DOMDocument' ) ) {
  344. return $images;
  345. }
  346. // Let's grab all image tags from the HTML.
  347. $dom_doc = new DOMDocument();
  348. // The @ is not enough to suppress errors when dealing with libxml,
  349. // we have to tell it directly how we want to handle errors.
  350. libxml_use_internal_errors( true );
  351. @$dom_doc->loadHTML( $html_info['html'] );
  352. libxml_use_internal_errors( false );
  353. $image_tags = $dom_doc->getElementsByTagName( 'img' );
  354. // For each image Tag, make sure it can be added to the $images array, and add it.
  355. foreach ( $image_tags as $image_tag ) {
  356. $img_src = $image_tag->getAttribute( 'src' );
  357. if ( empty( $img_src ) ) {
  358. continue;
  359. }
  360. // Do not grab smiley images that were automatically created by WP when entering text smilies.
  361. if ( stripos( $img_src, '/smilies/' ) ) {
  362. continue;
  363. }
  364. $meta = array(
  365. 'width' => (int) $image_tag->getAttribute( 'width' ),
  366. 'height' => (int) $image_tag->getAttribute( 'height' ),
  367. 'alt_text' => $image_tag->getAttribute( 'alt' ),
  368. );
  369. /**
  370. * Filters the switch to ignore minimum image size requirements. Can be used
  371. * to add custom logic to image dimensions, like only enforcing one of the dimensions,
  372. * or disabling it entirely.
  373. *
  374. * @since 6.4.0
  375. *
  376. * @param bool $ignore Should the image dimensions be ignored?
  377. * @param array $meta Array containing image dimensions parsed from the markup.
  378. */
  379. $ignore_dimensions = apply_filters( 'jetpack_postimages_ignore_minimum_dimensions', false, $meta );
  380. // Must be larger than 200x200 (or user-specified).
  381. if (
  382. ! $ignore_dimensions
  383. && (
  384. empty( $meta['width'] )
  385. || empty( $meta['height'] )
  386. || $meta['width'] < $width
  387. || $meta['height'] < $height
  388. )
  389. ) {
  390. continue;
  391. }
  392. $images[] = array(
  393. 'type' => 'image',
  394. 'from' => 'html',
  395. 'src' => $img_src,
  396. 'src_width' => $meta['width'],
  397. 'src_height' => $meta['height'],
  398. 'href' => $html_info['post_url'],
  399. 'alt_text' => $meta['alt_text'],
  400. );
  401. }
  402. return $images;
  403. }
  404. /**
  405. * @param int $post_id The post ID to check
  406. * @param int $size
  407. * @return array containing details of the image, or empty array if none.
  408. */
  409. static function from_blavatar( $post_id, $size = 96 ) {
  410. $permalink = get_permalink( $post_id );
  411. if ( function_exists( 'blavatar_domain' ) && function_exists( 'blavatar_exists' ) && function_exists( 'blavatar_url' ) ) {
  412. $domain = blavatar_domain( $permalink );
  413. if ( ! blavatar_exists( $domain ) ) {
  414. return array();
  415. }
  416. $url = blavatar_url( $domain, 'img', $size );
  417. } else {
  418. $url = get_site_icon_url( $size );
  419. if ( ! $url ) {
  420. return array();
  421. }
  422. }
  423. return array(
  424. array(
  425. 'type' => 'image',
  426. 'from' => 'blavatar',
  427. 'src' => $url,
  428. 'src_width' => $size,
  429. 'src_height' => $size,
  430. 'href' => $permalink,
  431. 'alt_text' => '',
  432. ),
  433. );
  434. }
  435. /**
  436. * Gets a post image from the author avatar.
  437. *
  438. * @param int $post_id The post ID to check.
  439. * @param int $size The size of the avatar to get.
  440. * @param string $default The default image to use.
  441. * @return array containing details of the image, or empty array if none.
  442. */
  443. static function from_gravatar( $post_id, $size = 96, $default = false ) {
  444. $post = get_post( $post_id );
  445. $permalink = get_permalink( $post_id );
  446. if ( function_exists( 'wpcom_get_avatar_url' ) ) {
  447. $url = wpcom_get_avatar_url( $post->post_author, $size, $default, true );
  448. if ( $url && is_array( $url ) ) {
  449. $url = $url[0];
  450. }
  451. } else {
  452. $url = get_avatar_url(
  453. $post->post_author,
  454. array(
  455. 'size' => $size,
  456. 'default' => $default,
  457. )
  458. );
  459. }
  460. return array(
  461. array(
  462. 'type' => 'image',
  463. 'from' => 'gravatar',
  464. 'src' => $url,
  465. 'src_width' => $size,
  466. 'src_height' => $size,
  467. 'href' => $permalink,
  468. 'alt_text' => '',
  469. ),
  470. );
  471. }
  472. /**
  473. * Run through the different methods that we have available to try to find a single good
  474. * display image for this post.
  475. *
  476. * @param int $post_id
  477. * @param array $args Other arguments (currently width and height required for images where possible to determine)
  478. * @return array containing details of the best image to be used
  479. */
  480. static function get_image( $post_id, $args = array() ) {
  481. $image = '';
  482. /**
  483. * Fires before we find a single good image for a specific post.
  484. *
  485. * @since 2.2.0
  486. *
  487. * @param int $post_id Post ID.
  488. */
  489. do_action( 'jetpack_postimages_pre_get_image', $post_id );
  490. $media = self::get_images( $post_id, $args );
  491. if ( is_array( $media ) ) {
  492. foreach ( $media as $item ) {
  493. if ( 'image' == $item['type'] ) {
  494. $image = $item;
  495. break;
  496. }
  497. }
  498. }
  499. /**
  500. * Fires after we find a single good image for a specific post.
  501. *
  502. * @since 2.2.0
  503. *
  504. * @param int $post_id Post ID.
  505. */
  506. do_action( 'jetpack_postimages_post_get_image', $post_id );
  507. return $image;
  508. }
  509. /**
  510. * Get an array containing a collection of possible images for this post, stopping once we hit a method
  511. * that returns something useful.
  512. *
  513. * @param int $post_id
  514. * @param array $args Optional args, see defaults list for details
  515. * @return array containing images that would be good for representing this post
  516. */
  517. static function get_images( $post_id, $args = array() ) {
  518. // Figure out which image to attach to this post.
  519. $media = false;
  520. /**
  521. * Filters the array of images that would be good for a specific post.
  522. * This filter is applied before options ($args) filter the original array.
  523. *
  524. * @since 2.0.0
  525. *
  526. * @param array $media Array of images that would be good for a specific post.
  527. * @param int $post_id Post ID.
  528. * @param array $args Array of options to get images.
  529. */
  530. $media = apply_filters( 'jetpack_images_pre_get_images', $media, $post_id, $args );
  531. if ( $media ) {
  532. return $media;
  533. }
  534. $defaults = array(
  535. 'width' => 200, // Required minimum width (if possible to determine)
  536. 'height' => 200, // Required minimum height (if possible to determine)
  537. 'fallback_to_avatars' => false, // Optionally include Blavatar and Gravatar (in that order) in the image stack
  538. 'avatar_size' => 96, // Used for both Grav and Blav
  539. 'gravatar_default' => false, // Default image to use if we end up with no Gravatar
  540. 'from_thumbnail' => true, // Use these flags to specify which methods to use to find an image
  541. 'from_slideshow' => true,
  542. 'from_gallery' => true,
  543. 'from_attachment' => true,
  544. 'from_blocks' => true,
  545. 'from_html' => true,
  546. 'html_content' => '', // HTML string to pass to from_html()
  547. );
  548. $args = wp_parse_args( $args, $defaults );
  549. $media = false;
  550. if ( $args['from_thumbnail'] ) {
  551. $media = self::from_thumbnail( $post_id, $args['width'], $args['height'] );
  552. }
  553. if ( ! $media && $args['from_slideshow'] ) {
  554. $media = self::from_slideshow( $post_id, $args['width'], $args['height'] );
  555. }
  556. if ( ! $media && $args['from_gallery'] ) {
  557. $media = self::from_gallery( $post_id );
  558. }
  559. if ( ! $media && $args['from_attachment'] ) {
  560. $media = self::from_attachment( $post_id, $args['width'], $args['height'] );
  561. }
  562. if ( ! $media && $args['from_blocks'] ) {
  563. if ( empty( $args['html_content'] ) ) {
  564. $media = self::from_blocks( $post_id, $args['width'], $args['height'] ); // Use the post_id, which will load the content
  565. } else {
  566. $media = self::from_blocks( $args['html_content'], $args['width'], $args['height'] ); // If html_content is provided, use that
  567. }
  568. }
  569. if ( ! $media && $args['from_html'] ) {
  570. if ( empty( $args['html_content'] ) ) {
  571. $media = self::from_html( $post_id, $args['width'], $args['height'] ); // Use the post_id, which will load the content
  572. } else {
  573. $media = self::from_html( $args['html_content'], $args['width'], $args['height'] ); // If html_content is provided, use that
  574. }
  575. }
  576. if ( ! $media && $args['fallback_to_avatars'] ) {
  577. $media = self::from_blavatar( $post_id, $args['avatar_size'] );
  578. if ( ! $media ) {
  579. $media = self::from_gravatar( $post_id, $args['avatar_size'], $args['gravatar_default'] );
  580. }
  581. }
  582. /**
  583. * Filters the array of images that would be good for a specific post.
  584. * This filter is applied after options ($args) filter the original array.
  585. *
  586. * @since 2.0.0
  587. *
  588. * @param array $media Array of images that would be good for a specific post.
  589. * @param int $post_id Post ID.
  590. * @param array $args Array of options to get images.
  591. */
  592. return apply_filters( 'jetpack_images_get_images', $media, $post_id, $args );
  593. }
  594. /**
  595. * Takes an image URL and pixel dimensions then returns a URL for the
  596. * resized and cropped image.
  597. *
  598. * @param string $src
  599. * @param int $dimension
  600. * @return string Transformed image URL
  601. */
  602. static function fit_image_url( $src, $width, $height ) {
  603. $width = (int) $width;
  604. $height = (int) $height;
  605. if ( $width < 1 || $height < 1 ) {
  606. return $src;
  607. }
  608. // See if we should bypass WordPress.com SaaS resizing
  609. if ( has_filter( 'jetpack_images_fit_image_url_override' ) ) {
  610. /**
  611. * Filters the image URL used after dimensions are set by Photon.
  612. *
  613. * @since 3.3.0
  614. *
  615. * @param string $src Image URL.
  616. * @param int $width Image width.
  617. * @param int $width Image height.
  618. */
  619. return apply_filters( 'jetpack_images_fit_image_url_override', $src, $width, $height );
  620. }
  621. // If WPCOM hosted image use native transformations
  622. $img_host = wp_parse_url( $src, PHP_URL_HOST );
  623. if ( '.files.wordpress.com' == substr( $img_host, -20 ) ) {
  624. return add_query_arg(
  625. array(
  626. 'w' => $width,
  627. 'h' => $height,
  628. 'crop' => 1,
  629. ),
  630. set_url_scheme( $src )
  631. );
  632. }
  633. // Use Photon magic
  634. if ( function_exists( 'jetpack_photon_url' ) ) {
  635. return jetpack_photon_url( $src, array( 'resize' => "$width,$height" ) );
  636. }
  637. // Arg... no way to resize image using WordPress.com infrastructure!
  638. return $src;
  639. }
  640. /**
  641. * Get HTML from given post content.
  642. *
  643. * @since 6.9.0
  644. *
  645. * @param mixed $html_or_id The HTML string to parse for images, or a post id.
  646. *
  647. * @return array $html_info {
  648. * @type string $html Post content.
  649. * @type string $post_url Post URL.
  650. * }
  651. */
  652. static function get_post_html( $html_or_id ) {
  653. if ( is_numeric( $html_or_id ) ) {
  654. $post = get_post( $html_or_id );
  655. if ( empty( $post ) || ! empty( $post->post_password ) ) {
  656. return '';
  657. }
  658. $html_info = array(
  659. 'html' => $post->post_content, // DO NOT apply the_content filters here, it will cause loops.
  660. 'post_url' => get_permalink( $post->ID ),
  661. );
  662. } else {
  663. $html_info = array(
  664. 'html' => $html_or_id,
  665. 'post_url' => '',
  666. );
  667. }
  668. return $html_info;
  669. }
  670. /**
  671. * Get info about a WordPress attachment.
  672. *
  673. * @since 6.9.0
  674. *
  675. * @param int $attachment_id Attachment ID.
  676. * @param string $post_url URL of the post, if we have one.
  677. * @param int $width Minimum Image width.
  678. * @param int $height Minimum Image height.
  679. * @return array|bool Image data or false if unavailable.
  680. */
  681. public static function get_attachment_data( $attachment_id, $post_url, $width, $height ) {
  682. if ( empty( $attachment_id ) ) {
  683. return false;
  684. }
  685. $meta = wp_get_attachment_metadata( $attachment_id );
  686. if ( empty( $meta ) ) {
  687. return false;
  688. }
  689. if ( ! empty( $meta['videopress'] ) ) {
  690. // Use poster image for VideoPress videos.
  691. $url = $meta['videopress']['poster'];
  692. $meta_width = $meta['videopress']['width'];
  693. $meta_height = $meta['videopress']['height'];
  694. } elseif ( ! empty( $meta['thumb'] ) ) {
  695. // On WordPress.com, VideoPress videos have a 'thumb' property with the
  696. // poster image filename instead.
  697. $media_url = wp_get_attachment_url( $attachment_id );
  698. $url = str_replace( wp_basename( $media_url ), $meta['thumb'], $media_url );
  699. $meta_width = $meta['width'];
  700. $meta_height = $meta['height'];
  701. } elseif ( wp_attachment_is( 'video', $attachment_id ) ) {
  702. // We don't have thumbnail images for non-VideoPress videos - skip them.
  703. return false;
  704. } else {
  705. if ( ! isset( $meta['width'] ) || ! isset( $meta['height'] ) ) {
  706. return false;
  707. }
  708. $url = wp_get_attachment_url( $attachment_id );
  709. $meta_width = $meta['width'];
  710. $meta_height = $meta['height'];
  711. }
  712. if ( $meta_width < $width || $meta_height < $height ) {
  713. return false;
  714. }
  715. return array(
  716. 'type' => 'image',
  717. 'from' => 'attachment',
  718. 'src' => $url,
  719. 'src_width' => $meta_width,
  720. 'src_height' => $meta_height,
  721. 'href' => $post_url,
  722. 'alt_text' => self::get_alt_text( $attachment_id ),
  723. );
  724. }
  725. /**
  726. * Get the alt text for an image or other media from the Media Library.
  727. *
  728. * @since 7.1
  729. *
  730. * @param int $attachment_id The Post ID of the media.
  731. * @return string The alt text value or an emptry string.
  732. */
  733. public static function get_alt_text( $attachment_id ) {
  734. return get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
  735. }
  736. /**
  737. * Get an image from a block.
  738. *
  739. * @since 7.8.0
  740. *
  741. * @param array $images Images found.
  742. * @param array $block Block and its attributes.
  743. * @param array $html_info Info about the post where the block is found.
  744. * @param int $width Desired image width.
  745. * @param int $height Desired image height.
  746. *
  747. * @return array Array of images found.
  748. */
  749. private static function get_images_from_block( $images, $block, $html_info, $width, $height ) {
  750. /**
  751. * Parse content from Core Image blocks.
  752. * If it is an image block for an image hosted on our site, it will have an ID.
  753. * If it does not have an ID, let `from_html` parse that content later,
  754. * and extract an image if it has size parameters.
  755. */
  756. if (
  757. 'core/image' === $block['blockName']
  758. && ! empty( $block['attrs']['id'] )
  759. ) {
  760. $images[] = self::get_attachment_data( $block['attrs']['id'], $html_info['post_url'], $width, $height );
  761. } elseif (
  762. 'core/media-text' === $block['blockName']
  763. && ! empty( $block['attrs']['mediaId'] )
  764. ) {
  765. $images[] = self::get_attachment_data( $block['attrs']['mediaId'], $html_info['post_url'], $width, $height );
  766. } elseif (
  767. /**
  768. * Parse content from Core Gallery blocks as well from Jetpack's Tiled Gallery and Slideshow blocks.
  769. * Gallery blocks include the ID of each one of the images in the gallery.
  770. */
  771. in_array( $block['blockName'], array( 'core/gallery', 'jetpack/tiled-gallery', 'jetpack/slideshow' ), true )
  772. && ! empty( $block['attrs']['ids'] )
  773. ) {
  774. foreach ( $block['attrs']['ids'] as $img_id ) {
  775. $images[] = self::get_attachment_data( $img_id, $html_info['post_url'], $width, $height );
  776. }
  777. } elseif (
  778. /**
  779. * Parse content from Jetpack's Story block.
  780. */
  781. 'jetpack/story' === $block['blockName']
  782. && ! empty( $block['attrs']['mediaFiles'] )
  783. ) {
  784. foreach ( $block['attrs']['mediaFiles'] as $media_file ) {
  785. if ( ! empty( $media_file['id'] ) ) {
  786. $images[] = self::get_attachment_data( $media_file['id'], $html_info['post_url'], $width, $height );
  787. }
  788. }
  789. }
  790. return $images;
  791. }
  792. /**
  793. * Check if a block has inner blocks.
  794. *
  795. * @since 7.8.0
  796. *
  797. * @param array $block Block and its attributes.
  798. *
  799. * @return bool
  800. */
  801. private static function is_nested_block( $block ) {
  802. if ( ! empty( $block['innerBlocks'] ) ) {
  803. return true;
  804. }
  805. return false;
  806. }
  807. }