1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* Gestion des termes (POST, PUT, GET, DELETE)
|
4
|
|
|
*
|
5
|
|
|
* @author Jimmy Latour <[email protected]>
|
6
|
|
|
* @since 1.0.0.0
|
7
|
|
|
* @version 1.3.0.0
|
8
|
|
|
* @copyright 2015-2017
|
9
|
|
|
* @package wpeo_model
|
10
|
|
|
* @subpackage class
|
11
|
|
|
*/
|
12
|
|
|
|
13
|
|
|
namespace eoxia;
|
14
|
|
|
|
15
|
|
|
if ( ! defined( 'ABSPATH' ) ) { exit; }
|
16
|
|
|
|
17
|
|
|
if ( ! class_exists( '\eoxia\Term_Class' ) ) {
|
18
|
|
|
/**
|
19
|
|
|
* Gestion des termes (POST, PUT, GET, DELETE)
|
20
|
|
|
*/
|
21
|
|
|
class Term_Class extends Singleton_Util {
|
22
|
|
|
|
23
|
|
|
/**
|
24
|
|
|
* Le nom du modèle
|
25
|
|
|
*
|
26
|
|
|
* @var string
|
27
|
|
|
*/
|
28
|
|
|
protected $model_name = 'term_model';
|
29
|
|
|
|
30
|
|
|
/**
|
31
|
|
|
* La clé principale pour post_meta
|
32
|
|
|
*
|
33
|
|
|
* @var string
|
34
|
|
|
*/
|
35
|
|
|
protected $meta_key = '_wpeo_term';
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* Le nom de la taxonomie
|
39
|
|
|
*
|
40
|
|
|
* @var string
|
41
|
|
|
*/
|
42
|
|
|
protected $taxonomy = 'category';
|
43
|
|
|
|
44
|
|
|
/**
|
45
|
|
|
* Utiles pour récupérer la clé unique
|
46
|
|
|
*
|
47
|
|
|
* @todo Rien à faire ici
|
48
|
|
|
* @var string
|
49
|
|
|
*/
|
50
|
|
|
protected $identifier_helper = 'term';
|
51
|
|
|
|
52
|
|
|
/**
|
53
|
|
|
* Fonction de callback après avoir récupérer le modèle en mode GET.
|
54
|
|
|
*
|
55
|
|
|
* @var array
|
56
|
|
|
*/
|
57
|
|
|
protected $after_get_function = array();
|
58
|
|
|
|
59
|
|
|
/**
|
60
|
|
|
* Fonction de callback avant d'insérer les données en mode POST.
|
61
|
|
|
*
|
62
|
|
|
* @var array
|
63
|
|
|
*/
|
64
|
|
|
protected $before_post_function = array();
|
65
|
|
|
|
66
|
|
|
/**
|
67
|
|
|
* Fonction de callback après avoir inséré les données en mode POST.
|
68
|
|
|
*
|
69
|
|
|
* @var array
|
70
|
|
|
*/
|
71
|
|
|
protected $after_post_function = array();
|
72
|
|
|
|
73
|
|
|
/**
|
74
|
|
|
* Fonction de callback avant de mêttre à jour les données en mode PUT.
|
75
|
|
|
*
|
76
|
|
|
* @var array
|
77
|
|
|
*/
|
78
|
|
|
protected $before_put_function = array();
|
79
|
|
|
|
80
|
|
|
/**
|
81
|
|
|
* Fonction de callback après avoir mis à jour les données en mode PUT.
|
82
|
|
|
*
|
83
|
|
|
* @var array
|
84
|
|
|
*/
|
85
|
|
|
protected $after_put_function = array();
|
86
|
|
|
|
87
|
|
|
/**
|
88
|
|
|
* Le constructeur
|
89
|
|
|
*
|
90
|
|
|
* @return void
|
91
|
|
|
*
|
92
|
|
|
* @since 1.0.0.0
|
93
|
|
|
* @version 1.3.0.0
|
94
|
|
|
*/
|
95
|
|
|
protected function construct() {}
|
96
|
|
|
|
97
|
|
|
/**
|
98
|
|
|
* Permet de récupérer le schéma avec les données du modèle par défault.
|
99
|
|
|
*
|
100
|
|
|
* @since 1.0.0.0
|
101
|
|
|
* @version 1.3.0.0
|
102
|
|
|
*
|
103
|
|
|
* @return Object
|
104
|
|
|
*/
|
105
|
|
|
public function get_schema() {
|
106
|
|
|
$model_name = $this->model_name;
|
107
|
|
|
$model = new $model_name( array(), array() );
|
108
|
|
|
return $model->get_model();
|
109
|
|
|
}
|
110
|
|
|
|
111
|
|
|
/**
|
112
|
|
|
* Récupères les données selon le modèle définis.
|
113
|
|
|
*
|
114
|
|
|
* @since 1.0.0.0
|
115
|
|
|
* @version 1.3.0.0
|
116
|
|
|
*
|
117
|
|
|
* @param array $args Les paramètres de get_terms @https://codex.wordpress.org/Function_Reference/get_terms.
|
118
|
|
|
* @param boolean $single Si on veut récupérer un tableau, ou qu'une seule entrée.
|
119
|
|
|
*
|
120
|
|
|
* @return Object
|
121
|
|
|
*/
|
122
|
|
|
public function get( $args = array(), $single = false ) {
|
123
|
|
|
$list_term = array();
|
124
|
|
|
$array_term = array();
|
125
|
|
|
|
126
|
|
|
$model_name = $this->model_name;
|
127
|
|
|
|
128
|
|
|
$term_final_args = array_merge( $args, array(
|
129
|
|
|
'hide_empty' => false,
|
130
|
|
|
) );
|
131
|
|
|
|
132
|
|
|
if ( ! empty( $args['id'] ) ) {
|
133
|
|
|
$array_term[] = get_term_by( 'id', $args['id'], $this->taxonomy, ARRAY_A );
|
134
|
|
|
} elseif ( ! empty( $args['post_id'] ) ) {
|
135
|
|
|
$array_term = wp_get_post_terms( $args['post_id'], $this->taxonomy, $term_final_args );
|
136
|
|
|
|
137
|
|
|
if ( empty( $array_term ) ) {
|
138
|
|
|
$array_term[] = array();
|
139
|
|
|
}
|
140
|
|
|
} elseif ( isset( $args['schema'] ) ) {
|
141
|
|
|
$array_term[] = array();
|
142
|
|
|
} else {
|
143
|
|
|
$array_term = get_terms( $this->taxonomy, $term_final_args );
|
144
|
|
|
}
|
145
|
|
|
|
146
|
|
View Code Duplication |
if ( ! empty( $array_term ) ) {
|
|
|
|
|
147
|
|
|
foreach ( $array_term as $key => $term ) {
|
148
|
|
|
$term = (array) $term;
|
149
|
|
|
|
150
|
|
|
if ( ! empty( $args['post_id'] ) ) {
|
151
|
|
|
$term['post_id'] = $args['post_id'];
|
152
|
|
|
}
|
153
|
|
|
|
154
|
|
|
if ( ! empty( $term['term_id'] ) ) {
|
155
|
|
|
$list_meta = get_term_meta( $term['term_id'] );
|
156
|
|
|
foreach ( $list_meta as &$meta ) {
|
157
|
|
|
$meta = array_shift( $meta );
|
158
|
|
|
}
|
159
|
|
|
|
160
|
|
|
$term = array_merge( $term, $list_meta );
|
161
|
|
|
|
162
|
|
|
if ( ! empty( $term[ $this->meta_key ] ) ) {
|
163
|
|
|
$term = array_merge( $term, json_decode( $term[ $this->meta_key ], true ) );
|
164
|
|
|
unset( $term[ $this->meta_key ] );
|
165
|
|
|
}
|
166
|
|
|
}
|
167
|
|
|
|
168
|
|
|
$list_term[ $key ] = new $model_name( $term );
|
169
|
|
|
|
170
|
|
|
$list_term[ $key ] = Model_Util::exec_callback( $list_term[ $key ], $this->after_get_function );
|
171
|
|
|
}
|
172
|
|
|
}
|
173
|
|
|
|
174
|
|
|
if ( true === $single && 1 === count( $list_term ) ) {
|
175
|
|
|
$list_term = $list_term[0];
|
176
|
|
|
}
|
177
|
|
|
|
178
|
|
|
return $list_term;
|
179
|
|
|
}
|
180
|
|
|
|
181
|
|
|
/**
|
182
|
|
|
* Appelle la méthode update.
|
183
|
|
|
*
|
184
|
|
|
* @since 1.0.0.0
|
185
|
|
|
* @version 1.3.0.0
|
186
|
|
|
*
|
187
|
|
|
* @param Array $data Les données.
|
188
|
|
|
* @return Array $data Les données
|
189
|
|
|
*/
|
190
|
|
|
public function create( $data ) {
|
191
|
|
|
return $this->update( $data );
|
192
|
|
|
}
|
193
|
|
|
|
194
|
|
|
/**
|
195
|
|
|
* Insère ou met à jour les données dans la base de donnée.
|
196
|
|
|
*
|
197
|
|
|
* @since 1.0.0.0
|
198
|
|
|
* @version 1.3.0.0
|
199
|
|
|
*
|
200
|
|
|
* @param Array $data Les données a insérer ou à mêttre à jour.
|
201
|
|
|
* @return Object L'objet construit grâce au modèle.
|
202
|
|
|
*/
|
203
|
|
|
public function update( $data ) {
|
204
|
|
|
$model_name = $this->model_name;
|
205
|
|
|
$object = new $model_name( (array) $data );
|
206
|
|
|
|
207
|
|
|
/** Sauvegarde des données dans la base de données / Save data into database */
|
208
|
|
|
if ( empty( $object->id ) ) {
|
209
|
|
|
$object = Model_Util::exec_callback( $object, $this->before_post_function );
|
210
|
|
|
$wp_category_danger = wp_insert_term( $object->name, $this->get_taxonomy(), array(
|
211
|
|
|
'description' => ! empty( $object->description ) ? $object->description : '',
|
212
|
|
|
'slug' => ! empty( $object->slug ) ? $object->slug : sanitize_title( $object->name ),
|
213
|
|
|
'parent' => ! empty( $object->parent_id ) ? (int) $object->parent_id : 0,
|
214
|
|
|
) );
|
215
|
|
|
$object = Model_Util::exec_callback( $object, $this->after_post_function );
|
216
|
|
|
} else {
|
217
|
|
|
$object = Model_Util::exec_callback( $object, $this->before_put_function );
|
218
|
|
|
$wp_category_danger = wp_update_term( $object->id, $this->get_taxonomy(), $object->do_wp_object() );
|
219
|
|
|
$object = Model_Util::exec_callback( $object, $this->after_put_function );
|
220
|
|
|
}
|
221
|
|
|
|
222
|
|
|
if ( ! is_wp_error( $wp_category_danger ) ) {
|
223
|
|
|
$object->id = $wp_category_danger['term_id'];
|
224
|
|
|
$object->term_taxonomy_id = $wp_category_danger['term_taxonomy_id'];
|
225
|
|
|
|
226
|
|
|
save_meta_class::g()->save_meta_data( $object, 'update_term_meta', $this->meta_key );
|
|
|
|
|
227
|
|
|
} else {
|
228
|
|
|
if ( ! empty( $wp_category_danger->error_data['term_exists'] ) && is_int( $wp_category_danger->error_data['term_exists'] ) ) {
|
229
|
|
|
$list_term_model = $this->get( array(
|
230
|
|
|
'id' => $wp_category_danger->error_data['term_exists'],
|
231
|
|
|
) );
|
232
|
|
|
return $list_term_model[0];
|
233
|
|
|
} else {
|
234
|
|
|
return array();
|
|
|
|
|
235
|
|
|
}
|
236
|
|
|
}
|
237
|
|
|
|
238
|
|
|
return $object;
|
239
|
|
|
}
|
240
|
|
|
|
241
|
|
|
/**
|
242
|
|
|
* Supprime un term
|
243
|
|
|
*
|
244
|
|
|
* @todo: Inutile ?
|
245
|
|
|
*
|
246
|
|
|
* @since 1.0.0.0
|
247
|
|
|
* @version 1.3.0.0
|
248
|
|
|
*
|
249
|
|
|
* @param int $id L'ID du term (term_id).
|
250
|
|
|
*/
|
251
|
|
|
public function delete( $id ) {
|
252
|
|
|
wp_delete_term( $id );
|
253
|
|
|
}
|
254
|
|
|
|
255
|
|
|
/**
|
256
|
|
|
* Récupères la taxonomie
|
257
|
|
|
*
|
258
|
|
|
* @since 1.0.0.0
|
259
|
|
|
* @version 1.3.6.0
|
260
|
|
|
*
|
261
|
|
|
* @return string Le nom de la taxonomie
|
262
|
|
|
*/
|
263
|
|
|
public function get_taxonomy() {
|
264
|
|
|
return $this->taxonomy;
|
265
|
|
|
}
|
266
|
|
|
|
267
|
|
|
/**
|
268
|
|
|
* Retourne la taxonomie
|
269
|
|
|
*
|
270
|
|
|
* @since 1.0.0.0
|
271
|
|
|
* @version 1.3.0.0
|
272
|
|
|
*
|
273
|
|
|
* @return string Le post type
|
274
|
|
|
*
|
275
|
|
|
* @todo: Doublon
|
276
|
|
|
*/
|
277
|
|
|
public function get_post_type() {
|
278
|
|
|
return $this->get_taxonomy();
|
279
|
|
|
}
|
280
|
|
|
|
281
|
|
|
/**
|
282
|
|
|
* Utile uniquement pour DigiRisk.
|
283
|
|
|
*
|
284
|
|
|
* @since 1.0.0.0
|
285
|
|
|
* @version 1.3.0.0
|
286
|
|
|
*
|
287
|
|
|
* @return string L'identifiant des commentaires pour DigiRisk.
|
288
|
|
|
*/
|
289
|
|
|
public function get_identifier_helper() {
|
290
|
|
|
return $this->identifier_helper;
|
291
|
|
|
}
|
292
|
|
|
}
|
293
|
|
|
} // End if().
|
|
|
|
|
294
|
|
|
|
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.