PluginProbe
Redirection / 4.7.1
Redirection v4.7.1
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.7.1, at api/api-redirect.php

305 lines 11.4 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 */
76
77 /**
78 * @apiDefine RedirectItem Redirect
79 * All data associated with a redirect
80 *
81 * @apiParam {String="enabled","disabled"} status Status of the redirect
82 * @apiParam {Integer} position Redirect position, used to determine order multiple redirects occur
83 * @apiParam {Object} match_data Additional match parameters
84 * @apiParam {Object} match_data.source Match against the source
85 * @apiParam {Boolean} match_data.source.flag_regex `true` for regular expression, `false` otherwise
86 * @apiParam {String="ignore","exact","pass"} match_data.source.flag_query Which query parameter matching to use
87 * @apiParam {Boolean} match_data.source.flag_case] `true` for case insensitive matches, `false` otherwise
88 * @apiParam {Boolean} match_data.source.flag_trailing] `true` to ignore trailing slashes, `false` otherwise
89 * @apiParam {Boolean} regex True for regular expression, `false` otherwise
90 * @apiParam {String} url The source URL
91 * @apiParam {String="url","referrer","agent","login","header","custom","cookie","role","server","ip","page","language"} match_type What URL matching to use
92 * @apiParam {String} [title] A descriptive title for the redirect, or empty for no title
93 * @apiParam {Integer} group_id The group this redirect belongs to
94 * @apiParam {String} action_type What to do when the URL is matched
95 * @apiParam {Integer} action_code The HTTP code to return
96 * @apiParam {String} action_data Any data associated with the `action_type` and `match_type`. For example, the target URL
97 */
98
99 /**
100 * @apiDefine RedirectList A list of redirects
101 * A list of redirects
102 *
103 * @apiSuccess {Object[]} items Array of redirect objects
104 * @apiSuccess {Integer} items.id ID of redirect
105 * @apiSuccess {String} items.url Source URL to match
106 * @apiSuccess {String} items.match_url Match URL
107 * @apiSuccess {Object} items.match_data Match against the source
108 * @apiSuccess {String} items.match_type What URL matching to use
109 * @apiSuccess {String} items.action_type What to do when the URL is matched
110 * @apiSuccess {Integer} items.action_code The HTTP code to return
111 * @apiSuccess {String} items.action_data Any data associated with the action_type. For example, the target URL
112 * @apiSuccess {String} items.title Optional A descriptive title for the redirect, or empty for no title
113 * @apiSuccess {String} items.hits Number of hits this redirect has received
114 * @apiSuccess {String} items.regex True for regular expression, false otherwise
115 * @apiSuccess {String} items.group_id The group this redirect belongs to
116 * @apiSuccess {String} items.position Redirect position, used to determine order multiple redirects occur
117 * @apiSuccess {String} items.last_access The date this redirect was last hit
118 * @apiSuccess {String} items.status Status of the redirect
119 * @apiSuccess {Integer} total Number of items
120 *
121 * @apiSuccessExample {json} Success-Response:
122 * HTTP/1.1 200 OK
123 * {
124 * "items": [
125 * {
126 * id: 3,
127 * url: "/source",
128 * match_url: "/source",
129 * match_data: "",
130 * action_code: "",
131 * action_type: "",
132 * action_data: "",
133 * match_type: "url",
134 * title: "Redirect title",
135 * hits: 5,
136 * regex: true,
137 * group_id: 15,
138 * position: 1,
139 * last_access: "2019-01-01 01:01:01"
140 * status: "enabled"
141 * }
142 * ],
143 * "total": 1
144 * }
145 */
146
147 /**
148 * @apiDefine RedirectQueryParams
149 *
150 * @apiParam (Query Parameter) {String="enabled","disabled"} filterBy[status] Filter the results by the supplied status
151 * @apiParam (Query Parameter) {String} filterBy[url] Filter the results by the supplied URL
152 * @apiParam (Query Parameter) {String="regular","plain"} filterBy[url-match] Filter the results by `regular` expressions or non regular expressions
153 * @apiParam (Query Parameter) {String} filterBy[match] Filter the results by the supplied match type
154 * @apiParam (Query Parameter) {String} filterBy[action] Filter the results by the supplied action type
155 * @apiParam (Query Parameter) {Integer} filterBy[http] Filter the results by the supplied redirect HTTP code
156 * @apiParam (Query Parameter) {String="year","month","all"} filterBy[access] Filter the results by how long the redirect was last accessed
157 * @apiParam (Query Parameter) {String} filterBy[target] Filter the results by the supplied redirect target
158 * @apiParam (Query Parameter) {String} filterBy[title] Filter the results by the supplied redirect title
159 * @apiParam (Query Parameter) {Integer} filterBy[group] Filter the results by the supplied redirect group ID
160 * @apiParam (Query Parameter) {Integer="1","2","3"} filterBy[module] Filter the results by the supplied module ID
161 * @apiParam (Query Parameter) {String="url","last_count","last_access","position","id"} orderby Order in which results are returned
162 * @apiParam (Query Parameter) {String="asc","desc"} direction Direction to order the results by (ascending or descending)
163 * @apiParam (Query Parameter) {Integer{1...200}} per_page Number of results per request
164 * @apiParam (Query Parameter) {Integer} page Current page of results
165 */
166 class Redirection_Api_Redirect extends Redirection_Api_Filter_Route {
167 public function __construct( $namespace ) {
168 $orders = [ 'url', 'last_count', 'last_access', 'position', 'id' ];
169 $filters = [ 'status', 'url-match', 'match', 'action', 'http', 'access', 'url', 'target', 'title', 'group' ];
170
171 register_rest_route( $namespace, '/redirect', array(
172 'args' => $this->get_filter_args( $orders, $filters ),
173 $this->get_route( WP_REST_Server::READABLE, 'route_list', [ $this, 'permission_callback_manage' ] ),
174 $this->get_route( WP_REST_Server::EDITABLE, 'route_create', [ $this, 'permission_callback_add' ] ),
175 ) );
176
177 register_rest_route( $namespace, '/redirect/(?P<id>[\d]+)', array(
178 $this->get_route( WP_REST_Server::EDITABLE, 'route_update', [ $this, 'permission_callback_add' ] ),
179 ) );
180
181 register_rest_route( $namespace, '/redirect/post', array(
182 $this->get_route( WP_REST_Server::READABLE, 'route_match_post', [ $this, 'permission_callback_manage' ] ),
183 'args' => [
184 'text' => [
185 'description' => 'Text to match',
186 'type' => 'string',
187 'required' => true,
188 ],
189 ],
190 ) );
191
192 $this->register_bulk( $namespace, '/bulk/redirect/(?P<bulk>delete|enable|disable|reset)', $orders, 'route_bulk', [ $this, 'permission_callback_bulk' ] );
193 }
194
195 public function permission_callback_manage( WP_REST_Request $request ) {
196 return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_REDIRECT_MANAGE );
197 }
198
199 public function permission_callback_bulk( WP_REST_Request $request ) {
200 if ( $request['bulk'] === 'delete' ) {
201 return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_REDIRECT_DELETE );
202 }
203
204 return $this->permission_callback_add( $request );
205 }
206
207 public function permission_callback_add( WP_REST_Request $request ) {
208 return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_REDIRECT_ADD );
209 }
210
211 public function route_list( WP_REST_Request $request ) {
212 return Red_Item::get_filtered( $request->get_params() );
213 }
214
215 public function route_create( WP_REST_Request $request ) {
216 $params = $request->get_params();
217 $urls = array();
218
219 if ( isset( $params['url'] ) ) {
220 $urls = array( $params['url'] );
221
222 if ( is_array( $params['url'] ) ) {
223 $urls = $params['url'];
224 }
225
226 foreach ( $urls as $url ) {
227 $params['url'] = $url;
228 $redirect = Red_Item::create( $params );
229
230 if ( is_wp_error( $redirect ) ) {
231 return $this->add_error_details( $redirect, __LINE__ );
232 }
233 }
234 }
235
236 return $this->route_list( $request );
237 }
238
239 public function route_update( WP_REST_Request $request ) {
240 $params = $request->get_params();
241 $redirect = Red_Item::get_by_id( intval( $params['id'], 10 ) );
242
243 if ( $redirect ) {
244 $result = $redirect->update( $params );
245
246 if ( is_wp_error( $result ) ) {
247 return $this->add_error_details( $result, __LINE__ );
248 }
249
250 return [ 'item' => $redirect->to_json() ];
251 }
252
253 return $this->add_error_details( new WP_Error( 'redirect_update_failed', 'Invalid redirect details' ), __LINE__ );
254 }
255
256 public function route_bulk( WP_REST_Request $request ) {
257 $action = $request['bulk'];
258 $items = $request['items'];
259
260 foreach ( $items as $item ) {
261 $redirect = Red_Item::get_by_id( intval( $item, 10 ) );
262
263 if ( $redirect ) {
264 if ( $action === 'delete' ) {
265 $redirect->delete();
266 } elseif ( $action === 'disable' ) {
267 $redirect->disable();
268 } elseif ( $action === 'enable' ) {
269 $redirect->enable();
270 } elseif ( $action === 'reset' ) {
271 $redirect->reset();
272 }
273 }
274 }
275
276 return $this->route_list( $request );
277 }
278
279 public function route_match_post( WP_REST_Request $request ) {
280 global $wpdb;
281
282 $params = $request->get_params();
283 $search = $params['text'];
284 $results = [];
285
286 $posts = $wpdb->get_results(
287 $wpdb->prepare(
288 "SELECT ID,post_title,post_name FROM $wpdb->posts WHERE post_status='publish' AND (post_title LIKE %s OR post_name LIKE %s) " .
289 "AND post_type NOT IN ('nav_menu_item','wp_block','oembed_cache')",
290 '%' . $wpdb->esc_like( $search ) . '%', '%' . $wpdb->esc_like( $search ) . '%'
291 )
292 );
293
294 foreach ( (array) $posts as $post ) {
295 $results[] = [
296 'title' => $post->post_title,
297 'slug' => $post->post_name,
298 'url' => get_permalink( $post->ID ),
299 ];
300 }
301
302 return $results;
303 }
304 }
305