1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Gestion des modules. |
4
|
|
|
* Les externals doivent être placés dans modules/ |
5
|
|
|
* |
6
|
|
|
* @author Eoxia <[email protected]> |
7
|
|
|
* @since 0.1.0 |
8
|
|
|
* @version 1.0.0 |
9
|
|
|
* @copyright 2015-2018 Eoxia |
10
|
|
|
* @package EO_Framework\Core\Util |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace eoxia; |
14
|
|
|
|
15
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
16
|
|
|
exit; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
if ( ! class_exists( '\eoxia\Module_Util' ) ) { |
20
|
|
|
/** |
21
|
|
|
* Gestion des modules |
22
|
|
|
*/ |
23
|
|
|
class Module_Util extends \eoxia\Singleton_Util { |
24
|
|
|
/** |
25
|
|
|
* Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util |
26
|
|
|
* |
27
|
|
|
* @since 0.1.0 |
28
|
|
|
* @version 1.0.0 |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
protected function construct() {} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Parcours le fichier digirisk.config.json pour récupérer les chemins vers tous les modules. |
36
|
|
|
* Initialise ensuite un par un, tous ses modules. |
37
|
|
|
* |
38
|
|
|
* @since 0.1.0 |
39
|
|
|
* @version 1.0.0 |
40
|
|
|
* |
41
|
|
|
* @param string $path Le chemin vers le module externe. |
42
|
|
|
* @param string $plugin_slug Le slug principale du plugin dans le fichier principale config.json. |
43
|
|
|
* |
44
|
|
|
* @return \WP_Error|boolean { |
45
|
|
|
* WP_Error Si le fichier est inexistant ou si le plugin n'a pas de submodule. |
46
|
|
|
* boolean True si aucune erreur s'est produite. |
47
|
|
|
*}. |
48
|
|
|
*/ |
49
|
|
|
public function exec_module( $path, $plugin_slug ) { |
50
|
|
|
if ( empty( \eoxia\Config_Util::$init[ $plugin_slug ] ) ) { |
51
|
|
|
return new \WP_Error( 'broke', sprintf( __( 'Main config %s not init', $plugin_slug ), $plugin_slug ) ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ( ! empty( \eoxia\Config_Util::$init[ $plugin_slug ]->modules ) ) { |
55
|
|
|
foreach ( \eoxia\Config_Util::$init[ $plugin_slug ]->modules as $module_json_path ) { |
56
|
|
|
self::inc_config_module( $plugin_slug, $path . $module_json_path ); |
57
|
|
|
self::inc_module( $plugin_slug, $path . $module_json_path ); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Appelle la méthode init_config de \eoxia\Config_Util pour initialiser les configs du module |
64
|
|
|
* |
65
|
|
|
* @since 0.1.0 |
66
|
|
|
* @version 1.0.0 |
67
|
|
|
* |
68
|
|
|
* @param string $plugin_slug Le slug du module externe à initialiser. |
69
|
|
|
* @param string $module_json_path Le chemin vers le dossier du module externe. |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
public function inc_config_module( $plugin_slug, $module_json_path ) { |
74
|
|
|
$init_status = \eoxia\Config_Util::g()->init_config( $module_json_path, $plugin_slug ); |
75
|
|
|
|
76
|
|
|
if ( \is_wp_error( $init_status ) ) { |
77
|
|
|
exit( $init_status->errors['broke'][0] ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Inclus les dépendences du module (qui sont défini dans le config.json du module en question) |
84
|
|
|
* |
85
|
|
|
* @since 0.1.0 |
86
|
|
|
* @version 1.0.0 |
87
|
|
|
* |
88
|
|
|
* @param string $plugin_slug Le slug du module externe à initialiser. |
89
|
|
|
* @param string $module_json_path Le chemin vers le dossier du module externe. |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function inc_module( $plugin_slug, $module_json_path ) { |
94
|
|
|
$module_name = basename( $module_json_path, '.config.json' ); |
95
|
|
|
$module_path = dirname( $module_json_path ); |
96
|
|
|
|
97
|
|
|
if ( 'eo-framework' !== $plugin_slug ) { |
98
|
|
|
if ( ! isset( \eoxia\Config_Util::$init[ $plugin_slug ]->$module_name ) ) { |
99
|
|
|
exit( __( sprintf( '%s not exists. You need to check: 1. The folder name and file name.config.json is equal at the slug name in config.', $module_name ) ) ); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ( ! isset( \eoxia\Config_Util::$init[ $plugin_slug ]->$module_name->state ) || ( isset( \eoxia\Config_Util::$init[ $plugin_slug ]->$module_name->state ) && \eoxia\Config_Util::$init[ $plugin_slug ]->$module_name->state ) ) { |
104
|
|
|
if ( ! empty( \eoxia\Config_Util::$init[ $plugin_slug ]->$module_name->dependencies ) ) { |
105
|
|
|
$can_inc = true; |
106
|
|
|
|
107
|
|
|
// Vérification de la dépendence d'inclusion. |
108
|
|
|
if ( ! empty( \eoxia\Config_Util::$init[ $plugin_slug ]->$module_name->dependencies_func ) ) { |
109
|
|
|
if ( ! empty( \eoxia\Config_Util::$init[ $plugin_slug ]->$module_name->dependencies_func ) ) { |
110
|
|
|
foreach ( \eoxia\Config_Util::$init[ $plugin_slug ]->$module_name->dependencies_func as $element ) { |
111
|
|
|
if ( ! call_user_func( $element ) ) { |
112
|
|
|
$can_inc = false; |
113
|
|
|
break; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ( $can_inc ) { |
120
|
|
|
foreach ( \eoxia\Config_Util::$init[ $plugin_slug ]->$module_name->dependencies as $dependence_folder => $list_option ) { |
121
|
|
|
$path_to_module_and_dependence_folder = $module_path . '/' . $dependence_folder . '/'; |
122
|
|
|
|
123
|
|
|
if ( ! empty( $list_option->priority ) ) { |
124
|
|
|
$this->inc_priority_file( $path_to_module_and_dependence_folder, $dependence_folder, $list_option->priority ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
\eoxia\Include_util::g()->in_folder( $path_to_module_and_dependence_folder ); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Inclus les fichiers prioritaires qui se trouvent dans la clé "priority" dans le .config.json du module |
136
|
|
|
* |
137
|
|
|
* @since 0.1.0 |
138
|
|
|
* @version 1.0.0 |
139
|
|
|
* |
140
|
|
|
* @param string $path_to_module_and_dependence_folder Le chemin vers le module. |
141
|
|
|
* @param string $dependence_folder le chemin vers le dossier à inclure. |
142
|
|
|
* @param array $list_priority_file La liste des chemins des fichiers à inclure en priorité. |
143
|
|
|
* @return void nothing |
144
|
|
|
*/ |
145
|
|
|
public function inc_priority_file( $path_to_module_and_dependence_folder, $dependence_folder, $list_priority_file ) { |
146
|
|
|
if ( ! empty( $list_priority_file ) ) { |
147
|
|
|
foreach ( $list_priority_file as $file_name ) { |
148
|
|
|
$path_file = realpath( $path_to_module_and_dependence_folder . $file_name ); |
149
|
|
|
|
150
|
|
|
require_once( $path_file ); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Désactives ou actives un module |
157
|
|
|
* |
158
|
|
|
* @since 1.2.0 |
159
|
|
|
* @version 1.2.0 |
160
|
|
|
* |
161
|
|
|
* @param string $namespace Le SLUG principale de lu plugin. |
162
|
|
|
* @param string $slug Le SLUG du module en question. |
163
|
|
|
* @param bool $state true ou false. |
164
|
|
|
*/ |
165
|
|
|
public function set_state( $namespace, $slug, $state ) { |
166
|
|
|
$path_to_json = \eoxia\Config_Util::$init[ $namespace ]->$slug->path . '/' . $slug . '.config.json'; |
167
|
|
|
|
168
|
|
|
$json_content = \eoxia\JSON_Util::g()->open_and_decode( $path_to_json ); |
169
|
|
|
$json_content->state = $state; |
170
|
|
|
$json_content = json_encode( $json_content, JSON_PRETTY_PRINT ); |
171
|
|
|
$json_content = preg_replace_callback( '/\\\\u([0-9a-f]{4})/i', function ( $matches ) { |
172
|
|
|
$sym = mb_convert_encoding( pack( 'H*', $matches[1] ), 'UTF-8', 'UTF-16' ); |
173
|
|
|
return $sym; |
174
|
|
|
}, $json_content ); |
175
|
|
|
|
176
|
|
|
$file = fopen( $path_to_json, 'w+' ); |
177
|
|
|
fwrite( $file, str_replace( '\/', '/', $json_content ) ); |
178
|
|
|
fclose( $file ); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} // End if(). |
182
|
|
|
|