| 1 |
<?php |
| 2 |
/** |
| 3 |
* Created by PhpStorm. |
| 4 |
* User: alex |
| 5 |
* Date: 1/11/19 |
| 6 |
* Time: 10:59 AM |
| 7 |
*/ |
| 8 |
|
| 9 |
class Brizy_Admin_Json_Main { |
| 10 |
|
| 11 |
/** |
| 12 |
* @return Brizy_Admin_Json_Main|mixed |
| 13 |
*/ |
| 14 |
public static function _init() { |
| 15 |
static $instance; |
| 16 |
if ( ! $instance ) { |
| 17 |
$instance = new self(); |
| 18 |
} |
| 19 |
|
| 20 |
return $instance; |
| 21 |
} |
| 22 |
|
| 23 |
public static function isJsonEnabled() { |
| 24 |
return Brizy_Editor_Storage_Common::instance()->get( 'json-upload', false ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Brizy_Admin_Json_Main constructor. |
| 29 |
* @throws Brizy_Editor_Exceptions_NotFound |
| 30 |
*/ |
| 31 |
public function __construct() { |
| 32 |
if ( self::isJsonEnabled() ) { |
| 33 |
$this->enableJsonUpload(); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public function addJsonMimeType( $mimes ) { |
| 38 |
$mimes['json'] = 'application/json'; |
| 39 |
|
| 40 |
return $mimes; |
| 41 |
} |
| 42 |
|
| 43 |
public function enableJsonUpload() { |
| 44 |
add_filter( 'upload_mimes', [ $this, 'addJsonMimeType' ] ); |
| 45 |
if ( extension_loaded( 'fileinfo' ) ) { |
| 46 |
add_filter( 'wp_check_filetype_and_ext', [ $this, 'checkJsonFiletype' ], 10, 3 ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
public function disableJsonUpload() { |
| 51 |
remove_filter( 'upload_mimes', [ $this, 'addJsonMimeType' ] ); |
| 52 |
remove_filter( 'wp_check_filetype_and_ext', [ $this, 'checkJsonFiletype' ] ); |
| 53 |
} |
| 54 |
|
| 55 |
public function checkJsonFiletype( $data, $file, $filename ) { |
| 56 |
$ext = pathinfo( $filename, PATHINFO_EXTENSION ); |
| 57 |
if ( $ext !== 'json' ) { |
| 58 |
return $data; |
| 59 |
} |
| 60 |
$content = file_get_contents( $file ); |
| 61 |
$json = json_decode( $content ); |
| 62 |
if ( ! $json ) { |
| 63 |
return $data; |
| 64 |
} |
| 65 |
$data['ext'] = 'json'; |
| 66 |
$data['type'] = 'application/json'; |
| 67 |
|
| 68 |
return $data; |
| 69 |
} |
| 70 |
} |
| 71 |
|