| 1 |
<?php |
| 2 |
|
| 3 |
namespace FL\Assistant\Controllers; |
| 4 |
|
| 5 |
use FL\Assistant\Data\Transformers\TermsTransformer; |
| 6 |
use FL\Assistant\Data\Repository\PostsRepository; |
| 7 |
use FL\Assistant\Data\Repository\TermsRepository; |
| 8 |
use FL\Assistant\System\Contracts\ControllerAbstract; |
| 9 |
use WP_REST_Request; |
| 10 |
use WP_REST_Response; |
| 11 |
use WP_REST_Server; |
| 12 |
|
| 13 |
/** |
| 14 |
* REST API logic for terms. |
| 15 |
*/ |
| 16 |
class TermsController extends ControllerAbstract { |
| 17 |
|
| 18 |
protected $terms; |
| 19 |
protected $posts; |
| 20 |
protected $transformer; |
| 21 |
|
| 22 |
public function __construct( TermsRepository $terms, PostsRepository $posts, TermsTransformer $transformer ) { |
| 23 |
$this->terms = $terms; |
| 24 |
$this->posts = $posts; |
| 25 |
$this->transformer = $transformer; |
| 26 |
} |
| 27 |
/** |
| 28 |
* Register routes. |
| 29 |
*/ |
| 30 |
public function register_routes() { |
| 31 |
$this->route( |
| 32 |
'/terms', [ |
| 33 |
[ |
| 34 |
'methods' => WP_REST_Server::READABLE, |
| 35 |
'callback' => [ $this, 'terms' ], |
| 36 |
'permission_callback' => function () { |
| 37 |
return current_user_can( 'edit_others_posts' ); |
| 38 |
}, |
| 39 |
], |
| 40 |
[ |
| 41 |
'methods' => WP_REST_Server::CREATABLE, |
| 42 |
'callback' => [ $this, 'create_term' ], |
| 43 |
'permission_callback' => function () { |
| 44 |
return current_user_can( 'edit_others_posts' ); |
| 45 |
}, |
| 46 |
], |
| 47 |
] |
| 48 |
); |
| 49 |
|
| 50 |
$this->route( |
| 51 |
'/terms/hierarchical', [ |
| 52 |
[ |
| 53 |
'methods' => WP_REST_Server::READABLE, |
| 54 |
'callback' => [ $this, 'hierarchical_terms' ], |
| 55 |
'permission_callback' => function () { |
| 56 |
return current_user_can( 'edit_others_posts' ); |
| 57 |
}, |
| 58 |
], |
| 59 |
] |
| 60 |
); |
| 61 |
|
| 62 |
$this->route( |
| 63 |
'/terms/get_parent_terms', [ |
| 64 |
[ |
| 65 |
'methods' => WP_REST_Server::READABLE, |
| 66 |
'callback' => [ $this, 'get_parent_terms' ], |
| 67 |
'permission_callback' => function () { |
| 68 |
return current_user_can( 'edit_others_posts' ); |
| 69 |
}, |
| 70 |
], |
| 71 |
] |
| 72 |
); |
| 73 |
|
| 74 |
$this->route( |
| 75 |
'/terms/count', [ |
| 76 |
[ |
| 77 |
'methods' => WP_REST_Server::READABLE, |
| 78 |
'callback' => [ $this, 'terms_count' ], |
| 79 |
'permission_callback' => function () { |
| 80 |
return current_user_can( 'moderate_comments' ); |
| 81 |
}, |
| 82 |
], |
| 83 |
] |
| 84 |
); |
| 85 |
|
| 86 |
$this->route( |
| 87 |
'/terms/(?P<id>\d+)', [ |
| 88 |
[ |
| 89 |
'methods' => WP_REST_Server::READABLE, |
| 90 |
'callback' => [ $this, 'term' ], |
| 91 |
'args' => [ |
| 92 |
'id' => [ |
| 93 |
'required' => true, |
| 94 |
'type' => 'number', |
| 95 |
], |
| 96 |
], |
| 97 |
'permission_callback' => function () { |
| 98 |
return current_user_can( 'edit_others_posts' ); |
| 99 |
}, |
| 100 |
], |
| 101 |
[ |
| 102 |
'methods' => WP_REST_Server::CREATABLE, |
| 103 |
'callback' => [ $this, 'update_term' ], |
| 104 |
'args' => [ |
| 105 |
'id' => [ |
| 106 |
'required' => true, |
| 107 |
'type' => 'number', |
| 108 |
], |
| 109 |
'action' => [ |
| 110 |
'required' => true, |
| 111 |
'type' => 'string', |
| 112 |
], |
| 113 |
], |
| 114 |
'permission_callback' => function () { |
| 115 |
return current_user_can( 'edit_others_posts' ); |
| 116 |
}, |
| 117 |
], |
| 118 |
] |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns an array of terms and related data. |
| 124 |
* @param WP_REST_Request $request |
| 125 |
* @return mixed|WP_REST_Response |
| 126 |
*/ |
| 127 |
public function terms( WP_REST_Request $request ) { |
| 128 |
|
| 129 |
return $this->terms->paginate( $request->get_params() ) |
| 130 |
->apply_transform( $this->transformer ) |
| 131 |
->to_rest_response(); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Returns an array of terms and related data |
| 136 |
* with child terms contained in the parent |
| 137 |
* term's data array. |
| 138 |
* @param WP_REST_Request $request |
| 139 |
* @return mixed|WP_REST_Response |
| 140 |
*/ |
| 141 |
public function hierarchical_terms( WP_REST_Request $request ) { |
| 142 |
$response = []; |
| 143 |
$children = []; |
| 144 |
$params = $request->get_params(); |
| 145 |
$terms = $this->terms->find_where( $params ); |
| 146 |
|
| 147 |
foreach ( $terms as $term ) { |
| 148 |
if ( $term->parent ) { |
| 149 |
if ( ! isset( $children[ $term->parent ] ) ) { |
| 150 |
$children[ $term->parent ] = []; |
| 151 |
} |
| 152 |
$children[ $term->parent ][] = $term; |
| 153 |
} |
| 154 |
$response[] = $term; |
| 155 |
} |
| 156 |
|
| 157 |
foreach ( $terms as $term ) { |
| 158 |
if ( ! $term->parent ) { |
| 159 |
$term->children = $this->terms->get_child_terms( $term, $children, $this->transformer ); |
| 160 |
$response[] = $term; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
return rest_ensure_response( array_map( $this->transformer, $response ) ); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
/** |
| 170 |
* Returns an array of parent terms and related data |
| 171 |
* parent term's data array. |
| 172 |
* @param WP_REST_Request $request |
| 173 |
* @return mixed|WP_REST_Response |
| 174 |
*/ |
| 175 |
public function get_parent_terms( WP_REST_Request $request ) { |
| 176 |
$response = []; |
| 177 |
$params = $request->get_params(); |
| 178 |
$terms = $this->terms->find_where( $params ); |
| 179 |
|
| 180 |
foreach ( $terms as $term ) { |
| 181 |
if ( $term->parent ) { |
| 182 |
if ( ! isset( $children[ $term->parent ] ) ) { |
| 183 |
$children[ $term->parent ] = []; |
| 184 |
} |
| 185 |
$children[ $term->parent ][] = $term; |
| 186 |
} |
| 187 |
if ( $term->parent !== $params['current'] ) { |
| 188 |
$response[] = $term; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
foreach ( $terms as $term ) { |
| 193 |
if ( ! $term->parent ) { |
| 194 |
$term->children = $this->terms->get_child_terms( $term, $children, $this->transformer ); |
| 195 |
$response[] = $term; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
return rest_ensure_response( array_map( $this->transformer, $response ) ); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
/** |
| 206 |
* Returns an array of counts by taxonomy type. |
| 207 |
* @return mixed|WP_REST_Response |
| 208 |
*/ |
| 209 |
public function terms_count() { |
| 210 |
|
| 211 |
$taxonomies = $this->posts->get_taxononies(); |
| 212 |
$response = []; |
| 213 |
|
| 214 |
foreach ( $taxonomies as $slug => $label ) { |
| 215 |
$count = wp_count_terms( $slug ); |
| 216 |
$response[ $slug ] = (int) $count; |
| 217 |
} |
| 218 |
|
| 219 |
return rest_ensure_response( $response ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Returns data for a single term. |
| 224 |
* @param WP_REST_Request $request |
| 225 |
* @return mixed|WP_REST_Response |
| 226 |
*/ |
| 227 |
public function term( WP_REST_Request $request ) { |
| 228 |
$id = $request->get_param( 'id' ); |
| 229 |
$term = $this->terms->find( $id, $this->transformer ); |
| 230 |
return rest_ensure_response( $term ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Creates a single term. |
| 235 |
* @param WP_REST_Request $request |
| 236 |
* @return array|mixed|WP_REST_Response |
| 237 |
*/ |
| 238 |
public function create_term( WP_REST_Request $request ) { |
| 239 |
$data = $request->get_params(); |
| 240 |
$meta = []; |
| 241 |
|
| 242 |
if ( isset( $data['meta'] ) ) { |
| 243 |
$meta = $data['meta']; |
| 244 |
unset( $data['meta'] ); |
| 245 |
} |
| 246 |
|
| 247 |
$data = array_map( 'sanitize_text_field', $data ); |
| 248 |
$id = wp_insert_term( |
| 249 |
$data['name'], |
| 250 |
$data['taxonomy'], |
| 251 |
[ |
| 252 |
'description' => $data['description'], |
| 253 |
'slug' => $data['slug'], |
| 254 |
'parent' => $data['parent'], |
| 255 |
] |
| 256 |
); |
| 257 |
|
| 258 |
if ( is_wp_error( $id ) ) { |
| 259 |
if ( isset( $id->errors['term_exists'] ) ) { |
| 260 |
return [ |
| 261 |
'error' => 'exists', |
| 262 |
]; |
| 263 |
} |
| 264 |
|
| 265 |
return [ |
| 266 |
'error' => true, |
| 267 |
]; |
| 268 |
} |
| 269 |
|
| 270 |
$this->update_term_meta( $id['term_id'], $meta ); |
| 271 |
$term = call_user_func( $this->transformer, get_term( $id['term_id'], $data['taxonomy'] ) ); |
| 272 |
return rest_ensure_response( $term ); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Updates a single term based on the specified action. |
| 277 |
* @param WP_REST_Request $request |
| 278 |
* @return mixed|WP_REST_Response |
| 279 |
*/ |
| 280 |
public function update_term( WP_REST_Request $request ) { |
| 281 |
$id = $request->get_param( 'id' ); |
| 282 |
$action = $request->get_param( 'action' ); |
| 283 |
$term = get_term( $id ); |
| 284 |
|
| 285 |
if ( ! current_user_can( 'edit_term', $id ) ) { |
| 286 |
return rest_ensure_response( |
| 287 |
[ |
| 288 |
'error' => true, |
| 289 |
] |
| 290 |
); |
| 291 |
} |
| 292 |
|
| 293 |
switch ( $action ) { |
| 294 |
case 'data': |
| 295 |
$data = (array) $request->get_param( 'data' ); |
| 296 |
if ( isset( $data['meta'] ) ) { |
| 297 |
$this->update_term_meta( $id, $data['meta'] ); |
| 298 |
unset( $data['meta'] ); |
| 299 |
} |
| 300 |
wp_update_term( $id, $term->taxonomy, $data ); |
| 301 |
break; |
| 302 |
case 'meta': |
| 303 |
$data = (array) $request->get_param( 'data' ); |
| 304 |
$this->update_term_meta( $id, $data ); |
| 305 |
break; |
| 306 |
case 'trash': |
| 307 |
wp_delete_term( $id, $term->taxonomy ); |
| 308 |
break; |
| 309 |
} |
| 310 |
|
| 311 |
return rest_ensure_response( |
| 312 |
[ |
| 313 |
'success' => true, |
| 314 |
] |
| 315 |
); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Updates meta values for a term. |
| 320 |
*/ |
| 321 |
public function update_term_meta( $id, $meta ) { |
| 322 |
foreach ( $meta as $key => $value ) { |
| 323 |
update_term_meta( $id, $key, $value ); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|