PluginProbe
Redirection / 4.2.1
Redirection v4.2.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.2.1, at api/api-group.php

97 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Redirection_Api_Group extends Redirection_Api_Filter_Route {
4 public function __construct( $namespace ) {
5 $filters = array( 'name', 'module' );
6 $orders = array( 'name', 'id' );
7
8 register_rest_route( $namespace, '/group', array(
9 'args' => $this->get_filter_args( $filters, $orders ),
10 $this->get_route( WP_REST_Server::READABLE, 'route_list' ),
11 array_merge(
12 $this->get_route( WP_REST_Server::EDITABLE, 'route_create' ),
13 array( 'args' => $this->get_group_args() )
14 ),
15 ) );
16
17 register_rest_route( $namespace, '/group/(?P<id>[\d]+)', array(
18 'args' => $this->get_group_args(),
19 $this->get_route( WP_REST_Server::EDITABLE, 'route_update' ),
20 ) );
21
22 $this->register_bulk( $namespace, '/bulk/group/(?P<bulk>delete|enable|disable)', $filters, $orders, 'route_bulk' );
23 }
24
25 private function get_group_args() {
26 return array(
27 'moduleId' => array(
28 'description' => 'Module ID',
29 'type' => 'integer',
30 'minimum' => 0,
31 'maximum' => 3,
32 'required' => true,
33 ),
34 'name' => array(
35 'description' => 'Group name',
36 'type' => 'string',
37 'required' => true,
38 ),
39 );
40 }
41
42 public function route_list( WP_REST_Request $request ) {
43 return Red_Group::get_filtered( $request->get_params() );
44 }
45
46 public function route_create( WP_REST_Request $request ) {
47 $params = $request->get_params( $request );
48 $group = Red_Group::create( isset( $params['name'] ) ? $params['name'] : '', isset( $params['moduleId'] ) ? $params['moduleId'] : 0 );
49
50 if ( $group ) {
51 return Red_Group::get_filtered( $params );
52 }
53
54 return $this->add_error_details( new WP_Error( 'redirect', 'Invalid group or parameters' ), __LINE__ );
55 }
56
57 public function route_update( WP_REST_Request $request ) {
58 $params = $request->get_params( $request );
59 $group = Red_Group::get( intval( $request['id'], 10 ) );
60
61 if ( $group ) {
62 $result = $group->update( $params );
63
64 if ( $result ) {
65 return array( 'item' => $group->to_json() );
66 }
67 }
68
69 return $this->add_error_details( new WP_Error( 'redirect', 'Invalid group details' ), __LINE__ );
70 }
71
72 public function route_bulk( WP_REST_Request $request ) {
73 $action = $request['bulk'];
74 $items = explode( ',', $request['items'] );
75
76 if ( is_array( $items ) ) {
77 foreach ( $items as $item ) {
78 $group = Red_Group::get( intval( $item, 10 ) );
79
80 if ( $group ) {
81 if ( $action === 'delete' ) {
82 $group->delete();
83 } elseif ( $action === 'disable' ) {
84 $group->disable();
85 } elseif ( $action === 'enable' ) {
86 $group->enable();
87 }
88 }
89 }
90
91 return $this->route_list( $request );
92 }
93
94 return $this->add_error_details( new WP_Error( 'redirect', 'Invalid array of items' ), __LINE__ );
95 }
96 }
97