admin
1 year ago
cache
1 year ago
css
1 year ago
images
1 year ago
javascript
1 year ago
vendor
1 year ago
class-cwvpsb-treeshaking.php
1 year ago
cwvpsb_iframe.css
2 years ago
cwvpsb_iframe.js
4 years ago
functions.php
1 year ago
gravatar.php
1 year ago
helper-section.php
2 years ago
helper-section.php
48 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class Core_Web_Vital_Helper_Section{ |
| 7 | static function convert_to_webp($filename){ |
| 8 | $source = $filename; |
| 9 | $upload = wp_upload_dir(); |
| 10 | $destinationPath = $upload['basedir']."/cwv-webp-images"; |
| 11 | if(!is_dir($destinationPath)) { wp_mkdir_p($destinationPath); } |
| 12 | $destination = str_replace($upload['basedir'], $destinationPath, $filename).".webp"; |
| 13 | try { |
| 14 | require_once CWVPSB_PLUGIN_DIR."/includes/vendor/autoload.php"; |
| 15 | $convertOptions = []; |
| 16 | \WebPConvert\WebPConvert::convert($source, $destination, $convertOptions); |
| 17 | } catch (\WebpConvert\Exceptions\WebPConvertException $e) { |
| 18 | if(function_exists('error_log')){ error_log($e->getMessage()); } |
| 19 | } catch (\Exception $e) { |
| 20 | $message = 'An exception was thrown!'; |
| 21 | if(function_exists('error_log')){ error_log($e->getMessage()); } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | |
| 26 | static function do_upload_with_webp($filearray, $overrides = false, $ignore = false){ |
| 27 | if (isset($filearray['file'])) { |
| 28 | try { |
| 29 | $filename = $filearray['file']; |
| 30 | $allowedMimeTypes = []; |
| 31 | $allowedMimeTypes[] = 'image/jpeg'; |
| 32 | $allowedMimeTypes[] = 'image/png'; |
| 33 | |
| 34 | if(isset($filearray['type']) && ($filearray['type'] == 'image/svg+xml' || $filearray['type'] == 'image/webp')){ |
| 35 | return $filearray; |
| 36 | } |
| 37 | |
| 38 | if (!in_array(wp_get_image_mime($filename), $allowedMimeTypes)) { |
| 39 | return $filearray; |
| 40 | } |
| 41 | self::convert_to_webp($filename); |
| 42 | } catch (Exception $e) { |
| 43 | if(function_exists('error_log')){ error_log($e->getMessage()); } |
| 44 | } |
| 45 | } |
| 46 | return $filearray; |
| 47 | } |
| 48 | } |