| 1 |
<?php |
| 2 |
/** |
| 3 |
* Dam class. |
| 4 |
* |
| 5 |
* Author: Andrei Baicus <andrei@themeisle.com> |
| 6 |
* Created on: 04/07/2023 |
| 7 |
* |
| 8 |
* @package \Optimole\Inc |
| 9 |
* @author Optimole <friends@optimole.com> |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Optml_Dam |
| 14 |
*/ |
| 15 |
class Optml_Dam { |
| 16 |
use Optml_Dam_Offload_Utils; |
| 17 |
use Optml_Normalizer; |
| 18 |
|
| 19 |
/** |
| 20 |
* Hold the settings object. |
| 21 |
* |
| 22 |
* @var Optml_Settings Settings object. |
| 23 |
*/ |
| 24 |
private $settings; |
| 25 |
|
| 26 |
/** |
| 27 |
* The dam endpoint. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
private $dam_endpoint = 'https://dashboard.optimole.com/dam'; |
| 32 |
|
| 33 |
const OM_DAM_IMPORTED_FLAG = 'om-dam-imported'; |
| 34 |
const URL_DAM_FLAG = '/dam:1'; |
| 35 |
const IS_EDIT_FLAG = 'om-dam-edit'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Optml_Dam constructor. |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
$this->settings = Optml_Main::instance()->admin->settings; |
| 42 |
|
| 43 |
if ( ! $this->settings->is_connected() ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
if ( $this->settings->get( 'cloud_images' ) === 'enabled' ) { |
| 48 |
add_action( 'admin_menu', [ $this, 'add_menu' ] ); |
| 49 |
add_action( 'print_media_templates', [ $this, 'print_media_template' ] ); |
| 50 |
add_action( 'wp_enqueue_media', [ $this, 'enqueue_media_scripts' ] ); |
| 51 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_page_scripts' ] ); |
| 52 |
} |
| 53 |
|
| 54 |
if ( defined( 'OPTML_DAM_ENDPOINT' ) && constant( 'OPTML_DAM_ENDPOINT' ) ) { |
| 55 |
$this->dam_endpoint = constant( 'OPTML_DAM_ENDPOINT' ); |
| 56 |
} |
| 57 |
|
| 58 |
add_filter( 'wp_get_attachment_image_src', [ $this, 'alter_attachment_image_src' ], 10, 4 ); |
| 59 |
add_filter( 'wp_get_attachment_metadata', [ $this, 'alter_attachment_metadata' ], 10, 2 ); |
| 60 |
add_filter( 'image_downsize', [ $this, 'catch_downsize' ], 10, 3 ); |
| 61 |
add_filter( 'wp_prepare_attachment_for_js', [$this, 'alter_attachment_for_js'], 10, 3 ); |
| 62 |
add_filter( 'wp_image_src_get_dimensions', [$this, 'alter_img_tag_w_h'], 10, 4 ); |
| 63 |
add_filter( 'get_attached_file', [$this, 'alter_attached_file_response'], 10, 2 ); |
| 64 |
add_filter( 'wp_calculate_image_srcset', [$this, 'disable_dam_images_srcset'], 1, 5 ); |
| 65 |
|
| 66 |
add_filter( |
| 67 |
'elementor/image_size/get_attachment_image_html', |
| 68 |
[ |
| 69 |
$this, |
| 70 |
'alter_elementor_image_size', |
| 71 |
], |
| 72 |
10, |
| 73 |
4 |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Catch image downsize for the DAM imported images. |
| 79 |
* |
| 80 |
* @param array $image { |
| 81 |
* Array of image data. |
| 82 |
* |
| 83 |
* @type string $0 Image source URL. |
| 84 |
* @type int $1 Image width in pixels. |
| 85 |
* @type int $2 Image height in pixels. |
| 86 |
* @type bool $3 Whether the image is a resized image. |
| 87 |
* |
| 88 |
* @param int $id attachment id. |
| 89 |
* @param string|int[] $size image size. |
| 90 |
* |
| 91 |
* @return array $image. |
| 92 |
*/ |
| 93 |
public function catch_downsize( $image, $id, $size ) { |
| 94 |
return $this->alter_attachment_image_src( $image, $id, $size, false ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Insert attachments. |
| 99 |
* |
| 100 |
* @param array $images Images array. |
| 101 |
* |
| 102 |
* @return array |
| 103 |
*/ |
| 104 |
public function insert_attachments( $images ) { |
| 105 |
$ids = []; |
| 106 |
|
| 107 |
$existing = $this->check_existing_attachments( $images ); |
| 108 |
|
| 109 |
foreach ( $images as $image ) { |
| 110 |
if ( ! isset( $image['isEdit'] ) && array_key_exists( $image['meta']['resourceS3'], $existing ) ) { |
| 111 |
$ids[] = $existing[ $image['meta']['resourceS3'] ]; |
| 112 |
|
| 113 |
continue; |
| 114 |
} |
| 115 |
|
| 116 |
$id = $this->insert_attachment( $image ); |
| 117 |
|
| 118 |
if ( $id === 0 ) { |
| 119 |
continue; |
| 120 |
} |
| 121 |
$ids[] = $id; |
| 122 |
} |
| 123 |
|
| 124 |
return $ids; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Insert single attachment |
| 129 |
* |
| 130 |
* @param array $image Image data. |
| 131 |
* |
| 132 |
* @return int |
| 133 |
*/ |
| 134 |
private function insert_attachment( $image ) { |
| 135 |
$filename = basename( $image['url'] ); |
| 136 |
$name = pathinfo( $filename, PATHINFO_FILENAME ); |
| 137 |
|
| 138 |
$args = [ |
| 139 |
'post_title' => $name, |
| 140 |
'post_type' => 'attachment', |
| 141 |
'post_mime_type' => $image['meta']['mimeType'], |
| 142 |
'guid' => $image['url'], |
| 143 |
]; |
| 144 |
|
| 145 |
$id = wp_insert_attachment( $args ); |
| 146 |
|
| 147 |
if ( $id === 0 ) { |
| 148 |
return $id; |
| 149 |
} |
| 150 |
|
| 151 |
update_post_meta( $id, self::OM_DAM_IMPORTED_FLAG, $image['meta']['resourceS3'] ); |
| 152 |
|
| 153 |
if ( isset( $image['isEdit'] ) ) { |
| 154 |
update_post_meta( $id, self::IS_EDIT_FLAG, true ); |
| 155 |
} |
| 156 |
|
| 157 |
$metadata = []; |
| 158 |
|
| 159 |
$metadata['file'] = '/id:' . $image['meta']['resourceS3'] . '/' . get_home_url() . '/' . $filename; |
| 160 |
$metadata['mime-type'] = $image['meta']['mimeType']; |
| 161 |
|
| 162 |
if ( isset( $image['meta']['filesize'] ) ) { |
| 163 |
$metadata['filesize'] = $image['meta']['fileSize']; |
| 164 |
} |
| 165 |
|
| 166 |
if ( isset( $image['meta']['originalWidth'] ) && isset( $image['meta']['originalHeight'] ) ) { |
| 167 |
$metadata['width'] = $image['meta']['originalWidth']; |
| 168 |
$metadata['height'] = $image['meta']['originalHeight']; |
| 169 |
} |
| 170 |
|
| 171 |
wp_update_attachment_metadata( $id, $metadata ); |
| 172 |
|
| 173 |
return $id; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Alter attachment image src for DAM imported images. |
| 178 |
* |
| 179 |
* @param array|false $image { |
| 180 |
* Array of image data. |
| 181 |
* |
| 182 |
* @type string $0 Image source URL. |
| 183 |
* @type int $1 Image width in pixels. |
| 184 |
* @type int $2 Image height in pixels. |
| 185 |
* @type bool $3 Whether the image is a resized image. |
| 186 |
* } |
| 187 |
* |
| 188 |
* @param int $attachment_id attachment id. |
| 189 |
* @param string|int[] $size image size. |
| 190 |
* @param bool $icon Whether the image should be treated as an icon. |
| 191 |
* |
| 192 |
* @return array $image. |
| 193 |
*/ |
| 194 |
public function alter_attachment_image_src( $image, $attachment_id, $size, $icon ) { |
| 195 |
// Skip if not DAM image. |
| 196 |
if ( ! $this->is_dam_imported_image( $attachment_id ) ) { |
| 197 |
return $image; |
| 198 |
} |
| 199 |
|
| 200 |
$image_url = wp_get_attachment_url( $attachment_id ); |
| 201 |
$incoming_size = $this->parse_dimension_from_optimized_url( $image_url ); |
| 202 |
$width = $incoming_size[0]; |
| 203 |
$height = $incoming_size[1]; |
| 204 |
|
| 205 |
// Skip resize in single attachment view on backend. |
| 206 |
if ( $this->is_attachment_edit_page( $attachment_id ) ) { |
| 207 |
return [ |
| 208 |
$image_url, |
| 209 |
$width, |
| 210 |
$height, |
| 211 |
false, |
| 212 |
]; |
| 213 |
} |
| 214 |
|
| 215 |
// Use the original size if the requested size is full. |
| 216 |
if ( $size === 'full' ) { |
| 217 |
$metadata = wp_get_attachment_metadata( $attachment_id ); |
| 218 |
|
| 219 |
$image_url = $this->replace_dam_url_args( |
| 220 |
[ |
| 221 |
'width' => $metadata['width'], |
| 222 |
'height' => $metadata['height'], |
| 223 |
'crop' => false, |
| 224 |
], |
| 225 |
$image_url |
| 226 |
); |
| 227 |
|
| 228 |
return [ |
| 229 |
$image_url, |
| 230 |
$metadata['width'], |
| 231 |
$metadata['height'], |
| 232 |
false, |
| 233 |
]; |
| 234 |
} |
| 235 |
|
| 236 |
$crop = false; |
| 237 |
|
| 238 |
// Size can be int [] containing width and height. |
| 239 |
if ( is_array( $size ) ) { |
| 240 |
$width = $size[0]; |
| 241 |
$height = $size[1]; |
| 242 |
$crop = true; |
| 243 |
} else { |
| 244 |
$sizes = $this->get_all_image_sizes(); |
| 245 |
|
| 246 |
if ( ! isset( $sizes[ $size ] ) ) { |
| 247 |
return [ |
| 248 |
$image_url, |
| 249 |
$width, |
| 250 |
$height, |
| 251 |
false, |
| 252 |
]; |
| 253 |
} |
| 254 |
|
| 255 |
$width = $sizes[ $size ]['width']; |
| 256 |
$height = $sizes[ $size ]['height']; |
| 257 |
$crop = (bool) $sizes[ $size ]['crop']; |
| 258 |
} |
| 259 |
|
| 260 |
$image_url = $this->replace_dam_url_args( |
| 261 |
[ |
| 262 |
'width' => $width, |
| 263 |
'height' => $height, |
| 264 |
'crop' => $crop, |
| 265 |
], |
| 266 |
$image_url |
| 267 |
); |
| 268 |
|
| 269 |
return [ |
| 270 |
$image_url, |
| 271 |
$width, |
| 272 |
$height, |
| 273 |
$crop, |
| 274 |
]; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Alter the attachment metadata. |
| 279 |
* |
| 280 |
* @param array $metadata attachment metadata. |
| 281 |
* @param int $id attachment ID. |
| 282 |
* |
| 283 |
* @return array |
| 284 |
*/ |
| 285 |
public function alter_attachment_metadata( $metadata, $id ) { |
| 286 |
if ( ! $this->is_dam_imported_image( $id ) ) { |
| 287 |
return $metadata; |
| 288 |
} |
| 289 |
|
| 290 |
return $this->get_altered_metadata_for_remote_images( $metadata, $id ); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Check if the images are already imported. |
| 295 |
* |
| 296 |
* @param array $images List of images to check. |
| 297 |
* |
| 298 |
* @return array List of images that are already imported. |
| 299 |
*/ |
| 300 |
private function check_existing_attachments( $images ) { |
| 301 |
$already_imported = $this->get_dam_imported_attachments( $images ); |
| 302 |
|
| 303 |
// All DAM imports are already in the DB. |
| 304 |
if ( count( $already_imported ) === count( $images ) ) { |
| 305 |
return $already_imported; |
| 306 |
} |
| 307 |
|
| 308 |
// Get the remaining images. |
| 309 |
$remaining = array_filter( |
| 310 |
$images, |
| 311 |
function ( $image ) use ( $already_imported ) { |
| 312 |
return ! array_key_exists( $image['meta']['resourceS3'], $already_imported ); |
| 313 |
} |
| 314 |
); |
| 315 |
|
| 316 |
// Offloaded images. |
| 317 |
if ( $this->settings->get( 'offload_media' ) === 'enabled' ) { |
| 318 |
$offloaded = $this->get_offloaded_attachments( $remaining ); |
| 319 |
|
| 320 |
$already_imported = array_merge( $already_imported, $offloaded ); |
| 321 |
} |
| 322 |
|
| 323 |
return $already_imported; |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Check if the images are already imported. |
| 328 |
* |
| 329 |
* @param array $images List of images to check. |
| 330 |
* |
| 331 |
* @return array |
| 332 |
*/ |
| 333 |
private function get_dam_imported_attachments( $images ) { |
| 334 |
global $wpdb; |
| 335 |
|
| 336 |
$s3_ids = []; |
| 337 |
|
| 338 |
foreach ( $images as $image ) { |
| 339 |
$s3_ids[] = esc_sql( strval( $image['meta']['resourceS3'] ) ); |
| 340 |
} |
| 341 |
|
| 342 |
$meta_values_str = "'" . join( "', '", $s3_ids ) . "'"; |
| 343 |
|
| 344 |
// Select all the posts that have the flag and are in the list of images. |
| 345 |
$found_attachments = $wpdb->get_results( |
| 346 |
$wpdb->prepare( |
| 347 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- This query cannot use interpolation. |
| 348 |
"SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value IN ( {$meta_values_str} )", |
| 349 |
self::OM_DAM_IMPORTED_FLAG |
| 350 |
) |
| 351 |
); |
| 352 |
|
| 353 |
if ( empty( $found_attachments ) ) { |
| 354 |
return []; |
| 355 |
} |
| 356 |
|
| 357 |
$map = []; |
| 358 |
|
| 359 |
// Remap this in a key/value array. |
| 360 |
// Also ensures that if there are multiple attachments with the same S3 ID, we only get the one. |
| 361 |
// Shouldn't happen, but just in case. |
| 362 |
foreach ( $found_attachments as $attachment ) { |
| 363 |
// Skip edits. |
| 364 |
if ( ! empty( get_post_meta( $attachment->post_id, self::IS_EDIT_FLAG, true ) ) ) { |
| 365 |
continue; |
| 366 |
} |
| 367 |
|
| 368 |
$map[ $attachment->meta_value ] = (int) $attachment->post_id; |
| 369 |
} |
| 370 |
|
| 371 |
return $map; |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* Get the offloaded attachments. |
| 376 |
* |
| 377 |
* [ S3 ID => Attachment Post ID ] |
| 378 |
* |
| 379 |
* @param array $images List of images to check. |
| 380 |
* |
| 381 |
* @return array |
| 382 |
*/ |
| 383 |
private function get_offloaded_attachments( $images ) { |
| 384 |
global $wpdb; |
| 385 |
|
| 386 |
$map = []; |
| 387 |
|
| 388 |
foreach ( $images as $image ) { |
| 389 |
$like = '%id:' . $image['meta']['resourceS3'] . '%'; |
| 390 |
|
| 391 |
$found_attachments = $wpdb->get_results( |
| 392 |
$wpdb->prepare( |
| 393 |
"SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value LIKE %s", |
| 394 |
'_wp_attachment_metadata', |
| 395 |
$like |
| 396 |
) |
| 397 |
); |
| 398 |
|
| 399 |
if ( empty( $found_attachments ) ) { |
| 400 |
return []; |
| 401 |
} |
| 402 |
|
| 403 |
$map[ $image['meta']['resourceS3'] ] = (int) $found_attachments[0]->post_id; |
| 404 |
} |
| 405 |
|
| 406 |
return $map; |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* Adds menu item for DAM. |
| 411 |
* |
| 412 |
* @return void |
| 413 |
*/ |
| 414 |
public function add_menu() { |
| 415 |
if ( defined( 'OPTIOMLE_HIDE_ADMIN_AREA' ) && OPTIOMLE_HIDE_ADMIN_AREA ) { |
| 416 |
return; |
| 417 |
} |
| 418 |
|
| 419 |
add_submenu_page( |
| 420 |
'optimole', |
| 421 |
__( 'Cloud Library', 'optimole-wp' ), |
| 422 |
__( 'Cloud Library', 'optimole-wp' ), |
| 423 |
'manage_options', |
| 424 |
'optimole-dam', |
| 425 |
[ $this, 'render_dashboard_page' ] |
| 426 |
); |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Add media template to be used in the media library. |
| 431 |
* |
| 432 |
* @return void |
| 433 |
*/ |
| 434 |
public function print_media_template() { |
| 435 |
?> |
| 436 |
<script type="text/html" id="tmpl-optimole-dam"> |
| 437 |
<?php $this->render_dashboard_page(); ?> |
| 438 |
</script> |
| 439 |
<?php |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Render the dashboard page. |
| 444 |
* |
| 445 |
* @return void |
| 446 |
*/ |
| 447 |
public function render_dashboard_page() { |
| 448 |
?> |
| 449 |
<iframe id="om-dam" style="display: none;" src="<?php echo( $this->build_iframe_url() ); ?>"></iframe> |
| 450 |
<div class="om-dam-loader"> |
| 451 |
<img src="<?php echo esc_url( OPTML_URL . 'assets/img/logo.png' ); ?>" alt="Optimole Logo" |
| 452 |
class="om-dam-logo"> |
| 453 |
<p><?php echo esc_html__( 'Loading', 'optimole-wp' ); ?>...</p> |
| 454 |
</div> |
| 455 |
<?php |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* Build the iFrame URL. |
| 460 |
* |
| 461 |
* @return string |
| 462 |
*/ |
| 463 |
public function build_iframe_url() { |
| 464 |
$api_key = $this->settings->get( 'api_key' ); |
| 465 |
$connected_sites = $this->settings->get( 'cloud_sites' ); |
| 466 |
|
| 467 |
if ( empty( $api_key ) ) { |
| 468 |
return ''; |
| 469 |
} |
| 470 |
|
| 471 |
if ( isset( $connected_sites['all'] ) && $connected_sites['all'] === 'true' ) { |
| 472 |
$connected_sites = []; |
| 473 |
} else { |
| 474 |
foreach ( $connected_sites as $site => $status ) { |
| 475 |
if ( $status !== 'true' ) { |
| 476 |
unset( $connected_sites[ $site ] ); |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
$data = [ |
| 482 |
'site' => get_site_url(), |
| 483 |
'token' => $api_key, |
| 484 |
'sites' => array_keys( $connected_sites ), |
| 485 |
]; |
| 486 |
|
| 487 |
$data = json_encode( $data ); |
| 488 |
$data = rtrim( base64_encode( $data ), '=' ); |
| 489 |
|
| 490 |
return add_query_arg( |
| 491 |
[ |
| 492 |
'data' => $data, |
| 493 |
], |
| 494 |
$this->dam_endpoint |
| 495 |
); |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
* Enqueue script for generating cloud media tab. |
| 500 |
* |
| 501 |
* @return void |
| 502 |
*/ |
| 503 |
public function enqueue_media_scripts() { |
| 504 |
$asset_file = include OPTML_PATH . 'assets/build/media/media-modal.asset.php'; |
| 505 |
|
| 506 |
wp_register_script( |
| 507 |
OPTML_NAMESPACE . '-media-modal', |
| 508 |
OPTML_URL . 'assets/build/media/media-modal.js', |
| 509 |
$asset_file['dependencies'], |
| 510 |
$asset_file['version'], |
| 511 |
true |
| 512 |
); |
| 513 |
|
| 514 |
wp_localize_script( OPTML_NAMESPACE . '-media-modal', 'optmlMediaModal', $this->get_localized_vars() ); |
| 515 |
wp_enqueue_script( OPTML_NAMESPACE . '-media-modal' ); |
| 516 |
wp_enqueue_style( OPTML_NAMESPACE . '-media-modal', OPTML_URL . 'assets/build/media/media-modal.css' ); |
| 517 |
} |
| 518 |
|
| 519 |
/** |
| 520 |
* Enqueue script for generating admin page. |
| 521 |
* |
| 522 |
* @return void |
| 523 |
*/ |
| 524 |
public function enqueue_admin_page_scripts() { |
| 525 |
$screen = get_current_screen(); |
| 526 |
|
| 527 |
if ( $screen->id !== 'optimole_page_optimole-dam' ) { |
| 528 |
return; |
| 529 |
} |
| 530 |
|
| 531 |
$asset_file = include OPTML_PATH . 'assets/build/media/admin-page.asset.php'; |
| 532 |
|
| 533 |
wp_register_script( |
| 534 |
OPTML_NAMESPACE . '-admin-page', |
| 535 |
OPTML_URL . 'assets/build/media/admin-page.js', |
| 536 |
$asset_file['dependencies'], |
| 537 |
$asset_file['version'], |
| 538 |
true |
| 539 |
); |
| 540 |
|
| 541 |
wp_localize_script( |
| 542 |
OPTML_NAMESPACE . '-admin-page', |
| 543 |
'optmlAdminPage', |
| 544 |
[ |
| 545 |
'siteUrl' => get_site_url(), |
| 546 |
] |
| 547 |
); |
| 548 |
|
| 549 |
wp_enqueue_script( OPTML_NAMESPACE . '-admin-page' ); |
| 550 |
|
| 551 |
wp_enqueue_style( OPTML_NAMESPACE . '-admin-page', OPTML_URL . 'assets/build/media/admin-page.css' ); |
| 552 |
} |
| 553 |
|
| 554 |
/** |
| 555 |
* Get localized variables for the media modal. |
| 556 |
* |
| 557 |
* @return array |
| 558 |
*/ |
| 559 |
private function get_localized_vars() { |
| 560 |
$routes = array_keys( Optml_Rest::$rest_routes['dam_routes'] ); |
| 561 |
|
| 562 |
foreach ( $routes as $route ) { |
| 563 |
$routes[ $route ] = OPTML_NAMESPACE . '/v1/' . $route; |
| 564 |
} |
| 565 |
|
| 566 |
return [ |
| 567 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 568 |
'routes' => $routes, |
| 569 |
]; |
| 570 |
} |
| 571 |
|
| 572 |
/** |
| 573 |
* Alter the image size for the image widget. |
| 574 |
* |
| 575 |
* @param string $html the attachment image HTML string. |
| 576 |
* @param array $settings Control settings. |
| 577 |
* @param string $image_size_key Optional. Settings key for image size. |
| 578 |
* Default is `image`. |
| 579 |
* @param string $image_key Optional. Settings key for image. Default |
| 580 |
* is null. If not defined uses image size key |
| 581 |
* as the image key. |
| 582 |
* |
| 583 |
* @return string |
| 584 |
*/ |
| 585 |
public function alter_elementor_image_size( $html, $settings, $image_size_key, $image_key ) { |
| 586 |
if ( ! isset( $settings['image'] ) ) { |
| 587 |
return $html; |
| 588 |
} |
| 589 |
|
| 590 |
$image = $settings['image']; |
| 591 |
|
| 592 |
if ( ! isset( $image['id'] ) ) { |
| 593 |
return $html; |
| 594 |
} |
| 595 |
|
| 596 |
if ( ! $this->is_dam_imported_image( $image['id'] ) ) { |
| 597 |
return $html; |
| 598 |
} |
| 599 |
|
| 600 |
if ( ! isset( $settings['image_size'] ) ) { |
| 601 |
return $html; |
| 602 |
} |
| 603 |
|
| 604 |
if ( $settings['image_size'] === 'custom' ) { |
| 605 |
if ( ! isset( $settings['image_custom_dimension'] ) ) { |
| 606 |
return $html; |
| 607 |
} |
| 608 |
|
| 609 |
$custom_dimensions = $settings['image_custom_dimension']; |
| 610 |
|
| 611 |
if ( ! isset( $custom_dimensions['width'] ) || ! isset( $custom_dimensions['height'] ) ) { |
| 612 |
return $html; |
| 613 |
} |
| 614 |
|
| 615 |
return $this->replace_dam_url_args( $custom_dimensions, $html ); |
| 616 |
} |
| 617 |
|
| 618 |
$all_sizes = $this->get_all_image_sizes(); |
| 619 |
|
| 620 |
if ( ! isset( $all_sizes[ $settings['image_size'] ] ) ) { |
| 621 |
return $html; |
| 622 |
} |
| 623 |
|
| 624 |
return $this->replace_dam_url_args( $all_sizes[ $settings['image_size'] ], $html ); |
| 625 |
} |
| 626 |
|
| 627 |
/** |
| 628 |
* Needed as some blocks might use the image sizes. |
| 629 |
* |
| 630 |
* @param array $response Array of prepared attachment data. @see wp_prepare_attachment_for_js(). |
| 631 |
* @param WP_Post $attachment Attachment object. |
| 632 |
* @param array|false $meta Array of attachment meta data, or false if there is none. |
| 633 |
* |
| 634 |
* @return array |
| 635 |
*/ |
| 636 |
public function alter_attachment_for_js( $response, $attachment, $meta ) { |
| 637 |
if ( ! $this->is_dam_imported_image( $attachment->ID ) ) { |
| 638 |
return $response; |
| 639 |
} |
| 640 |
|
| 641 |
$sizes = $this->get_all_image_sizes(); |
| 642 |
|
| 643 |
foreach ( $sizes as $size => $args ) { |
| 644 |
if ( isset( $response['sizes'][ $size ] ) ) { |
| 645 |
continue; |
| 646 |
} |
| 647 |
|
| 648 |
$args = [ |
| 649 |
'height' => $args['height'], |
| 650 |
'width' => $args['width'], |
| 651 |
'crop' => true, |
| 652 |
]; |
| 653 |
|
| 654 |
$response['sizes'][ $size ] = array_merge( |
| 655 |
$args, |
| 656 |
[ |
| 657 |
'url' => $this->replace_dam_url_args( $args, $response['url'] ), |
| 658 |
'orientation' => ( $args['height'] > $args['width'] ) ? 'portrait' : 'landscape', |
| 659 |
] |
| 660 |
); |
| 661 |
} |
| 662 |
|
| 663 |
$url_args = [ |
| 664 |
'height' => $response['height'], |
| 665 |
'width' => $response['width'], |
| 666 |
'crop' => false, |
| 667 |
]; |
| 668 |
|
| 669 |
$response['url'] = $this->replace_dam_url_args( $url_args, $response['url'] ); |
| 670 |
|
| 671 |
return $response; |
| 672 |
} |
| 673 |
|
| 674 |
/** |
| 675 |
* We have to short-circuit the logic that adds width and height to the img tag. |
| 676 |
* It compares the URL basename, and the `file` param for each image. |
| 677 |
* This happens for any image that gets its size set non-explicitly |
| 678 |
* e.g. an image block with its size set from the sidebar to `thumbnail`). |
| 679 |
* |
| 680 |
* Optimole has a single basename for all image resizes in its URL. |
| 681 |
* |
| 682 |
* @param array|false $dimensions Array with first element being the width |
| 683 |
* and second element being the height, or |
| 684 |
* false if dimensions could not be determined. |
| 685 |
* @param string $image_src The image URL (will be Optimole URL). |
| 686 |
* @param array $image_meta The image metadata. |
| 687 |
* @param int $attachment_id The image attachment ID. Default 0. |
| 688 |
*/ |
| 689 |
public function alter_img_tag_w_h( $dimensions, $image_src, $image_meta, $attachment_id ) { |
| 690 |
if ( ! $this->is_dam_imported_image( $attachment_id ) ) { |
| 691 |
return $dimensions; |
| 692 |
} |
| 693 |
|
| 694 |
// Get the dimensions from the optimized URL. |
| 695 |
$incoming_size = $this->parse_dimension_from_optimized_url( $image_src ); |
| 696 |
$width = $incoming_size[0]; |
| 697 |
$height = $incoming_size[1]; |
| 698 |
|
| 699 |
$sizes = $this->get_all_image_sizes(); |
| 700 |
|
| 701 |
// If this is an image size. Return its dimensions. |
| 702 |
foreach ( $sizes as $size => $args ) { |
| 703 |
if ( (int) $args['width'] !== (int) $width ) { |
| 704 |
continue; |
| 705 |
} |
| 706 |
|
| 707 |
if ( (int) $args['height'] !== (int) $height ) { |
| 708 |
continue; |
| 709 |
} |
| 710 |
|
| 711 |
return [ |
| 712 |
$args['width'], |
| 713 |
$args['height'], |
| 714 |
]; |
| 715 |
} |
| 716 |
|
| 717 |
// Fall-through with the original dimensions. |
| 718 |
return $dimensions; |
| 719 |
} |
| 720 |
|
| 721 |
/** |
| 722 |
* Replace the image size params in DAM URLs inside a string. |
| 723 |
* |
| 724 |
* @param array $args The arguments to replace. |
| 725 |
* - width: The width of the image. |
| 726 |
* - height: The height of the image. |
| 727 |
* - crop: Whether to crop the image. |
| 728 |
* @param string $subject The string to replace the arguments in. |
| 729 |
* |
| 730 |
* @return string |
| 731 |
*/ |
| 732 |
public function replace_dam_url_args( $args, $subject ) { |
| 733 |
$args = wp_parse_args( $args, [ 'width' => 'auto', 'height' => 'auto', 'crop' => false, 'dam' => true] ); |
| 734 |
|
| 735 |
$width = $args['width']; |
| 736 |
$height = $args['height']; |
| 737 |
$crop = (bool) $args['crop']; |
| 738 |
|
| 739 |
$gravity = Optml_Resize::GRAVITY_CENTER; |
| 740 |
|
| 741 |
if ( $this->settings->get( 'resize_smart' ) === 'enabled' ) { |
| 742 |
$gravity = Optml_Resize::GRAVITY_SMART; |
| 743 |
} |
| 744 |
|
| 745 |
if ( $width === 0 ) { |
| 746 |
$width = 'auto'; |
| 747 |
} |
| 748 |
|
| 749 |
if ( $height === 0 ) { |
| 750 |
$height = 'auto'; |
| 751 |
} |
| 752 |
|
| 753 |
// Use the proper replacement for the image size. |
| 754 |
$replacement = '/w:' . $width . '/h:' . $height; |
| 755 |
|
| 756 |
if ( $crop ) { |
| 757 |
$replacement .= '/g:' . $gravity . '/rt:fill'; |
| 758 |
} |
| 759 |
|
| 760 |
$replacement .= '/q:'; |
| 761 |
|
| 762 |
if ( $args['dam'] ) { |
| 763 |
$replacement = self::URL_DAM_FLAG . $replacement; |
| 764 |
} |
| 765 |
|
| 766 |
return preg_replace( '/\/w:(.*)\/h:(.*)\/q:/', $replacement, $subject ); |
| 767 |
} |
| 768 |
|
| 769 |
/** |
| 770 |
* Elementor checks if the file exists before requesting a specific image size. |
| 771 |
* |
| 772 |
* Needed because otherwise there won't be any width/height on the `img` tags, breaking lazyload. |
| 773 |
* |
| 774 |
* Also needed because some |
| 775 |
* |
| 776 |
* @param string $file The file path. |
| 777 |
* @param int $id The attachment ID. |
| 778 |
* |
| 779 |
* @return bool|string |
| 780 |
*/ |
| 781 |
public function alter_attached_file_response( $file, $id ) { |
| 782 |
if ( ! $this->is_dam_imported_image( $id ) ) { |
| 783 |
return $file; |
| 784 |
} |
| 785 |
|
| 786 |
$metadata = wp_get_attachment_metadata( $id ); |
| 787 |
|
| 788 |
if ( isset( $metadata['file'] ) ) { |
| 789 |
$uploads = wp_get_upload_dir(); |
| 790 |
|
| 791 |
return $uploads['basedir'] . '/' . $metadata['file']; |
| 792 |
} |
| 793 |
|
| 794 |
return true; |
| 795 |
} |
| 796 |
|
| 797 |
/** |
| 798 |
* Alter the srcSet for DAM images. |
| 799 |
* |
| 800 |
* @param array $sources Initial source array. |
| 801 |
* @param array $size_array Requested size. |
| 802 |
* @param string $image_src Image source URL. |
| 803 |
* @param array $image_meta Image meta data. |
| 804 |
* @param int $attachment_id Image attachment ID. |
| 805 |
* |
| 806 |
* @return array |
| 807 |
*/ |
| 808 |
public function disable_dam_images_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ) { |
| 809 |
if ( ! $this->is_dam_imported_image( $attachment_id ) ) { |
| 810 |
return $sources; |
| 811 |
} |
| 812 |
|
| 813 |
return []; |
| 814 |
} |
| 815 |
} |
| 816 |
|