wp-optimize
/
vendor
/
rosell-dk
/
webp-convert
/
src
/
Convert
/
Converters
/
ConverterTraits
/
CurlTrait.php
CurlTrait.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.21, at vendor/rosell-dk/webp-convert/src/Convert/Converters/ConverterTraits/CurlTrait.php
| 1 | <?php |
| 2 | |
| 3 | namespace WebPConvert\Convert\Converters\ConverterTraits; |
| 4 | |
| 5 | use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException; |
| 6 | use WebPConvert\Convert\Converters\AbstractConverter; |
| 7 | |
| 8 | /** |
| 9 | * Trait for converters that works by uploading to a cloud service. |
| 10 | * |
| 11 | * The trait adds a method for checking against upload limits. |
| 12 | * |
| 13 | * @package WebPConvert |
| 14 | * @author Bjørn Rosell <[email protected]> |
| 15 | * @since Class available since Release 2.0.0 |
| 16 | */ |
| 17 | trait CurlTrait |
| 18 | { |
| 19 | |
| 20 | /** |
| 21 | * Check basis operationality for converters relying on curl. |
| 22 | * |
| 23 | * Performs the same as ::checkOperationality(). It is here so converters that overrides the |
| 24 | * ::checkOperationality() still has a chance to do the checks. |
| 25 | * |
| 26 | * @throws SystemRequirementsNotMetException |
| 27 | * @return void |
| 28 | */ |
| 29 | public function checkOperationalityForCurlTrait() |
| 30 | { |
| 31 | if (!extension_loaded('curl')) { |
| 32 | throw new SystemRequirementsNotMetException('Required cURL extension is not available.'); |
| 33 | } |
| 34 | |
| 35 | if (!function_exists('curl_init')) { |
| 36 | throw new SystemRequirementsNotMetException('Required url_init() function is not available.'); |
| 37 | } |
| 38 | |
| 39 | if (!function_exists('curl_file_create')) { |
| 40 | throw new SystemRequirementsNotMetException( |
| 41 | 'Required curl_file_create() function is not available (requires PHP > 5.5).' |
| 42 | ); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Check basis operationality for converters relying on curl |
| 48 | * |
| 49 | * @throws SystemRequirementsNotMetException |
| 50 | * @return void |
| 51 | */ |
| 52 | public function checkOperationality() |
| 53 | { |
| 54 | $this->checkOperationalityForCurlTrait(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Init curl. |
| 59 | * |
| 60 | * @throws SystemRequirementsNotMetException if curl could not be initialized |
| 61 | * @return resource|\CurlHandle curl handle (from PHP8: CurlHandle) |
| 62 | */ |
| 63 | protected static function initCurl() |
| 64 | { |
| 65 | // Get curl handle |
| 66 | $ch = \curl_init(); |
| 67 | if ($ch === false) { |
| 68 | throw new SystemRequirementsNotMetException('Could not initialise cURL.'); |
| 69 | } |
| 70 | return $ch; |
| 71 | } |
| 72 | } |
| 73 |