1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Gestion des fichiers JSON |
4
|
|
|
* |
5
|
|
|
* @package Evarisk\Plugin |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace eoxia; |
9
|
|
|
|
10
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
11
|
|
|
exit; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
if ( ! class_exists( '\eoxia\JSON_Util' ) ) { |
15
|
|
|
/** |
16
|
|
|
* Gestion des fichiers JSON |
17
|
|
|
* |
18
|
|
|
* @author Jimmy Latour <[email protected]> |
19
|
|
|
* @version 1.1.0.0 |
20
|
|
|
*/ |
21
|
|
|
class JSON_Util extends \eoxia\Singleton_Util { |
22
|
|
|
/** |
23
|
|
|
* Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util |
24
|
|
|
* |
25
|
|
|
* @return void nothing |
26
|
|
|
*/ |
27
|
|
|
protected function construct() {} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Ouvres et décode le fichier JSON $path_to_json |
31
|
|
|
* |
32
|
|
|
* @param string $path_to_json Le chemin vers le fichier JSON. |
33
|
|
|
* @return array Les données du fichier JSON |
34
|
|
|
*/ |
35
|
|
|
public function open_and_decode( $path_to_json, $output = 'STDCLASS' ) { |
36
|
|
View Code Duplication |
if ( ! file_exists( $path_to_json ) ) { |
|
|
|
|
37
|
|
|
if ( function_exists( 'eo_log' ) ) { |
38
|
|
|
eo_log( 'digi_open_and_decode', array( |
39
|
|
|
'message' => 'Impossible d\'ouvrir le fichier json: ' . $path_to_json, |
40
|
|
|
), 2 ); |
41
|
|
|
} else { |
42
|
|
|
return new \WP_Error( 'broke', __( 'Impossible d\'ouvrir le fichier JSON', 'digirisk' ) ); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$config_content = file_get_contents( $path_to_json ); |
47
|
|
|
|
48
|
|
|
if ( 'STDCLASS' === $output ) { |
49
|
|
|
$data = json_decode( $config_content ); |
50
|
|
|
} elseif ( 'ARRAY_A' === $output ) { |
51
|
|
|
$data = json_decode( $config_content, true ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ( null === $data && json_last_error() !== JSON_ERROR_NONE ) { |
|
|
|
|
55
|
|
View Code Duplication |
if ( function_exists( 'eo_log' ) ) { |
|
|
|
|
56
|
|
|
eo_log( 'digi_open_and_decode', array( |
57
|
|
|
'message' => 'Les données dans le fichier json: ' . $path_to_json . ' semble erronées', |
58
|
|
|
), 2 ); |
59
|
|
|
} else { |
60
|
|
|
return new \WP_Error( 'broke', __( 'Les données du fichier JSON semble erronnés', 'digirisk' ) ); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ( function_exists( 'eo_log' ) ) { |
65
|
|
|
eo_log( 'digi_open_and_decode', array( |
66
|
|
|
'message' => 'Le fichier json: ' . $path_to_json . ' sont : ' . wp_json_encode( $data ), |
67
|
|
|
), 2 ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $data; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Décodes la chaine de caractère $json_to_decode |
75
|
|
|
* |
76
|
|
|
* @param string $json_to_decode La chaine de caractère JSON. |
77
|
|
|
* @return array Les données décodées |
78
|
|
|
*/ |
79
|
|
|
public function decode( $json_to_decode ) { |
80
|
|
|
if ( ! is_string( $json_to_decode ) ) { |
81
|
|
|
return $json_to_decode; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$json_decoded = json_decode( $json_to_decode, true ); |
85
|
|
|
|
86
|
|
|
if ( ! $json_decoded ) { |
87
|
|
|
$json_to_decode = str_replace( '\\', '', $json_to_decode ); |
88
|
|
|
$json_decoded = json_decode( $json_to_decode, true ); |
89
|
|
|
|
90
|
|
|
if ( ! $json_decoded ) { |
91
|
|
|
return $json_to_decode; |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $json_decoded; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} // End if(). |
|
|
|
|
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.