| 1 |
<?php |
| 2 |
|
| 3 |
namespace FL\Assistant\Controllers; |
| 4 |
|
| 5 |
use FL\Assistant\Data\Repository\NotationsRepository; |
| 6 |
use FL\Assistant\System\Contracts\ControllerAbstract; |
| 7 |
use WP_REST_Server; |
| 8 |
|
| 9 |
/** |
| 10 |
* REST API logic for notations. |
| 11 |
*/ |
| 12 |
class NotationsController extends ControllerAbstract { |
| 13 |
|
| 14 |
/** |
| 15 |
* @var NotationsRepository |
| 16 |
*/ |
| 17 |
protected $notations; |
| 18 |
|
| 19 |
/** |
| 20 |
* NotationsController constructor. |
| 21 |
* |
| 22 |
* @param NotationsRepository $notations |
| 23 |
*/ |
| 24 |
public function __construct( NotationsRepository $notations ) { |
| 25 |
$this->notations = $notations; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Register routes. |
| 30 |
*/ |
| 31 |
public function register_routes() { |
| 32 |
$this->route( |
| 33 |
'/notations/delete-where-meta', [ |
| 34 |
[ |
| 35 |
'methods' => WP_REST_Server::CREATABLE, |
| 36 |
'callback' => [ $this, 'delete_notation' ], |
| 37 |
'permission_callback' => function () { |
| 38 |
return current_user_can( 'edit_others_posts' ); |
| 39 |
}, |
| 40 |
], |
| 41 |
] |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Deletes notations. |
| 47 |
* |
| 48 |
* @param \WP_REST_Request $request |
| 49 |
* |
| 50 |
* @return mixed|\WP_REST_Response |
| 51 |
*/ |
| 52 |
public function delete_notation( \WP_REST_Request $request ) { |
| 53 |
$meta = $request->get_params(); |
| 54 |
$notations = $this->notations->get_by_meta( $meta ); |
| 55 |
|
| 56 |
foreach ( $notations as $notation ) { |
| 57 |
if ( ! current_user_can( 'edit_post', $notation['id'] ) ) { |
| 58 |
return rest_ensure_response( |
| 59 |
[ |
| 60 |
'error' => true, |
| 61 |
] |
| 62 |
); |
| 63 |
} |
| 64 |
wp_delete_post( $notation['id'] ); |
| 65 |
} |
| 66 |
|
| 67 |
return rest_ensure_response( |
| 68 |
[ |
| 69 |
'success' => true, |
| 70 |
] |
| 71 |
); |
| 72 |
} |
| 73 |
} |
| 74 |
|