/var/www/html/wp-content/plugins/elementor/includes/managers/image.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace Elementor;

use 
Elementor\Core\Editor\Editor;
use 
Elementor\Core\Utils\Collection;

if ( ! 
defined'ABSPATH' ) ) {
    exit; 
// Exit if accessed directly.
}

/**
 * Elementor images manager.
 *
 * Elementor images manager handler class is responsible for retrieving image
 * details.
 *
 * @since 1.0.0
 */
class Images_Manager {

    
/**
     * Get images details.
     *
     * Retrieve details for all the images.
     *
     * Fired by `wp_ajax_elementor_get_images_details` action.
     *
     * @since 1.0.0
     * @access public
     */
    
public function get_images_details() {
        if ( ! 
current_user_can'publish_posts' ) ) {
            
wp_send_json_error'Permission denied' );
        }

        
// PHPCS - Already validated by wp_ajax.
        
$items Utils::get_super_global_value$_POST'items' ) ?? []; // phpcs:ignore WordPress.Security.NonceVerification.Missing
        
$urls  = [];

        foreach ( 
$items as $item ) {
            
$urls$item['id'] ] = $this->get_details$item['id'], $item['size'], $item['is_first_time'] );
        }

        
wp_send_json_success$urls );
    }

    
/**
     * Get image details.
     *
     * Retrieve single image details.
     *
     * Fired by `wp_ajax_elementor_get_image_details` action.
     *
     * @since 1.0.0
     * @access public
     *
     * @param string       $id            Image attachment ID.
     * @param string|array $size          Image size. Accepts any valid image
     *                                    size, or an array of width and height
     *                                    values in pixels (in that order).
     * @param string       $is_first_time Set 'true' string to force reloading
     *                                    all image sizes.
     *
     * @return array URLs with different image sizes.
     */
    
public function get_details$id$size$is_first_time ) {
        if ( ! 
class_exists'Group_Control_Image_Size' ) ) {
            require_once 
ELEMENTOR_PATH '/includes/controls/groups/image-size.php';
        }

        if ( 
'true' === $is_first_time ) {
            
$sizes get_intermediate_image_sizes();
            
$sizes[] = 'full';
        } else {
            
$sizes = [];
        }

        
$sizes[] = $size;
        
$urls = [];
        foreach ( 
$sizes as $size ) {
            if ( 
=== strpos$size'custom_' ) ) {
                
preg_match'/custom_(\d*)x(\d*)/'$size$matches );

                
$matches[1] = (int) $matches[1];
                
$matches[2] = (int) $matches[2];

                
$instance = [
                    
'image_size' => 'custom',
                    
'image_custom_dimension' => [
                        
'width' => $matches[1],
                        
'height' => $matches[2],
                    ],
                ];

                
$url Group_Control_Image_Size::get_attachment_image_src$id'image'$instance );

                
$thumbs_path BFITHUMB_UPLOAD_DIR '/' basename$url );

                
$image_meta wp_get_attachment_metadata$id );

                
// Attach custom image to original.
                
$image_meta['sizes'][ 'elementor_' $size ] = [
                    
'file' => $thumbs_path,
                    
'width' => $matches[1],
                    
'height' => $matches[2],
                    
'mime-type' => get_post_mime_type$id ),
                ];

                
wp_update_attachment_metadata$id$image_meta );

                
$urls$size ] = $url;
            } else {
                
$urls$size ] = wp_get_attachment_image_src$id$size )[0];
            }
        }

        return 
$urls;
    }

    
/**
     * Get Light-Box Image Attributes
     *
     * Used to retrieve an array of image attributes to be used for displaying an image in Elementor's Light Box module.
     *
     * @param int $id       The ID of the image
     *
     * @return array An array of image attributes including `title` and `description`.
     * @since 2.9.0
     * @access public
     */

    
public function get_lightbox_image_attributes$id ) {
        
$attributes = [];
        
$kit Plugin::$instance->kits_manager->get_active_kit();
        
$lightbox_title_src $kit->get_settings'lightbox_title_src' );
        
$lightbox_description_src $kit->get_settings'lightbox_description_src' );
        
$attachment get_post$id );

        if ( 
$attachment ) {
            
$image_data = [
                
'alt' => get_post_meta$attachment->ID'_wp_attachment_image_alt'true ),
                
'caption' => $attachment->post_excerpt,
                
'description' => $attachment->post_content,
                
'title' => $attachment->post_title,
            ];

            if ( 
$lightbox_title_src && $image_data$lightbox_title_src ] ) {
                
$attributes['title'] = $image_data$lightbox_title_src ];
            }

            if ( 
$lightbox_description_src && $image_data$lightbox_description_src ] ) {
                
$attributes['description'] = $image_data$lightbox_description_src ];
            }
        }

        return 
$attributes;
    }

    private function 
delete_custom_images$post_id ) {
        
$image_meta wp_get_attachment_metadata$post_id );
        if ( ! empty( 
$image_meta ) && ! empty( $image_meta['sizes'] ) ) {
            ( new 
Collection$image_meta['sizes'] ) )
            ->
filter( function ( $value$key ) {
                return ( 
=== strpos$key'elementor_custom_' ) );
            } )
            ->
pluck'file' )
            ->
each( function ( $path ) {
                
$base_dir wp_get_upload_dir()['basedir'];
                
wp_delete_file$base_dir '/' $path );
            } );
        }
    }

    
/**
     * Images manager constructor.
     *
     * Initializing Elementor images manager.
     *
     * @since 1.0.0
     * @access public
     */
    
public function __construct() {
        
add_action'wp_ajax_elementor_get_images_details', [ $this'get_images_details' ] );

        
// Delete elementor thumbnail files on deleting its main image.
        
add_action'delete_attachment', function ( $post_id ) {
            
$this->delete_custom_images$post_id );
        } );
    }
}