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 / api / api-redirect.php

api-redirect.php in Redirection 4.5, at api/api-redirect.php

263 lines 10.1 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/redirect Get redirects
5 * @apiName GetRedirects
6 * @apiDescription Get a paged list of redirects based after applying a set of filters and result ordering.
7 * @apiGroup Redirect
8 *
9 * @apiUse RedirectQueryParams
10 *
11 * @apiUse RedirectList
12 * @apiUse 401Error
13 * @apiUse 404Error
14 */
15
16 /**
17 * @api {post} /redirection/v1/redirect Create redirect
18 * @apiName CreateRedirect
19 * @apiDescription Create a new redirect, and return a paged list of redirects.
20 * @apiGroup Redirect
21 *
22 * @apiUse RedirectItem
23 * @apiUse RedirectQueryParams
24 *
25 * @apiUse RedirectList
26 * @apiUse 401Error
27 * @apiUse 404Error
28 * @apiError (Error 400) redirect_create_failed Failed to create redirect
29 * @apiErrorExample {json} 404 Error Response:
30 * HTTP/1.1 400 Bad Request
31 * {
32 * "code": "redirect_create_failed",
33 * "message": "Failed to create redirect"
34 * }
35 */
36
37 /**
38 * @api {post} /redirection/v1/redirect/:id Update redirect
39 * @apiName UpdateRedirect
40 * @apiDescription Update an existing redirect.
41 * @apiGroup Redirect
42 *
43 * @apiParam (URL) {Integer} :id Redirect ID to update
44 *
45 * @apiUse RedirectItem
46 *
47 * @apiUse RedirectList
48 * @apiUse 401Error
49 * @apiUse 404Error
50 * @apiError (Error 400) redirect_update_failed Failed to update redirect
51 * @apiErrorExample {json} 404 Error Response:
52 * HTTP/1.1 400 Bad Request
53 * {
54 * "code": "redirect_update_failed",
55 * "message": "Failed to update redirect"
56 * }
57 */
58
59 /**
60 * @api {post} /redirection/v1/bulk/redirect/:type Bulk redirect action
61 * @apiName BulkAction
62 * @apiDescription Enable, disable, and delete a set of redirects. The endpoint will return the next page of results after.
63 * performing the action, based on the supplied query parameters. This information can be used to refresh a list displayed to the client.
64 * @apiGroup Redirect
65 *
66 * @apiParam (URL) {String="delete","enable","disable"} :type Type of bulk action that is applied to every group ID.
67 *
68 * @apiParam (Query Parameter) {Integer[]} items Array of redirect IDs to perform the action on
69 * @apiUse RedirectQueryParams
70 *
71 * @apiUse RedirectList
72 * @apiUse 401Error
73 * @apiUse 404Error
74 * @apiUse 400MissingError
75 * @apiError (Error 400) redirect_invalid_items Invalid array of items
76 * @apiErrorExample {json} 404 Error Response:
77 * HTTP/1.1 400 Bad Request
78 * {
79 * "code": "redirect_invalid_items",
80 * "message": "Invalid array of items"
81 * }
82 */
83
84 /**
85 * @apiDefine RedirectItem Redirect
86 * All data associated with a redirect
87 *
88 * @apiParam {String="enabled","disabled"} status Status of the redirect
89 * @apiParam {Integer} position Redirect position, used to determine order multiple redirects occur
90 * @apiParam {Object} match_data Additional match parameters
91 * @apiParam {Object} match_data.source Match against the source
92 * @apiParam {Boolean} match_data.source.flag_regex `true` for regular expression, `false` otherwise
93 * @apiParam {String="ignore","exact","pass"} match_data.source.flag_query Which query parameter matching to use
94 * @apiParam {Boolean} match_data.source.flag_case] `true` for case insensitive matches, `false` otherwise
95 * @apiParam {Boolean} match_data.source.flag_trailing] `true` to ignore trailing slashes, `false` otherwise
96 * @apiParam {Boolean} regex True for regular expression, `false` otherwise
97 * @apiParam {String} url The source URL
98 * @apiParam {String="url","referrer","agent","login","header","custom","cookie","role","server","ip","page","language"} match_type What URL matching to use
99 * @apiParam {String} [title] A descriptive title for the redirect, or empty for no title
100 * @apiParam {Integer} group_id The group this redirect belongs to
101 * @apiParam {String} action_type What to do when the URL is matched
102 * @apiParam {Integer} action_code The HTTP code to return
103 * @apiParam {String} action_data Any data associated with the `action_type` and `match_type`. For example, the target URL
104 */
105
106 /**
107 * @apiDefine RedirectList A list of redirects
108 * A list of redirects
109 *
110 * @apiSuccess {Object[]} items Array of redirect objects
111 * @apiSuccess {Integer} items.id ID of redirect
112 * @apiSuccess {String} items.url Source URL to match
113 * @apiSuccess {String} items.match_url Match URL
114 * @apiSuccess {Object} items.match_data Match against the source
115 * @apiSuccess {String} items.match_type What URL matching to use
116 * @apiSuccess {String} items.action_type What to do when the URL is matched
117 * @apiSuccess {Integer} items.action_code The HTTP code to return
118 * @apiSuccess {String} items.action_data Any data associated with the action_type. For example, the target URL
119 * @apiSuccess {String} items.title Optional A descriptive title for the redirect, or empty for no title
120 * @apiSuccess {String} items.hits Number of hits this redirect has received
121 * @apiSuccess {String} items.regex True for regular expression, false otherwise
122 * @apiSuccess {String} items.group_id The group this redirect belongs to
123 * @apiSuccess {String} items.position Redirect position, used to determine order multiple redirects occur
124 * @apiSuccess {String} items.last_access The date this redirect was last hit
125 * @apiSuccess {String} items.status Status of the redirect
126 * @apiSuccess {Integer} total Number of items
127 *
128 * @apiSuccessExample {json} Success-Response:
129 * HTTP/1.1 200 OK
130 * {
131 * "items": [
132 * {
133 * id: 3,
134 * url: "/source",
135 * match_url: "/source",
136 * match_data: "",
137 * action_code: "",
138 * action_type: "",
139 * action_data: "",
140 * match_type: "url",
141 * title: "Redirect title",
142 * hits: 5,
143 * regex: true,
144 * group_id: 15,
145 * position: 1,
146 * last_access: "2019-01-01 01:01:01"
147 * status: "enabled"
148 * }
149 * ],
150 * "total": 1
151 * }
152 */
153
154 /**
155 * @apiDefine RedirectQueryParams
156 *
157 * @apiParam (Query Parameter) {String="enabled","disabled"} filterBy[status] Filter the results by the supplied status
158 * @apiParam (Query Parameter) {String} filterBy[url] Filter the results by the supplied URL
159 * @apiParam (Query Parameter) {String="regular","plain"} filterBy[url-match] Filter the results by `regular` expressions or non regular expressions
160 * @apiParam (Query Parameter) {String} filterBy[match] Filter the results by the supplied match type
161 * @apiParam (Query Parameter) {String} filterBy[action] Filter the results by the supplied action type
162 * @apiParam (Query Parameter) {Integer} filterBy[http] Filter the results by the supplied redirect HTTP code
163 * @apiParam (Query Parameter) {String="year","month","all"} filterBy[access] Filter the results by how long the redirect was last accessed
164 * @apiParam (Query Parameter) {String} filterBy[target] Filter the results by the supplied redirect target
165 * @apiParam (Query Parameter) {String} filterBy[title] Filter the results by the supplied redirect title
166 * @apiParam (Query Parameter) {Integer} filterBy[group] Filter the results by the supplied redirect group ID
167 * @apiParam (Query Parameter) {Integer="1","2","3"} filterBy[module] Filter the results by the supplied module ID
168 * @apiParam (Query Parameter) {String="url","last_count","last_access","position","id"} orderby Order in which results are returned
169 * @apiParam (Query Parameter) {String="asc","desc"} direction Direction to order the results by (ascending or descending)
170 * @apiParam (Query Parameter) {Integer{1...200}} per_page Number of results per request
171 * @apiParam (Query Parameter) {Integer} page Current page of results
172 */
173 class Redirection_Api_Redirect extends Redirection_Api_Filter_Route {
174 public function __construct( $namespace ) {
175 $orders = [ 'url', 'last_count', 'last_access', 'position', 'id' ];
176 $filters = [ 'status', 'url-match', 'match', 'action', 'http', 'access', 'url', 'target', 'title', 'group' ];
177
178 register_rest_route( $namespace, '/redirect', array(
179 'args' => $this->get_filter_args( $orders, $filters ),
180 $this->get_route( WP_REST_Server::READABLE, 'route_list' ),
181 $this->get_route( WP_REST_Server::EDITABLE, 'route_create' ),
182 ) );
183
184 register_rest_route( $namespace, '/redirect/(?P<id>[\d]+)', array(
185 $this->get_route( WP_REST_Server::EDITABLE, 'route_update' ),
186 ) );
187
188 $this->register_bulk( $namespace, '/bulk/redirect/(?P<bulk>delete|enable|disable|reset)', $orders, 'route_bulk' );
189 }
190
191 public function route_list( WP_REST_Request $request ) {
192 return Red_Item::get_filtered( $request->get_params() );
193 }
194
195 public function route_create( WP_REST_Request $request ) {
196 $params = $request->get_params();
197 $urls = array();
198
199 if ( isset( $params['url'] ) ) {
200 $urls = array( $params['url'] );
201
202 if ( is_array( $params['url'] ) ) {
203 $urls = $params['url'];
204 }
205
206 foreach ( $urls as $url ) {
207 $params['url'] = $url;
208 $redirect = Red_Item::create( $params );
209
210 if ( is_wp_error( $redirect ) ) {
211 return $this->add_error_details( $redirect, __LINE__ );
212 }
213 }
214 }
215
216 return $this->route_list( $request );
217 }
218
219 public function route_update( WP_REST_Request $request ) {
220 $params = $request->get_params();
221 $redirect = Red_Item::get_by_id( intval( $params['id'], 10 ) );
222
223 if ( $redirect ) {
224 $result = $redirect->update( $params );
225
226 if ( is_wp_error( $result ) ) {
227 return $this->add_error_details( $result, __LINE__ );
228 }
229
230 return array( 'item' => $redirect->to_json() );
231 }
232
233 return $this->add_error_details( new WP_Error( 'redirect_update_failed', 'Invalid redirect details' ), __LINE__ );
234 }
235
236 public function route_bulk( WP_REST_Request $request ) {
237 $action = $request['bulk'];
238 $items = explode( ',', $request['items'] );
239
240 if ( is_array( $items ) ) {
241 foreach ( $items as $item ) {
242 $redirect = Red_Item::get_by_id( intval( $item, 10 ) );
243
244 if ( $redirect ) {
245 if ( $action === 'delete' ) {
246 $redirect->delete();
247 } elseif ( $action === 'disable' ) {
248 $redirect->disable();
249 } elseif ( $action === 'enable' ) {
250 $redirect->enable();
251 } elseif ( $action === 'reset' ) {
252 $redirect->reset();
253 }
254 }
255 }
256
257 return $this->route_list( $request );
258 }
259
260 return $this->add_error_details( new WP_Error( 'redirect_invalid_items', 'Invalid array of items' ), __LINE__ );
261 }
262 }
263