featured-images-fallback.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Get one image from a specified post in the following order:
  4. * Featured Image then first image from the_content HTML
  5. * and filter the post_thumbnail_html
  6. *
  7. * @param string $html The HTML for the image markup.
  8. * @param int $post_id The post ID to check.
  9. * @param int $post_thumbnail_id The ID of the featured image.
  10. * @param string $size The image size to return, defaults to 'post-thumbnail'.
  11. * @param string|array $attr Optional. Query string or array of attributes.
  12. *
  13. * @return string $html Thumbnail image with markup.
  14. */
  15. function jetpack_featured_images_fallback_get_image( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
  16. $opts = jetpack_featured_images_get_settings();
  17. if ( ! empty( $html ) || (bool) 1 !== (bool) $opts['fallback-option'] ) {
  18. return trim( $html );
  19. }
  20. if ( jetpack_featured_images_should_load() ) {
  21. if (
  22. ( true === $opts['archive'] && ( is_home() || is_archive() || is_search() ) && ! $opts['archive-option'] )
  23. || ( true === $opts['post'] && is_single() && ! $opts['post-option'] )
  24. || ! $opts['fallback-option']
  25. ) {
  26. return trim( $html );
  27. }
  28. }
  29. if ( class_exists( 'Jetpack_PostImages' ) ) {
  30. global $_wp_additional_image_sizes;
  31. $args = array(
  32. 'from_thumbnail' => false,
  33. 'from_slideshow' => true,
  34. 'from_gallery' => true,
  35. 'from_attachment' => false,
  36. );
  37. $image = Jetpack_PostImages::get_image( $post_id, $args );
  38. if ( ! empty( $image ) ) {
  39. $image['width'] = '';
  40. $image['height'] = '';
  41. $image['crop'] = '';
  42. if ( array_key_exists( $size, $_wp_additional_image_sizes ) ) {
  43. $image['width'] = $_wp_additional_image_sizes[ $size ]['width'];
  44. $image['height'] = $_wp_additional_image_sizes[ $size ]['height'];
  45. $image['crop'] = $_wp_additional_image_sizes[ $size ]['crop'];
  46. }
  47. $image_src = Jetpack_PostImages::fit_image_url( $image['src'], $image['width'], $image['height'] );
  48. // Use the theme's crop setting rather than forcing to true
  49. $image_src = add_query_arg( 'crop', $image['crop'], $image_src );
  50. $html = '<img src="' . esc_url( $image_src ) . '" title="' . esc_attr( strip_tags( get_the_title() ) ) . '" class="attachment-' . esc_attr( $size ) . ' wp-post-image" />';
  51. return trim( $html );
  52. }
  53. }
  54. return trim( $html );
  55. }
  56. add_filter( 'post_thumbnail_html', 'jetpack_featured_images_fallback_get_image', 10, 5 );
  57. /**
  58. * Get URL of one image from a specified post in the following order:
  59. * Featured Image then first image from the_content HTML
  60. *
  61. * @param int $post_id The post ID to check.
  62. * @param int $post_thumbnail_id The ID of the featured image.
  63. * @param string $size The image size to return, defaults to 'post-thumbnail'.
  64. *
  65. * @return string|null $image_src The URL of the thumbnail image.
  66. */
  67. function jetpack_featured_images_fallback_get_image_src( $post_id, $post_thumbnail_id, $size ) {
  68. $image_src = wp_get_attachment_image_src( $post_thumbnail_id, $size );
  69. $image_src = ( ! empty( $image_src[0] ) ) ? $image_src[0] : null;
  70. $opts = jetpack_featured_images_get_settings();
  71. if ( ! empty( $image_src ) || (bool) 1 !== (bool) $opts['fallback-option'] ) {
  72. return esc_url( $image_src );
  73. }
  74. if ( jetpack_featured_images_should_load() ) {
  75. if ( ( true === $opts['archive'] && ( is_home() || is_archive() || is_search() ) && ! $opts['archive-option'] )
  76. || ( true === $opts['post'] && is_single() && ! $opts['post-option'] ) ) {
  77. return esc_url( $image_src );
  78. }
  79. }
  80. if ( class_exists( 'Jetpack_PostImages' ) ) {
  81. global $_wp_additional_image_sizes;
  82. $args = array(
  83. 'from_thumbnail' => false,
  84. 'from_slideshow' => true,
  85. 'from_gallery' => true,
  86. 'from_attachment' => false,
  87. );
  88. $image = Jetpack_PostImages::get_image( $post_id, $args );
  89. if ( ! empty( $image ) ) {
  90. $image['width'] = '';
  91. $image['height'] = '';
  92. $image['crop'] = '';
  93. if ( array_key_exists( $size, $_wp_additional_image_sizes ) ) {
  94. $image['width'] = $_wp_additional_image_sizes[ $size ]['width'];
  95. $image['height'] = $_wp_additional_image_sizes[ $size ]['height'];
  96. $image['crop'] = $_wp_additional_image_sizes[ $size ]['crop'];
  97. }
  98. $image_src = Jetpack_PostImages::fit_image_url( $image['src'], $image['width'], $image['height'] );
  99. // Use the theme's crop setting rather than forcing to true
  100. $image_src = add_query_arg( 'crop', $image['crop'], $image_src );
  101. return esc_url( $image_src );
  102. }
  103. }
  104. return esc_url( $image_src );
  105. }
  106. /**
  107. * Check if post has an image attached, including a fallback.
  108. *
  109. * @param int $post The post ID to check.
  110. *
  111. * @return bool
  112. */
  113. function jetpack_has_featured_image( $post = null ) {
  114. return (bool) get_the_post_thumbnail( $post );
  115. }
  116. /**
  117. * Adds custom class to the array of post classes.
  118. *
  119. * @param array $classes Classes for the post element.
  120. * @param array $class Optional. Comma separated list of additional classes.
  121. * @param array $post_id Unique The post ID to check
  122. *
  123. * @return array $classes
  124. */
  125. function jetpack_featured_images_post_class( $classes, $class, $post_id ) {
  126. $post_password_required = post_password_required( $post_id );
  127. $opts = jetpack_featured_images_get_settings();
  128. if ( jetpack_has_featured_image( $post_id ) && (bool) 1 === (bool) $opts['fallback-option'] && ! is_attachment() && ! $post_password_required && 'post' === get_post_type() ) {
  129. $classes[] = 'has-post-thumbnail';
  130. }
  131. return $classes;
  132. }
  133. add_filter( 'post_class', 'jetpack_featured_images_post_class', 10, 3 );