embed-content.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Contains the post embed content template part
  4. *
  5. * When a post is embedded in an iframe, this file is used to create the content template part
  6. * output if the active theme does not include an embed-content.php template.
  7. *
  8. * @package WordPress
  9. * @subpackage Theme_Compat
  10. * @since 4.5.0
  11. */
  12. ?>
  13. <div <?php post_class( 'wp-embed' ); ?>>
  14. <?php
  15. $thumbnail_id = 0;
  16. if ( has_post_thumbnail() ) {
  17. $thumbnail_id = get_post_thumbnail_id();
  18. }
  19. if ( 'attachment' === get_post_type() && wp_attachment_is_image() ) {
  20. $thumbnail_id = get_the_ID();
  21. }
  22. /**
  23. * Filters the thumbnail image ID for use in the embed template.
  24. *
  25. * @since 4.9.0
  26. *
  27. * @param int|false $thumbnail_id Attachment ID, or false if there is none.
  28. */
  29. $thumbnail_id = apply_filters( 'embed_thumbnail_id', $thumbnail_id );
  30. if ( $thumbnail_id ) {
  31. $aspect_ratio = 1;
  32. $measurements = array( 1, 1 );
  33. $image_size = 'full'; // Fallback.
  34. $meta = wp_get_attachment_metadata( $thumbnail_id );
  35. if ( ! empty( $meta['sizes'] ) ) {
  36. foreach ( $meta['sizes'] as $size => $data ) {
  37. if ( $data['height'] > 0 && $data['width'] / $data['height'] > $aspect_ratio ) {
  38. $aspect_ratio = $data['width'] / $data['height'];
  39. $measurements = array( $data['width'], $data['height'] );
  40. $image_size = $size;
  41. }
  42. }
  43. }
  44. /**
  45. * Filters the thumbnail image size for use in the embed template.
  46. *
  47. * @since 4.4.0
  48. * @since 4.5.0 Added `$thumbnail_id` parameter.
  49. *
  50. * @param string $image_size Thumbnail image size.
  51. * @param int $thumbnail_id Attachment ID.
  52. */
  53. $image_size = apply_filters( 'embed_thumbnail_image_size', $image_size, $thumbnail_id );
  54. $shape = $measurements[0] / $measurements[1] >= 1.75 ? 'rectangular' : 'square';
  55. /**
  56. * Filters the thumbnail shape for use in the embed template.
  57. *
  58. * Rectangular images are shown above the title while square images
  59. * are shown next to the content.
  60. *
  61. * @since 4.4.0
  62. * @since 4.5.0 Added `$thumbnail_id` parameter.
  63. *
  64. * @param string $shape Thumbnail image shape. Either 'rectangular' or 'square'.
  65. * @param int $thumbnail_id Attachment ID.
  66. */
  67. $shape = apply_filters( 'embed_thumbnail_image_shape', $shape, $thumbnail_id );
  68. }
  69. if ( $thumbnail_id && 'rectangular' === $shape ) :
  70. ?>
  71. <div class="wp-embed-featured-image rectangular">
  72. <a href="<?php the_permalink(); ?>" target="_top">
  73. <?php echo wp_get_attachment_image( $thumbnail_id, $image_size ); ?>
  74. </a>
  75. </div>
  76. <?php endif; ?>
  77. <p class="wp-embed-heading">
  78. <a href="<?php the_permalink(); ?>" target="_top">
  79. <?php the_title(); ?>
  80. </a>
  81. </p>
  82. <?php if ( $thumbnail_id && 'square' === $shape ) : ?>
  83. <div class="wp-embed-featured-image square">
  84. <a href="<?php the_permalink(); ?>" target="_top">
  85. <?php echo wp_get_attachment_image( $thumbnail_id, $image_size ); ?>
  86. </a>
  87. </div>
  88. <?php endif; ?>
  89. <div class="wp-embed-excerpt"><?php the_excerpt_embed(); ?></div>
  90. <?php
  91. /**
  92. * Prints additional content after the embed excerpt.
  93. *
  94. * @since 4.4.0
  95. */
  96. do_action( 'embed_content' );
  97. ?>
  98. <div class="wp-embed-footer">
  99. <?php the_embed_site_title(); ?>
  100. <div class="wp-embed-meta">
  101. <?php
  102. /**
  103. * Prints additional meta content in the embed template.
  104. *
  105. * @since 4.4.0
  106. */
  107. do_action( 'embed_content_meta' );
  108. ?>
  109. </div>
  110. </div>
  111. </div>
  112. <?php