| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 |
|
| 5 |
if (!class_exists('WPO_WebP_Test_Run')) : |
| 6 |
/** |
| 7 |
* Test run |
| 8 |
*/ |
| 9 |
class WPO_WebP_Test_Run { |
| 10 |
|
| 11 |
/** |
| 12 |
* List of working coverters |
| 13 |
* |
| 14 |
* @var array |
| 15 |
*/ |
| 16 |
private static $working_converters = array(); |
| 17 |
|
| 18 |
/** |
| 19 |
* Errors array |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
private static $errors = array(); |
| 24 |
|
| 25 |
/** |
| 26 |
* Get a list of converters that don't use shell functions |
| 27 |
* |
| 28 |
* @return array |
| 29 |
*/ |
| 30 |
public static function get_converters_without_shell() { |
| 31 |
return apply_filters('wpo_converters_without_shell', array( |
| 32 |
'vips', |
| 33 |
'wpc', |
| 34 |
'ewww', |
| 35 |
'imagick', |
| 36 |
'gmagick', |
| 37 |
'gd', |
| 38 |
)); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get a list of converters that use shell functions |
| 43 |
* |
| 44 |
* @return array |
| 45 |
*/ |
| 46 |
public static function get_converters_with_shell() { |
| 47 |
return apply_filters('wpo_converters_with_shell', array( |
| 48 |
// 'cwebp', |
| 49 |
'imagemagick', |
| 50 |
'graphicsmagick', |
| 51 |
'ffmpeg', |
| 52 |
)); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get an array of working and non-working converters list |
| 57 |
* |
| 58 |
* @return array |
| 59 |
*/ |
| 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 |
|
| 65 |
self::$working_converters = array(); |
| 66 |
self::$errors = array(); |
| 67 |
|
| 68 |
self::try_converters($converters_without_shell); |
| 69 |
|
| 70 |
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 |
} |
| 80 |
} |
| 81 |
|
| 82 |
return array( |
| 83 |
'working_converters' => self::$working_converters, |
| 84 |
'errors' => self::$errors, |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Tries each converter from the list to convert to WebP |
| 90 |
* |
| 91 |
* @param array $converters |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
private static function try_converters($converters) { |
| 95 |
$source = WPO_PLUGIN_MAIN_PATH . 'images/logo/wpo_logo_small.png'; |
| 96 |
$upload_dir = wp_upload_dir(); |
| 97 |
$destination = $upload_dir['basedir']. '/wpo/images/wpo_logo_small.png.webp'; |
| 98 |
|
| 99 |
foreach ($converters as $converter_id) { |
| 100 |
$conversion_result = self::try_converter($converter_id, $source, $destination, $upload_dir['basedir']); |
| 101 |
|
| 102 |
if (true === $conversion_result) { |
| 103 |
self::$working_converters[] = $converter_id; |
| 104 |
} else { |
| 105 |
self::$errors[$converter_id] = $conversion_result; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Tries to convert to WebP and returns true if the conversion finishes successfully. |
| 112 |
* |
| 113 |
* @param string $converter_id |
| 114 |
* @param string $source |
| 115 |
* @param string $destination |
| 116 |
* @param string $target_dir |
| 117 |
* @return true|string |
| 118 |
*/ |
| 119 |
private static function try_converter($converter_id, $source, $destination, $target_dir) { |
| 120 |
try { |
| 121 |
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 |
return true; |
| 126 |
} catch (\Exception $e) { |
| 127 |
return $e->getMessage(); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
endif; |
| 133 |
|