-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjson-handler.php
63 lines (48 loc) · 1.46 KB
/
json-handler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/**
* JSON handler
*
* @copyright 2020 over-engineer
*/
namespace Animentor;
// Prevent direct access to files
if ( ! defined( 'ABSPATH' ) ) exit;
class Json_Handler {
const MIME_TYPE = 'application/json';
public function upload_mimes( $allowed_types ) {
$allowed_types['json'] = self::MIME_TYPE;
return $allowed_types;
}
public function wp_handle_upload_prefilter( $file ) {
if ( self::MIME_TYPE !== $file['type'] ) {
return $file;
}
$ext = pathinfo( $file['name'], PATHINFO_EXTENSION );
if ( 'json' !== $ext ) {
$file['error'] = sprintf(
__( 'The uploaded %s file is not supported. Please upload a valid JSON file', 'animentor-lottie-bodymovin-elementor' )
);
return $file;
}
return $file;
}
public function wp_check_filetype_and_ext( $data, $file, $filename, $mimes ) {
if ( ! empty( $data['ext'] ) && ! empty( $data['type'] ) ) {
return $data;
}
$filetype = wp_check_filetype( $filename, $mimes );
if ( 'json' === $filetype['ext'] ) {
$data['ext'] = 'json';
$data['type'] = self::MIME_TYPE;
}
return $data;
}
/**
* Json_Handler constructor.
*/
public function __construct() {
add_filter( 'upload_mimes', array( $this, 'upload_mimes' ) );
add_filter( 'wp_handle_upload_prefilter', array( $this, 'wp_handle_upload_prefilter' ) );
add_filter( 'wp_check_filetype_and_ext', array( $this, 'wp_check_filetype_and_ext' ), 10, 4 );
}
}