| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @api {get} /redirection/v1/group Get logs |
| 5 |
* @apiName GetLogs |
| 6 |
* @apiDescription Get a paged list of redirect logs after applying a set of filters and result ordering. |
| 7 |
* @apiGroup Log |
| 8 |
* |
| 9 |
* @apiUse LogQueryParams |
| 10 |
* |
| 11 |
* @apiUse LogList |
| 12 |
* @apiUse 401Error |
| 13 |
* @apiUse 404Error |
| 14 |
*/ |
| 15 |
|
| 16 |
/** |
| 17 |
* @api {post} /redirection/v1/log Delete logs |
| 18 |
* @apiName DeleteLogs |
| 19 |
* @apiDescription Delete logs by filter. If no filter is supplied then all entries will be deleted. The endpoint will return the next page of results after. |
| 20 |
* performing the action, based on the supplied query parameters. This information can be used to refresh a list displayed to the client. |
| 21 |
* @apiGroup Log |
| 22 |
* |
| 23 |
* @apiParam (Query Parameter) {String} filterBy[ip] Filter the results by the supplied IP |
| 24 |
* @apiParam (Query Parameter) {String} filterBy[url] Filter the results by the supplied URL |
| 25 |
* @apiParam (Query Parameter) {String} filterBy[url-exact] Filter the results by the exact URL (not a substring match, as per `url`) |
| 26 |
* @apiParam (Query Parameter) {String} filterBy[referrer] Filter the results by the supplied referrer |
| 27 |
* @apiParam (Query Parameter) {String} filterBy[agent] Filter the results by the supplied user agent |
| 28 |
* @apiParam (Query Parameter) {String} filterBy[target] Filter the results by the supplied redirect target |
| 29 |
* |
| 30 |
* @apiUse LogList |
| 31 |
* @apiUse 401Error |
| 32 |
* @apiUse 404Error |
| 33 |
*/ |
| 34 |
|
| 35 |
/** |
| 36 |
* @api {post} /redirection/v1/bulk/log/:type Bulk log action |
| 37 |
* @apiName BulkAction |
| 38 |
* @apiDescription Delete logs by ID |
| 39 |
* @apiGroup Log |
| 40 |
* |
| 41 |
* @apiParam (URL) {String="delete"} :type Type of bulk action that is applied to every log ID. |
| 42 |
* |
| 43 |
* @apiParam (Query Parameter) {Integer[]} items Array of group IDs to perform the action on |
| 44 |
* @apiUse LogQueryParams |
| 45 |
* |
| 46 |
* @apiUse LogList |
| 47 |
* @apiUse 401Error |
| 48 |
* @apiUse 404Error |
| 49 |
* @apiUse 400MissingError |
| 50 |
*/ |
| 51 |
|
| 52 |
/** |
| 53 |
* @apiDefine LogQueryParams Log query parameters |
| 54 |
* |
| 55 |
* @apiParam (Query Parameter) {String} filterBy[ip] Filter the results by the supplied IP |
| 56 |
* @apiParam (Query Parameter) {String} filterBy[url] Filter the results by the supplied URL |
| 57 |
* @apiParam (Query Parameter) {String} filterBy[url-exact] Filter the results by the exact URL (not a substring match, as per `url`) |
| 58 |
* @apiParam (Query Parameter) {String} filterBy[referrer] Filter the results by the supplied referrer |
| 59 |
* @apiParam (Query Parameter) {String} filterBy[agent] Filter the results by the supplied user agent |
| 60 |
* @apiParam (Query Parameter) {String} filterBy[target] Filter the results by the supplied redirect target |
| 61 |
* @apiParam (Query Parameter) {string="ip","url"} orderby Order by IP or URL |
| 62 |
* @apiParam (Query Parameter) {String="asc","desc"} direction Direction to order the results by (ascending or descending) |
| 63 |
* @apiParam (Query Parameter) {Integer{1...200}} per_page Number of results per request |
| 64 |
* @apiParam (Query Parameter) {Integer} page Current page of results |
| 65 |
*/ |
| 66 |
|
| 67 |
/** |
| 68 |
* @apiDefine LogList |
| 69 |
* |
| 70 |
* @apiSuccess {Object[]} items Array of log objects |
| 71 |
* @apiSuccess {Integer} items.id ID of log entry |
| 72 |
* @apiSuccess {String} items.created Date the log entry was recorded |
| 73 |
* @apiSuccess {Integer} items.created_time Unix time value for `created` |
| 74 |
* @apiSuccess {Integer} items.url The requested URL that caused the log entry |
| 75 |
* @apiSuccess {String} items.agent User agent of the client initiating the request |
| 76 |
* @apiSuccess {Integer} items.referrer Referrer of the client initiating the request |
| 77 |
* @apiSuccess {Integer} total Number of items |
| 78 |
* |
| 79 |
* @apiSuccessExample {json} Success 200: |
| 80 |
* HTTP/1.1 200 OK |
| 81 |
* { |
| 82 |
* "items": [ |
| 83 |
* { |
| 84 |
* "id": 3, |
| 85 |
* "created": "2019-01-01 12:12:00, |
| 86 |
* "created_time": "12345678", |
| 87 |
* "url": "/the-url", |
| 88 |
* "agent": "FancyBrowser", |
| 89 |
* "referrer": "http://site.com/previous/, |
| 90 |
* } |
| 91 |
* ], |
| 92 |
* "total": 1 |
| 93 |
* } |
| 94 |
*/ |
| 95 |
|
| 96 |
class Redirection_Api_Log extends Redirection_Api_Filter_Route { |
| 97 |
public function __construct( $namespace ) { |
| 98 |
$orders = [ 'url', 'ip' ]; |
| 99 |
$filters = [ 'ip', 'url-exact', 'referrer', 'agent', 'url', 'target' ]; |
| 100 |
|
| 101 |
register_rest_route( $namespace, '/log', array( |
| 102 |
'args' => $this->get_filter_args( $orders, $filters ), |
| 103 |
$this->get_route( WP_REST_Server::READABLE, 'route_log', [ $this, 'permission_callback_manage' ] ), |
| 104 |
$this->get_route( WP_REST_Server::EDITABLE, 'route_delete_all', [ $this, 'permission_callback_delete' ] ), |
| 105 |
) ); |
| 106 |
|
| 107 |
$this->register_bulk( $namespace, '/bulk/log/(?P<bulk>delete)', $orders, 'route_bulk', [ $this, 'permission_callback_delete' ] ); |
| 108 |
} |
| 109 |
|
| 110 |
public function permission_callback_manage( WP_REST_Request $request ) { |
| 111 |
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_LOG_MANAGE ); |
| 112 |
} |
| 113 |
|
| 114 |
public function permission_callback_delete( WP_REST_Request $request ) { |
| 115 |
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_LOG_DELETE ); |
| 116 |
} |
| 117 |
|
| 118 |
public function route_log( WP_REST_Request $request ) { |
| 119 |
return $this->get_logs( $request->get_params() ); |
| 120 |
} |
| 121 |
|
| 122 |
public function route_bulk( WP_REST_Request $request ) { |
| 123 |
$items = $request['items']; |
| 124 |
|
| 125 |
$items = array_map( 'intval', $items ); |
| 126 |
array_map( array( 'RE_Log', 'delete' ), $items ); |
| 127 |
return $this->route_log( $request ); |
| 128 |
} |
| 129 |
|
| 130 |
public function route_delete_all( WP_REST_Request $request ) { |
| 131 |
$params = $request->get_params(); |
| 132 |
|
| 133 |
RE_Log::delete_all( isset( $params['filterBy'] ) ? $params['filterBy'] : [] ); |
| 134 |
return $this->route_log( $request ); |
| 135 |
} |
| 136 |
|
| 137 |
private function get_logs( array $params ) { |
| 138 |
return RE_Filter_Log::get( 'redirection_logs', 'RE_Log', $params ); |
| 139 |
} |
| 140 |
} |
| 141 |
|