| @@ -1,34 +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 | -use \WebPConvert\Convert\ConverterFactory; | |
| 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 { | |
| 6 | 10 | |
| 7 | -require_once WPO_PLUGIN_MAIN_PATH . 'vendor/autoload.php'; | |
| 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'; | |
| 8 | 17 | |
| 9 | -if (!class_exists('WPO_WebP_Test_Run')) : | |
| 10 | 18 | /** |
| 11 | - * Test run | |
| 19 | + * List of working converters. | |
| 20 | + * | |
| 21 | + * @var array<string> | |
| 12 | 22 | */ |
| 13 | -class WPO_WebP_Test_Run { | |
| 23 | + private static $working_converters = array(); | |
| 14 | 24 | |
| 15 | 25 | /** |
| 16 | - * Get a test result object OR false, if tests cannot be made. | |
| 26 | + * Errors collected during converter tests. | |
| 17 | 27 | * |
| 18 | - * @return object|false | |
| 28 | + * @var array<string> | |
| 19 | 29 | */ |
| 20 | - public static function get_converter_status() { | |
| 21 | - $source = WPO_PLUGIN_MAIN_PATH . 'images/logo/wpo_logo_small.png'; | |
| 22 | - $upload_dir = wp_upload_dir(); | |
| 23 | - $destination = $upload_dir['basedir']. '/wpo/images/wpo_logo_small.webp'; | |
| 30 | + private static $errors = array(); | |
| 24 | 31 | |
| 25 | - $converters = array( | |
| 26 | - // 'cwebp', | |
| 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( | |
| 27 | 58 | 'vips', |
| 28 | - 'imagemagick', | |
| 29 | - 'graphicsmagick', | |
| 30 | - 'ffmpeg', | |
| 31 | 59 | 'wpc', |
| 32 | 60 | 'ewww', |
| 33 | 61 | 'imagick', |
| 34 | 62 | 'gmagick', |
| @@ -33,30 +61,123 @@ | ||
| 33 | 61 | 'imagick', |
| 34 | 62 | 'gmagick', |
| 35 | 63 | 'gd', |
| 36 | 64 | ); |
| 37 | - $working_converters = array(); | |
| 38 | - $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 | + } | |
| 39 | 70 | |
| 40 | - foreach ($converters as $converter) { | |
| 41 | - $converter_id = $converter; | |
| 42 | - try { | |
| 43 | - $converter_instance = ConverterFactory::makeConverter( | |
| 44 | - $converter_id, | |
| 45 | - $source, | |
| 46 | - $destination | |
| 47 | - ); | |
| 48 | - $converter_instance->doConvert(); | |
| 49 | - $working_converters[] = $converter_id; | |
| 50 | - } catch (\Exception $e) { | |
| 51 | - $errors[$converter_id] = $e->getMessage(); | |
| 52 | - } | |
| 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()); | |
| 53 | 102 | } |
| 54 | 103 | |
| 55 | 104 | return array( |
| 56 | - 'working_converters' => $working_converters, | |
| 57 | - 'errors' => $errors, | |
| 105 | + 'working_converters' => self::$working_converters, | |
| 106 | + 'errors' => self::$errors, | |
| 58 | 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); | |
| 59 | 180 | } |
| 60 | 181 | } |
| 61 | 182 | |
| 62 | 183 | endif; |