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-404.php

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

183 lines 6.9 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/404 Delete 404 logs
17 * @apiName DeleteLogs
18 * @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.
19 * performing the action, based on the supplied query parameters. This information can be used to refresh a list displayed to the client.
20 * @apiGroup 404
21 *
22 * @apiParam (Query Parameter) {String} filterBy[ip] Filter the results by the supplied IP
23 * @apiParam (Query Parameter) {String} filterBy[url] Filter the results by the supplied URL
24 * @apiParam (Query Parameter) {String} filterBy[url-exact] Filter the results by the exact URL (not a substring match, as per `url`)
25 * @apiParam (Query Parameter) {String} filterBy[referrer] Filter the results by the supplied referrer
26 * @apiParam (Query Parameter) {String} filterBy[agent] Filter the results by the supplied user agent
27 * @apiParam (Query Parameter) {String} filterBy[target] Filter the results by the supplied redirect target
28 *
29 * @apiUse 404List
30 * @apiUse 401Error
31 * @apiUse 404Error
32 */
33
34 /**
35 * @api {post} /redirection/v1/bulk/404/:type Bulk 404 action
36 * @apiName BulkAction
37 * @apiDescription Delete 404 logs by ID
38 * @apiGroup 404
39 *
40 * @apiParam (URL) {String="delete"} :type Type of bulk action that is applied to every log ID.
41 *
42 * @apiParam (Query Parameter) {Integer[]} items Array of group IDs to perform the action on
43 * @apiUse 404QueryParams
44 *
45 * @apiUse 404List
46 * @apiUse 401Error
47 * @apiUse 404Error
48 * @apiUse 400MissingError
49 * @apiError (Error 400) redirect_404_invalid_items Invalid array of items
50 * @apiErrorExample {json} 404 Error Response:
51 * HTTP/1.1 400 Bad Request
52 * {
53 * "code": "redirect_404_invalid_items",
54 * "message": "Invalid array of items"
55 * }
56 */
57
58 /**
59 * @apiDefine 404QueryParams 404 log query parameters
60 *
61 * @apiParam (Query Parameter) {String} filterBy[ip] Filter the results by the supplied IP
62 * @apiParam (Query Parameter) {String} filterBy[url] Filter the results by the supplied URL
63 * @apiParam (Query Parameter) {String} filterBy[url-exact] Filter the results by the exact URL (not a substring match, as per `url`)
64 * @apiParam (Query Parameter) {String} filterBy[referrer] Filter the results by the supplied referrer
65 * @apiParam (Query Parameter) {String} filterBy[agent] Filter the results by the supplied user agent
66 * @apiParam (Query Parameter) {String} filterBy[target] Filter the results by the supplied redirect target
67 * @apiParam (Query Parameter) {string="ip","url"} orderby Order by IP or URL
68 * @apiParam (Query Parameter) {String="asc","desc"} direction Direction to order the results by (ascending or descending)
69 * @apiParam (Query Parameter) {Integer{1...200}} per_page Number of results per request
70 * @apiParam (Query Parameter) {Integer} page Current page of results
71 * @apiParam (Query Parameter) {String="ip","url"} groupBy Group by IP or URL
72 */
73
74 /**
75 * @apiDefine 404List
76 *
77 * @apiSuccess {Object[]} items Array of 404 log objects
78 * @apiSuccess {Integer} items.id ID of 404 log entry
79 * @apiSuccess {String} items.created Date the 404 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 404 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 class Redirection_Api_404 extends Redirection_Api_Filter_Route {
103 public function __construct( $namespace ) {
104 $orders = [ 'url', 'ip', 'total' ];
105 $filters = [ 'ip', 'url-exact', 'referrer', 'agent', 'url' ];
106
107 register_rest_route( $namespace, '/404', array(
108 'args' => $this->get_filter_args( $orders, $filters ),
109 $this->get_route( WP_REST_Server::READABLE, 'route_404', [ $this, 'permission_callback_manage' ] ),
110 $this->get_route( WP_REST_Server::EDITABLE, 'route_delete_all', [ $this, 'permission_callback_delete' ] ),
111 ) );
112
113 $this->register_bulk( $namespace, '/bulk/404/(?P<bulk>delete)', $orders, 'route_bulk', [ $this, 'permission_callback_delete' ] );
114 }
115
116 public function permission_callback_manage( WP_REST_Request $request ) {
117 return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_404_MANAGE );
118 }
119
120 public function permission_callback_delete( WP_REST_Request $request ) {
121 return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_404_DELETE );
122 }
123
124 public function route_404( WP_REST_Request $request ) {
125 return $this->get_404( $request->get_params() );
126 }
127
128 public function route_bulk( WP_REST_Request $request ) {
129 $params = $request->get_params();
130 $items = explode( ',', $request['items'] );
131
132 if ( is_array( $items ) ) {
133 foreach ( $items as $item ) {
134 if ( is_numeric( $item ) ) {
135 RE_404::delete( intval( $item, 10 ) );
136 } else {
137 RE_404::delete_all( $this->get_delete_group( $params ), $item );
138 }
139 }
140
141 return $this->route_404( $request );
142 }
143
144 return $this->add_error_details( new WP_Error( 'redirect_404_invalid_items', 'Invalid array of items' ), __LINE__ );
145 }
146
147 private function get_delete_group( array $params ) {
148 if ( isset( $params['groupBy'] ) && $params['groupBy'] === 'ip' ) {
149 return 'ip';
150 }
151
152 return 'url-exact';
153 }
154
155 public function route_delete_all( WP_REST_Request $request ) {
156 $params = $request->get_params();
157
158 if ( isset( $params['items'] ) && is_array( $params['items'] ) ) {
159 foreach ( $params['items'] as $url ) {
160 RE_404::delete_all( $this->get_delete_group( $params ), $url );
161 }
162 } else {
163 $first_filter = isset( $params['filterBy'] ) ? array_keys( $params['filterBy'] )[0] : false;
164
165 RE_404::delete_all( $first_filter ? $first_filter : false, $first_filter ? $params['filterBy'][ $first_filter ] : false );
166
167 unset( $params['filterBy'] );
168 }
169
170 unset( $params['page'] );
171
172 return $this->get_404( $params );
173 }
174
175 private function get_404( array $params ) {
176 if ( isset( $params['groupBy'] ) && in_array( $params['groupBy'], array( 'ip', 'url' ), true ) ) {
177 return RE_Filter_Log::get_grouped( 'redirection_404', $params['groupBy'], $params );
178 }
179
180 return RE_Filter_Log::get( 'redirection_404', 'RE_404', $params );
181 }
182 }
183