| Server IP : 188.114.96.2 / Your IP : 104.23.197.230 Web Server : Apache/2.4.59 (Debian) System : Linux EDL-STRETCH 4.19.0-27-amd64 #1 SMP Debian 4.19.316-1 (2024-06-25) x86_64 User : edlftp ( 1002) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/nicolasj/www/blog.ecoledelangues.be/wp-content/plugins/redirection/api/ |
Upload File : |
<?php
/**
* @api {get} /redirection/v1/group Get list of groups
* @apiDescription Get list of groups
* @apiGroup Group
*
* @apiParam {string} orderby
* @apiParam {string} direction
* @apiParam {string} filter
* @apiParam {string} per_page
* @apiParam {string} page
*
* @apiSuccess {Array} ip Array of groups
* @apiSuccess {Integer} total Number of items
*
* @apiUse 400Error
*/
class Redirection_Api_Group extends Redirection_Api_Filter_Route {
public function __construct( $namespace ) {
$filters = array( 'name', 'module' );
$orders = array( 'name', 'id' );
register_rest_route( $namespace, '/group', array(
'args' => $this->get_filter_args( $filters, $orders ),
$this->get_route( WP_REST_Server::READABLE, 'route_list' ),
array_merge(
$this->get_route( WP_REST_Server::EDITABLE, 'route_create' ),
array( 'args' => $this->get_group_args() )
),
) );
register_rest_route( $namespace, '/group/(?P<id>[\d]+)', array(
'args' => $this->get_group_args(),
$this->get_route( WP_REST_Server::EDITABLE, 'route_update' ),
) );
$this->register_bulk( $namespace, '/bulk/group/(?P<bulk>delete|enable|disable)', $filters, $orders, 'route_bulk' );
}
private function get_group_args() {
return array(
'moduleId' => array(
'description' => 'Module ID',
'type' => 'integer',
'minimum' => 0,
'maximum' => 3,
'required' => true,
),
'name' => array(
'description' => 'Group name',
'type' => 'string',
'required' => true,
),
);
}
public function route_list( WP_REST_Request $request ) {
return Red_Group::get_filtered( $request->get_params() );
}
public function route_create( WP_REST_Request $request ) {
$params = $request->get_params( $request );
$group = Red_Group::create( isset( $params['name'] ) ? $params['name'] : '', isset( $params['moduleId'] ) ? $params['moduleId'] : 0 );
if ( $group ) {
return Red_Group::get_filtered( $params );
}
return $this->add_error_details( new WP_Error( 'redirect', 'Invalid group or parameters' ), __LINE__ );
}
public function route_update( WP_REST_Request $request ) {
$params = $request->get_params( $request );
$group = Red_Group::get( intval( $request['id'], 10 ) );
if ( $group ) {
$result = $group->update( $params );
if ( $result ) {
return array( 'item' => $group->to_json() );
}
}
return $this->add_error_details( new WP_Error( 'redirect', 'Invalid group details' ), __LINE__ );
}
public function route_bulk( WP_REST_Request $request ) {
$action = $request['bulk'];
$items = explode( ',', $request['items'] );
if ( is_array( $items ) ) {
foreach ( $items as $item ) {
$group = Red_Group::get( intval( $item, 10 ) );
if ( $group ) {
if ( $action === 'delete' ) {
$group->delete();
} elseif ( $action === 'disable' ) {
$group->disable();
} elseif ( $action === 'enable' ) {
$group->enable();
}
}
}
return $this->route_list( $request );
}
return $this->add_error_details( new WP_Error( 'redirect', 'Invalid array of items' ), __LINE__ );
}
}