image.php (3813B)
1 <?php 2 namespace Elementor; 3 4 if ( ! defined( 'ABSPATH' ) ) { 5 exit; // Exit if accessed directly. 6 } 7 8 /** 9 * Elementor images manager. 10 * 11 * Elementor images manager handler class is responsible for retrieving image 12 * details. 13 * 14 * @since 1.0.0 15 */ 16 class Images_Manager { 17 18 /** 19 * Get images details. 20 * 21 * Retrieve details for all the images. 22 * 23 * Fired by `wp_ajax_elementor_get_images_details` action. 24 * 25 * @since 1.0.0 26 * @access public 27 */ 28 public function get_images_details() { 29 // PHPCS - Already validated by wp_ajax. 30 $items = $_POST['items']; // phpcs:ignore WordPress.Security.NonceVerification.Missing 31 $urls = []; 32 33 foreach ( $items as $item ) { 34 $urls[ $item['id'] ] = $this->get_details( $item['id'], $item['size'], $item['is_first_time'] ); 35 } 36 37 wp_send_json_success( $urls ); 38 } 39 40 /** 41 * Get image details. 42 * 43 * Retrieve single image details. 44 * 45 * Fired by `wp_ajax_elementor_get_image_details` action. 46 * 47 * @since 1.0.0 48 * @access public 49 * 50 * @param string $id Image attachment ID. 51 * @param string|array $size Image size. Accepts any valid image 52 * size, or an array of width and height 53 * values in pixels (in that order). 54 * @param string $is_first_time Set 'true' string to force reloading 55 * all image sizes. 56 * 57 * @return array URLs with different image sizes. 58 */ 59 public function get_details( $id, $size, $is_first_time ) { 60 if ( ! class_exists( 'Group_Control_Image_Size' ) ) { 61 require_once ELEMENTOR_PATH . '/includes/controls/groups/image-size.php'; 62 } 63 64 if ( 'true' === $is_first_time ) { 65 $sizes = get_intermediate_image_sizes(); 66 $sizes[] = 'full'; 67 } else { 68 $sizes = []; 69 } 70 71 $sizes[] = $size; 72 $urls = []; 73 foreach ( $sizes as $size ) { 74 if ( 0 === strpos( $size, 'custom_' ) ) { 75 preg_match( '/custom_(\d*)x(\d*)/', $size, $matches ); 76 77 $instance = [ 78 'image_size' => 'custom', 79 'image_custom_dimension' => [ 80 'width' => $matches[1], 81 'height' => $matches[2], 82 ], 83 ]; 84 85 $urls[ $size ] = Group_Control_Image_Size::get_attachment_image_src( $id, 'image', $instance ); 86 } else { 87 $urls[ $size ] = wp_get_attachment_image_src( $id, $size )[0]; 88 } 89 } 90 91 return $urls; 92 } 93 94 /** 95 * Get Light-Box Image Attributes 96 * 97 * Used to retrieve an array of image attributes to be used for displaying an image in Elementor's Light Box module. 98 * 99 * @param int $id The ID of the image 100 * 101 * @return array An array of image attributes including `title` and `description`. 102 * @since 2.9.0 103 * @access public 104 */ 105 106 public function get_lightbox_image_attributes( $id ) { 107 $attributes = []; 108 $kit = Plugin::$instance->kits_manager->get_active_kit(); 109 $lightbox_title_src = $kit->get_settings( 'lightbox_title_src' ); 110 $lightbox_description_src = $kit->get_settings( 'lightbox_description_src' ); 111 $attachment = get_post( $id ); 112 $image_data = [ 113 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), 114 'caption' => $attachment->post_excerpt, 115 'description' => $attachment->post_content, 116 'title' => $attachment->post_title, 117 ]; 118 119 if ( $lightbox_title_src && $image_data[ $lightbox_title_src ] ) { 120 $attributes['title'] = $image_data[ $lightbox_title_src ]; 121 } 122 123 if ( $lightbox_description_src && $image_data[ $lightbox_description_src ] ) { 124 $attributes['description'] = $image_data[ $lightbox_description_src ]; 125 } 126 127 return $attributes; 128 } 129 130 /** 131 * Images manager constructor. 132 * 133 * Initializing Elementor images manager. 134 * 135 * @since 1.0.0 136 * @access public 137 */ 138 public function __construct() { 139 add_action( 'wp_ajax_elementor_get_images_details', [ $this, 'get_images_details' ] ); 140 } 141 }