PluginProbe
Redirection / 5.8.1
Redirection v5.8.1
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 5.8.1, at api/api-404.php

226 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @api {get} /redirection/v1/404 Get 404 logs
4 * @apiName GetLogs
5 * @apiDescription Get a paged list of 404 logs after applying a set of filters and result ordering.
6 * @apiGroup 404
7 *
8 * @apiUse 404QueryParams
9 *
10 * @apiUse 404List
11 * @apiUse 401Error
12 * @apiUse 404Error
13 */
14
15 /**
16 * @api {post} /redirection/v1/bulk/404/:type Bulk action
17 * @apiName BulkAction
18 * @apiDescription Delete 404 logs by ID
19 * @apiGroup 404
20 *
21 * @apiParam (URL) {String="delete"} :type Type of bulk action that is applied to every log ID.
22 *
23 * @apiParam (Query Parameter) {String[]} [items] Array of group IDs to perform the action on
24 * @apiParam (Query Parameter) {Boolean=false} [global] Perform action globally using the filter parameters
25 * @apiUse 404QueryParams
26 *
27 * @apiUse 404List
28 * @apiUse 401Error
29 * @apiUse 404Error
30 * @apiUse 400MissingError
31 */
32
33 /**
34 * @apiDefine 404QueryParams 404 log query parameters
35 *
36 * @apiParam (Query Parameter) {String} [filterBy[ip]] Filter the results by the supplied IP
37 * @apiParam (Query Parameter) {String} [filterBy[url]] Filter the results by the supplied URL
38 * @apiParam (Query Parameter) {String} [filterBy[url-]exact] Filter the results by the exact URL (not a substring match, as per `url`)
39 * @apiParam (Query Parameter) {String} [filterBy[referrer]] Filter the results by the supplied referrer
40 * @apiParam (Query Parameter) {String} [filterBy[agent]] Filter the results by the supplied user agent
41 * @apiParam (Query Parameter) {String} [filterBy[target]] Filter the results by the supplied redirect target
42 * @apiParam (Query Parameter) {String} [filterBy[domain]] Filter the results by the supplied domain name
43 * @apiParam (Query Parameter) {String="head","get","post"} [filterBy[method]] Filter the results by the supplied HTTP request method
44 * @apiParam (Query Parameter) {Integer} [filterBy[http]] Filter the results by the supplied redirect HTTP code
45 * @apiParam (Query Parameter) {string="ip","url"} [orderby] Order by IP or URL
46 * @apiParam (Query Parameter) {String="asc","desc"} [direction] Direction to order the results by (ascending or descending)
47 * @apiParam (Query Parameter) {Integer{1...200}} [per_page=25] Number of results per request
48 * @apiParam (Query Parameter) {Integer} [page=0] Current page of results
49 * @apiParam (Query Parameter) {String="ip","url"} [groupBy] Group by IP or URL
50 */
51
52 /**
53 * @apiDefine 404List
54 *
55 * @apiSuccess {Object[]} items Array of 404 log objects
56 * @apiSuccess {Integer} items.id ID of 404 log entry
57 * @apiSuccess {String} items.created Date the 404 log entry was recorded
58 * @apiSuccess {Integer} items.created_time Unix time value for `created`
59 * @apiSuccess {Integer} items.url The requested URL that caused the 404 log entry
60 * @apiSuccess {String} items.agent User agent of the client initiating the request
61 * @apiSuccess {Integer} items.referrer Referrer of the client initiating the request
62 * @apiSuccess {Integer} total Number of items
63 *
64 * @apiSuccessExample {json} Success 200:
65 * HTTP/1.1 200 OK
66 * {
67 * "items": [
68 * {
69 * "id": 3,
70 * "created": "2019-01-01 12:12:00,
71 * "created_time": "12345678",
72 * "url": "/the-url",
73 * "agent": "FancyBrowser",
74 * "referrer": "http://site.com/previous/,
75 * }
76 * ],
77 * "total": 1
78 * }
79 */
80
81 /**
82 * @phpstan-type Log404Response array{
83 * items: list<array<string, mixed>|object>,
84 * total: int
85 * }
86 *
87 * 404 API endpoint
88 */
89 class Redirection_Api_404 extends Redirection_Api_Filter_Route {
90 /**
91 * 404 API endpoint constructor
92 *
93 * @param non-falsy-string $api_namespace Namespace.
94 */
95 public function __construct( $api_namespace ) {
96 $orders = [ 'url', 'ip', 'total', 'count', '' ];
97 $filters = [ 'ip', 'url-exact', 'referrer', 'agent', 'url', 'domain', 'method', 'http' ];
98
99 // GET /404 - List 404 logs
100 register_rest_route(
101 $api_namespace,
102 '/404',
103 [
104 [
105 'methods' => WP_REST_Server::READABLE,
106 'callback' => [ $this, 'route_404' ],
107 'permission_callback' => [ $this, 'permission_callback_manage' ],
108 'args' => $this->get_filter_args( $orders, $filters ),
109 ],
110 ]
111 );
112
113 // POST /bulk/404/:bulk - Bulk delete 404 logs
114 register_rest_route(
115 $api_namespace,
116 '/bulk/404/(?P<bulk>delete)',
117 [
118 [
119 'methods' => WP_REST_Server::EDITABLE,
120 'callback' => [ $this, 'route_bulk' ],
121 'permission_callback' => [ $this, 'permission_callback_delete' ],
122 'args' => array_merge(
123 $this->get_filter_args( $orders, $filters ),
124 [
125 'items' => [
126 'description' => 'Comma separated list of item IDs to perform action on',
127 'type' => 'array',
128 'items' => [
129 'description' => 'Item ID',
130 'type' => [ 'string', 'number' ],
131 ],
132 ],
133 ]
134 ),
135 ],
136 ]
137 );
138 }
139
140 /**
141 * Checks a manage capability
142 *
143 * @param WP_REST_Request<array<string, mixed>> $request Request.
144 * @return bool
145 */
146 public function permission_callback_manage( WP_REST_Request $request ) {
147 return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_404_MANAGE );
148 }
149
150 /**
151 * Checks a delete capability
152 *
153 * @param WP_REST_Request<array<string, mixed>> $request Request.
154 * @return bool
155 */
156 public function permission_callback_delete( WP_REST_Request $request ) {
157 return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_404_DELETE );
158 }
159
160 /**
161 * Get 404 log
162 *
163 * @param WP_REST_Request<array<string, mixed>> $request The request.
164 * @return Log404Response
165 */
166 public function route_404( WP_REST_Request $request ) {
167 return $this->get_404( $request->get_params() );
168 }
169
170 /**
171 * Perform action on 404s
172 *
173 * @param WP_REST_Request<array<string, mixed>> $request The request.
174 * @return Log404Response
175 */
176 public function route_bulk( WP_REST_Request $request ) {
177 $params = $request->get_params();
178
179 if ( isset( $params['items'] ) && is_array( $params['items'] ) && count( $params['items'] ) > 0 ) {
180 $items = $params['items'];
181
182 foreach ( $items as $item ) {
183 if ( is_numeric( $item ) ) {
184 Red_404_Log::delete( intval( $item, 10 ) );
185 } elseif ( isset( $params['groupBy'] ) ) {
186 $group_by = sanitize_text_field( $params['groupBy'] );
187 $delete_by = 'url-exact';
188
189 if ( in_array( $group_by, [ 'ip', 'agent' ], true ) ) {
190 $delete_by = $group_by;
191 }
192
193 Red_404_Log::delete_all( [ 'filterBy' => [ $delete_by => $item ] ] );
194 }
195 }
196
197 if ( isset( $params['groupBy'] ) && $params['groupBy'] === 'url-exact' ) {
198 unset( $params['groupBy'] );
199 }
200 } elseif ( isset( $params['global'] ) && $params['global'] !== false ) {
201 Red_404_Log::delete_all( $params );
202 }
203
204 return $this->get_404( $params );
205 }
206
207 /**
208 * Get 404 log
209 *
210 * @param array<string, mixed> $params The request parameters.
211 * @return Log404Response
212 */
213 private function get_404( array $params ) {
214 if ( isset( $params['groupBy'] ) && in_array( $params['groupBy'], [ 'ip', 'url', 'agent', 'url-exact' ], true ) ) {
215 $group_by = sanitize_text_field( $params['groupBy'] );
216 if ( $group_by === 'url-exact' ) {
217 $group_by = 'url';
218 }
219
220 return Red_404_Log::get_grouped( $group_by, $params );
221 }
222
223 return Red_404_Log::get_filtered( $params );
224 }
225 }
226