| 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 |
* @apiUse GroupList |
| 45 |
* |
| 46 |
* @apiSuccess {String} item The updated group |
| 47 |
* @apiSuccess {Integer} item.id ID of group |
| 48 |
* @apiSuccess {String} item.name Name of this group |
| 49 |
* @apiSuccess {Boolean} item.enabled `true` if group (and redirects) are enabled, `false` otherwise |
| 50 |
* @apiSuccess {Integer} item.redirects Number of redirects in this group |
| 51 |
* @apiSuccess {String} item.moduleName Name of the module this group belongs to |
| 52 |
* @apiSuccess {Integer} item.module_id ID of the module this group belongs to |
| 53 |
* |
| 54 |
* @apiUse 401Error |
| 55 |
* @apiUse 404Error |
| 56 |
* |
| 57 |
* @apiError (Error 400) redirect_group_invalid Invalid group or parameters |
| 58 |
* @apiErrorExample {json} 404 Error Response: |
| 59 |
* HTTP/1.1 400 Bad Request |
| 60 |
* { |
| 61 |
* "code": "redirect_group_invalid", |
| 62 |
* "message": "Invalid group or parameters" |
| 63 |
* } |
| 64 |
*/ |
| 65 |
|
| 66 |
/** |
| 67 |
* @api {post} /redirection/v1/bulk/group/:type Bulk action |
| 68 |
* @apiName BulkAction |
| 69 |
* @apiDescription Enable, disable, and delete a set of groups. The endpoint will return the next page of results after. |
| 70 |
* performing the action, based on the supplied query parameters. This information can be used to refresh a list displayed to the client. |
| 71 |
* @apiGroup Group |
| 72 |
* |
| 73 |
* @apiParam (URL) {String="delete","enable","disable"} :type Type of bulk action that is applied to every group ID. |
| 74 |
* Enabling or disabling a group will also enable or disable all redirects in that group |
| 75 |
* |
| 76 |
* @apiParam (Query Parameter) {String[]} [items] Array of group IDs to perform the action on |
| 77 |
* @apiParam (Query Parameter) {Boolean=false} [global] Perform action globally using the filter parameters |
| 78 |
* @apiUse GroupQueryParams |
| 79 |
* |
| 80 |
* @apiUse GroupList |
| 81 |
* @apiUse 401Error |
| 82 |
* @apiUse 404Error |
| 83 |
* @apiUse 400MissingError |
| 84 |
*/ |
| 85 |
|
| 86 |
/** |
| 87 |
* @apiDefine GroupQueryParams |
| 88 |
* |
| 89 |
* @apiParam (Query Parameter) {String} [filterBy[name]] Filter the results by the supplied name |
| 90 |
* @apiParam (Query Parameter) {String="enabled","disabled"} [filterBy[status]] Filter the results by the supplied status |
| 91 |
* @apiParam (Query Parameter) {Integer="1","2","3"} [filterBy[module]] Filter the results by the supplied module ID |
| 92 |
* @apiParam (Query Parameter) {String="name"} [orderby] Order in which results are returned |
| 93 |
* @apiParam (Query Parameter) {String="asc","desc"} [direction=desc] Direction to order the results by (ascending or descending) |
| 94 |
* @apiParam (Query Parameter) {Integer{1...200}} [per_page=25] Number of results per request |
| 95 |
* @apiParam (Query Parameter) {Integer} [page=0] Current page of results |
| 96 |
*/ |
| 97 |
|
| 98 |
/** |
| 99 |
* @apiDefine GroupItem |
| 100 |
* |
| 101 |
* @apiParam (JSON Body) {String} name Name of the group |
| 102 |
* @apiParam (JSON Body) {Integer="1","2","3"} moduleID Module ID of the group, with 1 being WordPress, 2 is Apache, and 3 is Nginx |
| 103 |
*/ |
| 104 |
|
| 105 |
/** |
| 106 |
* @apiDefine GroupList |
| 107 |
* |
| 108 |
* @apiSuccess {Object[]} items Array of group objects |
| 109 |
* @apiSuccess {Integer} items.id ID of group |
| 110 |
* @apiSuccess {String} items.name Name of this group |
| 111 |
* @apiSuccess {Boolean} items.enabled `true` if group (and redirects) are enabled, `false` otherwise |
| 112 |
* @apiSuccess {Integer} items.redirects Number of redirects in this group |
| 113 |
* @apiSuccess {String} items.moduleName Name of the module this group belongs to |
| 114 |
* @apiSuccess {Integer} items.module_id ID of the module this group belongs to |
| 115 |
* @apiSuccess {Integer} total Number of items |
| 116 |
* |
| 117 |
* @apiSuccessExample {json} Success 200: |
| 118 |
* HTTP/1.1 200 OK |
| 119 |
* { |
| 120 |
* "items": [ |
| 121 |
* { |
| 122 |
* "id": 3, |
| 123 |
* "enabled": true, |
| 124 |
* "moduleName": "WordPress", |
| 125 |
* "module_id": 1, |
| 126 |
* "name": "Redirections", |
| 127 |
* "redirects": 0, |
| 128 |
* } |
| 129 |
* ], |
| 130 |
* "total": 1 |
| 131 |
* } |
| 132 |
*/ |
| 133 |
|
| 134 |
/** |
| 135 |
* Group API endpoint |
| 136 |
*/ |
| 137 |
class Redirection_Api_Group extends Redirection_Api_Filter_Route { |
| 138 |
/** |
| 139 |
* 404 API endpoint constructor |
| 140 |
* |
| 141 |
* @param String $namespace Namespace. |
| 142 |
*/ |
| 143 |
public function __construct( $namespace ) { |
| 144 |
$orders = [ 'name', 'id', '' ]; |
| 145 |
$filters = [ 'status', 'module', 'name' ]; |
| 146 |
|
| 147 |
register_rest_route( $namespace, '/group', array( |
| 148 |
'args' => $this->get_filter_args( $orders, $filters ), |
| 149 |
$this->get_route( WP_REST_Server::READABLE, 'route_list', [ $this, 'permission_callback_manage' ] ), |
| 150 |
array_merge( |
| 151 |
$this->get_route( WP_REST_Server::EDITABLE, 'route_create', [ $this, 'permission_callback_add' ] ), |
| 152 |
array( 'args' => $this->get_group_args() ) |
| 153 |
), |
| 154 |
) ); |
| 155 |
|
| 156 |
register_rest_route( $namespace, '/group/(?P<id>[\d]+)', array( |
| 157 |
'args' => $this->get_group_args(), |
| 158 |
$this->get_route( WP_REST_Server::EDITABLE, 'route_update', [ $this, 'permission_callback_add' ] ), |
| 159 |
) ); |
| 160 |
|
| 161 |
register_rest_route( $namespace, '/bulk/group/(?P<bulk>delete|enable|disable)', array( |
| 162 |
$this->get_route( WP_REST_Server::EDITABLE, 'route_bulk', [ $this, 'permission_callback_bulk' ] ), |
| 163 |
'args' => array_merge( $this->get_filter_args( $orders, $filters ), [ |
| 164 |
'items' => [ |
| 165 |
'description' => 'Comma separated list of item IDs to perform action on', |
| 166 |
'type' => 'array', |
| 167 |
'items' => [ |
| 168 |
'description' => 'Item ID', |
| 169 |
'type' => [ 'string', 'number' ], |
| 170 |
], |
| 171 |
], |
| 172 |
] ), |
| 173 |
) ); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Checks a manage capability |
| 178 |
* |
| 179 |
* Access to group data is required by the CAP_GROUP_MANAGE and CAP_REDIRECT_MANAGE caps |
| 180 |
* |
| 181 |
* @param WP_REST_Request $request Request. |
| 182 |
* @return Bool |
| 183 |
*/ |
| 184 |
public function permission_callback_manage( WP_REST_Request $request ) { |
| 185 |
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_GROUP_MANAGE ) || Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_REDIRECT_MANAGE ); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Checks a bulk capability |
| 190 |
* |
| 191 |
* @param WP_REST_Request $request Request. |
| 192 |
* @return Bool |
| 193 |
*/ |
| 194 |
public function permission_callback_bulk( WP_REST_Request $request ) { |
| 195 |
if ( $request['bulk'] === 'delete' ) { |
| 196 |
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_GROUP_DELETE ); |
| 197 |
} |
| 198 |
|
| 199 |
return $this->permission_callback_add( $request ); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Checks a create capability |
| 204 |
* |
| 205 |
* @param WP_REST_Request $request Request. |
| 206 |
* @return Bool |
| 207 |
*/ |
| 208 |
public function permission_callback_add( WP_REST_Request $request ) { |
| 209 |
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_GROUP_ADD ); |
| 210 |
} |
| 211 |
|
| 212 |
private function get_group_args() { |
| 213 |
return array( |
| 214 |
'moduleId' => array( |
| 215 |
'description' => 'Module ID', |
| 216 |
'type' => 'integer', |
| 217 |
'minimum' => 0, |
| 218 |
'maximum' => 3, |
| 219 |
'required' => true, |
| 220 |
), |
| 221 |
'name' => array( |
| 222 |
'description' => 'Group name', |
| 223 |
'type' => 'string', |
| 224 |
'required' => true, |
| 225 |
), |
| 226 |
'status' => [ |
| 227 |
'description' => 'Status of the group', |
| 228 |
], |
| 229 |
); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Get group list |
| 234 |
* |
| 235 |
* @param WP_REST_Request $request The request. |
| 236 |
* @return WP_Error|array Return an array of results, or a WP_Error |
| 237 |
*/ |
| 238 |
public function route_list( WP_REST_Request $request ) { |
| 239 |
return Red_Group::get_filtered( $request->get_params() ); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Create a group |
| 244 |
* |
| 245 |
* @param WP_REST_Request $request The request. |
| 246 |
* @return WP_Error|array Return an array of results, or a WP_Error |
| 247 |
*/ |
| 248 |
public function route_create( WP_REST_Request $request ) { |
| 249 |
$params = $request->get_params( $request ); |
| 250 |
$name = ''; |
| 251 |
$module = 0; |
| 252 |
|
| 253 |
if ( isset( $params['name'] ) ) { |
| 254 |
$name = sanitize_text_field( $params['name'] ); |
| 255 |
} |
| 256 |
|
| 257 |
if ( isset( $params['moduleId'] ) ) { |
| 258 |
$module = intval( $params['moduleId'], 10 ); |
| 259 |
} |
| 260 |
|
| 261 |
$group = Red_Group::create( $name, $module ); |
| 262 |
|
| 263 |
if ( $group ) { |
| 264 |
return Red_Group::get_filtered( $params ); |
| 265 |
} |
| 266 |
|
| 267 |
return $this->add_error_details( new WP_Error( 'redirect_group_invalid', 'Invalid group or parameters' ), __LINE__ ); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Update a 404 |
| 272 |
* |
| 273 |
* @param WP_REST_Request $request The request. |
| 274 |
* @return WP_Error|array Return an array of results, or a WP_Error |
| 275 |
*/ |
| 276 |
public function route_update( WP_REST_Request $request ) { |
| 277 |
$params = $request->get_params( $request ); |
| 278 |
$group = Red_Group::get( intval( $request['id'], 10 ) ); |
| 279 |
|
| 280 |
if ( $group ) { |
| 281 |
$result = $group->update( $params ); |
| 282 |
|
| 283 |
if ( $result ) { |
| 284 |
return array( 'item' => $group->to_json() ); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
return $this->add_error_details( new WP_Error( 'redirect_group_invalid', 'Invalid group details' ), __LINE__ ); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Perform action on groups |
| 293 |
* |
| 294 |
* @param WP_REST_Request $request The request. |
| 295 |
* @return WP_Error|array Return an array of results, or a WP_Error |
| 296 |
*/ |
| 297 |
public function route_bulk( WP_REST_Request $request ) { |
| 298 |
$params = $request->get_params(); |
| 299 |
$action = $request['bulk']; |
| 300 |
|
| 301 |
$items = []; |
| 302 |
if ( isset( $params['items'] ) && is_array( $params['items'] ) ) { |
| 303 |
// Array of integers, sanitized below |
| 304 |
$items = $params['items']; |
| 305 |
} elseif ( isset( $params['global'] ) && $params['global'] ) { |
| 306 |
// Groups have additional actions that fire and so we need to action them individually |
| 307 |
$groups = Red_Group::get_all( $params ); |
| 308 |
$items = array_column( $groups, 'id' ); |
| 309 |
} |
| 310 |
|
| 311 |
foreach ( $items as $item ) { |
| 312 |
$group = Red_Group::get( intval( $item, 10 ) ); |
| 313 |
|
| 314 |
if ( is_object( $group ) ) { |
| 315 |
if ( $action === 'delete' ) { |
| 316 |
$group->delete(); |
| 317 |
} elseif ( $action === 'disable' ) { |
| 318 |
$group->disable(); |
| 319 |
} elseif ( $action === 'enable' ) { |
| 320 |
$group->enable(); |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
return $this->route_list( $request ); |
| 326 |
} |
| 327 |
} |
| 328 |
|