image.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor images manager.
  8. *
  9. * Elementor images manager handler class is responsible for retrieving image
  10. * details.
  11. *
  12. * @since 1.0.0
  13. */
  14. class Images_Manager {
  15. /**
  16. * Get images details.
  17. *
  18. * Retrieve details for all the images.
  19. *
  20. * Fired by `wp_ajax_elementor_get_images_details` action.
  21. *
  22. * @since 1.0.0
  23. * @access public
  24. */
  25. public function get_images_details() {
  26. $items = $_POST['items'];
  27. $urls = [];
  28. foreach ( $items as $item ) {
  29. $urls[ $item['id'] ] = $this->get_details( $item['id'], $item['size'], $item['is_first_time'] );
  30. }
  31. wp_send_json_success( $urls );
  32. }
  33. /**
  34. * Get image details.
  35. *
  36. * Retrieve single image details.
  37. *
  38. * Fired by `wp_ajax_elementor_get_image_details` action.
  39. *
  40. * @since 1.0.0
  41. * @access public
  42. *
  43. * @param string $id Image attachment ID.
  44. * @param string|array $size Image size. Accepts any valid image
  45. * size, or an array of width and height
  46. * values in pixels (in that order).
  47. * @param string $is_first_time Set 'true' string to force reloading
  48. * all image sizes.
  49. *
  50. * @return array URLs with different image sizes.
  51. */
  52. public function get_details( $id, $size, $is_first_time ) {
  53. if ( ! class_exists( 'Group_Control_Image_Size' ) ) {
  54. require_once ELEMENTOR_PATH . '/includes/controls/groups/image-size.php';
  55. }
  56. if ( 'true' === $is_first_time ) {
  57. $sizes = get_intermediate_image_sizes();
  58. $sizes[] = 'full';
  59. } else {
  60. $sizes = [];
  61. }
  62. $sizes[] = $size;
  63. $urls = [];
  64. foreach ( $sizes as $size ) {
  65. if ( 0 === strpos( $size, 'custom_' ) ) {
  66. preg_match( '/custom_(\d*)x(\d*)/', $size, $matches );
  67. $instance = [
  68. 'image_size' => 'custom',
  69. 'image_custom_dimension' => [
  70. 'width' => $matches[1],
  71. 'height' => $matches[2],
  72. ],
  73. ];
  74. $urls[ $size ] = Group_Control_Image_Size::get_attachment_image_src( $id, 'image', $instance );
  75. } else {
  76. $urls[ $size ] = wp_get_attachment_image_src( $id, $size )[0];
  77. }
  78. }
  79. return $urls;
  80. }
  81. /**
  82. * Images manager constructor.
  83. *
  84. * Initializing Elementor images manager.
  85. *
  86. * @since 1.0.0
  87. * @access public
  88. */
  89. public function __construct() {
  90. add_action( 'wp_ajax_elementor_get_images_details', [ $this, 'get_images_details' ] );
  91. }
  92. }