PluginProbe
Redirection / 4.3.2
Redirection v4.3.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.3.2, at api/api-404.php

113 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} filter
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} filter
23 * @apiParam {string} filterBy
24 * @apiParam {string} groupBy Group by 'ip' or 'url'
25 */
26
27 /**
28 * @api {post} /redirection/v1/bulk/404/delete Bulk actions on 404s
29 * @apiDescription Delete 404 logs either by ID
30 * @apiGroup 404
31 */
32 class Redirection_Api_404 extends Redirection_Api_Filter_Route {
33 public function __construct( $namespace ) {
34 $filters = array( 'ip', 'url', 'url-exact', 'total' );
35
36 register_rest_route( $namespace, '/404', array(
37 'args' => $this->get_filter_args( $filters, $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)', $filters, $filters, '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 $filter = false;
79 $filter_by = false;
80
81 if ( isset( $params['items'] ) && is_array( $params['items'] ) ) {
82 foreach ( $params['items'] as $url ) {
83 RE_404::delete_all( $this->get_delete_group( $params ), $url );
84 }
85 } else {
86 if ( isset( $params['filter'] ) ) {
87 $filter = $params['filter'];
88 }
89
90 if ( isset( $params['filterBy'] ) ) {
91 $filter_by = $params['filterBy'];
92 }
93
94 RE_404::delete_all( $filter_by, $filter );
95
96 unset( $params['filterBy'] );
97 unset( $params['filter'] );
98 }
99
100 unset( $params['page'] );
101
102 return $this->get_404( $params );
103 }
104
105 private function get_404( array $params ) {
106 if ( isset( $params['groupBy'] ) && in_array( $params['groupBy'], array( 'ip', 'url' ), true ) ) {
107 return RE_Filter_Log::get_grouped( 'redirection_404', $params['groupBy'], $params );
108 }
109
110 return RE_Filter_Log::get( 'redirection_404', 'RE_404', $params );
111 }
112 }
113