| 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 |
private $directory = ''; |
| 9 |
|
| 10 |
private $filename = ''; |
| 11 |
|
| 12 |
private $original_extension = ''; |
| 13 |
|
| 14 |
private $webp_extension = '.webp'; |
| 15 |
|
| 16 |
private $meta = false; |
| 17 |
|
| 18 |
private $sizes = array(); |
| 19 |
|
| 20 |
private $images = array(); |
| 21 |
|
| 22 |
/** |
| 23 |
* Constructor |
| 24 |
*/ |
| 25 |
private function __construct() { |
| 26 |
add_action('delete_attachment', array($this, 'delete_related_images')); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Returns singleton instance |
| 31 |
* |
| 32 |
* @return WP_Optimize_WebP_Images |
| 33 |
*/ |
| 34 |
public static function get_instance() { |
| 35 |
static $instance = null; |
| 36 |
if (null === $instance) { |
| 37 |
$instance = new WP_Optimize_WebP_Images(); |
| 38 |
} |
| 39 |
return $instance; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Deletes related image sizes and alternate webp format images |
| 44 |
* |
| 45 |
* @param int $attachment_id |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function delete_related_images($attachment_id) { |
| 49 |
$this->set_meta($attachment_id); |
| 50 |
$this->set_file_info_properties($attachment_id); |
| 51 |
$this->set_sizes(); |
| 52 |
$this->set_images(); |
| 53 |
$this->delete_images(); |
| 54 |
$this->reset(); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Sets meta property with attachment metadata |
| 59 |
* |
| 60 |
* @param int $attachment_id |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
private function set_meta($attachment_id) { |
| 64 |
$this->meta = wp_get_attachment_metadata($attachment_id); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Sets file information properties |
| 69 |
* |
| 70 |
* @param int $attachment_id |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
private function set_file_info_properties($attachment_id) { |
| 74 |
$file_path = get_attached_file($attachment_id); |
| 75 |
$file_path_info = pathinfo($file_path); |
| 76 |
$this->filename = $file_path_info['filename']; |
| 77 |
$this->original_extension = '.' . $file_path_info['extension']; |
| 78 |
|
| 79 |
$file = $this->meta['file'] ?? ''; |
| 80 |
$basename = $file_path_info['basename']; |
| 81 |
$sub_directory = ''; |
| 82 |
if (!empty($file)) { |
| 83 |
$sub_directory = str_replace($basename, '', $file); |
| 84 |
} |
| 85 |
|
| 86 |
$uploads = wp_get_upload_dir(); |
| 87 |
$this->directory = $uploads['basedir'] . '/' . $sub_directory; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Returns all available image sizes for the given attachment id |
| 92 |
*/ |
| 93 |
private function set_sizes() { |
| 94 |
if (false !== $this->meta) { |
| 95 |
$this->sizes = $this->meta['sizes'] ?? array(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Sets images property |
| 101 |
* |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
private function set_images() { |
| 105 |
$webp_format = $this->directory . $this->filename . $this->original_extension . $this->webp_extension; |
| 106 |
$this->images[] = $webp_format; |
| 107 |
|
| 108 |
foreach ($this->sizes as $size) { |
| 109 |
$original_format = $this->directory . $this->filename . '-' . $size['width'] . 'x' . $size['height'] . $this->original_extension; |
| 110 |
$this->images[] = $original_format; |
| 111 |
$webp_format = $this->directory . preg_replace('/-scaled$/', '', $this->filename) . '-' . $size['width'] . 'x' . $size['height'] . $this->original_extension . $this->webp_extension; |
| 112 |
$this->images[] = $webp_format; |
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Delete related images |
| 119 |
* |
| 120 |
* @return void |
| 121 |
*/ |
| 122 |
private function delete_images() { |
| 123 |
foreach ($this->images as $image) { |
| 124 |
wp_delete_file($image); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Reset class property values |
| 130 |
*/ |
| 131 |
private function reset() { |
| 132 |
$this->directory = ''; |
| 133 |
$this->filename = ''; |
| 134 |
$this->original_extension = ''; |
| 135 |
$this->meta = false; |
| 136 |
$this->sizes = array(); |
| 137 |
$this->images = array(); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
endif; |
| 142 |
|