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

183 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 $instance = [
80 'image_size' => 'custom',
81 'image_custom_dimension' => [
82 'width' => $matches[1],
83 'height' => $matches[2],
84 ],
85 ];
86
87 $url = Group_Control_Image_Size::get_attachment_image_src( $id, 'image', $instance );
88
89 $thumbs_path = BFITHUMB_UPLOAD_DIR . '/' . basename( $url );
90
91 $image_meta = wp_get_attachment_metadata( $id );
92
93 // Attach custom image to original.
94 $image_meta['sizes'][ 'elementor_' . $size ] = [
95 'file' => $thumbs_path,
96 'width' => $matches[1],
97 'height' => $matches[2],
98 'mime-type' => get_post_mime_type( $id ),
99 ];
100
101 wp_update_attachment_metadata( $id, $image_meta );
102
103 $urls[ $size ] = $url;
104 } else {
105 $urls[ $size ] = wp_get_attachment_image_src( $id, $size )[0];
106 }
107 }
108
109 return $urls;
110 }
111
112 /**
113 * Get Light-Box Image Attributes
114 *
115 * Used to retrieve an array of image attributes to be used for displaying an image in Elementor's Light Box module.
116 *
117 * @param int $id The ID of the image
118 *
119 * @return array An array of image attributes including `title` and `description`.
120 * @since 2.9.0
121 * @access public
122 */
123
124 public function get_lightbox_image_attributes( $id ) {
125 $attributes = [];
126 $kit = Plugin::$instance->kits_manager->get_active_kit();
127 $lightbox_title_src = $kit->get_settings( 'lightbox_title_src' );
128 $lightbox_description_src = $kit->get_settings( 'lightbox_description_src' );
129 $attachment = get_post( $id );
130
131 if ( $attachment ) {
132 $image_data = [
133 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
134 'caption' => $attachment->post_excerpt,
135 'description' => $attachment->post_content,
136 'title' => $attachment->post_title,
137 ];
138
139 if ( $lightbox_title_src && $image_data[ $lightbox_title_src ] ) {
140 $attributes['title'] = $image_data[ $lightbox_title_src ];
141 }
142
143 if ( $lightbox_description_src && $image_data[ $lightbox_description_src ] ) {
144 $attributes['description'] = $image_data[ $lightbox_description_src ];
145 }
146 }
147
148 return $attributes;
149 }
150
151 private function delete_custom_images( $post_id ) {
152 $image_meta = wp_get_attachment_metadata( $post_id );
153 if ( ! empty( $image_meta ) && ! empty( $image_meta['sizes'] ) ) {
154 ( new Collection( $image_meta['sizes'] ) )
155 ->filter( function ( $value, $key ) {
156 return ( 0 === strpos( $key, 'elementor_custom_' ) );
157 } )
158 ->pluck( 'file' )
159 ->each( function ( $path ) {
160 $base_dir = wp_get_upload_dir()['basedir'];
161 wp_delete_file( $base_dir . '/' . $path );
162 } );
163 }
164 }
165
166 /**
167 * Images manager constructor.
168 *
169 * Initializing Elementor images manager.
170 *
171 * @since 1.0.0
172 * @access public
173 */
174 public function __construct() {
175 add_action( 'wp_ajax_elementor_get_images_details', [ $this, 'get_images_details' ] );
176
177 // Delete elementor thumbnail files on deleting its main image.
178 add_action( 'delete_attachment', function ( $post_id ) {
179 $this->delete_custom_images( $post_id );
180 } );
181 }
182 }
183