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-test-run.php +156 -29 3.5.0 → 4.7.0 View file →
@@ -1,30 +1,62 @@
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_Test_Run')) :
6 +/**
7 + * Handles WebP converter test runs to determine which converters are available on the server.
8 + */
9 +class WPO_WebP_Test_Run {
10 +
6 11 /**
7 - * Test run
12 + * Filename of the test image used for conversion attempts.
13 + *
14 + * @var string
8 15 */
9 -class WPO_WebP_Test_Run {
16 + const TEST_IMAGE_FILENAME = 'wpo_logo_small.png';
10 17
11 18 /**
12 - * Get a test result object OR false, if tests cannot be made.
19 + * List of working converters.
13 20 *
14 - * @return object|false
21 + * @var array<string>
15 22 */
16 - public static function get_converter_status() {
17 - $source = WPO_PLUGIN_MAIN_PATH . 'images/logo/wpo_logo_small.png';
18 - $upload_dir = wp_upload_dir();
19 - $destination = $upload_dir['basedir']. '/wpo/images/wpo_logo_small.png.webp';
23 + private static $working_converters = array();
20 24
21 - $converters = array(
22 - // 'cwebp',
25 + /**
26 + * Errors collected during converter tests.
27 + *
28 + * @var array<string>
29 + */
30 + private static $errors = array();
31 +
32 + /**
33 + * Get the full path to the source test image.
34 + *
35 + * @return string
36 + */
37 + private static function get_source_path(): string {
38 + return WPO_PLUGIN_MAIN_PATH . 'images/logo/' . self::TEST_IMAGE_FILENAME;
39 + }
40 +
41 + /**
42 + * Get the full path for the WebP test output in the uploads directory.
43 + *
44 + * @param string $base_dir The uploads base directory.
45 + * @return string
46 + */
47 + private static function get_destination_path($base_dir): string {
48 + return $base_dir . '/wpo/images/' . self::TEST_IMAGE_FILENAME . '.webp';
49 + }
50 +
51 + /**
52 + * Get a list of converters that don't use shell functions.
53 + *
54 + * @return array<string>
55 + */
56 + public static function get_converters_without_shell(): array {
57 + $converters_without_shell = array(
23 58 'vips',
24 - 'imagemagick',
25 - 'graphicsmagick',
26 - 'ffmpeg',
27 59 'wpc',
28 60 'ewww',
29 61 'imagick',
30 62 'gmagick',
@@ -29,28 +61,123 @@
29 61 'imagick',
30 62 'gmagick',
31 63 'gd',
32 64 );
33 - $working_converters = array();
34 - $errors = array();
65 + $filtered_converters_without_shell = apply_filters('wpo_converters_without_shell', $converters_without_shell);
66 + $filtered_converters_without_shell = is_array($filtered_converters_without_shell) ? $filtered_converters_without_shell : $converters_without_shell;
67 + /** @var array<string> $filtered_converters_without_shell */
68 + return $filtered_converters_without_shell;
69 + }
35 70
36 - foreach ($converters as $converter) {
37 - $converter_id = $converter;
38 - try {
39 - WPO_WebP_Utils::perform_webp_conversion($converter_id, $source, $destination);
40 - $working_converters[] = $converter_id;
41 - // Copying source file to `uploads` folder. To be used test redirection
42 - // We're doing it here, to make sure folders already exists `/wpo/images/`
43 - copy($source, $upload_dir['basedir'] . '/wpo/images/wpo_logo_small.png');
44 - } catch (\Exception $e) {
45 - $errors[$converter_id] = $e->getMessage();
46 - }
71 + /**
72 + * Get a list of converters that use shell functions.
73 + *
74 + * @return array<string>
75 + */
76 + public static function get_converters_with_shell(): array {
77 + $converters_with_shell = array(
78 + 'imagemagick',
79 + 'graphicsmagick',
80 + 'ffmpeg',
81 + );
82 + $filtered_converters_with_shell = apply_filters('wpo_converters_with_shell', $converters_with_shell);
83 + $filtered_converters_with_shell = is_array($filtered_converters_with_shell) ? $filtered_converters_with_shell : $converters_with_shell;
84 +
85 + /** @var array<string> $filtered_converters_with_shell */
86 + return $filtered_converters_with_shell;
87 + }
88 +
89 + /**
90 + * Get an array of working and non-working converters.
91 + *
92 + * @return array<string, array<string>>
93 + */
94 + public static function get_converter_status(): array {
95 + self::$working_converters = array();
96 + self::$errors = array();
97 +
98 + self::try_converters(self::get_converters_without_shell());
99 +
100 + if (empty(self::$working_converters)) {
101 + self::try_shell_converters(self::get_converters_with_shell());
47 102 }
48 103
49 104 return array(
50 - 'working_converters' => $working_converters,
51 - 'errors' => $errors,
105 + 'working_converters' => self::$working_converters,
106 + 'errors' => self::$errors,
52 107 );
108 + }
109 +
110 + /**
111 + * Try shell-based converters, logging an error for each if shell functions are unavailable.
112 + *
113 + * @param array<string> $converters List of shell-based converter IDs.
114 + * @return void
115 + */
116 + private static function try_shell_converters($converters): void {
117 + if (WP_Optimize_WebP::get_instance()->shell_functions_available()) {
118 + self::try_converters($converters);
119 + return;
120 + }
121 +
122 + foreach ($converters as $converter_id) {
123 + self::$errors[$converter_id] = __('Required WebP shell functions are not available on the server.', 'wp-optimize');
124 + }
125 + }
126 +
127 + /**
128 + * Try each converter from the given list to convert the test image to WebP.
129 + *
130 + * @param array<string> $converters List of converter IDs to try.
131 + * @return void
132 + */
133 + private static function try_converters($converters): void {
134 + $upload_dir = wp_upload_dir();
135 + $base_dir = $upload_dir['basedir'];
136 + $source = self::get_source_path();
137 + $destination = self::get_destination_path($base_dir);
138 +
139 + foreach ($converters as $converter_id) {
140 + $result = self::try_converter($converter_id, $source, $destination);
141 +
142 + if (true === $result) {
143 + self::$working_converters[] = $converter_id;
144 + self::copy_test_image($source, $base_dir);
145 + } else {
146 + self::$errors[$converter_id] = $result;
147 + }
148 + }
149 + }
150 +
151 + /**
152 + * Attempt a single WebP conversion and return the result.
153 + *
154 + * @param string $converter_id The converter identifier.
155 + * @param string $source Full path to the source image.
156 + * @param string $destination Full path for the WebP output.
157 + * @return true|string True on success, or an error message string on failure.
158 + */
159 + private static function try_converter($converter_id, $source, $destination) {
160 + try {
161 + WPO_WebP_Utils::perform_webp_conversion($converter_id, $source, $destination);
162 + return true;
163 + } catch (\Exception $e) {
164 + return $e->getMessage();
165 + }
166 + }
167 +
168 + /**
169 + * Copy the test source image to the uploads folder for redirection testing.
170 + *
171 + * The /wpo/images/ directory is guaranteed to exist at this point because
172 + * perform_webp_conversion() already created it during the conversion step.
173 + *
174 + * @param string $source Full path to the source image.
175 + * @param string $base_dir The uploads base directory.
176 + * @return void
177 + */
178 + private static function copy_test_image($source, $base_dir): void {
179 + copy($source, $base_dir . '/wpo/images/' . self::TEST_IMAGE_FILENAME);
53 180 }
54 181 }
55 182
56 183 endif;