PluginProbe
Redirection / 4.5
Redirection v4.5
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 / redirection-api.php

redirection-api.php in Redirection 4.5, at redirection-api.php

138 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 include_once dirname( __FILE__ ) . '/api/api-group.php';
4 include_once dirname( __FILE__ ) . '/api/api-redirect.php';
5 include_once dirname( __FILE__ ) . '/api/api-log.php';
6 include_once dirname( __FILE__ ) . '/api/api-404.php';
7 include_once dirname( __FILE__ ) . '/api/api-settings.php';
8 include_once dirname( __FILE__ ) . '/api/api-plugin.php';
9 include_once dirname( __FILE__ ) . '/api/api-import.php';
10 include_once dirname( __FILE__ ) . '/api/api-export.php';
11
12 define( 'REDIRECTION_API_NAMESPACE', 'redirection/v1' );
13
14 class Redirection_Api_Route {
15 protected function add_error_details( WP_Error $error, $line, $code = 400 ) {
16 global $wpdb;
17
18 $data = array(
19 'status' => $code,
20 'error_code' => $line,
21 );
22
23 if ( isset( $wpdb->last_error ) && $wpdb->last_error ) {
24 $data['wpdb'] = $wpdb->last_error;
25 }
26
27 $error->add_data( $data );
28 return $error;
29 }
30
31 public function permission_callback( WP_REST_Request $request ) {
32 return current_user_can( apply_filters( 'redirection_role', 'manage_options' ) );
33 }
34
35 public function get_route( $method, $callback ) {
36 return array(
37 'methods' => $method,
38 'callback' => array( $this, $callback ),
39 'permission_callback' => array( $this, 'permission_callback' ),
40 );
41 }
42 }
43
44 class Redirection_Api_Filter_Route extends Redirection_Api_Route {
45 public function validate_filter( $value, $request, $param ) {
46 $fields = $request->get_attributes()['args']['filterBy']['filter_fields'];
47
48 if ( ! is_array( $value ) ) {
49 return new WP_Error( 'rest_invalid_param', 'Filter is not an array', array( 'status' => 400 ) );
50 }
51
52 if ( ! empty( $fields ) ) {
53 foreach ( array_keys( $value ) as $key ) {
54 if ( ! in_array( $key, $fields, true ) ) {
55 return new WP_Error( 'rest_invalid_param', 'Filter type is not supported: ' . $key, array( 'status' => 400 ) );
56 }
57 }
58 }
59
60 return true;
61 }
62
63 protected function get_filter_args( $order_fields, $filters = [] ) {
64 return array(
65 'filterBy' => array(
66 'description' => 'Field to filter by',
67 'validate_callback' => [ $this, 'validate_filter' ],
68 'filter_fields' => $filters,
69 ),
70 'orderby' => array(
71 'description' => 'Field to order results by',
72 'type' => 'enum',
73 'enum' => $order_fields,
74 ),
75 'direction' => array(
76 'description' => 'Direction of ordered results',
77 'type' => 'enum',
78 'default' => 'desc',
79 'enum' => array( 'asc', 'desc' ),
80 ),
81 'per_page' => array(
82 'description' => 'Number of results per page',
83 'type' => 'integer',
84 'default' => 25,
85 'minimum' => 5,
86 'maximum' => RED_MAX_PER_PAGE,
87 ),
88 'page' => array(
89 'description' => 'Page offset',
90 'type' => 'integer',
91 'minimum' => 0,
92 'default' => 0,
93 ),
94 );
95 }
96
97 public function register_bulk( $namespace, $route, $orders, $callback ) {
98 register_rest_route( $namespace, $route, array(
99 $this->get_route( WP_REST_Server::EDITABLE, $callback ),
100 'args' => array_merge( $this->get_filter_args( $orders ), array(
101 'items' => array(
102 'description' => 'Comma separated list of item IDs to perform action on',
103 'type' => 'string|integer',
104 'required' => true,
105 ),
106 ) ),
107 ) );
108 }
109 }
110
111 class Redirection_Api {
112 private static $instance = null;
113 private $routes = array();
114
115 static function init() {
116 if ( is_null( self::$instance ) ) {
117 self::$instance = new Redirection_Api();
118 }
119
120 return self::$instance;
121 }
122
123 public function __construct() {
124 global $wpdb;
125
126 $wpdb->hide_errors();
127
128 $this->routes[] = new Redirection_Api_Redirect( REDIRECTION_API_NAMESPACE );
129 $this->routes[] = new Redirection_Api_Group( REDIRECTION_API_NAMESPACE );
130 $this->routes[] = new Redirection_Api_Log( REDIRECTION_API_NAMESPACE );
131 $this->routes[] = new Redirection_Api_404( REDIRECTION_API_NAMESPACE );
132 $this->routes[] = new Redirection_Api_Settings( REDIRECTION_API_NAMESPACE );
133 $this->routes[] = new Redirection_Api_Plugin( REDIRECTION_API_NAMESPACE );
134 $this->routes[] = new Redirection_Api_Import( REDIRECTION_API_NAMESPACE );
135 $this->routes[] = new Redirection_Api_Export( REDIRECTION_API_NAMESPACE );
136 }
137 }
138