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