PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.5.2
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.5.2
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
wp-optimize / webp / class-wpo-webp-utils.php

class-wpo-webp-utils.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.5.2, at webp/class-wpo-webp-utils.php

111 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) die('No direct access allowed');
4
5 use \WebPConvert\Convert\ConverterFactory;
6
7 require_once WPO_PLUGIN_MAIN_PATH . 'vendor/autoload.php';
8
9 if (!class_exists('WPO_WebP_Utils')) :
10
11 class WPO_WebP_Utils {
12
13 /**
14 * Determines whether we can do webp conversion or not
15 *
16 * @return bool
17 */
18 public static function can_do_webp_conversion() {
19 $webp_conversion = WP_Optimize()->get_options()->get_option('webp_conversion');
20 $webp_converters = WP_Optimize()->get_options()->get_option('webp_converters');
21 return $webp_conversion && !empty($webp_converters);
22 }
23
24 /**
25 * Convert given image file to webp format
26 *
27 * @param string $source Path of image file
28 *
29 * @return void
30 */
31 public static function do_webp_conversion($source) {
32 $webp_converter = new WPO_WebP_Convert();
33 $webp_converter->convert($source);
34 }
35
36 /**
37 * Converts an image to WebP format using a specified converter.
38 *
39 * This method acts as a wrapper for the `WebPConvert\Convert\Converters\AbstractConverter::doConvert()` method
40 * from the `webp-convert` library.
41 *
42 * @param string $converter The converter to be used for the conversion process.
43 * @param string $source The path to the source image file.
44 * @param string $destination The path to the destination WebP image file.
45 */
46 public static function perform_webp_conversion($converter, $source, $destination) {
47 $converter_instance = ConverterFactory::makeConverter(
48 $converter,
49 $source,
50 $destination
51 );
52
53 set_error_handler(array(__CLASS__, 'handle_webp_conversion_warnings'), E_WARNING); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler -- This is needed in order to suppress PHP warnings thrown by third party library
54
55 $converter_instance->doConvert();
56
57 restore_error_handler();
58 }
59
60 /**
61 * Custom error handler for handling PHP warnings during the WebP conversion process.
62 *
63 * @param int $errno The level of the error raised, as an integer.
64 * @param string $errstr The error message.
65 *
66 * @return bool
67 */
68 public static function handle_webp_conversion_warnings($errno, $errstr) {
69 $patterns = array(
70 '/unlink\(.+\): No such file or directory/',
71 '/rename\(.+\): No such file or directory/',
72 '/filesize\(\): stat failed for/',
73 );
74
75 foreach ($patterns as $pattern) {
76 if (preg_match($pattern, $errstr)) {
77 // Suppress the warning by returning true
78 return true;
79 }
80 }
81
82 // For other PHP warnings, use the default PHP error handler by returning false
83 return false;
84 }
85
86 /**
87 * Decide whether the browser requesting the URL can accept webp images or not
88 *
89 * @return bool
90 */
91 public static function is_browser_accepting_webp() {
92 $http_accept = TeamUpdraft\WP_Optimize\Includes\Fragments\fetch_superglobal('server', 'HTTP_ACCEPT', 'string', 'sanitize_text_field', '');
93 if (false !== strpos($http_accept, 'image/webp')) {
94 return true;
95 }
96
97 // Link to compatibility site - https://caniuse.com/webp
98 $user_agent = TeamUpdraft\WP_Optimize\Includes\Fragments\fetch_superglobal('server', 'HTTP_USER_AGENT', 'string', 'sanitize_text_field', '');
99 if (empty($user_agent)) return false;
100
101 // Check Firefox version number
102 if (preg_match('/Firefox\/([\d\.]+[a-z\d]*)/', $user_agent, $matches)) {
103 if (version_compare('65.0.0', $matches[1], '<=')) return true;
104 }
105
106 return false;
107 }
108 }
109
110 endif;
111