| @@ -2,34 +2,60 @@ | ||
| 2 | 2 | |
| 3 | 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 | - * List of working coverters | |
| 19 | + * List of working converters. | |
| 13 | 20 | * |
| 14 | - * @var array | |
| 21 | + * @var array<string> | |
| 15 | 22 | */ |
| 16 | 23 | private static $working_converters = array(); |
| 17 | 24 | |
| 18 | 25 | /** |
| 19 | - * Errors array | |
| 26 | + * Errors collected during converter tests. | |
| 20 | 27 | * |
| 21 | - * @var array | |
| 28 | + * @var array<string> | |
| 22 | 29 | */ |
| 23 | 30 | private static $errors = array(); |
| 24 | 31 | |
| 25 | 32 | /** |
| 26 | - * Get a list of converters that don't use shell functions | |
| 33 | + * Get the full path to the source test image. | |
| 27 | 34 | * |
| 28 | - * @return array | |
| 35 | + * @return string | |
| 29 | 36 | */ |
| 30 | - public static function get_converters_without_shell() { | |
| 31 | - return apply_filters('wpo_converters_without_shell', array( | |
| 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( | |
| 32 | 58 | 'vips', |
| 33 | 59 | 'wpc', |
| 34 | 60 | 'ewww', |
| 35 | 61 | 'imagick', |
| @@ -34,50 +60,46 @@ | ||
| 34 | 60 | 'ewww', |
| 35 | 61 | 'imagick', |
| 36 | 62 | 'gmagick', |
| 37 | 63 | 'gd', |
| 38 | - )); | |
| 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; | |
| 39 | 69 | } |
| 40 | 70 | |
| 41 | 71 | /** |
| 42 | - * Get a list of converters that use shell functions | |
| 72 | + * Get a list of converters that use shell functions. | |
| 43 | 73 | * |
| 44 | - * @return array | |
| 74 | + * @return array<string> | |
| 45 | 75 | */ |
| 46 | - public static function get_converters_with_shell() { | |
| 47 | - return apply_filters('wpo_converters_with_shell', array( | |
| 48 | - // 'cwebp', | |
| 76 | + public static function get_converters_with_shell(): array { | |
| 77 | + $converters_with_shell = array( | |
| 49 | 78 | 'imagemagick', |
| 50 | 79 | 'graphicsmagick', |
| 51 | 80 | 'ffmpeg', |
| 52 | - )); | |
| 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; | |
| 53 | 87 | } |
| 54 | 88 | |
| 55 | 89 | /** |
| 56 | - * Get an array of working and non-working converters list | |
| 90 | + * Get an array of working and non-working converters. | |
| 57 | 91 | * |
| 58 | - * @return array | |
| 92 | + * @return array<string, array<string>> | |
| 59 | 93 | */ |
| 60 | - public static function get_converter_status() { | |
| 61 | - | |
| 62 | - $converters_without_shell = self::get_converters_without_shell(); | |
| 63 | - $converters_with_shell = self::get_converters_with_shell(); | |
| 64 | - | |
| 94 | + public static function get_converter_status(): array { | |
| 65 | 95 | self::$working_converters = array(); |
| 66 | 96 | self::$errors = array(); |
| 67 | 97 | |
| 68 | - self::try_converters($converters_without_shell); | |
| 98 | + self::try_converters(self::get_converters_without_shell()); | |
| 69 | 99 | |
| 70 | 100 | if (empty(self::$working_converters)) { |
| 71 | - if (WP_Optimize_WebP::get_instance()->shell_functions_available()) { | |
| 72 | - self::try_converters($converters_with_shell); | |
| 73 | - } else { | |
| 74 | - // If no working converters that don’t require shell access are found, | |
| 75 | - // and shell functions are unavailable, store the errors. | |
| 76 | - foreach ($converters_with_shell as $converter_id) { | |
| 77 | - self::$errors[$converter_id] = __('Required WebP shell functions are not available on the server.', 'wp-optimize'); | |
| 78 | - } | |
| 79 | - } | |
| 101 | + self::try_shell_converters(self::get_converters_with_shell()); | |
| 80 | 102 | } |
| 81 | 103 | |
| 82 | 104 | return array( |
| 83 | 105 | 'working_converters' => self::$working_converters, |
| @@ -85,48 +107,77 @@ | ||
| 85 | 107 | ); |
| 86 | 108 | } |
| 87 | 109 | |
| 88 | 110 | /** |
| 89 | - * Tries each converter from the list to convert to WebP | |
| 111 | + * Try shell-based converters, logging an error for each if shell functions are unavailable. | |
| 90 | 112 | * |
| 91 | - * @param array $converters | |
| 113 | + * @param array<string> $converters List of shell-based converter IDs. | |
| 92 | 114 | * @return void |
| 93 | 115 | */ |
| 94 | - private static function try_converters($converters) { | |
| 95 | - $source = WPO_PLUGIN_MAIN_PATH . 'images/logo/wpo_logo_small.png'; | |
| 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 { | |
| 96 | 134 | $upload_dir = wp_upload_dir(); |
| 97 | - $destination = $upload_dir['basedir']. '/wpo/images/wpo_logo_small.png.webp'; | |
| 135 | + $base_dir = $upload_dir['basedir']; | |
| 136 | + $source = self::get_source_path(); | |
| 137 | + $destination = self::get_destination_path($base_dir); | |
| 98 | 138 | |
| 99 | 139 | foreach ($converters as $converter_id) { |
| 100 | - $conversion_result = self::try_converter($converter_id, $source, $destination, $upload_dir['basedir']); | |
| 140 | + $result = self::try_converter($converter_id, $source, $destination); | |
| 101 | 141 | |
| 102 | - if (true === $conversion_result) { | |
| 142 | + if (true === $result) { | |
| 103 | 143 | self::$working_converters[] = $converter_id; |
| 144 | + self::copy_test_image($source, $base_dir); | |
| 104 | 145 | } else { |
| 105 | - self::$errors[$converter_id] = $conversion_result; | |
| 146 | + self::$errors[$converter_id] = $result; | |
| 106 | 147 | } |
| 107 | 148 | } |
| 108 | 149 | } |
| 109 | 150 | |
| 110 | 151 | /** |
| 111 | - * Tries to convert to WebP and returns true if the conversion finishes successfully. | |
| 152 | + * Attempt a single WebP conversion and return the result. | |
| 112 | 153 | * |
| 113 | - * @param string $converter_id | |
| 114 | - * @param string $source | |
| 115 | - * @param string $destination | |
| 116 | - * @param string $target_dir | |
| 117 | - * @return true|string | |
| 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. | |
| 118 | 158 | */ |
| 119 | - private static function try_converter($converter_id, $source, $destination, $target_dir) { | |
| 159 | + private static function try_converter($converter_id, $source, $destination) { | |
| 120 | 160 | try { |
| 121 | 161 | WPO_WebP_Utils::perform_webp_conversion($converter_id, $source, $destination); |
| 122 | - // Copying source file to `uploads` folder. To be used test redirection | |
| 123 | - // We're doing it here, to make sure folders already exists `/wpo/images/` | |
| 124 | - copy($source, $target_dir. '/wpo/images/wpo_logo_small.png'); | |
| 125 | 162 | return true; |
| 126 | 163 | } catch (\Exception $e) { |
| 127 | 164 | return $e->getMessage(); |
| 128 | 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); | |
| 129 | 180 | } |
| 130 | 181 | } |
| 131 | 182 | |
| 132 | 183 | endif; |