1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Gestion des posts (POST, PUT, GET, DELETE) |
4
|
|
|
* |
5
|
|
|
* @author Eoxia <[email protected]> |
6
|
|
|
* @since 0.1.0 |
7
|
|
|
* @version 1.0.0 |
8
|
|
|
* @copyright 2015-2018 |
9
|
|
|
* @package EO_Framework\EO_Model\Class |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace eoxia; |
13
|
|
|
|
14
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
15
|
|
|
exit; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
if ( ! class_exists( '\eoxia\Post_Class' ) ) { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Gestion des posts (POST, PUT, GET, DELETE) |
22
|
|
|
*/ |
23
|
|
|
class Post_Class extends Object_Class { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Le nom du modèle |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $model_name = '\eoxia\Post_Model'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Le type du post |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $type = 'post'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Le type du post |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $base = 'post'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* La clé principale pour post_meta |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $meta_key = '_wpeo_post'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Le nom pour le resgister post type |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $post_type_name = 'posts'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Utiles pour récupérer la clé unique |
62
|
|
|
* |
63
|
|
|
* @todo Rien à faire ici |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected $identifier_helper = 'post'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* La liste des droits a avoir pour accèder aux différentes méthodes |
70
|
|
|
* |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
protected $capabilities = array( |
74
|
|
|
'get' => 'read', |
75
|
|
|
'put' => 'edit_posts', |
76
|
|
|
'post' => 'edit_posts', |
77
|
|
|
'delete' => 'delete_posts', |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Appelle l'action "init" de WordPress |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
protected function construct() { |
86
|
|
|
parent::construct(); |
87
|
|
|
|
88
|
|
|
add_action( 'init', array( $this, 'init_post_type' ) ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Initialise le post type selon $name et $name_singular. |
93
|
|
|
* Initialise la taxonomy si elle existe. |
94
|
|
|
* |
95
|
|
|
* @since 1.0.0 |
96
|
|
|
* @version 1.0.0 |
97
|
|
|
* |
98
|
|
|
* @see register_post_type |
99
|
|
|
* @return boolean |
100
|
|
|
*/ |
101
|
|
|
public function init_post_type() { |
102
|
|
|
$args = apply_filters( 'eo_model_' . $this->get_type() . '_register_post_type_args', array( |
103
|
|
|
'label' => $this->post_type_name, |
104
|
|
|
) ); |
105
|
|
|
|
106
|
|
|
$return = register_post_type( $this->get_type(), $args ); |
107
|
|
|
|
108
|
|
|
if ( ! empty( $this->attached_taxonomy_type ) ) { |
109
|
|
|
register_taxonomy( $this->attached_taxonomy_type, $this->get_type(), apply_filters( 'eo_model_' . $this->get_type() . '_' . $this->attached_taxonomy_type, array() ) ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Récupères les données selon le modèle défini. |
117
|
|
|
* |
118
|
|
|
* @since 1.0.0 |
119
|
|
|
* @version 1.0.0 |
120
|
|
|
* |
121
|
|
|
* @param array $args Les paramètres à appliquer pour la récupération @see https://codex.wordpress.org/Function_Reference/WP_Query. |
122
|
|
|
* @param boolean $single Si on veut récupérer un tableau, ou qu'une seule entrée. |
123
|
|
|
* |
124
|
|
|
* @return Object |
125
|
|
|
*/ |
126
|
|
|
public function get( $args = array(), $single = false ) { |
127
|
|
|
$array_posts = array(); |
128
|
|
|
|
129
|
|
|
// Définition des arguments par défaut pour la récupération des "posts". |
130
|
|
|
$default_args = array( |
131
|
|
|
'post_status' => 'any', |
132
|
|
|
'post_type' => $this->get_type(), |
133
|
|
|
'posts_per_page' => -1, |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
// Si le paramètre "id" est passé on le transforme en "post__in" pour eviter les problèmes de statuts. |
137
|
|
|
// Dans un soucis d'homogénéité du code, le paramètre "id" remplace le paramètre "p" qui est de base dans WP_Query. |
138
|
|
|
$args['id'] = ! empty( $args['ID'] ) ? $args['ID'] : ( isset( $args['id'] ) ? $args['id'] : null ); |
139
|
|
|
if ( ! empty( $args['id'] ) ) { |
140
|
|
|
if ( isset( $args['ID'] ) ) { |
141
|
|
|
unset( $args['ID'] ); |
142
|
|
|
} |
143
|
|
|
if ( ! isset( $args['post__in'] ) ) { |
144
|
|
|
$args['post__in'] = array(); |
145
|
|
|
} |
146
|
|
|
$args['post__in'] = array_merge( (array) $args['id'], $args['post__in'] ); |
147
|
|
|
} elseif ( isset( $args['id'] ) ) { |
148
|
|
|
$args['schema'] = true; |
149
|
|
|
} |
150
|
|
|
unset( $args['id'] ); |
151
|
|
|
|
152
|
|
|
$args_cb = array( |
153
|
|
|
'args' => $args, |
154
|
|
|
'default_args' => $default_args, |
155
|
|
|
); |
156
|
|
|
$final_args = apply_filters( 'eo_model_post_before_get', wp_parse_args( $args, $default_args ), $args_cb ); |
157
|
|
|
// Il ne faut pas lancer plusieurs fois pour post. |
158
|
|
|
if ( 'post' !== $this->get_type() ) { |
159
|
|
|
$final_args = apply_filters( 'eo_model_' . $this->get_type() . '_before_get', $final_args, $args_cb ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// Si l'argument "schema" est présent c'est lui qui prend le dessus et ne va pas récupérer d'élément dans la base de données. |
163
|
|
|
if ( isset( $args['schema'] ) ) { |
164
|
|
|
$array_posts[] = $final_args; |
165
|
|
|
} else { // On lance la requête pour récupèrer les "posts" demandés. |
166
|
|
|
$query_posts = new \WP_Query( $final_args ); |
167
|
|
|
$array_posts = $query_posts->posts; |
168
|
|
|
unset( $query_posts->posts ); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// Traitement de la liste des résultats pour le retour. |
172
|
|
|
$array_posts = $this->prepare_items_for_response( $array_posts, 'post', $this->meta_key, 'ID' ); |
173
|
|
|
|
174
|
|
|
// Si on a demandé qu'une seule entrée et qu'il n'y a bien qu'une seule entrée correspondant à la demande alors on ne retourne que cette entrée. |
175
|
|
|
if ( true === $single && 1 === count( $array_posts ) ) { |
176
|
|
|
$array_posts = $array_posts[0]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $array_posts; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Insère ou met à jour les données dans la base de donnée. |
184
|
|
|
* |
185
|
|
|
* @since 0.1.0 |
186
|
|
|
* @version 1.0.0 |
187
|
|
|
* |
188
|
|
|
* @param Array $data Les données a insérer ou à mêttre à jour. |
189
|
|
|
* |
190
|
|
|
* @return Object L'objet construit grâce au modèle. |
191
|
|
|
*/ |
192
|
|
|
public function update( $data ) { |
193
|
|
|
$model_name = $this->model_name; |
194
|
|
|
$data = (array) $data; |
195
|
|
|
$req_method = ( ! empty( $data['id'] ) ) ? 'put' : 'post'; |
196
|
|
|
$args_cb = array( |
197
|
|
|
'model_name' => $model_name, |
198
|
|
|
'req_method' => $req_method, |
199
|
|
|
'meta_key' => $this->meta_key, |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
if ( empty( $data['type'] ) ) { |
203
|
|
|
$data['type'] = $this->get_type(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$append = false; |
207
|
|
|
if ( isset( $data['$push'] ) ) { |
208
|
|
|
if ( ! empty( $data['$push'] ) ) { |
209
|
|
|
foreach ( $data['$push'] as $field_name => $field_to_push ) { |
210
|
|
|
if ( ! empty( $field_to_push ) ) { |
211
|
|
|
foreach ( $field_to_push as $sub_field_name => $value ) { |
212
|
|
|
if ( ! isset( $data[ $field_name ][ $sub_field_name ] ) ) { |
213
|
|
|
$data[ $field_name ][ $sub_field_name ] = array(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$data[ $field_name ][ $sub_field_name ][] = $value; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$append = true; |
223
|
|
|
unset( $data['$push'] ); |
224
|
|
|
} |
225
|
|
|
$args_cb['append_taxonomies'] = $append; |
226
|
|
|
|
227
|
|
|
$data = apply_filters( 'eo_model_post_before_' . $req_method, $data, $args_cb ); |
228
|
|
|
|
229
|
|
|
// Il ne faut pas lancer plusieurs fois pour post. |
230
|
|
|
if ( 'post' !== $this->get_type() ) { |
231
|
|
|
$data = apply_filters( 'eo_model_' . $this->get_type() . '_before_' . $req_method, $data, $args_cb ); |
232
|
|
|
} |
233
|
|
|
$args_cb['data'] = $data; |
234
|
|
|
|
235
|
|
|
$object = new $model_name( $data, $req_method ); |
236
|
|
|
|
237
|
|
|
if ( empty( $object->data['id'] ) ) { |
238
|
|
|
$post_save_result = wp_insert_post( $object->convert_to_wordpress(), true ); |
239
|
|
|
|
240
|
|
|
$object->data['id'] = $post_save_result; |
241
|
|
|
} else { |
242
|
|
|
$post_save_result = wp_update_post( $object->convert_to_wordpress(), true ); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
// Une erreur est survenue à la sauvegarden on retourne l'erreur. |
246
|
|
|
if ( is_wp_error( $post_save_result ) ) { |
247
|
|
|
return $post_save_result; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
|
251
|
|
|
$object = apply_filters( 'eo_model_post_after_' . $req_method, $object, $args_cb ); |
252
|
|
|
$object = $this->get( array( |
253
|
|
|
'id' => $object->data['id'], |
254
|
|
|
'post_status' => array( 'any', 'trash' ), |
255
|
|
|
), true ); |
256
|
|
|
|
257
|
|
|
// Il ne faut pas lancer plusieurs fois pour post. |
258
|
|
|
if ( 'post' !== $this->get_type() ) { |
259
|
|
|
$object = apply_filters( 'eo_model_' . $this->get_type() . '_after_' . $req_method, $object, $args_cb ); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return $object; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Recherche dans les meta value. |
267
|
|
|
* |
268
|
|
|
* @since 1.0.0 |
269
|
|
|
* @version 1.0.0 |
270
|
|
|
* |
271
|
|
|
* @param string $search Le terme de la recherche. |
272
|
|
|
* @param array $array La définition de la recherche. |
273
|
|
|
* |
274
|
|
|
* @return array |
275
|
|
|
*/ |
276
|
|
|
public function search( $search, $array ) { |
277
|
|
|
global $wpdb; |
278
|
|
|
|
279
|
|
|
if ( empty( $array ) || ! is_array( $array ) ) { |
280
|
|
|
return array(); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
$where = ' AND ( '; |
284
|
|
|
|
285
|
|
|
if ( ! empty( $array ) ) { |
286
|
|
|
foreach ( $array as $key => $element ) { |
287
|
|
|
if ( is_array( $element ) ) { |
288
|
|
|
foreach ( $element as $sub_element ) { |
289
|
|
|
$where .= ' AND ( ' === $where ? '' : ' OR '; |
290
|
|
|
$where .= ' (PM.meta_key="' . $sub_element . '" AND PM.meta_value LIKE "%' . $search . '%") '; |
291
|
|
|
} |
292
|
|
|
} else { |
293
|
|
|
$where .= ' AND ( ' === $where ? '' : ' OR '; |
294
|
|
|
$where .= ' P.' . $element . ' LIKE "%' . $search . '%" '; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
$where .= ' ) '; |
300
|
|
|
|
301
|
|
|
$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_type() . "'" . $where ); |
302
|
|
|
$list_model = array(); |
303
|
|
|
if ( ! empty( $list_group ) ) { |
304
|
|
|
foreach ( $list_group as $element ) { |
305
|
|
|
$list_model[] = $this->get( array( |
306
|
|
|
'id' => $element->ID, |
307
|
|
|
) ); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
return $list_model; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Retournes le nom de la catégorie attachée au post. |
316
|
|
|
* |
317
|
|
|
* @since 1.0.0 |
318
|
|
|
* @version 1.0.0 |
319
|
|
|
* |
320
|
|
|
* @return string Le nom de la catégorie. |
321
|
|
|
*/ |
322
|
|
|
public function get_attached_taxonomy() { |
323
|
|
|
return $this->attached_taxonomy_type; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Retournes le nom du post type. |
328
|
|
|
* |
329
|
|
|
* @since 1.0.0 |
330
|
|
|
* @version 1.0.0 |
331
|
|
|
* |
332
|
|
|
* @return string Le nom du post type. |
333
|
|
|
*/ |
334
|
|
|
public function get_post_type_name() { |
335
|
|
|
return $this->post_type_name; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
} |
339
|
|
|
} // End if(). |
340
|
|
|
|