Completed
Branch 2.0.0 (814c19)
by Jimmy
03:05
created

Comment_Class::update()   F

Complexity

Conditions 14
Paths 1400

Size

Total Lines 80

Duplication

Lines 6
Ratio 7.5 %

Importance

Changes 0
Metric Value
cc 14
nc 1400
nop 1
dl 6
loc 80
rs 2.1163
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Gestion des commentaires (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\Comment_Class' ) ) {
19
	/**
20
	 * Gestion des commentaires (POST, PUT, GET, DELETE)
21
	 */
22
	class Comment_Class extends Object_Class {
23
		/**
24
		 * Le nom du modèle à utiliser.
25
		 *
26
		 * @var string
27
		 */
28
		protected $model_name = '\eoxia\Comment_Model';
29
30
		/**
31
		 * La clé principale pour enregistrer les meta données.
32
		 *
33
		 * @var string
34
		 */
35
		protected $meta_key = '_comment';
36
37
		/**
38
		 * Le type du commentaire
39
		 *
40
		 * @var string
41
		 */
42
		protected $type = 'ping';
43
44
		/**
45
		 * Slug de base pour la route dans l'api rest
46
		 *
47
		 * @var string
48
		 */
49
		protected $base = 'comment';
50
51
		/**
52
		 * Uniquement utile pour DigiRisk...
53
		 *
54
		 * @var string
55
		 */
56
		protected $identifier_helper = 'comment';
57
58
		/**
59
		 * La liste des droits a avoir pour accèder aux différentes méthodes
60
		 *
61
		 * @var array
62
		 */
63
		protected $capabilities = array(
64
			'get'    => 'read',
65
			'put'    => 'moderate_comments',
66
			'post'   => 'moderate_comments',
67
			'delete' => 'moderate_comments',
68
		);
69
70
		/**
71
		 * Initialise pre_get_comments
72
		 *
73
		 * @since 1.0.0
74
		 * @version 1.0.0
75
		 *
76
		 * @return void
77
		 */
78
		protected function construct() {
79
			parent::construct();
80
81
			if ( ! in_array( $this->get_type(), \eoxia\Config_Util::$init['eo-framework']->not__in_display_comment ) ) {
82
				add_action( 'pre_get_comments', array( $this, 'callback_pre_get_comments' ) );
83
			}
84
		}
85
86
		/**
87
		 * N'affiches pas les commentaires dans la liste des commentaires.
88
		 *
89
		 * @since 1.0.0
90
		 * @version 1.0.0
91
		 *
92
		 * @param  WP_Comment_Query $query Query args.
93
		 *
94
		 * @return void
95
		 */
96
		public function callback_pre_get_comments( $query ) {
97
			global $pagenow;
98
99
			if ( $query->query_vars['type'] !== $this->get_type() && 'edit-comments.php' === $pagenow ) {
100
				$query->query_vars['type__not_in'] = array_merge( (array) $query->query_vars['type__not_in'], array( $this->get_type() ) );
101
			}
102
		}
103
104
		/**
105
		 * Récupères les données selon le modèle définis.
106
		 *
107
		 * @since 0.1.0
108
		 * @version 1.0.0
109
		 *
110
		 * @param array   $args Les paramètres de get_comments @https://codex.wordpress.org/Function_Reference/get_comments.
111
		 * @param boolean $single Si on veut récupérer un tableau, ou qu'une seule entrée.
112
		 *
113
		 * @return Comment_Model
114
		 */
115
		public function get( $args = array(), $single = false ) {
116
			$array_comments = array();
117
118
			$default_args = array(
119
				'type' => $this->get_type(),
120
			);
121
122
			// Si le paramètre "id" est passé on le transforme en "ID" qui est le paramètre attendu par get_comments.
123
			// Dans un souci d'homogénéité du code, le paramètre "id" remplace "ID".
124
			$args['id'] = ! empty( $args['comment_ID'] ) ? $args['comment_ID'] : ( isset( $args['id'] ) ? $args['id'] : null );
125
			if ( ! empty( $args['id'] ) ) {
126
				if ( isset( $args['comment_ID'] ) ) {
127
					unset( $args['comment_ID'] );
128
				}
129
				if ( ! isset( $args['comment__in'] ) ) {
130
					$args['comment__in'] = array();
131
				}
132
				$args['comment__in'] = array_merge( (array) $args['id'], $args['comment__in'] );
133
			} elseif ( isset( $args['id'] ) ) {
134
				$args['schema'] = true;
135
			}
136
			unset( $args['id'] );
137
138
			$args_cb    = array(
139
				'args'         => $args,
140
				'default_args' => $default_args,
141
			);
142
			$final_args = apply_filters( 'eo_model_comment_before_get', wp_parse_args( $args, $default_args ), $args_cb );
143
			// Il ne faut pas lancer plusieurs fois pour ping.
144
			if ( 'ping' !== $this->get_type() ) {
145
				$final_args = apply_filters( 'eo_model_' . $this->get_type() . '_before_get', $final_args, $args_cb );
146
			}
147
148
			// 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.
149
			if ( isset( $args['schema'] ) ) {
150
				$array_comments[] = $final_args;
151
			} else { // On lance la requête pour récupèrer les "comments" demandés.
152
				$array_comments = get_comments( $final_args );
153
			}
154
155
			// Traitement de la liste des résultats pour le retour.
156
			$array_comments = $this->prepare_items_for_response( $array_comments, 'comment', $this->meta_key, 'comment_ID' );
157
158
			// 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.
159
			if ( true === $single && 1 === count( $array_comments ) ) {
160
				$array_comments = $array_comments[0];
161
			}
162
163
			return $array_comments;
164
		}
165
166
		/**
167
		 * Insère ou met à jour les données dans la base de données.
168
		 *
169
		 * @since 0.1.0
170
		 * @version 1.0.0
171
		 *
172
		 * @param  Array $data Les données a insérer ou à mettre à jour.
173
		 */
174
		public function update( $data ) {
175
			$model_name = $this->model_name;
176
			$data       = (array) $data;
177
			$req_method = ( ! empty( $data['id'] ) ) ? 'put' : 'post';
178
			$args_cb    = array(
179
				'model_name' => $model_name,
180
				'req_method' => $req_method,
181
				'meta_key'   => $this->meta_key,
182
			);
183
184
			// Vérifie l'existence du type.
185
			if ( empty( $data['type'] ) ) {
186
				$data['type'] = $this->get_type();
187
			}
188
189
			if ( empty( $data['id'] ) ) {
190
191
				if ( ! empty( $data['author_id'] ) ) {
192
					$user = get_userdata( $data['author_id'] );
193
				} else {
194
					$user = wp_get_current_user();
195
				}
196
197
				if ( $user->exists() ) {
198
					if ( empty( $data['author_id'] ) ) {
199
						$data['author_id'] = $user->ID;
200
					}
201
202
					if ( empty( $data['author_nicename'] ) ) {
203
						$data['author_nicename'] = $user->display_name;
204
					}
205
206
					if ( empty( $data['author_email'] ) ) {
207
						$data['author_email'] = $user->user_email;
208
					}
209
210
					if ( empty( $data['author_url'] ) ) {
211
						$data['author_url'] = $user->user_url;
212
					}
213
				}
214
			}
215
216
			$data = apply_filters( 'eo_model_comment_before_' . $req_method, $data, $args_cb );
217
			// Il ne faut pas lancer plusieurs fois pour ping.
218
			if ( 'ping' !== $this->get_type() ) {
219
				$data = apply_filters( 'eo_model_' . $this->get_type() . '_before_' . $req_method, $data, $args_cb );
220
			}
221
			$args_cb['data'] = $data;
222
223
			$object = new $model_name( $data, $req_method );
224
225
			if ( empty( $object->data['id'] ) ) {
226
				add_filter( 'duplicate_comment_id', '__return_false' );
227
				add_filter( 'pre_comment_approved', function( $approved, $comment_data ) {
228
					return $comment_data['comment_approved'];
229
				}, 10, 2 );
230
				$inserted_comment = wp_insert_comment( $object->convert_to_wordpress() );
231
				if ( is_wp_error( $inserted_comment ) ) {
232
					return $inserted_comment;
233
				}
234
235
				$object->data['id'] = $inserted_comment;
236
			} else {
237
				wp_update_comment( $object->convert_to_wordpress() );
238
			}
239
240
			$object = apply_filters( 'eo_model_comment_after_' . $req_method, $object, $args_cb );
241
242
			$object = $this->get( array(
243
				'id'     => $object->data['id'],
244
				'status' => array( '1', 'trash' ),
245
			), true );
246
247
			// Il ne faut pas lancer plusieurs fois pour ping.
248
			if ( 'ping' !== $this->get_type() ) {
249
				$object = apply_filters( 'eo_model_' . $this->get_type() . '_after_' . $req_method, $object, $args_cb );
250
			}
251
252
			return $object;
253
		}
254
255
	}
256
} // End if().
257