class-fl-builder-photo.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Helper class for working with photos.
  4. *
  5. * @since 1.0
  6. */
  7. final class FLBuilderPhoto {
  8. /**
  9. * Returns an array of data for sizes that are
  10. * defined for WordPress images.
  11. *
  12. * @since 1.0
  13. * @return array
  14. */
  15. static public function sizes() {
  16. global $_wp_additional_image_sizes;
  17. $sizes = array();
  18. foreach ( get_intermediate_image_sizes() as $size ) {
  19. // Hidden size added in 4.4 for responsive images. We don't need it.
  20. if ( 'medium_large' == $size ) {
  21. continue;
  22. }
  23. $sizes[ $size ] = array( 0, 0 );
  24. if ( in_array( $size, array( 'thumbnail', 'medium', 'large' ) ) ) {
  25. $sizes[ $size ][0] = get_option( $size . '_size_w' );
  26. $sizes[ $size ][1] = get_option( $size . '_size_h' );
  27. } elseif ( isset( $_wp_additional_image_sizes ) && isset( $_wp_additional_image_sizes[ $size ] ) ) {
  28. $sizes[ $size ] = array(
  29. $_wp_additional_image_sizes[ $size ]['width'],
  30. $_wp_additional_image_sizes[ $size ]['height'],
  31. );
  32. }
  33. }
  34. return $sizes;
  35. }
  36. /**
  37. * Returns an object with data for an attachment using
  38. * wp_prepare_attachment_for_js based on the provided id.
  39. *
  40. * @since 1.0
  41. * @param string $id The attachment id.
  42. * @return object
  43. */
  44. static public function get_attachment_data( $id ) {
  45. $data = wp_prepare_attachment_for_js( $id );
  46. if ( gettype( $data ) == 'array' ) {
  47. return json_decode( json_encode( $data ) );
  48. }
  49. return $data;
  50. }
  51. /**
  52. * Renders the thumb URL for a photo object.
  53. *
  54. * @since 1.0
  55. * @param object $photo An object with photo data.
  56. * @return void
  57. */
  58. static public function get_thumb( $photo ) {
  59. if ( empty( $photo ) ) {
  60. echo FL_BUILDER_URL . 'img/spacer.png';
  61. } elseif ( ! isset( $photo->sizes ) ) {
  62. echo $photo->url;
  63. } elseif ( ! empty( $photo->sizes->thumbnail ) ) {
  64. echo $photo->sizes->thumbnail->url;
  65. } else {
  66. echo $photo->sizes->full->url;
  67. }
  68. }
  69. /**
  70. * Renders the options for a photo select field.
  71. *
  72. * @since 1.0
  73. * @param string $selected The selected URL.
  74. * @param object $photo An object with photo data.
  75. * @return void
  76. */
  77. static public function get_src_options( $selected, $photo ) {
  78. if ( ! isset( $photo->sizes ) ) {
  79. echo '<option value="' . $photo->url . '" selected="selected">' . _x( 'Full Size', 'Image size.', 'fl-builder' ) . '</option>';
  80. } else {
  81. $titles = array(
  82. 'full' => _x( 'Full Size', 'Image size.', 'fl-builder' ),
  83. 'large' => _x( 'Large', 'Image size.', 'fl-builder' ),
  84. 'medium' => _x( 'Medium', 'Image size.', 'fl-builder' ),
  85. 'thumbnail' => _x( 'Thumbnail', 'Image size.', 'fl-builder' ),
  86. );
  87. foreach ( $photo->sizes as $key => $val ) {
  88. if ( ! isset( $titles[ $key ] ) ) {
  89. $titles[ $key ] = ucwords( str_replace( array( '_', '-' ), ' ', $key ) );
  90. }
  91. echo '<option value="' . $val->url . '" ' . selected( basename( $selected ), basename( $val->url ) ) . '>' . $titles[ $key ] . ' - ' . $val->width . ' x ' . $val->height . '</option>';
  92. }
  93. }
  94. }
  95. }