| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 |
|
| 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 |
|
| 11 |
/** |
| 12 |
* Filename of the test image used for conversion attempts. |
| 13 |
* |
| 14 |
* @var string |
| 15 |
*/ |
| 16 |
const TEST_IMAGE_FILENAME = 'wpo_logo_small.png'; |
| 17 |
|
| 18 |
/** |
| 19 |
* List of working converters. |
| 20 |
* |
| 21 |
* @var array<string> |
| 22 |
*/ |
| 23 |
private static $working_converters = array(); |
| 24 |
|
| 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( |
| 58 |
'vips', |
| 59 |
'wpc', |
| 60 |
'ewww', |
| 61 |
'imagick', |
| 62 |
'gmagick', |
| 63 |
'gd', |
| 64 |
); |
| 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 |
} |
| 70 |
|
| 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()); |
| 102 |
} |
| 103 |
|
| 104 |
return array( |
| 105 |
'working_converters' => self::$working_converters, |
| 106 |
'errors' => self::$errors, |
| 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); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
endif; |
| 184 |
|