PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
← All changes | webp/class-wp-optimize-webp-images.php +60 -83 3.4.1 → 4.7.0 View file →
@@ -1,30 +1,16 @@
1 1 <?php
2 -if (!defined('WPO_VERSION')) die('No direct access allowed');
2 +if (!defined('ABSPATH')) die('No direct access allowed');
3 3
4 4 if (!class_exists('WP_Optimize_WebP_Images')) :
5 5
6 6 class WP_Optimize_WebP_Images {
7 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 8 /**
23 9 * Constructor
24 10 */
25 11 private function __construct() {
26 - add_action('delete_attachment', array($this, 'delete_related_images'), 10, 1);
12 + add_action('delete_attachment', array($this, 'delete_related_images'));
27 13 }
28 14
29 15 /**
30 16 * Returns singleton instance
@@ -30,112 +16,103 @@
30 16 * Returns singleton instance
31 17 *
32 18 * @return WP_Optimize_WebP_Images
33 19 */
34 - public static function get_instance() {
20 + public static function get_instance(): self {
35 21 static $instance = null;
36 22 if (null === $instance) {
37 - $instance = new WP_Optimize_WebP_Images();
23 + $instance = new self();
38 24 }
39 25 return $instance;
40 26 }
41 27
42 28 /**
43 - * Deletes related image sizes and alternate webp format images
29 + * Deletes related image sizes and alternate WebP format images
44 30 *
45 31 * @param int $attachment_id
46 32 * @return void
47 33 */
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 - }
34 + public function delete_related_images($attachment_id): void {
35 + $meta = wp_get_attachment_metadata($attachment_id);
36 + if (false === $meta) {
37 + return;
38 + }
56 39
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);
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);
65 58 }
66 59
67 60 /**
68 - * Sets file information properties
61 + * Computes the upload directory path for the attachment
69 62 *
70 - * @param int $attachment_id
71 - * @return void
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
72 66 */
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 = isset($this->meta['file']) ? $this->meta['file'] : '';
80 - $basename = $file_path_info['basename'];
67 + private function get_upload_directory($basename, $meta): string {
68 + $file = isset($meta['file']) && is_string($meta['file']) ? $meta['file'] : '';
81 69 $sub_directory = '';
82 - if (!empty($file)) {
70 + if ('' !== $file) {
83 71 $sub_directory = str_replace($basename, '', $file);
84 72 }
85 73
86 74 $uploads = wp_get_upload_dir();
87 - $this->directory = $uploads['basedir'] . '/' . $sub_directory;
75 + return $uploads['basedir'] . '/' . $sub_directory;
88 76 }
89 77
90 78 /**
91 - * Returns all available image sizes for the given attachment id
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
92 87 */
93 - private function set_sizes() {
94 - if (false !== $this->meta) {
95 - $this->sizes = isset($this->meta['sizes']) ? $this->meta['sizes'] : array();
96 - }
97 - }
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 + );
98 93
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;
94 + $unscaled_filename = preg_replace('/-scaled$/', '', $filename);
107 95
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;
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;
113 100 }
114 101
102 + return $images;
115 103 }
116 104
117 105 /**
118 - * Delete related images
106 + * Deletes the given list of files from disk
119 107 *
108 + * @param array<string> $files List of absolute file paths to delete
120 109 * @return void
121 110 */
122 - private function delete_images() {
123 - foreach ($this->images as $image) {
124 - wp_delete_file($image);
111 + private function delete_files($files): void {
112 + foreach ($files as $file) {
113 + wp_delete_file($file);
125 114 }
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 115 }
139 116 }
140 117
141 118 endif;