| 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 |
* 404 API endpoint |
| 83 |
*/ |
| 84 |
class Redirection_Api_404 extends Redirection_Api_Filter_Route { |
| 85 |
/** |
| 86 |
* 404 API endpoint constructor |
| 87 |
* |
| 88 |
* @param String $namespace Namespace. |
| 89 |
*/ |
| 90 |
public function __construct( $namespace ) { |
| 91 |
$orders = [ 'url', 'ip', 'total', '' ]; |
| 92 |
$filters = [ 'ip', 'url-exact', 'referrer', 'agent', 'url', 'domain', 'method', 'http' ]; |
| 93 |
|
| 94 |
register_rest_route( $namespace, '/404', array( |
| 95 |
'args' => $this->get_filter_args( $orders, $filters ), |
| 96 |
$this->get_route( WP_REST_Server::READABLE, 'route_404', [ $this, 'permission_callback_manage' ] ), |
| 97 |
) ); |
| 98 |
|
| 99 |
register_rest_route( $namespace, '/bulk/404/(?P<bulk>delete)', array( |
| 100 |
$this->get_route( WP_REST_Server::EDITABLE, 'route_bulk', [ $this, 'permission_callback_delete' ] ), |
| 101 |
'args' => array_merge( $this->get_filter_args( $orders, $filters ), [ |
| 102 |
'items' => [ |
| 103 |
'description' => 'Comma separated list of item IDs to perform action on', |
| 104 |
'type' => 'array', |
| 105 |
'items' => [ |
| 106 |
'description' => 'Item ID', |
| 107 |
'type' => [ 'string', 'number' ], |
| 108 |
], |
| 109 |
], |
| 110 |
] ), |
| 111 |
) ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Checks a manage capability |
| 116 |
* |
| 117 |
* @param WP_REST_Request $request Request. |
| 118 |
* @return Bool |
| 119 |
*/ |
| 120 |
public function permission_callback_manage( WP_REST_Request $request ) { |
| 121 |
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_404_MANAGE ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Checks a delete capability |
| 126 |
* |
| 127 |
* @param WP_REST_Request $request Request. |
| 128 |
* @return Bool |
| 129 |
*/ |
| 130 |
public function permission_callback_delete( WP_REST_Request $request ) { |
| 131 |
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_404_DELETE ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Get 404 log |
| 136 |
* |
| 137 |
* @param WP_REST_Request $request The request. |
| 138 |
* @return WP_Error|array Return an array of results, or a WP_Error |
| 139 |
*/ |
| 140 |
public function route_404( WP_REST_Request $request ) { |
| 141 |
return $this->get_404( $request->get_params() ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Perform action on 404s |
| 146 |
* |
| 147 |
* @param WP_REST_Request $request The request. |
| 148 |
* @return WP_Error|array Return an array of results, or a WP_Error |
| 149 |
*/ |
| 150 |
public function route_bulk( WP_REST_Request $request ) { |
| 151 |
$params = $request->get_params(); |
| 152 |
|
| 153 |
if ( isset( $params['items'] ) && is_array( $params['items'] ) ) { |
| 154 |
$items = $params['items']; |
| 155 |
|
| 156 |
foreach ( $items as $item ) { |
| 157 |
if ( is_numeric( $item ) ) { |
| 158 |
Red_404_Log::delete( intval( $item, 10 ) ); |
| 159 |
} elseif ( isset( $params['groupBy'] ) ) { |
| 160 |
$delete_by = 'url-exact'; |
| 161 |
|
| 162 |
if ( in_array( $params['groupBy'], [ 'ip', 'agent' ], true ) ) { |
| 163 |
$delete_by = $params['groupBy']; |
| 164 |
} |
| 165 |
|
| 166 |
Red_404_Log::delete_all( [ 'filterBy' => [ $delete_by => $item ] ] ); |
| 167 |
} |
| 168 |
} |
| 169 |
} elseif ( isset( $params['global'] ) && $params['global'] ) { |
| 170 |
Red_404_Log::delete_all( $params ); |
| 171 |
} |
| 172 |
|
| 173 |
return $this->get_404( $params ); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Get 404 log |
| 178 |
* |
| 179 |
* @param array $params The request. |
| 180 |
* @return WP_Error|array Return an array of results, or a WP_Error |
| 181 |
*/ |
| 182 |
private function get_404( array $params ) { |
| 183 |
if ( isset( $params['groupBy'] ) && in_array( $params['groupBy'], [ 'ip', 'url', 'agent' ], true ) ) { |
| 184 |
return Red_404_Log::get_grouped( $params['groupBy'], $params ); |
| 185 |
} |
| 186 |
|
| 187 |
return Red_404_Log::get_filtered( $params ); |
| 188 |
} |
| 189 |
} |
| 190 |
|