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 +40 -24 3.4.1 → 4.7.0 View file →
@@ -1,53 +1,69 @@
1 1 <?php
2 2
3 -if (!defined('WPO_VERSION')) die('No direct access allowed');
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
25 + * @return WPO_WebP_Convert
19 26 */
20 - public function convert($source) {
21 - if (count($this->converters) < 1) return false;
22 -
23 - $destination = $this->get_destination_path($source);
24 - $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;
25 33 }
26 34
27 35 /**
28 - * Returns the destination full path
36 + * Convert an image file to WebP format using the first available converter that succeeds.
29 37 *
30 - * @param string $source - path of the source file
31 - *
32 - * @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.
33 40 */
34 - protected function get_destination_path($source) {
35 - $path_parts = pathinfo($source);
36 - $destination = $path_parts['dirname'] . '/'. basename($source) . '.webp';
37 - return $destination;
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
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.
45 57 */
46 - protected function check_converters_and_do_conversion($source, $destination) {
58 + private function attempt_conversion($source, $destination): bool {
47 59 foreach ($this->converters as $converter) {
48 60 WPO_WebP_Utils::perform_webp_conversion($converter, $source, $destination);
49 - break;
61 + if (is_file($destination)) {
62 + return true;
63 + }
50 64 }
65 +
66 + return false;
51 67 }
52 68 }
53 69 endif;