| 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 |
*/ |
| 50 |
|
| 51 |
/** |
| 52 |
* @apiDefine 404QueryParams 404 log query parameters |
| 53 |
* |
| 54 |
* @apiParam (Query Parameter) {String} filterBy[ip] Filter the results by the supplied IP |
| 55 |
* @apiParam (Query Parameter) {String} filterBy[url] Filter the results by the supplied URL |
| 56 |
* @apiParam (Query Parameter) {String} filterBy[url-exact] Filter the results by the exact URL (not a substring match, as per `url`) |
| 57 |
* @apiParam (Query Parameter) {String} filterBy[referrer] Filter the results by the supplied referrer |
| 58 |
* @apiParam (Query Parameter) {String} filterBy[agent] Filter the results by the supplied user agent |
| 59 |
* @apiParam (Query Parameter) {String} filterBy[target] Filter the results by the supplied redirect target |
| 60 |
* @apiParam (Query Parameter) {string="ip","url"} orderby Order by IP or URL |
| 61 |
* @apiParam (Query Parameter) {String="asc","desc"} direction Direction to order the results by (ascending or descending) |
| 62 |
* @apiParam (Query Parameter) {Integer{1...200}} per_page Number of results per request |
| 63 |
* @apiParam (Query Parameter) {Integer} page Current page of results |
| 64 |
* @apiParam (Query Parameter) {String="ip","url"} groupBy Group by IP or URL |
| 65 |
*/ |
| 66 |
|
| 67 |
/** |
| 68 |
* @apiDefine 404List |
| 69 |
* |
| 70 |
* @apiSuccess {Object[]} items Array of 404 log objects |
| 71 |
* @apiSuccess {Integer} items.id ID of 404 log entry |
| 72 |
* @apiSuccess {String} items.created Date the 404 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 404 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 |
class Redirection_Api_404 extends Redirection_Api_Filter_Route { |
| 96 |
public function __construct( $namespace ) { |
| 97 |
$orders = [ 'url', 'ip', 'total' ]; |
| 98 |
$filters = [ 'ip', 'url-exact', 'referrer', 'agent', 'url' ]; |
| 99 |
|
| 100 |
register_rest_route( $namespace, '/404', array( |
| 101 |
'args' => $this->get_filter_args( $orders, $filters ), |
| 102 |
$this->get_route( WP_REST_Server::READABLE, 'route_404', [ $this, 'permission_callback_manage' ] ), |
| 103 |
$this->get_route( WP_REST_Server::EDITABLE, 'route_delete_all', [ $this, 'permission_callback_delete' ] ), |
| 104 |
) ); |
| 105 |
|
| 106 |
register_rest_route( $namespace, '/bulk/404/(?P<bulk>delete)', array( |
| 107 |
$this->get_route( WP_REST_Server::EDITABLE, 'route_bulk', [ $this, 'permission_callback_delete' ] ), |
| 108 |
'args' => array_merge( $this->get_filter_args( $orders ), [ |
| 109 |
'items' => [ |
| 110 |
'description' => 'Comma separated list of item IDs to perform action on', |
| 111 |
'type' => 'string', |
| 112 |
'required' => true, |
| 113 |
'sanitize_callback' => [ $this, 'sanitize_bulk' ], |
| 114 |
], |
| 115 |
] ), |
| 116 |
) ); |
| 117 |
} |
| 118 |
|
| 119 |
public function sanitize_bulk( $param ) { |
| 120 |
return explode( ',', $param ); |
| 121 |
} |
| 122 |
|
| 123 |
public function permission_callback_manage( WP_REST_Request $request ) { |
| 124 |
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_404_MANAGE ); |
| 125 |
} |
| 126 |
|
| 127 |
public function permission_callback_delete( WP_REST_Request $request ) { |
| 128 |
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_404_DELETE ); |
| 129 |
} |
| 130 |
|
| 131 |
public function route_404( WP_REST_Request $request ) { |
| 132 |
return $this->get_404( $request->get_params() ); |
| 133 |
} |
| 134 |
|
| 135 |
public function route_bulk( WP_REST_Request $request ) { |
| 136 |
$params = $request->get_params(); |
| 137 |
$items = $request['items']; |
| 138 |
|
| 139 |
foreach ( $items as $item ) { |
| 140 |
if ( is_numeric( $item ) ) { |
| 141 |
RE_404::delete( intval( $item, 10 ) ); |
| 142 |
} else { |
| 143 |
RE_404::delete_all( $this->get_delete_group( $params ), $item ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
return $this->route_404( $request ); |
| 148 |
} |
| 149 |
|
| 150 |
private function get_delete_group( array $params ) { |
| 151 |
if ( isset( $params['groupBy'] ) && $params['groupBy'] === 'ip' ) { |
| 152 |
return 'ip'; |
| 153 |
} |
| 154 |
|
| 155 |
return 'url-exact'; |
| 156 |
} |
| 157 |
|
| 158 |
public function route_delete_all( WP_REST_Request $request ) { |
| 159 |
$params = $request->get_params(); |
| 160 |
|
| 161 |
if ( isset( $params['items'] ) && is_array( $params['items'] ) ) { |
| 162 |
foreach ( $params['items'] as $url ) { |
| 163 |
RE_404::delete_all( $this->get_delete_group( $params ), $url ); |
| 164 |
} |
| 165 |
} else { |
| 166 |
$first_filter = isset( $params['filterBy'] ) ? array_keys( $params['filterBy'] )[0] : false; |
| 167 |
|
| 168 |
RE_404::delete_all( $first_filter ? $first_filter : false, $first_filter ? $params['filterBy'][ $first_filter ] : false ); |
| 169 |
|
| 170 |
unset( $params['filterBy'] ); |
| 171 |
} |
| 172 |
|
| 173 |
unset( $params['page'] ); |
| 174 |
|
| 175 |
return $this->get_404( $params ); |
| 176 |
} |
| 177 |
|
| 178 |
private function get_404( array $params ) { |
| 179 |
if ( isset( $params['groupBy'] ) && in_array( $params['groupBy'], array( 'ip', 'url' ), true ) ) { |
| 180 |
return RE_Filter_Log::get_grouped( 'redirection_404', $params['groupBy'], $params ); |
| 181 |
} |
| 182 |
|
| 183 |
return RE_Filter_Log::get( 'redirection_404', 'RE_404', $params ); |
| 184 |
} |
| 185 |
} |
| 186 |
|