Bez popisu

tiled-gallery-item.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. abstract class Jetpack_Tiled_Gallery_Item {
  3. public $image;
  4. public function __construct( $attachment_image, $needs_attachment_link, $grayscale ) {
  5. $this->image = $attachment_image;
  6. $this->grayscale = $grayscale;
  7. $this->image_title = $this->image->post_title;
  8. $this->image_alt = get_post_meta( $this->image->ID, '_wp_attachment_image_alt', true );
  9. // If no Alt value, use the caption
  10. if ( empty( $this->image_alt ) && ! empty( $this->image->post_excerpt ) ) {
  11. $this->image_alt = trim( strip_tags( $this->image->post_excerpt ) );
  12. }
  13. // If still no Alt value, use the title
  14. if ( empty( $this->image_alt ) && ! empty( $this->image->post_title ) ) {
  15. $this->image_alt = trim( strip_tags( $this->image->post_title ) );
  16. }
  17. $this->orig_file = wp_get_attachment_url( $this->image->ID );
  18. $this->link = $needs_attachment_link
  19. ? get_attachment_link( $this->image->ID )
  20. // The filter will photonize the URL if and only if Photon is active
  21. : apply_filters( 'jetpack_photon_url', $this->orig_file );
  22. $img_args = array(
  23. 'w' => $this->image->width,
  24. 'h' => $this->image->height,
  25. );
  26. // If h and w are the same, there's a reasonably good chance the image will need cropping to avoid being stretched.
  27. if ( $this->image->height == $this->image->width ) {
  28. $img_args['crop'] = true;
  29. }
  30. // The function will always photonoize the URL (even if Photon is
  31. // not active). We need to photonize the URL to set the width/height.
  32. $this->img_src = jetpack_photon_url( $this->orig_file, $img_args );
  33. }
  34. public function fuzzy_image_meta() {
  35. $meta = wp_get_attachment_metadata( $this->image->ID );
  36. $img_meta = ( ! empty( $meta['image_meta'] ) ) ? (array) $meta['image_meta'] : array();
  37. if ( ! empty( $img_meta ) ) {
  38. foreach ( $img_meta as $k => $v ) {
  39. if ( 'latitude' == $k || 'longitude' == $k ) {
  40. unset( $img_meta[ $k ] );
  41. }
  42. }
  43. }
  44. return $img_meta;
  45. }
  46. public function meta_width() {
  47. $meta = wp_get_attachment_metadata( $this->image->ID );
  48. return isset( $meta['width'] ) ? (int) $meta['width'] : '';
  49. }
  50. public function meta_height() {
  51. $meta = wp_get_attachment_metadata( $this->image->ID );
  52. return isset( $meta['height'] ) ? (int) $meta['height'] : '';
  53. }
  54. public function medium_file() {
  55. $medium_file_info = wp_get_attachment_image_src( $this->image->ID, 'medium' );
  56. $medium_file = isset( $medium_file_info[0] ) ? $medium_file_info[0] : '';
  57. return $medium_file;
  58. }
  59. public function large_file() {
  60. $large_file_info = wp_get_attachment_image_src( $this->image->ID, 'large' );
  61. $large_file = isset( $large_file_info[0] ) ? $large_file_info[0] : '';
  62. return $large_file;
  63. }
  64. }
  65. class Jetpack_Tiled_Gallery_Rectangular_Item extends Jetpack_Tiled_Gallery_Item {
  66. public function __construct( $attachment_image, $needs_attachment_link, $grayscale ) {
  67. parent::__construct( $attachment_image, $needs_attachment_link, $grayscale );
  68. $this->img_src_grayscale = jetpack_photon_url( $this->img_src, array( 'filter' => 'grayscale' ) );
  69. $this->size = 'large';
  70. if ( $this->image->width < 250 ) {
  71. $this->size = 'small';
  72. }
  73. }
  74. }
  75. class Jetpack_Tiled_Gallery_Square_Item extends Jetpack_Tiled_Gallery_Item {
  76. public function __construct( $attachment_image, $needs_attachment_link, $grayscale ) {
  77. parent::__construct( $attachment_image, $needs_attachment_link, $grayscale );
  78. $this->img_src_grayscale = jetpack_photon_url(
  79. $this->img_src,
  80. array(
  81. 'filter' => 'grayscale',
  82. 'resize' => array(
  83. $this->image->width,
  84. $this->image->height,
  85. ),
  86. )
  87. );
  88. }
  89. }
  90. class Jetpack_Tiled_Gallery_Circle_Item extends Jetpack_Tiled_Gallery_Square_Item {
  91. }