PluginProbe
Redirection / 4.6.2
Redirection v4.6.2
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / api / api-log.php

api-log.php in Redirection 4.6.2, at api/api-log.php

152 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @apiError (Error 400) redirect_log_invalid_items Invalid array of items
51 * @apiErrorExample {json} 404 Error Response:
52 * HTTP/1.1 400 Bad Request
53 * {
54 * "code": "redirect_log_invalid_items",
55 * "message": "Invalid array of items"
56 * }
57 */
58
59 /**
60 * @apiDefine LogQueryParams Log query parameters
61 *
62 * @apiParam (Query Parameter) {String} filterBy[ip] Filter the results by the supplied IP
63 * @apiParam (Query Parameter) {String} filterBy[url] Filter the results by the supplied URL
64 * @apiParam (Query Parameter) {String} filterBy[url-exact] Filter the results by the exact URL (not a substring match, as per `url`)
65 * @apiParam (Query Parameter) {String} filterBy[referrer] Filter the results by the supplied referrer
66 * @apiParam (Query Parameter) {String} filterBy[agent] Filter the results by the supplied user agent
67 * @apiParam (Query Parameter) {String} filterBy[target] Filter the results by the supplied redirect target
68 * @apiParam (Query Parameter) {string="ip","url"} orderby Order by IP or URL
69 * @apiParam (Query Parameter) {String="asc","desc"} direction Direction to order the results by (ascending or descending)
70 * @apiParam (Query Parameter) {Integer{1...200}} per_page Number of results per request
71 * @apiParam (Query Parameter) {Integer} page Current page of results
72 */
73
74 /**
75 * @apiDefine LogList
76 *
77 * @apiSuccess {Object[]} items Array of log objects
78 * @apiSuccess {Integer} items.id ID of log entry
79 * @apiSuccess {String} items.created Date the log entry was recorded
80 * @apiSuccess {Integer} items.created_time Unix time value for `created`
81 * @apiSuccess {Integer} items.url The requested URL that caused the log entry
82 * @apiSuccess {String} items.agent User agent of the client initiating the request
83 * @apiSuccess {Integer} items.referrer Referrer of the client initiating the request
84 * @apiSuccess {Integer} total Number of items
85 *
86 * @apiSuccessExample {json} Success 200:
87 * HTTP/1.1 200 OK
88 * {
89 * "items": [
90 * {
91 * "id": 3,
92 * "created": "2019-01-01 12:12:00,
93 * "created_time": "12345678",
94 * "url": "/the-url",
95 * "agent": "FancyBrowser",
96 * "referrer": "http://site.com/previous/,
97 * }
98 * ],
99 * "total": 1
100 * }
101 */
102
103 class Redirection_Api_Log extends Redirection_Api_Filter_Route {
104 public function __construct( $namespace ) {
105 $orders = [ 'url', 'ip' ];
106 $filters = [ 'ip', 'url-exact', 'referrer', 'agent', 'url', 'target' ];
107
108 register_rest_route( $namespace, '/log', array(
109 'args' => $this->get_filter_args( $orders, $filters ),
110 $this->get_route( WP_REST_Server::READABLE, 'route_log', [ $this, 'permission_callback_manage' ] ),
111 $this->get_route( WP_REST_Server::EDITABLE, 'route_delete_all', [ $this, 'permission_callback_delete' ] ),
112 ) );
113
114 $this->register_bulk( $namespace, '/bulk/log/(?P<bulk>delete)', $orders, 'route_bulk', [ $this, 'permission_callback_delete' ] );
115 }
116
117 public function permission_callback_manage( WP_REST_Request $request ) {
118 return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_LOG_MANAGE );
119 }
120
121 public function permission_callback_delete( WP_REST_Request $request ) {
122 return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_LOG_DELETE );
123 }
124
125 public function route_log( WP_REST_Request $request ) {
126 return $this->get_logs( $request->get_params() );
127 }
128
129 public function route_bulk( WP_REST_Request $request ) {
130 $items = explode( ',', $request['items'] );
131
132 if ( is_array( $items ) ) {
133 $items = array_map( 'intval', $items );
134 array_map( array( 'RE_Log', 'delete' ), $items );
135 return $this->route_log( $request );
136 }
137
138 return $this->add_error_details( new WP_Error( 'redirect_log_invalid_items', 'Invalid array of items' ), __LINE__ );
139 }
140
141 public function route_delete_all( WP_REST_Request $request ) {
142 $params = $request->get_params();
143
144 RE_Log::delete_all( isset( $params['filterBy'] ) ? $params['filterBy'] : [] );
145 return $this->route_log( $request );
146 }
147
148 private function get_logs( array $params ) {
149 return RE_Filter_Log::get( 'redirection_logs', 'RE_Log', $params );
150 }
151 }
152