1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Gestion des posts (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' ) ) { |
16
|
|
|
exit; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
if ( ! class_exists( '\eoxia\Post_Class' ) ) { |
20
|
|
|
/** |
21
|
|
|
* Gestion des posts (POST, PUT, GET, DELETE) |
22
|
|
|
*/ |
23
|
|
|
class Post_Class extends Singleton_Util { |
24
|
|
|
/** |
25
|
|
|
* Le nom du modèle |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $model_name = 'post_model'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Le type du post |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $post_type = 'post'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* La clé principale pour post_meta |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $meta_key = '_wpeo_post'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Le nom pour le resgister post type |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $post_type_name = 'posts'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Utiles pour récupérer la clé unique |
54
|
|
|
* |
55
|
|
|
* @todo Rien à faire ici |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $identifier_helper = 'post'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Fonction de callback après avoir récupérer le modèle en mode GET. |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $after_get_function = array(); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Fonction de callback avant d'insérer les données en mode POST. |
69
|
|
|
* |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
protected $before_post_function = array(); |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Fonction de callback avant de dispacher les données en mode POST. |
76
|
|
|
* |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
protected $before_model_post_function = array(); |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Fonction de callback après avoir inséré les données en mode POST. |
83
|
|
|
* |
84
|
|
|
* @var array |
85
|
|
|
*/ |
86
|
|
|
protected $after_post_function = array(); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Fonction de callback avant de mêttre à jour les données en mode PUT. |
90
|
|
|
* |
91
|
|
|
* @var array |
92
|
|
|
*/ |
93
|
|
|
protected $before_put_function = array(); |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Fonction de callback avant de dispatcher les données en mode PUT. |
97
|
|
|
* |
98
|
|
|
* @var array |
99
|
|
|
*/ |
100
|
|
|
protected $before_model_put_function = array(); |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Fonction de callback après avoir mis à jour les données en mode PUT. |
104
|
|
|
* |
105
|
|
|
* @var array |
106
|
|
|
*/ |
107
|
|
|
protected $after_put_function = array(); |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Appelle l'action "init" de WordPress |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
protected function construct() { |
115
|
|
|
add_action( 'init', array( $this, 'init_post_type' ) ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Initialise le post type selon $name et $name_singular. |
120
|
|
|
* Initialise la taxonomy si elle existe. |
121
|
|
|
* |
122
|
|
|
* @see register_post_type |
123
|
|
|
* @return boolean |
124
|
|
|
* |
125
|
|
|
* @since 1.0.0.0 |
126
|
|
|
* @version 1.3.0.0 |
127
|
|
|
*/ |
128
|
|
|
public function init_post_type() { |
129
|
|
|
$args = array( |
130
|
|
|
// 'public' => Config_Util::$init['digirisk']->debug ? true : false, |
|
|
|
|
131
|
|
|
'label' => $this->post_type_name, |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
$return = register_post_type( $this->post_type, $args ); |
135
|
|
|
Log_Class::g()->exec( 'digi_post_type', '', 'Enregistres le post personnalisé : ' . $this->post_type, $args ); |
|
|
|
|
136
|
|
|
|
137
|
|
|
if ( ! empty( $this->attached_taxonomy_type ) ) { |
138
|
|
|
register_taxonomy( $this->attached_taxonomy_type, $this->post_type ); |
|
|
|
|
139
|
|
|
Log_Class::g()->exec( 'digi_taxonomy', '', 'Enregistres la taxonomie : ' . $this->attached_taxonomy_type ); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $return; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Permet de récupérer le schéma avec les données du modèle par défault. |
147
|
|
|
* |
148
|
|
|
* @since 1.0.0.0 |
149
|
|
|
* @version 1.3.0.0 |
150
|
|
|
* |
151
|
|
|
* @return Object |
152
|
|
|
*/ |
153
|
|
|
public function get_schema() { |
154
|
|
|
$model_name = $this->model_name; |
155
|
|
|
$model = new $model_name( array() ); |
156
|
|
|
return $model->get_model(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Récupères les données selon le modèle définis. |
161
|
|
|
* |
162
|
|
|
* @since 1.0.0.0 |
163
|
|
|
* @version 1.3.0.0 |
164
|
|
|
* |
165
|
|
|
* @param array $args Les paramètres de get_comments @https://codex.wordpress.org/Function_Reference/WP_Query. |
166
|
|
|
* @param boolean $single Si on veut récupérer un tableau, ou qu'une seule entrée. |
167
|
|
|
* |
168
|
|
|
* @return Object |
169
|
|
|
* |
170
|
|
|
* @todo: ligne 128 - Temporaire |
171
|
|
|
*/ |
172
|
|
|
public function get( $args = array( |
173
|
|
|
'posts_per_page' => -1, |
174
|
|
|
), $single = false ) { |
175
|
|
|
|
176
|
|
|
$array_posts = array(); |
177
|
|
|
$args['post_type'] = $this->post_type; |
178
|
|
|
|
179
|
|
|
if ( ! empty( $args['include'] ) ) { |
180
|
|
|
$args['post__in'] = $args['include']; |
181
|
|
|
if ( ! is_array( $args['post__in'] ) ) { |
182
|
|
|
$args['post__in'] = (array) $args['post__in']; |
183
|
|
|
} |
184
|
|
|
unset( $args['include'] ); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if ( ! isset( $args['posts_per_page'] ) ) { |
188
|
|
|
$args['posts_per_page'] = -1; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if ( isset( $args['id'] ) ) { |
192
|
|
|
$array_posts[] = get_post( $args['id'], ARRAY_A ); |
193
|
|
|
unset( $args['id'] ); |
194
|
|
|
} elseif ( isset( $args['schema'] ) ) { |
195
|
|
|
$array_posts[] = array(); |
196
|
|
|
} else { |
197
|
|
|
$query_posts = new \WP_Query( $args ); |
198
|
|
|
$array_posts = $query_posts->posts; |
199
|
|
|
unset( $query_posts->posts ); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
foreach ( $array_posts as $key => $post ) { |
203
|
|
|
$post = (array) $post; |
204
|
|
|
|
205
|
|
|
// Si post['ID'] existe, on récupère les meta. |
206
|
|
|
if ( ! empty( $post['ID'] ) ) { |
207
|
|
|
$list_meta = get_post_meta( $post['ID'] ); |
208
|
|
|
foreach ( $list_meta as &$meta ) { |
209
|
|
|
$meta = array_shift( $meta ); |
210
|
|
|
$meta = JSON_Util::g()->decode( $meta ); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$post = array_merge( $post, $list_meta ); |
214
|
|
|
|
215
|
|
|
if ( ! empty( $post[ $this->meta_key ] ) ) { |
216
|
|
|
$data_json = JSON_Util::g()->decode( $post[ $this->meta_key ] ); |
|
|
|
|
217
|
|
|
if ( is_array( $data_json ) ) { |
218
|
|
|
$post = array_merge( $post, $data_json ); |
219
|
|
|
} else { |
220
|
|
|
$post[ $this->meta_key ] = $data_json; |
221
|
|
|
} |
222
|
|
|
unset( $post[ $this->meta_key ] ); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$model_name = $this->model_name; |
227
|
|
|
$array_posts[ $key ] = new $model_name( $post ); |
228
|
|
|
$array_posts[ $key ] = $this->get_taxonomies_id( $array_posts[ $key ] ); |
229
|
|
|
|
230
|
|
|
$array_posts[ $key ] = Model_Util::exec_callback( $array_posts[ $key ], $this->after_get_function ); |
231
|
|
|
} // End foreach(). |
|
|
|
|
232
|
|
|
|
233
|
|
|
if ( true === $single && 1 === count( $array_posts ) ) { |
234
|
|
|
$array_posts = $array_posts[0]; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $array_posts; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Appelle la méthode update. |
242
|
|
|
* |
243
|
|
|
* @since 1.0.0.0 |
244
|
|
|
* @version 1.3.0.0 |
245
|
|
|
* |
246
|
|
|
* @param Array $data Les données. |
247
|
|
|
* @return Array $data Les données |
248
|
|
|
*/ |
249
|
|
|
public function create( $data ) { |
250
|
|
|
return $this->update( $data ); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Insère ou met à jour les données dans la base de donnée. |
255
|
|
|
* |
256
|
|
|
* @since 1.0.0.0 |
257
|
|
|
* @version 1.3.0.0 |
258
|
|
|
* |
259
|
|
|
* @param Array $data Les données a insérer ou à mêttre à jour. |
260
|
|
|
* @return Object L'objet construit grâce au modèle. |
261
|
|
|
*/ |
262
|
|
|
public function update( $data ) { |
263
|
|
|
$model_name = $this->model_name; |
264
|
|
|
$data = (array) $data; |
265
|
|
|
|
266
|
|
|
if ( empty( $data['id'] ) ) { |
267
|
|
|
$data = Model_Util::exec_callback( $data, $this->before_model_post_function ); |
268
|
|
|
|
269
|
|
|
$data = new $model_name( $data ); |
270
|
|
|
|
271
|
|
|
// Ajout du post type si il est vide ! |
272
|
|
|
if ( empty( $data->type ) ) { |
273
|
|
|
$data->type = $this->post_type; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$data = Model_Util::exec_callback( $data, $this->before_post_function ); |
277
|
|
|
|
278
|
|
|
if ( ! empty( $data->error ) && $data->error ) { |
279
|
|
|
return false; |
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$post_save = wp_insert_post( $data->do_wp_object(), true ); |
283
|
|
|
|
284
|
|
|
if ( ! is_wp_error( $post_save ) ) { |
285
|
|
|
$data->id = $post_save; |
286
|
|
|
} else { |
287
|
|
|
$data = $post_save; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
if ( ! is_wp_error( $data ) ) { |
291
|
|
|
Save_Meta_Class::g()->save_meta_data( $data, 'update_post_meta', $this->meta_key ); |
|
|
|
|
292
|
|
|
// Save taxonomy! |
293
|
|
|
$this->save_taxonomies( $data ); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
$data = Model_Util::exec_callback( $data, $this->after_post_function ); |
297
|
|
|
} else { |
298
|
|
|
$data = Model_Util::exec_callback( $data, $this->before_model_put_function ); |
299
|
|
|
|
300
|
|
|
$current_data = $this->get( array( |
301
|
|
|
'id' => $data['id'], |
302
|
|
|
), true ); |
303
|
|
|
$obj_merged = (object) array_merge( (array) $current_data, (array) $data ); |
304
|
|
|
$data = new $model_name( (array) $obj_merged ); |
305
|
|
|
$data->type = $this->post_type; |
306
|
|
|
|
307
|
|
|
$data = Model_Util::exec_callback( $data, $this->before_put_function ); |
308
|
|
|
|
309
|
|
|
if ( ! empty( $data->error ) && $data->error ) { |
310
|
|
|
return false; |
|
|
|
|
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
$post_save = wp_update_post( $data->do_wp_object(), true ); |
314
|
|
|
if ( is_wp_error( $post_save ) ) { |
315
|
|
|
$data = $post_save; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
if ( ! is_wp_error( $data ) ) { |
319
|
|
|
Save_Meta_Class::g()->save_meta_data( $data, 'update_post_meta', $this->meta_key ); |
|
|
|
|
320
|
|
|
// Save taxonomy! |
321
|
|
|
$this->save_taxonomies( $data ); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
$data = Model_Util::exec_callback( $data, $this->after_put_function ); |
325
|
|
|
} // End if(). |
|
|
|
|
326
|
|
|
|
327
|
|
|
|
328
|
|
|
|
329
|
|
|
return $data; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Recherche dans les meta value. |
334
|
|
|
* |
335
|
|
|
* @since 1.0.0.0 |
336
|
|
|
* @version 1.3.0.0 |
337
|
|
|
* |
338
|
|
|
* @param string $search Le terme de la recherche. |
339
|
|
|
* @param array $array La définition de la recherche. |
340
|
|
|
* |
341
|
|
|
* @return array |
342
|
|
|
*/ |
343
|
|
|
public function search( $search, $array ) { |
344
|
|
|
global $wpdb; |
345
|
|
|
|
346
|
|
|
if ( empty( $array ) || ! is_array( $array ) ) { |
347
|
|
|
return array(); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
$where = ' AND ( '; |
351
|
|
|
if ( ! empty( $array ) ) { |
352
|
|
|
foreach ( $array as $key => $element ) { |
353
|
|
|
if ( is_array( $element ) ) { |
354
|
|
|
foreach ( $element as $sub_element ) { |
355
|
|
|
$where .= ' AND ( ' === $where ? '' : ' OR '; |
356
|
|
|
$where .= ' (PM.meta_key="' . $sub_element . '" AND PM.meta_value LIKE "%' . $search . '%") '; |
357
|
|
|
} |
358
|
|
|
} else { |
359
|
|
|
$where .= ' AND ( ' === $where ? '' : ' OR '; |
360
|
|
|
$where .= ' P.' . $element . ' LIKE "%' . $search . '%" '; |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
$where .= ' ) '; |
366
|
|
|
|
367
|
|
|
$list_group = $wpdb->get_results( "SELECT DISTINCT P.ID FROM {$wpdb->posts} as P JOIN {$wpdb->postmeta} AS PM ON PM.post_id=P.ID WHERE P.post_type='" . $this->get_post_type() . "'" . $where ); |
368
|
|
|
$list_model = array(); |
369
|
|
|
if ( ! empty( $list_group ) ) { |
370
|
|
|
foreach ( $list_group as $element ) { |
371
|
|
|
$list_model[] = $this->get( array( |
372
|
|
|
'id' => $element->ID, |
373
|
|
|
) ); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
return $list_model; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Retourne le post type |
382
|
|
|
* |
383
|
|
|
* @since 1.0.0.0 |
384
|
|
|
* @version 1.3.0.0 |
385
|
|
|
* |
386
|
|
|
* @return string Le post type |
387
|
|
|
*/ |
388
|
|
|
public function get_post_type() { |
389
|
|
|
return $this->post_type; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* Utile uniquement pour DigiRisk. |
394
|
|
|
* |
395
|
|
|
* @since 1.0.0.0 |
396
|
|
|
* @version 1.3.0.0 |
397
|
|
|
* |
398
|
|
|
* @return string L'identifiant des commentaires pour DigiRisk. |
399
|
|
|
*/ |
400
|
|
|
public function get_identifier_helper() { |
401
|
|
|
return $this->identifier_helper; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Récupères les ID des taxonomies lié à ce post. |
406
|
|
|
* |
407
|
|
|
* @param object $data L'objet courant. |
408
|
|
|
* @return object L'objet avec les taxonomies. |
409
|
|
|
* |
410
|
|
|
* @since 1.0.0.0 |
411
|
|
|
* @version 1.3.0.0 |
412
|
|
|
*/ |
413
|
|
|
private function get_taxonomies_id( $data ) { |
414
|
|
|
$model = $data->get_model(); |
415
|
|
|
if ( ! empty( $model['taxonomy']['child'] ) ) { |
416
|
|
|
foreach ( $model['taxonomy']['child'] as $key => $value ) { |
417
|
|
|
$data->taxonomy[ $key ] = wp_get_object_terms( $data->id, $key, array( |
418
|
|
|
'fields' => 'ids', |
419
|
|
|
) ); |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
return $data; |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Sauvegardes les taxonomies |
428
|
|
|
* |
429
|
|
|
* @param object $data L'objet avec les taxonomies à sauvegarder. |
430
|
|
|
*/ |
431
|
|
|
private function save_taxonomies( $data ) { |
432
|
|
|
if ( ! empty( $data->taxonomy ) ) { |
433
|
|
|
foreach ( $data->taxonomy as $taxonomy_name => $taxonomy_data ) { |
434
|
|
|
if ( ! empty( $taxonomy_name ) ) { |
435
|
|
|
wp_set_object_terms( $data->id, $taxonomy_data, $taxonomy_name, true ); |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Permet de changer le modèle en dur. |
443
|
|
|
* |
444
|
|
|
* @param string $model_name Le nom du modèle. |
445
|
|
|
* |
446
|
|
|
* @since 1.0.0.0 |
447
|
|
|
* @version 1.3.6.0 |
448
|
|
|
*/ |
449
|
|
|
public function set_model( $model_name ) { |
450
|
|
|
$this->model_name = $model_name; |
451
|
|
|
} |
452
|
|
|
} |
453
|
|
|
} // End if(). |
|
|
|
|
454
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.