| 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( Editor::EDITING_CAPABILITY ) ) { |
| 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 |
|
| 132 |
public function get_lightbox_image_attributes( $id ) { |
| 133 |
$attributes = []; |
| 134 |
$kit = Plugin::$instance->kits_manager->get_active_kit(); |
| 135 |
$lightbox_title_src = $kit->get_settings( 'lightbox_title_src' ); |
| 136 |
$lightbox_description_src = $kit->get_settings( 'lightbox_description_src' ); |
| 137 |
$attachment = get_post( $id ); |
| 138 |
|
| 139 |
if ( $attachment ) { |
| 140 |
$image_data = [ |
| 141 |
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), |
| 142 |
'caption' => $attachment->post_excerpt, |
| 143 |
'description' => $attachment->post_content, |
| 144 |
'title' => $attachment->post_title, |
| 145 |
]; |
| 146 |
|
| 147 |
if ( $lightbox_title_src && $image_data[ $lightbox_title_src ] ) { |
| 148 |
$attributes['title'] = $image_data[ $lightbox_title_src ]; |
| 149 |
} |
| 150 |
|
| 151 |
if ( $lightbox_description_src && $image_data[ $lightbox_description_src ] ) { |
| 152 |
$attributes['description'] = $image_data[ $lightbox_description_src ]; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return $attributes; |
| 157 |
} |
| 158 |
|
| 159 |
private function delete_custom_images( $post_id ) { |
| 160 |
$image_meta = wp_get_attachment_metadata( $post_id ); |
| 161 |
if ( ! empty( $image_meta ) && ! empty( $image_meta['sizes'] ) ) { |
| 162 |
( new Collection( $image_meta['sizes'] ) ) |
| 163 |
->filter( function ( $value, $key ) { |
| 164 |
return ( 0 === strpos( $key, 'elementor_custom_' ) ); |
| 165 |
} ) |
| 166 |
->pluck( 'file' ) |
| 167 |
->each( function ( $path ) { |
| 168 |
$base_dir = wp_get_upload_dir()['basedir']; |
| 169 |
wp_delete_file( $base_dir . '/' . $path ); |
| 170 |
} ); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Images manager constructor. |
| 176 |
* |
| 177 |
* Initializing Elementor images manager. |
| 178 |
* |
| 179 |
* @since 1.0.0 |
| 180 |
* @access public |
| 181 |
*/ |
| 182 |
public function __construct() { |
| 183 |
add_action( 'wp_ajax_elementor_get_images_details', [ $this, 'get_images_details' ] ); |
| 184 |
|
| 185 |
// Delete elementor thumbnail files on deleting its main image. |
| 186 |
add_action( 'delete_attachment', function ( $post_id ) { |
| 187 |
$this->delete_custom_images( $post_id ); |
| 188 |
} ); |
| 189 |
} |
| 190 |
} |
| 191 |
|