PluginProbe
Elementor Website Builder – more than just a page builder / 3.28.0-dev2
Elementor Website Builder – more than just a page builder v3.28.0-dev2
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.28.0-dev2, at includes/managers/image.php

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