Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 23 | class Term_Class extends Object_Class {
|
||
| 24 | |||
| 25 | /** |
||
| 26 | * Le nom du modèle |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $model_name = 'term_model'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * La clé principale pour post_meta |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $meta_key = '_wpeo_term'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Le nom de la taxonomie |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $type = 'category'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Slug de base pour la route dans l'api rest |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $base = 'category'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Pour l'association de la taxonomy |
||
| 55 | * |
||
| 56 | * @var string|array |
||
| 57 | */ |
||
| 58 | protected $associate_post_types = array(); |
||
| 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 = 'term'; |
||
| 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' => 'manage_categories', |
||
| 76 | 'post' => 'manage_categories', |
||
| 77 | 'delete' => 'manage_categories', |
||
| 78 | ); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Le constructeur |
||
| 82 | * |
||
| 83 | * @return void |
||
| 84 | * |
||
| 85 | * @since 0.1.0 |
||
| 86 | * @version 1.0.0 |
||
| 87 | */ |
||
| 88 | protected function construct() {
|
||
| 89 | parent::construct(); |
||
| 90 | |||
| 91 | add_action( 'init', array( $this, 'callback_init' ) ); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Initialise la taxonomie |
||
| 96 | * |
||
| 97 | * @since 1.0.0 |
||
| 98 | * @version 1.0.0 |
||
| 99 | * |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | public function callback_init() {
|
||
| 103 | $args = array( |
||
| 104 | 'hierarchical' => true, |
||
| 105 | 'show_ui' => true, |
||
| 106 | 'show_admin_column' => true, |
||
| 107 | 'query_var' => true, |
||
| 108 | ); |
||
| 109 | |||
| 110 | register_taxonomy( $this->get_type(), $this->associate_post_types, $args ); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Récupères les données selon le modèle définis. |
||
| 115 | * |
||
| 116 | * @since 0.1.0 |
||
| 117 | * @version 1.0.0 |
||
| 118 | * |
||
| 119 | * @param array $args Les paramètres de get_terms @https://codex.wordpress.org/Function_Reference/get_terms. |
||
| 120 | * @param boolean $single Si on veut récupérer un tableau, ou qu'une seule entrée. |
||
| 121 | * |
||
| 122 | * @return Object |
||
| 123 | */ |
||
| 124 | public function get( $args = array(), $single = false ) {
|
||
| 125 | $array_terms = array(); |
||
| 126 | |||
| 127 | $default_args = array( |
||
| 128 | 'hide_empty' => false, |
||
| 129 | 'taxonomy' => $this->get_type(), |
||
| 130 | ); |
||
| 131 | |||
| 132 | // Si le paramètre "id" est passé on le transforme en "include" qui est la paramètre attendu par WP_Term_Query. |
||
| 133 | // Dans un soucis d'homogénéité du code, le paramètre "id" remplace le paramètre "include" dans les appels de la fonction. |
||
| 134 | $args['id'] = ! empty( $args['term_id'] ) ? $args['term_id'] : ( isset( $args['id'] ) ? $args['id'] : null ); |
||
| 135 | if ( ! empty( $args['id'] ) ) {
|
||
| 136 | if ( isset( $args['term_id'] ) ) {
|
||
| 137 | unset( $args['term_id'] ); |
||
| 138 | } |
||
| 139 | if ( ! isset( $args['include'] ) ) {
|
||
| 140 | $args['include'] = array(); |
||
| 141 | } |
||
| 142 | $args['include'] = array_merge( $args['include'], (array) $args['id'] ); |
||
| 143 | } elseif ( isset( $args['id'] ) ) {
|
||
| 144 | $args['schema'] = true; |
||
| 145 | } |
||
| 146 | unset( $args['id'] ); |
||
| 147 | |||
| 148 | // @Todo: a voir pourquoi wp_get_post_terms et pas wp_get_object_terms et si pas d'autre moyen que ici. |
||
| 149 | // elseif ( isset( $args['post_id'] ) ) {
|
||
| 150 | // $array_term = wp_get_post_terms( $args['post_id'], $this->get_type(), $term_final_args ); |
||
| 151 | // |
||
| 152 | // if ( empty( $array_term ) ) {
|
||
| 153 | // $array_term[] = array(); |
||
| 154 | // } |
||
| 155 | // } |
||
| 156 | |||
| 157 | $args_cb = array( |
||
| 158 | 'args' => $args, |
||
| 159 | 'default_args' => $default_args, |
||
| 160 | ); |
||
| 161 | $final_args = apply_filters( 'eo_model_term_before_get', wp_parse_args( $args, $default_args ), $args_cb ); |
||
| 162 | // Il ne faut pas lancer plusieurs fois pour term. |
||
| 163 | if ( 'term' !== $this->get_type() ) {
|
||
| 164 | $final_args = apply_filters( 'eo_model_' . $this->get_type() . '_before_get', $final_args, $args_cb ); |
||
| 165 | } |
||
| 166 | |||
| 167 | // 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. |
||
| 168 | if ( isset( $args['schema'] ) ) {
|
||
| 169 | $array_terms[] = $final_args; |
||
| 170 | } else { // On lance la requête pour récupèrer les "terms" demandés.
|
||
| 171 | $query_terms = new \WP_Term_Query( $final_args ); |
||
| 172 | $array_terms = $query_terms->terms; |
||
| 173 | unset( $query_terms->terms ); |
||
| 174 | } |
||
| 175 | |||
| 176 | // Traitement de la liste des résultats pour le retour. |
||
| 177 | $array_terms = $this->prepare_items_for_response( $array_terms, 'term', $this->meta_key, 'term_id' ); |
||
| 178 | |||
| 179 | // 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. |
||
| 180 | if ( true === $single && 1 === count( $array_terms ) ) {
|
||
| 181 | $array_terms = $array_terms[0]; |
||
| 182 | } |
||
| 183 | |||
| 184 | return $array_terms; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Insère ou met à jour les données dans la base de donnée. |
||
| 189 | * |
||
| 190 | * @since 0.1.0 |
||
| 191 | * @version 1.0.0 |
||
| 192 | * |
||
| 193 | * @param Array $data Les données a insérer ou à mêttre à jour. |
||
| 194 | * @return Object L'objet construit grâce au modèle. |
||
| 195 | */ |
||
| 196 | public function update( $data ) {
|
||
| 197 | $model_name = $this->model_name; |
||
| 198 | $data = (array) $data; |
||
| 199 | $req_method = ( ! empty( $data['id'] ) ) ? 'put' : 'post'; |
||
| 200 | $args_cb = array( |
||
| 201 | 'model_name' => $model_name, |
||
| 202 | 'req_method' => $req_method, |
||
| 203 | 'meta_key' => $this->meta_key, |
||
| 204 | ); |
||
| 205 | |||
| 206 | $data = apply_filters( 'eo_model_term_before_' . $req_method, $data, $args_cb ); |
||
| 207 | // Il ne faut pas lancer plusieurs fois pour category. |
||
| 208 | if ( 'category' !== $this->get_type() ) {
|
||
| 209 | $data = apply_filters( 'eo_model_' . $this->get_type() . '_before_' . $req_method, $data, $args_cb ); |
||
| 210 | } |
||
| 211 | $args_cb['data'] = $data; |
||
| 212 | |||
| 213 | $object = new $model_name( $data, $req_method ); |
||
| 214 | |||
| 215 | if ( empty( $object->data['id'] ) ) {
|
||
| 216 | $term = wp_insert_term( $object->data['name'], $this->get_type(), $object->convert_to_wordpress() ); |
||
| 217 | } else {
|
||
| 218 | $term = wp_update_term( $object->data['id'], $this->get_type(), $object->convert_to_wordpress() ); |
||
| 219 | } |
||
| 220 | |||
| 221 | if ( is_wp_error( $term ) ) {
|
||
| 222 | if ( ! empty( $term->error_data['term_exists'] ) && is_int( $term->error_data['term_exists'] ) ) {
|
||
| 223 | return $this->get( array( |
||
| 224 | 'id' => $term->error_data['term_exists'], |
||
| 225 | ), true ); |
||
| 226 | } |
||
| 227 | |||
| 228 | return $term; |
||
| 229 | } |
||
| 230 | |||
| 231 | // Lors de la création, $object->data['id'] est vide là, du coup le get ne marchait pas. |
||
| 232 | $object->data['id'] = $term['term_id']; |
||
| 233 | |||
| 234 | $object = apply_filters( 'eo_model_term_after_' . $req_method, $object, $args_cb ); |
||
| 235 | $object = $this->get( array( 'id' => $object->data['id'] ), true ); |
||
| 236 | |||
| 237 | // Il ne faut pas lancer plusieurs fois pour category. |
||
| 238 | if ( 'category' !== $this->get_type() ) {
|
||
| 239 | $object = apply_filters( 'eo_model_' . $this->get_type() . '_after_' . $req_method, $object, $args_cb ); |
||
| 240 | } |
||
| 241 | |||
| 242 | return $object; |
||
| 243 | } |
||
| 244 | |||
| 245 | } |
||
| 246 | } // End if(). |
||
| 247 |