PluginProbe
Redirection / 4.5.1
Redirection v4.5.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-group.php

api-group.php in Redirection 4.5.1, at api/api-group.php

230 lines 7.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/group Get groups
5 * @apiName GetGroups
6 * @apiDescription Get a paged list of groups based after applying a set of filters and result ordering.
7 * @apiGroup Group
8 *
9 * @apiUse GroupQueryParams
10 *
11 * @apiUse GroupList
12 * @apiUse 401Error
13 * @apiUse 404Error
14 */
15
16 /**
17 * @api {post} /redirection/v1/group Create group
18 * @apiName CreateGroup
19 * @apiDescription Create a new group, and return a paged list of groups.
20 * @apiGroup Group
21 *
22 * @apiUse GroupItem
23 * @apiUse GroupQueryParams
24 *
25 * @apiUse GroupList
26 * @apiUse 401Error
27 * @apiUse 404Error
28 * @apiError (Error 400) redirect_group_invalid Invalid group or parameters
29 * @apiErrorExample {json} 404 Error Response:
30 * HTTP/1.1 400 Bad Request
31 * {
32 * "code": "redirect_group_invalid",
33 * "message": "Invalid group or parameters"
34 * }
35 */
36
37 /**
38 * @api {post} /redirection/v1/group/:id Update group
39 * @apiName UpdateGroup
40 * @apiDescription Update an existing group.
41 * @apiGroup Group
42 *
43 * @apiParam (URL) {Integer} :id Group ID to update
44 *
45 * @apiSuccess {String} item The updated group
46 * @apiSuccess {Integer} item.id ID of group
47 * @apiSuccess {String} item.name Name of this group
48 * @apiSuccess {Boolean} item.enabled `true` if group (and redirects) are enabled, `false` otherwise
49 * @apiSuccess {Integer} item.redirects Number of redirects in this group
50 * @apiSuccess {String} item.moduleName Name of the module this group belongs to
51 * @apiSuccess {Integer} item.module_id ID of the module this group belongs to
52 * @apiUse 401Error
53 * @apiUse 404Error
54 * @apiError (Error 400) redirect_group_invalid Invalid group or parameters
55 * @apiErrorExample {json} 404 Error Response:
56 * HTTP/1.1 400 Bad Request
57 * {
58 * "code": "redirect_group_invalid",
59 * "message": "Invalid group or parameters"
60 * }
61 */
62
63 /**
64 * @api {post} /redirection/v1/bulk/group/:type Bulk group action
65 * @apiName BulkAction
66 * @apiDescription Enable, disable, and delete a set of groups. The endpoint will return the next page of results after.
67 * performing the action, based on the supplied query parameters. This information can be used to refresh a list displayed to the client.
68 * @apiGroup Group
69 *
70 * @apiParam (URL) {String="delete","enable","disable"} :type Type of bulk action that is applied to every group ID.
71 * Enabling or disabling a group will also enable or disable all redirects in that group
72 *
73 * @apiParam (Query Parameter) {Integer[]} items Array of group IDs to perform the action on
74 * @apiUse GroupQueryParams
75 *
76 * @apiUse GroupList
77 * @apiUse 401Error
78 * @apiUse 404Error
79 * @apiUse 400MissingError
80 * @apiError (Error 400) redirect_group_invalid_items Invalid array of items
81 * @apiErrorExample {json} 404 Error Response:
82 * HTTP/1.1 400 Bad Request
83 * {
84 * "code": "redirect_group_invalid_items",
85 * "message": "Invalid array of items"
86 * }
87 */
88
89 /**
90 * @apiDefine GroupQueryParams
91 *
92 * @apiParam (Query Parameter) {String} filterBy[name] Filter the results by the supplied name
93 * @apiParam (Query Parameter) {String="enabled","disabled"} filterBy[status] Filter the results by the supplied status
94 * @apiParam (Query Parameter) {Integer="1","2","3"} filterBy[module] Filter the results by the supplied module ID
95 * @apiParam (Query Parameter) {String="name"} orderby Order in which results are returned
96 * @apiParam (Query Parameter) {String="asc","desc"} direction Direction to order the results by (ascending or descending)
97 * @apiParam (Query Parameter) {Integer{1...200}} per_page Number of results per request
98 * @apiParam (Query Parameter) {Integer} page Current page of results
99 */
100
101 /**
102 * @apiDefine GroupItem
103 *
104 * @apiParam (JSON Body) {String} name Name of the group
105 * @apiParam (JSON Body) {Integer="1","2","3"} moduleID Module ID of the group, with 1 being WordPress, 2 is Apache, and 3 is Nginx
106 */
107
108 /**
109 * @apiDefine GroupList
110 *
111 * @apiSuccess {Object[]} items Array of group objects
112 * @apiSuccess {Integer} items.id ID of group
113 * @apiSuccess {String} items.name Name of this group
114 * @apiSuccess {Boolean} items.enabled `true` if group (and redirects) are enabled, `false` otherwise
115 * @apiSuccess {Integer} items.redirects Number of redirects in this group
116 * @apiSuccess {String} items.moduleName Name of the module this group belongs to
117 * @apiSuccess {Integer} items.module_id ID of the module this group belongs to
118 * @apiSuccess {Integer} total Number of items
119 *
120 * @apiSuccessExample {json} Success 200:
121 * HTTP/1.1 200 OK
122 * {
123 * "items": [
124 * {
125 * "id": 3,
126 * "enabled": true,
127 * "moduleName": "WordPress",
128 * "module_id": 1,
129 * "name": "Redirections",
130 * "redirects": 0,
131 * }
132 * ],
133 * "total": 1
134 * }
135 */
136 class Redirection_Api_Group extends Redirection_Api_Filter_Route {
137 public function __construct( $namespace ) {
138 $orders = [ 'name', 'id' ];
139 $filters = [ 'status', 'module', 'name' ];
140
141 register_rest_route( $namespace, '/group', array(
142 'args' => $this->get_filter_args( $orders, $filters ),
143 $this->get_route( WP_REST_Server::READABLE, 'route_list' ),
144 array_merge(
145 $this->get_route( WP_REST_Server::EDITABLE, 'route_create' ),
146 array( 'args' => $this->get_group_args() )
147 ),
148 ) );
149
150 register_rest_route( $namespace, '/group/(?P<id>[\d]+)', array(
151 'args' => $this->get_group_args(),
152 $this->get_route( WP_REST_Server::EDITABLE, 'route_update' ),
153 ) );
154
155 $this->register_bulk( $namespace, '/bulk/group/(?P<bulk>delete|enable|disable)', $orders, 'route_bulk' );
156 }
157
158 private function get_group_args() {
159 return array(
160 'moduleId' => array(
161 'description' => 'Module ID',
162 'type' => 'integer',
163 'minimum' => 0,
164 'maximum' => 3,
165 'required' => true,
166 ),
167 'name' => array(
168 'description' => 'Group name',
169 'type' => 'string',
170 'required' => true,
171 ),
172 );
173 }
174
175 public function route_list( WP_REST_Request $request ) {
176 return Red_Group::get_filtered( $request->get_params() );
177 }
178
179 public function route_create( WP_REST_Request $request ) {
180 $params = $request->get_params( $request );
181 $group = Red_Group::create( isset( $params['name'] ) ? $params['name'] : '', isset( $params['moduleId'] ) ? $params['moduleId'] : 0 );
182
183 if ( $group ) {
184 return Red_Group::get_filtered( $params );
185 }
186
187 return $this->add_error_details( new WP_Error( 'redirect_group_invalid', 'Invalid group or parameters' ), __LINE__ );
188 }
189
190 public function route_update( WP_REST_Request $request ) {
191 $params = $request->get_params( $request );
192 $group = Red_Group::get( intval( $request['id'], 10 ) );
193
194 if ( $group ) {
195 $result = $group->update( $params );
196
197 if ( $result ) {
198 return array( 'item' => $group->to_json() );
199 }
200 }
201
202 return $this->add_error_details( new WP_Error( 'redirect_group_invalid', 'Invalid group details' ), __LINE__ );
203 }
204
205 public function route_bulk( WP_REST_Request $request ) {
206 $action = $request['bulk'];
207 $items = explode( ',', $request['items'] );
208
209 if ( is_array( $items ) ) {
210 foreach ( $items as $item ) {
211 $group = Red_Group::get( intval( $item, 10 ) );
212
213 if ( $group ) {
214 if ( $action === 'delete' ) {
215 $group->delete();
216 } elseif ( $action === 'disable' ) {
217 $group->disable();
218 } elseif ( $action === 'enable' ) {
219 $group->enable();
220 }
221 }
222 }
223
224 return $this->route_list( $request );
225 }
226
227 return $this->add_error_details( new WP_Error( 'redirect_group_invalid_items', 'Invalid array of items' ), __LINE__ );
228 }
229 }
230