PluginProbe
Redirection / 4.4
Redirection v4.4
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.4, at api/api-404.php

104 lines 3.0 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 * @apiDescription Get 404 logs
6 * @apiGroup 404
7 *
8 * @apiParam {string} groupBy Group by 'ip' or 'url'
9 * @apiParam {string} orderby
10 * @apiParam {string} direction
11 * @apiParam {string} filterBy
12 * @apiParam {string} per_page
13 * @apiParam {string} page
14 */
15
16 /**
17 * @api {post} /redirection/v1/404 Delete 404 logs
18 * @apiDescription Delete 404 logs either by ID or filter or group
19 * @apiGroup 404
20 *
21 * @apiParam {string} items Array of log IDs
22 * @apiParam {string} filterBy
23 * @apiParam {string} groupBy Group by 'ip' or 'url'
24 */
25
26 /**
27 * @api {post} /redirection/v1/bulk/404/delete Bulk actions on 404s
28 * @apiDescription Delete 404 logs either by ID
29 * @apiGroup 404
30 */
31 class Redirection_Api_404 extends Redirection_Api_Filter_Route {
32 public function __construct( $namespace ) {
33 $orders = [ 'url', 'ip', 'total' ];
34 $filters = [ 'ip', 'url-exact', 'referrer', 'agent', 'url' ];
35
36 register_rest_route( $namespace, '/404', array(
37 'args' => $this->get_filter_args( $orders, $filters ),
38 $this->get_route( WP_REST_Server::READABLE, 'route_404' ),
39 $this->get_route( WP_REST_Server::EDITABLE, 'route_delete_all' ),
40 ) );
41
42 $this->register_bulk( $namespace, '/bulk/404/(?P<bulk>delete)', $orders, 'route_bulk' );
43 }
44
45 public function route_404( WP_REST_Request $request ) {
46 return $this->get_404( $request->get_params() );
47 }
48
49 public function route_bulk( WP_REST_Request $request ) {
50 $params = $request->get_params();
51 $items = explode( ',', $request['items'] );
52
53 if ( is_array( $items ) ) {
54 foreach ( $items as $item ) {
55 if ( is_numeric( $item ) ) {
56 RE_404::delete( intval( $item, 10 ) );
57 } else {
58 RE_404::delete_all( $this->get_delete_group( $params ), $item );
59 }
60 }
61
62 return $this->route_404( $request );
63 }
64
65 return $this->add_error_details( new WP_Error( 'redirect', 'Invalid array of items' ), __LINE__ );
66 }
67
68 private function get_delete_group( array $params ) {
69 if ( isset( $params['groupBy'] ) && $params['groupBy'] === 'ip' ) {
70 return 'ip';
71 }
72
73 return 'url-exact';
74 }
75
76 public function route_delete_all( WP_REST_Request $request ) {
77 $params = $request->get_params();
78
79 if ( isset( $params['items'] ) && is_array( $params['items'] ) ) {
80 foreach ( $params['items'] as $url ) {
81 RE_404::delete_all( $this->get_delete_group( $params ), $url );
82 }
83 } else {
84 $first_filter = isset( $params['filterBy'] ) ? array_keys( $params['filterBy'] )[0] : false;
85
86 RE_404::delete_all( $first_filter ? $first_filter : false, $first_filter ? $params['filterBy'][ $first_filter ] : false );
87
88 unset( $params['filterBy'] );
89 }
90
91 unset( $params['page'] );
92
93 return $this->get_404( $params );
94 }
95
96 private function get_404( array $params ) {
97 if ( isset( $params['groupBy'] ) && in_array( $params['groupBy'], array( 'ip', 'url' ), true ) ) {
98 return RE_Filter_Log::get_grouped( 'redirection_404', $params['groupBy'], $params );
99 }
100
101 return RE_Filter_Log::get( 'redirection_404', 'RE_404', $params );
102 }
103 }
104