PluginProbe
Elementor Website Builder – more than just a page builder / 3.9.1
Elementor Website Builder – more than just a page builder v3.9.1
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / managers / image.php

image.php in Elementor Website Builder – more than just a page builder 3.9.1, at includes/managers/image.php

186 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 use Elementor\Core\Utils\Collection;
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly.
8 }
9
10 /**
11 * Elementor images manager.
12 *
13 * Elementor images manager handler class is responsible for retrieving image
14 * details.
15 *
16 * @since 1.0.0
17 */
18 class Images_Manager {
19
20 /**
21 * Get images details.
22 *
23 * Retrieve details for all the images.
24 *
25 * Fired by `wp_ajax_elementor_get_images_details` action.
26 *
27 * @since 1.0.0
28 * @access public
29 */
30 public function get_images_details() {
31 // PHPCS - Already validated by wp_ajax.
32 $items = $_POST['items']; // phpcs:ignore WordPress.Security.NonceVerification.Missing
33 $urls = [];
34
35 foreach ( $items as $item ) {
36 $urls[ $item['id'] ] = $this->get_details( $item['id'], $item['size'], $item['is_first_time'] );
37 }
38
39 wp_send_json_success( $urls );
40 }
41
42 /**
43 * Get image details.
44 *
45 * Retrieve single image details.
46 *
47 * Fired by `wp_ajax_elementor_get_image_details` action.
48 *
49 * @since 1.0.0
50 * @access public
51 *
52 * @param string $id Image attachment ID.
53 * @param string|array $size Image size. Accepts any valid image
54 * size, or an array of width and height
55 * values in pixels (in that order).
56 * @param string $is_first_time Set 'true' string to force reloading
57 * all image sizes.
58 *
59 * @return array URLs with different image sizes.
60 */
61 public function get_details( $id, $size, $is_first_time ) {
62 if ( ! class_exists( 'Group_Control_Image_Size' ) ) {
63 require_once ELEMENTOR_PATH . '/includes/controls/groups/image-size.php';
64 }
65
66 if ( 'true' === $is_first_time ) {
67 $sizes = get_intermediate_image_sizes();
68 $sizes[] = 'full';
69 } else {
70 $sizes = [];
71 }
72
73 $sizes[] = $size;
74 $urls = [];
75 foreach ( $sizes as $size ) {
76 if ( 0 === strpos( $size, 'custom_' ) ) {
77 preg_match( '/custom_(\d*)x(\d*)/', $size, $matches );
78
79 $matches[1] = (int) $matches[1];
80 $matches[2] = (int) $matches[2];
81
82 $instance = [
83 'image_size' => 'custom',
84 'image_custom_dimension' => [
85 'width' => $matches[1],
86 'height' => $matches[2],
87 ],
88 ];
89
90 $url = Group_Control_Image_Size::get_attachment_image_src( $id, 'image', $instance );
91
92 $thumbs_path = BFITHUMB_UPLOAD_DIR . '/' . basename( $url );
93
94 $image_meta = wp_get_attachment_metadata( $id );
95
96 // Attach custom image to original.
97 $image_meta['sizes'][ 'elementor_' . $size ] = [
98 'file' => $thumbs_path,
99 'width' => $matches[1],
100 'height' => $matches[2],
101 'mime-type' => get_post_mime_type( $id ),
102 ];
103
104 wp_update_attachment_metadata( $id, $image_meta );
105
106 $urls[ $size ] = $url;
107 } else {
108 $urls[ $size ] = wp_get_attachment_image_src( $id, $size )[0];
109 }
110 }
111
112 return $urls;
113 }
114
115 /**
116 * Get Light-Box Image Attributes
117 *
118 * Used to retrieve an array of image attributes to be used for displaying an image in Elementor's Light Box module.
119 *
120 * @param int $id The ID of the image
121 *
122 * @return array An array of image attributes including `title` and `description`.
123 * @since 2.9.0
124 * @access public
125 */
126
127 public function get_lightbox_image_attributes( $id ) {
128 $attributes = [];
129 $kit = Plugin::$instance->kits_manager->get_active_kit();
130 $lightbox_title_src = $kit->get_settings( 'lightbox_title_src' );
131 $lightbox_description_src = $kit->get_settings( 'lightbox_description_src' );
132 $attachment = get_post( $id );
133
134 if ( $attachment ) {
135 $image_data = [
136 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
137 'caption' => $attachment->post_excerpt,
138 'description' => $attachment->post_content,
139 'title' => $attachment->post_title,
140 ];
141
142 if ( $lightbox_title_src && $image_data[ $lightbox_title_src ] ) {
143 $attributes['title'] = $image_data[ $lightbox_title_src ];
144 }
145
146 if ( $lightbox_description_src && $image_data[ $lightbox_description_src ] ) {
147 $attributes['description'] = $image_data[ $lightbox_description_src ];
148 }
149 }
150
151 return $attributes;
152 }
153
154 private function delete_custom_images( $post_id ) {
155 $image_meta = wp_get_attachment_metadata( $post_id );
156 if ( ! empty( $image_meta ) && ! empty( $image_meta['sizes'] ) ) {
157 ( new Collection( $image_meta['sizes'] ) )
158 ->filter( function ( $value, $key ) {
159 return ( 0 === strpos( $key, 'elementor_custom_' ) );
160 } )
161 ->pluck( 'file' )
162 ->each( function ( $path ) {
163 $base_dir = wp_get_upload_dir()['basedir'];
164 wp_delete_file( $base_dir . '/' . $path );
165 } );
166 }
167 }
168
169 /**
170 * Images manager constructor.
171 *
172 * Initializing Elementor images manager.
173 *
174 * @since 1.0.0
175 * @access public
176 */
177 public function __construct() {
178 add_action( 'wp_ajax_elementor_get_images_details', [ $this, 'get_images_details' ] );
179
180 // Delete elementor thumbnail files on deleting its main image.
181 add_action( 'delete_attachment', function ( $post_id ) {
182 $this->delete_custom_images( $post_id );
183 } );
184 }
185 }
186