PluginProbe
Brizy – Page Builder / trunk
Brizy – Page Builder vtrunk
2.8.23 2.8.22 2.8.21 2.8.20 2.8.19 2.8.18 2.8.17 2.8.16 2.8.15 2.8.14 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.15 2.7.16 All 188 releases
brizy / admin / json / main.php

main.php in Brizy – Page Builder trunk, at admin/json/main.php

71 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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