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-wpo-webp-convert.php +37 -25 4.5.2 → 4.7.0 View file →
@@ -3,53 +3,65 @@
3 3 if (!defined('ABSPATH')) die('No direct access allowed');
4 4
5 5 if (!class_exists('WPO_WebP_Convert')) :
6 6
7 +/**
8 + * Handles WebP conversion by iterating through available converters.
9 + */
7 10 class WPO_WebP_Convert {
8 11
9 - public $converters = null;
12 + /**
13 + * @var array<string>
14 + */
15 + private $converters = array();
10 16
11 - public function __construct() {
12 - $this->converters = WP_Optimize()->get_options()->get_option('webp_converters');
17 + private function __construct() {
18 + $webp_converters = WP_Optimize()->get_options()->get_option('webp_converters');
19 + $this->converters = false === $webp_converters ? array() : $webp_converters;
13 20 }
14 21
15 22 /**
16 - * Converts uploaded image to webp format
23 + * Singleton instance
17 24 *
18 - * @param string $source - path of the source file
19 - * @return bool
25 + * @return WPO_WebP_Convert
20 26 */
21 - public function convert($source) {
22 - if (count($this->converters) < 1) return false;
23 -
24 - $destination = $this->get_destination_path($source);
25 - return $this->check_converters_and_do_conversion($source, $destination);
27 + public static function get_instance(): self {
28 + static $instance = null;
29 + if (null === $instance) {
30 + $instance = new self();
31 + }
32 + return $instance;
26 33 }
27 34
28 35 /**
29 - * Returns the destination full path
36 + * Convert an image file to WebP format using the first available converter that succeeds.
30 37 *
31 - * @param string $source - path of the source file
32 - *
33 - * @return string $destination - path of destination file
38 + * @param string $source Path of the source file.
39 + * @return bool True if conversion succeeded, false otherwise.
34 40 */
35 - public function get_destination_path($source) {
36 - $path_parts = pathinfo($source);
37 - return $path_parts['dirname'] . '/'. basename($source) . '.webp';
41 + public function convert($source): bool {
42 + if (empty($this->converters)) {
43 + return false;
44 + }
45 +
46 + $destination = WPO_WebP_Utils::get_destination_path($source);
47 +
48 + return $this->attempt_conversion($source, $destination);
38 49 }
39 50
40 51 /**
41 - * Loop through available converters and do the conversion
52 + * Try each configured converter in order until one produces a WebP file.
42 53 *
43 - * @param string $source - path of source file
44 - * @param string $destination - path of destination file
45 - *
46 - * @return bool
54 + * @param string $source Path of the source file.
55 + * @param string $destination Path of the destination file.
56 + * @return bool True if a converter produced the destination file.
47 57 */
48 - protected function check_converters_and_do_conversion($source, $destination) {
58 + private function attempt_conversion($source, $destination): bool {
49 59 foreach ($this->converters as $converter) {
50 60 WPO_WebP_Utils::perform_webp_conversion($converter, $source, $destination);
51 - if (is_file($destination)) return true;
61 + if (is_file($destination)) {
62 + return true;
63 + }
52 64 }
53 65
54 66 return false;
55 67 }