PluginProbe
Redirection / 4.3.2
Redirection v4.3.2
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.3.2, at api/api-group.php

113 lines 3.0 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 list of groups
5 * @apiDescription Get list of groups
6 * @apiGroup Group
7 *
8 * @apiParam {string} orderby
9 * @apiParam {string} direction
10 * @apiParam {string} filter
11 * @apiParam {string} per_page
12 * @apiParam {string} page
13 *
14 * @apiSuccess {Array} ip Array of groups
15 * @apiSuccess {Integer} total Number of items
16 *
17 * @apiUse 400Error
18 */
19 class Redirection_Api_Group extends Redirection_Api_Filter_Route {
20 public function __construct( $namespace ) {
21 $filters = array( 'name', 'module' );
22 $orders = array( 'name', 'id' );
23
24 register_rest_route( $namespace, '/group', array(
25 'args' => $this->get_filter_args( $filters, $orders ),
26 $this->get_route( WP_REST_Server::READABLE, 'route_list' ),
27 array_merge(
28 $this->get_route( WP_REST_Server::EDITABLE, 'route_create' ),
29 array( 'args' => $this->get_group_args() )
30 ),
31 ) );
32
33 register_rest_route( $namespace, '/group/(?P<id>[\d]+)', array(
34 'args' => $this->get_group_args(),
35 $this->get_route( WP_REST_Server::EDITABLE, 'route_update' ),
36 ) );
37
38 $this->register_bulk( $namespace, '/bulk/group/(?P<bulk>delete|enable|disable)', $filters, $orders, 'route_bulk' );
39 }
40
41 private function get_group_args() {
42 return array(
43 'moduleId' => array(
44 'description' => 'Module ID',
45 'type' => 'integer',
46 'minimum' => 0,
47 'maximum' => 3,
48 'required' => true,
49 ),
50 'name' => array(
51 'description' => 'Group name',
52 'type' => 'string',
53 'required' => true,
54 ),
55 );
56 }
57
58 public function route_list( WP_REST_Request $request ) {
59 return Red_Group::get_filtered( $request->get_params() );
60 }
61
62 public function route_create( WP_REST_Request $request ) {
63 $params = $request->get_params( $request );
64 $group = Red_Group::create( isset( $params['name'] ) ? $params['name'] : '', isset( $params['moduleId'] ) ? $params['moduleId'] : 0 );
65
66 if ( $group ) {
67 return Red_Group::get_filtered( $params );
68 }
69
70 return $this->add_error_details( new WP_Error( 'redirect', 'Invalid group or parameters' ), __LINE__ );
71 }
72
73 public function route_update( WP_REST_Request $request ) {
74 $params = $request->get_params( $request );
75 $group = Red_Group::get( intval( $request['id'], 10 ) );
76
77 if ( $group ) {
78 $result = $group->update( $params );
79
80 if ( $result ) {
81 return array( 'item' => $group->to_json() );
82 }
83 }
84
85 return $this->add_error_details( new WP_Error( 'redirect', 'Invalid group details' ), __LINE__ );
86 }
87
88 public function route_bulk( WP_REST_Request $request ) {
89 $action = $request['bulk'];
90 $items = explode( ',', $request['items'] );
91
92 if ( is_array( $items ) ) {
93 foreach ( $items as $item ) {
94 $group = Red_Group::get( intval( $item, 10 ) );
95
96 if ( $group ) {
97 if ( $action === 'delete' ) {
98 $group->delete();
99 } elseif ( $action === 'disable' ) {
100 $group->disable();
101 } elseif ( $action === 'enable' ) {
102 $group->enable();
103 }
104 }
105 }
106
107 return $this->route_list( $request );
108 }
109
110 return $this->add_error_details( new WP_Error( 'redirect', 'Invalid array of items' ), __LINE__ );
111 }
112 }
113