| 1 |
<?php |
| 2 |
/** |
| 3 |
* @api {get} /redirection/v1/log Get log logs |
| 4 |
* @apiDescription Get log logs |
| 5 |
* @apiGroup Log |
| 6 |
* |
| 7 |
* @apiParam {string} groupBy Group by 'ip' or 'url' |
| 8 |
* @apiParam {string} orderby |
| 9 |
* @apiParam {string} direction |
| 10 |
* @apiParam {string} filter |
| 11 |
* @apiParam {string} per_page |
| 12 |
* @apiParam {string} page |
| 13 |
*/ |
| 14 |
|
| 15 |
/** |
| 16 |
* @api {post} /redirection/v1/log Delete log logs |
| 17 |
* @apiDescription Delete log logs either by ID or filter or group |
| 18 |
* @apiGroup Log |
| 19 |
* |
| 20 |
* @apiParam {string} items Array of log IDs |
| 21 |
* @apiParam {string} filter |
| 22 |
* @apiParam {string} filterBy |
| 23 |
* @apiParam {string} groupBy Group by 'ip' or 'url' |
| 24 |
*/ |
| 25 |
|
| 26 |
/** |
| 27 |
* @api {post} /redirection/v1/bulk/log/delete Bulk actions on logs |
| 28 |
* @apiDescription Delete log logs either by ID |
| 29 |
* @apiGroup Log |
| 30 |
*/ |
| 31 |
class Redirection_Api_Log extends Redirection_Api_Filter_Route { |
| 32 |
public function __construct( $namespace ) { |
| 33 |
$filters = array( 'url', 'ip', 'url-exact' ); |
| 34 |
$orders = array( 'url', 'ip' ); |
| 35 |
|
| 36 |
register_rest_route( $namespace, '/log', array( |
| 37 |
'args' => $this->get_filter_args( $filters, $orders ), |
| 38 |
$this->get_route( WP_REST_Server::READABLE, 'route_log' ), |
| 39 |
$this->get_route( WP_REST_Server::EDITABLE, 'route_delete_all' ), |
| 40 |
) ); |
| 41 |
|
| 42 |
$this->register_bulk( $namespace, '/bulk/log/(?P<bulk>delete)', $filters, $filters, 'route_bulk' ); |
| 43 |
} |
| 44 |
|
| 45 |
public function route_log( WP_REST_Request $request ) { |
| 46 |
return $this->get_logs( $request->get_params() ); |
| 47 |
} |
| 48 |
|
| 49 |
public function route_bulk( WP_REST_Request $request ) { |
| 50 |
$items = explode( ',', $request['items'] ); |
| 51 |
|
| 52 |
if ( is_array( $items ) ) { |
| 53 |
$items = array_map( 'intval', $items ); |
| 54 |
array_map( array( 'RE_Log', 'delete' ), $items ); |
| 55 |
return $this->route_log( $request ); |
| 56 |
} |
| 57 |
|
| 58 |
return $this->add_error_details( new WP_Error( 'redirect', 'Invalid array of items' ), __LINE__ ); |
| 59 |
} |
| 60 |
|
| 61 |
public function route_delete_all( WP_REST_Request $request ) { |
| 62 |
$params = $request->get_params(); |
| 63 |
$filter = false; |
| 64 |
$filter_by = false; |
| 65 |
|
| 66 |
if ( isset( $params['filter'] ) ) { |
| 67 |
$filter = $params['filter']; |
| 68 |
} |
| 69 |
|
| 70 |
if ( isset( $params['filterBy'] ) && in_array( $params['filterBy'], array( 'url', 'ip', 'url-exact' ), true ) ) { |
| 71 |
$filter_by = $params['filterBy']; |
| 72 |
} |
| 73 |
|
| 74 |
RE_Log::delete_all( $filter_by, $filter ); |
| 75 |
return $this->route_log( $request ); |
| 76 |
} |
| 77 |
|
| 78 |
private function get_logs( array $params ) { |
| 79 |
return RE_Filter_Log::get( 'redirection_logs', 'RE_Log', $params ); |
| 80 |
} |
| 81 |
} |
| 82 |
|