PluginProbe
Redirection / 4.5
Redirection v4.5
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-404.php

api-404.php in Redirection 4.5, at api/api-404.php

176 lines 6.4 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/404 Get 404 logs
5 * @apiName GetLogs
6 * @apiDescription Get a paged list of 404 logs after applying a set of filters and result ordering.
7 * @apiGroup 404
8 *
9 * @apiUse 404QueryParams
10 *
11 * @apiUse 404List
12 * @apiUse 401Error
13 * @apiUse 404Error
14 */
15
16 /**
17 * @api {post} /redirection/v1/404 Delete 404 logs
18 * @apiName DeleteLogs
19 * @apiDescription Delete 404 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 404
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 404List
31 * @apiUse 401Error
32 * @apiUse 404Error
33 */
34
35 /**
36 * @api {post} /redirection/v1/bulk/404/:type Bulk 404 action
37 * @apiName BulkAction
38 * @apiDescription Delete 404 logs by ID
39 * @apiGroup 404
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 404QueryParams
45 *
46 * @apiUse 404List
47 * @apiUse 401Error
48 * @apiUse 404Error
49 * @apiUse 400MissingError
50 * @apiError (Error 400) redirect_404_invalid_items Invalid array of items
51 * @apiErrorExample {json} 404 Error Response:
52 * HTTP/1.1 400 Bad Request
53 * {
54 * "code": "redirect_404_invalid_items",
55 * "message": "Invalid array of items"
56 * }
57 */
58
59 /**
60 * @apiDefine 404QueryParams 404 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 * @apiParam (Query Parameter) {String="ip","url"} groupBy Group by IP or URL
73 */
74
75 /**
76 * @apiDefine 404List
77 *
78 * @apiSuccess {Object[]} items Array of 404 log objects
79 * @apiSuccess {Integer} items.id ID of 404 log entry
80 * @apiSuccess {String} items.created Date the 404 log entry was recorded
81 * @apiSuccess {Integer} items.created_time Unix time value for `created`
82 * @apiSuccess {Integer} items.url The requested URL that caused the 404 log entry
83 * @apiSuccess {String} items.agent User agent of the client initiating the request
84 * @apiSuccess {Integer} items.referrer Referrer of the client initiating the request
85 * @apiSuccess {Integer} total Number of items
86 *
87 * @apiSuccessExample {json} Success 200:
88 * HTTP/1.1 200 OK
89 * {
90 * "items": [
91 * {
92 * "id": 3,
93 * "created": "2019-01-01 12:12:00,
94 * "created_time": "12345678",
95 * "url": "/the-url",
96 * "agent": "FancyBrowser",
97 * "referrer": "http://site.com/previous/,
98 * }
99 * ],
100 * "total": 1
101 * }
102 */
103 class Redirection_Api_404 extends Redirection_Api_Filter_Route {
104 public function __construct( $namespace ) {
105 $orders = [ 'url', 'ip', 'total' ];
106 $filters = [ 'ip', 'url-exact', 'referrer', 'agent', 'url' ];
107
108 register_rest_route( $namespace, '/404', array(
109 'args' => $this->get_filter_args( $orders, $filters ),
110 $this->get_route( WP_REST_Server::READABLE, 'route_404' ),
111 $this->get_route( WP_REST_Server::EDITABLE, 'route_delete_all' ),
112 ) );
113
114 $this->register_bulk( $namespace, '/bulk/404/(?P<bulk>delete)', $orders, 'route_bulk' );
115 }
116
117 public function route_404( WP_REST_Request $request ) {
118 return $this->get_404( $request->get_params() );
119 }
120
121 public function route_bulk( WP_REST_Request $request ) {
122 $params = $request->get_params();
123 $items = explode( ',', $request['items'] );
124
125 if ( is_array( $items ) ) {
126 foreach ( $items as $item ) {
127 if ( is_numeric( $item ) ) {
128 RE_404::delete( intval( $item, 10 ) );
129 } else {
130 RE_404::delete_all( $this->get_delete_group( $params ), $item );
131 }
132 }
133
134 return $this->route_404( $request );
135 }
136
137 return $this->add_error_details( new WP_Error( 'redirect_404_invalid_items', 'Invalid array of items' ), __LINE__ );
138 }
139
140 private function get_delete_group( array $params ) {
141 if ( isset( $params['groupBy'] ) && $params['groupBy'] === 'ip' ) {
142 return 'ip';
143 }
144
145 return 'url-exact';
146 }
147
148 public function route_delete_all( WP_REST_Request $request ) {
149 $params = $request->get_params();
150
151 if ( isset( $params['items'] ) && is_array( $params['items'] ) ) {
152 foreach ( $params['items'] as $url ) {
153 RE_404::delete_all( $this->get_delete_group( $params ), $url );
154 }
155 } else {
156 $first_filter = isset( $params['filterBy'] ) ? array_keys( $params['filterBy'] )[0] : false;
157
158 RE_404::delete_all( $first_filter ? $first_filter : false, $first_filter ? $params['filterBy'][ $first_filter ] : false );
159
160 unset( $params['filterBy'] );
161 }
162
163 unset( $params['page'] );
164
165 return $this->get_404( $params );
166 }
167
168 private function get_404( array $params ) {
169 if ( isset( $params['groupBy'] ) && in_array( $params['groupBy'], array( 'ip', 'url' ), true ) ) {
170 return RE_Filter_Log::get_grouped( 'redirection_404', $params['groupBy'], $params );
171 }
172
173 return RE_Filter_Log::get( 'redirection_404', 'RE_404', $params );
174 }
175 }
176