| 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 |
*/ |
| 81 |
|
| 82 |
/** |
| 83 |
* @apiDefine GroupQueryParams |
| 84 |
* |
| 85 |
* @apiParam (Query Parameter) {String} filterBy[name] Filter the results by the supplied name |
| 86 |
* @apiParam (Query Parameter) {String="enabled","disabled"} filterBy[status] Filter the results by the supplied status |
| 87 |
* @apiParam (Query Parameter) {Integer="1","2","3"} filterBy[module] Filter the results by the supplied module ID |
| 88 |
* @apiParam (Query Parameter) {String="name"} orderby Order in which results are returned |
| 89 |
* @apiParam (Query Parameter) {String="asc","desc"} direction Direction to order the results by (ascending or descending) |
| 90 |
* @apiParam (Query Parameter) {Integer{1...200}} per_page Number of results per request |
| 91 |
* @apiParam (Query Parameter) {Integer} page Current page of results |
| 92 |
*/ |
| 93 |
|
| 94 |
/** |
| 95 |
* @apiDefine GroupItem |
| 96 |
* |
| 97 |
* @apiParam (JSON Body) {String} name Name of the group |
| 98 |
* @apiParam (JSON Body) {Integer="1","2","3"} moduleID Module ID of the group, with 1 being WordPress, 2 is Apache, and 3 is Nginx |
| 99 |
*/ |
| 100 |
|
| 101 |
/** |
| 102 |
* @apiDefine GroupList |
| 103 |
* |
| 104 |
* @apiSuccess {Object[]} items Array of group objects |
| 105 |
* @apiSuccess {Integer} items.id ID of group |
| 106 |
* @apiSuccess {String} items.name Name of this group |
| 107 |
* @apiSuccess {Boolean} items.enabled `true` if group (and redirects) are enabled, `false` otherwise |
| 108 |
* @apiSuccess {Integer} items.redirects Number of redirects in this group |
| 109 |
* @apiSuccess {String} items.moduleName Name of the module this group belongs to |
| 110 |
* @apiSuccess {Integer} items.module_id ID of the module this group belongs to |
| 111 |
* @apiSuccess {Integer} total Number of items |
| 112 |
* |
| 113 |
* @apiSuccessExample {json} Success 200: |
| 114 |
* HTTP/1.1 200 OK |
| 115 |
* { |
| 116 |
* "items": [ |
| 117 |
* { |
| 118 |
* "id": 3, |
| 119 |
* "enabled": true, |
| 120 |
* "moduleName": "WordPress", |
| 121 |
* "module_id": 1, |
| 122 |
* "name": "Redirections", |
| 123 |
* "redirects": 0, |
| 124 |
* } |
| 125 |
* ], |
| 126 |
* "total": 1 |
| 127 |
* } |
| 128 |
*/ |
| 129 |
class Redirection_Api_Group extends Redirection_Api_Filter_Route { |
| 130 |
public function __construct( $namespace ) { |
| 131 |
$orders = [ 'name', 'id' ]; |
| 132 |
$filters = [ 'status', 'module', 'name' ]; |
| 133 |
|
| 134 |
register_rest_route( $namespace, '/group', array( |
| 135 |
'args' => $this->get_filter_args( $orders, $filters ), |
| 136 |
$this->get_route( WP_REST_Server::READABLE, 'route_list', [ $this, 'permission_callback_manage' ] ), |
| 137 |
array_merge( |
| 138 |
$this->get_route( WP_REST_Server::EDITABLE, 'route_create', [ $this, 'permission_callback_add' ] ), |
| 139 |
array( 'args' => $this->get_group_args() ) |
| 140 |
), |
| 141 |
) ); |
| 142 |
|
| 143 |
register_rest_route( $namespace, '/group/(?P<id>[\d]+)', array( |
| 144 |
'args' => $this->get_group_args(), |
| 145 |
$this->get_route( WP_REST_Server::EDITABLE, 'route_update', [ $this, 'permission_callback_add' ] ), |
| 146 |
) ); |
| 147 |
|
| 148 |
$this->register_bulk( $namespace, '/bulk/group/(?P<bulk>delete|enable|disable)', $orders, 'route_bulk', [ $this, 'permission_callback_bulk' ] ); |
| 149 |
} |
| 150 |
|
| 151 |
// Access to group data is required by the CAP_GROUP_MANAGE and CAP_REDIRECT_MANAGE caps |
| 152 |
public function permission_callback_manage( WP_REST_Request $request ) { |
| 153 |
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_GROUP_MANAGE ) || Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_REDIRECT_MANAGE ); |
| 154 |
} |
| 155 |
|
| 156 |
public function permission_callback_bulk( WP_REST_Request $request ) { |
| 157 |
if ( $request['bulk'] === 'delete' ) { |
| 158 |
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_GROUP_DELETE ); |
| 159 |
} |
| 160 |
|
| 161 |
return $this->permission_callback_add( $request ); |
| 162 |
} |
| 163 |
|
| 164 |
public function permission_callback_add( WP_REST_Request $request ) { |
| 165 |
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_GROUP_ADD ); |
| 166 |
} |
| 167 |
|
| 168 |
private function get_group_args() { |
| 169 |
return array( |
| 170 |
'moduleId' => array( |
| 171 |
'description' => 'Module ID', |
| 172 |
'type' => 'integer', |
| 173 |
'minimum' => 0, |
| 174 |
'maximum' => 3, |
| 175 |
'required' => true, |
| 176 |
), |
| 177 |
'name' => array( |
| 178 |
'description' => 'Group name', |
| 179 |
'type' => 'string', |
| 180 |
'required' => true, |
| 181 |
), |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
public function route_list( WP_REST_Request $request ) { |
| 186 |
return Red_Group::get_filtered( $request->get_params() ); |
| 187 |
} |
| 188 |
|
| 189 |
public function route_create( WP_REST_Request $request ) { |
| 190 |
$params = $request->get_params( $request ); |
| 191 |
$group = Red_Group::create( isset( $params['name'] ) ? $params['name'] : '', isset( $params['moduleId'] ) ? $params['moduleId'] : 0 ); |
| 192 |
|
| 193 |
if ( $group ) { |
| 194 |
return Red_Group::get_filtered( $params ); |
| 195 |
} |
| 196 |
|
| 197 |
return $this->add_error_details( new WP_Error( 'redirect_group_invalid', 'Invalid group or parameters' ), __LINE__ ); |
| 198 |
} |
| 199 |
|
| 200 |
public function route_update( WP_REST_Request $request ) { |
| 201 |
$params = $request->get_params( $request ); |
| 202 |
$group = Red_Group::get( intval( $request['id'], 10 ) ); |
| 203 |
|
| 204 |
if ( $group ) { |
| 205 |
$result = $group->update( $params ); |
| 206 |
|
| 207 |
if ( $result ) { |
| 208 |
return array( 'item' => $group->to_json() ); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
return $this->add_error_details( new WP_Error( 'redirect_group_invalid', 'Invalid group details' ), __LINE__ ); |
| 213 |
} |
| 214 |
|
| 215 |
public function route_bulk( WP_REST_Request $request ) { |
| 216 |
$action = $request['bulk']; |
| 217 |
$items = $request['items']; |
| 218 |
|
| 219 |
foreach ( $items as $item ) { |
| 220 |
$group = Red_Group::get( intval( $item, 10 ) ); |
| 221 |
|
| 222 |
if ( $group ) { |
| 223 |
if ( $action === 'delete' ) { |
| 224 |
$group->delete(); |
| 225 |
} elseif ( $action === 'disable' ) { |
| 226 |
$group->disable(); |
| 227 |
} elseif ( $action === 'enable' ) { |
| 228 |
$group->enable(); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return $this->route_list( $request ); |
| 234 |
} |
| 235 |
} |
| 236 |
|