assets
5 years ago
json-handler.php
5 years ago
lottie-handler.php
5 years ago
lottie.php
5 years ago
json-handler.php
90 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ElementsKit_Lite; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | class ElementsKit_Json_Handler { |
| 8 | |
| 9 | const MIME_TYPE = 'application/json'; |
| 10 | const EXT = 'json'; |
| 11 | |
| 12 | |
| 13 | /** |
| 14 | * The plugin instance. |
| 15 | */ |
| 16 | public static $instance = null; |
| 17 | |
| 18 | |
| 19 | /** |
| 20 | * constructor function. |
| 21 | */ |
| 22 | public function __construct() { |
| 23 | add_filter('upload_mimes', array($this, 'upload_mimes')); |
| 24 | add_filter('wp_handle_upload_prefilter', array($this, 'wp_handle_upload_prefilter')); |
| 25 | add_filter('wp_check_filetype_and_ext', array($this, 'wp_check_filetype_and_ext'), 10, 4); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * Adds json file format |
| 31 | */ |
| 32 | public function upload_mimes($allowed_types) { |
| 33 | $allowed_types[self::EXT] = self::MIME_TYPE; |
| 34 | |
| 35 | return $allowed_types; |
| 36 | } |
| 37 | |
| 38 | |
| 39 | public function wp_handle_upload_prefilter($file) { |
| 40 | if(self::MIME_TYPE !== $file['type']) { |
| 41 | return $file; |
| 42 | } |
| 43 | |
| 44 | $ext = pathinfo($file['name'], PATHINFO_EXTENSION); |
| 45 | |
| 46 | if(self::EXT !== $ext) { |
| 47 | $file['error'] = sprintf( |
| 48 | __('The uploaded %s file is not supported. Please upload a valid JSON file', 'elementskit-lite'), |
| 49 | $file['name'] |
| 50 | ); |
| 51 | |
| 52 | return $file; |
| 53 | } |
| 54 | |
| 55 | return $file; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | public function wp_check_filetype_and_ext($data, $file, $filename, $mimes) { |
| 60 | if(!empty($data['ext']) && !empty($data['type'])) { |
| 61 | return $data; |
| 62 | } |
| 63 | |
| 64 | $filetype = wp_check_filetype($filename, $mimes); |
| 65 | |
| 66 | if(self::EXT === $filetype['ext']) { |
| 67 | $data['ext'] = self::EXT; |
| 68 | $data['type'] = self::MIME_TYPE; |
| 69 | } |
| 70 | |
| 71 | return $data; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * Instance. |
| 77 | */ |
| 78 | public static function instance() { |
| 79 | if(is_null(self::$instance)) { |
| 80 | // Fire when ElementsKit_Lite instance. |
| 81 | self::$instance = new self(); |
| 82 | } |
| 83 | |
| 84 | return self::$instance; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Run the instance. |
| 89 | ElementsKit_Json_Handler::instance(); |
| 90 |