| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 3 |
|
| 4 |
if (!class_exists('WP_Optimize_WebP_Images')) : |
| 5 |
|
| 6 |
class WP_Optimize_WebP_Images { |
| 7 |
|
| 8 |
/** |
| 9 |
* Constructor |
| 10 |
*/ |
| 11 |
private function __construct() { |
| 12 |
add_action('delete_attachment', array($this, 'delete_related_images')); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Returns singleton instance |
| 17 |
* |
| 18 |
* @return WP_Optimize_WebP_Images |
| 19 |
*/ |
| 20 |
public static function get_instance(): self { |
| 21 |
static $instance = null; |
| 22 |
if (null === $instance) { |
| 23 |
$instance = new self(); |
| 24 |
} |
| 25 |
return $instance; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Deletes related image sizes and alternate WebP format images |
| 30 |
* |
| 31 |
* @param int $attachment_id |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public function delete_related_images($attachment_id): void { |
| 35 |
$meta = wp_get_attachment_metadata($attachment_id); |
| 36 |
if (false === $meta) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
$file_path = get_attached_file($attachment_id); |
| 41 |
if (false === $file_path) { |
| 42 |
return; |
| 43 |
} |
| 44 |
$file_info = pathinfo($file_path); |
| 45 |
$directory = $this->get_upload_directory($file_info['basename'], $meta); |
| 46 |
$filename = $file_info['filename']; |
| 47 |
$original_extension = '.' . ($file_info['extension'] ?? ''); |
| 48 |
$sizes = empty($meta['sizes']) ? array() : $meta['sizes']; |
| 49 |
|
| 50 |
$images = $this->build_image_paths( |
| 51 |
$directory, |
| 52 |
$filename, |
| 53 |
$original_extension, |
| 54 |
$sizes |
| 55 |
); |
| 56 |
|
| 57 |
$this->delete_files($images); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Computes the upload directory path for the attachment |
| 62 |
* |
| 63 |
* @param string $basename The original file basename |
| 64 |
* @param array<string, int|string|array<string, array<string, string>>> $meta The attachment metadata |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
private function get_upload_directory($basename, $meta): string { |
| 68 |
$file = isset($meta['file']) && is_string($meta['file']) ? $meta['file'] : ''; |
| 69 |
$sub_directory = ''; |
| 70 |
if ('' !== $file) { |
| 71 |
$sub_directory = str_replace($basename, '', $file); |
| 72 |
} |
| 73 |
|
| 74 |
$uploads = wp_get_upload_dir(); |
| 75 |
return $uploads['basedir'] . '/' . $sub_directory; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Builds a list of image file paths to delete, including both |
| 80 |
* original size variants and their WebP counterparts |
| 81 |
* |
| 82 |
* @param string $directory Upload directory path |
| 83 |
* @param string $filename Image filename without extension |
| 84 |
* @param string $original_extension Original file extension with leading dot |
| 85 |
* @param array<string, array<string, string>> $sizes Registered image size definitions |
| 86 |
* @return array<string> List of absolute file paths |
| 87 |
*/ |
| 88 |
private function build_image_paths($directory, $filename, $original_extension, $sizes) { |
| 89 |
$webp_extension = '.webp'; |
| 90 |
$images = array( |
| 91 |
$directory . $filename . $original_extension . $webp_extension, |
| 92 |
); |
| 93 |
|
| 94 |
$unscaled_filename = preg_replace('/-scaled$/', '', $filename); |
| 95 |
|
| 96 |
foreach ($sizes as $size) { |
| 97 |
$size_suffix = '-' . $size['width'] . 'x' . $size['height']; |
| 98 |
$images[] = $directory . $filename . $size_suffix . $original_extension; |
| 99 |
$images[] = $directory . $unscaled_filename . $size_suffix . $original_extension . $webp_extension; |
| 100 |
} |
| 101 |
|
| 102 |
return $images; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Deletes the given list of files from disk |
| 107 |
* |
| 108 |
* @param array<string> $files List of absolute file paths to delete |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
private function delete_files($files): void { |
| 112 |
foreach ($files as $file) { |
| 113 |
wp_delete_file($file); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
endif; |
| 119 |
|